Execute Brain****/Python: Difference between revisions
Content added Content deleted
m (moved RCBF/Python to Execute Brain****/Python) |
(Neater code, (almost) infinite tape, 'exit' opcode (']' outside of any loop).) |
||
Line 1: | Line 1: | ||
{{implementation|Brainf***}}{{collection|RCBF}} |
{{implementation|Brainf***}}{{collection|RCBF}} |
||
Implementation of a [[Brainfuck]] interpreter in [[Python]]. |
|||
<lang python>#!/usr/bin/python |
<lang python>#!/usr/bin/python |
||
⚫ | |||
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() |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
loop_ptrs[ptr], loop_ptrs[sptr] = sptr, ptr |
|||
if loop_stack: |
|||
raise SyntaxError ("unclosed loops at {}".format(loop_stack)) |
|||
tape = collections.defaultdict(int) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
opcode = source[ptr] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
elif (opcode == '[' and tape[cell]) or \ |
|||
(opcode == ']' and not tape[cell]): ptr = loop_ptrs[ptr] |
|||
⚫ | |||
⚫ | |||
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.' |
|||
⚫ | |||
⚫ | |||
self.match_braces() |
|||
def match_braces(self): |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
elif instruction == ']':loops[ptr],loops[loops[ptr]] = loop_stack.pop(),ptr |
|||
self.start_interpreting(loops) |
|||
def start_interpreting(self, loops): |
|||
tape = [0] * 30000 |
|||
⚫ | |||
pointer = 0 |
|||
⚫ | |||
instruction = self.instructions[pointer] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
elif instruction == '<': |
|||
⚫ | |||
elif instruction == '+': |
|||
⚫ | |||
elif instruction == '-': |
|||
⚫ | |||
elif instruction == ',': |
|||
⚫ | |||
elif instruction == '.': |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
pointer = loops[pointer] |
|||
pointer += 1 |
|||
⚫ | |||
interpreter = BrainFuck() |
|||
interpreter.main()</lang> |
Revision as of 22:17, 12 February 2011
Execute Brain****/Python is an implementation of Brainf***.
Other implementations of Brainf***.
Execute Brain****/Python is part of RCBF. You may find other members of RCBF at Category:RCBF.
Implementation of a Brainfuck interpreter in Python.
<lang python>#!/usr/bin/python import sys import collections
def brainfuck (filename=None):
fd = filename or (open(sys.argv[1]) if sys.argv[1:] else sys.stdin) source = fd.read() loop_ptrs = {} loop_stack = [] for ptr, opcode in enumerate(source): if opcode == '[': loop_stack.append(ptr) if opcode == ']': if not loop_stack: source = source[:ptr] break sptr = loop_stack.pop() 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 ptr < len(source): opcode = source[ptr] if opcode == '>': cell += 1 elif opcode == '<': cell -= 1 elif opcode == '+': tape[cell] += 1 elif opcode == '-': tape[cell] -= 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>