Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
  2.  
  3. program_index = 0
  4.  
  5. data = []
  6. for i in range(0, 30000):
  7. data.append(0)
  8.  
  9. data_index = 0
  10.  
  11.  
  12. while program_index < len(program):
  13. command = program[program_index]
  14.  
  15. if command == '>':
  16. data_index += 1
  17. elif command == '<':
  18. data_index -= 1
  19. elif command == '+':
  20. data[data_index] += 1
  21. elif command == '-':
  22. data[data_index] -= 1
  23. elif command == '.':
  24. print(chr(data[data_index]))
  25. elif command == ',':
  26. user_input = ord(raw_input()[0])
  27. data[data_index] = user_input
  28. elif command == '[':
  29. if data[data_index] == 0:
  30. nesting_counter = 0
  31. while nesting_counter > 0 or program[program_index] != ']':
  32. if program[program_index] == '[':
  33. nesting_counter += 1
  34. elif program[program_index] == ']':
  35. nesting_counter -= 1
  36. program_index += 1
  37. elif command == ']':
  38. if data[data_index] != 0:
  39. nesting_counter = 0
  40. program_index -= 1
  41. while nesting_counter > 0 or program[program_index] != '[':
  42. if program[program_index] == ']':
  43. nesting_counter += 1
  44. elif program[program_index] == '[':
  45. nesting_counter -= 1
  46.  
  47. program_index -= 1
  48.  
  49. program_index += 1
  50.  
  51.  
  52. print('Program complete.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement