diff --git a/runtime/alloc.cpp b/runtime/alloc.cpp new file mode 100644 index 0000000..b1c587e --- /dev/null +++ b/runtime/alloc.cpp @@ -0,0 +1,16 @@ +#include "alloc.hpp" +#include +#include + +using namespace sliger::heap; + +auto Array::at(int32_t index) & -> Value& +{ + if (index >= static_cast(this->values.size()) || index < 0) { + std::cout << std::format( + "index not in range, expected to be in range (0..{}), got: {}", + this->values.size(), index); + exit(1); + } + return values.at(static_cast(index)); +} diff --git a/runtime/alloc.hpp b/runtime/alloc.hpp index a4bc9fe..b75f515 100644 --- a/runtime/alloc.hpp +++ b/runtime/alloc.hpp @@ -13,13 +13,8 @@ namespace sliger::heap { struct Array { std::vector values; - inline auto at(int32_t index)& -> Value& { - if (index >= static_cast(this->values.size()) || index < 0) { - std::cout << std::format("index not in range, expected to be in range (0..{}), got: {}", this->values.size(), index); - exit(1); - } - return values.at(index); - } + + auto at(int32_t index) & -> Value&; }; struct Struct { @@ -40,9 +35,21 @@ template <> struct AllocTypeType { using Type = Struct; }; // clang-format on struct AllocItem { - AllocItem(Value&& val) : type(AllocType::Value), value(val) {} - AllocItem(Array&& val) : type(AllocType::Array), value(val) {} - AllocItem(Struct&& val) : type(AllocType::Struct), value(val) {} + AllocItem(Value&& val) + : type(AllocType::Value) + , value(val) + { + } + AllocItem(Array&& val) + : type(AllocType::Array) + , value(val) + { + } + AllocItem(Struct&& val) + : type(AllocType::Struct) + , value(val) + { + } template inline auto as() & -> AllocTypeType::Type& { @@ -65,26 +72,25 @@ struct AllocItem { std::get::Type>(this->value)); } - inline auto as_value() & -> Value& { + inline auto as_value() & -> Value& { return std::get(this->value); } + inline auto as_value() const& -> const Value& + { return std::get(this->value); } - inline auto as_value() const & -> const Value& { - return std::get(this->value); - } - inline auto as_array() & -> Array& { + inline auto as_array() & -> Array& { return std::get(this->value); } + inline auto as_array() const& -> const Array& + { return std::get(this->value); } - inline auto as_array() const & -> const Array& { - return std::get(this->value); - } - inline auto as_struct() & -> Struct& { + inline auto as_struct() & -> Struct& + { return std::get(this->value); } - inline auto as_struct() const & -> const Struct& { + inline auto as_struct() const& -> const Struct& + { return std::get(this->value); } - AllocType type; std::variant value; }; diff --git a/runtime/value.cpp b/runtime/value.cpp index 12a259a..34d0c4a 100644 --- a/runtime/value.cpp +++ b/runtime/value.cpp @@ -4,6 +4,17 @@ using namespace sliger; +auto String::at(int32_t index) -> int32_t +{ + if (index >= static_cast(this->value.length()) || index < 0) { + std::cout << std::format( + "index not in range, expected to be in range (0..{}), got: {}", + this->value.length() - 1, index); + exit(1); + } + return this->value.at(static_cast(index)); +} + auto Value::to_string() const -> std::string { switch (this->m_type) { diff --git a/runtime/value.hpp b/runtime/value.hpp index e1e3d52..02a8181 100644 --- a/runtime/value.hpp +++ b/runtime/value.hpp @@ -68,13 +68,8 @@ struct Bool { }; struct String { std::string value; - inline auto at(int32_t index) -> int32_t { - if (index >= static_cast(this->value.length()) || index < 0) { - std::cout << std::format("index not in range, expected to be in range (0..{}), got: {}", this->value.length()-1, index); - exit(1); - } - return this->value.at(index); - } + + auto at(int32_t index) -> int32_t; }; struct Ptr { uint32_t value;