11std::unique_ptr<Program> Parser::parse()
13 auto program = std::make_unique<Program>();
16 program->statements.push_back(declaration());
21std::unique_ptr<Statement> Parser::declaration()
26 return functionDeclaration();
31 return varDeclaration();
36std::unique_ptr<Statement> Parser::functionDeclaration()
40 std::vector<FunctionDecl::Param> params;
47 auto type = parseType();
48 params.push_back({paramName.lexeme, std::move(type)});
53 std::unique_ptr<TypeNode> returnType =
nullptr;
56 returnType = parseType();
62 std::string previousFunction = currentFunction;
63 currentFunction = name.lexeme;
68 currentFunction = previousFunction;
70 return std::make_unique<FunctionDecl>(name.lexeme, std::move(params), std::move(returnType), std::move(body));
73std::unique_ptr<TypeNode> Parser::parseType()
75 bool isPointer =
false;
82 std::vector<int> dims;
86 dims.push_back(std::stoi(size.lexeme));
89 return std::make_unique<TypeNode>(typeName.lexeme, isPointer, dims);
92std::unique_ptr<Statement> Parser::varDeclaration()
96 std::unique_ptr<Expression> initializer =
nullptr;
97 initializer = expression();
98 return std::make_unique<VarDecl>(name.lexeme, std::move(initializer));
101std::unique_ptr<Statement> Parser::statement()
106 return printStatement();
111 return ifStatement();
116 return whileStatement();
121 return returnStatement();
126 return std::make_unique<BreakStmt>();
131 return std::make_unique<ContinueStmt>();
139 return expressionStatement();
142std::unique_ptr<Statement> Parser::ifStatement()
145 auto condition = expression();
147 auto thenBranch = statement();
148 std::unique_ptr<Statement> elseBranch =
nullptr;
151 elseBranch = statement();
153 return std::make_unique<IfStmt>(std::move(condition), std::move(thenBranch), std::move(elseBranch));
156std::unique_ptr<Statement> Parser::whileStatement()
159 auto condition = expression();
161 auto body = statement();
162 return std::make_unique<WhileStmt>(std::move(condition), std::move(body));
165std::unique_ptr<Statement> Parser::forStatement()
169 std::unique_ptr<Statement> initializer =
nullptr;
173 initializer = varDeclaration();
177 initializer = expressionStatement();
180 std::unique_ptr<Expression> condition =
nullptr;
181 condition = expression();
183 std::unique_ptr<Expression> increment =
nullptr;
186 increment = expression();
190 auto body = statement();
191 return std::make_unique<ForStmt>(std::move(initializer), std::move(condition), std::move(increment),
195std::unique_ptr<Statement> Parser::switchStatement()
198 auto expr = expression();
202 std::vector<CaseClause> cases;
203 std::vector<std::unique_ptr<Statement>> defaultStmts;
209 throw std::runtime_error(
"Unterminated switch statement.");
215 auto caseValue = expression();
218 std::vector<std::unique_ptr<Statement>> stmts;
224 throw std::runtime_error(
"Unterminated case clause.");
226 stmts.push_back(declaration());
228 cases.emplace_back(std::move(caseValue), std::move(stmts));
240 throw std::runtime_error(
"Unterminated default clause.");
242 defaultStmts.push_back(declaration());
247 throw std::runtime_error(
"Expected 'case' or 'default' in switch statement.");
252 return std::make_unique<SwitchStmt>(std::move(expr), std::move(cases), std::move(defaultStmts));
255std::unique_ptr<Statement> Parser::returnStatement()
257 std::unique_ptr<Expression> value =
nullptr;
258 value = expression();
259 return std::make_unique<ReturnStmt>(std::move(value));
262std::unique_ptr<BlockStmt> Parser::block()
264 std::vector<std::unique_ptr<Statement>> statements;
269 throw std::runtime_error(
"Unterminated block.");
271 statements.push_back(declaration());
274 return std::make_unique<BlockStmt>(std::move(statements));
277std::unique_ptr<Statement> Parser::printStatement()
279 auto expr = expression();
280 return std::make_unique<PrintStmt>(std::move(expr));
283std::unique_ptr<Statement> Parser::expressionStatement()
285 auto expr = expression();
286 return std::make_unique<ExpressionStmt>(std::move(expr));
289std::unique_ptr<Expression> Parser::expression()
294std::unique_ptr<Expression> Parser::assignment()
296 auto expr = logicalOr();
300 auto value = assignment();
301 return std::make_unique<AssignmentExpr>(std::move(expr), std::move(value));
307std::unique_ptr<Expression> Parser::logicalOr()
309 auto expr = logicalAnd();
314 auto right = logicalAnd();
315 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::Or, std::move(right));
321std::unique_ptr<Expression> Parser::logicalAnd()
323 auto expr = equality();
328 auto right = equality();
329 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::And, std::move(right));
335std::unique_ptr<Expression> Parser::equality()
337 auto expr = comparison();
341 Token op = advance();
342 auto right = comparison();
344 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
350std::unique_ptr<Expression> Parser::comparison()
355 (peek().lexeme ==
"<" || peek().lexeme ==
">" || peek().lexeme ==
"<=" || peek().lexeme ==
">="))
357 Token op = advance();
360 if (op.lexeme ==
"<")
362 binOp = BinaryOp::LessThan;
364 else if (op.lexeme ==
">")
366 binOp = BinaryOp::GreaterThan;
368 else if (op.lexeme ==
"<=")
370 binOp = BinaryOp::LessEqual;
374 binOp = BinaryOp::GreaterEqual;
377 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
383std::unique_ptr<Expression> Parser::term()
385 auto expr = factor();
389 Token op = advance();
390 auto right = factor();
392 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
398std::unique_ptr<Expression> Parser::factor()
404 Token op = advance();
405 auto right = unary();
407 if (op.lexeme ==
"*")
409 binOp = BinaryOp::Multiply;
411 else if (op.lexeme ==
"/")
413 binOp = BinaryOp::Divide;
417 binOp = BinaryOp::Modulo;
420 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
426std::unique_ptr<Expression> Parser::unary()
430 Token op = advance();
431 auto right = unary();
433 return std::make_unique<UnaryExpr>(uOp, std::move(right));
438 auto right = unary();
439 return std::make_unique<UnaryExpr>(UnaryOp::AddressOf, std::move(right));
444 auto right = unary();
445 return std::make_unique<UnaryExpr>(UnaryOp::Dereference, std::move(right));
450std::unique_ptr<Expression> Parser::call()
452 auto expr = primary();
458 expr = finishCall(std::move(expr));
462 expr = std::make_unique<PostfixExpr>(PostfixOp::Increment, std::move(expr));
466 expr = std::make_unique<PostfixExpr>(PostfixOp::Decrement, std::move(expr));
477std::unique_ptr<Expression> Parser::finishCall(std::unique_ptr<Expression> callee)
479 std::vector<std::unique_ptr<Expression>> arguments;
484 arguments.push_back(expression());
491 if (
auto *ident =
dynamic_cast<IdentifierExpr *
>(callee.get()))
493 return std::make_unique<CallExpr>(ident->name, std::move(arguments));
495 if (
auto field =
dynamic_cast<FieldAccessExpr *
>(callee.get()))
498 std::string methodName = field->fieldName;
499 arguments.insert(arguments.begin(), std::move(field->object));
500 return std::make_unique<CallExpr>(methodName, std::move(arguments));
503 throw std::runtime_error(
"Can only call named functions.");
506std::unique_ptr<Expression> Parser::primary()
510 return std::make_unique<NumberExpr>(previous().lexeme);
514 return std::make_unique<StringExpr>(previous().lexeme);
518 Token identTok = peek();
520 return std::make_unique<IdentifierExpr>(identTok.lexeme);
524 return std::make_unique<BooleanExpr>(
true);
528 return std::make_unique<BooleanExpr>(
false);
532 return std::make_unique<NullExpr>();
536 auto expr = expression();
540 std::cerr <<
"Error: Expect expression at '" << peek().lexeme <<
"'";
541 std::cerr <<
" (line " << peek().line <<
", column " << peek().column <<
")\n";
542 throw std::runtime_error(
"Expect expression.");
547 return tokens[current];
550Token Parser::peekNext()
552 if (current + 1 >=
static_cast<int>(tokens.size()))
554 return tokens[current];
556 return tokens[current + 1];
559Token Parser::previous()
561 return tokens[current - 1];
564Token Parser::advance()
573bool Parser::isAtEnd()
578bool Parser::check(TokenType type)
584 return peek().type == type;
587bool Parser::match(TokenType type)
597Token Parser::consume(TokenType type,
const std::string &message)
604 std::cerr <<
"Error: " << message <<
" at '" << peek().lexeme <<
"'";
605 std::cerr <<
" (line " << peek().line <<
", column " << peek().column <<
")";
606 if (!currentFunction.empty())
608 std::cerr <<
" [in function '" << currentFunction <<
"']";
612 throw std::runtime_error(message);
615bool Parser::match(TokenType type,
const std::string &lexeme)
617 if (check(type) && peek().lexeme == lexeme)
625Token Parser::consume(TokenType type,
const std::string &lexeme,
const std::string &message)
627 if (check(type) && peek().lexeme == lexeme)
632 std::cerr <<
"Error: " << message <<
" at '" << peek().lexeme <<
"'";
633 std::cerr <<
" (line " << peek().line <<
", column " << peek().column <<
")";
634 if (!currentFunction.empty())
636 std::cerr <<
" [in function '" << currentFunction <<
"']";
640 throw std::runtime_error(message);
643Token Parser::expect(TokenType type,
const std::string &message)
649 throw std::runtime_error(message);
Parser(const std::vector< Token > &tokens)
UnaryOp
Unary operator types.
BinaryOp
Binary operator types.
The Pulsar Scripting Language.