refactor(AST): use pointers instead of results inside of AST nodes

This commit is contained in:
2026-06-03 18:19:20 +02:00
parent 3006b58212
commit 2cfbc12977
8 changed files with 129 additions and 117 deletions
+6 -14
View File
@@ -21,8 +21,7 @@ void var_read_expression_node::accept(visitor& visitor) const {
}
std::ostream& var_read_expression_node::print(std::ostream& os) const {
if (m_name.has_error()) return os << m_name.error();
return os << *m_name;
return os << m_name;
}
bool var_read_expression_node::equal(const node& rhsNode) const {
@@ -47,7 +46,7 @@ void unaryop_expression_node::accept(visitor& visitor) const {
}
std::ostream& unaryop_expression_node::print(std::ostream& os) const {
if (!m_node.has_value()) return os;
if (m_node == nullptr) return os;
switch (m_type) {
case unaryop_expression_node_t::Positive:
case unaryop_expression_node_t::Negative:
@@ -132,12 +131,9 @@ void function_definition_node::accept(visitor& visitor) const {
std::ostream& function_definition_node::print(std::ostream& os) const {
function_declaration_node::print(os);
os << ":\n";
if (m_body.has_value()) {
for (const auto& entry : m_body->statements)
os << entry << '\n';
return os << m_body->end << ": " << p_name << " end";
}
return os << m_body.error();
for (const auto& entry : m_body.statements)
os << entry << '\n';
return os << m_body.end << ": " << p_name << " end";
}
bool function_definition_node::equal(const node& rhs) const {
@@ -192,11 +188,7 @@ bool compound_statement_node::equal(const node& rhs) const {
void program_node::accept(visitor& visitor) const {
for (const auto& decl : m_declarations) {
if (decl.has_error()) {
visitor.visit_error(decl.error());
} else {
decl.value()->accept(visitor);
}
decl->accept(visitor);
}
}
+16 -27
View File
@@ -6,7 +6,6 @@
#include "furc/ast/statement.hpp" // IWYU pragma: keep
#include <cassert>
#include <iostream>
namespace furc::front {
@@ -18,28 +17,18 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()));
push_block();
if (funcDef.body().has_error()) {
std::cerr << funcDef.body().error() << '\n';
return;
}
for (const auto& stmt : funcDef.body()->statements) {
for (const auto& stmt : funcDef.body().statements) {
stmt.value()->accept(*this);
}
m_currentBlock->emplace<ir::return_instruction>();
m_module.push(std::move(m_currentFunction));
}
void ir_generator::visit(const ast::return_statement_node& returnStmt) {
if (!returnStmt.value().has_value()) return;
auto value = returnStmt.value().value();
if (value.has_error()) {
std::cerr << value.error() << '\n';
return;
}
if (value.has_value()) {
value.value()->accept(*this);
if (returnStmt.value().has_value()) {
returnStmt.value().value()->accept(*this);
push<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
} else {
push<ir::return_instruction>();
@@ -47,19 +36,19 @@ void ir_generator::visit(const ast::return_statement_node& returnStmt) {
}
void ir_generator::visit(const ast::if_statement_node& node) {
node.cond().value()->accept(*this);
node.cond()->accept(*this);
ir_register cond = m_registerCounter - 1;
push<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
m_currentFunction->blocks().size(),
m_currentFunction->blocks().size() + 1);
push_block(); // then block
node.then().value()->accept(*this);
node.then()->accept(*this);
if (node.elze().has_value()) {
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
push_block(); // else block
node.elze().value().value()->accept(*this);
node.elze().value()->accept(*this);
}
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size());
@@ -67,7 +56,7 @@ void ir_generator::visit(const ast::if_statement_node& node) {
}
void ir_generator::visit(const ast::compound_statement_node& node) {
for (const auto& stmt : node.body()->statements) {
for (const auto& stmt : node.body().statements) {
stmt.value()->accept(*this);
}
}
@@ -83,7 +72,7 @@ void ir_generator::visit(const ast::integer_literal_node& node) {
}
void ir_generator::visit(const ast::var_read_expression_node& node) {
if (auto it = m_variables.find(*node.get_name()); it != m_variables.end()) {
if (auto it = m_variables.find(node.get_name()); it != m_variables.end()) {
push<furlang::ir::assign_instruction>(ir::operand::new_reg(it->second),
ir::operand::new_reg(m_registerCounter++));
} else {
@@ -114,9 +103,9 @@ static inline furlang::ir::binary_op_instruction_t binary_op_instruction_t(ast::
}
void ir_generator::visit(const ast::binop_expression_node& node) {
node.lhs().value()->accept(*this);
node.lhs()->accept(*this);
ir_register lhs = m_registerCounter - 1;
node.rhs().value()->accept(*this);
node.rhs()->accept(*this);
ir_register rhs = m_registerCounter - 1;
ir_register dst = m_registerCounter++;
push<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
@@ -126,16 +115,16 @@ void ir_generator::visit(const ast::binop_expression_node& node) {
}
void ir_generator::visit(const ast::var_assign_expression_node& node) {
node.rhs().value()->accept(*this);
node.rhs()->accept(*this);
ir_register rhs = m_registerCounter - 1;
assert(node.lhs().value()->expression_type() == ast::expression_node_t::VarRead);
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs().value());
assert(node.lhs()->expression_type() == ast::expression_node_t::VarRead);
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs());
ir_register reg = 0;
if (auto it = m_variables.find(*lhs->get_name()); it != m_variables.end()) {
if (auto it = m_variables.find(lhs->get_name()); it != m_variables.end()) {
reg = it->second;
} else {
m_variables[*lhs->get_name()] = reg = m_registerCounter++;
m_variables[lhs->get_name()] = reg = m_registerCounter++;
}
auto compound = node.compound();
+56 -21
View File
@@ -34,10 +34,15 @@ ast::program_node_r parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>(location{ m_filename });
while (peek_token().has_value()) {
program->push(std::move(parse_declaration()));
auto decl = parse_declaration();
if (decl.has_error()) {
program = nullptr;
} else if (program != nullptr) {
program->push(std::move(decl.value()));
}
}
return std::move(program);
return program != nullptr ? std::move(program) : ast::program_node_r(ast::error{ location{ m_filename } });
}
ast::declaration_node_r parser::parse_declaration() {
@@ -63,7 +68,7 @@ ast::declaration_node_r parser::parse_declaration() {
if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::function_definition_node>(first->location,
name->value.string,
std::move(body));
std::move(body.value()));
}
case token_t::Semicolon: {
m_peekBuffer.clear();
@@ -109,7 +114,7 @@ ast::statement_node_r parser::parse_statement() {
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>(location, std::move(value));
return m_arena.allocate_shared<ast::return_statement_node>(location, std::move(value.value()));
}
case keyword_token::If: {
auto tok = next_token();
@@ -128,20 +133,28 @@ ast::statement_node_r parser::parse_statement() {
peek_token()->value.keyword == keyword_token::Else) {
next_token();
auto elseBody = parse_statement();
if (elseBody.has_error()) return ast::statement_node_r(ast::error{ elseBody.error().location });
return m_arena.allocate_shared<ast::if_statement_node>(location,
std::move(cond),
std::move(then),
std::move(parse_statement()));
std::move(cond.value()),
std::move(then.value()),
std::move(elseBody.value()));
}
return m_arena.allocate_shared<ast::if_statement_node>(location, std::move(cond), std::move(then));
return m_arena.allocate_shared<ast::if_statement_node>(location,
std::move(cond.value()),
std::move(then.value()));
}
case keyword_token::None:
case keyword_token::Func:
default: break;
}
}
case token_t::LBrace: return m_arena.allocate_shared<ast::compound_statement_node>(location, parse_body());
case token_t::LBrace: {
auto body = parse_body();
if (body.has_error()) return ast::statement_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::compound_statement_node>(location, std::move(body.value()));
}
default: break;
}
@@ -159,7 +172,11 @@ ast::statement_node_r parser::parse_statement() {
}
ast::expression_node_r parser::parse_expression(std::uint32_t precedence) {
return parse_expression_rhs(parse_expression_unary(precedence), precedence);
auto expr = parse_expression_unary(precedence);
if (expr.has_error()) {
return ast::expression_node_r(ast::error{ expr.error().location });
}
return parse_expression_rhs(std::move(expr.value()), precedence);
}
ast::expression_node_r parser::parse_expression_primary() {
@@ -214,12 +231,16 @@ ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence)
if (current.precedence >= precedence) break;
auto token = next_token();
ast::expression_node_r expression;
ast::expression_node_p expression;
auto nextIt = s_prefixes.find(peek_token()->type);
if (nextIt != s_prefixes.end()) {
auto next = nextIt->second;
expression = parse_expression_unary(current.precedence + 1);
auto next = nextIt->second;
auto expr = parse_expression_unary(current.precedence + 1);
if (expr.has_error()) {
return ast::expression_node_r(ast::error{ expr.error().location });
}
expression = std::move(std::move(expr.value()));
}
result =
@@ -227,7 +248,13 @@ ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence)
}
if (result == nullptr) return parse_expression_primary();
if (result->get_node().has_value()) result->set_node(parse_expression_primary());
if (result->get_node() == nullptr) {
auto expr = parse_expression_primary();
if (expr.has_error()) {
return ast::expression_node_r(ast::error{ expr.error().location });
}
result->set_node(std::move(std::move(expr.value())));
}
return result;
}
@@ -282,7 +309,7 @@ struct rhsop_info {
}
};
ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& init, std::uint32_t precedence) {
ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_p&& init, std::uint32_t precedence) {
static std::unordered_map<token_t, rhsop_info> s_rhsops = {
{ token_t::Plus, rhsop_info::create(ast::binop_expression_node_t::Add, 5, associativity::Left) },
{ token_t::Minus, rhsop_info::create(ast::binop_expression_node_t::Sub, 5, associativity::Left) },
@@ -306,7 +333,7 @@ ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& ini
{ token_t::GreaterEq, rhsop_info::create(ast::binop_expression_node_t::GreaterEqual, 9, associativity::Left) },
};
ast::expression_node_r lhs = std::move(init);
ast::expression_node_p lhs = std::move(init);
while (peek_token().has_value()) {
auto it = s_rhsops.find(peek_token()->type);
if (it == s_rhsops.end()) return lhs;
@@ -315,20 +342,28 @@ ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& ini
if (current.precedence >= precedence) return lhs;
auto opToken = next_token();
ast::expression_node_r rhs;
ast::expression_node_p rhs;
if (current.type != rhsop_info_t::Unaryop) {
rhs = parse_expression_unary(current.precedence + 1); // unary prefix is always right-associative
auto expr = parse_expression_unary(current.precedence + 1); // unary prefix is always right-associative
if (expr.has_error()) {
return ast::expression_node_r(ast::error{ expr.error().location });
}
rhs = std::move(expr.value());
}
auto nextIt = s_rhsops.find(peek_token()->type);
if (nextIt != s_rhsops.end()) {
rhsop_info next = nextIt->second;
auto expr = std::move(parse_expression_rhs(std::move(rhs),
current.precedence + static_cast<std::uint32_t>(current.associativity == associativity::Right)));
if (expr.has_error()) {
return ast::expression_node_r(ast::error{ expr.error().location });
}
if (current.type != rhsop_info_t::Unaryop) {
rhs = std::move(parse_expression_rhs(std::move(rhs),
current.precedence + static_cast<std::uint32_t>(current.associativity == associativity::Right)));
rhs = std::move(expr.value());
} else {
lhs = std::move(parse_expression_rhs(std::move(lhs), current.precedence));
lhs = std::move(expr.value());
}
}