-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
414 lines (319 loc) · 13.7 KB
/
interpreter.py
File metadata and controls
414 lines (319 loc) · 13.7 KB
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
import logging
import sys
import random
from exceptions.exceptions import VariableAlreadyDeclared, \
ConstantAlreadyDeclared, InvalidType, CannotAssignConstant, \
AssignmentToUndefinedVariable, WrongAssignment, CannotReadArray, \
VariableUndeclared, UndeclaredSymbol, UnimplementedOperation, SymbolAlreadyInUse, CallWithWrongArity, \
UndeclaredFunction
from variables.variables import LanceScriptScalarVariable, LanceScriptArrayVariable
from variables.constants import LanceConstant
from functions.functions import LanceFunction, LanceFunctionParameter
from variables.types import VarTypes
class LanceInterpreter:
def __init__(self, log_level=logging.WARNING, **kwargs):
self.vars = [{}]
self.constants = {}
self.functions = {}
self.LOG = logging.getLogger("AST2Intermediate")
self.LOG.setLevel(log_level)
self.mock_input = kwargs.get('mock_input', False)
def evaluate(self, tree, variables=None):
if variables is not None:
self.vars.append(variables)
f = getattr(self, tree.data, self.__default__)
ret = f(tree)
if variables is not None:
self.vars.pop()
return ret
def __default__(self, tree):
self.LOG.log(logging.WARNING, "Action %s not implemented", tree.data)
return tree
def program(self, tree):
for child in tree.children:
if child.data == 'return_stmt':
return
else:
self.evaluate(child)
def code_block(self, tree):
for child in tree.children:
if child.data == 'return_stmt':
return
else:
self.evaluate(child)
# ------------------ VARIABLES & CONSTANTS ------------------
def const_decl(self, matches):
const_name = matches.children[0]
const_val = self.evaluate(matches.children[1])
if self.var_is_decl(const_name):
raise VariableAlreadyDeclared("Symbol %s already used for a variable", const_name)
if self.const_is_declared(const_name):
raise ConstantAlreadyDeclared("Cannot redefine constant %s", const_name)
self.LOG.log(logging.DEBUG, "Declaring constant %s as %s", const_name, const_val)
self.constants[const_name] = LanceConstant(VarTypes.INT, const_name, const_val)
def var_decl(self, matches):
var_type = self.str_to_type(matches.children[0])
declarations = matches.children[1].children
for declaration in declarations:
decl_type = declaration.data
var_name = declaration.children[0]
if self.var_is_decl(var_name):
raise VariableAlreadyDeclared("Cannot declare variable %s twice", var_name)
if var_name in self.constants:
raise ConstantAlreadyDeclared("The symbol %s is already defined as constant", var_name)
if decl_type == 'scalar_declaration':
self.current_context()[var_name] = LanceScriptScalarVariable(var_name, var_type)
elif decl_type == 'array_declaration':
size = self.evaluate(declaration.children[1])
self.current_context()[var_name] = LanceScriptArrayVariable(var_name, var_type, size)
elif decl_type == 'declaration_and_assignment':
value = self.evaluate(declaration.children[1])
self.current_context()[var_name] = LanceScriptScalarVariable(var_name, var_type, value)
def scalar_assignment(self, matches):
var_name = matches.children[0]
value = self.evaluate(matches.children[1])
if self.const_is_declared(var_name):
raise CannotAssignConstant("Trying to assign constant {}", var_name)
if not self.var_is_decl(var_name):
raise AssignmentToUndefinedVariable("Variable {} is not declared", var_name)
if not self.var_is_scalar(var_name):
raise WrongAssignment("Cannot assign array as scalar")
self.resolve_scoped_var(var_name).set(value)
def array_assignment(self, matches):
var_name = matches.children[0]
idx = self.evaluate(matches.children[1])
val = self.evaluate(matches.children[2])
if self.const_is_declared(var_name):
raise CannotAssignConstant("Cannot assign constant %s", var_name)
if not self.var_is_decl(var_name):
raise AssignmentToUndefinedVariable("Variable %s is not declared", var_name)
if not self.var_is_array(var_name):
raise WrongAssignment("Variable %s is scalar, trying to assign as array", var_name)
self.resolve_scoped_var(var_name).set(idx, val)
# ------------------ FUNCTIONS ------------------
def function_declaration(self, matches):
function_type = self.str_to_type(matches.children[0])
function_name = matches.children[1]
if self.symbol_is_declared(function_name):
raise SymbolAlreadyInUse("The symbol %s is already declared", function_name)
if len(matches.children) == 4:
function_parameters = self.evaluate(matches.children[2])
code = matches.children[3]
else:
code = matches.children[2]
self.functions[function_name] = LanceFunction(function_name, function_parameters, code)
def function_parameters_declaration(self, matches):
parameters = []
for parameter in matches.children:
parameters.append(self.evaluate(parameter))
return parameters
def scalar_parameter(self, matches):
type = self.str_to_type(matches.children[0])
name = matches.children[1]
return LanceFunctionParameter(name, type)
def array_parameter(self, matches):
type = self.str_to_type(matches.children[0])
name = matches.children[1]
return LanceFunctionParameter(name, type, True)
def function_call(self, matches):
function_name = matches.children[0]
if not self.func_is_declared(function_name):
raise UndeclaredFunction("Call to undeclared function %s", function_name)
function = self.functions[function_name]
formal_parameters = function.parameters
parameters_expressions = matches.children[1].children
parameters = self.evaluate_parameters(formal_parameters, parameters_expressions)
return self.evaluate(function.code, parameters)
def evaluate_parameters(self, formal_parameters, parameters_expressions):
formal_nparams = len(formal_parameters)
nparams = len(parameters_expressions)
if formal_nparams != nparams:
raise CallWithWrongArity("Function %s expects %n parameters, %n given", formal_nparams, nparams)
# TODO: check types, when and if we will ever support anything other than integers
parameters = {}
for fparam, expr in zip(formal_parameters, parameters_expressions):
name = fparam.name
type = fparam.type
is_array = fparam.is_array
if is_array:
# TODO: check this, it's probably broken
parameters[name] = self.resolve_scoped_var(expr.children[0])
else:
val = self.evaluate(expr)
parameters[name] = LanceScriptScalarVariable(name, type, val)
return parameters
# ------------------ IO ------------------
def read_stmt(self, matches):
var_name = matches.children[0]
if self.var_is_array(var_name):
raise CannotReadArray()
if self.const_is_declared(var_name):
raise CannotAssignConstant("Cannot read into a constant")
if not self.var_is_decl(var_name):
raise VariableUndeclared("Trying to read into undefined variable")
sys.stdout.write("Input value for {} > ".format(var_name))
if self.mock_input:
val = random.randint(0, 256)
sys.stdout.write(str(val) + "\n")
else:
val = int(input())
self.resolve_scoped(var_name).set(val)
def write_stmt(self, matches):
val = self.evaluate(matches.children[0])
sys.stdout.write(str(val))
sys.stdout.write("\n")
# ------------------ EXPRESSIONS ------------------
def binexpr(self, matches):
op1 = self.evaluate(matches.children[0])
op = matches.children[1]
op2 = self.evaluate(matches.children[2])
if op == '+':
return op1 + op2
elif op == '-':
return op1 - op2
elif op == '*':
return op1 * op2
elif op == '/':
return op1 // op2
elif op == '%':
return op1 % op2
elif op == '!=':
return self.bool_to_int(op1 != op2)
elif op == '==':
return self.bool_to_int(op1 == op2)
elif op == '>':
return self.bool_to_int(op1 > op2)
elif op == '>=':
return self.bool_to_int(op1 >= op2)
elif op == '<':
return self.bool_to_int(op1 < op2)
elif op == '<=':
return self.bool_to_int(op1 <= op2)
elif op == '&&':
# Just to be sure operations get short circuited
if self.evaluate(op1) == 0:
return 0
elif self.evaluate(op2) == 0:
return 0
else:
return 1
elif op == '||':
if self.evaluate(op1) != 0:
return 1
elif self.evaluate(op2) != 0:
return 1
else:
return 0
elif op == '&':
return op1 & op2
elif op == '|':
return op1 | op2
elif op == '^':
return op1 ^ op2
else:
raise UnimplementedOperation("Operation %s is not implemented", op)
# TODO: handle other operations
@staticmethod
def intexpr(matches):
return int(matches.children[0])
def scalar_expr(self, matches):
name = matches.children[0]
val = self.resolve_scoped(name).get()
return val
def array_expr(self, matches):
name = matches.children[0]
idx = self.evaluate(matches.children[1])
val = self.resolve_scoped_var(name).get(idx)
return val
def lnot_expr(self, matches):
"""Evaluates expression of type '! expr' (logical not)."""
subexpr_val = self.evaluate(matches.children[0])
return self.bool_to_int(subexpr_val != 0)
def neg_expr(self, matches):
"""Evaluates arithmetic negative of an expression"""
subexpr_val = self.evaluate(matches.children[1])
return - subexpr_val
# ------------------ CONTROL FLOW ------------------
def if_statement(self, matches):
expr_val = self.evaluate(matches.children[0])
if expr_val != 0:
self.evaluate(matches.children[1])
def if_else_statement(self, matches):
if_statement = matches.children[0]
condition_expression = if_statement.children[0]
if_block = if_statement.children[1]
else_block = matches.children[1]
expr_val = self.evaluate(condition_expression)
if expr_val != 0:
self.evaluate(if_block)
else:
self.evaluate(else_block)
def while_construct(self, matches):
condition = matches.children[0]
code = matches.children[1]
while self.evaluate(condition) != 0:
self.evaluate(code)
def do_while_construct(self, matches):
code = matches.children[0]
condition = matches.children[1]
self.evaluate(code)
while self.evaluate(condition) != 0:
self.evaluate(code)
def for_construct(self, matches):
initialization = matches.children[0]
condition = matches.children[1]
assignment = matches.children[2]
code = matches.children[3]
self.evaluate(initialization)
while self.evaluate(condition) != 0:
self.evaluate(code)
self.evaluate(assignment)
# ------------------ HELPERS ------------------
def var_is_decl(self, var_name):
try:
self.resolve_scoped(var_name)
return True
except UndeclaredSymbol as e:
return False
def const_is_declared(self, const_name):
return const_name in self.constants
def func_is_declared(self, func_name):
return func_name in self.functions
def symbol_is_declared(self, symbol_name):
return (self.var_is_decl(symbol_name)
or self.const_is_declared(symbol_name)
or self.func_is_declared(symbol_name))
def var_is_scalar(self, var_name):
return isinstance(self.resolve_scoped_var(var_name), LanceScriptScalarVariable)
def var_is_array(self, var_name):
return isinstance(self.resolve_scoped_var(var_name), LanceScriptArrayVariable)
@staticmethod
def bool_to_int(val):
if val:
return 1
else:
return 0
@staticmethod
def str_to_type(val):
# TODO: handle types (kinda pointless while we have only integers in the grammar)
if val == 'int':
var_type = VarTypes.INT
else:
raise InvalidType("This type is not implemented yet")
return var_type
def current_context(self):
return self.vars[-1]
def resolve_scoped_var(self, name):
def helper(name, contexts):
if len(contexts) == 0:
raise UndeclaredSymbol("%s is not declared", name)
curr_context = contexts[-1]
if name in curr_context:
return curr_context[name]
else:
return helper(name, contexts[:-1])
return helper(name, self.vars)
def resolve_scoped(self, name):
if name in self.constants:
return self.constants[name]
return self.resolve_scoped_var(name)