Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Brainf*** – Bugs on while loops

I am a relative beginner to python, and in order to strengthen my skills, I am (attempting) to write a compiler for the Brainfu** language. All is good, except for the bracket [] loops. The program I am using to test my code is >++[>++<-]>+, which should set cell 2 to 5. When I run this, however, it does this:

0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 0 >
1 [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 +
2 [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 +
3 [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 [
4 [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 0 >
5 [0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 +
6 [0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 +
7 [0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 <
8 [0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 -
3 [0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1 [
10 [0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 >
11 [0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 3 +
[0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

(The lines are formatted in the iteration, then the list at that point, then the value it's focused on and then the character it's running.)

My current code is

def generateArray(code):
    array = []
    for i in range(0,20):
        array.append(0);
    return array

def run(code):
    print code
    data = generateArray(code)
    chars = list(code)
    pointer = 0

    for i in range(0, len(chars)):
        current = chars[i]
        if(current == "+"):
            data[pointer] += 1

        if(current == ">"):
            pointer += 1

        if(current == "-"):
            data[pointer] -= 1

        if(current == "<"):
            pointer -= 1

        if(current == "."):
            print str(chr(data[pointer]))

        if(current == ","):
            given = raw_input()
            data[pointer] = ord( given )

        if(current == "["):
            posOfEnd = chars[i:len(chars)].index("]")
            if(data[pointer] == 0):
                i += posOfEnd+1

        if(current == "]"):
            posOfBegin = len(chars) - 1 - chars[::-1].index('[')
            i = posOfBegin



        print i, data, data[pointer], chars[i]

    return data

print run(">++[>++<-]>+")

posOfEnd is trying to find out where the next bracket is, and posOfBegin is trying to find out where the previous bracket is.

like image 652
Olly Britton Avatar asked Jan 13 '17 13:01

Olly Britton


1 Answers

I suppose the problem is your loop variable i which you modify during the loop:

i += posOfEnd+1

and

i = posOfBegin

However python for loops are different from their C/C++ counterparts. In python the variable i will be set to each element of the iterable you provide it, in this case range. range(n) evaluates to a list containing all numbers from 0 up to n-1. If you modify your loop variable during an iteration then this modification remains for only that iteration but for the next iteration the loop variable will be assigned the next element of the iterable (not preserving your modifications).

You might want to use a while loop instead.

like image 72
a_guest Avatar answered Sep 29 '22 08:09

a_guest