-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.py
196 lines (152 loc) · 5.97 KB
/
resolver.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
import expr as e
import stmt as s
import lox as l
from enum import Enum, auto
class FunctionType(Enum):
NONE=auto()
FUNCTION=auto()
METHOD=auto()
INITIALIZER=auto()
class ClassType(Enum):
NONE=auto()
CLASS=auto()
SUBCLASS=auto()
class Resolver(e.Visitor, s.Visitor):
def __init__(self, interpreter):
self.interpreter=interpreter
self.scopes=[]
self.currentFunction=FunctionType.NONE
self.currentClass=ClassType.NONE
def visitBlockStmt(self, stmt):
self.beginScope()
self.resolveBlock(stmt.statements)
self.endScope()
def visitClassStmt(self, stmt):
enclosingClass=self.currentClass
self.currentClass=ClassType.CLASS
self.declare(stmt.name)
self.define(stmt.name)
if stmt.superclass is not None and stmt.name.lexeme==stmt.superclass.name.lexeme:
l.Lox.error(stmt.superclass.name, "A class can't inherit from itself.")
if stmt.superclass is not None:
self.currentClass=ClassType.SUBCLASS
self.resolveExpression(stmt.superclass)
if stmt.superclass is not None:
self.beginScope()
self.scopes[-1]["super"]=True
self.beginScope()
self.scopes[-1]["this"]=True
# print(self.scopes)
for method in stmt.methods:
declaration=FunctionType.METHOD
if method.name.lexeme=="init":
declaration=FunctionType.INITIALIZER
self.resolveFunction(method, declaration)
self.endScope()
if stmt.superclass is not None:
self.endScope()
self.currentClass=enclosingClass
def visitVarStmt(self, stmt):
self.declare(stmt.name)
if stmt.initializer is not None:
self.resolveExpression(stmt.initializer)
self.define(stmt.name)
def visitFunctionStmt(self, stmt):
self.declare(stmt.name)
self.define(stmt.name)
self.resolveFunction(stmt, FunctionType.FUNCTION)
def visitExpressionStmt(self, stmt):
self.resolveExpression(stmt.expression)
def visitIfStmt(self, stmt):
self.resolveExpression(stmt.condition)
self.resolveStatement(stmt.thenBranch)
if stmt.elseBranch is not None:
self.resolveStatement(stmt.elseBranch)
def visitPrintStmt(self, stmt):
self.resolveExpression(stmt.expression)
def visitReturnStmt(self, stmt):
if self.currentFunction==FunctionType.NONE:
l.parseError(stmt.keyword, "Can't return from top-level code.")
if stmt.value is not None:
if self.currentFunction==FunctionType.INITIALIZER:
l.Lox.error(stmt.keyword, "Can't return a value from an initializer.")
self.resolveExpression(stmt.value)
def visitWhileStmt(self, stmt):
self.resolveExpression(stmt.condition)
self.resolveStatement(stmt.body)
def visitVariableExpr(self, expr):
if len(self.scopes)!=0 and self.scopes[-1].get(expr.name.lexeme) is False:
l.parseError(expr.name, "Can't read local variable in its own initializer.")
self.resolveLocal(expr, expr.name)
def visitAssignExpr(self, expr):
self.resolveExpression(expr.value)
self.resolveLocal(expr, expr.name)
def visitBinaryExpr(self, expr):
self.resolveExpression(expr.left)
self.resolveExpression(expr.right)
def visitCallExpr(self, expr):
self.resolveExpression(expr.callee)
for argument in expr.arguments:
self.resolveExpression(argument)
def visitGetExpr(self, expr):
self.resolveExpression(expr.obj)
def visitSetExpr(self, expr):
self.resolveExpression(expr.value)
self.resolveExpression(expr.obj)
def visitSuperExpr(self, expr):
if self.currentClass==ClassType.NONE:
l.Lox.error(expr.keyword, "Can't use 'super' outside of a class.")
elif self.currentClass!=ClassType.SUBCLASS:
l.Lox.error(expr.keyword, "Can't use 'super' in a class with no superclass.")
self.resolveLocal(expr, expr.keyword)
def visitThisExpr(self, expr):
if self.currentClass==ClassType.NONE:
l.Lox.error(expr.keyword, "Can't use 'this' outside of a class.")
return None
self.resolveLocal(expr, expr.keyword)
def visitGroupingExpr(self, expr):
self.resolveExpression(expr.expression)
def visitLiteralExpr(self, expr):
pass
def visitLogicalExpr(self, expr):
self.resolveExpression(expr.left)
self.resolveExpression(expr.right)
def visitUnaryExpr(self, expr):
self.resolveExpression(expr.right)
def resolveLocal(self, expr, name):
for i in reversed(range(len(self.scopes))):
if name.lexeme in self.scopes[i]:
self.interpreter.resolve(expr, len(self.scopes)-1-i)
return
def resolveFunction(self, function, type):
enclosingFunction=self.currentFunction
self.currentFunction=type
self.beginScope()
for param in function.params:
self.declare(param)
self.define(param)
self.resolveBlock(function.body)
self.endScope()
self.currentFunction=enclosingFunction
def resolveBlock(self, statements):
for statement in statements:
self.resolveStatement(statement)
def resolveStatement(self, statement):
statement.accept(self)
def resolveExpression(self, expression):
expression.accept(self)
def beginScope(self):
self.scopes.append({})
def endScope(self):
self.scopes.pop()
def declare(self, name):
if len(self.scopes)==0:
return
scope=self.scopes[-1]
if name.lexeme in scope.keys():
l.parseError(name, "Already a variable with this name in this scope.")
scope[name.lexeme]=False
def define(self, name):
if len(self.scopes)==0:
return
self.scopes[-1][name.lexeme]=True