-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
404 lines (317 loc) · 11.4 KB
/
parser.cpp
File metadata and controls
404 lines (317 loc) · 11.4 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "parser.h"
#include <stdexcept>
#include <sstream>
namespace AR437 {
Parser::Parser(const std::vector<Token>& tokens) : tokens(tokens), current(0) {}
std::unique_ptr<Program> Parser::parse() {
try {
return program();
} catch (const ParseError& error) {
throw;
}
}
Token& Parser::peek() {
return tokens[current];
}
Token& Parser::previous() {
return tokens[current - 1];
}
bool Parser::isAtEnd() {
return peek().type == TokenType::EOF_TOK;
}
Token Parser::advance() {
if (!isAtEnd()) current++;
return previous();
}
bool Parser::check(TokenType type) {
if (isAtEnd()) return false;
return peek().type == type;
}
bool Parser::match(const std::vector<TokenType>& types) {
for (TokenType type : types) {
if (check(type)) {
advance();
return true;
}
}
return false;
}
void Parser::consume(TokenType type, const std::string& message) {
if (check(type)) {
advance();
return;
}
Token token = peek();
throw ParseError(message + " at line " + std::to_string(token.line) +
", column " + std::to_string(token.column), token.line, token.column);
}
void Parser::synchronize() {
advance();
while (!isAtEnd()) {
if (previous().type == TokenType::SEMICOLON) return;
switch (peek().type) {
case TokenType::FUNC:
case TokenType::LET:
case TokenType::IF:
case TokenType::WHILE:
case TokenType::DISPLAY:
case TokenType::READ:
case TokenType::RETURN:
return;
default:
break;
}
advance();
}
}
std::unique_ptr<Program> Parser::program() {
auto prog = std::make_unique<Program>();
while (!isAtEnd()) {
// Skip newlines at the top level
if (match({TokenType::NEWLINE})) {
continue;
}
try {
auto decl = declaration();
if (decl) {
prog->statements.push_back(std::move(decl));
}
} catch (const ParseError& error) {
synchronize();
throw;
}
}
return prog;
}
std::unique_ptr<Statement> Parser::declaration() {
if (match({TokenType::FUNC})) return funcDeclaration();
if (match({TokenType::LET})) return varDeclaration();
return statement();
}
std::unique_ptr<Statement> Parser::varDeclaration() {
consume(TokenType::IDENTIFIER, "Expected variable name");
std::string name = previous().value;
std::unique_ptr<Expression> initializer = nullptr;
if (match({TokenType::ASSIGN})) {
initializer = expression();
}
consume(TokenType::SEMICOLON, "Expected ';' after variable declaration");
return std::make_unique<VariableDeclaration>(name, std::move(initializer));
}
std::unique_ptr<Statement> Parser::funcDeclaration() {
consume(TokenType::IDENTIFIER, "Expected function name");
std::string name = previous().value;
consume(TokenType::LPAREN, "Expected '(' after function name");
std::vector<std::string> parameters;
if (!check(TokenType::RPAREN)) {
do {
consume(TokenType::IDENTIFIER, "Expected parameter name");
parameters.push_back(previous().value);
} while (match({TokenType::COMMA}));
}
consume(TokenType::RPAREN, "Expected ')' after parameters");
auto body = blockStatement();
return std::make_unique<FunctionDeclaration>(name, parameters, std::move(body));
}
std::unique_ptr<Statement> Parser::statement() {
if (match({TokenType::DISPLAY})) return displayStatement();
if (match({TokenType::READ})) return readStatement();
if (match({TokenType::RETURN})) return returnStatement();
if (match({TokenType::IF})) return ifStatement();
if (match({TokenType::WHILE})) return whileStatement();
if (match({TokenType::LBRACE})) return blockStatement();
return expressionStatement();
}
std::unique_ptr<Statement> Parser::displayStatement() {
auto expr = expression();
consume(TokenType::SEMICOLON, "Expected ';' after display statement");
return std::make_unique<DisplayStatement>(std::move(expr));
}
std::unique_ptr<Statement> Parser::readStatement() {
consume(TokenType::IDENTIFIER, "Expected variable name after 'read'");
std::string varName = previous().value;
consume(TokenType::SEMICOLON, "Expected ';' after read statement");
return std::make_unique<ReadStatement>(varName);
}
std::unique_ptr<Statement> Parser::returnStatement() {
std::unique_ptr<Expression> value = nullptr;
if (!check(TokenType::SEMICOLON)) {
value = expression();
}
consume(TokenType::SEMICOLON, "Expected ';' after return statement");
return std::make_unique<ReturnStatement>(std::move(value));
}
std::unique_ptr<Statement> Parser::ifStatement() {
consume(TokenType::LPAREN, "Expected '(' after 'if'");
auto condition = expression();
consume(TokenType::RPAREN, "Expected ')' after if condition");
auto thenBranch = statement();
std::unique_ptr<Statement> elseBranch = nullptr;
if (match({TokenType::ELSE})) {
elseBranch = statement();
}
return std::make_unique<IfStatement>(std::move(condition), std::move(thenBranch), std::move(elseBranch));
}
std::unique_ptr<Statement> Parser::whileStatement() {
consume(TokenType::LPAREN, "Expected '(' after 'while'");
auto condition = expression();
consume(TokenType::RPAREN, "Expected ')' after while condition");
auto body = statement();
return std::make_unique<WhileStatement>(std::move(condition), std::move(body));
}
std::unique_ptr<Statement> Parser::expressionStatement() {
auto expr = expression();
consume(TokenType::SEMICOLON, "Expected ';' after expression");
return std::make_unique<ExpressionStatement>(std::move(expr));
}
std::unique_ptr<Block> Parser::blockStatement() {
auto block = std::make_unique<Block>();
while (!check(TokenType::RBRACE) && !isAtEnd()) {
// Skip newlines inside blocks
if (match({TokenType::NEWLINE})) {
continue;
}
auto stmt = declaration();
if (stmt) {
block->statements.push_back(std::move(stmt));
}
}
if (!isAtEnd()) {
consume(TokenType::RBRACE, "Expected '}' after block");
}
return block;
}
std::unique_ptr<Expression> Parser::expression() {
return assignment();
}
std::unique_ptr<Expression> Parser::assignment() {
auto expr = logicalOr();
if (match({TokenType::ASSIGN})) {
auto value = assignment();
// Check if left side is an identifier
auto identifier = dynamic_cast<Identifier*>(expr.get());
if (identifier) {
std::string name = identifier->name;
expr.release(); // Release ownership since we're using the name
return std::make_unique<Assignment>(name, std::move(value));
}
throw ParseError("Invalid assignment target");
}
return expr;
}
std::unique_ptr<Expression> Parser::logicalOr() {
auto expr = logicalAnd();
while (match({TokenType::OR})) {
TokenType operator_ = previous().type;
auto right = logicalAnd();
expr = std::make_unique<BinaryOperation>(std::move(expr), operator_, std::move(right));
}
return expr;
}
std::unique_ptr<Expression> Parser::logicalAnd() {
auto expr = equality();
while (match({TokenType::AND})) {
TokenType operator_ = previous().type;
auto right = equality();
expr = std::make_unique<BinaryOperation>(std::move(expr), operator_, std::move(right));
}
return expr;
}
std::unique_ptr<Expression> Parser::equality() {
auto expr = comparison();
while (match({TokenType::NOT_EQUAL, TokenType::EQUAL})) {
TokenType operator_ = previous().type;
auto right = comparison();
expr = std::make_unique<BinaryOperation>(std::move(expr), operator_, std::move(right));
}
return expr;
}
std::unique_ptr<Expression> Parser::comparison() {
auto expr = term();
while (match({TokenType::GREATER, TokenType::GREATER_EQUAL, TokenType::LESS, TokenType::LESS_EQUAL})) {
TokenType operator_ = previous().type;
auto right = term();
expr = std::make_unique<BinaryOperation>(std::move(expr), operator_, std::move(right));
}
return expr;
}
std::unique_ptr<Expression> Parser::term() {
auto expr = factor();
while (match({TokenType::MINUS, TokenType::PLUS})) {
TokenType operator_ = previous().type;
auto right = factor();
expr = std::make_unique<BinaryOperation>(std::move(expr), operator_, std::move(right));
}
return expr;
}
std::unique_ptr<Expression> Parser::factor() {
auto expr = unary();
while (match({TokenType::DIVIDE, TokenType::MULTIPLY, TokenType::MODULO})) {
TokenType operator_ = previous().type;
auto right = unary();
expr = std::make_unique<BinaryOperation>(std::move(expr), operator_, std::move(right));
}
return expr;
}
std::unique_ptr<Expression> Parser::unary() {
if (match({TokenType::NOT, TokenType::MINUS})) {
TokenType operator_ = previous().type;
auto right = unary();
return std::make_unique<UnaryOperation>(operator_, std::move(right));
}
return call();
}
std::unique_ptr<Expression> Parser::call() {
auto expr = primary();
while (true) {
if (match({TokenType::LPAREN})) {
// Function call
auto identifier = dynamic_cast<Identifier*>(expr.get());
if (!identifier) {
throw ParseError("Can only call functions");
}
std::string name = identifier->name;
expr.release(); // Release ownership
auto call = std::make_unique<FunctionCall>(name);
if (!check(TokenType::RPAREN)) {
do {
call->arguments.push_back(expression());
} while (match({TokenType::COMMA}));
}
consume(TokenType::RPAREN, "Expected ')' after arguments");
expr = std::move(call);
} else {
break;
}
}
return expr;
}
std::unique_ptr<Expression> Parser::primary() {
if (match({TokenType::TRUE})) {
return std::make_unique<BooleanLiteral>(true);
}
if (match({TokenType::FALSE})) {
return std::make_unique<BooleanLiteral>(false);
}
if (match({TokenType::NULL_TOK})) {
return std::make_unique<NullLiteral>();
}
if (match({TokenType::NUMBER})) {
double value = std::stod(previous().value);
return std::make_unique<NumberLiteral>(value);
}
if (match({TokenType::STRING})) {
return std::make_unique<StringLiteral>(previous().value);
}
if (match({TokenType::IDENTIFIER})) {
return std::make_unique<Identifier>(previous().value);
}
if (match({TokenType::LPAREN})) {
auto expr = expression();
consume(TokenType::RPAREN, "Expected ')' after expression");
return expr;
}
throw ParseError("Expected expression at line " + std::to_string(peek().line) +
", column " + std::to_string(peek().column));
}
} // namespace AR437