This commit is contained in:
Andre Wagner
2026-04-29 15:18:30 -05:00
parent 635596c31d
commit 30bfb38e9a
5 changed files with 70 additions and 19 deletions

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