-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
executable file
·630 lines (534 loc) · 18.2 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#! /usr/bin/env python3
"""
Do Not Modify This File
"""
__version__ = '2018-04-28 18:00'
import itertools
from pprint import pprint
import argparse
import sys
import random
import numbers
import operator
import copy
import itertools
from rply import LexerGenerator, ParserGenerator
MEMORY = {}
SYMBOL_TABLE = {}
REGISTER_TABLE = {}
LABEL_TABLE = {}
INSTRUCTIONS = []
STACK = []
INPUT = ""
OUTPUT = ""
DEBUG_FILENAME = 'debug.txt'
MAX_STEPS = 10000
class InterpreterError(Exception):
pass
class LMAOError(InterpreterError):
pass
class ROFLError(InterpreterError):
pass
common_commands = [
'VAL_COPY',
'ADD',
'SUB',
'MULT',
'DIV',
'TEST_LESS',
'TEST_GTR',
'TEST_EQU',
'TEST_NEQU',
'TEST_GTE',
'TEST_LTE',
'JUMP_IF_0',
'JUMP_IF_N0',
'JUMP',
'RANDOM',
'OUT_NUM',
'OUT_CHAR',
'IN_CHAR',
'NOP',
]
lmao_commands = [
'AR_GET_IDX',
'AR_SET_IDX',
'AR_GET_SIZE',
'AR_SET_SIZE',
'AR_COPY',
'PUSH',
'POP',
]
rofl_commands = [
'LOAD',
'STORE',
'MEM_COPY',
]
def build_lexer():
lg = LexerGenerator()
commands = sorted(itertools.chain(common_commands, lmao_commands, rofl_commands))
for command in reversed(commands):
lg.add(command, command)
lg.add('NEWLINE', r'\n')
lg.add('SCALAR_VAR', r's\d+')
lg.add('ARRAY_VAR', r'a\d+')
lg.add('REGISTER', r'reg[A-H]')
lg.add('LABEL', r'[a-zA-Z_][a-zA-Z_0-9]*')
lg.add('NUM_LITERAL', r'-?((\d+)(\.\d+)?)|(\.\d+)')
lg.add('CHAR_LITERAL', r"'([^\\']|\\n|\\t|\\'|\\\\)'")
lg.add('COLON', r':')
lg.ignore(r'[ \t]')
lg.ignore(r'\#.*')
lg.add('ERROR', r'.')
return lg.build()
def get_possible_tokens(lexer):
return [rule.name for rule in lexer.rules if rule.name != 'ERROR']
def check_for_lexing_errors(tokens, language):
for token in tokens:
if token.name == 'ERROR':
raise InterpreterError(f'Lexing error on token ({token.value}).')
if token.name in lmao_commands and language == 'ROFL':
raise ROFLError(f'Command {token.name} not allowed in ROFLcode.')
if token.name in rofl_commands and language == 'LMAO':
raise LMAOError(f'Command {token.name} not allowed in LMAOcode.')
class Var:
def __init__(self, name):
self.name = name
def __repr__(self):
return str(self.name)
def nice_str(self):
return f'{self.name}({self.get_value()})'
def __eq__(self, other):
return ((type(self) == type(other))
and (self.name == other.name))
def __hash__(self):
return hash(self.name)
def get_value(self):
raise NotImplementedError()
def set_value(self, value):
raise NotImplementedError()
class Constant(Var):
def get_value(self):
return self.name
class SymbolTableVar(Var):
def get_value(self):
if self.name not in SYMBOL_TABLE:
raise InterpreterError(f'{self.name} not in interpreter symbol table')
# SYMBOL_TABLE[self.name] = 0
return SYMBOL_TABLE[self.name]
def set_value(self, value):
SYMBOL_TABLE[self.name] = value
class ScalarVar(SymbolTableVar):
pass
class ArrayVar(SymbolTableVar):
def get_value(self):
if self.name not in SYMBOL_TABLE:
SYMBOL_TABLE[self.name] = []
return SYMBOL_TABLE[self.name]
class Register(Var):
def get_value(self):
return REGISTER_TABLE[self.name]
def set_value(self, value):
REGISTER_TABLE[self.name] = value
class Label(Var):
def get_value(self):
if self.name not in LABEL_TABLE:
raise InterpreterError(f'{self.name} not in interpreter label table')
# return -1
return LABEL_TABLE[self.name]
def build_parser(possible_tokens, language):
assert language in {'LMAOcode', 'ROFLcode'}, language
pg = ParserGenerator(possible_tokens)
@pg.production('program : optional_newlines statements')
def program(p):
return p[1]
@pg.production('program : empty_program')
def empty_program(p):
return []
@pg.production('empty_program : optional_newlines')
@pg.production('optional_newlines : newlines')
@pg.production('optional_newlines : ')
@pg.production('newlines : NEWLINE')
@pg.production('newlines : NEWLINE newlines')
def do_nothing(p):
pass
@pg.production('statements : statement newlines statements')
def multiple_statements(p):
return [p[0]] + p[2]
@pg.production('statements : statement newlines')
def single_statement(p):
return [p[0]]
@pg.production('math_command : ADD')
@pg.production('math_command : SUB')
@pg.production('math_command : MULT')
@pg.production('math_command : DIV')
@pg.production('math_command : TEST_LESS')
@pg.production('math_command : TEST_GTR')
@pg.production('math_command : TEST_EQU')
@pg.production('math_command : TEST_NEQU')
@pg.production('math_command : TEST_GTE')
@pg.production('math_command : TEST_LTE')
def math_command(p):
return p[0]
@pg.production('statement : math_command value value store_value')
@pg.production('statement : VAL_COPY value store_value')
@pg.production('statement : JUMP value')
@pg.production('statement : JUMP_IF_0 value value')
@pg.production('statement : JUMP_IF_N0 value value')
@pg.production('statement : RANDOM store_value')
@pg.production('statement : OUT_NUM value')
@pg.production('statement : OUT_CHAR value')
@pg.production('statement : NOP')
@pg.production('statement : PUSH value')
@pg.production('statement : POP store_value')
@pg.production('statement : PUSH array_var')
@pg.production('statement : POP array_var')
@pg.production('statement : AR_GET_IDX array_var value store_value')
@pg.production('statement : AR_SET_IDX array_var value value')
@pg.production('statement : AR_GET_SIZE array_var store_value')
@pg.production('statement : AR_SET_SIZE array_var value')
@pg.production('statement : AR_COPY array_var array_var')
@pg.production('statement : LOAD value value')
@pg.production('statement : STORE value value')
@pg.production('statement : MEM_COPY value value')
@pg.production('statement : IN_CHAR store_value')
def complete_statement(p):
p[0] = p[0].getstr()
return p
@pg.production('value : CHAR_LITERAL')
def char_literal(p):
char_str = p[0].getstr()
assert char_str[0] == "'"
assert char_str[-1] == "'"
char_str_without_quotes = char_str[1:-1]
if len(char_str_without_quotes) == 1:
return Constant(ord(char_str_without_quotes))
assert len(char_str_without_quotes) == 2
assert char_str_without_quotes[0] == '\\'
escaped_char = char_str_without_quotes[1]
mapping = {
'n': '\n',
'\\': '\\',
't': '\t',
"'": "'"
}
assert escaped_char in mapping
return Constant(ord(mapping[escaped_char]))
@pg.production('value : NUM_LITERAL')
def num_literal(p):
float_value = float(p[0].getstr())
if float_value.is_integer():
value = int(float_value)
else:
value = float_value
return Constant(value)
@pg.production('label : LABEL')
def label(p):
return Label(p[0].getstr())
@pg.production('scalar_var : SCALAR_VAR')
def scalar_var(p):
if language == 'ROFLcode':
raise ROFLError('Can\'t use scalar variables in ROLFcode.')
return ScalarVar(p[0].getstr())
@pg.production('array_var : ARRAY_VAR')
def array_var(p):
if language == 'ROFLcode':
raise ROFLError('Can\'t use array variables in ROFLcode.')
return ArrayVar(p[0].getstr())
@pg.production('register : REGISTER')
def register(p):
if language == 'LMAOcode':
raise LMAOError('Can\'t use registers in LMAOcode.')
return Register(p[0].getstr())
@pg.production('value : store_value')
@pg.production('store_value : scalar_var')
@pg.production('store_value : register')
@pg.production('value : label')
def values(p):
return p[0]
@pg.production('statement : label COLON')
def label_statement(p):
return [p[0], ':']
@pg.error
def error_handler(token):
raise InterpreterError(f"Ran into a {token} where it wasn't expected {token.source_pos}.")
return pg.build()
def execute_bad_instruction(instruction):
command = instruction[0]
binary_arithmetic_commands = {
'ADD': operator.add,
'SUB': operator.sub,
'DIV': operator.truediv,
'MULT': operator.mul
}
binary_logic_commands = {
'TEST_LESS': operator.lt,
'TEST_GTR': operator.gt,
'TEST_EQU': operator.eq,
'TEST_NEQU': operator.ne,
'TEST_GTE': operator.ge,
'TEST_LTE': operator.le
}
if command == 'VAL_COPY':
a = instruction[1].get_value()
instruction[2].set_value(a)
return
if command in binary_arithmetic_commands:
a = instruction[1].get_value()
b = instruction[2].get_value()
result = binary_arithmetic_commands[command](a, b)
instruction[3].set_value(result)
return
if command in binary_logic_commands:
a = instruction[1].get_value()
b = instruction[2].get_value()
is_true = binary_logic_commands[command](a, b)
if is_true:
result = 1
else:
result = 0
instruction[3].set_value(result)
return
if isinstance(command, Label):
return
if command == 'JUMP':
a = instruction[1].get_value()
return a
if command == 'JUMP_IF_0':
a = instruction[1].get_value()
b = instruction[2].get_value()
if a == 0:
return b
return None
if command == 'JUMP_IF_N0':
a = instruction[1].get_value()
b = instruction[2].get_value()
if a != 0:
return b
return None
if command == 'RANDOM':
result = random.randrange(100)
instruction[1].set_value(result)
return
if command == 'IN_CHAR':
global INPUT
if not INPUT:
raise InterpreterError('Not enough standard input provided')
result = INPUT[0]
INPUT = INPUT[1:]
instruction[1].set_value(ord(result))
return
global OUTPUT
if command == 'OUT_NUM':
a = instruction[1].get_value()
result = str(a)
OUTPUT += result
return
if command == 'OUT_CHAR':
a = instruction[1].get_value()
result = chr(a)
OUTPUT += result
return
if command == 'NOP':
return
if command == 'PUSH':
a = instruction[1].get_value()
STACK.append(a)
return
if command == 'POP':
a = STACK.pop()
instruction[1].set_value(a)
return
if command == 'AR_GET_IDX':
array = instruction[1].get_value()
idx = instruction[2].get_value()
result = array[int(idx)]
instruction[3].set_value(result)
return
if command == 'AR_SET_IDX':
array = instruction[1].get_value()
idx = instruction[2].get_value()
value = instruction[3].get_value()
array[int(idx)] = value
return
if command == 'AR_GET_SIZE':
array = instruction[1].get_value()
result = len(array)
instruction[2].set_value(result)
return
if command == 'AR_SET_SIZE':
array = instruction[1].get_value()
new_size = instruction[2].get_value()
while new_size < len(array):
array.pop()
while new_size > len(array):
array.append(0)
return
if command == 'AR_COPY':
source_array = instruction[1].get_value()
instruction[2].set_value(copy.deepcopy(source_array))
return
if command == 'STORE':
value = instruction[1].get_value()
address = int(instruction[2].get_value())
assert address >= 0
MEMORY[address] = value
return
if command == 'LOAD':
address = int(instruction[1].get_value())
assert address >= 0
value = MEMORY.get(address, 0)
instruction[2].set_value(value)
return
if command == 'MEM_COPY':
address_source = int(instruction[1].get_value())
address_dest = int(instruction[2].get_value())
assert address_source >= 0 and address_dest >= 0
value = MEMORY.get(address_source, 0)
MEMORY[address_dest] = value
return
raise InterpreterError("Unknown command: {}".format(command))
def add_label_locations_to_label_table(INSTRUCTIONS):
for i, instruction in enumerate(INSTRUCTIONS):
if instruction:
first_instruction = instruction[0]
if isinstance(first_instruction, Label):
assert first_instruction not in LABEL_TABLE
LABEL_TABLE[first_instruction.name] = i
def execute_bad_instructions(instructions, language):
cycles = 0
step = 0
add_label_locations_to_label_table(instructions)
instruction_pointer = 0
while instruction_pointer < len(instructions):
if step > MAX_STEPS:
raise InterpreterError(
f'More than {MAX_STEPS} steps, likely infinite loop.')
step += 1
instruction = instructions[instruction_pointer]
new_ip = None
if instruction:
if language == 'LMAO':
cycles += 1000
elif instruction[0] in {'load', 'store', 'mem_copy'}:
cycles += 100
else:
cycles += 1
if language == 'LMAOcode':
if instruction[0] in rofl_commands:
raise LMAOError(f'Can not use ROFLcode command ({instruction[0]}) in LMAOcode.')
else: # language == 'LMAOcode':
if instruction[0] in lmao_commands:
raise LMAOError(f'Can not use LMAOcode command ({instruction[0]}) in ROFLcode.')
new_ip = execute_bad_instruction(instruction)
if DEBUG_MODE:
debug_output(instruction, instruction_pointer, step)
if new_ip is not None:
instruction_pointer = new_ip
else:
instruction_pointer += 1
return cycles
def debug_output(instruction, line, step):
output = []
def print_line_from_table(name, table):
if table:
line = name + " { "
for key in sorted(table):
line += "{}:{} ".format(key, table[key])
line += "}"
output.append(line)
def print_line_from_stack(name, stack):
if stack:
line = name + " (top) { "
for value in reversed(stack):
line += "{} ".format(value)
line += "} (bottom)"
output.append(line)
def print_line_of_instruction(instruction):
result = ''
for part in instruction:
if isinstance(part, Var):
value = part.nice_str()
else:
value = str(part)
result += value + ' '
return result
output.append("Step # {}".format(step))
instruction_str = print_line_of_instruction(instruction)
output.append("Executed line # {} : {}".format(line + 1, instruction_str))
print_line_from_table("ST", SYMBOL_TABLE)
print_line_from_table("Regs", REGISTER_TABLE)
print_line_from_table("Mem", MEMORY)
print_line_from_stack("Stack", STACK)
output.append("")
with open(DEBUG_FILENAME, "a") as debug_handle:
debug_handle.write("\n".join(output) + "\n")
def interpret(input_, language, debug_mode=False, profile_mode=False, standard_input="", seed=0):
assert language in {'LMAOcode', 'ROFLcode'}
global DEBUG_MODE
DEBUG_MODE = debug_mode
reset(debug_mode=debug_mode)
random.seed(seed)
global INPUT
INPUT = standard_input
lexer = build_lexer()
possible_tokens = get_possible_tokens(lexer)
# pprint(possible_tokens)
tokens = list(lexer.lex(input_))
check_for_lexing_errors(tokens, language)
# pprint(tokens)
parser = build_parser(possible_tokens, language)
instructions = parser.parse(iter(tokens))
cycles = execute_bad_instructions(instructions, language)
if profile_mode:
return cycles
global OUTPUT
return OUTPUT
def main():
parser = argparse.ArgumentParser(description="""Takes LMAOcode (or ROFLcode)
from stdin, runs it, and
prints output to stdout.""")
parser.add_argument('-l', '--language', choices=['LMAOcode', 'ROFLcode'], default='LMAOcode', help="""
select the language to interpret.""")
parser.add_argument('-d', '--debug', action='store_true', help="""
add this flag to enable debug mode, printing debug
output to stdout""")
parser.add_argument('-p', '--profile', action='store_true', help="""
add this flag to enable profile mode, printing number
of cycles executed to stdout""")
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
default=sys.stdin, help="""
input (should be LMAOcode or ROFLcode) file
(defaults to stdin)""")
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default=sys.stdout, help="""
output file (defaults to stdout)""")
args = parser.parse_args()
source = args.infile.read() + '\n'
output = interpret(source, args.language, args.debug, args.profile)
args.outfile.write(output)
def reset(debug_mode):
global MEMORY
MEMORY.clear()
global SYMBOL_TABLE
SYMBOL_TABLE.clear()
global REGISTER_TABLE
REGISTER_TABLE.clear()
global LABEL_TABLE
LABEL_TABLE.clear()
global INSTRUCTIONS
INSTRUCTIONS = []
global STACK
STACK = []
global OUTPUT
OUTPUT = ""
global INPUT
INPUT = ""
global DEBUG_MODE
DEBUG_MODE = debug_mode
if DEBUG_MODE:
handle = open(DEBUG_FILENAME, "w")
handle.write("")
handle.close()
if __name__ == "__main__":
main()