valeri/src/vm.hpp

99 lines
2.6 KiB
C++
Raw Normal View History

#pragma once
#include "common.hpp"
class VM {
public:
VM() {}
VM(StackFrame&& stack, Dict&& globals, Value&& res)
: _stack(std::move(stack)),
_globals(std::move(globals)),
_res(std::move(res)) {}
VM(VM&& vm)
: _stack(std::move(vm._stack)),
_globals(std::move(vm._globals)),
_res(std::move(vm._res)) {}
VM(const VM&) = delete;
static Result<VM> create(const Module& mod, const Dict& globals) {
auto fun = Value(TRY(mod.fun()));
auto nil = Value(TRY(Nil::create()));
auto stack = TRY(StackFrame::create(nil, fun));
auto globals_copy = TRY(globals.copy());
/*
auto code = TRY(fun.code());
auto constants = TRY(fun.constants());
auto closure = TRY(fun.closure());
auto globals_copy = TRY(globals.copy());
*/
return VM(std::move(stack), std::move(globals_copy), std::move(nil));
}
Result<Value> run();
Result<void> step();
Result<void> step_bytecode();
Result<void> step_native();
Result<void> vm_mov(Opcode& oc);
Result<void> vm_add(Opcode& oc);
Result<void> vm_mul(Opcode& oc);
Result<void> vm_sub(Opcode& oc);
Result<void> vm_div(Opcode& oc);
Result<void> vm_call(Opcode& oc);
Result<void> vm_selfcall(Opcode& oc);
Result<void> vm_call_lisp(Opcode& oc, Function& fun);
Result<void> vm_call_stdlib(Opcode& oc, StdlibFunction& fun);
Result<void> vm_ret(Opcode& oc);
2024-08-23 20:30:05 +00:00
Result<void> vm_equal(Opcode& oc);
Result<void> vm_less(Opcode& oc);
Result<void> vm_less_equal(Opcode& oc);
2024-08-15 00:18:05 +00:00
Result<void> vm_jump(Opcode& oc);
Result<void> vm_make_closure(Opcode& oc);
Result<void> vm_closure_load(Opcode& oc);
Result<void> vm_global_load(Opcode& oc);
Result<void> vm_global_store(Opcode& oc);
Result<StackFrame> handle_raise(const Continuation& cont);
Result<Value> get(bool is_const, uint64_t idx);
Result<Value> getconst(uint64_t idx);
Result<Value> getreg(uint64_t idx);
Result<StackFrame> setreg(uint64_t idx, const Value& value);
Result<Dict> globals() { return _globals.copy(); }
Result<Array> code() {
auto fun = TRY(_stack.fun());
if (!fun.is<Function>()) return ERROR(TypeMismatch);
return fun.to<Function>()->code();
}
Result<Array> constants() {
auto fun = TRY(_stack.fun());
if (!fun.is<Function>()) return ERROR(TypeMismatch);
return fun.to<Function>()->constants();
}
Result<Array> closure() {
auto fun = TRY(_stack.fun());
if (!fun.is<Function>()) return ERROR(TypeMismatch);
return fun.to<Function>()->closure();
}
Result<String> backtrace(uint64_t indent = 0) {
return _stack.backtrace(indent);
}
private:
StackFrame _stack;
Dict _globals;
Value _res;
};