-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinterpreterv3.py
550 lines (476 loc) · 22.4 KB
/
interpreterv3.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
import copy
from enum import Enum
from brewparse import parse_program
from env_v3 import EnvironmentManager
from intbase import InterpreterBase, ErrorType
from type_valuev3 import *
class ExecStatus(Enum):
CONTINUE = 1
RETURN = 2
# Main interpreter class
class Interpreter(InterpreterBase):
# constants
NIL_VALUE = TypeManager.create_value(InterpreterBase.NIL_DEF)
VOID_VALUE = TypeManager.create_value(InterpreterBase.VOID_DEF)
TRUE_VALUE = TypeManager.create_value(InterpreterBase.TRUE_DEF)
BIN_OPS = {"+", "-", "*", "/", "==", "!=", ">", ">=", "<", "<=", "||", "&&"}
# methods
def __init__(self, console_output=True, inp=None, trace_output=False):
super().__init__(console_output, inp)
self.trace_output = trace_output
self.__setup_ops()
self.__call_stack = []
self.type_manager = TypeManager()
self.env = EnvironmentManager()
self.func_name_to_ast = {}
# run a program that's provided in a string
# usese the provided Parser found in brewparse.py to parse the program
# into an abstract syntax tree (ast)
def run(self, program):
ast = parse_program(program)
self.__set_up_struct_table(ast)
self.__set_up_function_table(ast)
self.__call_func_aux("main", [])
def __set_up_struct_table(self, ast):
struct_asts = ast.get("structs")
if struct_asts is None:
return
for struct_ast in struct_asts:
struct_type_name = struct_ast.get("name")
if not self.type_manager.define_struct(struct_ast):
super().error(
ErrorType.TYPE_ERROR,
f"Invalid type when defining struct {struct_type_name}"
)
def __set_up_function_table(self, ast):
for func_def in ast.get("functions"):
func_name = func_def.get("name")
formal_args = func_def.get("args")
return_type = func_def.get("return_type")
num_params = len(formal_args)
if func_name not in self.func_name_to_ast:
self.func_name_to_ast[func_name] = {}
self.func_name_to_ast[func_name][num_params] = func_def
self.__validate_formal_parameter_types_and_return(func_name, formal_args, return_type)
# DOCUMENT
def __validate_formal_parameter_types_and_return(self, func_name, formal_args, return_type):
for formal_ast in formal_args:
arg_name = formal_ast.get("name")
arg_type = formal_ast.get("var_type")
if not self.type_manager.valid_var_type(arg_type):
super().error(
ErrorType.TYPE_ERROR,
f"Invalid type for formal parameter {arg_name} in function {func_name}"
)
if not self.type_manager.valid_var_type(return_type) and return_type != Type.VOID:
super().error(
ErrorType.TYPE_ERROR,
f"Invalid return type {return_type} in function {func_name}"
)
def __get_func_by_name(self, name, num_params):
if name not in self.func_name_to_ast:
super().error(ErrorType.NAME_ERROR, f"Function {name} not found")
candidate_funcs = self.func_name_to_ast[name]
if num_params not in candidate_funcs:
super().error(
ErrorType.NAME_ERROR,
f"Function {name} taking {num_params} params not found",
)
return candidate_funcs[num_params]
def __get_return_type_of_current_function(self):
current_func = self.__call_stack[-1]
return current_func.get("return_type")
def __run_statements(self, statements):
self.env.push_block()
for statement in statements:
if self.trace_output:
print(statement)
status, return_val = self.__run_statement(statement)
if status == ExecStatus.RETURN:
self.env.pop_block()
return (status, return_val)
self.env.pop_block()
return (ExecStatus.CONTINUE, Interpreter.NIL_VALUE)
def __run_statement(self, statement):
status = ExecStatus.CONTINUE
return_val = None
if statement.elem_type == InterpreterBase.FCALL_NODE:
self.__call_func(statement)
elif statement.elem_type == "=":
self.__assign(statement)
elif statement.elem_type == InterpreterBase.VAR_DEF_NODE:
self.__var_def(statement)
elif statement.elem_type == InterpreterBase.RETURN_NODE:
status, return_val = self.__do_return(statement)
elif statement.elem_type == Interpreter.IF_NODE:
status, return_val = self.__do_if(statement)
elif statement.elem_type == Interpreter.FOR_NODE:
status, return_val = self.__do_for(statement)
return (status, return_val)
def __call_func(self, call_node):
func_name = call_node.get("name")
actual_args = call_node.get("args")
return self.__call_func_aux(func_name, actual_args)
def __call_func_aux(self, func_name, actual_args):
if func_name == "print":
return self.__call_print(actual_args)
if func_name == "inputi" or func_name == "inputs":
return self.__call_input(func_name, actual_args)
func_ast = self.__get_func_by_name(func_name, len(actual_args))
self.__call_stack.append(func_ast)
formal_args = func_ast.get("args")
return_type = self.__get_return_type_of_current_function()
if len(actual_args) != len(formal_args):
super().error(
ErrorType.NAME_ERROR,
f"Function {func_ast.get('name')} with {len(actual_args)} args not found",
)
# first evaluate all of the actual parameters and associate them with the formal parameter names
args = {}
for formal_ast, actual_ast in zip(formal_args, actual_args):
result = copy.copy(self.__eval_expr(actual_ast))
arg_name = formal_ast.get("name")
arg_type = formal_ast.get("var_type")
if not self.__compatible_types_for_assignment(Variable(arg_type), result):
super().error(
ErrorType.TYPE_ERROR,
f"Type mismatch on formal parameter {arg_name}"
)
args[arg_name] = Variable(arg_type, self.__coerce(arg_type, result))
# then create the new activation record
self.env.push_func()
# and add the formal arguments to the activation record
for arg_name, variable in args.items():
self.env.create(arg_name, variable)
exec_status, return_val = self.__run_statements(func_ast.get("statements"))
self.env.pop_func()
self.__call_stack.pop()
if exec_status == ExecStatus.RETURN:
return return_val
return self.type_manager.create_default_value(return_type) # DOCUMENT no return statement returns default value
def __coerce(self, target_type, value_obj):
if target_type == Type.BOOL and value_obj.type() == Type.INT:
return Value(Type.BOOL, bool(value_obj.value()))
# We "coerce" nil when assigning it to an variable with declared structure type
if self.type_manager.is_struct_type(target_type) and value_obj.type() == Type.NIL:
return self.type_manager.create_default_value(target_type)
return value_obj
def __call_print(self, args):
output = ""
for arg in args:
result = self.__eval_expr(arg) # result is a Value object
if result.type() == Type.VOID:
super().error(ErrorType.TYPE_ERROR, "Void not allowed as argument")
output = output + TypeManager.get_printable(result) # DOCUMENT need to be able to print "nil" now, undefined for a struct
super().output(output)
return Interpreter.VOID_VALUE
def __call_input(self, name, args):
if args is not None and len(args) == 1:
result = self.__eval_expr(args[0])
if result.type() == Type.VOID:
super().error(ErrorType.TYPE_ERROR, "Void not allowed as argument")
super().output(TypeManager.get_printable(result))
elif args is not None and len(args) > 1:
super().error(
ErrorType.NAME_ERROR, "No inputi() function that takes > 1 parameter"
)
inp = super().get_input()
if name == "inputi":
return Value(Type.INT, int(inp))
if name == "inputs":
return Value(Type.STRING, inp)
def __assign(self, assign_ast):
var_name = assign_ast.get("name")
lhs_var = self.__get_variable(var_name)
rhs_val = self.__eval_expr(assign_ast.get("expression"))
if not self.__compatible_types_for_assignment(lhs_var, rhs_val): # DOCUMENT
super().error(
ErrorType.TYPE_ERROR, f"Type mismatch {lhs_var.type()} vs {rhs_val.type()} in assignment"
)
# perform coercion
if lhs_var.type() == Type.BOOL:
rhs_val = self.__coerce(Type.BOOL, rhs_val)
elif self.type_manager.is_struct_type(lhs_var.type()) and rhs_val.type() == Type.NIL:
rhs_val = self.__coerce(lhs_var.type(), rhs_val)
lhs_var.set_value(rhs_val)
def __get_variable(self, var_name):
split_var = var_name.split(".")
base_var = self.env.get(split_var[0])
if base_var is None:
super().error(
ErrorType.NAME_ERROR, f"Undefined variable {split_var[0]}"
)
while len(split_var) > 1:
val_type = base_var.value().type()
var_val = base_var.value().value()
if val_type == Type.NIL or (self.type_manager.is_struct_type(val_type) and var_val is None):
super().error(
ErrorType.FAULT_ERROR, f"Error dereferencing nil value {split_var[0]} in {var_name}"
)
if not self.type_manager.is_struct_type(val_type):
super().error(
ErrorType.TYPE_ERROR, f"Dot used with non-struct {base_var} in {var_name}"
)
split_var.pop(0)
base_var = var_val.get(split_var[0], None) # var_val is a dictionary which implements the struct "field" -> Variable object
if base_var is None:
super().error(
ErrorType.NAME_ERROR, f"Unknown member {split_var[0]} in {var_name}"
)
return base_var
def __var_def(self, var_ast):
var_name = var_ast.get("name")
var_type = var_ast.get("var_type") # DOCUMENT change in AST and in syntax
default_value = self.type_manager.create_default_value(var_type) # DOCUMENT: default value for defined variables
variable = Variable(var_type, default_value)
if default_value is None or not self.type_manager.valid_var_type(var_type):
super().error(
ErrorType.TYPE_ERROR, f"Unknown/invalid type specified {var_type}"
)
if not self.env.create(var_name, variable):
super().error(
ErrorType.NAME_ERROR, f"Duplicate definition for variable {var_name}"
)
def __eval_expr(self, expr_ast):
if expr_ast.elem_type == InterpreterBase.NIL_NODE:
return Interpreter.NIL_VALUE
if expr_ast.elem_type == InterpreterBase.INT_NODE:
return Value(Type.INT, expr_ast.get("val"))
if expr_ast.elem_type == InterpreterBase.STRING_NODE:
return Value(Type.STRING, expr_ast.get("val"))
if expr_ast.elem_type == InterpreterBase.BOOL_NODE:
return Value(Type.BOOL, expr_ast.get("val"))
if expr_ast.elem_type == InterpreterBase.VAR_NODE:
var_name = expr_ast.get("name")
variable = self.__get_variable(var_name) # error checks
return variable.value()
if expr_ast.elem_type == InterpreterBase.FCALL_NODE:
return self.__call_func(expr_ast)
if expr_ast.elem_type == InterpreterBase.NEW_NODE:
return self.__new_struct(expr_ast)
if expr_ast.elem_type in Interpreter.BIN_OPS:
return self.__eval_op(expr_ast)
if expr_ast.elem_type == Interpreter.NEG_NODE:
return self.__eval_unary_neg(expr_ast)
if expr_ast.elem_type == Interpreter.NOT_NODE:
return self.__eval_unary_not(expr_ast)
def __new_struct(self, new_ast):
var_type = new_ast.get("var_type")
default_value = self.type_manager.new_struct_value(var_type)
if default_value is None:
super().error(
ErrorType.TYPE_ERROR,
f"Invalid type {var_type} for new operation",
)
return default_value
def __eval_op(self, arith_ast):
left_value_obj = self.__eval_expr(arith_ast.get("op1"))
right_value_obj = self.__eval_expr(arith_ast.get("op2"))
ltype = left_value_obj.type()
rtype = right_value_obj.type()
if ltype == rtype and ltype in self.op_to_lambda:
f = self.op_to_lambda[ltype].get(arith_ast.elem_type)
if f is not None:
return f(left_value_obj, right_value_obj)
if arith_ast.elem_type in ["==", "!="]:
return self.__eval_compare(arith_ast.elem_type, left_value_obj, right_value_obj)
if arith_ast.elem_type in ["||", "&&"]:
return self.__eval_and_or(arith_ast.elem_type, left_value_obj, right_value_obj)
super().error(
ErrorType.TYPE_ERROR,
f"Incompatible operator {arith_ast.elem_type} for types {left_value_obj.type()} and {right_value_obj.type()}",
)
def __eval_and_or(self, oper, obj1, obj2):
# DOCUMENT: coercion comparison rules
type1 = obj1.type()
type2 = obj2.type()
if Type.VOID in (type1, type2):
super().error(
ErrorType.TYPE_ERROR,
"Can't compare void type"
)
allowed_types = (Type.BOOL, Type.INT)
if type1 not in allowed_types or type2 not in allowed_types:
super().error(
ErrorType.TYPE_ERROR,
f"Invalid types used with operator {oper}"
)
obj1 = self.__coerce(Type.BOOL, obj1)
obj2 = self.__coerce(Type.BOOL, obj2)
if oper == "||":
return Value(Type.BOOL, obj1.value() or obj2.value())
return Value(Type.BOOL, obj1.value() and obj2.value())
def __eval_compare(self, oper, obj1, obj2):
type1 = obj1.type()
type2 = obj2.type()
# No comparison allowed against void, period
if Type.VOID in (type1, type2):
super().error(
ErrorType.TYPE_ERROR,
"Can't compare void type"
)
# Create a comparison function based on operation == or !=
cmp = (lambda x,y: x == y) if oper == "==" else (lambda x,y: x != y)
# If the two types match, then just compare their values and get a result
if type1 == type2:
return Value(Type.BOOL, cmp(obj1.value(), obj2.value())) # DOCUMENT that we compare object references for structs
# Handle the case where we're comparing a valid struct to nil
if Type.NIL in (type1, type2):
# two literal nils already handled by the case above
if self.type_manager.is_struct_type(type1):
return Value(Type.BOOL, cmp(obj1.value(), None))
elif self.type_manager.is_struct_type(type2):
return Value(Type.BOOL, cmp(obj2.value(), None))
# trying to compare some type other than a struct to nil; error
super().error(
ErrorType.TYPE_ERROR,
"Can't compare type to nil"
)
# Handle the case where we're comparing int to bool, or bool to int (already handled bool to bool up above)
# For this case we need to do our coercion to bool before comparing
if Type.BOOL in (type1, type2):
if Type.INT in (type1, type2):
obj1 = self.__coerce(Type.BOOL, obj1)
obj2 = self.__coerce(Type.BOOL, obj2)
return Value(Type.BOOL, cmp(obj1.value(), obj2.value()))
super().error(
ErrorType.TYPE_ERROR,
f"Can't compare unrelated types {type1} and {type2}"
)
def __compatible_types(self, oper, obj1, obj2):
# DOCUMENT: allow comparisons ==/!= of anything against anything
type1 = obj1.type()
type2 = obj2.type()
if type1 == Type.VOID or type2 == Type.VOID:
return False
if oper in ["==", "!="]:
if (self.type_manager.is_struct_type(type1) and type2 == Type.NIL) or \
(self.type_manager.is_struct_type(type2) and type1 == Type.NIL):
return True
if oper in ["||","&&"]:
if (type1 == Type.INT and type2 == Type.BOOL) or \
(type1 == Type.BOOL and type2 == Type.INT):
return True
return obj1.type() == obj2.type()
def __compatible_types_for_assignment(self, lhs_variable, rhs_value):
lhs_type = lhs_variable.type()
rhs_type = rhs_value.type()
if lhs_type == rhs_type:
return True
if lhs_type == Type.BOOL and rhs_type == Type.INT:
return True
return self.type_manager.is_struct_type(lhs_type) and rhs_value.type() == Type.NIL
def __eval_unary_neg(self, arith_ast):
value_obj = self.__eval_expr(arith_ast.get("op1"))
if value_obj.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
f"Incompatible type for {arith_ast.elem_type} operation",
)
return Value(Type.INT, -value_obj.value())
def __eval_unary_not(self, arith_ast):
value_obj = self.__eval_expr(arith_ast.get("op1"))
val_type = value_obj.type()
if val_type != Type.BOOL and val_type != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
f"Incompatible type for {arith_ast.elem_type} operation",
)
notted = Value(Type.BOOL, not self.__coerce(Type.BOOL, value_obj).value())
return notted
def __setup_ops(self):
self.op_to_lambda = {}
# set up operations on integers
self.op_to_lambda[Type.INT] = {}
self.op_to_lambda[Type.INT]["+"] = lambda x, y: Value(
x.type(), x.value() + y.value()
)
self.op_to_lambda[Type.INT]["-"] = lambda x, y: Value(
x.type(), x.value() - y.value()
)
self.op_to_lambda[Type.INT]["*"] = lambda x, y: Value(
x.type(), x.value() * y.value()
)
self.op_to_lambda[Type.INT]["/"] = lambda x, y: Value(
x.type(), x.value() // y.value()
)
self.op_to_lambda[Type.INT]["=="] = lambda x, y: Value(
Type.BOOL, x.type() == y.type() and x.value() == y.value()
)
self.op_to_lambda[Type.INT]["!="] = lambda x, y: Value(
Type.BOOL, x.type() != y.type() or x.value() != y.value()
)
self.op_to_lambda[Type.INT]["<"] = lambda x, y: Value(
Type.BOOL, x.value() < y.value()
)
self.op_to_lambda[Type.INT]["<="] = lambda x, y: Value(
Type.BOOL, x.value() <= y.value()
)
self.op_to_lambda[Type.INT][">"] = lambda x, y: Value(
Type.BOOL, x.value() > y.value()
)
self.op_to_lambda[Type.INT][">="] = lambda x, y: Value(
Type.BOOL, x.value() >= y.value()
)
# set up operations on strings
self.op_to_lambda[Type.STRING] = {}
self.op_to_lambda[Type.STRING]["+"] = lambda x, y: Value(
x.type(), x.value() + y.value()
)
# set up operations on void
self.op_to_lambda[Type.VOID] = {}
def __do_if(self, if_ast):
cond_ast = if_ast.get("condition")
result = self.__eval_expr(cond_ast)
if result.type() != Type.BOOL and result.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible type for if condition",
)
if result.value():
statements = if_ast.get("statements")
status, return_val = self.__run_statements(statements)
return (status, return_val)
else:
else_statements = if_ast.get("else_statements")
if else_statements is not None:
status, return_val = self.__run_statements(else_statements)
return (status, return_val)
return (ExecStatus.CONTINUE, Interpreter.NIL_VALUE)
def __do_for(self, for_ast):
init_ast = for_ast.get("init")
cond_ast = for_ast.get("condition")
update_ast = for_ast.get("update")
self.__run_statement(init_ast) # initialize counter variable
run_for = Interpreter.TRUE_VALUE
while run_for.value():
run_for = self.__eval_expr(cond_ast) # check for-loop condition
if run_for.type() != Type.BOOL and run_for.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible type for for condition",
)
if run_for.value():
statements = for_ast.get("statements")
status, return_val = self.__run_statements(statements)
if status == ExecStatus.RETURN:
return status, return_val
self.__run_statement(update_ast) # update counter variable
return (ExecStatus.CONTINUE, Interpreter.NIL_VALUE)
def __do_return(self, return_ast):
expr_ast = return_ast.get("expression")
func_ret_type = self.__get_return_type_of_current_function()
if expr_ast is None:
return (ExecStatus.RETURN, self.type_manager.create_default_value(func_ret_type)) # DOCUMENT return; as returning default value
value_obj = copy.copy(self.__eval_expr(expr_ast)) # DOCUMENT
if value_obj.type() == Type.VOID:
super().error(
ErrorType.TYPE_ERROR,
"Cannot use void in return value"
)
if not self.__compatible_types_for_assignment(Variable(func_ret_type), value_obj):
super().error(
ErrorType.TYPE_ERROR,
f"Returned value's type {value_obj.type()} is inconsistent with function's return type {func_ret_type}"
)
return (ExecStatus.RETURN, self.__coerce(func_ret_type, value_obj)) # DOCUMENT all coercions!