From 251956045117de20d13de7048d3ebeb7169a8214 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Mon, 6 Jul 2026 22:06:53 +0200 Subject: [PATCH] refactor: remove name from function Closes: #47 --- furc/src/back/furvm.cpp | 10 ++--- furvm/include/furvm/function.hpp | 26 +++---------- furvm/include/furvm/module.hpp | 66 ++++++++++---------------------- furvm/src/function.cpp | 6 +-- furvm/src/main.cpp | 4 +- furvm/src/module.cpp | 15 +++++--- 6 files changed, 45 insertions(+), 82 deletions(-) diff --git a/furc/src/back/furvm.cpp b/furc/src/back/furvm.cpp index b592053..2c3de20 100644 --- a/furc/src/back/furvm.cpp +++ b/furc/src/back/furvm.cpp @@ -23,9 +23,9 @@ void furvm_generator::generate_function(furvm::mod& mod, const furlang::ir::func switch (function.type()) { case furlang::ir::function_t::Normal: { if (function.access() == furlang::ir::function_access_t::Public) - mod.emplace_function(function.name(), function.param_count(), mod.bytecode().size()).dispatch(); + mod.emplace_function_named(function.name(), function.param_count(), mod.bytecode().size()).dispatch(); else - mod.emplace_function_private(function.name(), function.param_count(), mod.bytecode().size()).dispatch(); + mod.emplace_function(function.param_count(), mod.bytecode().size()).dispatch(); function_context ctx; for (furlang::ir::block_index idx = 0; idx < function.blocks().size(); ++idx) { @@ -48,9 +48,9 @@ void furvm_generator::generate_function(furvm::mod& mod, const furlang::ir::func } break; case furlang::ir::function_t::Native: { if (function.access() == furlang::ir::function_access_t::Public) - mod.emplace_function(function.name(), function.param_count(), function.name()).dispatch(); + mod.emplace_function_named(function.name(), function.param_count(), function.name()).dispatch(); else - mod.emplace_function_private(function.name(), function.param_count(), function.name()).dispatch(); + mod.emplace_function(function.param_count(), function.name()).dispatch(); } break; } } @@ -150,7 +150,7 @@ void furvm_generator::generate_instruction(furvm::mod& mod, const auto& call = dynamic_cast(instr); // TODO: Implement a queue for unknown functions - furvm::function_id func = mod.get_function_id(call.name()); + furvm::function_id func = mod.function_at(call.name()).id(); mod.bytecode().push_back(static_cast(furvm::instruction_t::Call)); mod.bytecode().push_back((func >> 0) & 0xFF); mod.bytecode().push_back((func >> 8) & 0xFF); diff --git a/furvm/include/furvm/function.hpp b/furvm/include/furvm/function.hpp index dfc95fa..caf4b50 100644 --- a/furvm/include/furvm/function.hpp +++ b/furvm/include/furvm/function.hpp @@ -36,29 +36,21 @@ public: /** * @brief Constructs a normal function. * - * @param name Name of the function. * @param paramCount Paremeter count. * @param position Offset in bytecode of the function. */ - template - function(Name&& name, std::uint32_t paramCount, bytecode_pos position) - : m_name(std::forward(name)), m_type(function_t::Normal), m_paramCount(paramCount), m_value(position) {} + function(std::uint32_t paramCount, bytecode_pos position) + : m_type(function_t::Normal), m_paramCount(paramCount), m_value(position) {} /** * @brief Constructs a native function. * - * @param name Name of the function. * @param paramCount Paremeter count. * @param native Native function tag. */ - template >> - function(Name&& name, std::uint32_t paramCount, Native&& native) - : m_name(std::forward(name)), - m_type(function_t::Native), - m_paramCount(paramCount), - m_value(std::forward(native)) {} + template >> + function(std::uint32_t paramCount, Native&& native) + : m_type(function_t::Native), m_paramCount(paramCount), m_value(std::forward(native)) {} /** * @brief Constructs an import function. @@ -103,13 +95,6 @@ public: */ function& operator=(const function&); public: - /** - * @brief Returns a name of this function. - * - * @return The name. - */ - constexpr const std::string& name() const { return m_name; } - /** * @brief Returns a type of this function. * @@ -154,7 +139,6 @@ public: return m_value.imp; } private: - std::string m_name; function_t m_type; std::uint32_t m_paramCount; diff --git a/furvm/include/furvm/module.hpp b/furvm/include/furvm/module.hpp index 5b50e3e..c2d766e 100644 --- a/furvm/include/furvm/module.hpp +++ b/furvm/include/furvm/module.hpp @@ -86,51 +86,38 @@ public: function_h emplace_function(Args&&... args) { function_h function; if constexpr (std::is_constructible_v) { - function = m_functions.emplace_back(std::forward(args)...); + function = std::move(m_functions.emplace_back(std::forward(args)...)); } else { - function = m_functions.emplace(std::forward(args)...); + function = std::move(m_functions.emplace(std::forward(args)...)); } - - m_functionMap[function->name()] = function.id(); - m_publicFunctions[function->name()] = function.id(); + m_functionMap[function.id()] = ""; return std::move(function); } /** * @brief Emplaces a function in the module's function container. * - * Emplaces the function in module's function container and name to function map . + * Emplaces the function in module's function container and name to function map. * * @param args Arguments forwarded into the container's emplace_back function. * @return A handle to the emplaced function. */ - template - function_h emplace_function_private(Args&&... args) { - function_h function = m_functions.emplace_back(std::forward(args)...); - m_functionMap[function->name()] = function.id(); + template >> + function_h emplace_function_named(NameFwd&& name, Args&&... args) { + function_h function; + if constexpr (std::is_constructible_v) { + function = std::move(m_functions.emplace_back(std::forward(args)...)); + } else { + function = std::move(m_functions.emplace(std::forward(args)...)); + } + std::string nameInst = std::forward(name); + m_functionNames[nameInst] = function.id(); + m_functionMap[function.id()] = nameInst; return std::move(function); } - /** - * @brief Inserts a function in the module's function container. - * - * @param function Function to insert. - */ - template - auto push_back(Function&& function) { - return emplace_function(std::forward(function)); - } - - /** - * @brief Inserts a function in the module's function container. - * - * @param function Function to insert. - */ - template - auto push_back_private(Function&& function) { - return emplace_function_private(std::forward(function)); - } - /** * @brief Returns a function from the module. * @@ -155,7 +142,7 @@ public: */ template >> auto function_at(NameFwd&& name) { - return function_at(m_publicFunctions.at(std::forward(name))); + return function_at(m_functionNames.at(std::forward(name))); } /** @@ -166,18 +153,7 @@ public: */ template >> auto function_at(NameFwd&& name) const { - return function_at(m_publicFunctions.at(std::forward(name))); - } - - /** - * @brief Returns an id of a function from the module. - * - * @param name Name of the function. - * @return An id of the function. - */ - template - auto get_function_id(NameFwd&& name) { - return m_functionMap.at(std::forward(name)); + return function_at(m_functionNames.at(std::forward(name))); } /** @@ -262,8 +238,8 @@ public: private: bytecode_t m_bytecode; - std::unordered_map m_functionMap; - std::unordered_map m_publicFunctions; + std::unordered_map m_functionNames; + std::unordered_map m_functionMap; handle_container m_functions; handle_container m_types; diff --git a/furvm/src/function.cpp b/furvm/src/function.cpp index 36fa5a7..f1f18fb 100644 --- a/furvm/src/function.cpp +++ b/furvm/src/function.cpp @@ -23,7 +23,7 @@ function::~function() { } function::function(function&& other) noexcept - : m_name(std::move(other.m_name)), m_type(other.m_type), m_paramCount(other.m_paramCount) { + : m_type(other.m_type), m_paramCount(other.m_paramCount) { switch (m_type) { case function_t::Normal: { m_value.position = other.m_value.position; @@ -42,7 +42,6 @@ function::function(function&& other) noexcept function& function::operator=(function&& other) noexcept { if (this == &other) return *this; - m_name = std::move(other.m_name); m_type = other.m_type; m_paramCount = other.m_paramCount; switch (m_type) { @@ -63,7 +62,7 @@ function& function::operator=(function&& other) noexcept { } function::function(const function& other) - : m_name(other.m_name), m_type(other.m_type), m_paramCount(other.m_paramCount) { + : m_type(other.m_type), m_paramCount(other.m_paramCount) { switch (m_type) { case function_t::Normal: { m_value.position = other.m_value.position; @@ -81,7 +80,6 @@ function::function(const function& other) function& function::operator=(const function& other) { if (this == &other) return *this; - m_name = other.m_name; m_type = other.m_type; m_paramCount = other.m_paramCount; switch (m_type) { diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index d55f3d3..3ec0e3e 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -60,8 +60,8 @@ int main(int argc, char** argv) { furvm::mod_h mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode)); mod->emplace_type(std::make_shared(context->at("core")->type_at(2), 10)).dispatch(); - auto mainFunc = mod->emplace_function("main", 0, 0); - mod->emplace_function_private("print", 1, "print").dispatch(); + auto mainFunc = mod->emplace_function_named("main", 0, 0); + mod->emplace_function(1, "print").dispatch(); #endif mod->set_native_function("print", [](furvm::executor& executor) { print_thing(*executor.load_thing(0)); }); diff --git a/furvm/src/module.cpp b/furvm/src/module.cpp index 857b31b..bfd4a13 100644 --- a/furvm/src/module.cpp +++ b/furvm/src/module.cpp @@ -27,9 +27,8 @@ std::ostream& mod::serialize(std::ostream& os) const { continue; } - function_h func = m_functions.at(id); - bool isPublic = m_publicFunctions.find(func->name()) != m_publicFunctions.end(); - detail::serialize(os, isPublic ? func->name() : ""); // private functions have empty names + function_h func = m_functions.at(id); + detail::serialize(os, m_functionMap.at(func.id())); detail::serialize(os, func->param_count()); detail::serialize(os, std::uint8_t(func->type())); switch (func->type()) { @@ -98,12 +97,18 @@ mod mod::load(std::istream& is) { case function_t::Normal: { decltype(std::declval().position()) offset = 0; detail::load(is, offset); - mod.emplace_function(id, std::move(name), paramCount, offset).dispatch(); + if (name.empty()) + mod.emplace_function_named(std::move(name), id, paramCount, offset).dispatch(); + else + mod.emplace_function(id, paramCount, offset).dispatch(); } break; case function_t::Native: { std::string native; detail::load(is, native); - mod.emplace_function(id, std::move(name), paramCount, std::move(native)).dispatch(); + if (name.empty()) + mod.emplace_function_named(std::move(name), id, paramCount, std::move(native)).dispatch(); + else + mod.emplace_function(id, paramCount, std::move(native)).dispatch(); } break; case function_t::Import: { std::string modName;