I am a beginning programmer in Python, trying to practice by building a BF interpreter. However, it will not work with the Hello, World! program:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
giving a RuntimeError: maximum recursion depth exceeded in cmp. My code is below.
import sys
script, file = sys.argv
tape, tapeslct, cmdslct = [0], 0, 0
code = open(file)
program = filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-'], code.read())
print(str(program))
program = list(program)
def execute(cmd):
global tape
global tapeslct
global cmdslct
if cmd == "+":
tape[tapeslct] += 1
if cmd == "-":
tape[tapeslct] -= 1
if cmd == ">":
tapeslct += 1
if tapeslct == len(tape):
tape.append(0)
if cmd == "<":
tapeslct -= 1
if cmd == ".":
print(chr(tape[tapeslct]))
if cmd == ",":
tape[tapeslct] = ord(input())
if cmd == "[":
if tape[tapeslct] >= 1:
loopend = 0
testfor = cmdslct
while loopend == 0:
testfor += 1
if program[testfor] == "]" and tape[tapeslct] >= 0:
testfor = cmdslct + 1
execute(program[testfor])
elif program[testfor] == "]" and tape[tapeslct] == 0:
loopend = 1
cmdslct = testfor + 1
else:
execute(program[testfor])
else:
while loopend == 0:
testfor += 1
if program[testfor] == "]":
loopend = 1
while len(program) > cmdslct:
execute(program[cmdslct])
cmdslct += 1
The lambda snippet was taken from another BF interpreter in Python, found here.
I've missed something, most likely.
[will jump to the first[, which should not happen. (Investigating why)