From a077457636632ad2e00ef9c7f56dd010ccd81071 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sat, 6 Jun 2026 22:52:25 +0200 Subject: [PATCH] feat(furvm): add a thing --- furvm/include/furvm/executor.hpp | 63 +++++++++++++++++++++++++++++++- furvm/include/furvm/fwd.hpp | 21 +++++++++++ furvm/include/furvm/thing.hpp | 59 ++++++++++++++++++++++++++++++ furvm/src/executor.cpp | 30 +++++++++++++++ furvm/src/thing.cpp | 0 5 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 furvm/include/furvm/thing.hpp create mode 100644 furvm/src/thing.cpp diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index 467c412..88d9e3a 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -2,8 +2,10 @@ #define FURVM_EXECUTOR_HPP #include "furvm/fwd.hpp" +#include "furvm/thing.hpp" #include +#include namespace furvm { @@ -33,6 +35,17 @@ private: struct egzekutor { explicit egzekutor() = default; }; +public: + /** + * @brief Executor frame. + * + * Call frame. + */ + struct frame { + std::shared_ptr mod; /**< Shared pointer to a module with the bytecode. */ + std::size_t position; /**< Cursor to a current instruction in the bytecode. */ + std::size_t stackBase; /**< Snapshot of the stack size before this frame. */ + }; public: /** * @brief Private constructor. @@ -47,12 +60,12 @@ public: /** * @brief Move constructor. */ - executor(executor&&) = default; + executor(executor&&) noexcept = default; /** * @brief Move constructor. */ - executor& operator=(executor&&) = default; + executor& operator=(executor&&) noexcept = default; executor(const executor&) = delete; executor& operator=(const executor&) = delete; @@ -77,10 +90,56 @@ public: * @return The flags. */ executor_flags flags() const { return m_flags; } +public: + /** + * @brief Pushes a new frame. + * + * @param mod A shared pointer to a furvm module. + * @param position Position in the module's bytecode. + */ + void push_frame(const std::shared_ptr& mod, std::size_t position); + + /** + * @brief Pops the top frame. + * + * @return The popped frame. + */ + frame pop_frame(); + + /** + * @brief Returns the top frame. + * + * @return The frame. + */ + frame frame() const; +public: + /** + * @brief Pushes a thing onto the stack. + * + * @param thing The thing to push. + */ + void push_thing(const std::shared_ptr& thing); + + /** + * @brief Pops a thing from the stack. + * + * @return The popped thing. + */ + std::shared_ptr pop_thing(); + + /** + * @brief Returns the top thing on the stack. + * + * @return The thing. + */ + std::shared_ptr thing() const; private: executor_handle m_id; executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) std::shared_ptr m_context; + + std::stack m_frames; + std::stack> m_stack; }; } // namespace furvm diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index 1b6b0f5..a049982 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -61,6 +61,27 @@ class mod; */ using module_handle = std::uint32_t; +// thing.hpp + +/** + * @enum thing_t + * @brief Thing type. + */ +enum class thing_t : std::uint8_t; + +/** + * @class thing + * @brief Furvm thing. + * + * A stack element. Think of it like of a value in C++ or I guess a class in java. + */ +class thing; + +/** + * @brief Furvm thing's index. + */ +using thing_handle = std::uint32_t; + // executor.hpp /** diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp new file mode 100644 index 0000000..839331d --- /dev/null +++ b/furvm/include/furvm/thing.hpp @@ -0,0 +1,59 @@ +#ifndef FURVM_THING_HPP +#define FURVM_THING_HPP + +#include "furvm/fwd.hpp" + +#include + +namespace furvm { + +enum class thing_t : std::uint8_t { + Int, +}; + +class thing { + friend class executor; +private: + /** + * @brief A private token for the private constructor. + * + * Also `rzecz` in Polish translates to `thing` I think. + */ + struct rzecz { + explicit rzecz() = default; + }; +public: + /** + * @brief Private constructor. + * + * @param id + * @param type + * @param context + */ + thing(rzecz, thing_handle id, thing_t type, const std::shared_ptr& context); + + ~thing(); + + /** + * @brief Move constructor. + */ + thing(thing&&) noexcept = default; + + /** + * @brief Move constructor. + */ + thing& operator=(thing&&) noexcept = default; + + thing(const thing&) = delete; + thing& operator=(const thing&) = delete; +private: + thing_handle m_id; + thing_t m_type; + std::shared_ptr m_context; + + void* m_data; +}; + +} // namespace furvm + +#endif // FURVM_THING_HPP \ No newline at end of file diff --git a/furvm/src/executor.cpp b/furvm/src/executor.cpp index 4fc7e58..bb26a61 100644 --- a/furvm/src/executor.cpp +++ b/furvm/src/executor.cpp @@ -17,4 +17,34 @@ std::shared_ptr executor::create(const std::shared_ptr& conte return std::move(ex); } +void executor::push_frame(const std::shared_ptr& mod, std::size_t position) { + m_frames.emplace((struct executor::frame){ mod, position, m_stack.size() }); +} + +struct executor::frame executor::pop_frame() { + struct executor::frame frame = m_frames.top(); + m_frames.pop(); + return frame; +} + +struct executor::frame executor::frame() const { + return m_frames.top(); +} + +void executor::push_thing(const std::shared_ptr& thing) { + m_stack.push(thing); +} + +std::shared_ptr executor::pop_thing() { + if (m_frames.top().stackBase >= m_stack.size()) return {}; + std::shared_ptr top = std::move(m_stack.top()); + m_stack.pop(); + return top; +} + +std::shared_ptr executor::thing() const { + if (m_frames.top().stackBase >= m_stack.size()) return {}; + return m_stack.top(); +} + } // namespace furvm \ No newline at end of file diff --git a/furvm/src/thing.cpp b/furvm/src/thing.cpp new file mode 100644 index 0000000..e69de29