roza/lib/VM.hpp

40 lines
907 B
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>)>;
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::vector<param_t> m_stack;
std::shared_ptr<Program> m_last_program;
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