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

From Rosetta Code
Content added Content deleted
(New page: {{implementation|Brainf***}}{{collection|RCBF}}Category:Python Quick implementation of a Brainfuck interpreter in Python. <python> #!/usr/bin/python from __future__ import with_...)
 
No edit summary
Line 12: Line 12:
class BrainFuck():
class BrainFuck():
def __init__(self):
def __init__(self):
self.instructions = []
self.instructions = ''
def main(self):
def main(self):
if len(sys.argv[1]) > 0:
if len(sys.argv[1:]) > 0:
source_file = sys.argv[1]
source_file = sys.argv[1]
with open(source_file) as source_handle:
with open(source_file) as source_handle:
self.instructions = [char for char in source_handle.read()]
self.instructions = source_handle.read()
else:
else:
print 'No source file.'
print >> sys.stderr, 'No source file.'
sys.exit(2)
sys.exit(2)
Line 27: Line 27:
def match_braces(self):
def match_braces(self):
loops = {}
loops = {}
loop_stack = {}
loop_stack = []
lsptr = 0
ptr = 0
for instruction in self.instructions:
for ptr, instruction in enumerate(self.instructions):
if instruction == '[':
if instruction == '[':
loop_stack[lsptr] = ptr
loop_stack.append(ptr)
lsptr += 1
elif instruction == ']':
elif instruction == ']':
lsptr -= 1
startptr = loop_stack.pop()
startptr = loop_stack[lsptr]
loops[startptr] = ptr
loops[startptr] = ptr
loops[ptr] = startptr
loops[ptr] = startptr
ptr += 1
self.start_interpreting(loops)
self.start_interpreting(loops)
def start_interpreting(self, loops):
def start_interpreting(self, loops):
tape = [0 for x in range(0, 30000)]
tape = [0] * 30000
cell = 0
cell = 0
pointer = 0
pointer = 0
while True:
while pointer < len(self.instructions):
if pointer > len(self.instructions) - 1:
break
instruction = self.instructions[pointer]
instruction = self.instructions[pointer]
Line 69: Line 60:
sys.stdout.write(chr(tape[cell]))
sys.stdout.write(chr(tape[cell]))
elif instruction == '[' and tape[cell] == 0:
elif instruction == '[' and tape[cell] == 0:
pointer = loops.get(pointer)
pointer = loops[pointer]
elif instruction == ']' and tape[cell] != 0:
elif instruction == ']' and tape[cell] != 0:
pointer = loops.get(pointer)
pointer = loops[pointer]
pointer += 1
pointer += 1

Revision as of 06:38, 7 January 2009

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.

Quick implementation of a Brainfuck interpreter in Python.

<python>

  1. !/usr/bin/python

from __future__ import with_statement

import sys

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 == ']':
               startptr = loop_stack.pop()
               loops[startptr] = ptr
               loops[ptr] = startptr
       
       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()

</python>