Created
October 24, 2013 21:01
-
-
Save mdamien/7144876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def execute(code): | |
code = ''.join([x for x in code if x in '.,+-<>[]']) | |
cells,ptr,i = [0]*3000,0,0 | |
while i < len(code): | |
c = code[i] | |
if c == '>': ptr += 1 | |
if c == '<': ptr -= 1 | |
if c == '+': cells[ptr] += 1 | |
if c == '-': cells[ptr] -= 1 | |
if c == '.': sys.stdout.write(chr(cells[ptr])) | |
if c == ',': cells[ptr] = ord(sys.stdin.read(1)) | |
if c == '[' and cells[ptr] == 0: i = code.find("]",i) | |
if c == ']': i = code[:i].find('[')-1 | |
i += 1 | |
if __name__ == '__main__': | |
hello_world = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." | |
execute(hello_world) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bug on lines 14 and 15. think about nested loops.