Skip to content

Commit

Permalink
brainf*** interpreter with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jtauber committed Jul 27, 2012
1 parent f0fd3fe commit ddf88e1
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions brainf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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()

0 comments on commit ddf88e1

Please sign in to comment.