-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreterv4.py
625 lines (574 loc) · 25.3 KB
/
interpreterv4.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
from enum import Enum
from intbase import InterpreterBase, ErrorType
from brewparse import parse_program
from env_v4 import EnvironmentManager
from type_valuev4 import Type, Value, LazyValue, create_value, get_printable
class ExecStatus(Enum):
CONTINUE = 1
RETURN = 2
RAISE = 3
class Interpreter(InterpreterBase):
binary_operators = {
"+",
"-",
"*",
"/",
"==",
"<",
"<=",
">",
">=",
"!=",
"&&",
"||",
}
def __init__(self, console_output=True, inp=None, trace_output=False):
# call InterpreterBase's constructor
super().__init__(console_output, inp)
self.functions = []
def run(self, program):
ast = parse_program(program)
self.variables = EnvironmentManager()
main_func_node = None
for function in ast.dict["functions"]:
if function.dict["name"] == "main":
main_func_node = function
else:
self.functions.append(function)
if not main_func_node:
super().error(
ErrorType.NAME_ERROR,
"No main() function was found",
)
status, _ = self.run_function(main_func_node)
if status == ExecStatus.RAISE:
super().error(
ErrorType.FAULT_ERROR,
"Uncaught raise",
)
def run_function(self, func_node, args=None, env=None):
if not env:
env = self.variables
self.variables.push_scope("function")
temp_args = func_node.dict["args"]
# instantiate args with the right values
for i in range(len(temp_args)):
self.variables.create(temp_args[i].dict["name"], LazyValue(args[i], env))
for statement_node in func_node.dict["statements"]:
status, res = self.run_statement(statement_node, env)
# if statement_node is a return, return that value
if status == ExecStatus.RETURN or status == ExecStatus.RAISE:
self.variables.pop_scope()
return (status, res)
# otherwise return NIL
self.variables.pop_scope()
return (ExecStatus.CONTINUE, Value(Type.NIL))
def run_statement(self, statement_node, env=None):
if not env:
env = self.variables
match statement_node.elem_type:
# variable definition
case "vardef":
name = statement_node.dict["name"]
if not self.variables.create(name, Value(Type.NIL)):
super().error(
ErrorType.NAME_ERROR,
f"Vardef: Variable {name} defined more than once",
)
# assignment
case "=":
name = statement_node.dict["name"]
node = statement_node.dict["expression"]
lazy = LazyValue(node, self.variables.copy())
if not self.variables.set(name, lazy):
super().error(
ErrorType.NAME_ERROR,
f"Equal: Variable {name} has not been defined",
)
# function call
case "fcall":
status, res = self.run_function_call(statement_node)
return (status, res)
# if statement
case "if":
self.variables.push_scope("if")
condition = statement_node.dict["condition"]
statements = statement_node.dict["statements"]
else_statements = statement_node.dict["else_statements"]
# test if statement
status, cond = self.evaluate_expression_and_lazy(condition)
if status == ExecStatus.RAISE:
return (status, cond)
if cond.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Invalid if condition",
)
if cond.value():
for statement in statements:
status, res = self.run_statement(statement)
if status == ExecStatus.RETURN or status == ExecStatus.RAISE:
self.variables.pop_scope()
return (status, res)
# if if statement fails, test else statement
else:
if else_statements:
for statement in else_statements:
status, res = self.run_statement(statement)
if (
status == ExecStatus.RETURN
or status == ExecStatus.RAISE
):
self.variables.pop_scope()
return (status, res)
self.variables.pop_scope()
# for loop
case "for":
# assignment statement
init = statement_node.dict["init"]
condition = statement_node.dict["condition"]
statements = statement_node.dict["statements"]
status, res = self.run_statement(init)
if status == ExecStatus.RAISE:
return (status, res)
# condition must be true
status, cond = self.evaluate_expression_and_lazy(condition)
if status == ExecStatus.RAISE:
return (status, cond)
if cond.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Invalid for condition",
)
while cond.value():
self.variables.push_scope("for")
for statement in statements:
status, res = self.run_statement(statement)
if status == ExecStatus.RETURN or status == ExecStatus.RAISE:
self.variables.pop_scope()
return (status, res)
self.variables.pop_scope()
update = statement_node.dict["update"]
status, res = self.run_statement(update)
if status == ExecStatus.RAISE:
return (status, res)
status, cond = self.evaluate_expression_and_lazy(condition)
if status == ExecStatus.RAISE:
return (status, cond)
# return
case "return":
expression = statement_node.dict["expression"]
if expression is None:
return (ExecStatus.RETURN, Value(Type.NIL))
return (ExecStatus.RETURN, LazyValue(expression, self.variables.copy()))
# try
case "try":
statements = statement_node.dict["statements"]
catchers = statement_node.dict["catchers"]
self.variables.push_scope("try")
for statement in statements:
status, res = self.run_statement(statement)
if status == ExecStatus.RETURN:
self.variables.pop_scope()
return (status, res)
elif status == ExecStatus.RAISE:
self.variables.pop_scope()
for catch in catchers:
catch_type, catch_statements = self.run_statement(catch)
# matching raise / catch
# execute catch block
if catch_type == res.value():
self.variables.push_scope("catch")
for statement in catch_statements:
status, res = self.run_statement(statement)
if (
status == ExecStatus.RETURN
or status == ExecStatus.RAISE
):
self.variables.pop_scope()
return (status, res)
self.variables.pop_scope()
return (ExecStatus.CONTINUE, None)
# no match
return (ExecStatus.RAISE, res)
# try block finishes normally
self.variables.pop_scope()
return (ExecStatus.CONTINUE, None)
# catch
case "catch":
type = statement_node.dict["exception_type"]
statements = statement_node.dict["statements"]
return (type, statements)
# raise
case "raise":
type = statement_node.dict["exception_type"]
_, value = self.evaluate_expression(type)
if isinstance(value, LazyValue):
_, value = self.evaluate_lazy(value)
if value.type() != Type.STRING:
super().error(
ErrorType.TYPE_ERROR,
"Raise type not a string",
)
# return what we're raising
return (ExecStatus.RAISE, value)
return (ExecStatus.CONTINUE, None)
def evaluate_expression(self, expression_node, env=None):
if not env:
env = self.variables
# binary operations
if expression_node.elem_type in Interpreter.binary_operators:
match expression_node.elem_type:
case "+":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if (op1.type() == Type.INT and op2.type() == Type.INT) or (
op1.type() == Type.STRING and op2.type() == Type.STRING
):
return (
ExecStatus.CONTINUE,
create_value(op1.value() + op2.value()),
)
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of arithmetic operation on non-integer types",
)
case "-":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of arithmetic operation on non-integer types",
)
return (
ExecStatus.CONTINUE,
Value(Type.INT, op1.value() - op2.value()),
)
case "*":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of arithmetic operation on non-integer types",
)
return (
ExecStatus.CONTINUE,
Value(Type.INT, op1.value() * op2.value()),
)
case "/":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
# divide by 0
if op2.value() == 0:
return (ExecStatus.RAISE, Value(Type.STRING, "div0"))
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of arithmetic operation on non-integer types",
)
return (
ExecStatus.CONTINUE,
Value(Type.INT, op1.value() // op2.value()),
)
case "==":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != op2.type():
return (ExecStatus.CONTINUE, Value(Type.BOOL, False))
return (
ExecStatus.CONTINUE,
Value(Type.BOOL, op1.value() == op2.value()),
)
case "<":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison <",
)
return (
ExecStatus.CONTINUE,
Value(Type.BOOL, op1.value() < op2.value()),
)
case "<=":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison <=",
)
return (
ExecStatus.CONTINUE,
Value(Type.BOOL, op1.value() <= op2.value()),
)
case ">":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison >",
)
return (
ExecStatus.CONTINUE,
Value(Type.BOOL, op1.value() > op2.value()),
)
case ">=":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison >=",
)
return (
ExecStatus.CONTINUE,
Value(Type.BOOL, op1.value() >= op2.value()),
)
case "!=":
status, op1, op2 = self.get_ops(expression_node, env)
if status == ExecStatus.RAISE:
return (ExecStatus.RAISE, op1 or op2)
if op1.type() != op2.type():
return (ExecStatus.CONTINUE, Value(Type.BOOL, True))
return (
ExecStatus.CONTINUE,
Value(Type.BOOL, op1.value() != op2.value()),
)
case "&&":
status, op1 = self.evaluate_expression_and_lazy(
expression_node.dict["op1"], env
)
if status == ExecStatus.RAISE:
return (status, op1)
if op1.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison &&",
)
if not op1.value():
return (ExecStatus.CONTINUE, Value(Type.BOOL, False))
status, op2 = self.evaluate_expression_and_lazy(
expression_node.dict["op2"], env
)
if status == ExecStatus.RAISE:
return (status, op2)
if op2.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison &&",
)
return (ExecStatus.CONTINUE, Value(Type.BOOL, op2.value()))
case "||":
status, op1 = self.evaluate_expression_and_lazy(
expression_node.dict["op1"], env
)
if status == ExecStatus.RAISE:
return (status, op1)
if op1.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison ||",
)
if op1.value():
return (ExecStatus.CONTINUE, Value(Type.BOOL, True))
status, op2 = self.evaluate_expression_and_lazy(
expression_node.dict["op2"], env
)
if status == ExecStatus.RAISE:
return (status, op2)
if op2.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison &&",
)
return (ExecStatus.CONTINUE, Value(Type.BOOL, op2.value()))
else:
match expression_node.elem_type:
# value node
case "int" | "string" | "bool":
return (
ExecStatus.CONTINUE,
create_value(expression_node.dict["val"]),
)
case "nil":
return (ExecStatus.CONTINUE, Value(Type.NIL))
# variable node
case "var":
name = expression_node.dict["name"]
result = env.get(name)
if result is None:
super().error(
ErrorType.NAME_ERROR,
f"EE Var: Variable {name} has not been defined",
)
return (ExecStatus.CONTINUE, result)
# unary operations
case "neg":
status, op1 = self.evaluate_expression_and_lazy(
expression_node.dict["op1"], env
)
if status == ExecStatus.RAISE:
return (status, op1)
if op1.type() != Type.INT and op1.type() != Type.STRING:
super().error(
ErrorType.TYPE_ERROR,
"Invalid negation type",
)
return (ExecStatus.CONTINUE, Value(Type.INT, -op1.value()))
case "!":
status, op1 = self.evaluate_expression_and_lazy(
expression_node.dict["op1"], env
)
if status == ExecStatus.RAISE:
return (status, op1)
if op1.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of not operation on non-boolean type",
)
return (
(ExecStatus.CONTINUE, Value(Type.BOOL, True))
if op1.value() is False
else (ExecStatus.CONTINUE, Value(Type.BOOL, False))
)
# function call
case "fcall":
status, res = self.run_function_call(expression_node, env)
return (status, res)
def run_function_call(self, function_call, env=None):
if not env:
env = self.variables
name = function_call.dict["name"]
arg_nodes = function_call.dict["args"]
match name:
case "print":
res = ""
for arg in function_call.dict["args"]:
status, output = self.evaluate_expression_and_lazy(arg, env)
if status == ExecStatus.RAISE:
return (status, output)
res += get_printable(output)
super().output(res)
return (ExecStatus.CONTINUE, Value(Type.NIL))
case "inputi":
if len(function_call.dict["args"]) > 1:
super().error(
ErrorType.NAME_ERROR,
"No inputi() function found that takes > 1 parameter",
)
elif len(function_call.dict["args"]) == 1:
status, output = self.evaluate_expression_and_lazy(
function_call.dict["args"][0], env
)
if status == ExecStatus.RAISE:
return (status, output)
super().output(output.value())
return (ExecStatus.CONTINUE, Value(Type.INT, int(super().get_input())))
case "inputs":
if len(function_call.dict["args"]) > 1:
super().error(
ErrorType.NAME_ERROR,
"No inputs() function found that takes > 1 parameter",
)
elif len(function_call.dict["args"]) == 1:
status, output = self.evaluate_expression_and_lazy(
function_call.dict["args"][0], env
)
if status == ExecStatus.RAISE:
return (status, output)
super().output(output.value())
return (ExecStatus.CONTINUE, Value(Type.STRING, super().get_input()))
case _:
for function in self.functions:
# if same name and same amount of args
if function.dict["name"] == name and len(arg_nodes) == len(
function.dict["args"]
):
status, res = self.run_function(function, arg_nodes, env.copy())
if status == ExecStatus.RETURN:
return (ExecStatus.CONTINUE, res)
elif status == ExecStatus.RAISE:
return (status, res)
# return NIL if no return value
return (ExecStatus.CONTINUE, Value(Type.NIL))
super().error(
ErrorType.NAME_ERROR,
f"Function {name} has not been defined",
)
def evaluate_lazy(self, val):
if not val.evaluated():
status, res = self.evaluate_expression(val.ast(), val.env())
if status == ExecStatus.RAISE:
return (status, res)
while isinstance(res, LazyValue):
status, res = self.evaluate_lazy(res)
if status == ExecStatus.RAISE:
return (status, res)
val.set_value(res)
val.set_eval()
# should return a fully evaluated Value
return (ExecStatus.CONTINUE, val.value())
def evaluate_expression_and_lazy(self, exp, env=None):
status, output = self.evaluate_expression(exp, env)
if status == ExecStatus.RAISE:
return (status, output)
if isinstance(output, LazyValue):
status, output = self.evaluate_lazy(output)
if status == ExecStatus.RAISE:
return (status, output)
return (status, output)
# gets op1 and op2 when evaluating an expression
def get_ops(self, expression_node, env):
status1, op1 = self.evaluate_expression_and_lazy(
expression_node.dict["op1"], env
)
if status1 == ExecStatus.RAISE:
return (status1, op1, None)
status2, op2 = self.evaluate_expression_and_lazy(
expression_node.dict["op2"], env
)
if status2 == ExecStatus.RAISE:
return (status2, None, op2)
return (ExecStatus.CONTINUE, op1, op2)
if __name__ == "__main__":
# program = """func main() {
# var a;
# foo("entered function");
# }
# func foo(a) {
# print(a);
# var a;
# }
# """
program = """func foo(x) {
var y;
y = 5;
print(x);
}
func main() {
var y;
y = 10;
foo(y);
}
/*
*OUT*
10
*OUT*
*/
"""
interpreter = Interpreter()
interpreter.run(program)