VM basics #5
19
src/vm/vm.cc
19
src/vm/vm.cc
@@ -1,5 +1,7 @@
|
|||||||
#include "vm.hh"
|
#include "vm.hh"
|
||||||
|
|
||||||
|
#include "vm_exceptions.hh"
|
||||||
|
|
||||||
namespace tyche {
|
namespace tyche {
|
||||||
|
|
||||||
void VM::load_bytecode(ByteArray const& ba)
|
void VM::load_bytecode(ByteArray const& ba)
|
||||||
@@ -10,7 +12,22 @@ void VM::load_bytecode(ByteArray const& ba)
|
|||||||
|
|
||||||
void VM::call(size_t n_params)
|
void VM::call(size_t n_params)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO - parameters
|
||||||
|
|
||||||
|
Value f = stack_.pop();
|
||||||
|
if (f.type() != Type::Function)
|
||||||
|
throw VMTypeError(Type::Function, f.type());
|
||||||
|
|
||||||
|
loc_.emplace(f.as_function_id(), 0);
|
||||||
|
stack_.push_fp();
|
||||||
|
run_until_return();
|
||||||
|
stack_.pop_fp();
|
||||||
|
loc_.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VM::run_until_return()
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // tyche
|
} // tyche
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
struct Location { uint32_t function_id; uint32_t pc; };
|
struct Location { uint32_t function_id; uint32_t pc; };
|
||||||
|
|
||||||
|
void run_until_return();
|
||||||
|
|
||||||
Stack stack_;
|
Stack stack_;
|
||||||
Code code_;
|
Code code_;
|
||||||
std::stack<Location> loc_;
|
std::stack<Location> loc_;
|
||||||
|
|||||||
@@ -24,6 +24,13 @@ public:
|
|||||||
explicit VMStackOutOfRange() : VMRuntimeError("Item does not exist in stack") {}
|
explicit VMStackOutOfRange() : VMRuntimeError("Item does not exist in stack") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class VMTypeError : public VMRuntimeError
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit VMTypeError(Type expected, Type found) : VMRuntimeError("Type error") {} // TODO - print types
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //TYCHE_VM_EXCEPTIONS_HH
|
#endif //TYCHE_VM_EXCEPTIONS_HH
|
||||||
|
|||||||
Reference in New Issue
Block a user