made the parsing functions for the different nodes

return the type they are parsing (necessary for
the parsing of the identifiers while parsing the
list of parameteres in a function literal)
This commit is contained in:
Karma Riuk
2025-07-15 00:24:53 +02:00
parent 586a172d90
commit 85f530f5f9
2 changed files with 21 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
#include "ast/errors/error.hpp" #include "ast/errors/error.hpp"
#include "ast/expressions/boolean.hpp" #include "ast/expressions/boolean.hpp"
#include "ast/expressions/function.hpp"
#include "ast/expressions/identifier.hpp" #include "ast/expressions/identifier.hpp"
#include "ast/expressions/if_then_else.hpp" #include "ast/expressions/if_then_else.hpp"
#include "ast/expressions/infix.hpp" #include "ast/expressions/infix.hpp"
@@ -225,16 +226,16 @@ namespace parser {
register_infix(type, fn); register_infix(type, fn);
}; };
ast::expression* parser::parse_identifier() { ast::identifier* parser::parse_identifier() {
return new ast::identifier(current, current.literal); return new ast::identifier(current, current.literal);
}; };
ast::expression* parser::parse_integer() { ast::integer_literal* parser::parse_integer() {
TRACE_FUNCTION; TRACE_FUNCTION;
return new ast::integer_literal(current, std::stoi(current.literal)); return new ast::integer_literal(current, std::stoi(current.literal));
}; };
ast::expression* parser::parse_boolean() { ast::boolean_literal* parser::parse_boolean() {
TRACE_FUNCTION; TRACE_FUNCTION;
return new ast::boolean_literal( return new ast::boolean_literal(
current, current,
@@ -242,7 +243,7 @@ namespace parser {
); );
}; };
ast::expression* parser::parse_prefix_expr() { ast::prefix_expr* parser::parse_prefix_expr() {
TRACE_FUNCTION; TRACE_FUNCTION;
ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal); ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal);
next_token(); next_token();
@@ -263,7 +264,7 @@ namespace parser {
return ret; return ret;
}; };
ast::expression* parser::parse_if_then_else() { ast::if_then_else* parser::parse_if_then_else() {
TRACE_FUNCTION; TRACE_FUNCTION;
ast::if_then_else* ret = new ast::if_then_else(current); ast::if_then_else* ret = new ast::if_then_else(current);
if (!expect_next(token::type::LPAREN)) { if (!expect_next(token::type::LPAREN)) {
@@ -320,7 +321,7 @@ namespace parser {
return ret; return ret;
} }
ast::expression* parser::parse_infix_expr(ast::expression* left) { ast::infix_expr* parser::parse_infix_expr(ast::expression* left) {
TRACE_FUNCTION; TRACE_FUNCTION;
ast::infix_expr* ret = ast::infix_expr* ret =
new ast::infix_expr(current, current.literal, left); new ast::infix_expr(current, current.literal, left);

View File

@@ -2,6 +2,13 @@
#include "ast/ast.hpp" #include "ast/ast.hpp"
#include "ast/errors/error.hpp" #include "ast/errors/error.hpp"
#include "ast/expressions/boolean.hpp"
#include "ast/expressions/function.hpp"
#include "ast/expressions/identifier.hpp"
#include "ast/expressions/if_then_else.hpp"
#include "ast/expressions/infix.hpp"
#include "ast/expressions/integer.hpp"
#include "ast/expressions/prefix.hpp"
#include "ast/program.hpp" #include "ast/program.hpp"
#include "ast/statements/block.hpp" #include "ast/statements/block.hpp"
#include "ast/statements/expression.hpp" #include "ast/statements/expression.hpp"
@@ -50,14 +57,15 @@ namespace parser {
void register_infix(token::type, infix_parse_fn); void register_infix(token::type, infix_parse_fn);
void register_infix(std::vector<token::type>, infix_parse_fn); void register_infix(std::vector<token::type>, infix_parse_fn);
ast::expression* parse_identifier(); ast::identifier* parse_identifier();
ast::expression* parse_integer(); ast::integer_literal* parse_integer();
ast::expression* parse_boolean(); ast::boolean_literal* parse_boolean();
ast::expression* parse_prefix_expr(); ast::function_literal* parse_function();
ast::prefix_expr* parse_prefix_expr();
ast::expression* parse_grouped_expr(); ast::expression* parse_grouped_expr();
ast::expression* parse_if_then_else(); ast::if_then_else* parse_if_then_else();
ast::block_stmt* parse_block(); ast::block_stmt* parse_block();
ast::expression* parse_infix_expr(ast::expression*); ast::infix_expr* parse_infix_expr(ast::expression*);
}; };
} // namespace parser } // namespace parser