-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolver.java
305 lines (265 loc) · 7.9 KB
/
Resolver.java
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
package SLox;
import java.sql.Statement;
import java.util.*;
class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
public final ArrayList<Token> visited = new ArrayList<>();
private final Interpreter interpreter;
private final Stack<Map<String, Boolean>> scopes = new Stack<>();
private FunctionType currentFunction = FunctionType.NONE;
private ClassType currentClass = ClassType.NONE;
Resolver(Interpreter interpreter) {
this.interpreter = interpreter;
}
private enum FunctionType {
NONE,
FUNCTION,
METHOD,
INITIALIZER,
LAMBDA
}
private enum ClassType {
NONE,
CLASS,
SUBCLASS
}
@Override
public Void visitBlockStmt(Stmt.Block stmt) {
beginScope();
resolve(stmt.statements);
endScope();
return null;
}
@Override
public Void visitVarStmt(Stmt.Var stmt) {
declare(stmt.name);
if (stmt.initializer != null) {
resolve(stmt.initializer);
}
define(stmt.name);
return null;
}
@Override
public Void visitVariableExpr(Expr.Variable expr) {
if (!scopes.isEmpty() &&
scopes.peek().get(expr.name.lexeme) == Boolean.FALSE) {
lox.error(expr.name,
"Can't read local variable in its own initializer.");
}
visited.removeIf(obj -> Objects.equals(obj.lexeme, expr.name.lexeme)); //same var decl multiple blocks
resolveLocal(expr, expr.name);
return null;
}
@Override
public Void visitAssignExpr(Expr.Assign expr) {
resolve(expr.value);
resolveLocal(expr, expr.name);
return null;
}
@Override
public Void visitFunctionStmt(Stmt.Function stmt) {
declare(stmt.name);
define(stmt.name);
resolveFunction(stmt, FunctionType.FUNCTION);
return null;
}
private void resolveFunction(Stmt.Function function, FunctionType type) {
FunctionType enclosingFunction = currentFunction;
currentFunction = type;
beginScope();
for (Token param : function.params) {
declare(param);
define(param);
}
resolve(function.body);
endScope();
currentFunction = enclosingFunction;
}
@Override
public Void visitExpressionStmt(Stmt.Expression stmt) {
resolve(stmt.expression);
return null;
}
@Override
public Void visitIfStmt(Stmt.If stmt) {
resolve(stmt.condition);
return null;
}
@Override
public Void visitPrintStmt(Stmt.Print stmt) {
resolve(stmt.expression);
return null;
}
@Override
public Void visitReturnStmt(Stmt.Return stmt) {
if (currentFunction == FunctionType.NONE) {
lox.error(stmt.keyword, "Can't return from top-level code.");
}
if (stmt.value != null) {
if (currentFunction == FunctionType.INITIALIZER) {
lox.error(stmt.keyword,
"Can't return a value from an initializer.");
}
resolve(stmt.value);
}
return null;
}
@Override
public Void visitWhileStmt(Stmt.While stmt) {
resolve(stmt.condition);
resolve(stmt.body);
return null;
}
@Override
public Void visitBinaryExpr(Expr.Binary expr) {
resolve(expr.left);
resolve(expr.right);
return null;
}
@Override
public Void visitCallExpr(Expr.Call expr) {
resolve(expr.callee);
for (Expr argument : expr.arguments) {
resolve(argument);
}
return null;
}
@Override
public Void visitGroupingExpr(Expr.Grouping expr) {
resolve(expr.expression);
return null;
}
@Override
public Void visitLiteralExpr(Expr.Literal expr) {
return null;
}
@Override
public Void visitLogicalExpr(Expr.Logical expr) {
resolve(expr.left);
resolve(expr.right);
return null;
}
@Override
public Void visitUnaryExpr(Expr.Unary expr) {
resolve(expr.right);
return null;
}
@Override
public Void visitClassStmt(Stmt.Class stmt) {
ClassType enclosingClass = currentClass;
currentClass = ClassType.CLASS;
declare(stmt.name);
define(stmt.name);
if (stmt.superclass != null &&
stmt.name.lexeme.equals(stmt.superclass.name.lexeme)) {
lox.error(stmt.superclass.name,
"A class can't inherit from itself.");
}
if (stmt.superclass != null) {
currentClass = ClassType.SUBCLASS;
resolve(stmt.superclass);
}
if (stmt.superclass != null) {
beginScope();
scopes.peek().put("super", true);
}
beginScope();
scopes.peek().put("ana",true);
for (Stmt.Function method : stmt.methods) {
FunctionType declaration = FunctionType.METHOD;
if (method.name.lexeme.equals("init")) {
declaration = FunctionType.INITIALIZER;
}
resolveFunction(method, declaration);
}
endScope();
if (stmt.superclass != null) endScope();
currentClass = enclosingClass;
return null;
}
@Override
public Void visitGetExpr(Expr.Get expr) {
resolve(expr.object);
return null;
}
@Override
public Void visitSetExpr(Expr.Set expr) {
resolve(expr.value);
resolve(expr.object);
return null;
}
@Override
public Void visitAnaExpr(Expr.Ana expr){
if (currentClass == ClassType.NONE) {
lox.error(expr.keyword,
"Can't use 'ana' outside of a class.");
return null;
}
resolveLocal(expr, expr.keyword);
return null;
}
@Override
public Void visitLambdaExpr(Expr.Lambda expr) {
FunctionType enclosingFunction = currentFunction;
currentFunction = FunctionType.LAMBDA;
beginScope();
for (Token param : expr.params) {
declare(param);
define(param);
}
resolve(expr.body);
endScope();
currentFunction = enclosingFunction;
return null;
}
@Override
public Void visitSuperExpr(Expr.Super expr) {
if (currentClass == ClassType.NONE) {
lox.error(expr.keyword,
"Can't use 'super' outside of a class.");
} else if (currentClass != ClassType.SUBCLASS) {
lox.error(expr.keyword,
"Can't use 'super' in a class with no superclass.");
}
resolveLocal(expr, expr.keyword);
return null;
}
private void resolveLocal(Expr expr, Token name) {
for (int i = scopes.size() - 1; i >= 0; i--) {
if (scopes.get(i).containsKey(name.lexeme)) {
interpreter.resolve(expr, scopes.size() - 1 - i);
return;
}
}
}
private void beginScope() {
scopes.push(new HashMap<String, Boolean>());
}
private void endScope() {
scopes.pop();
}
private void declare(Token name) {
visited.add(name);
if (scopes.isEmpty()) return;
Map<String, Boolean> scope = scopes.peek();
if (scope.containsKey(name.lexeme)) {
lox.error(name,
"Already a variable with this name in this scope.");
}
scope.put(name.lexeme, false);
}
private void define(Token name) {
if (scopes.isEmpty()) return;
scopes.peek().put(name.lexeme, true);
}
void resolve(List<Stmt> statements) {
for (Stmt statement : statements) {
resolve(statement);
}
}
private void resolve(Stmt stmt) {
stmt.accept(this);
}
private void resolve(Expr expr) {
expr.accept(this);
}
}