From 30a06186db0eb592ff719af8b8adc1b2651ee25d Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Tue, 1 Sep 2026 22:33:18 +0200 Subject: [PATCH] feat(furvm): introduce stack storage Closes: #62 --- furvm/include/furvm/executor.hpp | 38 ++++++++------- furvm/include/furvm/function.hpp | 1 - furvm/include/furvm/thing.hpp | 72 +++++++++++++++++++++++++---- furvm/src/executor.cpp | 79 +++++++++++++++++++------------- 4 files changed, 130 insertions(+), 60 deletions(-) diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index e269511..6ddeb2f 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -3,7 +3,8 @@ #include "furvm/fwd.hpp" #include "furvm/module.hpp" // IWYU pragma: keep -#include "furvm/thing.hpp" // IWYU pragma: keep +#include "furvm/stack.hpp" +#include "furvm/thing.hpp" // IWYU pragma: keep #include #include @@ -39,6 +40,8 @@ public: static constexpr executor_flags STATE_FLAGS = executor_flags::JustHit; using new_frame_callback = std::function; + + using stack_thing = thing; public: /** * @brief Executor frame. @@ -50,8 +53,8 @@ public: std::size_t position; /**< Cursor to a current instruction in the bytecode. */ std::size_t stackBase; /**< Snapshot of the stack size before this frame. */ - thing_type* returnType; /**< Return type. */ - std::vector> variables; /**< Frame variables. */ + thing_type* returnType; /**< Return type. */ + std::vector variables; /**< Frame variables. */ }; public: ~executor() = default; @@ -131,27 +134,27 @@ public: * @param thing Thing. * @return The pushed handle. */ - thing<>& push_thing(thing<>&& thing); + stack_thing& push_thing(stack_thing&& thing); - thing<>& push_thing(const thing<>& thing); + stack_thing& push_thing(const stack_thing& thing); /** * @brief Pops a thing from the stack. * * @return A handle to the popped thing. */ - thing<> pop_thing(); + stack_thing pop_thing(); /** * @brief Returns the top thing on the stack. * * @return A handle to the top thing. */ - thing<>& top_thing(); + stack_thing& top_thing(); - const thing<>& top_thing() const; + const stack_thing& top_thing() const; - const std::vector>& stack() const { return m_stack; } + const std::vector& stack() const { return m_stack; } public: /** * @brief Stores a thing in a frame variable. @@ -159,7 +162,7 @@ public: * @param variable Id of the variable in which the handle will be put. * @param thing Thing handle. */ - void store_thing(variable_t variable, const thing<>& thing); + void store_thing(variable_t variable, const stack_thing& thing); /** * @brief Stores a thing in a frame variable. @@ -167,7 +170,7 @@ public: * @param variable Id of the variable in which the handle will be put. * @param thing Thing handle. */ - void store_thing(variable_t variable, thing<>&& thing); + void store_thing(variable_t variable, stack_thing&& thing); /** * @brief Returns a thing stored in a variable. @@ -175,9 +178,9 @@ public: * @param variable Id of the variable from which the handle will be fetched. * @return A handle stored in the variable. */ - thing<>& load_thing(variable_t variable); + stack_thing& load_thing(variable_t variable); - const thing<>& load_thing(variable_t variable) const; + const stack_thing& load_thing(variable_t variable) const; public: /** * @brief Executes next instruction. @@ -190,11 +193,12 @@ private: private: static bool compare_thing_types(const thing_type& lhs, const thing_type& rhs); private: - executor_flags m_flags = executor_flags::Done; - context* m_context; + executor_flags m_flags = executor_flags::Done; + context* m_context; + furvm::stack m_stackStorage; - std::stack m_frames; - std::vector> m_stack; + std::stack m_frames; + std::vector m_stack; new_frame_callback m_newFrameCb = nullptr; }; diff --git a/furvm/include/furvm/function.hpp b/furvm/include/furvm/function.hpp index 8177cc1..70f984b 100644 --- a/furvm/include/furvm/function.hpp +++ b/furvm/include/furvm/function.hpp @@ -1,7 +1,6 @@ #ifndef FURVM_FUNCTION_HPP #define FURVM_FUNCTION_HPP -#include "furlang/utility/hash.hpp" #include "furvm/fwd.hpp" #include "furvm/handle.hpp" // IWYU pragma: keep diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index 91429b9..2778f64 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -194,6 +194,9 @@ private: template