-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cs
302 lines (246 loc) · 7.1 KB
/
Parser.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
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
using System.Collections.Generic;
using LoxSharp;
using ExprNamespace;
public class Parser
{
public class ParseError : System.Exception
{
public ParseError()
{
}
}
private List<Token> tokens;
private int current = 0;
public Parser(List<Token> tokens)
{
this.tokens = tokens;
}
public List<StmtNamespace.Stmt> parse()
{
List<StmtNamespace.Stmt> statements = new List<StmtNamespace.Stmt>();
while (!isAtEnd())
{
statements.Add(declaration());
}
return statements;
}
private Expr expression()
{
return assignment();
}
private StmtNamespace.Stmt declaration()
{
try
{
if (match(new TokenType[] { TokenType.VAR })) return varDeclaration();
return statement();
}
catch (ParseError error)
{
synchronize();
return null;
}
}
private StmtNamespace.Stmt statement()
{
if (match(new TokenType[] { TokenType.PRINT })) return printStatement();
if (match(new TokenType[] { TokenType.LEFT_BRACE })) return new StmtNamespace.Stmt.Block(block());
return expressionStatement();
}
private StmtNamespace.Stmt printStatement()
{
Expr value = expression();
consume(TokenType.SEMICOLON, "Expect ';' after value.");
return new StmtNamespace.Stmt.Print(value);
}
private StmtNamespace.Stmt varDeclaration()
{
Token name = consume(TokenType.IDENTIFIER, "Expect variable name.");
Expr initializer = null;
if (match(new TokenType[] { TokenType.EQUAL }))
{
initializer = expression();
}
consume(TokenType.SEMICOLON, "Expect ';' after variable declaration.");
return new StmtNamespace.Stmt.Var(name, initializer);
}
private StmtNamespace.Stmt expressionStatement()
{
Expr expr = expression();
consume(TokenType.SEMICOLON, "Expect ';' after expression.");
return new StmtNamespace.Stmt.Expression(expr);
}
private List<StmtNamespace.Stmt> block()
{
List<StmtNamespace.Stmt> statements = new List<StmtNamespace.Stmt>();
while (!check(TokenType.RIGHT_BRACE) && !isAtEnd())
{
statements.Add(declaration());
}
consume(TokenType.RIGHT_BRACE, "Expect '}' after block.");
return statements;
}
private Expr assignment()
{
Expr expr = equality();
if (match(new TokenType[] { TokenType.EQUAL }))
{
Token equals = previous();
Expr value = assignment();
if (expr.GetType() == typeof(Expr.Variable))
{
Token name = ((Expr.Variable)expr).name;
return new Expr.Assign(name, value);
}
error(equals, "Invalid assignment target.");
}
return expr;
}
private Expr equality()
{
Expr expr = comparison();
while (match(new TokenType[] { TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL }))
{
Token op = previous();
Expr right = comparison();
expr = new Expr.Binary(expr, op, right);
}
return expr;
}
private bool match(TokenType[] types)
{
foreach (TokenType type in types)
{
if (check(type))
{
advance();
return true;
}
}
return false;
}
private bool check(TokenType type)
{
if (isAtEnd()) return false;
return peek().type == type;
}
private Token advance()
{
if (!isAtEnd()) current++;
return previous();
}
private bool isAtEnd()
{
return peek().type == TokenType.EOF;
}
private Token peek()
{
return tokens[current];
}
private Token previous()
{
return tokens[current - 1];
}
private Expr comparison()
{
Expr expr = term();
while (match(new TokenType[] { TokenType.GREATER, TokenType.GREATER_EQUAL, TokenType.LESS, TokenType.LESS_EQUAL }))
{
Token op = previous();
Expr right = term();
expr = new Expr.Binary(expr, op, right);
}
return expr;
}
private Expr term()
{
Expr expr = factor();
while (match(new TokenType[] { TokenType.MINUS, TokenType.PLUS }))
{
Token op = previous();
Expr right = factor();
expr = new Expr.Binary(expr, op, right);
}
return expr;
}
private Expr factor()
{
Expr expr = unary();
while (match(new TokenType[] { TokenType.SLASH, TokenType.STAR }))
{
Token op = previous();
Expr right = unary();
expr = new Expr.Binary(expr, op, right);
}
return expr;
}
private Expr unary()
{
if (match(new TokenType[] { TokenType.BANG, TokenType.MINUS }))
{
Token op = previous();
Expr right = unary();
return new Expr.Unary(op, right);
}
return primary();
}
private Expr primary()
{
if (match(new TokenType[] { TokenType.FALSE })) return new Expr.Literal(TokenType.FALSE);
if (match(new TokenType[] { TokenType.TRUE })) return new Expr.Literal(TokenType.TRUE);
if (match(new TokenType[] { TokenType.NIL })) return new Expr.Literal(TokenType.NIL);
if (match(new TokenType[] { TokenType.NUMBER, TokenType.STRING }))
{
return new Expr.Literal(previous().literal);
}
if (match(new TokenType[] { TokenType.IDENTIFIER }))
{
return new Expr.Variable(previous());
}
if (match(new TokenType[] { TokenType.LEFT_PAREN }))
{
Expr expr = expression();
consume(TokenType.RIGHT_PAREN, "Expect ')' after expression.");
return new Expr.Grouping(expr);
}
throw error(peek(), "Expect expression.");
}
private Token consume(TokenType type, string message)
{
if (check(type)) return advance();
throw error(peek(), message);
}
private ParseError error(Token token, string message)
{
LoxSharp.Program.error(token, message);
return new ParseError();
}
private void synchronize()
{
advance();
while (!isAtEnd())
{
if (previous().type == TokenType.SEMICOLON) return;
switch (peek().type)
{
case TokenType.CLASS:
break;
case TokenType.FUN:
break;
case TokenType.VAR:
break;
case TokenType.FOR:
break;
case TokenType.IF:
break;
case TokenType.WHILE:
break;
case TokenType.PRINT:
break;
case TokenType.RETURN:
return;
}
advance();
}
}
}