-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.py
63 lines (51 loc) · 1.34 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
BF_ALLOWED_SYMBOLS = '><+-.,[]'
def load_bf_code(filename):
with open(filename) as f:
return [
command
for command in f.read()
if command in BF_ALLOWED_SYMBOLS
]
def main(input_filename):
bf_code = load_bf_code(input_filename)
array = [0] * 30000
ptr = 0
stack = []
i = 0
length = len(bf_code)
while i < length:
op = bf_code[i]
if op == '>':
ptr += 1
elif op == '<':
ptr -= 1
elif op == '+':
array[ptr] += 1
elif op == '-':
array[ptr] -= 1
elif op == '.':
sys.stdout.write(chr(array[ptr]))
elif op == ',':
array[ptr] = ord(sys.stdin.read(1))
elif op == '[':
if array[ptr]:
stack.append(i)
else:
depth = 0
while True:
op = bf_code[i]
if op == '[':
depth += 1
elif op == ']':
depth -= 1
if depth == 0:
break
i += 1
elif op == ']':
i = stack.pop() - 1
i += 1
if __name__ == '__main__':
main(*sys.argv[1:])