refactor(furc): improve function declarations

Refs: #16
This commit is contained in:
2026-06-24 15:52:14 +02:00
parent d497e2de45
commit a61f5c194b
3 changed files with 32 additions and 6 deletions
+19 -4
View File
@@ -4,6 +4,7 @@
#include "furc/ast/node.hpp" #include "furc/ast/node.hpp"
#include "furc/ast/statement.hpp" #include "furc/ast/statement.hpp"
#include <optional>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
@@ -79,10 +80,11 @@ public:
* *
* @param location Node location. * @param location Node location.
* @param name Name of the function. * @param name Name of the function.
* @param type Return type of the function.
*/ */
template <typename T> template <typename T>
function_declaration_node(struct location location, T&& name) function_declaration_node(struct location location, T&& name, std::optional<type>&& returnType)
: declaration_node(location), p_name(std::forward<T>(name)) {} : declaration_node(location), p_name(std::forward<T>(name)), p_returnType(std::move(returnType)) {}
public: public:
/** /**
* @brief Returns this node's declaration type. * @brief Returns this node's declaration type.
@@ -97,6 +99,13 @@ public:
* @return Name of the function. * @return Name of the function.
*/ */
std::string name() const { return p_name; } std::string name() const { return p_name; }
/**
* @brief Returns function's return type.
*
* @return Function's return type.
*/
const std::optional<type>& return_type() const { return p_returnType; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -108,6 +117,11 @@ protected:
* @brief Name of the function. * @brief Name of the function.
*/ */
std::string p_name; std::string p_name;
/**
* @brief Return type of the function.
*/
std::optional<type> p_returnType;
}; };
/** /**
@@ -120,11 +134,12 @@ public:
* *
* @param location Node location. * @param location Node location.
* @param name Name of the function. * @param name Name of the function.
* @param type Return type of the function.
* @param body Body of the function. * @param body Body of the function.
*/ */
template <typename T> template <typename T>
function_definition_node(struct location location, T&& name, body&& body) function_definition_node(struct location location, T&& name, std::optional<type>&& type, body&& body)
: function_declaration_node(location, std::forward<T>(name)), m_body(std::move(body)) {} : function_declaration_node(location, std::forward<T>(name), std::move(type)), m_body(std::move(body)) {}
public: public:
/** /**
* @brief Returns this node's declaration type. * @brief Returns this node's declaration type.
+12 -1
View File
@@ -69,6 +69,14 @@ ast::declaration_node_r parser::parse_declaration() {
tok = eat_token(token_t::RParen); tok = eat_token(token_t::RParen);
if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location }); if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
std::optional<ast::type> returnType;
if (peek_token().has_value() && peek_token()->type == token_t::SlimArrow) {
auto tok = next_token();
auto type = parse_type();
if (type.has_error()) return ast::declaration_node_r(ast::error{ tok->location });
returnType = *type;
}
const auto& peek = peek_token(); const auto& peek = peek_token();
if (peek.has_error()) return ast::declaration_node_r(ast::error{ peek.error().location }); if (peek.has_error()) return ast::declaration_node_r(ast::error{ peek.error().location });
switch (peek->type) { switch (peek->type) {
@@ -77,11 +85,14 @@ ast::declaration_node_r parser::parse_declaration() {
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, return m_arena->allocate_shared<ast::function_definition_node>(first->location,
name->value.string, name->value.string,
std::move(returnType),
std::move(body.value())); std::move(body.value()));
} }
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->value.string); return m_arena->allocate_shared<ast::function_declaration_node>(first->location,
name->value.string,
std::move(returnType));
} }
default: return ast::declaration_node_r(ast::error{ tok->location }); default: return ast::declaration_node_r(ast::error{ tok->location });
} }
+1 -1
View File
@@ -13,7 +13,7 @@
int main(void) { int main(void) {
try { try {
std::string programStr = R"( std::string programStr = R"(
func main() { func main() -> int32 {
x = 0; x = 0;
y = 10; y = 10;
z = 1; z = 1;