-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolver.java
More file actions
350 lines (291 loc) · 8.14 KB
/
Resolver.java
File metadata and controls
350 lines (291 loc) · 8.14 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
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
/**
* Class for resolver
*/
public class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void>{
private final Interpreter interpreter;
private final Stack<Map<String, Boolean>> scopes = new Stack<>();
private FunctionType currentFunction = FunctionType.NONE;
public Resolver(Interpreter interpreter) {
this.interpreter = interpreter;
}
/**
* Enum for function type
*/
private enum FunctionType {
NONE,
FUNCTION,
INTIALIZER,
METHOD
}
private enum ClassType {
NONE,
CLASS,
SUBCLASS
}
private ClassType currentClass = ClassType.NONE;
@Override
public Void visitBlockStmt(Stmt.Block stmt){
beginScope();
resolve(stmt.statements);
endScope();
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)) {
Fein.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("this", true);
for(Stmt.Function method : stmt.methods) {
FunctionType declaration = FunctionType.METHOD;
if(method.name.lexeme.equals("init")) {
declaration = FunctionType.INTIALIZER;
}
resolveFunction(method, declaration);
}
endScope();
if(stmt.superclass != null) endScope();
currentClass = enclosingClass;
return null;
}
@Override
public Void visitExpressionStmt(Stmt.Expression stmt) {
resolve(stmt.expression);
return null;
}
@Override
public Void visitFunctionStmt(Stmt.Function stmt) {
declare(stmt.name);
define(stmt.name);
resolveFunction(stmt, FunctionType.FUNCTION);
return null;
}
@Override
public Void visitIfStmt(Stmt.If stmt) {
resolve(stmt.condition);
resolve(stmt.thenBranch);
if(stmt.elseBranch != null) resolve(stmt.elseBranch);
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) {
Fein.error(stmt.keyword, "Can't return from top-level code.");
}
if(stmt.value != null) {
if(currentFunction == FunctionType.INTIALIZER) {
Fein.error(stmt.keyword , "Can't return a value from an initializer.");
}
resolve(stmt.value);
}
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 visitWhileStmt(Stmt.While stmt) {
resolve(stmt.condition);
resolve(stmt.body);
return null;
}
@Override
public Void visitVariableExpr(Expr.Variable expr) {
if(!scopes.isEmpty()
&& scopes.peek().get(expr.name.lexeme) == Boolean.FALSE ) {
Fein.error(expr.name, "Can't read local variable in its own initializer.");
}
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 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 visitGetExpr(Expr.Get expr) {
resolve(expr.object);
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 visitSetExpr(Expr.Set expr) {
resolve(expr.value);
resolve(expr.object);
return null;
}
@Override
public Void visitSuperExpr(Expr.Super expr) {
if(currentClass == ClassType.NONE) {
Fein.error(expr.keyword, "Can't use 'super' outside of a class.");
} else if (currentClass != ClassType.SUBCLASS) {
Fein.error(expr.keyword, "Can't use 'super' in a class with no superclass.");
}
resolveLocal(expr, expr.keyword);
return null;
}
@Override
public Void visitThisExpr(Expr.This expr) {
if(currentClass == ClassType.NONE) {
Fein.error(expr.keyword, "Can't use 'this' outside of class.");
return null;
}
resolveLocal(expr, expr.keyword);
return null;
}
@Override
public Void visitUnaryExpr(Expr.Unary expr) {
resolve(expr.right);
return null;
}
/**
* Method to declare the variable statement for resolving
*
* @param name Token
*/
private void declare(Token name) {
if(scopes.isEmpty()) return;
Map<String, Boolean> scope = scopes.peek();
if(scope.containsKey(name.lexeme)) {
Fein.error(name, "Already a variable with this name in this scope.");
}
scope.put(name.lexeme, false);
}
/**
* Method to define the variable statement for resolving
*
* @param name Token
*/
private void define(Token name) {
if(scopes.isEmpty()) return;
scopes.peek().put(name.lexeme , true);
}
/**
* Method to resolve function
*
* @param function Stmt.Function
*/
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;
}
/**
* Method to resolve local
*
* @param expr Expr
* @param name Token
*/
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;
}
}
}
/**
* Method to resolve statements
*
* @param statements List<Stmt>
*/
void resolve(List<Stmt> statements) {
for(Stmt statement : statements){
resolve(statement);
}
}
/**
* Method to resolve statement
*
* @param stmt Stmt
*/
private void resolve(Stmt stmt){
stmt.accept(this);
}
/**
* Method to resolve expression
*
* @param expr Expr
*/
private void resolve(Expr expr){
expr.accept(this);
}
/**
* Method to begin scope and push hashmap to stack
*/
private void beginScope() {
scopes.push(new HashMap<String, Boolean>());
}
/**
* Method to end scope and pop out of the stack
*/
private void endScope(){
scopes.pop();
}
}