Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- prog= """
- ++>++<-->--< Simple Python Brainfuck interpreter
- """
- cells,pointer,pos,loops={},0,0,0
- while pos < len(prog):
- # print(prog)
- # print(" "*pos+"I")
- command = prog[pos]
- if command == ">":
- pointer +=1
- elif command == "<":
- pointer -=1
- elif command == "-":
- cells[pointer] -=1
- elif command == "+":
- if pointer in cells: cells[pointer] += 1
- else: cells[pointer] = 1
- elif command == ".":
- print(chr(cells[pointer]),end='')
- elif command == ",":
- cells[pointer] = ord(input("Input a single character: "))
- elif command == "[":
- if not pointer in cells or cells[pointer] == 0:
- loops = 1
- while loops > 0:
- pos += 1
- command = prog[pos]
- if command == "[":
- loops +=1
- elif command == "]":
- loops -=1
- elif command == "]":
- if pointer in cells and cells[pointer]!=0:
- loops = 1
- while loops > 0:
- pos -= 1
- command = prog[pos]
- if command == "[":
- loops -=1
- elif command == "]":
- loops +=1
- pos +=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement