VM basics #5
@@ -75,6 +75,8 @@ add_library(lib${PROJECT_NAME} SHARED
|
||||
src/vm/stack.cc
|
||||
src/vm/stack.hh
|
||||
src/vm/vm_exceptions.hh
|
||||
src/vm/vm.cc
|
||||
src/vm/vm.hh
|
||||
)
|
||||
|
||||
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
|
||||
namespace tyche {
|
||||
|
||||
void Code::import_bytecode(ByteArray incoming)
|
||||
FunctionId Code::import_bytecode(ByteArray incoming)
|
||||
{
|
||||
Bytecode bc(std::move(incoming));
|
||||
// TODO - adjust function calls, constants
|
||||
|
||||
bytecode_ = std::move(bc);
|
||||
|
||||
return 0; // TODO
|
||||
}
|
||||
|
||||
std::string Code::disassemble() const
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#ifndef TYCHE_CODE_HH
|
||||
#define TYCHE_CODE_HH
|
||||
|
||||
#include "value.hh"
|
||||
#include "../bytecode/bytecode.hh"
|
||||
|
||||
namespace tyche {
|
||||
|
||||
class Code {
|
||||
public:
|
||||
void import_bytecode(ByteArray incoming);
|
||||
FunctionId import_bytecode(ByteArray incoming);
|
||||
|
||||
[[nodiscard]] std::string disassemble() const;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ Type Value::type() const
|
||||
[](int32_t) { return Type::Integer; },
|
||||
[](float) { return Type::Float; },
|
||||
[](std::string const&) { return Type::String; },
|
||||
[](Function const&) { return Type::Function; },
|
||||
}, value_);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,17 @@
|
||||
|
||||
namespace tyche {
|
||||
|
||||
using FunctionId = uint32_t;
|
||||
|
||||
enum class Type : uint8_t
|
||||
{
|
||||
Nil = 0, Integer, Float, String, Array, Table, Function, NativePointer,
|
||||
};
|
||||
|
||||
|
||||
class Value {
|
||||
struct Function { FunctionId f_id; };
|
||||
|
||||
public:
|
||||
Value() : value_(std::monostate()) {}
|
||||
|
||||
@@ -19,15 +24,17 @@ public:
|
||||
static Value CreateInteger(int32_t v) { return Value(v); }
|
||||
static Value CreateFloat(float f) { return Value(f); }
|
||||
static Value CreateString(std::string const& str) { return Value(str); }
|
||||
static Value CreateFunctionId(FunctionId f_id) { return Value(Function { f_id }); }
|
||||
|
||||
[[nodiscard]] Type type() const;
|
||||
|
||||
[[nodiscard]] int32_t as_integer() const { return std::get<int32_t>(value_); }
|
||||
[[nodiscard]] float as_float() const { return std::get<float>(value_); }
|
||||
[[nodiscard]] std::string as_string() const { return std::get<std::string>(value_); }
|
||||
[[nodiscard]] FunctionId as_function_id() const { return std::get<Function>(value_).f_id; }
|
||||
|
||||
private:
|
||||
using Internal = std::variant<std::monostate, int32_t, float, std::string>;
|
||||
using Internal = std::variant<std::monostate, int32_t, float, std::string, Function>;
|
||||
Internal value_;
|
||||
|
||||
explicit Value(Internal const& internal) : value_(internal) {}
|
||||
|
||||
16
src/vm/vm.cc
Normal file
16
src/vm/vm.cc
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "vm.hh"
|
||||
|
||||
namespace tyche {
|
||||
|
||||
void VM::load_bytecode(ByteArray const& ba)
|
||||
{
|
||||
FunctionId f_id = code_.import_bytecode(ba);
|
||||
stack_.push(Value::CreateFunctionId(f_id));
|
||||
}
|
||||
|
||||
void VM::call(size_t n_params)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
} // tyche
|
||||
25
src/vm/vm.hh
Normal file
25
src/vm/vm.hh
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef TYCHE_VM_HH
|
||||
#define TYCHE_VM_HH
|
||||
|
||||
#include "code.hh"
|
||||
#include "stack.hh"
|
||||
|
||||
namespace tyche {
|
||||
|
||||
class VM {
|
||||
public:
|
||||
void load_bytecode(ByteArray const& ba);
|
||||
|
||||
void call(size_t n_params);
|
||||
|
||||
private:
|
||||
struct Location { uint32_t function_id; uint32_t pc; };
|
||||
|
||||
Stack stack_;
|
||||
Code code_;
|
||||
std::stack<Location> loc_;
|
||||
};
|
||||
|
||||
} // tyche
|
||||
|
||||
#endif //TYCHE_VM_HH
|
||||
Reference in New Issue
Block a user