-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolver.java
More file actions
319 lines (260 loc) · 7.62 KB
/
Resolver.java
File metadata and controls
319 lines (260 loc) · 7.62 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
package dev.alephpt.Dis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
class Resolver implements Express.Visitor<Void>, Statement.Visitor<Void> {
private final Interpreter interpreter;
private final Stack<Map<String, Boolean>> scopes = new Stack<>();
private OperationType currentOperation = OperationType.NONE;
private ObjectType currentObject = ObjectType.NONE;
Resolver(Interpreter interpreter) {
this.interpreter = interpreter;
}
private enum OperationType {
NONE,
OPERATION,
METHOD
}
private enum ObjectType {
NONE,
OBJECT,
}
@Override
public Void visitObjStatement(Statement.Obj object) {
ObjectType enclosingObject = currentObject;
currentObject = ObjectType.OBJECT;
declare(object.name);
define(object.name);
beginScope();
scopes.peek().put("this", true);
for (int i = 0; i < object.body.size(); i++) {
if (object.body.get(i) instanceof Statement.Operation) {
resolveOperation(((Statement.Operation)object.body.get(i)), OperationType.METHOD);
} else if (object.body.get(i) instanceof Statement.Variable) {
resolve(((Statement.Variable)object.body.get(i)).initial);
}
}
endScope();
currentObject = enclosingObject;
return null;
}
@Override
public Void visitEnumStatement(Statement.Enum enumstmnt) {
declare(enumstmnt.name);
define(enumstmnt.name);
return null;
}
@Override
public Void visitFormStatement(Statement.Form form) {
declare(form.name);
for (Statement.Variable variable : form.members) {
declare(variable.name);
if (variable.initial != null) { resolve(variable.initial); }
define(variable.name);
}
return null;
}
@Override
public Void visitBodyStatement(Statement.Body body) {
beginScope();
resolve(body.statements);
endScope();
return null;
}
@Override
public Void visitOperationStatement(Statement.Operation operation) {
declare(operation.name);
define(operation.name);
resolveOperation(operation, OperationType.OPERATION);
return null;
}
@Override
public Void visitReturnStatement(Statement.Return returnstmnt) {
if (currentOperation == OperationType.NONE){
DisC.error(returnstmnt.keyword, "Cannot return from top-level execution.");
}
if (returnstmnt.value != null) {
resolve(returnstmnt.value);
}
return null;
}
@Override
public Void visitPrintStatement(Statement.Print print) {
resolve(print.expression);
return null;
}
@Override
public Void visitWhenStatement(Statement.When when) {
resolve(when.condition);
resolve(when.thenBranch);
for(Statement.Or or : when.orBranches) {
resolve(or.condition);
resolve(or.orBranch);
}
if (when.elseBranch != null) resolve(when.elseBranch);
return null;
}
@Override
public Void visitOrStatement(Statement.Or or) {
resolve(or.condition);
resolve(or.orBranch);
return null;
}
@Override
public Void visitWhileStatement(Statement.While whilestmnt) {
resolve(whilestmnt.condition);
resolve(whilestmnt.body);
return null;
}
@Override
public Void visitVariableStatement(Statement.Variable variable) {
declare(variable.name);
if (variable.initial != null) { resolve(variable.initial); }
define(variable.name);
return null;
}
@Override
public Void visitExpressionStatement(Statement.Expression expressstmnt) {
resolve(expressstmnt.expression);
return null;
}
@Override
public Void visitSelfExpress(Express.Self self) {
if (currentObject == ObjectType.NONE) {
DisC.error(self.keyword, "Self Reference cannot be done outside of Objects.");
}
resolveLocal(self, self.keyword);
return null;
}
@Override
public Void visitVariableExpress(Express.Variable variable) {
if(!scopes.isEmpty() && scopes.peek().get(variable.name.lexeme) == Boolean.FALSE) {
DisC.error(variable.name, "Needs to return 'none'");
}
resolveLocal(variable, variable.name);
return null;
}
@Override
public Void visitGlobalVariableExpress(Express.GlobalVariable globally) {
resolveGlobal(globally, globally.name);
return null;
}
@Override
public Void visitParentVariableExpress(Express.ParentVariable parental) {
resolveParent(parental, parental.name);
return null;
}
@Override
public Void visitGetPropsExpress(Express.GetProps props) {
resolve(props.object);
return null;
}
@Override
public Void visitSetPropsExpress(Express.SetProps props) {
resolve(props.value);
resolve(props.object);
return null;
}
@Override
public Void visitAssignExpress(Express.Assign assignment) {
resolve(assignment.value);
resolveLocal(assignment, assignment.name);
return null;
}
@Override
public Void visitBinaryExpress(Express.Binary binary) {
resolve(binary.left);
resolve(binary.right);
return null;
}
@Override
public Void visitGroupingExpress(Express.Grouping parens) {
resolve(parens.expression);
return null;
}
@Override
public Void visitCountExpress(Express.Count count) {
resolve(count.identifier);
resolveLocal(count, count.name);
return null;
}
@Override
public Void visitCallingExpress(Express.Calling call) {
resolve(call.called);
for (Express argument : call.args) {
resolve(argument);
}
return null;
}
@Override
public Void visitLiteralExpress(Express.Literal literally) {
return null;
}
@Override
public Void visitLogicalExpress(Express.Logical logical) {
resolve(logical.left);
resolve(logical.right);
return null;
}
@Override
public Void visitUnaryExpress(Express.Unary unary) {
resolve(unary.right);
return null;
}
/// HELPER FUNCTIONS //
private void beginScope() { scopes.push(new HashMap<String, Boolean>()); }
private void endScope() { scopes.pop(); }
private void resolve(Statement statement) { statement.accept(this); }
private void resolve(Express express) { express.accept(this); }
void resolve(List<Statement> statements) {
for (Statement statement : statements) {
resolve(statement);
}
}
private void resolveLocal(Express express, Token name) {
for (int i = scopes.size() - 1; i >= 0; i--) {
if (scopes.get(i).containsKey(name.lexeme)) {
interpreter.resolve(express, scopes.size() - 1 - i);
return;
}
}
}
private void resolveGlobal(Express express, Token name) {
if (scopes.get(0).containsKey(name.lexeme)) {
interpreter.resolve(express, scopes.size() - 1); // -1?
return;
}
}
private void resolveParent(Express express, Token name) {
int scopeSize = scopes.size() - 1;
if (scopes.get(scopeSize).containsKey(name.lexeme)) {
interpreter.resolve(express, scopeSize);
return;
}
}
private void resolveOperation(Statement.Operation operation, OperationType type) {
OperationType enclosingOperation = currentOperation;
currentOperation = type;
beginScope(); // is this what we need?
for (Token parameter : operation.params) {
declare(parameter);
define(parameter);
}
resolve(operation.body);
endScope();
currentOperation = enclosingOperation;
}
private void declare(Token name) {
if (scopes.isEmpty()) return;
Map<String, Boolean> scope = scopes.peek();
if (scope.containsKey(name.lexeme)) {
DisC.error(name, "This variable already exists within this scope.");
}
scope.put(name.lexeme, false);
}
private void define(Token name) {
if (scopes.isEmpty()) return;
scopes.peek().put(name.lexeme, true);
}
}