-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreterv3.py
757 lines (691 loc) · 28.2 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
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
from intbase import InterpreterBase, ErrorType
from brewparse import parse_program
from env_v3 import EnvironmentManager
from type_valuev3 import Type, Value, create_value, get_printable
class Interpreter(InterpreterBase):
binary_operators = {
"+",
"-",
"*",
"/",
"==",
"<",
"<=",
">",
">=",
"!=",
"&&",
"||",
}
default_types = {"bool": False, "int": 0, "string": "", "void": None}
def __init__(self, console_output=True, inp=None, trace_output=False):
# call InterpreterBase's constructor
super().__init__(console_output, inp)
self.functions = []
self.structs = {}
def run(self, program):
ast = parse_program(program)
self.variables = EnvironmentManager()
for struct in ast.dict["structs"]:
self.structs[struct.dict["name"]] = struct
main_func_node = None
for function in ast.dict["functions"]:
name = function.dict["name"]
# check invalid args
for arg in function.dict["args"]:
if (
arg.dict["var_type"] not in Interpreter.default_types
and arg.dict["var_type"] not in self.structs
):
super().error(
ErrorType.TYPE_ERROR,
f"Invalid argument type for {name}",
)
# check invalid returns
if (
function.dict["return_type"] not in Interpreter.default_types
and function.dict["return_type"] not in self.structs
):
super().error(
ErrorType.TYPE_ERROR,
f"Invalid return type for {name}",
)
if 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",
)
self.run_function(main_func_node)
def run_function(self, func_node, args=None):
self.variables.push_scope("function")
# temp_args is the args within the function
# args is the information that's being passed into the function
temp_args = func_node.dict["args"]
return_type = func_node.dict["return_type"]
# instantiate args with the right values
for i in range(len(temp_args)):
name = temp_args[i].dict["name"]
type = temp_args[i].dict["var_type"]
# assigning an int to a bool
if type == Type.BOOL and args[i].type() == Type.INT:
args[i] = self.check_bool(args[i])
if type not in self.structs and args[i].type() != type:
super().error(
ErrorType.TYPE_ERROR,
f"{args[i].type()} cannot be assigned to a {type}",
)
if (
type in self.structs
and args[i].type() in self.structs
and type != args[i].type()
):
super().error(
ErrorType.TYPE_ERROR,
f"Struct type {args[i].type()} cannot be assigned to struct type {type}",
)
if args[i].type() == Type.NIL and type in self.structs:
self.variables.create(name, Value(type))
else:
self.variables.create(name, args[i])
for statement_node in func_node.dict["statements"]:
res = self.run_statement(statement_node)
# if statement_node is a return, return that value
if res:
self.variables.pop_scope()
if res.type() == Type.VOID:
return self.return_default(return_type)
self.check_return(return_type, res)
return res
# otherwise, return the default return value
self.variables.pop_scope()
return self.return_default(return_type)
def run_statement(self, statement_node):
match statement_node.elem_type:
# variable definition
case "vardef":
name = statement_node.dict["name"]
type = statement_node.dict["var_type"]
self.type_to_variable(name, type)
# assignment
case "=":
name = statement_node.dict["name"]
node = statement_node.dict["expression"]
value = self.evaluate_expression(node)
# if struct variable
if "." in name:
self.set_nested_variable(name, value)
else:
if not self.variables.get(name):
super().error(
ErrorType.NAME_ERROR,
f"Assign: Variable {name} has not been defined",
)
type = self.variables.get(name).type()
self.check_return(type, value)
# setting the variable
if type in self.structs and value.type() == Type.NIL:
res = self.variables.set(name, Value(type))
else:
if value.type() == Type.NIL:
super().error(
ErrorType.TYPE_ERROR,
f"Assign: Assigning {name} with a nil type",
)
res = self.variables.set(name, value)
if not res:
super().error(
ErrorType.NAME_ERROR,
f"Assign: Unable to set {name}",
)
# function call
case "fcall":
self.run_function_call(statement_node)
# 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
cond = self.evaluate_expression(condition)
# coercion if int
cond = self.check_bool(cond)
if cond.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Invalid if condition",
)
if cond.value():
for statement in statements:
res = self.run_statement(statement)
if res:
self.variables.pop_scope()
return res
# if if statement fails, test else statement
else:
if else_statements:
for statement in else_statements:
res = self.run_statement(statement)
if res:
self.variables.pop_scope()
return 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"]
self.run_statement(init)
# condition must be true
cond = self.evaluate_expression(condition)
# coercion if int
cond = self.check_bool(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:
res = self.run_statement(statement)
if res:
self.variables.pop_scope()
return res
self.variables.pop_scope()
update = statement_node.dict["update"]
self.run_statement(update)
cond = self.evaluate_expression(condition)
# return
case "return":
expression = statement_node.dict["expression"]
return (
self.evaluate_expression(expression)
if expression
else Value(Type.VOID)
)
def evaluate_expression(self, expression_node):
# binary operations
if expression_node.elem_type in Interpreter.binary_operators:
op1 = self.evaluate_expression(expression_node.dict["op1"])
op2 = self.evaluate_expression(expression_node.dict["op2"])
# print("op1", op1.type(), op1.value())
# print("op2", op2.type(), op2.value())
match expression_node.elem_type:
case "+":
if (op1.type() == Type.INT and op2.type() == Type.INT) or (
op1.type() == Type.STRING and op2.type() == Type.STRING
):
return create_value(op1.value() + op2.value())
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of arithmetic operation on non-integer types",
)
case "-":
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 Value(Type.INT, op1.value() - op2.value())
case "*":
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 Value(Type.INT, op1.value() * op2.value())
case "/":
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 Value(Type.INT, op1.value() // op2.value())
case "==":
if op1.type() == Type.VOID or op2.type() == Type.VOID:
super().error(
ErrorType.TYPE_ERROR,
"Comparing with a void value",
)
if (
op1.type() in self.structs or op2.type() in self.structs
) and not (
op1.type() == op2.type()
or op1.type() == Type.NIL
or op2.type() == Type.NIL
):
super().error(
ErrorType.TYPE_ERROR,
"Comparing a struct type to a different type",
)
if op1.value() is None and op2.value() is None:
return Value(Type.BOOL, True)
if op1.type() == Type.BOOL or op2.type() == Type.BOOL:
op1 = self.check_bool(op1)
op2 = self.check_bool(op2)
if (
op1.type() in Interpreter.default_types
or op2.type() in Interpreter.default_types
) and op1.type() != op2.type():
super().error(
ErrorType.TYPE_ERROR,
"Comparing different primitive types",
)
return Value(Type.BOOL, op1.value() == op2.value())
case "<":
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison <",
)
return Value(Type.BOOL, op1.value() < op2.value())
case "<=":
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison <=",
)
return Value(Type.BOOL, op1.value() <= op2.value())
case ">":
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison >",
)
return Value(Type.BOOL, op1.value() > op2.value())
case ">=":
if op1.type() != Type.INT or op2.type() != Type.INT:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison >=",
)
return Value(Type.BOOL, op1.value() >= op2.value())
case "!=":
if op1.type() == Type.VOID or op2.type() == Type.VOID:
super().error(
ErrorType.TYPE_ERROR,
"Comparing with a void value",
)
if (
op1.type() in self.structs or op2.type() in self.structs
) and not (
op1.type() == op2.type()
or op1.type() == Type.NIL
or op2.type() == Type.NIL
):
super().error(
ErrorType.TYPE_ERROR,
"Comparing a struct type to a different type",
)
if op1.value() is None and op2.value() is None:
return Value(Type.BOOL, False)
if op1.type() == Type.BOOL or op2.type() == Type.BOOL:
op1 = self.check_bool(op1)
op2 = self.check_bool(op2)
if (
op1.type() in Interpreter.default_types
or op2.type() in Interpreter.default_types
) and op1.type() != op2.type():
super().error(
ErrorType.TYPE_ERROR,
"Comparing different primitive types",
)
return Value(Type.BOOL, op1.value() != op2.value())
case "&&":
op1 = self.check_bool(op1)
op2 = self.check_bool(op2)
if op1.type() != Type.BOOL or op2.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison &&",
)
left, right = op1.value(), op2.value()
if left is True and right is True:
return Value(Type.BOOL, True)
else:
return Value(Type.BOOL, False)
case "||":
op1 = self.check_bool(op1)
op2 = self.check_bool(op2)
if op1.type() != Type.BOOL or op2.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Incompatible types for comparison ||",
)
left, right = op1.value(), op2.value()
if left is True or right is True:
return Value(Type.BOOL, True)
else:
return Value(Type.BOOL, False)
else:
match expression_node.elem_type:
# value node
case "int" | "string" | "bool":
return create_value(expression_node.dict["val"])
case "nil":
return Value(Type.NIL)
# variable node
case "var":
name = expression_node.dict["name"]
result = self.get_nested_variable(name)
return result
# unary operations
case "neg":
op1 = self.evaluate_expression(expression_node.dict["op1"])
if op1.type() != Type.INT and op1.type() != Type.STRING:
super().error(
ErrorType.TYPE_ERROR,
"Invalid negation type",
)
return Value(Type.INT, -op1.value())
case "!":
op1 = self.evaluate_expression(expression_node.dict["op1"])
op1 = self.check_bool(op1)
if op1.type() != Type.BOOL:
super().error(
ErrorType.TYPE_ERROR,
"Illegal usage of not operation on non-boolean type",
)
return (
Value(Type.BOOL, True)
if op1.value() is False
else Value(Type.BOOL, False)
)
# new instance
case "new":
var_type = expression_node.dict["var_type"]
if var_type not in self.structs:
super().error(
ErrorType.TYPE_ERROR,
"Invalid struct type",
)
# create variables for all the struct-specific variables
struct = self.structs[var_type]
variables = {}
for var in struct.dict["fields"]:
name = var.dict["name"]
type = var.dict["var_type"]
if type in Interpreter.default_types:
variables[name] = Value(
type, Interpreter.default_types[type]
)
else:
if type not in self.structs:
super().error(
ErrorType.TYPE_ERROR,
"Unrecognized type for variable in struct",
)
variables[name] = Value(type)
# return a reference to the struct
return Value(var_type, variables)
# function call
case "fcall":
return self.run_function_call(expression_node)
def run_function_call(self, function_call):
name = function_call.dict["name"]
arg_nodes = function_call.dict["args"]
match name:
case "print":
res = ""
for arg in function_call.dict["args"]:
output = self.evaluate_expression(arg)
if output.type() == Type.VOID:
super().error(
ErrorType.TYPE_ERROR,
"Using void in print",
)
res += get_printable(output)
super().output(res)
# self.variables.print()
return Value(Type.VOID)
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:
super().output(function_call.dict["args"][0].dict["val"])
return 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:
super().output(function_call.dict["args"][0].dict["val"])
return 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"]
):
args = []
for arg in arg_nodes:
args.append(self.evaluate_expression(arg))
res = self.run_function(function, args)
return res if res else Value(Type.VOID)
super().error(
ErrorType.NAME_ERROR,
f"Function {name} has not been defined",
)
def check_return(self, return_type, return_value):
# return_type is the return type of the function
# return_value is the value we're returning from the function
# print(
# "return_type",
# return_type,
# "return_value",
# return_value.value(),
# return_value.type(),
# )
# void function returning something
if return_type == Type.VOID and return_value.value():
super().error(
ErrorType.TYPE_ERROR,
"Returning a value from a void function",
)
# not a struct variable and assigning nil
if (
return_type not in self.structs
and return_type != Type.NIL
and return_value.type() == Type.NIL
):
super().error(
ErrorType.TYPE_ERROR,
f"nil cannot be assigned to a {return_type} (1)",
)
# mismatched struct types
if (
return_type in self.structs
and return_value.type() != return_type
and return_value.type() != Type.NIL
):
super().error(
ErrorType.TYPE_ERROR,
"Incorrect struct type return",
)
# if return type is bool and return value is int (coercion)
if return_type == Type.BOOL and return_value.type() == Type.INT:
self.convert_to_bool(return_value)
# mismatched primitive types
if (
return_type in Interpreter.default_types
and return_type != return_value.type()
):
super().error(
ErrorType.TYPE_ERROR,
f"{return_value.type()} cannot be assigned to a {return_type} (2)",
)
def check_field_access(self, obj, var):
# variable to the left of a dot is nil
if obj is None:
super().error(
ErrorType.FAULT_ERROR,
"CFA: Variable to the left of a dot is nil",
)
# variable to the left of a dot is not a struct
if not isinstance(obj, dict):
super().error(
ErrorType.TYPE_ERROR,
"CFA: Variable to the left of a dot is not a struct",
)
# invalid field name
if var not in obj:
super().error(
ErrorType.NAME_ERROR,
f"CFA: {var} does not exist",
)
# checks and returns the bool equiv of an int
def check_bool(self, val):
if val.type() != Type.INT:
return val
else:
if val.value() == 0:
return Value(Type.BOOL, False)
else:
return Value(Type.BOOL, True)
# converts a value from int to bool
def convert_to_bool(self, val):
if val.type() == Type.INT:
if val.value() == 0:
val.set(Type.BOOL, False)
else:
val.set(Type.BOOL, True)
def type_to_variable(self, name, type):
match type:
case "bool":
res = self.variables.create(name, Value(Type.BOOL, False))
case "int":
res = self.variables.create(name, Value(Type.INT, 0))
case "string":
res = self.variables.create(name, Value(Type.STRING, ""))
case _:
# if type is a struct
if type in self.structs:
# create a variable for the one representing the struct
res = self.variables.create(name, Value(type))
else:
# no matching types
super().error(
ErrorType.TYPE_ERROR,
"Not a valid type for a variable",
)
if not res:
super().error(
ErrorType.NAME_ERROR,
f"TTV: Variable {name} defined more than once",
)
def return_default(self, type):
match type:
case "bool":
return Value(Type.BOOL, False)
case "int":
return Value(Type.INT, 0)
case "string":
return Value(Type.STRING, "")
case "void":
return Value(Type.VOID)
case _:
return Value(Type.NIL)
def get_nested_variable(self, name):
parts = name.split(".")
current = self.variables.get(parts[0])
if not current:
super().error(
ErrorType.NAME_ERROR,
f"GNV: Variable {name} has not been defined",
)
for part in parts[1:]:
current = current.value()
self.check_field_access(current, part)
current = current[part]
return current
def set_nested_variable(self, name, value):
parts = name.split(".")
current = self.variables.get(parts[0])
if not current:
super().error(
ErrorType.NAME_ERROR,
f"SNV: Variable {name} has not been defined",
)
# traverse until the second-to-last part
for part in parts[1:-1]:
current = current.value()
self.check_field_access(current, part)
current = current[part]
# set the final part to the new value
last_part = parts[-1]
current = current.value()
self.check_field_access(current, last_part)
self.check_return(current[last_part].type(), value)
current[last_part] = value
if __name__ == "__main__":
program = """
struct list {
val: int;
next: list;
}
func merge(l1: list, l2: list) : list {
if (l1 == nil) {
return l2;
}
if (l2 == nil) {
return l1;
}
if (l1.val <= l2.val) {
l1.next = merge(l1.next, l2);
return l1;
} else {
l2.next = merge(l1, l2.next);
return l2;
}
}
func print_list(l: list): void {
var x: list;
var n: int;
for (x = l; x != nil; x = x.next) {
print(x.val);
n = n + 1;
}
print("N=", n);
}
func cons(val: int, l: list) : list {
var h: list;
h = new list;
h.val = val;
h.next = l;
return h;
}
func main() : void {
var l1: list;
var l2: list;
var result: list;
var n: int;
var i: int;
n = inputi();
for (i = 0; i < n; i = i + 1) {
var v: int;
v = inputi();
l1 = cons(v, l1);
}
n = inputi();
for (i = 0; i < n; i = i + 1) {
var v: int;
v = inputi();
l2 = cons(v, l2);
}
result = merge(l1, l2);
print_list(result);
}
"""
interpreter = Interpreter()
interpreter.run(program)