roza/lib/VM.hpp

54 lines
1.1 KiB
C++

#ifndef roza_VM_HPP
#define roza_VM_HPP
#include "commons.hpp"
#include "Program.hpp"
#include "StatusLog.hpp"
namespace roza
{
using unop_t = std::function<std::shared_ptr<Value>
(std::shared_ptr<Value>)>;
using binop_t = std::function<std::shared_ptr<Value>
(std::shared_ptr<Value>,
std::shared_ptr<Value>)>;
using var_map = std::unordered_map<int, std::shared_ptr<Value>>;
struct Frame {
var_map locals;
size_t pc;
size_t sp;
size_t bp;
};
class VM
{
public:
explicit VM(StatusLog& log);
virtual ~VM();
void exec(std::shared_ptr<Program> program);
std::string string() const;
private:
StatusLog& m_log;
std::array<param_t, 4096> m_stack;
std::shared_ptr<Program> m_last_program;
var_map m_globals;
std::vector<Frame> m_frames;
size_t m_sp = 0;
size_t m_bp = 0;
size_t m_pc = 0;
void push(param_t param);
param_t pop();
void apply_binop(std::shared_ptr<Program> program, binop_t op);
void apply_unop(std::shared_ptr<Program> program, unop_t op);
};
}
#endif