unlambda.py
wfocke at mothership.phoenixdsl.com
wfocke at mothership.phoenixdsl.com
Tue Aug 22 03:15:59 EDT 2000
Simon Brunning taunted:
>> From: Ben Wolfson [<address elided>]
>> There are probably some memory issues.
>
>I think that you have all sorts of issues. ;)
>
>What's next on your list - Brainf***, Orthogonal, or the classic INTERCAL?
I wrote this a year ago, and had more sense than to let it out.
Now you've driven me to it, you beastly man.
but-I-think-it'll-all-work-out-in-the-end-
as-long-as-no-one-does-Befunge-ly y'rs
Warren Focke
------------------- bfi.py --------------------
import array, sys
default_size = 30000
point_big = 1
point_neg = 2
loop_open = 4
loop_close = 8
class BrainFork:
def __init__(self, size=default_size, typecode='B',
istream=sys.stdin, ostream=sys.stdout):
self.point = 0
self.size = size
self.array = array.array(typecode, [0] * size)
self.istream = istream
self.ostream = ostream
self._pmax = self.point
self._pmin = self.point
self._error = 0
def interp(self, string):
iptr = 0
lim = len(string)
while iptr < lim:
command = string[iptr]
if command == '+':
self.array[self.point] = self.array[self.point] + 1
elif command == '-':
self.array[self.point] = self.array[self.point] - 1
elif command == '>':
self.point = self.point + 1
if self.point > self._pmax:
self._pmax = self.point
if self.point >= self.size:
self._error = self._error | point_big
elif command == '<':
self.point = self.point - 1
if self.point < self._pmin:
self._pmin = self.point
if self.point < 0:
self._error = self._error | point_neg
elif command == '[':
# scan forward to find end of loop
depth = 1
lstart = iptr + 1
while iptr < lim:
iptr = iptr + 1
command = string[iptr]
if command == '[':
depth = depth + 1
elif command == ']':
depth = depth - 1
if depth == 0:
# loop is done
break
else:
# the loop did not finish
self._error = self._error | loop_close
while self.array[self.point]:
self.interp(string[lstart:iptr])
elif command == ']':
# loop close without an open
self._error = self._error | loop_open
elif command == '.':
self.ostream.write(chr(self.array[self.point]))
self.ostream.flush()
elif command == ',':
self.array[self.point] = ord(self.istream.read(1))
else:
pass
iptr = iptr + 1
if __name__ == '__main__':
if len(sys.argv) > 1:
file = open(sys.argv[1])
else:
file = sys.stdin
bfi = BrainFork()
script = file.read()
bfi.interp(script)
--
fnord
More information about the Python-list
mailing list