diff --git a/CMakeLists.txt b/CMakeLists.txt index 81b6b6c..27e1f49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,8 @@ add_library(lib${PROJECT_NAME} SHARED src/bytecode/constant.hh src/vm/instruction.hh src/vm/instruction.cc + src/vm/value.cc + src/vm/value.hh ) target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) diff --git a/doc/OPCODES b/doc/OPCODES index a22f4c2..ece0501 100644 --- a/doc/OPCODES +++ b/doc/OPCODES @@ -86,22 +86,3 @@ Control flow: Error handling: (0xa0~0xaf) ??? - - - - -Internal handling of values ---------------------------- - -## Supported types - Nil 0 - Integer 1 - Float 2 - String 3 - Array 4 - Table 5 - Function 6 - NativePointer 7 - -## Internal format - ??? \ No newline at end of file diff --git a/doc/VM b/doc/VM new file mode 100644 index 0000000..733bbdd --- /dev/null +++ b/doc/VM @@ -0,0 +1,15 @@ +Internal handling of values +--------------------------- + +## Supported types + Nil 0 + Integer 1 + Float 2 + String 3 + Array 4 + Table 5 + Function 6 + NativePointer 7 + +## Internal format + ??? \ No newline at end of file diff --git a/src/vm/value.cc b/src/vm/value.cc new file mode 100644 index 0000000..4d3bc49 --- /dev/null +++ b/src/vm/value.cc @@ -0,0 +1,17 @@ +#include "value.hh" + +#include "../common/overloaded.hh" + +namespace tyche { + +Type Value::type() const +{ + return std::visit(overloaded { + [](std::monostate) { return Type::Nil; }, + [](int32_t) { return Type::Integer; }, + [](float) { return Type::Float; }, + [](std::string const&) { return Type::String; }, + }, value_); +} + +} diff --git a/src/vm/value.hh b/src/vm/value.hh new file mode 100644 index 0000000..9009c4d --- /dev/null +++ b/src/vm/value.hh @@ -0,0 +1,36 @@ +#ifndef TYCHE_VALUE_HH +#define TYCHE_VALUE_HH + +#include +#include + +namespace tyche { + +enum class Type : uint8_t +{ + Nil = 0, Integer, Float, String, Array, Table, Function, NativePointer, +}; + +class Value { +public: + static Value CreateNil() { return Value(std::monostate()); } + 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); } + + [[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_); } + +private: + using Internal = std::variant; + Internal value_; + + explicit Value(Internal const& internal) : value_(internal) {} +}; + +} + +#endif //TYCHE_VALUE_HH