-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.py
141 lines (117 loc) · 3.9 KB
/
brainfuck.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class BFArray:
def __init__(self, n):
self.arr = [0 for _ in range(n)]
self.pointer = 0
self.cell_width = 256
def right(self):
self.pointer += 1
self.pointer %= len(self.arr)
def left(self):
self.pointer -= 1
self.pointer %= len(self.arr)
def inc(self):
self.arr[self.pointer] += 1
self.arr[self.pointer] %= self.cell_width
def dec(self):
self.arr[self.pointer] -= 1
self.arr[self.pointer] %= self.cell_width
def get(self):
return self.arr[self.pointer]
def put(self, x):
self.arr[self.pointer] = x
class BFInterpreter:
def __init__(self, n):
self.arr = BFArray(n)
self.lines = []
self.program_counter = (0, 0)
self.loops = []
self.open_brackets = 0
self.looped = False
self.input_buffer = []
self.op_codes = {
'+':self.arr.inc,
'-':self.arr.dec,
'<':self.arr.left,
'>':self.arr.right,
'.':self.output,
',':self.input,
'[':self.start_loop,
']':self.loop,
'#':self.debug
}
def debug(self):
print('Program counter:', self.program_counter)
print('Array pointer:', self.arr.pointer)
print('Value at {}:'.format(self.arr.pointer), self.arr.get())
print('Top of the stack:', self.loops[-1] if len(self.loops)>0 else '-')
def open_file(self, file_name):
with open(file_name, 'r') as file:
self.lines.extend(file.readlines())
def read_line(self):
l = input("BF > ")
self.lines.append(l)
if l[0] == '0':
return False
else:
return True
def step(self):
#extract program counter
i = self.program_counter[0]
j = self.program_counter[1]
#if looping forward
if self.open_brackets > 0:
#'[' goes deeper
if self.lines[i][j] == '[':
self.open_brackets += 1
#']' goes up
elif self.lines[i][j] == ']':
self.open_brackets -= 1
if self.open_brackets == 0:
if self.lines[i][j] in self.op_codes.keys():
self.op_codes[self.lines[i][j]]()
if not self.looped:
j += 1
if j == len(self.lines[i]):
j = 0
i += 1
self.program_counter = (i, j)
else:
self.looped = False
return (self.program_counter[0] < len(self.lines))
def output(self):
print(chr(self.arr.get()), end = '')
def input(self):
if len(self.input_buffer) == 0:
self.input_buffer = list(str(input()))
c = self.input_buffer.pop(0)
self.arr.put(ord(c))
def start_loop(self):
self.loops.append(self.program_counter)
if self.arr.get() == 0:
self.open_brackets = 1
def loop(self):
pc = self.loops.pop()
if self.arr.get() != 0:
self.program_counter = pc
self.looped = True
def main():
bf = BFInterpreter(1000)
menu = str(input("Choose mode [live|file]: ")).casefold()
while menu != 'live' and menu != 'file':
menu = str(input("Choose mode [live|file]: "))
if menu == 'file':
file_name = input('File name: ')
bf.open_file(file_name)
while(bf.step()):
pass
else:
print("--- Live mode ---")
print("Type a line of code, enter to run, # for debug, 0 to end")
while True:
print()
if not bf.read_line():
return
while bf.step():
pass
if __name__ == '__main__':
main()