VM basics #5

Merged
andre merged 9 commits from vm into master 2026-04-30 13:34:49 -05:00
6 changed files with 31 additions and 1 deletions
Showing only changes of commit b29f322035 - Show all commits

View File

@@ -16,7 +16,7 @@ void Stack::push(Value const& value)
Value Stack::pop()
{
if (stack_.size() <= fps_.size())
if (stack_.size() <= fps_.top())
throw VMStackUnderflow();
Value v = stack_.back();
@@ -58,4 +58,15 @@ void Stack::pop_fp()
fps_.pop();
}
std::string Stack::debug() const
{
if (stack_.empty())
return "empty";
std::string out;
for (size_t i = 0; i < stack_.size(); ++i)
out += "[" + stack_.at(i).to_string() + "] ";
return out;
}
} // tyche

View File

@@ -23,6 +23,8 @@ public:
[[nodiscard]] size_t fp_level() const { return fps_.size(); }
[[nodiscard]] std::string debug() const;
private:
std::vector<Value> stack_;
std::stack<size_t> fps_;

View File

@@ -84,7 +84,9 @@ TEST(VM, BasicCode)
VM vm;
vm.load_bytecode(std::move(ba));
printf("%s\n", vm.debug_stack().c_str());
vm.call(0);
printf("%s\n", vm.debug_stack().c_str());
int32_t result = vm.to_integer(-1);
ASSERT_EQ(result, 5);

View File

@@ -15,4 +15,15 @@ Type Value::type() const
}, value_);
}
std::string Value::to_string() const
{
return std::visit(overloaded {
[](std::monostate) { return std::string("nil"); },
[](int32_t i) { return std::to_string(i); },
[](float f) { return std::to_string(f); },
[](std::string const& s) { return s; },
[](Function const& f) { return "@" + std::to_string(f.f_id); }
}, value_);
}
}

View File

@@ -33,6 +33,8 @@ public:
[[nodiscard]] std::string as_string() const { return std::get<std::string>(value_); }
[[nodiscard]] FunctionId as_function_id() const { return std::get<Function>(value_).f_id; }
[[nodiscard]] std::string to_string() const;
private:
using Internal = std::variant<std::monostate, int32_t, float, std::string, Function>;
Internal value_;

View File

@@ -17,6 +17,8 @@ public:
void push_integer(int32_t value);
[[nodiscard]] std::string debug_stack() const { return stack_.debug(); }
private:
void run_until_return();
void step();