feat(furvm): add variables

This commit is contained in:
2026-06-14 14:22:45 +02:00
parent f0fe6b4179
commit f6f716e8c9
4 changed files with 86 additions and 3 deletions
+28 -1
View File
@@ -4,6 +4,7 @@
#include "furvm/fwd.hpp"
#include <stack>
#include <vector>
namespace furvm {
@@ -44,6 +45,8 @@ public:
mod_p 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. */
std::vector<thing_p> variables; /**< Frame variables. */
};
public:
/**
@@ -139,6 +142,30 @@ public:
* @return The thing.
*/
thing_p thing() const;
public:
/**
* @brief Stores a thing in a variable.
*
* @param variable Variable to store the thing in.
* @param thing Thing to store.
*/
void store_thing(variable_t variable, const thing_p& thing);
/**
* @brief Stores a thing in a variable.
*
* @param variable Variable to store the thing in.
* @param thing Thing to store.
*/
void store_thing(variable_t variable, thing_p&& thing);
/**
* @brief Returns a thing stored in a variable.
*
* @param variable Variable where the thing is stored.
* @return The thing stored in the variable.
*/
thing_p load_thing(variable_t variable) const;
public:
/**
* @brief Executes next instruction.
@@ -155,4 +182,4 @@ private:
} // namespace furvm
#endif // FURVM_EXECUTOR_HPP
#endif // FURVM_EXECUTOR_HPP
+6 -1
View File
@@ -132,6 +132,11 @@ using thing_handle = std::uint32_t;
// executor.hpp
/**
* @brief A variable index type.
*/
using variable_t = std::uint16_t;
/**
* @enum executor_flags
* @brief Flags of an executor.
@@ -181,4 +186,4 @@ class stack_underflow;
} // namespace furvm
#endif // FURVM_FWD_HPP
#endif // FURVM_FWD_HPP
+15 -1
View File
@@ -65,6 +65,20 @@ enum class instruction_t : byte {
*/
Mod,
/**
* @brief Pushes a variable onto the stack.
*
* Fetches a variable denoted by next two bytes in little-endian and pushes it onto the stack.
*/
Load,
/**
* @brief Stores an element from the stack in a variable.
*
* Pops a thing from the stack and stores it in a variable denoted by next two bytes in little-endian.
*/
Store,
/**
* @brief Calls a function.
*
@@ -96,4 +110,4 @@ struct instruction {
} // namespace furvm
#endif // FURVM_INSTRUCTION_HPP
#endif // FURVM_INSTRUCTION_HPP