-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolver.cs
256 lines (214 loc) · 6.84 KB
/
Resolver.cs
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
using System.Collections.Generic;
// group members: Peter Zhang, Madeline Moore, Cara Cannarozzi
// Crafting Interpreters book by Robert Nystrom used as a reference
// https://craftinginterpreters.com/contents.html
namespace LoxInterpreter
{
/// <summary>
/// resolves statements
/// </summary>
public class Resolver : Expr.IVisitor<object>, Stmt.IVisitor<object>
{
private readonly Interpreter interpreter;
private readonly List<Dictionary<string, bool>> scopes = new List<Dictionary<string, bool>>();
private FunctionType currentFunction = FunctionType.NONE;
public Resolver(Interpreter interpreter)
{
this.interpreter = interpreter;
}
// begins scope, traverses into statements, discards scope
public object VisitBlockStmt(Stmt.Block stmt)
{
BeginScope();
Resolve(stmt.statements);
EndScope();
return null;
}
// resolves expression statement
public object VisitExpressionStmt(Stmt.Expression stmt)
{
Resolve(stmt.expression);
return null;
}
// resolves functions
public object VisitFunctionStmt(Stmt.Function stmt)
{
Declare(stmt.name);
Define(stmt.name);
ResolveFunction(stmt, FunctionType.FUNCTION);
return null;
}
// resolves if statement
public object VisitIfStmt(Stmt.If stmt)
{
Resolve(stmt.condition);
Resolve(stmt.thenBranch);
if (stmt.elseBranch != null) Resolve(stmt.elseBranch);
return null;
}
// resolves print statement
public object VisitPrintStmt(Stmt.Print stmt)
{
Resolve(stmt.expression);
return null;
}
// resolves return statement
public object VisitReturnStmt(Stmt.Return stmt)
{
Resolve(stmt.Value);
return null;
}
// resolves variable declarations
public object VisitVarStmt(Stmt.Var stmt)
{
Declare(stmt.name);
if (stmt.initializer != null) Resolve(stmt.initializer);
Define(stmt.name);
return null;
}
// resolves while loop statement
public object VisitWhileStmt(Stmt.While stmt)
{
Resolve(stmt.condition);
Resolve(stmt.body);
return null;
}
// resolves variable assignment
public object VisitAssignExpr(Expr.Assign expr)
{
Resolve(expr.value);
ResolveLocal(expr, expr.name);
return null;
}
// resolves binary expressions
public object VisitBinaryExpr(Expr.Binary expr)
{
// resolves left and right sides of expression
Resolve(expr.left);
Resolve(expr.right);
return null;
}
// resolves function calls
public object VisitCallExpr(Expr.Call expr)
{
Resolve(expr.callee);
foreach (var argument in expr.arguments) Resolve(argument);
return null;
}
public object VisitGetExpr(Expr.Get expr)
{
Resolve(expr.obj);
return null;
}
// resolves parentheses
public object VisitGroupingExpr(Expr.Grouping expr)
{
Resolve(expr.expression);
return null;
}
// resolves literals
public object VisitLiteralExpr(Expr.Literal expr)
{
return null;
}
// resolves logical expressions
public object VisitLogicalExpr(Expr.Logical expr)
{
Resolve(expr.left);
Resolve(expr.right);
return null;
}
// resolves set expressions (setters)
public object VisitSetExpr(Expr.Set expr)
{
Resolve(expr.value);
Resolve(expr.obj);
return null;
}
// resolves unary expressions
public object VisitUnaryExpr(Expr.Unary expr)
{
Resolve(expr.right);
return null;
}
// resolves variable expressions
public object VisitVariableExpr(Expr.Variable expr)
{
ResolveLocal(expr, expr.name);
return null;
}
// resolves each statement in a list of statements
public void Resolve(List<Stmt> statements)
{
foreach (var statement in statements) Resolve(statement);
}
// resolves statements
private void Resolve(Stmt stmt)
{
stmt.Accept(this);
}
// resolves expressions
private void Resolve(Expr expr)
{
expr.Accept(this);
}
// resolves function body (helper to VisitFunctionStmt)
private void ResolveFunction(Stmt.Function function, FunctionType type)
{
var enclosingFunction = currentFunction;
currentFunction = type;
BeginScope();
// declares and defines function parameters
foreach (var param in function.parms)
{
Declare(param);
Define(param);
}
// resolves function body
Resolve(function.body);
EndScope();
currentFunction = enclosingFunction;
}
// creates new block scope
private void BeginScope()
{
scopes.Add(new Dictionary<string, bool>());
}
// removes scope from the dictionary of scopes (popping if this were a stack)
private void EndScope()
{
scopes.RemoveAt(scopes.Count - 1);
}
// adds variable to inner scope
private void Declare(Token name)
{
if (scopes.Count < 1) return;
var scope = scopes[scopes.Count - 1];
scope.Add(name.lexeme, false);
}
// defines a variable
private void Define(Token name)
{
if (scopes.Count < 1)
return;
scopes[scopes.Count - 1][name.lexeme] = true;
}
// resolves variable (helper function for VisitVariableExpr)
private void ResolveLocal(Expr expr, Token name)
{
for (var i = scopes.Count - 1; i >= 0; i--)
// Csharp doesn't access Stack<> by index (without resorting to Linq), hence used List<> instead
if (scopes[i].ContainsKey(name .lexeme))
{
interpreter.Resolve(expr, scopes.Count - 1 - i);
return;
}
}
// function types (only none and function since not implementing classes)
private enum FunctionType
{
NONE,
FUNCTION,
}
}
}