Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Parser.cpp
Go to the documentation of this file.
1#include "Parser.hpp"
2#include <iostream>
3
4namespace Phasor
5{
6
7using namespace AST;
8
9Parser::Parser(const std::vector<Token> &tokens) : tokens(tokens)
10{
11}
12
13std::unique_ptr<Program> Parser::parse()
14{
15 auto program = std::make_unique<Program>();
16 while (!isAtEnd())
17 {
18 program->statements.push_back(declaration());
19 }
20 return program;
21}
22
23std::unique_ptr<Statement> Parser::declaration()
24{
25 if (check(TokenType::Keyword) && peek().lexeme == "fn")
26 {
27 advance();
28 return functionDeclaration();
29 }
30 if (check(TokenType::Keyword) && peek().lexeme == "var")
31 {
32 advance();
33 return varDeclaration();
34 }
35 if (check(TokenType::Keyword) && peek().lexeme == "import")
36 {
37 advance();
38 return importStatement();
39 }
40 if (check(TokenType::Keyword) && peek().lexeme == "export")
41 {
42 advance();
43 return exportStatement();
44 }
45 if (check(TokenType::Keyword) && peek().lexeme == "struct")
46 {
47 return structDecl();
48 }
49 return statement();
50}
51
52std::unique_ptr<Statement> Parser::functionDeclaration()
53{
54 Token name = consume(TokenType::Identifier, "Expect function name.");
55 consume(TokenType::Symbol, "(", "Expect '(' after function name.");
56 std::vector<FunctionDecl::Param> params;
57 if (!check(TokenType::Symbol) || peek().lexeme != ")")
58 {
59 do
60 {
61 Token paramName = consume(TokenType::Identifier, "Expect parameter name.");
62 consume(TokenType::Symbol, ":", "Expect ':' after parameter name.");
63 auto type = parseType();
64 params.push_back({paramName.lexeme, std::move(type)});
65 } while (match(TokenType::Symbol, ","));
66 }
67 consume(TokenType::Symbol, ")", "Expect ')' after parameters.");
68
69 std::unique_ptr<TypeNode> returnType = nullptr;
70 if (match(TokenType::Symbol, "->"))
71 {
72 returnType = parseType();
73 }
74
75 consume(TokenType::Symbol, "{", "Expect '{' before function body.");
76
77 // Track function context for better error messages
78 std::string previousFunction = currentFunction;
80
81 auto body = block();
82
83 // Restore previous function context
84 currentFunction = previousFunction;
85
86 return std::make_unique<FunctionDecl>(name.lexeme, std::move(params), std::move(returnType), std::move(body));
87}
88
89std::unique_ptr<TypeNode> Parser::parseType()
90{
91 bool isPointer = false;
92 if (match(TokenType::Symbol, "*"))
93 {
94 isPointer = true;
95 }
96 Token typeName = consume(TokenType::Identifier, "Expect type name.");
97
98 std::vector<int> dims;
99 while (match(TokenType::Symbol, "[")) {
100 Token size = consume(TokenType::Number, "Expect array size in type declaration.");
101 dims.push_back(std::stoi(size.lexeme));
102 consume(TokenType::Symbol, "]", "Expect ']' after array size.");
103 }
104 return std::make_unique<TypeNode>(typeName.lexeme, isPointer, dims);
105}
106
107std::unique_ptr<Statement> Parser::varDeclaration()
108{
109 Token name = consume(TokenType::Identifier, "Expect variable name.");
110 std::unique_ptr<Expression> initializer = nullptr;
111 if (match(TokenType::Symbol, "="))
112 {
113 initializer = expression();
114 }
115 consume(TokenType::Symbol, ";", "Expect ';' after variable declaration.");
116 return std::make_unique<VarDecl>(name.lexeme, std::move(initializer));
117}
118
119std::unique_ptr<Statement> Parser::statement()
120{
121 if (check(TokenType::Keyword) && peek().lexeme == "print")
122 {
123 advance();
124 return printStatement();
125 }
126 if (check(TokenType::Keyword) && peek().lexeme == "if")
127 {
128 advance();
129 return ifStatement();
130 }
131 if (check(TokenType::Keyword) && peek().lexeme == "while")
132 {
133 advance();
134 return whileStatement();
135 }
136 if (check(TokenType::Keyword) && peek().lexeme == "for")
137 {
138 advance();
139 return forStatement();
140 }
141 if (check(TokenType::Keyword) && peek().lexeme == "switch")
142 {
143 advance();
144 return switchStatement();
145 }
146 if (check(TokenType::Keyword) && peek().lexeme == "return")
147 {
148 advance();
149 return returnStatement();
150 }
151 if (check(TokenType::Keyword) && peek().lexeme == "break")
152 {
153 advance();
154 consume(TokenType::Symbol, ";", "Expect ';' after 'break'.");
155 return std::make_unique<BreakStmt>();
156 }
157 if (check(TokenType::Keyword) && peek().lexeme == "continue")
158 {
159 advance();
160 consume(TokenType::Symbol, ";", "Expect ';' after 'continue'.");
161 return std::make_unique<ContinueStmt>();
162 }
163 if (check(TokenType::Keyword) && peek().lexeme == "unsafe")
164 {
165 advance();
166 return unsafeStatement();
167 }
168 if (check(TokenType::Symbol) && peek().lexeme == "{")
169 {
170 advance();
171 auto blk = block();
172 return blk;
173 }
174 return expressionStatement();
175}
176
177std::unique_ptr<Statement> Parser::ifStatement()
178{
179 consume(TokenType::Symbol, "(", "Expect '(' after 'if'.");
180 auto condition = expression();
181 consume(TokenType::Symbol, ")", "Expect ')' after if condition.");
182 auto thenBranch = statement();
183 std::unique_ptr<Statement> elseBranch = nullptr;
184 if (match(TokenType::Keyword, "else"))
185 {
186 elseBranch = statement();
187 }
188 return std::make_unique<IfStmt>(std::move(condition), std::move(thenBranch), std::move(elseBranch));
189}
190
191std::unique_ptr<Statement> Parser::whileStatement()
192{
193 consume(TokenType::Symbol, "(", "Expect '(' after 'while'.");
194 auto condition = expression();
195 consume(TokenType::Symbol, ")", "Expect ')' after while condition.");
196 auto body = statement();
197 return std::make_unique<WhileStmt>(std::move(condition), std::move(body));
198}
199
200std::unique_ptr<Statement> Parser::forStatement()
201{
202 consume(TokenType::Symbol, "(", "Expect '(' after 'for'.");
203
204 std::unique_ptr<Statement> initializer = nullptr;
205 if (!check(TokenType::Symbol) || peek().lexeme != ";")
206 {
207 if (check(TokenType::Keyword) && peek().lexeme == "var")
208 {
209 advance();
210 initializer = varDeclaration();
211 }
212 else
213 {
214 initializer = expressionStatement();
215 }
216 }
217 else
218 {
219 consume(TokenType::Symbol, ";", "Expect ';'.");
220 }
221
222 std::unique_ptr<Expression> condition = nullptr;
223 if (!check(TokenType::Symbol) || peek().lexeme != ";")
224 {
225 condition = expression();
226 }
227 consume(TokenType::Symbol, ";", "Expect ';' after loop condition.");
228
229 std::unique_ptr<Expression> increment = nullptr;
230 if (!check(TokenType::Symbol) || peek().lexeme != ")")
231 {
232 increment = expression();
233 }
234 consume(TokenType::Symbol, ")", "Expect ')' after for clauses.");
235
236 auto body = statement();
237 return std::make_unique<ForStmt>(std::move(initializer), std::move(condition), std::move(increment),
238 std::move(body));
239}
240
241std::unique_ptr<Statement> Parser::switchStatement()
242{
243 consume(TokenType::Symbol, "(", "Expect '(' after 'switch'.");
244 auto expr = expression();
245 consume(TokenType::Symbol, ")", "Expect ')' after switch expression.");
246 consume(TokenType::Symbol, "{", "Expect '{' after switch.");
247
248 std::vector<CaseClause> cases;
249 std::vector<std::unique_ptr<Statement>> defaultStmts;
250
251 while (!check(TokenType::Symbol) || peek().lexeme != "}")
252 {
253 if (isAtEnd())
254 throw std::runtime_error("Unterminated switch statement.");
255
256 if (check(TokenType::Keyword) && peek().lexeme == "case")
257 {
258 advance();
259 auto caseValue = expression();
260 consume(TokenType::Symbol, ":", "Expect ':' after case value.");
261
262 std::vector<std::unique_ptr<Statement>> stmts;
263 while ((!check(TokenType::Keyword) || (peek().lexeme != "case" && peek().lexeme != "default")) &&
264 (!check(TokenType::Symbol) || peek().lexeme != "}"))
265 {
266 if (isAtEnd())
267 throw std::runtime_error("Unterminated case clause.");
268 stmts.push_back(declaration());
269 }
270 cases.emplace_back(std::move(caseValue), std::move(stmts));
271 }
272 else if (check(TokenType::Keyword) && peek().lexeme == "default")
273 {
274 advance();
275 consume(TokenType::Symbol, ":", "Expect ':' after default.");
276
277 while ((!check(TokenType::Keyword) || (peek().lexeme != "case" && peek().lexeme != "default")) &&
278 (!check(TokenType::Symbol) || peek().lexeme != "}"))
279 {
280 if (isAtEnd())
281 throw std::runtime_error("Unterminated default clause.");
282 defaultStmts.push_back(declaration());
283 }
284 }
285 else
286 {
287 throw std::runtime_error("Expected 'case' or 'default' in switch statement.");
288 }
289 }
290
291 consume(TokenType::Symbol, "}", "Expect '}' after switch body.");
292 return std::make_unique<SwitchStmt>(std::move(expr), std::move(cases), std::move(defaultStmts));
293}
294
295std::unique_ptr<Statement> Parser::returnStatement()
296{
297 std::unique_ptr<Expression> value = nullptr;
298 if (!check(TokenType::Symbol) || peek().lexeme != ";")
299 {
300 value = expression();
301 }
302 consume(TokenType::Symbol, ";", "Expect ';' after return value.");
303 return std::make_unique<ReturnStmt>(std::move(value));
304}
305
306std::unique_ptr<Statement> Parser::unsafeStatement()
307{
308 consume(TokenType::Symbol, "{", "Expect '{' after 'unsafe'.");
309 return std::make_unique<UnsafeBlockStmt>(block());
310}
311
312std::unique_ptr<BlockStmt> Parser::block()
313{
314 std::vector<std::unique_ptr<Statement>> statements;
315 while (!check(TokenType::Symbol) || peek().lexeme != "}")
316 {
317 if (isAtEnd())
318 throw std::runtime_error("Unterminated block.");
319 statements.push_back(declaration());
320 }
321 consume(TokenType::Symbol, "}", "Expect '}' after block.");
322 return std::make_unique<BlockStmt>(std::move(statements));
323}
324
325std::unique_ptr<Statement> Parser::printStatement()
326{
327 auto expr = expression();
328 consume(TokenType::Symbol, ";", "Expect ';' after print statement.");
329 return std::make_unique<PrintStmt>(std::move(expr));
330}
331
332std::unique_ptr<Statement> Parser::importStatement()
333{
334 Token path = consume(TokenType::String, "Expect string after 'import'.");
335 consume(TokenType::Symbol, ";", "Expect ';' after import statement.");
336 return std::make_unique<ImportStmt>(path.lexeme);
337}
338
339std::unique_ptr<Statement> Parser::exportStatement()
340{
341 return std::make_unique<ExportStmt>(declaration());
342}
343
344std::unique_ptr<Statement> Parser::expressionStatement()
345{
346 auto expr = expression();
347 consume(TokenType::Symbol, ";", "Expect ';' after expression.");
348 return std::make_unique<ExpressionStmt>(std::move(expr));
349}
350
351std::unique_ptr<Expression> Parser::expression()
352{
353 return assignment();
354}
355
356std::unique_ptr<Expression> Parser::assignment()
357{
358 auto expr = logicalOr();
359
360 if (match(TokenType::Symbol, "="))
361 {
362 auto value = assignment(); // Right-associative
363 return std::make_unique<AssignmentExpr>(std::move(expr), std::move(value));
364 }
365
366 return expr;
367}
368
369std::unique_ptr<Expression> Parser::logicalOr()
370{
371 auto expr = logicalAnd();
372
373 while (check(TokenType::Symbol) && peek().lexeme == "||")
374 {
375 advance();
376 auto right = logicalAnd();
377 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::Or, std::move(right));
378 }
379
380 return expr;
381}
382
383std::unique_ptr<Expression> Parser::logicalAnd()
384{
385 auto expr = equality();
386
387 while (check(TokenType::Symbol) && peek().lexeme == "&&")
388 {
389 advance();
390 auto right = equality();
391 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::And, std::move(right));
392 }
393
394 return expr;
395}
396
397std::unique_ptr<Expression> Parser::equality()
398{
399 auto expr = comparison();
400
401 while (check(TokenType::Symbol) && (peek().lexeme == "==" || peek().lexeme == "!="))
402 {
403 Token op = advance();
404 auto right = comparison();
405 BinaryOp binOp = (op.lexeme == "==") ? BinaryOp::Equal : BinaryOp::NotEqual;
406 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
407 }
408
409 return expr;
410}
411
412std::unique_ptr<Expression> Parser::comparison()
413{
414 auto expr = term();
415
416 while (check(TokenType::Symbol) &&
417 (peek().lexeme == "<" || peek().lexeme == ">" || peek().lexeme == "<=" || peek().lexeme == ">="))
418 {
419 Token op = advance();
420 auto right = term();
421 BinaryOp binOp;
422 if (op.lexeme == "<")
423 binOp = BinaryOp::LessThan;
424 else if (op.lexeme == ">")
425 binOp = BinaryOp::GreaterThan;
426 else if (op.lexeme == "<=")
427 binOp = BinaryOp::LessEqual;
428 else
429 binOp = BinaryOp::GreaterEqual;
430
431 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
432 }
433
434 return expr;
435}
436
437std::unique_ptr<Expression> Parser::term()
438{
439 auto expr = factor();
440
441 while (check(TokenType::Symbol) && (peek().lexeme == "+" || peek().lexeme == "-"))
442 {
443 Token op = advance();
444 auto right = factor();
445 BinaryOp binOp = (op.lexeme == "+") ? BinaryOp::Add : BinaryOp::Subtract;
446 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
447 }
448
449 return expr;
450}
451
452std::unique_ptr<Expression> Parser::factor()
453{
454 auto expr = unary();
455
456 while (check(TokenType::Symbol) && (peek().lexeme == "*" || peek().lexeme == "/" || peek().lexeme == "%"))
457 {
458 Token op = advance();
459 auto right = unary();
460 BinaryOp binOp;
461 if (op.lexeme == "*")
462 binOp = BinaryOp::Multiply;
463 else if (op.lexeme == "/")
464 binOp = BinaryOp::Divide;
465 else
466 binOp = BinaryOp::Modulo;
467
468 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
469 }
470
471 return expr;
472}
473
474std::unique_ptr<Expression> Parser::unary()
475{
476 if (check(TokenType::Symbol) && (peek().lexeme == "!" || peek().lexeme == "-"))
477 {
478 Token op = advance();
479 auto right = unary();
480 UnaryOp uOp = (op.lexeme == "!") ? UnaryOp::Not : UnaryOp::Negate;
481 return std::make_unique<UnaryExpr>(uOp, std::move(right));
482 }
483 if (check(TokenType::Symbol) && peek().lexeme == "&")
484 {
485 advance();
486 auto right = unary();
487 return std::make_unique<UnaryExpr>(UnaryOp::AddressOf, std::move(right));
488 }
489 if (check(TokenType::Symbol) && peek().lexeme == "*")
490 {
491 advance();
492 auto right = unary();
493 return std::make_unique<UnaryExpr>(UnaryOp::Dereference, std::move(right));
494 }
495 return call();
496}
497
498std::unique_ptr<Expression> Parser::call()
499{
500 auto expr = primary();
501
502 while (true)
503 {
504 if (match(TokenType::Symbol, "("))
505 {
506 expr = finishCall(std::move(expr));
507 }
508 else if (match(TokenType::Symbol, "."))
509 {
510 expr = fieldAccess(std::move(expr));
511 }
512 else if (match(TokenType::Symbol, "++"))
513 {
514 expr = std::make_unique<PostfixExpr>(PostfixOp::Increment, std::move(expr));
515 }
516 else if (match(TokenType::Symbol, "--"))
517 {
518 expr = std::make_unique<PostfixExpr>(PostfixOp::Decrement, std::move(expr));
519 }
520 else if (match(TokenType::Symbol, "["))
521 {
522 auto index = expression();
523 consume(TokenType::Symbol, "]", "Expect ']' after index.");
524 expr = std::make_unique<ArrayAccessExpr>(std::move(expr), std::move(index));
525 }
526 else
527 {
528 break;
529 }
530 }
531
532 return expr;
533}
534
535std::unique_ptr<Expression> Parser::finishCall(std::unique_ptr<Expression> callee)
536{
537 std::vector<std::unique_ptr<Expression>> arguments;
538 if (!check(TokenType::Symbol) || peek().lexeme != ")")
539 {
540 do
541 {
542 arguments.push_back(expression());
543 } while (match(TokenType::Symbol, ","));
544 }
545 consume(TokenType::Symbol, ")", "Expect ')' after arguments.");
546
547 // For now, we only support direct function calls by name, or calls on field access which are
548 // rewritten to pass the object as the first argument.
549 if (auto ident = dynamic_cast<IdentifierExpr *>(callee.get()))
550 {
551 return std::make_unique<CallExpr>(ident->name, std::move(arguments));
552 }
553 else if (auto field = dynamic_cast<FieldAccessExpr *>(callee.get()))
554 {
555 // Transform obj.method(args) -> method(obj, args)
556 std::string methodName = field->fieldName;
557 arguments.insert(arguments.begin(), std::move(field->object));
558 return std::make_unique<CallExpr>(methodName, std::move(arguments));
559 }
560
561 throw std::runtime_error("Can only call named functions.");
562}
563
564std::unique_ptr<Expression> Parser::primary()
565{
567 {
568 return std::make_unique<NumberExpr>(previous().lexeme);
569 }
571 {
572 return std::make_unique<StringExpr>(previous().lexeme);
573 }
575 {
576 // Look ahead for struct instance syntax: Name{ ... }
577 Token identTok = peek();
578 if (!isAtEnd() && peekNext().type == TokenType::Symbol && peekNext().lexeme == "{")
579 {
580 return structInstance();
581 }
582 advance();
583 return std::make_unique<IdentifierExpr>(identTok.lexeme);
584 }
585 if (match(TokenType::Symbol, "["))
586 {
587 std::vector<std::unique_ptr<Expression>> elements;
588 if (!check(TokenType::Symbol) || peek().lexeme != "]") {
589 do {
590 elements.push_back(expression());
591 } while (match(TokenType::Symbol, ","));
592 }
593 consume(TokenType::Symbol, "]", "Expect ']' after array elements.");
594 return std::make_unique<ArrayLiteralExpr>(std::move(elements));
595 }
596 if (match(TokenType::Keyword, "true"))
597 {
598 return std::make_unique<BooleanExpr>(true);
599 }
600 if (match(TokenType::Keyword, "false"))
601 {
602 return std::make_unique<BooleanExpr>(false);
603 }
604 if (match(TokenType::Keyword, "null"))
605 {
606 return std::make_unique<NullExpr>();
607 }
608 if (match(TokenType::Symbol, "("))
609 {
610 auto expr = expression();
611 consume(TokenType::Symbol, ")", "Expect ')' after expression.");
612 return expr;
613 }
614 std::cerr << "Error: Expect expression at '" << peek().lexeme << "'";
615 std::cerr << " (line " << peek().line << ", column " << peek().column << ")\n";
616 throw std::runtime_error("Expect expression.");
617}
618
619// Struct declaration
620// struct Point { x: int, y: int }
621std::unique_ptr<StructDecl> Parser::structDecl()
622{
623 // 'struct' keyword
624 consume(TokenType::Keyword, "struct", "Expected 'struct'");
625 Token nameTok = consume(TokenType::Identifier, "Expected struct name");
626 consume(TokenType::Symbol, "{", "Expected '{' in struct declaration");
627
628 std::vector<StructField> fields;
629 while (!check(TokenType::Symbol) || peek().lexeme != "}")
630 {
631 if (isAtEnd())
632 throw std::runtime_error("Unterminated struct declaration.");
633
634 Token fieldNameTok = consume(TokenType::Identifier, "Expected field name");
635 consume(TokenType::Symbol, ":", "Expected ':' after field name");
636 auto type = parseType();
637 fields.emplace_back(fieldNameTok.lexeme, std::move(type));
638
639 if (!match(TokenType::Symbol, ","))
640 {
641 break;
642 }
643 }
644
645 consume(TokenType::Symbol, "}", "Expected '}' after struct fields");
646 return std::make_unique<StructDecl>(nameTok.lexeme, std::move(fields));
647}
648
649// Struct instantiation
650// Point{ x: 10, y: 20 }
651std::unique_ptr<StructInstanceExpr> Parser::structInstance()
652{
653 Token nameTok = consume(TokenType::Identifier, "Expected struct name");
654 consume(TokenType::Symbol, "{", "Expected '{' in struct instance");
655
656 std::vector<std::pair<std::string, std::unique_ptr<Expression>>> fields;
657 while (!check(TokenType::Symbol) || peek().lexeme != "}")
658 {
659 if (isAtEnd())
660 throw std::runtime_error("Unterminated struct instance.");
661
662 Token fieldNameTok = consume(TokenType::Identifier, "Expected field name");
663 consume(TokenType::Symbol, ":", "Expected ':' after field name");
664 auto value = expression();
665 fields.emplace_back(fieldNameTok.lexeme, std::move(value));
666
667 if (!match(TokenType::Symbol, ","))
668 {
669 break;
670 }
671 }
672
673 consume(TokenType::Symbol, "}", "Expected '}' after struct fields");
674 return std::make_unique<StructInstanceExpr>(nameTok.lexeme, std::move(fields));
675}
676
677// Field access
678// point.x
679std::unique_ptr<Expression> Parser::fieldAccess(std::unique_ptr<Expression> object)
680{
681 Token nameTok = consume(TokenType::Identifier, "Expected field name after '.'");
682 return std::make_unique<FieldAccessExpr>(std::move(object), nameTok.lexeme);
683}
684
686{
687 return tokens[current];
688}
689
691{
692 if (current + 1 >= static_cast<int>(tokens.size()))
693 {
694 return tokens[current];
695 }
696 return tokens[current + 1];
697}
698
700{
701 return tokens[current - 1];
702}
703
705{
706 if (!isAtEnd())
707 current++;
708 return previous();
709}
710
712{
713 return peek().type == TokenType::EndOfFile;
714}
715
717{
718 if (isAtEnd())
719 return false;
720 return peek().type == type;
721}
722
724{
725 if (check(type))
726 {
727 advance();
728 return true;
729 }
730 return false;
731}
732
733Token Parser::consume(TokenType type, std::string message)
734{
735 if (check(type))
736 return advance();
737
738 std::cerr << "Error: " << message << " at '" << peek().lexeme << "'";
739 std::cerr << " (line " << peek().line << ", column " << peek().column << ")";
740 if (!currentFunction.empty())
741 std::cerr << " [in function '" << currentFunction << "']";
742 std::cerr << "\n";
743
744 throw std::runtime_error(message);
745}
746
747bool Parser::match(TokenType type, std::string lexeme)
748{
749 if (check(type) && peek().lexeme == lexeme)
750 {
751 advance();
752 return true;
753 }
754 return false;
755}
756
757Token Parser::consume(TokenType type, std::string lexeme, std::string message)
758{
759 if (check(type) && peek().lexeme == lexeme)
760 {
761 return advance();
762 }
763
764 std::cerr << "Error: " << message << " at '" << peek().lexeme << "'";
765 std::cerr << " (line " << peek().line << ", column " << peek().column << ")";
766 if (!currentFunction.empty())
767 std::cerr << " [in function '" << currentFunction << "']";
768 std::cerr << "\n";
769
770 throw std::runtime_error(message);
771}
772
773Token Parser::expect(TokenType type, const std::string &message)
774{
775 if (check(type))
776 {
777 return advance();
778 }
779 throw std::runtime_error(message);
780}
781} // namespace Phasor
std::unique_ptr< AST::Statement > varDeclaration()
Definition Parser.cpp:107
std::unique_ptr< AST::Statement > expressionStatement()
Definition Parser.cpp:344
std::unique_ptr< AST::Expression > expression()
Definition Parser.cpp:351
Token peekNext()
Definition Parser.cpp:690
std::unique_ptr< AST::Statement > ifStatement()
Definition Parser.cpp:177
std::unique_ptr< AST::Statement > printStatement()
Definition Parser.cpp:325
std::unique_ptr< AST::Statement > switchStatement()
Definition Parser.cpp:241
std::unique_ptr< AST::Expression > logicalAnd()
Definition Parser.cpp:383
Token advance()
Definition Parser.cpp:704
std::unique_ptr< AST::Expression > primary()
Definition Parser.cpp:564
std::unique_ptr< AST::Statement > statement()
Definition Parser.cpp:119
std::unique_ptr< AST::BlockStmt > block()
Definition Parser.cpp:312
std::unique_ptr< AST::StructDecl > structDecl()
Definition Parser.cpp:621
std::unique_ptr< AST::Expression > fieldAccess(std::unique_ptr< AST::Expression > object)
Definition Parser.cpp:679
std::vector< Token > tokens
Definition Parser.hpp:18
std::unique_ptr< AST::Statement > returnStatement()
Definition Parser.cpp:295
Token previous()
Definition Parser.cpp:699
std::unique_ptr< AST::Program > parse()
Definition Parser.cpp:13
std::unique_ptr< AST::Statement > whileStatement()
Definition Parser.cpp:191
std::unique_ptr< AST::Expression > assignment()
Definition Parser.cpp:356
bool match(TokenType type)
Definition Parser.cpp:723
std::unique_ptr< AST::Expression > call()
Definition Parser.cpp:498
std::unique_ptr< AST::Statement > declaration()
Definition Parser.cpp:23
std::string currentFunction
Definition Parser.hpp:20
Parser(const std::vector< Token > &tokens)
Definition Parser.cpp:9
std::unique_ptr< AST::Statement > importStatement()
Definition Parser.cpp:332
std::unique_ptr< AST::Expression > term()
Definition Parser.cpp:437
Token expect(TokenType type, const std::string &message)
Definition Parser.cpp:773
std::unique_ptr< AST::Statement > unsafeStatement()
Definition Parser.cpp:306
std::unique_ptr< AST::Expression > logicalOr()
Definition Parser.cpp:369
std::unique_ptr< AST::Statement > exportStatement()
Definition Parser.cpp:339
bool check(TokenType type)
Definition Parser.cpp:716
std::unique_ptr< AST::Statement > functionDeclaration()
Definition Parser.cpp:52
std::unique_ptr< AST::Statement > forStatement()
Definition Parser.cpp:200
std::unique_ptr< AST::Expression > equality()
Definition Parser.cpp:397
bool isAtEnd()
Definition Parser.cpp:711
std::unique_ptr< AST::Expression > comparison()
Definition Parser.cpp:412
std::unique_ptr< AST::TypeNode > parseType()
Definition Parser.cpp:89
std::unique_ptr< AST::StructInstanceExpr > structInstance()
Definition Parser.cpp:651
std::unique_ptr< AST::Expression > unary()
Definition Parser.cpp:474
Token consume(TokenType type, std::string message)
Definition Parser.cpp:733
std::unique_ptr< AST::Expression > finishCall(std::unique_ptr< AST::Expression > callee)
Definition Parser.cpp:535
std::unique_ptr< AST::Expression > factor()
Definition Parser.cpp:452
Token peek()
Definition Parser.cpp:685
Abstract Syntax Tree (AST) namespace.
Definition AST.hpp:11
BinaryOp
Binary operator types.
Definition AST.hpp:140
UnaryOp
Unary operator types.
Definition AST.hpp:131
The Phasor Programming Language and Runtime.
Definition AST.hpp:8
TokenType
Definition Lexer.hpp:10
Field Access Expression Node.
Definition AST.hpp:670
Identifier Expression Node.
Definition AST.hpp:96
size_t line
Definition Lexer.hpp:24
std::string lexeme
Definition Lexer.hpp:23
TokenType type
Definition Lexer.hpp:22
size_t column
Definition Lexer.hpp:25