-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.py
358 lines (281 loc) · 12.1 KB
/
Interpreter.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
from sre_compile import dis
from Expr import *
from LoxCallable import LoxInstance
from Stmt import *
from Environment import Environment
from TokenType import TokenType
from typing import Any
from RuntimeError import RuntimeError
from Return import ReturnException
class Interpreter:
def __init__(self):
self.globals: Environment = Environment()
self.environment: Environment = self.globals
self.locals: dict[Expr, int] = dict()
from LoxCallable import Clock
self.globals.define("clock", Clock())
def interpret(self, statements: list[Stmt]) -> None:
try:
for statement in statements:
self.execute(statement)
except RuntimeError as error:
import Lox
Lox.runtime_error(error)
def resolve(self, expr: Expr, depth: int) -> None:
self.locals[expr] = depth
def execute(self, stmt: Stmt) -> None:
match stmt:
case Print():
self.visitPrintStmt(stmt)
case Expression():
self.visitExpressionStmt(stmt)
case Block():
self.visitBlockStmt(stmt)
case Var():
self.visitVarStmt(stmt)
case If():
return self.visitIfStmt(stmt)
case While():
return self.visitWhileStmt(stmt)
case Function():
return self.visitFunctionStmt(stmt)
case Return():
return self.visitReturnStmt(stmt)
case Class():
return self.visitClassStmt(stmt)
case _:
raise Exception(f"Attempted to execute unmatched stmt type.")
def evaluate(self, expr: Expr) -> Any:
match expr:
case Literal():
return self.visitLiteralExpr(expr)
case Group():
return self.visitGroupExpr(expr)
case Unary():
return self.visitUnaryExpr(expr)
case Binary():
return self.visitBinaryExpr(expr)
case Variable():
return self.visitVariableExpr(expr)
case Assign():
return self.visitAssignExpr(expr)
case Logical():
return self.visitLogicalExpr(expr)
case Call():
return self.visitCallExpr(expr)
case Get():
return self.visitGetExpr(expr)
case Set():
return self.visitSetExpr(expr)
case This():
return self.visitThisExpr(expr)
case Super():
return self.visitSuperExpr(expr)
case _:
raise Exception(f"Attempted to evaluate unmatched expression type.")
def stringify(self, object: Any) -> str:
if object is None:
return 'nil'
elif type(object) is float:
text: str = str(object)
if len(text) >= 2 and text[-2:] == ".0":
text = text[:-2]
return text
return str(object)
def executeBlock(self, statements: list[Stmt], environment: Environment) -> None:
previous: Environment = self.environment
try:
self.environment = environment
for statement in statements:
self.execute(statement)
finally:
self.environment = previous
def visitClassStmt(self, stmt: Class) -> None:
superclass: Any = None
if stmt.superclass is not None:
superclass = self.evaluate(stmt.superclass)
from LoxCallable import LoxClass
if not (type(superclass) is LoxClass):
raise RuntimeError(stmt.superclass.name, "Superclass must be a class.")
self.environment.define(stmt.name.lexeme, None)
if stmt.superclass is not None:
self.environment = Environment(self.environment)
self.environment.define("super", superclass)
from LoxCallable import LoxFunction
methods: dict[str, LoxFunction] = dict()
for method in stmt.methods:
function: LoxFunction = LoxFunction(
method, self.environment, method.name.lexeme == "init")
methods[method.name.lexeme] = function
from LoxCallable import LoxClass
loxClass: LoxClass = LoxClass(stmt.name.lexeme, superclass, methods)
if superclass is not None:
assert self.environment.enclosing is not None
self.environment = self.environment.enclosing
self.environment.assign(stmt.name, loxClass)
def visitReturnStmt(self, stmt: Return) -> None:
value: Any = None
if stmt.value is not None:
value = self.evaluate(stmt.value)
raise ReturnException(value)
def visitFunctionStmt(self, stmt: Function) -> None:
from LoxCallable import LoxFunction
function: LoxFunction = LoxFunction(stmt, self.environment,
False)
self.environment.define(stmt.name.lexeme, function)
def visitWhileStmt(self, stmt: While) -> None:
while self.isTruthy(self.evaluate(stmt.condition)):
self.execute(stmt.body)
def visitIfStmt(self, stmt: If) -> None:
if self.isTruthy(self.evaluate(stmt.condition)):
self.execute(stmt.thenBranch)
elif stmt.elseBranch is not None:
self.execute(stmt.elseBranch)
def visitBlockStmt(self, stmt: Block) -> None:
self.executeBlock(stmt.statements, Environment(enclosing=self.environment))
def visitVarStmt(self, stmt: Var) -> None:
value: Any = None
if stmt.initializer is not None:
value = self.evaluate(stmt.initializer)
self.environment.define(stmt.name.lexeme, value)
def visitExpressionStmt(self, stmt: Expression) -> None:
self.evaluate(stmt.expression)
def visitPrintStmt(self, stmt: Print) -> None:
value: Any = self.evaluate(stmt.expression)
print(self.stringify(value))
def visitSuperExpr(self, expr: Super) -> Any:
distance: int = self.locals[expr]
from LoxCallable import LoxClass, LoxInstance, LoxFunction
superclass: LoxClass = self.environment.getAt(distance, "super")
thing: LoxInstance = self.environment.getAt(distance - 1, "this")
method: LoxFunction | None = superclass.findMethod(expr.method.lexeme)
if method is None:
raise RuntimeError(expr.method, f"Undefined property '{expr.method.lexeme}'.")
return method.bind(thing)
def visitThisExpr(self, expr: This) -> Any:
return self.lookUpVariable(expr.keyword, expr)
def visitSetExpr(self, expr: Set) -> Any:
thing: Any = self.evaluate(expr.thing)
if not type(thing) is LoxInstance:
raise RuntimeError(expr.name, "Only instances have fields.")
value: Any = self.evaluate(expr.value)
thing.setField(expr.name, value)
def visitGetExpr(self, expr: Get) -> Any:
thing: Any = self.evaluate(expr.thing)
if type(thing) is LoxInstance:
return thing.getField(expr.name)
raise RuntimeError(expr.name, "Only instances have properties.")
def visitCallExpr(self, expr: Call) -> Any:
callee: Any = self.evaluate(expr.callee)
arguments: list[Expr] = []
for argument in expr.arguments:
arguments.append(self.evaluate(argument))
from LoxCallable import LoxCallable
if not issubclass(type(callee), LoxCallable):
print(type(callee))
raise RuntimeError(
expr.paren,
"Can only call functions and classes.")
function: LoxCallable = callee
if len(arguments) != function.arity():
raise RuntimeError(
expr.paren,
f"Expected {function.arity()} arguments but got {len(arguments)}.")
return function.call(self, arguments)
def visitLogicalExpr(self, expr: Logical) -> Any:
left: Any = self.evaluate(expr.left)
if expr.operator.token_type == TokenType.OR:
if self.isTruthy(left):
return left
else:
if not self.isTruthy(left):
return left
return self.evaluate(expr.right)
def visitAssignExpr(self, expr: Assign) -> Any:
value: Any = self.evaluate(expr.value)
if expr in self.locals.keys():
distance: int = self.locals[expr]
return self.environment.assignAt(distance, expr.name, value)
else:
self.globals.assign(expr.name, value)
return value
def visitVariableExpr(self, expr: Variable) -> Any:
return self.lookUpVariable(expr.name, expr)
def lookUpVariable(self, name: Token, expr: Expr) -> Any:
if expr in self.locals.keys():
distance: int = self.locals[expr]
return self.environment.getAt(distance, name.lexeme)
else:
return self.globals.get(name)
def visitLiteralExpr(self, expr: Literal) -> Any:
return expr.value
def visitGroupExpr(self, expr: Group) -> Any:
return expr.expression
def visitUnaryExpr(self, expr: Unary) -> Any:
right: Any = self.evaluate(expr.right)
match expr.operator.token_type:
case TokenType.MINUS:
return -float(right)
case TokenType.BANG:
return not self.isTruthy(right)
return None
def visitBinaryExpr(self, expr: Binary) -> Any:
left: Any = self.evaluate(expr.left)
right: Any = self.evaluate(expr.right)
match expr.operator.token_type:
case TokenType.BANG_EQUAL:
self.checkNumberOperands(expr.operator, left, right)
return not self.isEqual(left, right)
case TokenType.EQUAL_EQUAL:
self.checkNumberOperands(expr.operator, left, right)
return self.isEqual(left, right)
case TokenType.GREATER:
self.checkNumberOperands(expr.operator, left, right)
return float(left) > float(right)
case TokenType.GREATER_EQUAL:
self.checkNumberOperands(expr.operator, left, right)
return float(left) >= float(right)
case TokenType.LESS:
self.checkNumberOperands(expr.operator, left, right)
return float(left) < float(right)
case TokenType.LESS_EQUAL:
self.checkNumberOperands(expr.operator, left, right)
return float(left) <= float(right)
case TokenType.MINUS:
self.checkNumberOperand(expr.operator, right)
return float(left) - float(right)
case TokenType.PLUS:
if type(left) is float and type(right) is float:
return float(left) + float(right)
elif type(left) is str and type(right) is str:
return str(left) + str(right)
raise RuntimeError(
expr.operator,
"Operands must both be numbers or strings")
case TokenType.SLASH:
self.checkNumberOperands(expr.operator, left, right)
return float(left) / float(right)
case TokenType.STAR:
self.checkNumberOperands(expr.operator, left, right)
return float(left) * float(right)
def checkNumberOperand(self, operator: Token, operand: Any) -> None:
if type(operand) is float:
return
raise RuntimeError(operator, "Operand must be a number.")
def checkNumberOperands(self, operator: Token, left: Any, right: Any) -> None:
if type(left) is float and type(right) is float:
return
raise RuntimeError(operator, "Operands must be numbers.")
def isEqual(self, left: Any, right: Any) -> bool:
if left == None and right == None:
return True
elif left == None:
return False
return left == right
def isTruthy(self, object: Any) -> bool:
match object:
case None:
return False
case bool():
return object
return True