roza/lib/VM.hpp

41 lines
970 B
C++
Raw 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-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;
std::vector<param_t> m_stack;
2023-08-30 22:31:19 +00:00
std::shared_ptr<Program> m_last_program;
std::unordered_map<int, std::shared_ptr<Value>> m_globals;
2023-08-30 18:06:26 +00:00
size_t m_pc = 0;
void push(param_t param);
param_t pop();
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