Skip to content

Commit

Permalink
restructered code as well as added the ability to import code in the …
Browse files Browse the repository at this point in the history
…REPL
  • Loading branch information
joelbm24 committed Mar 16, 2013
1 parent df0a4b6 commit ac4fe9b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -21,3 +21,9 @@ To run type:
or

$ brainy <file name>

I also recommend using rlwrap, it adds readline functionality.

With rlwrap you would type

$ rlwrap brainy
56 changes: 39 additions & 17 deletions bin/brainy
Expand Up @@ -22,27 +22,40 @@ import sys
from brainy import bfinter
bf = bfinter.Brainy()

if len(sys.argv) > 1:
try: f = open(sys.argv[1])
def import_script(file_name, exit=True):
try:
f = open(file_name)
bf.eval(f.read())
f.close()
except:
print "File not found"
quit()
if exit:
print "ERROR: File not found"
quit()
else:
print "ERROR: File not found"
pass

def display():
if bf.cur_cell > bf.tape_end:
bf.tape_end = bf.cur_cell
bf.tape_start = bf.tape_end - bf.tape_length
print bf.get_tape(bf.tape_start, bf.tape_end)

code = f.read()
f.close()
bf.eval(code)
if len(sys.argv) > 1:
import_script(sys.argv[1])
quit()


print "Welcome to the Brainy REPL"
print "For help type 'h'. To quit just type 'q' or press ctrl+d\n"
print '''Brainy Copyright (C) 2012 Joel Buchheim-Moore
print '''Brainy Copyright (C) 2012-2013 Joel Buchheim-Moore
This program comes with ABSOLUTELY NO WARRANTY; for details type `w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `c' for details.
'''
user_in = ''
tape_start = 0
tape_end = 10



while True:
try: user_in = raw_input(">>> ")
Expand All @@ -64,18 +77,27 @@ while True:
elif user_in == 'c':
print '''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 3 of the License, or
(at your option) any later version.
the Free Software Foundation, either version 3 of the License, or any later version.
'''

elif user_in == 'st':
tape_start = input('Set beginning cell: ')
tape_end = input('Set ending cell: ')
elif user_in.split(" ")[0] == 'st':
try:
tape_start = int(user_in.split(" ")[1])
tape_end = int(user_in.split(" ")[2])
except:
print "st: requires two arguments"

elif user_in.split(" ")[0] == 'import':
import_script(user_in.split(" ")[1], False)
display()

elif user_in == 'h':
print 'st: allows you to change your view of the tape'
print 'w: print warrenty'
print 'c: print conditions'
print 'st: set tape, allows you to set the range of the tape view'
print 'q: quit the repl'
print 'import: import a BF file'

else:
bf.eval(user_in)
print bf.get_tape(tape_start, tape_end)
display()
29 changes: 23 additions & 6 deletions lib/bfinter.py
@@ -1,6 +1,6 @@
'''
Brainy: A brainfuck interpreter/repl written in python
Copyright (C) 2012 Joel Buchheim-Moore
Copyright (C) 2012-2013 Joel Buchheim-Moore
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
Expand All @@ -27,12 +27,25 @@ def __init__(self):
self.loop_list = []
self.__output = ''

self.tape_start = 0
self.tape_end = 10
self.tape_length = self.tape_end - self.tape_start

def control_head(self, char):
if char == "+": self.tape[self.cur_cell] += 1
elif char == "-": self.tape[self.cur_cell] -= 1
elif char == ">": self.cur_cell += 1
elif char == "<": self.cur_cell -= 1
if char == "+":
if self.tape[self.cur_cell] == 255: self.tape[self.cur_cell] = 255
else: self.tape[self.cur_cell] += 1
elif char == "-":
if self.tape[self.cur_cell] == 0: self.tape[self.cur_cell] = 0
else: self.tape[self.cur_cell] -= 1
elif char == ">":
if self.cur_cell >= len(self.tape):
self.tape.append(0)
self.cur_cell += 1
else: self.cur_cell += 1
elif char == "<":
if self.cur_cell <= 0: self.cur_cell = 0
else: self.cur_cell -= 1
elif char == ".":
sys.stdout.write(chr(self.tape[self.cur_cell]))
self.__output += chr(self.tape[self.cur_cell])
Expand Down Expand Up @@ -65,10 +78,14 @@ def get_output(self): return self.__output

def get_tape(self, start=0, end=10):
'''Pretty prints the tape values'''
tmp = '\n'
self.tape_start = start
self.tape_end = end
self.tape_length = end - start
tmp = '\n'+"|"+str(start)+"| "
for i in xrange(len(self.tape[start:end])):
if i == self.cur_cell:
tmp += "[" + str(self.tape[i]) + "] "
else: tmp += ":" + str(self.tape[i]) + ": "
tmp += " |"+str(end)+"|"
return tmp

2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -11,5 +11,5 @@
url='http://github.com/joelbm24/brainy',
license='LICENSE.txt',
description='brainfuck interpreter',
long_description=open('README.txt').read(),
long_description=open('README.md').read(),
)

0 comments on commit ac4fe9b

Please sign in to comment.