44 lines
794 B
C++
44 lines
794 B
C++
|
#pragma once
|
||
|
|
||
|
#include "arch.hpp"
|
||
|
#include "value.hpp"
|
||
|
#include <cstddef>
|
||
|
#include <cstdint>
|
||
|
#include <vector>
|
||
|
|
||
|
namespace sliger {
|
||
|
|
||
|
class VM {
|
||
|
public:
|
||
|
VM(const std::vector<Op>& program)
|
||
|
: program(program.data())
|
||
|
, program_size(program.size())
|
||
|
{
|
||
|
}
|
||
|
void run();
|
||
|
|
||
|
inline void step() { this->pc += 1; }
|
||
|
|
||
|
inline auto eat_as_op() -> Op
|
||
|
{
|
||
|
auto value = curr_as_op();
|
||
|
step();
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
inline auto curr_as_op() const -> Op
|
||
|
{
|
||
|
return static_cast<Op>(this->program[this->pc]);
|
||
|
}
|
||
|
|
||
|
inline auto done() const -> bool { return this->pc >= this->program_size; }
|
||
|
|
||
|
private:
|
||
|
uint32_t pc = 0;
|
||
|
const Op* program;
|
||
|
size_t program_size;
|
||
|
std::vector<Value> stack;
|
||
|
std::vector<Value> pool_heap;
|
||
|
};
|
||
|
}
|