-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resolver.cpp
391 lines (342 loc) · 9.1 KB
/
Resolver.cpp
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
#include "Resolver.h"
Resolver::Resolver(Interpreter *interpreter)
{
this->interpreter = interpreter;
this->currentFunction = FunctionType_None;
this->currentClass = ClassType_None;
}
void Resolver::beginScope()
{
scopes.push_back(new std::unordered_map<std::string, VariableFlags>());
}
void Resolver::endScope()
{
auto scope = scopes.back();
for (const auto &pair : *scope)
{
if (!pair.second.referenced)
{
//Lox::error(0, "Unreferenced variable: " + pair.first);
}
}
delete scope;
scopes.pop_back();
}
void Resolver::declare(Token * name)
{
if (scopes.empty())
{
return;
}
auto scope = scopes.back();
auto it = scope->find(name->lexeme);
if (it != scope->end())
{
Lox::error(*name, "Variable already defined in this scope.");
}
(*scope)[name->lexeme].initialized = false;
(*scope)[name->lexeme].referenced = false;
}
void Resolver::define(Token * name)
{
if (scopes.empty())
{
return;
}
auto scope = scopes.back();
(*scope)[name->lexeme].initialized = true;
}
void Resolver::resolveLocal(Expr *expr, Token *name)
{
for (int i = scopes.size() - 1; i >= 0; i--)
{
auto scope = scopes[i];
auto it = scope->find(name->lexeme);
if (it != scope->end())
{
interpreter->resolve(expr, scopes.size() - 1 - i);
return;
}
}
}
void Resolver::visitAssign(Assign * expr)
{
resolve(expr->value);
resolveLocal(expr, expr->name);
}
void Resolver::visitTernary(Ternary * expr)
{
resolve(expr->first);
resolve(expr->second);
resolve(expr->third);
}
void Resolver::visitBinary(Binary * expr)
{
resolve(expr->left);
resolve(expr->right);
}
void Resolver::visitLogical(Logical * expr)
{
resolve(expr->left);
resolve(expr->right);
}
void Resolver::visitGrouping(Grouping * expr)
{
resolve(expr->expression);
}
void Resolver::visitLiteral(Literal * expr)
{
}
void Resolver::visitUnary(Unary * expr)
{
resolve(expr->right);
}
void Resolver::visitVariable(Variable * expr)
{
if (!scopes.empty())
{
auto scope = scopes.back();
auto it = scope->find(expr->name->lexeme);
if (it != scope->end())
{
if (!it->second.initialized)
{
Lox::error(*expr->name, "Can't read local variable in its own initializer.");
}
it->second.referenced = true;
}
}
resolveLocal(expr, expr->name);
}
void Resolver::visitCall(Call * stmt)
{
resolve(stmt->callee);
for (auto &argument : stmt->arguments->list)
{
resolve(argument);
}
}
void Resolver::visitGet(Get *expr)
{
resolve(expr->object);
}
void Resolver::visitSet(Set *expr)
{
resolve(expr->value);
resolve(expr->object);
}
void Resolver::visitSuper(Super *expr)
{
if (currentClass == ClassType_None)
{
Lox::error(*expr->keyword, "Can't use 'super' outside of a class.");
return;
}
else if (currentClass != ClassType_Subclass)
{
Lox::error(*expr->keyword, "Can't use 'super' in a class with no superclass.");
return;
}
resolveLocal(expr, expr->keyword);
}
void Resolver::visitThis(This *expr)
{
if (currentClass == ClassType_None)
{
Lox::error(*expr->keyword, "Can't use 'this' outside of a class.");
return;
}
resolveLocal(expr, expr->keyword);
}
void Resolver::visitLambda(Lambda* stmt)
{
resolveFunction(stmt, FunctionType_Lambda);
}
void Resolver::resolve(Expr * expr)
{
if (expr)
{
switch (expr->type)
{
case ExprType_Assign: visitAssign((Assign *)expr); break;
case ExprType_Ternary: visitTernary((Ternary *)expr); break;
case ExprType_Binary: visitBinary((Binary *)expr); break;
case ExprType_Logical: visitLogical((Logical *)expr); break;
case ExprType_Set: visitSet((Set *)expr); break;
case ExprType_Super: visitSuper((Super *)expr); break;
case ExprType_This: visitThis((This *)expr); break;
case ExprType_Grouping: visitGrouping((Grouping *)expr); break;
case ExprType_Literal: visitLiteral((Literal *)expr); break;
case ExprType_Unary: visitUnary((Unary *)expr); break;
case ExprType_Variable: visitVariable((Variable *)expr); break;
case ExprType_Call: visitCall((Call *)expr); break;
case ExprType_Get: visitGet((Get *)expr); break;
case ExprType_Lambda: visitLambda((Lambda *)expr); break;
default: Lox::error(0, "Invalid expression type.");
}
}
}
void Resolver::visitPrint(Print * stmt)
{
resolve(stmt->expression);
}
void Resolver::visitExpression(Expression * stmt)
{
resolve(stmt->expression);
}
void Resolver::visitVar(Var * stmt)
{
declare(stmt->name);
if (stmt->initializer)
{
resolve(stmt->initializer);
}
define(stmt->name);
}
void Resolver::visitBlock(Block * stmt)
{
beginScope();
resolve(stmt->statements->list);
endScope();
}
void Resolver::visitIf(If * stmt)
{
resolve(stmt->condition);
resolve(stmt->thenBranch);
if (stmt->elseBranch)
{
resolve(stmt->elseBranch);
}
}
void Resolver::visitWhile(While * stmt)
{
resolve(stmt->condition);
resolve(stmt->body);
}
void Resolver::visitBreak(Break * stmt)
{
}
void Resolver::visitFunction(Function * stmt)
{
declare(stmt->name);
define(stmt->name);
resolveFunction(stmt, FunctionType_Function);
}
void Resolver::visitClass(Class *stmt)
{
ClassType enclosingClass = currentClass;
currentClass = ClassType_Class;
declare(stmt->name);
define(stmt->name);
if (stmt->superclass)
{
currentClass = ClassType_Subclass;
if (stmt->name->lexeme == stmt->superclass->name->lexeme)
{
Lox::error(*stmt->superclass->name, "A class can't inherit from itself.");
}
resolve(stmt->superclass);
beginScope();
VariableFlags flags = { true, true };
scopes.back()->emplace("super", flags);
}
beginScope();
VariableFlags flags = { true, true };
scopes.back()->emplace("this", flags);
for (auto method : stmt->methods->list)
{
FunctionType declaration = FunctionType_Method;
if (method->name->lexeme == "init")
{
declaration = FunctionType_Initializer;
}
resolveFunction(method, declaration);
}
for (auto method : stmt->statics->list)
{
FunctionType declaration = FunctionType_Function;
if (method->name->lexeme == "init")
{
Lox::error(0, "'init' cannot be a static method");
}
resolveFunction(method, declaration);
}
endScope(); // scope for 'this'
if (stmt->superclass)
{
endScope(); // scope for 'super'
}
currentClass = enclosingClass;
}
void Resolver::resolveFunction(void *stmt, FunctionType type)
{
FunctionType enclosingFunction = currentFunction;
currentFunction = type;
beginScope();
ListToken *params = 0;
ListStmt *statements = 0;
if (type == FunctionType_Function || type == FunctionType_Method || type == FunctionType_Initializer)
{
params = ((Function *)stmt)->params;
statements = ((Function *)stmt)->body->statements;
}
else if (type == FunctionType_Lambda)
{
params = ((Lambda *)stmt)->params;
statements = ((Lambda *)stmt)->body->statements;
}
else
{
Lox::error(0, "FunctionType not implemented!");
}
for (auto ¶m : params->list)
{
declare(param);
define(param);
}
resolve(statements->list);
endScope();
currentFunction = enclosingFunction;
}
void Resolver::visitReturn(Return * stmt)
{
if (this->currentFunction == FunctionType_None)
{
Lox::error(*stmt->keyword, "Can't return from top-level code.");
}
if (stmt->value)
{
if (this->currentFunction == FunctionType_Initializer)
{
Lox::error(*stmt->keyword, "Can't return a value from an initializer.");
}
resolve(stmt->value);
}
}
void Resolver::resolve(std::vector<Stmt*> statements)
{
for (auto &stmt : statements)
{
resolve(stmt);
}
}
void Resolver::resolve(Stmt *stmt)
{
if (stmt)
{
switch (stmt->type)
{
case StmtType_Print: visitPrint((Print *)stmt); break;
case StmtType_Expression: visitExpression((Expression *)stmt); break;
case StmtType_Var: visitVar((Var *)stmt); break;
case StmtType_Block: visitBlock((Block *)stmt); break;
case StmtType_If: visitIf((If *)stmt); break;
case StmtType_While: visitWhile((While *)stmt); break;
case StmtType_Break: visitBreak((Break *)stmt); break;
case StmtType_Function: visitFunction((Function *)stmt); break;
case StmtType_Class: visitClass((Class *)stmt); break;
case StmtType_Return: visitReturn((Return *)stmt); break;
default: Lox::error(0, "Invalid statement type.");
}
}
}