.
This commit is contained in:
@@ -70,6 +70,8 @@ add_library(lib${PROJECT_NAME} SHARED
|
|||||||
src/bytecode/constant.hh
|
src/bytecode/constant.hh
|
||||||
src/vm/instruction.hh
|
src/vm/instruction.hh
|
||||||
src/vm/instruction.cc
|
src/vm/instruction.cc
|
||||||
|
src/vm/value.cc
|
||||||
|
src/vm/value.hh
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
|
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
|
||||||
|
|||||||
19
doc/OPCODES
19
doc/OPCODES
@@ -86,22 +86,3 @@ Control flow:
|
|||||||
|
|
||||||
Error handling: (0xa0~0xaf)
|
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
|
|
||||||
???
|
|
||||||
15
doc/VM
Normal file
15
doc/VM
Normal file
@@ -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
|
||||||
|
???
|
||||||
17
src/vm/value.cc
Normal file
17
src/vm/value.cc
Normal file
@@ -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_);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
36
src/vm/value.hh
Normal file
36
src/vm/value.hh
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef TYCHE_VALUE_HH
|
||||||
|
#define TYCHE_VALUE_HH
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
|
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<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_); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
using Internal = std::variant<std::monostate, int32_t, float, std::string>;
|
||||||
|
Internal value_;
|
||||||
|
|
||||||
|
explicit Value(Internal const& internal) : value_(internal) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //TYCHE_VALUE_HH
|
||||||
Reference in New Issue
Block a user