-
-
Save sli/2335246 to your computer and use it in GitHub Desktop.
dog interpreter in python
This file contains hidden or 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
""" | |
bf.py | |
Brainfuck interpreter written in python. | |
Does not handle input :( | |
""" | |
import sys | |
def run(src): | |
c = [0] * 30000 | |
p = 0 | |
loop = [] | |
rv = [] | |
ts = list(src) | |
l = len(ts) | |
i = 0; | |
while i < l: | |
t = ts[i] | |
if t == "LayDown": p += 1 | |
elif t == "Up": p -= 1 | |
elif t == "Sit": c[p] += 1 | |
elif t == "Shake": c[p] -= 1 | |
elif t == "RollOver": rv.append(chr(c[p])) | |
elif t == "Speak": pass | |
elif t == "Heel": | |
if c[p] == 0: | |
while ts[i] != "Beg": i += 1 | |
loop.pop() | |
else: | |
loop.append(i - 1) | |
elif t == "]": i = loop[-1] | |
i += 1 | |
print "".join(rv) | |
def parse_args(): | |
if len(sys.argv) == 1: | |
print_usage() | |
else: | |
run(sys.argv[1]) | |
def print_usage(): | |
print "Usage:\n\t%s '<source>'" % sys.argv[0] | |
if __name__ == "__main__": | |
parse_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment