How to build a Brainfuck interpreter in Python?

I worked on the BF interpreter, trying to ensure that it does not use external libraries and works in one function.

The problem I am facing is that some programs work fine while others do not. This makes debugging and analysis difficult, and what goes wrong.

A common factor is that it cannot process a BF program with more than one set of brackets (although there are some exceptions, then the programs work, and 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 that this is not the best Python code, I just thought that I would give it away.

Programs that work:

,----------[----------------------.,----------]  

- Converts lowercase to uppercase

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

- Hello World!

Currently, the program I'm trying to do is as follows:

++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]

* s.

, , 0, 1, 0, 1..... .. .

, , 120, .

.

.

+4
1

[ ]: , , , , !!! , . bf python, - open_braces, 1 , , , . :

elif code[i] == '[':
    if array[pointerLocation] == 0:
        open_braces = 1
        while open_braces > 0:
            i += 1
            if code[i] == '[':
                open_braces += 1
            elif code[i] == ']':
                open_braces -= 1
elif code[i] == ']':
    # you don't need to check array[pointerLocation] because the matching '[' will skip behind this instruction if array[pointerLocation] is zero
    open_braces = 1
    while open_braces > 0:
        i -= 1
        if code[i] == '[':
            open_braces -= 1
        elif code[i] == ']':
            open_braces += 1
    # i still gets incremented in your main while loop
    i -= 1

, if array[pointerLocation] == 0 elif code[i] == ']': -, . , .

+4
source

Source: https://habr.com/ru/post/1681977/


All Articles