refactor(parser): change body_h to body result

Refs: #1
This commit is contained in:
2026-06-03 10:45:57 +02:00
parent 6ce2fba9d2
commit 26340c279d
7 changed files with 45 additions and 20 deletions
+5 -5
View File
@@ -156,13 +156,13 @@ void function_definition_node::accept(visitor& visitor) const {
std::ostream& function_definition_node::print(std::ostream& os) const {
function_declaration_node::print(os);
os << ':';
if (m_body.present()) {
os << ":\n";
if (m_body.has_value()) {
for (const auto& entry : m_body->statements)
os << '\n' << entry;
return os << '\n' << m_body->end << ": " << p_name->string << " end";
os << entry << '\n';
return os << m_body->end << ": " << p_name->string << " end";
}
return os << m_body.error(); // error
return os << m_body.error().location << ": ERROR: unknown";
}
bool function_definition_node::equal(const node& rhs) const {
+1 -1
View File
@@ -18,7 +18,7 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
push_block();
if (funcDef.body().has_error()) {
std::cerr << funcDef.body().error() << '\n';
std::cerr << funcDef.body().error().location << ": ERROR: unknown\n";
return;
}
for (const auto& stmt : funcDef.body()->statements) {
+6 -6
View File
@@ -59,8 +59,8 @@ ast::declaration_node_h parser::parse_declaration() {
if (peek.has_error()) return error_handle(peek);
switch (peek->type) {
case token_t::LBrace: {
ast::body_h body = parse_body();
if (body.has_error()) return body;
ast::body_r body = parse_body();
if (body.has_error()) return ast::node_handle<ast::node>(body.error().location, "Body error");
return ast::function_definition_node_h{ first->location, m_arena, *name, std::move(body) };
}
case token_t::Semicolon: {
@@ -367,11 +367,11 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
return lhs;
}
ast::body_h parser::parse_body() {
ast::body_r parser::parse_body() {
ast::body body;
auto begin = eat_token(token_t::LBrace);
if (begin.has_error()) return error_handle(begin);
if (begin.has_error()) return ast::body_r(ast::error{ begin.error().location });
body.begin = begin->location;
while (!peek_token().has_error() && peek_token()->type != token_t::None && peek_token()->type != token_t::RBrace) {
@@ -379,10 +379,10 @@ ast::body_h parser::parse_body() {
}
auto end = eat_token(token_t::RBrace);
if (end.has_error()) return error_handle(end);
if (end.has_error()) return ast::body_r(ast::error{ end.error().location });
body.end = end->location;
return { begin->location, body };
return body;
}
ast::node_handle<ast::node> parser::error_handle(const token_r& result) {