2024-07-19 01:29:30 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <concepts>
|
|
|
|
#include <cstdint>
|
2024-07-20 21:42:49 +00:00
|
|
|
#include <cstring>
|
2024-07-19 01:29:30 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "arena.hpp"
|
2024-08-03 17:53:14 +00:00
|
|
|
#include "error.hpp"
|
2024-07-19 01:29:30 +00:00
|
|
|
#include "pod.hpp"
|
2024-07-28 15:40:52 +00:00
|
|
|
#include "utf8.hpp"
|
2024-07-19 01:29:30 +00:00
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
|
|
|
|
class Value;
|
2024-07-28 15:40:52 +00:00
|
|
|
class String;
|
2024-08-01 17:56:38 +00:00
|
|
|
class Nil;
|
|
|
|
class Int64;
|
|
|
|
class Float;
|
|
|
|
class Bool;
|
|
|
|
class String;
|
|
|
|
class Symbol;
|
|
|
|
class Syntax;
|
|
|
|
class Pair;
|
|
|
|
class Array;
|
|
|
|
class ByteArray;
|
2024-08-01 21:45:16 +00:00
|
|
|
class Writer;
|
2024-08-01 17:56:38 +00:00
|
|
|
|
|
|
|
short cmp_tag(Tag lhs, Tag rhs);
|
2024-07-19 01:29:30 +00:00
|
|
|
|
|
|
|
class Object {
|
|
|
|
public:
|
|
|
|
virtual Tag tag() = 0;
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const = 0;
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() = 0;
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object*) = 0;
|
2024-07-27 18:40:13 +00:00
|
|
|
virtual ~Object() = default;
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object&) = 0;
|
|
|
|
virtual Result<short> cmp(Nil&) { return cmp_tag(tag(), Tag::Nil); }
|
|
|
|
virtual Result<short> cmp(Int64&) { return cmp_tag(tag(), Tag::Int64); }
|
|
|
|
virtual Result<short> cmp(Float&) { return cmp_tag(tag(), Tag::Float); }
|
|
|
|
virtual Result<short> cmp(Bool&) { return cmp_tag(tag(), Tag::Bool); }
|
|
|
|
virtual Result<short> cmp(String&) { return cmp_tag(tag(), Tag::String); }
|
|
|
|
virtual Result<short> cmp(Symbol&) { return cmp_tag(tag(), Tag::Symbol); }
|
|
|
|
virtual Result<short> cmp(Syntax&) { return cmp_tag(tag(), Tag::Syntax); }
|
|
|
|
virtual Result<short> cmp(Pair&) { return cmp_tag(tag(), Tag::Pair); }
|
|
|
|
virtual Result<short> cmp(Array&) { return cmp_tag(tag(), Tag::Array); }
|
|
|
|
virtual Result<short> cmp(ByteArray&) {
|
2024-08-01 17:56:38 +00:00
|
|
|
return cmp_tag(tag(), Tag::ByteArray);
|
|
|
|
}
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> get(Value& key);
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-07-27 18:40:13 +00:00
|
|
|
Object() = default;
|
|
|
|
Object(const Object&) = delete;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Nil : public Object {
|
|
|
|
public:
|
|
|
|
Nil() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
Nil(Nil&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-27 15:25:44 +00:00
|
|
|
Nil(GcRoot<PodNil>&& val) : _value(std::move(val)) {}
|
2024-07-19 01:29:30 +00:00
|
|
|
|
|
|
|
virtual Tag tag() final { return Tag::Nil; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Nil& rhs) final { return 0; }
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Nil> create(PodNil* obj) { return Nil(TRY(MkGcRoot(obj))); }
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Nil> create() {
|
|
|
|
auto pod = TRY(arena_alloc<PodNil>());
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::Nil;
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Nil(TRY(MkGcRoot(pod)));
|
2024-07-27 15:25:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Nil(std::move(_value)); }
|
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
private:
|
|
|
|
GcRoot<PodNil> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
2024-07-30 22:52:23 +00:00
|
|
|
class Array : public Object {
|
|
|
|
public:
|
|
|
|
Array() {}
|
|
|
|
Array(Array&& rhs) : _value(std::move(rhs._value)) {}
|
|
|
|
Array(GcRoot<PodArray>&& val) : _value(std::move(val)) {}
|
|
|
|
|
|
|
|
Array& operator=(Array&& rhs) {
|
|
|
|
_value = std::move(rhs._value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Tag tag() final { return Tag::Array; }
|
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Array& rhs) final { return 0; }
|
2024-07-30 22:52:23 +00:00
|
|
|
|
|
|
|
virtual void move(Object* obj) final { new (obj) Array(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Array> create(PodArray* obj) {
|
|
|
|
return Array(TRY(MkGcRoot(obj)));
|
2024-07-30 22:52:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Array> create() {
|
|
|
|
auto pod = TRY(arena_alloc<PodArray>());
|
2024-07-30 22:52:23 +00:00
|
|
|
pod->header.tag = Tag::Array;
|
|
|
|
|
|
|
|
pod->size = 0;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Array(TRY(MkGcRoot(pod)));
|
2024-07-30 22:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t size() { return _value->size; }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-30 22:52:23 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Value> get(uint64_t idx);
|
|
|
|
virtual Result<Value> get(Value& key) final;
|
2024-07-30 22:52:23 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Array> append(Value& rhs);
|
2024-08-03 23:33:17 +00:00
|
|
|
template <class V>
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Array> append(const V& value);
|
2024-07-30 22:52:23 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Array> concat(Array& rhs) {
|
2024-07-30 22:52:23 +00:00
|
|
|
uint64_t rhs_size = rhs.size();
|
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(PodObject*)));
|
2024-07-30 22:52:23 +00:00
|
|
|
pod->size = res_size;
|
2024-08-06 19:32:55 +00:00
|
|
|
for (uint64_t i = 0; i < size(); i++) {
|
|
|
|
pod->data[i] = _value->data[i];
|
|
|
|
}
|
|
|
|
for (uint64_t i = size(); i < rhs.size(); i++) {
|
|
|
|
pod->data[i] = rhs._value->data[i];
|
|
|
|
}
|
2024-07-30 22:52:23 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Array(TRY(MkGcRoot(pod)));
|
2024-07-30 22:52:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Array> sub(uint64_t start, uint64_t end) {
|
2024-08-10 10:17:20 +00:00
|
|
|
if (start > end) return ERROR(IndexOutOfRange);
|
2024-07-30 22:52:23 +00:00
|
|
|
uint64_t res_size = end - start;
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(PodObject*)));
|
2024-07-30 22:52:23 +00:00
|
|
|
pod->size = res_size;
|
2024-08-06 19:32:55 +00:00
|
|
|
for (uint64_t i = 0; i < end - start; i++) {
|
|
|
|
pod->data[i] = _value->data[start + i];
|
|
|
|
}
|
2024-07-30 22:52:23 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Array(TRY(MkGcRoot(pod)));
|
2024-07-30 22:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GcRoot<PodArray> _value;
|
|
|
|
};
|
|
|
|
|
2024-07-28 14:42:18 +00:00
|
|
|
class ByteArray : public Object {
|
|
|
|
public:
|
|
|
|
ByteArray() {}
|
|
|
|
ByteArray(ByteArray&& rhs) : _value(std::move(rhs._value)) {}
|
|
|
|
ByteArray(GcRoot<PodByteArray>&& val) : _value(std::move(val)) {}
|
|
|
|
|
|
|
|
ByteArray& operator=(ByteArray&& rhs) {
|
|
|
|
_value = std::move(rhs._value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-07-30 22:52:23 +00:00
|
|
|
virtual Tag tag() final { return Tag::ByteArray; }
|
2024-07-28 14:42:18 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(ByteArray& rhs) final { return 0; }
|
2024-07-28 14:42:18 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final {
|
|
|
|
new (obj) ByteArray(std::move(_value));
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<ByteArray> create(PodByteArray* obj) {
|
|
|
|
return ByteArray(TRY(MkGcRoot(obj)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<ByteArray> create(char* chars, int64_t size) {
|
|
|
|
auto pod = TRY(arena_alloc<PodByteArray>(size * sizeof(char)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::ByteArray;
|
2024-07-28 14:42:18 +00:00
|
|
|
|
|
|
|
memcpy(pod->data, chars, size * sizeof(char32_t));
|
|
|
|
pod->size = size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return ByteArray(TRY(MkGcRoot(pod)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<ByteArray> create(const char* str) {
|
2024-07-28 14:42:18 +00:00
|
|
|
uint64_t size = strlen(str);
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodByteArray>(size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::ByteArray;
|
2024-07-28 14:42:18 +00:00
|
|
|
|
|
|
|
memcpy(pod->data, str, size);
|
|
|
|
pod->size = size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return ByteArray(TRY(MkGcRoot(pod)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<ByteArray> create(String& str);
|
2024-07-28 15:40:52 +00:00
|
|
|
|
2024-07-28 14:42:18 +00:00
|
|
|
uint64_t size() { return _value->size; }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-28 14:42:18 +00:00
|
|
|
|
2024-07-28 15:40:52 +00:00
|
|
|
Result<char> operator[](uint64_t idx) {
|
2024-08-10 10:17:20 +00:00
|
|
|
if (idx >= _value->size) return ERROR(IndexOutOfRange);
|
2024-07-28 14:42:18 +00:00
|
|
|
return _value->data[idx];
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<ByteArray> concat(const char* rhs) {
|
2024-07-28 14:42:18 +00:00
|
|
|
uint64_t rhs_size = strlen(rhs);
|
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodByteArray>(res_size * sizeof(char)));
|
2024-07-28 14:42:18 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data, sizeof(char) * lhs_size);
|
|
|
|
memcpy(pod->data, rhs + lhs_size, sizeof(char) * rhs_size);
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return ByteArray(TRY(MkGcRoot(pod)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<ByteArray> concat(const char* rhs, uint64_t rhs_size) {
|
2024-07-28 14:42:18 +00:00
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodByteArray>(res_size * sizeof(char)));
|
2024-07-28 14:42:18 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data, sizeof(char) * lhs_size);
|
|
|
|
memcpy(pod->data, rhs + lhs_size, sizeof(char) * rhs_size);
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return ByteArray(TRY(MkGcRoot(pod)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<ByteArray> concat(ByteArray& rhs) {
|
2024-07-28 14:42:18 +00:00
|
|
|
uint64_t rhs_size = rhs.size();
|
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodByteArray>(res_size * sizeof(char)));
|
2024-07-28 14:42:18 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data, sizeof(char) * lhs_size);
|
|
|
|
memcpy(pod->data + lhs_size, rhs._value->data, sizeof(char) * rhs_size);
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return ByteArray(TRY(MkGcRoot(pod)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<ByteArray> sub(uint64_t start, uint64_t end) {
|
2024-08-10 10:17:20 +00:00
|
|
|
if (start > end) return ERROR(IndexOutOfRange);
|
2024-07-28 14:42:18 +00:00
|
|
|
uint64_t res_size = end - start;
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodByteArray>(res_size * sizeof(char)));
|
2024-07-28 14:42:18 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data + start, sizeof(char) * res_size);
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return ByteArray(TRY(MkGcRoot(pod)));
|
2024-07-28 14:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GcRoot<PodByteArray> _value;
|
|
|
|
};
|
|
|
|
|
2024-08-01 17:56:38 +00:00
|
|
|
class Dict : public Object {
|
|
|
|
public:
|
2024-08-01 21:45:16 +00:00
|
|
|
friend class Writer;
|
2024-08-01 17:56:38 +00:00
|
|
|
Dict() {}
|
|
|
|
Dict(Dict&& rhs) : _value(std::move(rhs._value)) {}
|
|
|
|
Dict(GcRoot<PodDict>&& val) : _value(std::move(val)) {}
|
|
|
|
|
|
|
|
Dict& operator=(Dict&& rhs) {
|
|
|
|
_value = std::move(rhs._value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Tag tag() final { return Tag::Dict; }
|
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Dict& rhs) final;
|
2024-08-01 17:56:38 +00:00
|
|
|
|
|
|
|
virtual void move(Object* obj) final { new (obj) Dict(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Dict> create(PodDict* obj) { return Dict(TRY(MkGcRoot(obj))); }
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Dict> create() {
|
|
|
|
auto pod = TRY(arena_alloc<PodDict>());
|
2024-08-01 17:56:38 +00:00
|
|
|
pod->header.tag = Tag::Dict;
|
|
|
|
|
|
|
|
pod->size = 0;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Dict(TRY(MkGcRoot(pod)));
|
2024-08-01 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> get(Value& key) final;
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Dict> insert(Value& key, Value& value);
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-01 21:45:16 +00:00
|
|
|
template <class K, class V>
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Dict> insert(const K& key, const V& value);
|
2024-08-01 21:45:16 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Dict> concat(Dict& rhs) {
|
2024-08-01 17:56:38 +00:00
|
|
|
uint64_t rhs_size = rhs.size();
|
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodDict>(2 * res_size * sizeof(PodObject*)));
|
2024-08-01 17:56:38 +00:00
|
|
|
pod->size = res_size;
|
2024-08-06 19:32:55 +00:00
|
|
|
|
|
|
|
for (uint64_t i = 0; i < 2 * size(); i++) {
|
|
|
|
pod->data[i] = _value->data[i];
|
|
|
|
}
|
|
|
|
for (uint64_t i = 2 * size(); i < 2 * rhs.size(); i++) {
|
|
|
|
pod->data[i] = rhs._value->data[i];
|
|
|
|
}
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Dict(TRY(MkGcRoot(pod)));
|
2024-08-01 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<uint64_t> find(Value& key);
|
2024-08-01 21:45:16 +00:00
|
|
|
|
2024-08-01 17:56:38 +00:00
|
|
|
uint64_t size() { return _value->size; }
|
|
|
|
GcRoot<PodDict> _value;
|
|
|
|
};
|
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
class String : public Object {
|
|
|
|
public:
|
2024-07-26 18:32:27 +00:00
|
|
|
String() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
String(String&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-26 18:32:27 +00:00
|
|
|
String(GcRoot<PodString>&& val) : _value(std::move(val)) {}
|
2024-07-27 22:13:59 +00:00
|
|
|
|
|
|
|
String& operator=(String&& rhs) {
|
|
|
|
_value = std::move(rhs._value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
virtual Tag tag() final { return Tag::String; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(String& rhs) final {
|
2024-08-02 22:27:36 +00:00
|
|
|
auto lsize = size();
|
|
|
|
auto rsize = rhs.size();
|
|
|
|
uint64_t i = 0;
|
|
|
|
uint64_t j = 0;
|
|
|
|
while (1) {
|
|
|
|
if (i == lsize && j == lsize) return 0;
|
|
|
|
|
|
|
|
short cmp = short(i == lsize) - short(j == rsize);
|
|
|
|
if (cmp != 0) return cmp;
|
|
|
|
|
|
|
|
char32_t lc = _value->data[i];
|
|
|
|
char32_t rc = rhs._value->data[j];
|
|
|
|
cmp = (lc > rc) - (lc < rc);
|
|
|
|
if (cmp != 0) return cmp;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) String(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<String> create(PodString* obj) {
|
|
|
|
return String(TRY(MkGcRoot(obj)));
|
2024-07-26 18:32:27 +00:00
|
|
|
}
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<String> create(char32_t* chars, int64_t size) {
|
|
|
|
auto pod = TRY(arena_alloc<PodString>(size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::String;
|
2024-07-20 21:42:49 +00:00
|
|
|
|
2024-07-28 23:14:30 +00:00
|
|
|
memcpy(pod->data, chars, size * sizeof(char32_t));
|
|
|
|
pod->size = size;
|
2024-07-20 21:42:49 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return String(TRY(MkGcRoot(pod)));
|
2024-07-20 21:42:49 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<String> create(const char* str) {
|
2024-07-27 00:01:38 +00:00
|
|
|
uint64_t size = strlen(str);
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodString>(size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::String;
|
2024-07-27 00:01:38 +00:00
|
|
|
|
2024-07-28 23:14:30 +00:00
|
|
|
for (uint64_t i = 0; i < size; i++) pod->data[i] = str[i];
|
|
|
|
pod->size = size;
|
2024-07-27 00:01:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return String(TRY(MkGcRoot(pod)));
|
2024-07-27 00:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t size() { return _value->size; }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-27 00:01:38 +00:00
|
|
|
|
|
|
|
Result<char32_t> operator[](uint64_t idx) {
|
2024-08-10 10:17:20 +00:00
|
|
|
if (idx >= _value->size) return ERROR(IndexOutOfRange);
|
2024-07-27 00:01:38 +00:00
|
|
|
return _value->data[idx];
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<String> concat(const char* rhs) {
|
2024-07-27 22:13:59 +00:00
|
|
|
uint64_t rhs_size = strlen(rhs);
|
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodString>(res_size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::String;
|
2024-07-27 22:13:59 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data, sizeof(char32_t) * lhs_size);
|
|
|
|
for (uint64_t i = 0; i < rhs_size; i++) pod->data[lhs_size + i] = rhs[i];
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return String(TRY(MkGcRoot(pod)));
|
2024-07-27 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<String> concat(const char32_t* rhs, uint64_t rhs_size) {
|
2024-07-27 22:13:59 +00:00
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodString>(res_size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::String;
|
2024-07-27 22:13:59 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data, sizeof(char32_t) * lhs_size);
|
|
|
|
for (uint64_t i = 0; i < rhs_size; i++) pod->data[lhs_size + i] = rhs[i];
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return String(TRY(MkGcRoot(pod)));
|
2024-07-27 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<String> concat(String& rhs) {
|
2024-07-27 22:13:59 +00:00
|
|
|
uint64_t rhs_size = rhs.size();
|
|
|
|
uint64_t lhs_size = size();
|
|
|
|
uint64_t res_size = lhs_size + rhs_size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodString>(res_size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::String;
|
2024-07-27 22:13:59 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data, sizeof(char32_t) * lhs_size);
|
|
|
|
memcpy(pod->data + lhs_size, rhs._value->data, sizeof(char32_t) * rhs_size);
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return String(TRY(MkGcRoot(pod)));
|
2024-07-27 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<String> sub(uint64_t start, uint64_t end) {
|
2024-08-10 10:17:20 +00:00
|
|
|
if (start > end) return ERROR(IndexOutOfRange);
|
2024-07-27 22:13:59 +00:00
|
|
|
uint64_t res_size = end - start;
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodString>(res_size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::String;
|
2024-07-27 22:13:59 +00:00
|
|
|
pod->size = res_size;
|
|
|
|
memcpy(pod->data, _value->data + start, sizeof(char32_t) * res_size);
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return String(TRY(MkGcRoot(pod)));
|
2024-07-27 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
friend class Symbol;
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-26 18:32:27 +00:00
|
|
|
GcRoot<PodString> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Symbol : public Object {
|
|
|
|
public:
|
2024-07-26 18:32:27 +00:00
|
|
|
Symbol() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
Symbol(Symbol&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-26 18:32:27 +00:00
|
|
|
Symbol(GcRoot<PodSymbol>&& val) : _value(std::move(val)) {}
|
2024-07-19 01:29:30 +00:00
|
|
|
virtual Tag tag() final { return Tag::Symbol; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Symbol& rhs) final {
|
2024-08-04 00:03:01 +00:00
|
|
|
auto lsize = size();
|
|
|
|
auto rsize = rhs.size();
|
|
|
|
uint64_t i = 0;
|
|
|
|
uint64_t j = 0;
|
|
|
|
while (1) {
|
|
|
|
if (i == lsize && j == lsize) return 0;
|
|
|
|
|
|
|
|
short cmp = short(i == lsize) - short(j == rsize);
|
|
|
|
if (cmp != 0) return cmp;
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-08-04 00:03:01 +00:00
|
|
|
char32_t lc = _value->data[i];
|
|
|
|
char32_t rc = rhs._value->data[j];
|
|
|
|
cmp = (lc > rc) - (lc < rc);
|
|
|
|
if (cmp != 0) return cmp;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-08-10 10:17:20 +00:00
|
|
|
virtual Result<short> cmp(const char* rhs) final {
|
|
|
|
auto lsize = size();
|
|
|
|
auto rsize = strlen(rhs);
|
|
|
|
uint64_t i = 0;
|
|
|
|
uint64_t j = 0;
|
|
|
|
while (1) {
|
|
|
|
if (i == lsize && j == lsize) return 0;
|
|
|
|
|
|
|
|
short cmp = short(i == lsize) - short(j == rsize);
|
|
|
|
if (cmp != 0) return cmp;
|
|
|
|
|
|
|
|
char32_t lc = _value->data[i];
|
|
|
|
char32_t rc = rhs[j];
|
|
|
|
cmp = (lc > rc) - (lc < rc);
|
|
|
|
if (cmp != 0) return cmp;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Symbol(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Symbol> create(PodSymbol* obj) {
|
|
|
|
return Symbol(TRY(MkGcRoot(obj)));
|
2024-07-26 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Symbol> create(char32_t* chars, int64_t size) {
|
|
|
|
auto pod = TRY(arena_alloc<PodSymbol>(size * sizeof(char32_t)));
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::Symbol;
|
2024-07-20 21:42:49 +00:00
|
|
|
|
2024-07-28 23:14:30 +00:00
|
|
|
memcpy(pod->data, chars, size * sizeof(char32_t));
|
2024-07-20 21:42:49 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Symbol(TRY(MkGcRoot(pod)));
|
2024-07-20 21:42:49 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Symbol> create(const char* str) {
|
2024-08-04 00:03:01 +00:00
|
|
|
uint64_t size = strlen(str);
|
2024-08-09 22:45:06 +00:00
|
|
|
auto pod = TRY(arena_alloc<PodSymbol>(size * sizeof(char32_t)));
|
2024-08-04 00:03:01 +00:00
|
|
|
pod->header.tag = Tag::Symbol;
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < size; i++) pod->data[i] = str[i];
|
|
|
|
pod->size = size;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Symbol(TRY(MkGcRoot(pod)));
|
2024-08-04 00:03:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Symbol> create(String& rhs);
|
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-28 16:54:04 +00:00
|
|
|
uint64_t size() { return _value->size; }
|
|
|
|
|
|
|
|
Result<char32_t> operator[](uint64_t idx) {
|
2024-08-10 10:17:20 +00:00
|
|
|
if (idx >= _value->size) return ERROR(IndexOutOfRange);
|
2024-07-28 16:54:04 +00:00
|
|
|
return _value->data[idx];
|
|
|
|
}
|
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-26 18:32:27 +00:00
|
|
|
GcRoot<PodSymbol> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Syntax : public Object {
|
|
|
|
public:
|
2024-07-26 18:32:27 +00:00
|
|
|
Syntax() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
Syntax(Syntax&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-26 18:32:27 +00:00
|
|
|
Syntax(GcRoot<PodSyntax>&& val) : _value(std::move(val)) {}
|
2024-07-19 01:29:30 +00:00
|
|
|
Syntax(String filename, String modulename, Value expression);
|
|
|
|
virtual Tag tag() final { return Tag::Syntax; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Syntax& rhs) final { return 0; }
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Syntax(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Syntax> create(PodSyntax* obj) {
|
|
|
|
return Syntax(TRY(MkGcRoot(obj)));
|
2024-07-26 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Value> get_value();
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-26 18:32:27 +00:00
|
|
|
GcRoot<PodSyntax> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Pair : public Object {
|
|
|
|
public:
|
2024-07-26 18:32:27 +00:00
|
|
|
Pair() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
Pair(Pair&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-26 18:32:27 +00:00
|
|
|
Pair(GcRoot<PodPair>&& val) : _value(std::move(val)) {}
|
2024-07-19 01:29:30 +00:00
|
|
|
virtual Tag tag() final { return Tag::Pair; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Pair& rhs) final { return 0; }
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Pair(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Pair> create(PodPair* obj) { return Pair(TRY(MkGcRoot(obj))); }
|
|
|
|
static Result<Pair> create(Value& first, Value& rest);
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Value> first();
|
|
|
|
Result<Value> rest();
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-26 18:32:27 +00:00
|
|
|
GcRoot<PodPair> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Int64 : public Object {
|
|
|
|
public:
|
2024-08-01 17:56:38 +00:00
|
|
|
friend class Float;
|
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
Int64() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
Int64(Int64&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-27 15:25:44 +00:00
|
|
|
Int64(GcRoot<PodInt64>&& val) : _value(std::move(val)) {}
|
2024-07-19 01:29:30 +00:00
|
|
|
virtual Tag tag() final { return Tag::Int64; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Int64& rhs) final {
|
2024-08-01 17:56:38 +00:00
|
|
|
return (_value->value > rhs._value->value) -
|
|
|
|
(_value->value < rhs._value->value);
|
|
|
|
}
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Float& rhs) final;
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Int64(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Int64> create(PodInt64* obj) {
|
|
|
|
return Int64(TRY(MkGcRoot(obj)));
|
2024-07-27 15:25:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Int64> create(double val) {
|
|
|
|
auto pod = TRY(arena_alloc<PodInt64>());
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::Int64;
|
2024-07-27 15:25:44 +00:00
|
|
|
pod->value = val;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Int64(TRY(MkGcRoot(pod)));
|
2024-07-27 15:25:44 +00:00
|
|
|
}
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
int64_t value() { return _value->value; }
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-27 15:25:44 +00:00
|
|
|
GcRoot<PodInt64> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Float : public Object {
|
|
|
|
public:
|
2024-08-01 17:56:38 +00:00
|
|
|
friend class Int64;
|
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
Float() {}
|
2024-07-27 18:40:13 +00:00
|
|
|
Float(Float&& rhs) : _value(std::move(rhs._value)) {}
|
2024-07-27 15:25:44 +00:00
|
|
|
Float(GcRoot<PodFloat>&& val) : _value(std::move(val)) {}
|
2024-07-19 01:29:30 +00:00
|
|
|
virtual Tag tag() final { return Tag::Float; }
|
2024-07-27 15:25:44 +00:00
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Float& rhs) final {
|
2024-08-01 17:56:38 +00:00
|
|
|
return (_value->value > rhs._value->value) -
|
|
|
|
(_value->value < rhs._value->value);
|
|
|
|
}
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Int64& rhs) final;
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Float(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Float> create(PodFloat* obj) {
|
|
|
|
return Float(TRY(MkGcRoot(obj)));
|
2024-07-27 15:25:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Float> create(double val) {
|
|
|
|
auto pod = TRY(arena_alloc<PodFloat>());
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::Float;
|
2024-07-27 15:25:44 +00:00
|
|
|
pod->value = val;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Float(TRY(MkGcRoot(pod)));
|
2024-07-27 15:25:44 +00:00
|
|
|
}
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
double value() { return _value->value; }
|
2024-07-19 01:29:30 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-27 15:25:44 +00:00
|
|
|
GcRoot<PodFloat> _value;
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
2024-07-27 18:40:13 +00:00
|
|
|
class Bool : public Object {
|
|
|
|
public:
|
|
|
|
Bool() {}
|
|
|
|
Bool(Bool&& rhs) : _value(std::move(rhs._value)) {}
|
|
|
|
Bool(GcRoot<PodBool>&& val) : _value(std::move(val)) {}
|
|
|
|
|
|
|
|
virtual Tag tag() final { return Tag::Bool; }
|
|
|
|
virtual PodObject* pod() final { return _value.get(); }
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<short> cmp(Object& rhs) final { return -TRY(rhs.cmp(*this)); }
|
|
|
|
virtual Result<short> cmp(Bool& rhs) final { return 0; }
|
2024-07-27 18:40:13 +00:00
|
|
|
|
2024-07-29 23:14:09 +00:00
|
|
|
virtual void move(Object* obj) final { new (obj) Bool(std::move(_value)); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Bool> create(PodBool* obj) { return Bool(TRY(MkGcRoot(obj))); }
|
2024-07-27 18:40:13 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Bool> create(bool val) {
|
|
|
|
auto pod = TRY(arena_alloc<PodBool>());
|
2024-07-28 23:14:30 +00:00
|
|
|
pod->header.tag = Tag::Bool;
|
2024-07-27 18:40:13 +00:00
|
|
|
pod->value = val;
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return Bool(TRY(MkGcRoot(pod)));
|
2024-07-27 18:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool value() { return _value->value; }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
virtual Result<Value> copy() const final;
|
2024-07-27 18:40:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
GcRoot<PodBool> _value;
|
|
|
|
};
|
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
// note: this class doesn't perform proper destruction of objects in some cases
|
|
|
|
class Value {
|
|
|
|
public:
|
|
|
|
Value() { new (buf) Nil(); }
|
|
|
|
~Value() { ((Object*)buf)->~Object(); }
|
2024-07-27 00:01:38 +00:00
|
|
|
Value(Value&& val) {
|
2024-07-29 23:14:09 +00:00
|
|
|
((Object*)val.buf)->move((Object*)buf);
|
2024-07-27 00:01:38 +00:00
|
|
|
new (val.buf) Nil();
|
|
|
|
}
|
2024-07-26 18:32:27 +00:00
|
|
|
Value(const Value&) = delete;
|
2024-07-19 01:29:30 +00:00
|
|
|
|
|
|
|
template <class T>
|
2024-07-26 18:32:27 +00:00
|
|
|
Value(T&& obj)
|
|
|
|
requires std::derived_from<T, Object>
|
2024-07-19 01:29:30 +00:00
|
|
|
{
|
2024-07-26 18:32:27 +00:00
|
|
|
new (buf) T(std::move(obj));
|
2024-07-19 01:29:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
Value& operator=(Value&& val) {
|
2024-07-28 22:37:43 +00:00
|
|
|
((Object*)buf)->~Object();
|
2024-07-29 23:14:09 +00:00
|
|
|
((Object*)val.buf)->move((Object*)buf);
|
2024-07-27 15:25:44 +00:00
|
|
|
new (val.buf) Nil();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Value> create(PodObject* obj);
|
2024-07-26 19:20:13 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Value> create(const int64_t& value) {
|
|
|
|
return Value(TRY(Int64::create(value)));
|
2024-08-01 21:45:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
static Result<Value> create(const char* value) {
|
|
|
|
return Value(TRY(String::create(value)));
|
2024-08-02 22:27:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
template <class T>
|
|
|
|
bool is() {
|
|
|
|
return dynamic_cast<T*>((Object*)buf) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T* to() {
|
|
|
|
return dynamic_cast<T*>((Object*)buf);
|
|
|
|
}
|
|
|
|
|
2024-07-27 15:25:44 +00:00
|
|
|
PodObject* pod() { return ((Object*)buf)->pod(); }
|
2024-07-28 16:54:04 +00:00
|
|
|
Tag tag() { return ((Object*)buf)->tag(); }
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
Object& operator*() { return *(Object*)(buf); }
|
|
|
|
Object* operator->() { return (Object*)(buf); }
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<short> cmp(Value& rhs) {
|
|
|
|
return ((Object*)buf)->cmp(*(Object*)rhs.buf);
|
2024-08-01 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Value> get(Value& key) { return ((Object*)buf)->get(key); }
|
|
|
|
Result<Value> get(int64_t key) {
|
|
|
|
Value k = TRY(Int64::create(key));
|
|
|
|
return ((Object*)buf)->get(k);
|
2024-08-03 17:53:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 12:43:59 +00:00
|
|
|
// TODO: cmp() probably doesn't need arena parameter
|
|
|
|
// Result<bool> operator==(Value& rhs) { return TRY(cmp(rhs)) == 0; }
|
|
|
|
// Result<bool> operator!=(Value& rhs) { return TRY(cmp(rhs)) != 0; }
|
2024-08-01 17:56:38 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Value> copy() const { return ((Object*)buf)->copy(); }
|
2024-07-26 18:32:27 +00:00
|
|
|
|
2024-07-19 01:29:30 +00:00
|
|
|
private:
|
2024-07-26 18:32:27 +00:00
|
|
|
uint8_t buf[24];
|
2024-07-19 01:29:30 +00:00
|
|
|
};
|
|
|
|
|
2024-07-26 18:32:27 +00:00
|
|
|
Result<Value> syntax_unwrap(Value& val);
|
2024-07-27 15:25:44 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Value> reverse(Value& val);
|
2024-07-28 22:37:43 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<void> debug_print(String& val);
|
|
|
|
Result<void> debug_print(Value& val);
|
2024-08-01 21:45:16 +00:00
|
|
|
|
|
|
|
template <class K, class V>
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Dict> Dict::insert(const K& key, const V& value) {
|
|
|
|
Value k = TRY(Value::create(key));
|
|
|
|
Value v = TRY(Value::create(value));
|
2024-08-01 21:45:16 +00:00
|
|
|
|
2024-08-09 22:45:06 +00:00
|
|
|
return insert(k, v);
|
2024-08-01 21:45:16 +00:00
|
|
|
}
|
2024-08-03 23:33:17 +00:00
|
|
|
|
|
|
|
template <class V>
|
2024-08-09 22:45:06 +00:00
|
|
|
Result<Array> Array::append(const V& value) {
|
|
|
|
Value v = TRY(Value::create(value));
|
|
|
|
return append(v);
|
2024-08-03 23:33:17 +00:00
|
|
|
}
|