-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfp_interpreter.py
102 lines (85 loc) · 2.41 KB
/
bfp_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
bf+ Compiler - by Prokop Schovanec
Just basic bf, but with some
more features.
Read README for help.
"""
import sys
def bf(code):
arr = [0]
pointerPos = 0
maxInt = 4
i = 0
ob = 0
while i < len(code):
if code[i] == "<":
if pointerPos > 0:
pointerPos -= 1
elif code[i] == ">":
pointerPos += 1
if pointerPos >= len(arr):
arr.append(0)
elif code[i] == "+":
arr[pointerPos] += 1
elif code[i] == "-":
arr[pointerPos] -= 1
elif code[i] == ".":
print(pointerPos, arr[pointerPos], chr(arr[pointerPos]))
elif code[i] == ",":
inp = input("> ")
try:
np = int(inp)
except ValueError:
np = ord(inp)
arr[pointerPos] = np
elif code[i] == "*":
plMov = ""
for n in range(maxInt):
try:
num = int(code[i + n])
plMov += str(num)
except ValueError:
pass
pointerPos += int(plMov)
if pointerPos >= len(arr):
for n in range(int(plMov)):
arr.append(0)
elif code[i] == "@":
plNum = ""
for n in range(maxInt):
try:
num = int(code[i + n])
plNum += str(num)
except ValueError:
pass
arr[pointerPos] = int(plNum)
elif code[i] == "[":
if arr[pointerPos] == 0:
ob = 1
while ob > 0:
i += 1
if code[i] == "[":
ob += 1
elif code[i] == "]":
ob -= 1
elif code[i] == "]":
ob = 1
while ob > 0:
i -= 1
if code[i] == "[":
ob -= 1
elif code[i] == "]":
ob += 1
i -= 1
i += 1
#bf("++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>------------.<+++++++++++.")
#bf(">++++++++++[<+++++++>-]<.")
#bf(">,[>,]<[<]>[.>]")
#bf("@72.@69.@76.@76.@79.")
#bf("*777.")
if __name__ == "__main__":
try:
bfCode = str(sys.argv[1])
bf(bfCode)
except IndexError:
print("no args")