Skip to content

Instantly share code, notes, and snippets.

@jtauber
Created August 13, 2009 05:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtauber/167007 to your computer and use it in GitHub Desktop.
Save jtauber/167007 to your computer and use it in GitHub Desktop.
# brainf*** Interpreter in Python
# Copyright (c) 2002 & 2006, James Tauber
#
# see http://en.wikipedia.org/wiki/Brainfuck
import sys
class brainf:
def __init__(self, program):
self.mem = [0] * 65536
self.p = 0
self.pc = 0
self.program = program
def run(self, stop=None):
if stop is None:
stop = len(self.program)
while self.pc < stop:
c = self.program[self.pc]
if c == ">": self.p += 1
elif c == "<": self.p -= 1
elif c == "+": self.mem[self.p] += 1
elif c == "-": self.mem[self.p] -= 1
elif c == ".": sys.stdout.write(chr(self.mem[self.p]))
elif c == ",": self.mem[self.p] = ord(sys.stdin.read(1))
elif c == "[":
depth = 1
start = end = self.pc
while depth:
end += 1
if self.program[end] == "[": depth += 1
if self.program[end] == "]": depth -= 1
while self.mem[self.p]:
self.pc = start + 1
self.run(end)
self.pc = end
elif c == "]": raise "unbalanced ]"
else: pass
self.pc += 1
# hello world sample
hello_world = """
>+++++++++[<++++++++>-]<.
>+++++++[<++++>-]<+.
+++++++.
.
+++.
[-]>++++++++[<++++>-]<.
>+++++++++++[<++++++++>-]<-.
--------.
+++.
------.
--------.
[-]>++++++++[<++++>-]<+.
[-]++++++++++.
"""
# add two one-digit numbers to make a third (if one-digit)
add = """
,>++++++[<-------->-],[<+>-],<.>.
"""
brainf(hello_world).run()
brainf(add).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment