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
+37
View File
@@ -3,9 +3,11 @@
#include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/exceptions.hpp"
#include "furvm/function.hpp" // IWYU pragma: keep
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
#include <cstdint>
#include <stdexcept>
namespace furvm {
@@ -62,6 +64,31 @@ thing_p executor::thing() const {
return m_stack.top();
}
void executor::store_thing(variable_t variable, const thing_p& thing) {
auto& frame = m_frames.top();
frame.variables.resize(variable + 1);
if (frame.variables[variable] != nullptr) {
frame.variables[variable]->remove_reference();
}
frame.variables[variable] = thing;
thing->add_reference();
}
void executor::store_thing(variable_t variable, thing_p&& thing) {
auto& frame = m_frames.top();
frame.variables.resize(variable + 1);
if (frame.variables[variable] != nullptr) {
frame.variables[variable]->remove_reference();
}
thing->add_reference();
frame.variables[variable] = std::move(thing);
}
thing_p executor::load_thing(variable_t variable) const {
const auto& frame = m_frames.top();
return frame.variables[variable];
}
void executor::step() {
if ((m_flags & executor_flags::Suspended) == executor_flags::Suspended) return;
@@ -109,6 +136,16 @@ void executor::step() {
auto lhs = pop_thing();
push_thing(lhs % rhs);
} break;
case instruction_t::Load: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
push_thing(load_thing(variable));
} break;
case instruction_t::Store: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
store_thing(variable, std::move(pop_thing()));
} break;
case instruction_t::Call: {
function_handle funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);