feat(furvm): add stack underflow exception

This commit is contained in:
2026-06-07 12:02:36 +02:00
parent 464b24eeed
commit 48c6da1134
4 changed files with 57 additions and 4 deletions
+43
View File
@@ -0,0 +1,43 @@
#ifndef FURVM_EXCEPTIONS_HPP
#define FURVM_EXCEPTIONS_HPP
#include <exception>
namespace furvm {
class stack_underflow : public std::exception {
public:
stack_underflow() = default;
~stack_underflow() override = default;
/**
* @brief Move constructor.
*/
stack_underflow(stack_underflow&&) noexcept = default;
/**
* @brief Move constructor.
*/
stack_underflow& operator=(stack_underflow&&) noexcept = default;
/**
* @brief Copy constructor.
*/
stack_underflow(const stack_underflow&) = default;
/**
* @brief Copy constructor.
*/
stack_underflow& operator=(const stack_underflow&) = default;
public:
/**
* @brief Returns a C-style string describing the cause of the error.
*
* @return The cause of the error.
*/
const char* what() const noexcept override { return "stack underflow"; }
};
} // namespace furvm
#endif // FURVM_EXCEPTIONS_HPP
+7
View File
@@ -140,6 +140,13 @@ class context;
*/
using context_p = std::shared_ptr<context>;
// exceptions.hpp:
/**
* @brief Stack underflow exception.
*/
class stack_underflow;
} // namespace furvm
#endif // FURVM_FWD_HPP
+5 -3
View File
@@ -1,6 +1,7 @@
#include "furvm/executor.hpp"
#include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/exceptions.hpp"
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
@@ -26,6 +27,7 @@ void executor::push_frame(const mod_p& mod, std::size_t position) {
}
struct executor::frame executor::pop_frame() {
if (m_frames.empty()) throw stack_underflow();
struct executor::frame frame = m_frames.top();
m_frames.pop();
return frame;
@@ -40,14 +42,14 @@ void executor::push_thing(const thing_p& thing) {
}
thing_p executor::pop_thing() {
if (m_frames.top().stackBase >= m_stack.size()) return {};
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
thing_p top = std::move(m_stack.top());
m_stack.pop();
return top;
}
thing_p executor::thing() const {
if (m_frames.top().stackBase >= m_stack.size()) return {};
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
return m_stack.top();
}
@@ -65,7 +67,7 @@ void executor::step() {
m_stack.push(std::move(thing));
} break;
case instruction_t::Drop: {
m_stack.pop();
pop_thing();
} break;
case instruction_t::Return: {
pop_frame();
+2 -1
View File
@@ -8,9 +8,10 @@
#include <cstddef>
#include <memory>
static constexpr std::array<furvm::byte, 3> s_bytecode = {
static constexpr std::array<furvm::byte, 4> s_bytecode = {
furvm::byte(furvm::instruction_t::PushB2I),
67,
furvm::byte(furvm::instruction_t::Drop),
furvm::byte(furvm::instruction_t::Return),
};