VM basics #5

Merged
andre merged 9 commits from vm into master 2026-04-30 13:34:49 -05:00
7 changed files with 60 additions and 6 deletions
Showing only changes of commit 3d4ce8cd15 - Show all commits

View File

@@ -75,6 +75,8 @@ add_library(lib${PROJECT_NAME} SHARED
src/vm/stack.cc src/vm/stack.cc
src/vm/stack.hh src/vm/stack.hh
src/vm/vm_exceptions.hh src/vm/vm_exceptions.hh
src/vm/vm.cc
src/vm/vm.hh
) )
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})

View File

@@ -4,12 +4,14 @@
namespace tyche { namespace tyche {
void Code::import_bytecode(ByteArray incoming) FunctionId Code::import_bytecode(ByteArray incoming)
{ {
Bytecode bc(std::move(incoming)); Bytecode bc(std::move(incoming));
// TODO - adjust function calls, constants // TODO - adjust function calls, constants
bytecode_ = std::move(bc); bytecode_ = std::move(bc);
return 0; // TODO
} }
std::string Code::disassemble() const std::string Code::disassemble() const

View File

@@ -1,13 +1,14 @@
#ifndef TYCHE_CODE_HH #ifndef TYCHE_CODE_HH
#define TYCHE_CODE_HH #define TYCHE_CODE_HH
#include "value.hh"
#include "../bytecode/bytecode.hh" #include "../bytecode/bytecode.hh"
namespace tyche { namespace tyche {
class Code { class Code {
public: public:
void import_bytecode(ByteArray incoming); FunctionId import_bytecode(ByteArray incoming);
[[nodiscard]] std::string disassemble() const; [[nodiscard]] std::string disassemble() const;

View File

@@ -11,6 +11,7 @@ Type Value::type() const
[](int32_t) { return Type::Integer; }, [](int32_t) { return Type::Integer; },
[](float) { return Type::Float; }, [](float) { return Type::Float; },
[](std::string const&) { return Type::String; }, [](std::string const&) { return Type::String; },
[](Function const&) { return Type::Function; },
}, value_); }, value_);
} }

View File

@@ -6,12 +6,17 @@
namespace tyche { namespace tyche {
using FunctionId = uint32_t;
enum class Type : uint8_t enum class Type : uint8_t
{ {
Nil = 0, Integer, Float, String, Array, Table, Function, NativePointer, Nil = 0, Integer, Float, String, Array, Table, Function, NativePointer,
}; };
class Value { class Value {
struct Function { FunctionId f_id; };
public: public:
Value() : value_(std::monostate()) {} Value() : value_(std::monostate()) {}
@@ -19,15 +24,17 @@ public:
static Value CreateInteger(int32_t v) { return Value(v); } static Value CreateInteger(int32_t v) { return Value(v); }
static Value CreateFloat(float f) { return Value(f); } static Value CreateFloat(float f) { return Value(f); }
static Value CreateString(std::string const& str) { return Value(str); } 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]] Type type() const;
[[nodiscard]] int32_t as_integer() const { return std::get<int32_t>(value_); } [[nodiscard]] int32_t as_integer() const { return std::get<int32_t>(value_); }
[[nodiscard]] float as_float() const { return std::get<float>(value_); } [[nodiscard]] float as_float() const { return std::get<float>(value_); }
[[nodiscard]] std::string as_string() const { return std::get<std::string>(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: 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_; Internal value_;
explicit Value(Internal const& internal) : value_(internal) {} explicit Value(Internal const& internal) : value_(internal) {}

16
src/vm/vm.cc Normal file
View 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
View 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