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;
208 throw std::runtime_error(
"Unterminated switch statement.");
213 auto caseValue = expression();
216 std::vector<std::unique_ptr<Statement>> stmts;
221 throw std::runtime_error(
"Unterminated case clause.");
222 stmts.push_back(declaration());
224 cases.emplace_back(std::move(caseValue), std::move(stmts));
235 throw std::runtime_error(
"Unterminated default clause.");
236 defaultStmts.push_back(declaration());
241 throw std::runtime_error(
"Expected 'case' or 'default' in switch statement.");
246 return std::make_unique<SwitchStmt>(std::move(expr), std::move(cases), std::move(defaultStmts));
249std::unique_ptr<Statement> Parser::returnStatement()
251 std::unique_ptr<Expression> value =
nullptr;
252 value = expression();
253 return std::make_unique<ReturnStmt>(std::move(value));
256std::unique_ptr<BlockStmt> Parser::block()
258 std::vector<std::unique_ptr<Statement>> statements;
262 throw std::runtime_error(
"Unterminated block.");
263 statements.push_back(declaration());
266 return std::make_unique<BlockStmt>(std::move(statements));
269std::unique_ptr<Statement> Parser::printStatement()
271 auto expr = expression();
272 return std::make_unique<PrintStmt>(std::move(expr));
275std::unique_ptr<Statement> Parser::expressionStatement()
277 auto expr = expression();
278 return std::make_unique<ExpressionStmt>(std::move(expr));
281std::unique_ptr<Expression> Parser::expression()
286std::unique_ptr<Expression> Parser::assignment()
288 auto expr = logicalOr();
292 auto value = assignment();
293 return std::make_unique<AssignmentExpr>(std::move(expr), std::move(value));
299std::unique_ptr<Expression> Parser::logicalOr()
301 auto expr = logicalAnd();
306 auto right = logicalAnd();
307 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::Or, std::move(right));
313std::unique_ptr<Expression> Parser::logicalAnd()
315 auto expr = equality();
320 auto right = equality();
321 expr = std::make_unique<BinaryExpr>(std::move(expr), BinaryOp::And, std::move(right));
327std::unique_ptr<Expression> Parser::equality()
329 auto expr = comparison();
333 Token op = advance();
334 auto right = comparison();
336 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
342std::unique_ptr<Expression> Parser::comparison()
347 (peek().lexeme ==
"<" || peek().lexeme ==
">" || peek().lexeme ==
"<=" || peek().lexeme ==
">="))
349 Token op = advance();
352 if (op.lexeme ==
"<")
353 binOp = BinaryOp::LessThan;
354 else if (op.lexeme ==
">")
355 binOp = BinaryOp::GreaterThan;
356 else if (op.lexeme ==
"<=")
357 binOp = BinaryOp::LessEqual;
359 binOp = BinaryOp::GreaterEqual;
361 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
367std::unique_ptr<Expression> Parser::term()
369 auto expr = factor();
373 Token op = advance();
374 auto right = factor();
376 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
382std::unique_ptr<Expression> Parser::factor()
388 Token op = advance();
389 auto right = unary();
391 if (op.lexeme ==
"*")
392 binOp = BinaryOp::Multiply;
393 else if (op.lexeme ==
"/")
394 binOp = BinaryOp::Divide;
396 binOp = BinaryOp::Modulo;
398 expr = std::make_unique<BinaryExpr>(std::move(expr), binOp, std::move(right));
404std::unique_ptr<Expression> Parser::unary()
408 Token op = advance();
409 auto right = unary();
411 return std::make_unique<UnaryExpr>(uOp, std::move(right));
416 auto right = unary();
417 return std::make_unique<UnaryExpr>(UnaryOp::AddressOf, std::move(right));
422 auto right = unary();
423 return std::make_unique<UnaryExpr>(UnaryOp::Dereference, std::move(right));
428std::unique_ptr<Expression> Parser::call()
430 auto expr = primary();
436 expr = finishCall(std::move(expr));
440 expr = std::make_unique<PostfixExpr>(PostfixOp::Increment, std::move(expr));
444 expr = std::make_unique<PostfixExpr>(PostfixOp::Decrement, std::move(expr));
455std::unique_ptr<Expression> Parser::finishCall(std::unique_ptr<Expression> callee)
457 std::vector<std::unique_ptr<Expression>> arguments;
462 arguments.push_back(expression());
469 if (
auto ident =
dynamic_cast<IdentifierExpr *
>(callee.get()))
471 return std::make_unique<CallExpr>(ident->name, std::move(arguments));
473 else if (
auto field =
dynamic_cast<FieldAccessExpr *
>(callee.get()))
476 std::string methodName = field->fieldName;
477 arguments.insert(arguments.begin(), std::move(field->object));
478 return std::make_unique<CallExpr>(methodName, std::move(arguments));
481 throw std::runtime_error(
"Can only call named functions.");
484std::unique_ptr<Expression> Parser::primary()
488 return std::make_unique<NumberExpr>(previous().lexeme);
492 return std::make_unique<StringExpr>(previous().lexeme);
496 Token identTok = peek();
498 return std::make_unique<IdentifierExpr>(identTok.lexeme);
502 return std::make_unique<BooleanExpr>(
true);
506 return std::make_unique<BooleanExpr>(
false);
510 return std::make_unique<NullExpr>();
514 auto expr = expression();
518 std::cerr <<
"Error: Expect expression at '" << peek().lexeme <<
"'";
519 std::cerr <<
" (line " << peek().line <<
", column " << peek().column <<
")\n";
520 throw std::runtime_error(
"Expect expression.");
525 return tokens[current];
528Token Parser::peekNext()
530 if (current + 1 >=
static_cast<int>(tokens.size()))
532 return tokens[current];
534 return tokens[current + 1];
537Token Parser::previous()
539 return tokens[current - 1];
542Token Parser::advance()
549bool Parser::isAtEnd()
554bool Parser::check(TokenType type)
558 return peek().type == type;
561bool Parser::match(TokenType type)
571Token Parser::consume(TokenType type, std::string message)
576 std::cerr <<
"Error: " << message <<
" at '" << peek().lexeme <<
"'";
577 std::cerr <<
" (line " << peek().line <<
", column " << peek().column <<
")";
578 if (!currentFunction.empty())
579 std::cerr <<
" [in function '" << currentFunction <<
"']";
582 throw std::runtime_error(message);
585bool Parser::match(TokenType type, std::string lexeme)
587 if (check(type) && peek().lexeme == lexeme)
595Token Parser::consume(TokenType type, std::string lexeme, std::string message)
597 if (check(type) && peek().lexeme == lexeme)
602 std::cerr <<
"Error: " << message <<
" at '" << peek().lexeme <<
"'";
603 std::cerr <<
" (line " << peek().line <<
", column " << peek().column <<
")";
604 if (!currentFunction.empty())
605 std::cerr <<
" [in function '" << currentFunction <<
"']";
608 throw std::runtime_error(message);
611Token Parser::expect(TokenType type,
const std::string &message)
617 throw std::runtime_error(message);
Parser(const std::vector< Token > &tokens)
UnaryOp
Unary operator types.
BinaryOp
Binary operator types.
The Pulsar Scripting Language.