15 auto program = std::make_unique<Program>();
56 std::vector<FunctionDecl::Param> params;
64 params.push_back({paramName.
lexeme, std::move(type)});
69 std::unique_ptr<TypeNode> returnType =
nullptr;
86 return std::make_unique<FunctionDecl>(name.
lexeme, std::move(params), std::move(returnType), std::move(body));
91 bool isPointer =
false;
98 std::vector<int> dims;
101 dims.push_back(std::stoi(size.
lexeme));
104 return std::make_unique<TypeNode>(typeName.
lexeme, isPointer, dims);
110 std::unique_ptr<Expression> initializer =
nullptr;
116 return std::make_unique<VarDecl>(name.
lexeme, std::move(initializer));
155 return std::make_unique<BreakStmt>();
161 return std::make_unique<ContinueStmt>();
183 std::unique_ptr<Statement> elseBranch =
nullptr;
188 return std::make_unique<IfStmt>(std::move(condition), std::move(thenBranch), std::move(elseBranch));
197 return std::make_unique<WhileStmt>(std::move(condition), std::move(body));
204 std::unique_ptr<Statement> initializer =
nullptr;
222 std::unique_ptr<Expression> condition =
nullptr;
229 std::unique_ptr<Expression> increment =
nullptr;
237 return std::make_unique<ForStmt>(std::move(initializer), std::move(condition), std::move(increment),
248 std::vector<CaseClause> cases;
249 std::vector<std::unique_ptr<Statement>> defaultStmts;
254 throw std::runtime_error(
"Unterminated switch statement.");
262 std::vector<std::unique_ptr<Statement>> stmts;
267 throw std::runtime_error(
"Unterminated case clause.");
270 cases.emplace_back(std::move(caseValue), std::move(stmts));
281 throw std::runtime_error(
"Unterminated default clause.");
287 throw std::runtime_error(
"Expected 'case' or 'default' in switch statement.");
292 return std::make_unique<SwitchStmt>(std::move(expr), std::move(cases), std::move(defaultStmts));
297 std::unique_ptr<Expression> value =
nullptr;
303 return std::make_unique<ReturnStmt>(std::move(value));
309 return std::make_unique<UnsafeBlockStmt>(
block());
314 std::vector<std::unique_ptr<Statement>> statements;
318 throw std::runtime_error(
"Unterminated block.");
322 return std::make_unique<BlockStmt>(std::move(statements));
329 return std::make_unique<PrintStmt>(std::move(expr));
336 return std::make_unique<ImportStmt>(path.
lexeme);
341 return std::make_unique<ExportStmt>(
declaration());
348 return std::make_unique<ExpressionStmt>(std::move(expr));
363 return std::make_unique<AssignmentExpr>(std::move(expr), std::move(value));
377 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::Or, std::move(right));
391 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::And, std::move(right));
405 BinaryOp binOp = (op.
lexeme ==
"==") ? BinaryOp::Equal : BinaryOp::NotEqual;
406 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
417 (
peek().lexeme ==
"<" ||
peek().lexeme ==
">" ||
peek().lexeme ==
"<=" ||
peek().lexeme ==
">="))
423 binOp = BinaryOp::LessThan;
424 else if (op.
lexeme ==
">")
425 binOp = BinaryOp::GreaterThan;
426 else if (op.
lexeme ==
"<=")
427 binOp = BinaryOp::LessEqual;
429 binOp = BinaryOp::GreaterEqual;
431 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
445 BinaryOp binOp = (op.
lexeme ==
"+") ? BinaryOp::Add : BinaryOp::Subtract;
446 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
459 auto right =
unary();
462 binOp = BinaryOp::Multiply;
463 else if (op.
lexeme ==
"/")
464 binOp = BinaryOp::Divide;
466 binOp = BinaryOp::Modulo;
468 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
479 auto right =
unary();
480 UnaryOp uOp = (op.
lexeme ==
"!") ? UnaryOp::Not : UnaryOp::Negate;
481 return std::make_unique<UnaryExpr>(uOp, std::move(right));
486 auto right =
unary();
487 return std::make_unique<UnaryExpr>(UnaryOp::AddressOf, std::move(right));
492 auto right =
unary();
493 return std::make_unique<UnaryExpr>(UnaryOp::Dereference, std::move(right));
514 expr = std::make_unique<PostfixExpr>(PostfixOp::Increment, std::move(expr));
518 expr = std::make_unique<PostfixExpr>(PostfixOp::Decrement, std::move(expr));
524 expr = std::make_unique<ArrayAccessExpr>(std::move(expr), std::move(index));
537 std::vector<std::unique_ptr<Expression>> arguments;
551 return std::make_unique<CallExpr>(ident->name, std::move(arguments));
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));
561 throw std::runtime_error(
"Can only call named functions.");
568 return std::make_unique<NumberExpr>(
previous().lexeme);
572 return std::make_unique<StringExpr>(
previous().lexeme);
583 return std::make_unique<IdentifierExpr>(identTok.
lexeme);
587 std::vector<std::unique_ptr<Expression>> elements;
594 return std::make_unique<ArrayLiteralExpr>(std::move(elements));
598 return std::make_unique<BooleanExpr>(
true);
602 return std::make_unique<BooleanExpr>(
false);
606 return std::make_unique<NullExpr>();
614 std::cerr <<
"Error: Expect expression at '" <<
peek().
lexeme <<
"'";
616 throw std::runtime_error(
"Expect expression.");
628 std::vector<StructField> fields;
632 throw std::runtime_error(
"Unterminated struct declaration.");
637 fields.emplace_back(fieldNameTok.
lexeme, std::move(type));
646 return std::make_unique<StructDecl>(nameTok.
lexeme, std::move(fields));
656 std::vector<std::pair<std::string, std::unique_ptr<Expression>>> fields;
660 throw std::runtime_error(
"Unterminated struct instance.");
665 fields.emplace_back(fieldNameTok.
lexeme, std::move(value));
674 return std::make_unique<StructInstanceExpr>(nameTok.
lexeme, std::move(fields));
682 return std::make_unique<FieldAccessExpr>(std::move(
object), nameTok.
lexeme);
738 std::cerr <<
"Error: " << message <<
" at '" <<
peek().
lexeme <<
"'";
744 throw std::runtime_error(message);
749 if (
check(type) &&
peek().lexeme == lexeme)
759 if (
check(type) &&
peek().lexeme == lexeme)
764 std::cerr <<
"Error: " << message <<
" at '" <<
peek().
lexeme <<
"'";
770 throw std::runtime_error(message);
779 throw std::runtime_error(message);
std::unique_ptr< AST::Statement > varDeclaration()
std::unique_ptr< AST::Statement > expressionStatement()
std::unique_ptr< AST::Expression > expression()
std::unique_ptr< AST::Statement > ifStatement()
std::unique_ptr< AST::Statement > printStatement()
std::unique_ptr< AST::Statement > switchStatement()
std::unique_ptr< AST::Expression > logicalAnd()
std::unique_ptr< AST::Expression > primary()
std::unique_ptr< AST::Statement > statement()
std::unique_ptr< AST::BlockStmt > block()
std::unique_ptr< AST::StructDecl > structDecl()
std::unique_ptr< AST::Expression > fieldAccess(std::unique_ptr< AST::Expression > object)
std::vector< Token > tokens
std::unique_ptr< AST::Statement > returnStatement()
std::unique_ptr< AST::Program > parse()
std::unique_ptr< AST::Statement > whileStatement()
std::unique_ptr< AST::Expression > assignment()
bool match(TokenType type)
std::unique_ptr< AST::Expression > call()
std::unique_ptr< AST::Statement > declaration()
std::string currentFunction
Parser(const std::vector< Token > &tokens)
std::unique_ptr< AST::Statement > importStatement()
std::unique_ptr< AST::Expression > term()
Token expect(TokenType type, const std::string &message)
std::unique_ptr< AST::Statement > unsafeStatement()
std::unique_ptr< AST::Expression > logicalOr()
std::unique_ptr< AST::Statement > exportStatement()
bool check(TokenType type)
std::unique_ptr< AST::Statement > functionDeclaration()
std::unique_ptr< AST::Statement > forStatement()
std::unique_ptr< AST::Expression > equality()
std::unique_ptr< AST::Expression > comparison()
std::unique_ptr< AST::TypeNode > parseType()
std::unique_ptr< AST::StructInstanceExpr > structInstance()
std::unique_ptr< AST::Expression > unary()
Token consume(TokenType type, std::string message)
std::unique_ptr< AST::Expression > finishCall(std::unique_ptr< AST::Expression > callee)
std::unique_ptr< AST::Expression > factor()
Abstract Syntax Tree (AST) namespace.
BinaryOp
Binary operator types.
UnaryOp
Unary operator types.
The Phasor Programming Language and Runtime.
Field Access Expression Node.
Identifier Expression Node.