refactor(AST): node location

Add location function to node and introduce a new abstract_node class.

Refs: #1
This commit is contained in:
2026-06-03 13:47:01 +02:00
parent 9b92c06d87
commit 52537e0955
8 changed files with 162 additions and 67 deletions
+22 -15
View File
@@ -31,7 +31,7 @@ parser::parser(std::string_view filename)
}
ast::program_node_r parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>();
auto program = m_arena.allocate_shared<ast::program_node>(location{ m_filename });
while (peek_token().has_value()) {
program->push(std::move(parse_declaration()));
@@ -61,11 +61,11 @@ ast::declaration_node_r parser::parse_declaration() {
case token_t::LBrace: {
ast::body_r body = parse_body();
if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::function_definition_node>(*name, std::move(body));
return m_arena.allocate_shared<ast::function_definition_node>(first->location, *name, std::move(body));
}
case token_t::Semicolon: {
m_peekBuffer.clear();
return m_arena.allocate_shared<ast::function_declaration_node>(*name);
return m_arena.allocate_shared<ast::function_declaration_node>(first->location, *name);
}
default: return ast::declaration_node_r(ast::error{ tok->location });
}
@@ -101,13 +101,13 @@ ast::statement_node_r parser::parse_statement() {
auto tok = next_token();
if (peek_token()->type == token_t::Semicolon) {
next_token();
return m_arena.allocate_shared<ast::return_statement_node>();
return m_arena.allocate_shared<ast::return_statement_node>(location);
}
auto value = parse_expression();
auto err = eat_token(token_t::Semicolon);
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
return m_arena.allocate_shared<ast::return_statement_node>(std::move(value));
return m_arena.allocate_shared<ast::return_statement_node>(location, std::move(value));
}
case keyword_token::If: {
auto tok = next_token();
@@ -126,19 +126,20 @@ ast::statement_node_r parser::parse_statement() {
peek_token()->value.keyword == keyword_token::Else) {
next_token();
return m_arena.allocate_shared<ast::if_statement_node>(std::move(cond),
return m_arena.allocate_shared<ast::if_statement_node>(location,
std::move(cond),
std::move(then),
std::move(parse_statement()));
}
return m_arena.allocate_shared<ast::if_statement_node>(std::move(cond), std::move(then));
return m_arena.allocate_shared<ast::if_statement_node>(location, std::move(cond), std::move(then));
}
case keyword_token::None:
case keyword_token::Func:
default: break;
}
}
case token_t::LBrace: return m_arena.allocate_shared<ast::compound_statement_node>(parse_body());
case token_t::LBrace: return m_arena.allocate_shared<ast::compound_statement_node>(location, parse_body());
default: break;
}
@@ -165,12 +166,12 @@ ast::literal_node_r parser::parse_literal() {
case token_t::String: {
auto tok = next_token();
if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::string_literal_node>((*tok)->string);
return m_arena.allocate_shared<ast::string_literal_node>(tok->location, (*tok)->string);
}
case token_t::Integer: {
auto tok = next_token();
if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::integer_literal_node>((*tok)->integer);
return m_arena.allocate_shared<ast::integer_literal_node>(tok->location, (*tok)->integer);
}
default: break;
}
@@ -184,7 +185,7 @@ ast::expression_node_r parser::parse_expression_primary() {
case token_t::Identifier: {
auto tok = next_token();
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::var_read_expression_node>((*tok)->string);
return m_arena.allocate_shared<ast::var_read_expression_node>(tok->location, (*tok)->string);
}
case token_t::LParen: {
auto tok = next_token();
@@ -230,7 +231,8 @@ ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence)
expression = parse_expression_unary(current.precedence + 1);
}
result = m_arena.allocate_shared<ast::unaryop_expression_node>(current.type, std::move(expression));
result =
m_arena.allocate_shared<ast::unaryop_expression_node>(token->location, current.type, std::move(expression));
}
if (result == nullptr) return parse_expression_primary();
@@ -341,13 +343,18 @@ ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& ini
switch (current.type) {
case rhsop_info_t::Unaryop:
lhs = m_arena.allocate_shared<ast::unaryop_expression_node>(current.unary, std::move(lhs));
lhs =
m_arena.allocate_shared<ast::unaryop_expression_node>(opToken->location, current.unary, std::move(lhs));
break;
case rhsop_info_t::Binop:
lhs = m_arena.allocate_shared<ast::binop_expression_node>(current.binary, std::move(lhs), std::move(rhs));
lhs = m_arena.allocate_shared<ast::binop_expression_node>(opToken->location,
current.binary,
std::move(lhs),
std::move(rhs));
break;
case rhsop_info_t::Assignment:
lhs = m_arena.allocate_shared<ast::var_assign_expression_node>(current.assignment,
lhs = m_arena.allocate_shared<ast::var_assign_expression_node>(opToken->location,
current.assignment,
std::move(lhs),
std::move(rhs));
break;