feat(furc): introduce function declaration parameters

Closes: #16
This commit is contained in:
2026-06-24 18:06:40 +02:00
parent a61f5c194b
commit abeb5b390b
2 changed files with 61 additions and 11 deletions
+35 -6
View File
@@ -7,6 +7,7 @@
#include <optional>
#include <string>
#include <type_traits>
#include <vector>
namespace furc {
namespace ast {
@@ -70,6 +71,14 @@ protected:
bool equal(const node& rhs) const override;
};
/**
* @brief Parameter of function declaration AST node.
*/
struct function_declaration_param {
std::string name;
type type;
};
/**
* @brief Function declaration AST node.
*/
@@ -82,9 +91,12 @@ public:
* @param name Name of the function.
* @param type Return type of the function.
*/
template <typename T>
function_declaration_node(struct location location, T&& name, std::optional<type>&& returnType)
: declaration_node(location), p_name(std::forward<T>(name)), p_returnType(std::move(returnType)) {}
template <typename T, typename ParamsFwd>
function_declaration_node(struct location location, T&& name, std::optional<type>&& returnType, ParamsFwd&& params)
: declaration_node(location),
p_name(std::forward<T>(name)),
p_returnType(std::move(returnType)),
p_params(std::forward<ParamsFwd>(params)) {}
public:
/**
* @brief Returns this node's declaration type.
@@ -106,6 +118,13 @@ public:
* @return Function's return type.
*/
const std::optional<type>& return_type() const { return p_returnType; }
/**
* @brief Returns function's parameters.
*
* @return Function's parameters.
*/
const std::vector<function_declaration_param>& params() const { return p_params; }
public:
void accept(visitor& visitor) const override;
@@ -122,6 +141,11 @@ protected:
* @brief Return type of the function.
*/
std::optional<type> p_returnType;
/**
* @brief Parameters of the function.
*/
std::vector<function_declaration_param> p_params;
};
/**
@@ -137,9 +161,14 @@ public:
* @param type Return type of the function.
* @param body Body of the function.
*/
template <typename T>
function_definition_node(struct location location, T&& name, std::optional<type>&& type, body&& body)
: function_declaration_node(location, std::forward<T>(name), std::move(type)), m_body(std::move(body)) {}
template <typename T, typename ParamsFwd>
function_definition_node(struct location location,
T&& name,
std::optional<type>&& type,
ParamsFwd&& params,
body&& body)
: function_declaration_node(location, std::forward<T>(name), std::move(type), std::forward<ParamsFwd>(params)),
m_body(std::move(body)) {}
public:
/**
* @brief Returns this node's declaration type.
+25 -4
View File
@@ -12,6 +12,7 @@
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
namespace furc::front {
@@ -66,6 +67,25 @@ ast::declaration_node_r parser::parse_declaration() {
auto tok = eat_token(token_t::LParen);
if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
std::vector<ast::function_declaration_param> params;
if (peek_token().has_value() && peek_token()->type != token_t::RParen) {
while (true) {
auto name = eat_token(token_t::Identifier);
if (name.has_error()) return ast::declaration_node_r(ast::error{ name.error().location });
auto colon = eat_token(token_t::Colon);
if (colon.has_error()) return ast::declaration_node_r(ast::error{ colon.error().location });
auto type = parse_type();
if (type.has_error()) return ast::declaration_node_r(ast::error{ type.error().location });
params.push_back(
ast::function_declaration_param{ std::string(name->value.string), std::move(*type) });
auto comma = eat_token(token_t::Comma);
if (comma.has_error()) break;
}
}
tok = eat_token(token_t::RParen);
if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
@@ -86,13 +106,15 @@ ast::declaration_node_r parser::parse_declaration() {
return m_arena->allocate_shared<ast::function_definition_node>(first->location,
name->value.string,
std::move(returnType),
std::move(params),
std::move(body.value()));
}
case token_t::Semicolon: {
m_peekBuffer.clear();
return m_arena->allocate_shared<ast::function_declaration_node>(first->location,
name->value.string,
std::move(returnType));
std::move(returnType),
std::move(params));
}
default: return ast::declaration_node_r(ast::error{ tok->location });
}
@@ -465,15 +487,14 @@ const token_r& parser::peek_token() {
}
token_r parser::eat_token(token_t type) {
auto token = next_token();
if (const auto& token = peek_token(); token.has_error() || peek_token()->type != type) {
if (token.has_error()) return token;
if (token->type != type) {
if (token->type == token_t::None)
return token_r(token_error{ token->location, token_error_t::UnexpectedToken, ", expected " + type });
return token_r(
token_error{ token->location, token_error_t::UnexpectedToken, ""s + token->type + ", expected " + type });
}
return token;
return next_token();
}
} // namespace furc::front