-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinterpreter.py
More file actions
46 lines (41 loc) · 1.21 KB
/
interpreter.py
File metadata and controls
46 lines (41 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def interpreter(program):
cells = [0]
position = 0
i = 0
output = ''
while i < len(program):
token = program[i]
if token == '+':
cells[position] = (cells[position] + 1) % 256
elif token == '-':
cells[position] = (cells[position] - 1) % 256
elif token == '<':
position -= 1
elif token == '>':
position += 1
try:
cells[position]
except:
cells.append(0)
elif token == '[':
open = int(cells[position] == 0)
while open:
i += 1
if program[i] == '[':
open += 1
elif program[i] == ']':
open -= 1
elif token == ']':
open = int(cells[position] != 0)
while open:
i -= 1
if program[i] == ']':
open += 1
elif program[i] == '[':
open -= 1
elif token == '.':
output += chr(cells[position])
i += 1
return output
if __name__=='__main__':
print(1, 'Cf', interpreter('++++++++++++++[>+++++>-----------<<-]>---.>.'))