Skip to content

Instantly share code, notes, and snippets.

@giovaneliberato
Created May 27, 2013 23:45
Show Gist options
  • Save giovaneliberato/5659598 to your computer and use it in GitHub Desktop.
Save giovaneliberato/5659598 to your computer and use it in GitHub Desktop.
Basic brainfuck interpreter written in python
salt = 65
def incr():
compiled[pointer] += 1
def decr():
compiled[pointer] -= 1
def fw():
global pointer
pointer += 1
def bw():
global pointer
pointer -= 1
def print_v():
result = ''
for i in compiled:
result += unichr(i + salt) if i > -1 else '0'
print('... %s' %result)
functions = {"+": incr, "-": decr, ">": fw, "<":bw, ".": print_v}
while(1):
compiled = [-1]
pointer = 0
code = raw_input('-> ')
[compiled.append(-1) for char in code if char not in '.[]+-<']
try:
[functions[char]() for char in code]
except:
print("!!! error")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment