-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.py
64 lines (58 loc) · 1.77 KB
/
Interpreter.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
import putget as pg
from MemoryTape import MemoryTape
def main(argv):
# first, get name of brainfuck source file to be interpreted (passed as the FIRST parameter)
file = argv[0]
# read in the code
with open(file, 'r') as sourcefile:
source = sourcefile.read()
# build the tape and set pointer to its starting location
mem = MemoryTape()
ptr = 0
# parse input program character by character and thus run the brainfuck program
pos = 0
while pos < len(source):
char = source[pos] # get current command
if char == '>':
ptr += 1
pos += 1
elif char == '<':
ptr -= 1
pos += 1
elif char == '+':
mem[ptr] += 1
pos += 1
elif char == '-':
mem[ptr] -= 1
pos += 1
elif char == '.':
pg.putch(chr(mem[ptr]))
pos += 1
elif char == ',':
mem[ptr] = ord(pg.getch())
pos += 1
elif char == '[':
if mem[ptr] == 0:
# '[' counts +1, ']' counts -1
balance = 1
while balance != 0:
pos += 1
if source[pos] == '[':
balance += 1
elif source[pos] == ']':
balance -= 1
pos += 1
elif char == ']':
if mem[ptr] != 0:
balance = -1
while balance != 0:
pos -= 1
if source[pos] == '[':
balance += 1
elif source[pos] == ']':
balance -= 1
else:
pos += 1
if __name__ == "__main__":
main(sys.argv[1:])