I have been working on a BF interpreter, trying to ensure it uses no external libraries, and works in a single function.
The issue I am running into is that some programs work perfectly well, and others don't. This is making it hard to debug and figure and what's going wrong.
The common factor seems to be it cannot handle a BF program with more then one set of brackets (although there are some exceptions, but then the programs work, just not completely).
The Code:
def interpret(code):
array = [0]
pointerLocation = 0
i = 0
c = 0
print(code)
while i < len(code):
if code[i] == '<':
if pointerLocation > 0:
pointerLocation -= 1
elif code[i] == '>':
pointerLocation += 1
if len(array) <= pointerLocation:
array.append(0)
elif code[i] == '+':
array[pointerLocation] += 1
elif code[i] == '-':
if array[pointerLocation] > 0:
array[pointerLocation] -= 1
elif code[i] == '.':
print(array[pointerLocation], chr(array[pointerLocation]))
elif code[i] == ',':
x = input("Input:")
try:
y = int(x)
except ValueError:
y = ord(x)
array[pointerLocation] = y
elif code[i] == '[':
if array[pointerLocation] == 0:
while code[i] != ']':
i += 1
elif code[i] == ']':
if array[pointerLocation] != 0:
while code[i] != '[':
i -= 1
i += 1
interpret("""
#This is where the BF code goes
""")
I know this is not the best Python code, I just thought I'd give it a go.
The programs that work:
,----------[----------------------.,----------]
- Converts lowercase to uppercase
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
- Hello World!
The program I am currently trying to make work is:
++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]
It's designed to output a Sierpinski Triangle with *s.
I get no output, but if I output the array it appears to create and almost endless array of sequenced 0, 1, 0, 1.....etc. etc.
From running it through a proper interpreter I know that the array should only end up with a length of 120, and I am getting into the thousands within seconds.
Any help would be appreciated.
Thanks.