Advertisement
Guest User

BF

a guest
May 29th, 2009
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. prog= """
  2.  
  3. ++>++<-->--< Simple Python Brainfuck interpreter
  4.  
  5. """
  6. cells,pointer,pos,loops={},0,0,0
  7. while pos < len(prog):
  8. #    print(prog)
  9. #    print(" "*pos+"I")
  10.  
  11.     command = prog[pos]
  12.     if   command == ">":
  13.         pointer +=1
  14.     elif command == "<":
  15.         pointer -=1
  16.     elif command == "-":
  17.         cells[pointer] -=1
  18.     elif command == "+":
  19.         if pointer in cells: cells[pointer] += 1
  20.         else: cells[pointer] = 1
  21.     elif command == ".":
  22.         print(chr(cells[pointer]),end='')
  23.     elif command == ",":
  24.         cells[pointer] = ord(input("Input a single character: "))
  25.     elif command == "[":
  26.         if not pointer in cells or cells[pointer] == 0:
  27.             loops = 1
  28.             while loops > 0:
  29.                 pos += 1
  30.                 command = prog[pos]
  31.                 if   command == "[":
  32.                     loops +=1
  33.                 elif command == "]":
  34.                     loops -=1
  35.     elif command == "]":
  36.         if pointer in cells and cells[pointer]!=0:
  37.             loops = 1
  38.             while loops > 0:
  39.                 pos -= 1
  40.                 command = prog[pos]
  41.                 if   command == "[":
  42.                     loops -=1
  43.                 elif command == "]":
  44.                     loops +=1
  45.     pos +=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement