fix format header error

This commit is contained in:
SimonFJ20 2024-12-13 16:16:11 +01:00
parent 8839e19857
commit 70473f353a
4 changed files with 56 additions and 28 deletions

16
runtime/alloc.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "alloc.hpp"
#include <format>
#include <iostream>
using namespace sliger::heap;
auto Array::at(int32_t index) & -> Value&
{
if (index >= static_cast<int32_t>(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<size_t>(index));
}

View File

@ -13,13 +13,8 @@ namespace sliger::heap {
struct Array { struct Array {
std::vector<Value> values; std::vector<Value> values;
inline auto at(int32_t index)& -> Value& {
if (index >= static_cast<int32_t>(this->values.size()) || index < 0) { auto at(int32_t index) & -> Value&;
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);
}
}; };
struct Struct { struct Struct {
@ -40,9 +35,21 @@ template <> struct AllocTypeType<AllocType::Struct> { using Type = Struct; };
// clang-format on // clang-format on
struct AllocItem { struct AllocItem {
AllocItem(Value&& val) : type(AllocType::Value), value(val) {} AllocItem(Value&& val)
AllocItem(Array&& val) : type(AllocType::Array), value(val) {} : type(AllocType::Value)
AllocItem(Struct&& val) : type(AllocType::Struct), value(val) {} , value(val)
{
}
AllocItem(Array&& val)
: type(AllocType::Array)
, value(val)
{
}
AllocItem(Struct&& val)
: type(AllocType::Struct)
, value(val)
{
}
template <AllocType AT> inline auto as() & -> AllocTypeType<AT>::Type& template <AllocType AT> inline auto as() & -> AllocTypeType<AT>::Type&
{ {
@ -65,26 +72,25 @@ struct AllocItem {
std::get<typename AllocTypeType<AT>::Type>(this->value)); std::get<typename AllocTypeType<AT>::Type>(this->value));
} }
inline auto as_value() & -> Value& { inline auto as_value() & -> Value& { return std::get<Value>(this->value); }
inline auto as_value() const& -> const Value&
{
return std::get<Value>(this->value); return std::get<Value>(this->value);
} }
inline auto as_value() const & -> const Value& { inline auto as_array() & -> Array& { return std::get<Array>(this->value); }
return std::get<Value>(this->value); inline auto as_array() const& -> const Array&
} {
inline auto as_array() & -> Array& {
return std::get<Array>(this->value); return std::get<Array>(this->value);
} }
inline auto as_array() const & -> const Array& { inline auto as_struct() & -> Struct&
return std::get<Array>(this->value); {
}
inline auto as_struct() & -> Struct& {
return std::get<Struct>(this->value); return std::get<Struct>(this->value);
} }
inline auto as_struct() const & -> const Struct& { inline auto as_struct() const& -> const Struct&
{
return std::get<Struct>(this->value); return std::get<Struct>(this->value);
} }
AllocType type; AllocType type;
std::variant<Value, Array, Struct> value; std::variant<Value, Array, Struct> value;
}; };

View File

@ -4,6 +4,17 @@
using namespace sliger; using namespace sliger;
auto String::at(int32_t index) -> int32_t
{
if (index >= static_cast<int32_t>(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<size_t>(index));
}
auto Value::to_string() const -> std::string auto Value::to_string() const -> std::string
{ {
switch (this->m_type) { switch (this->m_type) {

View File

@ -68,13 +68,8 @@ struct Bool {
}; };
struct String { struct String {
std::string value; std::string value;
inline auto at(int32_t index) -> int32_t {
if (index >= static_cast<int32_t>(this->value.length()) || index < 0) { auto at(int32_t index) -> int32_t;
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);
}
}; };
struct Ptr { struct Ptr {
uint32_t value; uint32_t value;