- 1

Why does this code trigger a run time error?

#Brainf**k interpreter import sys f = open(sys.argv[0], "r") point = f.read() f.close() pointPos = 0 cell = [0] cellPos = 0 while pointPos < len(point): if point[pointPos] == ">": cellPos += 1 if len(cell) > cellPos: cell.append(0) elif point[pointPos] == "<": cellPos -= 1 if cellPos < 0: print("Error: Moved off tape!") sys.exit(0) elif point[pointPos] == "+": cell[cellPos] += 1 if cell[cellPos] >= 255: cell[cellPos] = 0 elif point[pointPos] == "-": cell[cellPos] -= 1 if cell[cellPos] <= -1: cell[cellPos] = 255 elif point[pointPos] == ".": print(chr(cell[cellPos]), end = "") elif point[pointPos] == ",": inp = input() cell[cellPos] = ord(inp[0]) elif point[pointPos] == "[": if cell[cellPos] == 0: countOpened = 0 point[pointPos] += 1 while pointPos < len(point): if point[pointPos] == "]" and countOpened == 0: break elif point[pointPos] == "[": countOpened += 1 elif point[pointPos] == "]": countOpened -= 1 pointPos += 1 elif point[pointPos] == "]": if cell[cellPos] != 0: countOpened = 0 pointPos -= 1 while pointPos > 0: if point[pointPos] == "[" and countOpened == 0: break elif point[pointPos] == "]": countOpened += 1 elif point[pointPos] == "[": countOpened -= 1 pointPos -= 1 # Input: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. # output: ‘Hello World’ https://code.sololearn.com/cFl30Rhcu2L3/?ref=app

30th Aug 2019, 1:36 AM
Evan
2 odpowiedzi
+ 1
It's an infinite loop: "pointPos" never increases in value.
30th Aug 2019, 3:02 AM
Diego
Diego - avatar
+ 1
Maximum call stack exceeded
30th Aug 2019, 5:37 AM
Qudusayo
Qudusayo - avatar