-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreterv2.py
304 lines (269 loc) · 11.4 KB
/
interpreterv2.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
from brewparse import parse_program
from element import Element
from intbase import InterpreterBase, ErrorType
class Return(Exception):
def __init__(self, value):
super().__init__()
self.value = value
class Interpreter(InterpreterBase):
builtin_functions = {"print", "inputi", "inputs"}
def __init__(self, console_output=True, inp=None, trace_output=False):
super().__init__(console_output, inp)
self.trace_output = trace_output
self.function_defs = {}
self.variables = {}
self.scopes = []
def run(self, program):
program_node = parse_program(program)
functions = program_node.get("functions")
for function in functions:
name = function.get("name")
params = function.get("args")
self.function_defs.setdefault(name, {})
self.function_defs[name][len(params)] = function
main_function = Element("fcall", name="main", args=[])
self.run_function(main_function)
def run_statement(self, statement):
match statement.elem_type:
case "if":
self.run_if(statement)
case "while":
self.run_while(statement)
case "return":
self.run_return(statement)
case "fcall":
self.run_function(statement)
case "=":
self.run_assignment(statement)
def create_scope(self):
self.scopes.append([])
def delete_scope(self):
for variable_name in self.scopes[-1]:
self.variables[variable_name].pop()
if not self.variables[variable_name]:
del self.variables[variable_name]
self.scopes.pop()
def run_if(self, if_block):
statements = if_block.get("statements")
else_statements = if_block.get("else_statements")
condition = self.evaluate_expression(if_block.get("condition"))
if condition.elem_type != "bool":
self.error(
ErrorType.TYPE_ERROR, "If condition does not evaluate to a boolean"
)
self.create_scope()
try:
if condition.get("val"):
for statement in statements:
self.run_statement(statement)
elif else_statements:
for statement in else_statements:
self.run_statement(statement)
finally:
self.delete_scope()
def run_while(self, while_block):
statements = while_block.get("statements")
self.create_scope()
try:
while True:
condition = self.evaluate_expression(while_block.get("condition"))
if condition.elem_type != "bool":
self.error(
ErrorType.TYPE_ERROR,
"While condition does not evaluate to a boolean",
)
if condition.get("val"):
for statement in statements:
self.run_statement(statement)
else:
break
finally:
self.delete_scope()
def run_return(self, statement):
expression = statement.get("expression")
if expression:
raise Return(self.evaluate_expression(expression))
else:
raise Return(Element("nil"))
def run_function(self, function):
name = function.get("name")
args = function.get("args")
if name in self.function_defs and len(args) in self.function_defs[name]:
function_def = self.function_defs[name][len(args)]
params = function_def.get("args")
statements = function_def.get("statements")
for param, arg in zip(params, args):
param_name = param.get("name")
self.variables.setdefault(param_name, [])
self.variables[param_name].append(self.evaluate_expression(arg))
self.create_scope()
try:
for statement in statements:
self.run_statement(statement)
return Element("nil")
except Return as ret:
return ret.value
finally:
self.delete_scope()
for param in params:
param_name = param.get("name")
self.variables[param_name].pop()
if not self.variables[param_name]:
del self.variables[param_name]
elif name in self.builtin_functions:
return self.run_builtin(function)
else:
self.error(
ErrorType.NAME_ERROR,
f"No {name}() function found that takes {len(args)} parameters",
)
def run_builtin(self, function):
name = function.get("name")
args = function.get("args")
match name:
case "print":
self.output("".join(self.fmt(arg) for arg in args))
return Element("nil")
case "inputi":
if len(args) == 1:
self.output(self.evaluate_expression(args[0]).get("val"))
elif len(args) > 1:
self.error(
ErrorType.NAME_ERROR,
"No inputi() function found that takes > 1 parameter",
)
return Element("int", val=int(self.get_input()))
case "inputs":
if len(args) == 1:
self.output(self.evaluate_expression(args[0]).get("val"))
elif len(args) > 1:
self.error(
ErrorType.NAME_ERROR,
"No inputs() function found that takes > 1 parameter",
)
return Element("string", val=self.get_input())
def fmt(self, expression):
element = self.evaluate_expression(expression)
match element.elem_type:
case "int" | "string":
return str(element.get("val"))
case "bool":
return str(element.get("val")).lower()
def run_assignment(self, assignment):
name = assignment.get("name")
expression = assignment.get("expression")
if name not in self.variables:
self.scopes[-1].append(name)
self.variables[name] = [self.evaluate_expression(expression)]
else:
self.variables[name][-1] = self.evaluate_expression(expression)
def evaluate_expression(self, expression):
match expression.elem_type:
case "neg" | "!":
return self.evaluate_unary_operation(expression)
case "+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "||" | "&&":
return self.evaluate_binary_operation(expression)
case "fcall":
return self.run_function(expression)
case "var":
name = expression.get("name")
if name in self.variables:
return self.variables[name][-1]
else:
self.error(
ErrorType.NAME_ERROR, f"Variable {name} has not been defined"
)
case "int" | "string" | "bool" | "nil":
return expression
def evaluate_unary_operation(self, operation):
op1 = self.evaluate_expression(operation.get("op1"))
try:
match operation.elem_type:
case "neg":
if op1.elem_type == "int":
return Element("int", val=-op1.get("val"))
else:
raise TypeError
case "!":
if op1.elem_type == "bool":
return Element("bool", val=not op1.get("val"))
else:
raise TypeError
except:
self.error(
ErrorType.TYPE_ERROR,
f"Incompatible type for operation {operation.elem_type}: {op1.elem_type}",
)
def evaluate_binary_operation(self, operation):
# Strict evaluation
op1 = self.evaluate_expression(operation.get("op1"))
op2 = self.evaluate_expression(operation.get("op2"))
try:
match operation.elem_type:
case "+":
if op1.elem_type == op2.elem_type == "int":
return Element("int", val=op1.get("val") + op2.get("val"))
elif op1.elem_type == op2.elem_type == "string":
return Element("string", val=op1.get("val") + op2.get("val"))
else:
raise TypeError
case "-":
if op1.elem_type == op2.elem_type == "int":
return Element("int", val=op1.get("val") - op2.get("val"))
else:
raise TypeError
case "*":
if op1.elem_type == op2.elem_type == "int":
return Element("int", val=op1.get("val") * op2.get("val"))
else:
raise TypeError
case "/":
if op1.elem_type == op2.elem_type == "int":
return Element("int", val=op1.get("val") // op2.get("val"))
else:
raise TypeError
case "==":
if op1.elem_type != op2.elem_type:
return Element("bool", val=False)
else:
return Element("bool", val=op1.get("val") == op2.get("val"))
case "!=":
if op1.elem_type != op2.elem_type:
return Element("bool", val=True)
else:
return Element("bool", val=op1.get("val") != op2.get("val"))
case "<":
if op1.elem_type == op2.elem_type == "int":
return Element("bool", val=op1.get("val") < op2.get("val"))
else:
raise TypeError
case ">":
if op1.elem_type == op2.elem_type == "int":
return Element("bool", val=op1.get("val") > op2.get("val"))
else:
raise TypeError
case "<=":
if op1.elem_type == op2.elem_type == "int":
return Element("bool", val=op1.get("val") <= op2.get("val"))
else:
raise TypeError
case ">=":
if op1.elem_type == op2.elem_type == "int":
return Element("bool", val=op1.get("val") >= op2.get("val"))
else:
raise TypeError
case "||":
if op1.elem_type == op2.elem_type == "bool":
return Element("bool", val=op1.get("val") or op2.get("val"))
else:
raise TypeError
case "&&":
if op1.elem_type == op2.elem_type == "bool":
return Element("bool", val=op1.get("val") and op2.get("val"))
else:
raise TypeError
except TypeError:
self.error(
ErrorType.TYPE_ERROR,
f"Incompatible types for operation {operation.elem_type}: {op1.elem_type} and {op2.elem_type}",
)