feat(furvm): add a thing

This commit is contained in:
2026-06-06 22:52:25 +02:00
parent 5ace40b6aa
commit a077457636
5 changed files with 171 additions and 2 deletions
+61 -2
View File
@@ -2,8 +2,10 @@
#define FURVM_EXECUTOR_HPP
#include "furvm/fwd.hpp"
#include "furvm/thing.hpp"
#include <memory>
#include <stack>
namespace furvm {
@@ -33,6 +35,17 @@ private:
struct egzekutor {
explicit egzekutor() = default;
};
public:
/**
* @brief Executor frame.
*
* Call frame.
*/
struct frame {
std::shared_ptr<mod> 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>& 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<class thing>& thing);
/**
* @brief Pops a thing from the stack.
*
* @return The popped thing.
*/
std::shared_ptr<class thing> pop_thing();
/**
* @brief Returns the top thing on the stack.
*
* @return The thing.
*/
std::shared_ptr<class thing> thing() const;
private:
executor_handle m_id;
executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization)
std::shared_ptr<context> m_context;
std::stack<struct frame> m_frames;
std::stack<std::shared_ptr<class thing>> m_stack;
};
} // namespace furvm
+21
View File
@@ -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
/**
+59
View File
@@ -0,0 +1,59 @@
#ifndef FURVM_THING_HPP
#define FURVM_THING_HPP
#include "furvm/fwd.hpp"
#include <memory>
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>& 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<context> m_context;
void* m_data;
};
} // namespace furvm
#endif // FURVM_THING_HPP
+30
View File
@@ -17,4 +17,34 @@ std::shared_ptr<executor> executor::create(const std::shared_ptr<context>& conte
return std::move(ex);
}
void executor::push_frame(const std::shared_ptr<mod>& 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<class thing>& thing) {
m_stack.push(thing);
}
std::shared_ptr<class thing> executor::pop_thing() {
if (m_frames.top().stackBase >= m_stack.size()) return {};
std::shared_ptr<class thing> top = std::move(m_stack.top());
m_stack.pop();
return top;
}
std::shared_ptr<class thing> executor::thing() const {
if (m_frames.top().stackBase >= m_stack.size()) return {};
return m_stack.top();
}
} // namespace furvm
View File