roza/lib/VM.hpp

54 lines
1.1 KiB
C++
Raw Permalink Normal View History

2023-08-30 18:06:26 +00:00
#ifndef roza_VM_HPP
#define roza_VM_HPP
#include "commons.hpp"
#include "Program.hpp"
#include "StatusLog.hpp"
namespace roza
{
2023-08-30 22:31:19 +00:00
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>)>;
2023-09-01 20:38:12 +00:00
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;
};
2023-08-30 18:06:26 +00:00
class VM
{
public:
explicit VM(StatusLog& log);
virtual ~VM();
void exec(std::shared_ptr<Program> program);
2023-08-30 22:31:19 +00:00
std::string string() const;
2023-08-30 18:06:26 +00:00
private:
StatusLog& m_log;
2023-09-01 20:38:12 +00:00
std::array<param_t, 4096> m_stack;
2023-08-30 22:31:19 +00:00
std::shared_ptr<Program> m_last_program;
2023-09-01 20:38:12 +00:00
var_map m_globals;
std::vector<Frame> m_frames;
size_t m_sp = 0;
size_t m_bp = 0;
2023-08-30 18:06:26 +00:00
size_t m_pc = 0;
void push(param_t param);
param_t pop();
2023-09-01 20:38:12 +00:00
2023-08-30 22:31:19 +00:00
void apply_binop(std::shared_ptr<Program> program, binop_t op);
void apply_unop(std::shared_ptr<Program> program, unop_t op);
2023-08-30 18:06:26 +00:00
};
}
#endif