-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
263 lines (196 loc) · 7.86 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
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
# coding=utf-8
import locale, codecs, os
if codecs.lookup(locale.getpreferredencoding()).name == 'ascii':
os.environ['LANG'] = 'en_US.utf-8'
from dots.interpreter import AsciiDotsInterpreter
from dots.callbacks import IOCallbacksStorage
from dots import terminalsize
import curses
import click
import sys
import os
import time
import signal
from dots.states import DeadState
interpreter = None
debug_ = True
autostep_debug_ = False
class Default_IO_Callbacks(IOCallbacksStorage):
def __init__(self, ticks, silent, debug, compat_debug, debug_lines, autostep_debug, head):
super().__init__()
self.ticks = ticks
self.silent = silent
self.debug = debug
self.compat_debug = compat_debug
self.debug_lines = debug_lines
self.autostep_debug = autostep_debug
self.head = head
self.tick_number = 0
self.output_count = 0
if self.debug and not self.compat_debug:
self.logging_loc = 0
self.logging_x = 1
self.stdscr = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.noecho()
curses.curs_set(False)
self.win_program = curses.newwin(self.debug_lines, curses.COLS - 1, 0, 0)
self.logging_pad = curses.newpad(1000, curses.COLS - 1)
def signal_handler(signal, frame):
self.on_finish()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def get_input(self):
if not self.debug or self.compat_debug:
if not sys.stdin.isatty():
return input('?: ')
else:
x = input('?: ')
return x
else:
return self.curses_input(self.stdscr, curses.LINES - 3, 2, '?: ')
def curses_input(self, stdscr, r, c, prompt_string):
curses.echo()
stdscr.addstr(r, c, str(prompt_string), curses.A_REVERSE)
stdscr.addstr(r + 1, c, " " * (curses.COLS - 1))
stdscr.refresh()
input_val = ""
while len(input_val) <= 0:
input_val = stdscr.getstr(r + 1, c, 20)
return input_val
def on_output(self, value):
self.output_count += 1
if self.head != -1 and self.output_count > self.head:
self.on_finish()
return
if self.silent:
return
if not self.debug or self.compat_debug:
print(value, end='', flush=True)
else:
self.logging_pad.addstr(self.logging_loc, self.logging_x, str(value))
self.logging_pad.refresh(self.logging_loc - min(self.logging_loc, curses.LINES -
self.debug_lines - 1), 0, self.debug_lines, 0,
curses.LINES - 1, curses.COLS - 1)
# FIXME: This should count the number of newlines instead
if str(value).endswith('\n'):
self.logging_loc += 1
self.logging_x = 1
else:
self.logging_x += len(value)
def on_finish(self):
global interpreter
if self.debug and not self.compat_debug:
curses.nocbreak()
self.stdscr.keypad(False)
curses.echo()
curses.endwin()
# sys.exit(0)
def on_error(self, error_text):
self.on_output('error: {}'.format(error_text))
self.on_finish()
def on_microtick(self, dot):
if self.debug and not self.silent:
if self.autostep_debug:
time.sleep(self.autostep_debug)
else:
if self.compat_debug:
try:
input("Press enter to step...")
except SyntaxError:
pass
else:
self.stdscr.getch()
d_l = []
for idx in reversed(range(len(interpreter.get_all_dots()))):
d = interpreter.dots[idx]
if not d.state.isDeadState():
d_l.append((d.x, d.y))
special_char = False
last_blank = False
display_y = 0
if self.compat_debug:
if os.name == 'nt':
os.system('cls') # For Windows
else:
os.system('clear') # For Linux/OS X
else:
self.win_program.refresh()
for y in range(len(interpreter.world._data_array)):
if display_y > self.debug_lines - 2:
break
if len(''.join(interpreter.world._data_array[y]).rstrip()) < 1:
if last_blank:
continue
else:
last_blank = True
else:
last_blank = False
for x in range(len(interpreter.world._data_array[y])):
char = interpreter.world._data_array[y][x]
#RGYB
if (x, y) in d_l:
if self.compat_debug:
print('\033[0;31m'+char+'\033[0m', end='') # Red
else:
self.win_program.addstr(display_y, x, char, curses.color_pair(1))
elif char.isLibWarp():
if self.compat_debug:
print('\033[0;32m'+char+'\033[0m', end='') # Green
else:
self.win_program.addstr(display_y, x, char, curses.color_pair(2))
elif char.isWarp():
if self.compat_debug:
print('\033[0;33m'+char+'\033[0m', end='') # Yellow
else:
self.win_program.addstr(display_y, x, char, curses.color_pair(3))
elif char in '#@~' or char.isOper():
if self.compat_debug:
print('\033[0;34m'+char+'\033[0m', end='') # Blue
else:
self.win_program.addstr(display_y, x, char, curses.color_pair(4))
else:
if self.compat_debug:
print(char, end='')
else:
self.win_program.addstr(display_y, x, char)
display_y += 1
if self.ticks is not False:
if self.tick_number > self.ticks:
self.on_output('QUITTING next step!\n')
self.on_finish()
sys.exit(0)
self.tick_number += 1
_, terminal_lines = terminalsize.get_terminal_size()
default_debug_lines = int(terminal_lines*2/3)
del terminal_lines
def main(filename='--', ticks=False, silent=False, debug=False, compat_debug=False, debug_lines=default_debug_lines, autostep_debug=False, head=-1):
global interpreter
if autostep_debug is not False:
autostep_debug = float(autostep_debug)
if ticks is not False:
ticks = int(ticks)
head = int(head)
io_callbacks = Default_IO_Callbacks(ticks, silent, debug, compat_debug, debug_lines, autostep_debug, head)
program_dir = '.'
program = []
while True:
line = input('$ ')
if line.rstrip() == '%EOF':
break
else:
program.append(line)
# print(program, flush=True)
try:
interpreter = AsciiDotsInterpreter(program, program_dir, io_callbacks)
interpreter.run()
except Exception as e:
io_callbacks.on_finish()
interpreter.terminate()
print(e)
if __name__ == "__main__":
main()