-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterpreter.py
More file actions
267 lines (227 loc) · 11.2 KB
/
Interpreter.py
File metadata and controls
267 lines (227 loc) · 11.2 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Brainfuck interpreter
# Does not implement input (,)
# The input code split over multiple lines with space
# Each call to step returns the char index + line index of a non-space command + any output
# When the end of the program is reaced (False, c, l, '') is returned
import cv2
import numpy as np
class Visualnterpreter:
INTERPRETER_OFFSET_Y = 0
code = []
jumpmap = {}
cells = []
cell_pointer = 0
code_pointer_char = 0
code_pointer_line = 0
finished = False
debug_slowdown_count = 0
debug_slowdown_factor = 3
in_loop_level = 0
last_movement_forward = True
history = []
def build_jumpmap(self):
temp_jumpstack, jumpmap = [], {}
for line_number, code_line in enumerate(self.code):
for char_number, command in enumerate(code_line):
if command == '[':
temp_jumpstack.append((char_number, line_number))
if command == ']':
if len(temp_jumpstack) == 0:
#print('Syntax error: Can\'t close a loop that hasn\'t started yet!')
return False, (line_number, char_number)
start = temp_jumpstack.pop()
jumpmap[start] = (char_number, line_number)
jumpmap[(char_number, line_number)] = start
if len (temp_jumpstack) > 0:
(char_number, line_number) = temp_jumpstack.pop()
#print('Syntax error: Unclosed loop detected!')
return False, (line_number, char_number)
self.jumpmap = jumpmap
return True, (0, 0)
def input_code(self, code):
self.code = code
def prepare_code(self):
ok, (l, c) = self.build_jumpmap()
if not ok:
return False, (l, c)
self.cells = []
self.cell_pointer = 0
self.code_pointer_char = -1
self.code_pointer_line = 0
self.debug_slowdown_count = 0
self.finished = False
self.in_loop_level = 0
self.history = []
return True, (l, c)
def step(self, single_step = False):
# No code
if len(self.code) == 0:
#print('No code to execute')
return True, False, 0, 0, ''
# Step, step, step
if not single_step and self.in_loop_level == 0 and self.debug_slowdown_count % self.debug_slowdown_factor != 0:
self.debug_slowdown_count += 1
return False, False, self.code_pointer_char, self.code_pointer_line, ''
else:
self.debug_slowdown_count = 1
if self.code_pointer_char == None:
self.code_pointer_char = 0
self.code_pointer_line = 0
elif len(self.code) >= self.code_pointer_line and self.code_pointer_char < len(self.code[self.code_pointer_line]) - 1:
self.code_pointer_char += 1
elif self.code_pointer_line < len(self.code):
self.code_pointer_char = 0
self.code_pointer_line += 1
# Point past last line
if self.code_pointer_line >= len(self.code) or self.code_pointer_char >= len(self.code[self.code_pointer_line]):
self.code_pointer_char = len(self.code[self.code_pointer_line - 1])
self.code_pointer_line = len(self.code) - 1
return True, True, self.code_pointer_char, self.code_pointer_line, ''
command = self.code[self.code_pointer_line][self.code_pointer_char]
# Handle formatted with single spaces, never double
if command == ' ':
self.code_pointer_char += 1
command = self.code[self.code_pointer_line][self.code_pointer_char]
char = self.code_pointer_char
line = self.code_pointer_line
output = ''
if command == ">":
self.cell_pointer += 1
# Dynamically add more cells as needed
while self.cell_pointer >= len(self.cells):
self.cells.append(0)
if command == "<":
if self.cell_pointer <= 0:
self.cell_pointer = 0
else:
self.cell_pointer -= 1
if command == "+":
self.cells[self.cell_pointer] += 1
if self.cells[self.cell_pointer] > 255:
self.cells[self.cell_pointer] = 0
if command == "-":
self.cells[self.cell_pointer] -= 1
if self.cells[self.cell_pointer] < 0:
self.cells[self.cell_pointer] = 255
if command == "[":
if self.cells[self.cell_pointer] == 0:
self.code_pointer_char, self.code_pointer_line = self.jumpmap[(char, line)]
else:
self.in_loop_level += 1
if command == "]":
if self.cells[self.cell_pointer] != 0:
self.code_pointer_char, self.code_pointer_line = self.jumpmap[(char, line)]
else:
self.in_loop_level -= 1
if command == ".":
output = chr(self.cells[self.cell_pointer])
self.last_movement_forward = True
return False, True, self.code_pointer_char, self.code_pointer_line, output
def history_append(self, historic_output):
if self.code_pointer_line <= len(self.code) and self.code_pointer_char <= len(self.code[self.code_pointer_line]):
self.history.append((False, False, self.code_pointer_char, self.code_pointer_line, self.cell_pointer, self.cells[:], historic_output))
def step_back(self):
if len(self.history) < 2:
self.code_pointer_char = None
self.code_pointer_line = None
self.cell_pointer = 0
self.cells = []
return False, False, None, None, ''
finished, remember, char, line, cell_pointer, cells, output = self.history[-2]
self.code_pointer_char = char
self.code_pointer_line = line
self.cell_pointer = cell_pointer
self.cells = cells[:]
_ = self.history.pop()
self.last_movement_forward = False
return (finished, remember, char, line, output)
def print_single_line_of_code(self, img, line_number, line_of_code, margin_h, color = (255,255,255)):
line_height = 36
line_margin_v = 8
cv2.rectangle(img, (0, line_number * line_height), (img.shape[1], line_height + line_number * line_height), (0,0,0), -1)
cv2.putText(img, line_of_code.strip(), (margin_h, line_number * line_height + (line_height - line_margin_v)), cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
def print_lines_of_code(self, img, n, margin_h):
for i, line_of_code in enumerate(self.code[-n:]):
self.print_single_line_of_code(img, i, line_of_code, margin_h)
def get_text_width(self, text, font_face, font_scale, font_line_thickness):
((txt_w, _), _) = cv2.getTextSize(text, font_face, font_scale, font_line_thickness)
return txt_w
def get_text_size(self, text, font_face, font_scale, font_line_thickness):
((txt_w, txt_h), _) = cv2.getTextSize(text, font_face, font_scale, font_line_thickness)
return txt_w, txt_h
def draw_black_alpha_box(self, img, x, y, h, w):
sub_img = img[y:y+h, x:x+w]
black_rect = np.ones(sub_img.shape, dtype=np.uint8) * 0
res = cv2.addWeighted(sub_img, 0.3, black_rect, 0.7, 1.0)
img[y:y+h, x:x+w] = res
def debug_single_line_of_code(self, img, line_number, line_of_code, margin_h, margin_v, color = (255,255,255)):
line_height = 36
line_margin_v = 8
cv2.putText(img, line_of_code.strip(), (margin_h, line_number * line_height + margin_v + (line_height - line_margin_v)), cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
def debug_lines_of_code(self, img, margin_h):
if len(self.code) == 0:
return
lines = len(self.code)
self.draw_black_alpha_box(img, 0, self.INTERPRETER_OFFSET_Y, 40 * lines, img.shape[1])
for i, line_of_code in enumerate(self.code):
self.debug_single_line_of_code(img, i, line_of_code, margin_h, self.INTERPRETER_OFFSET_Y)
def highlight_debug_command(self, img, char_number, line_number, margin_h, color = (50, 205, 50)):
if char_number == None or line_number == None:
return
if line_number >= len(self.code) or char_number >= len(self.code[line_number]):
return
line_height = 36
line_margin_v = 8
previous_code = self.code[line_number][:char_number]
if len(previous_code) > 0:
# Subtract 2, beacuse the measurement of an empty string apparently is 2
offset = self.get_text_width(previous_code, cv2.FONT_HERSHEY_PLAIN, 2, 2) - 2
else:
offset = 0
command = self.code[line_number][char_number]
cv2.putText(img, command, (margin_h + offset, self.INTERPRETER_OFFSET_Y + line_number * line_height + (line_height - line_margin_v)), cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
# Print the first 8 cells
# v
# +-------------------------------+
# | 0 | 0 | 0 | 0 | 3 | 0 | 0 | 0 |
# +-------------------------------+
def print_cells(self, img):
if self.cell_pointer == None:
return
self.draw_black_alpha_box(img, 0, 428, 70, img.shape[1])
# Draw cells
cv2.line(img, (4, 432), (636, 432), (255,255,255), 2)
cv2.line(img, (4, 476), (636, 476), (255,255,255), 2)
for i in range(4, 640, 79):
cv2.line(img, (i, 432), (i, 476), (255,255,255), 2)
# Draw cell values
for i, cell in enumerate(self.cells[:8]):
offset = self.get_text_width(str(cell), cv2.FONT_HERSHEY_PLAIN, 2, 2) - 2
cv2.putText(img, str(cell), ((i + 1) * 79 - offset, 465), cv2.FONT_HERSHEY_PLAIN, 2, (255,255,255), 2)
# Draw pointer to current cell (cell pointer)
xp = 40 + 79 * self.cell_pointer
yp = 422
cv2.line(img, (xp, yp), (xp - 20, yp - 15), (255,255,255), 5)
cv2.line(img, (xp, yp), (xp + 20, yp - 15), (255,255,255), 5)
cv2.line(img, (xp, yp), (xp - 20, yp - 15), (0,0,255), 3)
cv2.line(img, (xp, yp), (xp + 20, yp - 15), (0,0,255), 3)
def print_outout(self, img, output, color):
if len(output) > 0:
font_size = 5
font_thickness = 3
width, height = self.get_text_size(output, cv2.FONT_HERSHEY_PLAIN, font_size, font_thickness)
# If text is too long, make it smaller - still only single line
while width > img.shape[1] - 10:
if font_size > 2:
font_size -= 1
else:
font_size *= 0.75
if font_thickness > 1:
font_thickness -= 1
width, height = self.get_text_size(output, cv2.FONT_HERSHEY_PLAIN, font_size, font_thickness)
if width > 0 and height > 0:
# Consider allowing multipe lines of output
self.draw_black_alpha_box(img, 0, 320, 70, img.shape[1])
# Offset to keep text centered, while getting smaller
offset = int((47 - height) / 2)
cv2.putText(img, output, (int((img.shape[1] - width) / 2) , 380 - offset), cv2.FONT_HERSHEY_PLAIN, font_size, color, font_thickness)