diff --git a/CMakeLists.txt b/CMakeLists.txt index ca26996..8b89896 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/src/vm/code.cc b/src/vm/code.cc index 83fe958..450963c 100644 --- a/src/vm/code.cc +++ b/src/vm/code.cc @@ -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 diff --git a/src/vm/code.hh b/src/vm/code.hh index 1a86df2..2925be3 100644 --- a/src/vm/code.hh +++ b/src/vm/code.hh @@ -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; diff --git a/src/vm/value.cc b/src/vm/value.cc index 4d3bc49..b051120 100644 --- a/src/vm/value.cc +++ b/src/vm/value.cc @@ -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_); } diff --git a/src/vm/value.hh b/src/vm/value.hh index 8039233..4b7ef4a 100644 --- a/src/vm/value.hh +++ b/src/vm/value.hh @@ -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(value_); } - [[nodiscard]] float as_float() const { return std::get(value_); } - [[nodiscard]] std::string as_string() const { return std::get(value_); } + [[nodiscard]] int32_t as_integer() const { return std::get(value_); } + [[nodiscard]] float as_float() const { return std::get(value_); } + [[nodiscard]] std::string as_string() const { return std::get(value_); } + [[nodiscard]] FunctionId as_function_id() const { return std::get(value_).f_id; } private: - using Internal = std::variant; + using Internal = std::variant; Internal value_; explicit Value(Internal const& internal) : value_(internal) {} diff --git a/src/vm/vm.cc b/src/vm/vm.cc new file mode 100644 index 0000000..ac5fe27 --- /dev/null +++ b/src/vm/vm.cc @@ -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 diff --git a/src/vm/vm.hh b/src/vm/vm.hh new file mode 100644 index 0000000..5bfe52d --- /dev/null +++ b/src/vm/vm.hh @@ -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 loc_; +}; + +} // tyche + +#endif //TYCHE_VM_HH