From 6ec4710eaa349397315399c6183d43fcb99219ed Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Wed, 1 Jul 2026 21:58:33 +0200 Subject: [PATCH] feat: introduce reference instruction --- furvm/include/furvm/instruction.hpp | 7 +++++++ furvm/include/furvm/thing.hpp | 2 +- furvm/src/executor.cpp | 4 ++++ furvm/src/main.cpp | 5 +++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/furvm/include/furvm/instruction.hpp b/furvm/include/furvm/instruction.hpp index fa4e44b..b728a9a 100644 --- a/furvm/include/furvm/instruction.hpp +++ b/furvm/include/furvm/instruction.hpp @@ -40,6 +40,13 @@ enum class instruction_t : byte { */ Clone, + /** + * @brief Pushes a new reference onto the stack. + * + * Pops the top thing from the stack and pushes its reference. + */ + Reference, + /** * @brief Adds two things together on the stack. */ diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index 17d0afd..3e65943 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -368,7 +368,7 @@ public: } thing reference() const { - thing res = { std::make_shared({ m_type }), m_allocator }; + thing res = { std::make_shared(m_type), m_allocator }; res.get() = m_data; return std::move(res); } diff --git a/furvm/src/executor.cpp b/furvm/src/executor.cpp index fe61fef..07c6ba2 100644 --- a/furvm/src/executor.cpp +++ b/furvm/src/executor.cpp @@ -102,6 +102,10 @@ void executor::step() { case instruction_t::Clone: { push_thing(std::move(thing()->clone())); } break; + case instruction_t::Reference: { + auto thing = pop_thing(); + push_thing(std::move(thing->reference())); + } break; case instruction_t::Add: { auto rhs = pop_thing(); auto lhs = pop_thing(); diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index 2b1973e..715992c 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -8,10 +8,11 @@ #include #include -static constexpr std::array s_bytecode = { +static constexpr std::array s_bytecode = { furvm::byte(furvm::instruction_t::PushB2I), 67, - furvm::byte(furvm::instruction_t::Clone), + furvm::byte(furvm::instruction_t::Duplicate), + furvm::byte(furvm::instruction_t::Reference), furvm::byte(furvm::instruction_t::Add), furvm::byte(furvm::instruction_t::Call), 1,