""" This program is a BrainF**k interpreter. Copyright (C) 2004 John Bauman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ import fileinput import psyco import msvcrt import sys psyco.full() program = "" for line in fileinput.input(): program += (line) currloc = 0 memory = [0] memptr = 0 stack = [] while True: #print currloc pch = program[currloc] if pch == "+": memory[memptr] += 1 if pch == "-": memory[memptr] -= 1 if pch == ">": memptr += 1 if memptr >= len(memory): memory.extend([0] * 10) if pch == "<": memptr -= 1 if memptr < 0: print "Memory pointer toofar left!" if pch == "[": if memory[memptr] != 0: stack.append(currloc) else: stackp = 1 while stackp > 0: currloc += 1 if program[currloc] == "[": stackp += 1 if program[currloc] == "]": stackp -= 1 if pch == "]": if memory[memptr] != 0: currloc = stack[-1] else: stack.pop() if pch == ",": val = msvcrt.getche() if ord(val) == 3: break memory[memptr] = ord(val) if pch == ".": sys.stdout.write(chr(memory[memptr])) currloc += 1 if currloc >= len(program): break