Repository navigation
Expand file tree
/
Copy pathResolver.cpp
More file actions
249 lines (209 loc) · 6.13 KB
/
Copy pathResolver.cpp
File metadata and controls
249 lines (209 loc) · 6.13 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
#include "Resolver.h"
#include "types.hpp"
#include "Interpreter.h"
void Resolver::beginScope() {
scopes.push_back(std::unordered_map<std::string, bool>{});
}
void Resolver::endScope() {
scopes.pop_back();
}
void Resolver::declare(const Token &token) {
// std::cout << "[Resolver] declare: " << token << " scopes : " << scopes.size() << std::endl;
if (scopes.empty()) return;
auto scope = scopes.back();
if (scope.contains(token.lexeme)) {
has_error = true;
error(token, "Already a variable with this name in this scope.");
}
scope[token.lexeme] = false;
}
void Resolver::define(const Token &token) {
if (scopes.empty()) return;
scopes.back()[token.lexeme] = true;
}
ObjPtr Resolver::visitVariable(const Expr::Variable &expr) {
if (scopes.empty()) {
resolveLocal(expr, expr.name);
return ObjPtr();
}
const auto scope = scopes.back();
const auto it = scope.find(expr.name.lexeme);
if (
it != scope.end() &&
it->second == false
) {
has_error = true;
error(expr.name, "Can't read local variable in its own initializer.");
};
resolveLocal(expr, expr.name);
return ObjPtr();
}
ObjPtr Resolver::visitAssign(const Expr::Assign &expr) {
resolve(*expr.value);
resolveLocal(expr, expr.name);
return ObjPtr();
}
ObjPtr Resolver::visitBinary(const Expr::Binary &expr) {
resolve(*expr.left);
resolve(*expr.right);
return ObjPtr();
}
ObjPtr Resolver::visitCall(const Expr::Call &expr) {
resolve(*expr.callee);
for (auto &arg: *expr.arguments) {
resolve(*arg);
}
return ObjPtr();
}
ObjPtr Resolver::visitGet(const Expr::Get &expr) {
resolve(*expr.object);
return ObjPtr();
}
ObjPtr Resolver::visitGrouping(const Expr::Grouping &expr) {
resolve(*expr.expression);
return ObjPtr();
}
ObjPtr Resolver::visitLiteral(const Expr::Literal &expr) {
return ObjPtr();
}
ObjPtr Resolver::visitLogical(const Expr::Logical &expr) {
resolve(*expr.left);
resolve(*expr.right);
return ObjPtr();
}
ObjPtr Resolver::visitSet(const Expr::Set &expr) {
resolve(*expr.value);
resolve(*expr.object);
return ObjPtr();
}
ObjPtr Resolver::visitSuper(const Expr::Super &expr) {
if (currentClass == ClassType::NONE) {
has_error = true;
error(expr.keyword, "Can't use 'super' outside of a class.");
} else if (currentClass != ClassType::SUBCLASS) {
has_error = true;
error(expr.keyword, "Can't use 'super' in a class with no superclass.");
}
resolveLocal(expr, expr.keyword);
return ObjPtr();
}
ObjPtr Resolver::visitThis(const Expr::This &expr) {
if (currentClass == ClassType::NONE) {
has_error = true;
error(expr.keyword, "Can't use 'this' outside of a class.");
} // else if (currentFunction == FunctionType::STATIC_METHOD) {
// has_error = true;
// error(expr.keyword, "Can't use 'this' inside a static method.");
// }
resolveLocal(expr, expr.keyword);
return ObjPtr();
}
ObjPtr Resolver::visitUnary(const Expr::Unary &expr) {
resolve(*expr.right);
return ObjPtr();
}
void Resolver::visitBlock(const Stmt::Block &stmt) {
beginScope();
resolve(*stmt.statements);
endScope();
}
void Resolver::visitVar(const Stmt::Var &stmt) {
declare(stmt.name);
if (stmt.initializer != nullptr) {
resolve(*stmt.initializer);
}
define(stmt.name);
}
void Resolver::visitFunction(const Stmt::Function &stmt) {
declare(stmt.name);
define(stmt.name);
resolveFunction(stmt, FunctionType::FUNCTION);
}
void Resolver::visitClass(const Stmt::Class &stmt) {
ClassType enclosingClass = currentClass;
currentClass = ClassType::CLASS;
declare(stmt.name);
define(stmt.name);
if (stmt.superclass != nullptr && stmt.name.lexeme == stmt.superclass->name.lexeme) {
has_error = true;
error(stmt.superclass->name, "A class can't inherit from itself.");
}
if (stmt.superclass != nullptr) {
currentClass = ClassType::SUBCLASS;
resolve(*stmt.superclass);
beginScope();
scopes.back()["super"] = true;
}
beginScope();
scopes.back()["this"] = true;
for (const auto& method: *stmt.methods) {
resolveFunction(*method, (method->name.lexeme == "init" ? FunctionType::INITIALIZER : (method->is_static ? FunctionType::STATIC_METHOD : FunctionType::METHOD)));
}
endScope();
if (stmt.superclass != nullptr) endScope();
currentClass = enclosingClass;
}
void Resolver::resolveLocal(const Expr::Expr &expr, const Token &name) const {
for (int i = scopes.size() - 1; i >= 0; i--) {
if (scopes.at(i).contains(name.lexeme)) {
interpreter_->resolve(expr, scopes.size() - i - 1);
return;
}
}
}
void Resolver::resolveFunction(const Stmt::Function &stmt, FunctionType type) {
const FunctionType enclosingFunction = currentFunction;
currentFunction = type;
beginScope();
for (auto ¶m : stmt.params) {
declare(param);
define(param);
}
resolve(*stmt.body);
endScope();
currentFunction = enclosingFunction;
}
void Resolver::visitExpression(const Stmt::Expression &stmt) {
resolve(*stmt.expression);
}
void Resolver::visitIf(const Stmt::If &stmt) {
resolve(*stmt.condition);
resolve(*stmt.thenBranch);
if (stmt.elseBranch != nullptr) resolve(*stmt.elseBranch);
}
void Resolver::visitPrint(const Stmt::Print &stmt) {
resolve(*stmt.expression);
}
void Resolver::visitReturn(const Stmt::Return &stmt) {
if (currentFunction == FunctionType::NONE) {
has_error = true;
error(stmt.keyword, "Can't return from top-level code.");
}
if (stmt.value != nullptr) {
if (currentFunction == FunctionType::INITIALIZER) {
has_error = true;
error(stmt.keyword, "Can't return a value from an initializer.");
}
resolve(*stmt.value);
}
}
void Resolver::visitWhile(const Stmt::While &stmt) {
resolve(*stmt.condition);
resolve(*stmt.body);
}
void Resolver::resolve(const std::vector<std::unique_ptr<Stmt::Stmt>> &statements) {
for (const auto& stmt : statements) {
resolve(*stmt);
}
}
void Resolver::resolve(const std::shared_ptr<std::vector<std::unique_ptr<Stmt::Stmt>>> &statements) {
for (const auto& stmt : *statements) {
resolve(*stmt);
}
}
void Resolver::resolve(const Stmt::Stmt &stmt) {
stmt.accept(*this);
}
void Resolver::resolve(const Expr::Expr &expr) {
expr.accept(*this);
}