refactor(AST): improve function declaration AST node

Change p_name to be of type std::string instead of furc::token.
This commit is contained in:
2026-06-03 16:11:20 +02:00
parent 02ce25df2d
commit b68a559980
4 changed files with 19 additions and 13 deletions
+10 -7
View File
@@ -3,7 +3,8 @@
#include "furc/ast/node.hpp" #include "furc/ast/node.hpp"
#include "furc/ast/statement.hpp" #include "furc/ast/statement.hpp"
#include "furc/front/token.hpp"
#include <string>
namespace furc { namespace furc {
namespace ast { namespace ast {
@@ -64,8 +65,9 @@ public:
* @param location Node location. * @param location Node location.
* @param name Name of the function. * @param name Name of the function.
*/ */
function_declaration_node(struct location location, front::token name) template <typename T>
: declaration_node(location), p_name(name) {} function_declaration_node(struct location location, T&& name)
: declaration_node(location), p_name(std::forward<T>(name)) {}
public: public:
/** /**
* @brief Returns this node's declaration type. * @brief Returns this node's declaration type.
@@ -79,7 +81,7 @@ public:
* *
* @return Name of the function. * @return Name of the function.
*/ */
front::token name() const { return p_name; } std::string name() const { return p_name; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -90,7 +92,7 @@ protected:
/** /**
* @brief Name of the function. * @brief Name of the function.
*/ */
front::token p_name; std::string p_name;
}; };
/** /**
@@ -105,8 +107,9 @@ public:
* @param name Name of the function. * @param name Name of the function.
* @param body Body of the function. * @param body Body of the function.
*/ */
function_definition_node(struct location location, front::token name, body_r&& body) template <typename T>
: function_declaration_node(location, name), m_body(std::move(body)) {} function_definition_node(struct location location, T&& name, body_r&& body)
: function_declaration_node(location, std::forward<T>(name)), m_body(std::move(body)) {}
public: public:
/** /**
* @brief Returns this node's declaration type. * @brief Returns this node's declaration type.
+2 -2
View File
@@ -118,7 +118,7 @@ void function_declaration_node::accept(visitor& visitor) const {
} }
std::ostream& function_declaration_node::print(std::ostream& os) const { std::ostream& function_declaration_node::print(std::ostream& os) const {
return os << "function " << p_name->string << " declaration"; return os << "function " << p_name << " declaration";
} }
bool function_declaration_node::equal(const node& rhs) const { bool function_declaration_node::equal(const node& rhs) const {
@@ -135,7 +135,7 @@ std::ostream& function_definition_node::print(std::ostream& os) const {
if (m_body.has_value()) { if (m_body.has_value()) {
for (const auto& entry : m_body->statements) for (const auto& entry : m_body->statements)
os << entry << '\n'; os << entry << '\n';
return os << m_body->end << ": " << p_name->string << " end"; return os << m_body->end << ": " << p_name << " end";
} }
return os << m_body.error(); return os << m_body.error();
} }
+2 -1
View File
@@ -5,6 +5,7 @@
#include "furc/ast/literal.hpp" // IWYU pragma: keep #include "furc/ast/literal.hpp" // IWYU pragma: keep
#include "furc/ast/statement.hpp" // IWYU pragma: keep #include "furc/ast/statement.hpp" // IWYU pragma: keep
#include <cassert>
#include <iostream> #include <iostream>
namespace furc::front { namespace furc::front {
@@ -14,7 +15,7 @@ namespace ir = furlang::ir;
} }
void ir_generator::visit(const ast::function_definition_node& funcDef) { void ir_generator::visit(const ast::function_definition_node& funcDef) {
m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()->string)); m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()));
push_block(); push_block();
if (funcDef.body().has_error()) { if (funcDef.body().has_error()) {
+5 -3
View File
@@ -37,7 +37,7 @@ ast::program_node_r parser::parse() & {
program->push(std::move(parse_declaration())); program->push(std::move(parse_declaration()));
} }
return program; return std::move(program);
} }
ast::declaration_node_r parser::parse_declaration() { ast::declaration_node_r parser::parse_declaration() {
@@ -61,11 +61,13 @@ ast::declaration_node_r parser::parse_declaration() {
case token_t::LBrace: { case token_t::LBrace: {
ast::body_r body = parse_body(); ast::body_r body = parse_body();
if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location }); 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, std::move(body)); return m_arena.allocate_shared<ast::function_definition_node>(first->location,
name->value.string,
std::move(body));
} }
case token_t::Semicolon: { case token_t::Semicolon: {
m_peekBuffer.clear(); m_peekBuffer.clear();
return m_arena.allocate_shared<ast::function_declaration_node>(first->location, *name); return m_arena.allocate_shared<ast::function_declaration_node>(first->location, name->value.string);
} }
default: return ast::declaration_node_r(ast::error{ tok->location }); default: return ast::declaration_node_r(ast::error{ tok->location });
} }