Execute Brain****/Python: Difference between revisions

Neater code, (almost) infinite tape, 'exit' opcode (']' outside of any loop).
(Neater code, (almost) infinite tape, 'exit' opcode (']' outside of any loop).)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
 
Quick implementationImplementation of a [[Brainfuck]] interpreter in [[Python]].
 
<lang python>#!/usr/bin/python
import sys
import collections
 
def brainfuck (filename=None):
from __future__ import with_statement
fd = filename or (open(sys.argv[1]) if sys.argv[1:] else sys.stdin)
 
source = fd.read()
import sys
loopsloop_ptrs = {}
loop_stack = []
for ptr, instructionopcode in enumerate(self.instructionssource):
if instructionopcode == '[': loop_stack.append(ptr)
if instructionopcode == '>]':
if not cell += 1loop_stack:
pointersource = loopssource[pointer:ptr]
cell -= 1break
syssptr = loop_stack.exitpop(2)
loop_ptrs[ptr], loop_ptrs[sptr] = sptr, ptr
if loop_stack:
raise SyntaxError ("unclosed loops at {}".format(loop_stack))
tape = collections.defaultdict(int)
cell = 0
ptr = 0
while pointerptr < len(self.instructionssource):
opcode = source[ptr]
if opcode == '>': tape[cell] += 1
elif instructionopcode == '[<': and tape[cell] =-= 0:1
elif opcode == '+': tape[cell] -+= 1
elif instructionopcode == ']-' and: tape[cell] !-= 0:1
elif opcode == ',': tape[cell] = ord(sys.stdin.read(1))
elif opcode == '.': sys.stdout.write(chr(tape[cell]))
elif (opcode == '[' and tape[cell]) or \
(opcode == ']' and not tape[cell]): ptr = loop_ptrs[ptr]
ptr += 1
 
if __name__ == "__main__": brainfuck()</lang>
class BrainFuck():
def __init__(self):
self.instructions = ''
def main(self):
if len(sys.argv[1:]) > 0:
source_file = sys.argv[1]
with open(source_file) as source_handle:
self.instructions = source_handle.read()
else:
print >> sys.stderr, 'No source file.'
sys.exit(2)
self.match_braces()
def match_braces(self):
loops = {}
loop_stack = []
for ptr, instruction in enumerate(self.instructions):
if instruction == '[':loop_stack.append(ptr)
elif instruction == ']':loops[ptr],loops[loops[ptr]] = loop_stack.pop(),ptr
self.start_interpreting(loops)
def start_interpreting(self, loops):
tape = [0] * 30000
cell = 0
pointer = 0
while pointer < len(self.instructions):
instruction = self.instructions[pointer]
if instruction == '>':
cell += 1
elif instruction == '<':
cell -= 1
elif instruction == '+':
tape[cell] += 1
elif instruction == '-':
tape[cell] -= 1
elif instruction == ',':
tape[cell] = ord(sys.stdin.read(1))
elif instruction == '.':
sys.stdout.write(chr(tape[cell]))
elif instruction == '[' and tape[cell] == 0:
pointer = loops[pointer]
elif instruction == ']' and tape[cell] != 0:
pointer = loops[pointer]
pointer += 1
if __name__ == "__main__":
interpreter = BrainFuck()
interpreter.main()</lang>
Anonymous user