slige/runtime/alloc.cpp

33 lines
843 B
C++
Raw Permalink Normal View History

2024-12-13 15:16:11 +00:00
#include "alloc.hpp"
#include <format>
#include <iostream>
2024-12-31 02:38:38 +00:00
#include <string>
2024-12-13 15:16:11 +00:00
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: {}\n",
2024-12-13 15:16:11 +00:00
this->values.size(), index);
exit(1);
}
return values.at(static_cast<size_t>(index));
}
2024-12-31 02:38:38 +00:00
auto Struct::at(const std::string& field) & -> Value&
{
if (this->fields.find(field) == this->fields.end()) {
std::cout << std::format(
"field name not in struct, got: \"{}\"\n", field);
exit(1);
}
return this->fields.at(field);
}
void Struct::assign(const std::string& field, Value&& value)
{
this->fields.insert_or_assign(field, value);
}