More const qualifiers added

This commit is contained in:
Konstantin Nazarov 2024-08-10 18:56:42 +01:00
parent f1fd15171c
commit a195b8d0e3
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 23 additions and 19 deletions

View file

@ -156,18 +156,18 @@ Result<void> debug_print(Value& val) {
return debug_print(s);
}
Result<Value> Array::get(uint64_t idx) {
Result<Value> Array::get(uint64_t idx) const {
if (idx >= _value->size) return ERROR(IndexOutOfRange);
auto val = _value->data[idx].get();
return Value::create(val);
}
Result<Value> Array::get(Value& key) {
Result<Value> Array::get(const Value& key) const {
if (!key.is<Int64>()) return ERROR(TypeMismatch);
return get(key.to<Int64>()->value());
}
Result<Array> Array::append(Value& rhs) {
Result<Array> Array::append(const Value& rhs) const {
uint64_t res_size = size() + 1;
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(OffPtr<PodObject>)));
@ -187,9 +187,9 @@ short cmp_tag(Tag lhs, Tag rhs) {
return 0;
}
Result<Value> Dict::get(Value& key) {
Result<Value> Dict::get(const Value& key) const {
auto pos = TRY(find(key));
if (pos > size()) return ERROR(KeyError);
if (pos >= size()) return ERROR(KeyError);
auto k = TRY(Value::create(_value->data[pos * 2].get()));
if (TRY(k.cmp(key)) != 0) {
@ -232,7 +232,7 @@ Result<Dict> Dict::insert(const Value& key, const Value& value) {
return Dict(TRY(MkGcRoot(pod)));
}
Result<uint64_t> Dict::find(const Value& key) {
Result<uint64_t> Dict::find(const Value& key) const {
uint64_t left = 0;
uint64_t right = size();
uint64_t pos = (left + right) / 2;
@ -284,4 +284,6 @@ Result<short> Dict::cmp(const Dict& rhs) const {
return 0;
}
Result<Value> Object::get(Value& key) { return ERROR(TypeMismatch); }
Result<Value> Object::get(const Value& key) const {
return ERROR(TypeMismatch);
}

View file

@ -68,7 +68,7 @@ class Object {
virtual Result<short> cmp(const ByteArray&) const {
return cmp_tag(tag(), Tag::ByteArray);
}
virtual Result<Value> get(Value& key);
virtual Result<Value> get(const Value& key) const;
Object() = default;
Object(const Object&) = delete;
@ -138,15 +138,15 @@ class Array : public Object {
return Array(TRY(MkGcRoot(pod)));
}
uint64_t size() { return _value->size; }
uint64_t size() const { return _value->size; }
virtual Result<Value> copy() const final;
Result<Value> get(uint64_t idx);
virtual Result<Value> get(Value& key) final;
Result<Value> get(uint64_t idx) const;
virtual Result<Value> get(const Value& key) const final;
Result<Array> append(Value& rhs);
Result<Array> append(const Value& rhs) const;
template <class V>
Result<Array> append(const V& value);
Result<Array> append(const V& value) const;
Result<Array> concat(Array& rhs) {
uint64_t rhs_size = rhs.size();
@ -323,7 +323,7 @@ class Dict : public Object {
virtual Result<Value> copy() const final;
virtual Result<Value> get(Value& key) final;
virtual Result<Value> get(const Value& key) const final;
Result<Dict> insert(const Value& key, const Value& value);
@ -349,7 +349,7 @@ class Dict : public Object {
}
private:
Result<uint64_t> find(const Value& key);
Result<uint64_t> find(const Value& key) const;
uint64_t size() const { return _value->size; }
GcRoot<PodDict> _value;
@ -820,12 +820,12 @@ class Value {
}
template <class T>
bool is() {
bool is() const {
return dynamic_cast<T*>((Object*)buf) != nullptr;
}
template <class T>
T* to() {
T* to() const {
return dynamic_cast<T*>((Object*)buf);
}
@ -871,7 +871,7 @@ Result<Dict> Dict::insert(const K& key, const V& value) {
}
template <class V>
Result<Array> Array::append(const V& value) {
Result<Array> Array::append(const V& value) const {
Value v = TRY(Value::create(value));
return append(v);
}

View file

@ -23,7 +23,7 @@ struct Context {
return reg;
}
Result<int64_t> add_const(Value& val) {
Result<int64_t> add_const(const Value& val) {
auto idx = constants_dict.get(val);
if (idx.has_value()) {
@ -64,6 +64,7 @@ Result<Value> Compiler::compile(Value& expr) {
TRY(compile_expr(context, expr));
return ERROR(NotImplemented);
}
Result<Expression> Compiler::compile_expr(Context& context, Value& expr) {
switch (expr.tag()) {
case Tag::Pair:
@ -91,6 +92,7 @@ Result<Expression> Compiler::compile_primop(Context& context, Symbol& op,
Array code = TRY(Array::create());
uint64_t reg = context.alloc_reg();
int64_t zero = TRY(context.add_const(TRY(Value::create((int64_t)0))));
std::cout << "load-immediate r" << reg << ", " << 0 << "\n";
code = TRY(add_opcode(code, Oc::LoadImmediate, {0, (int64_t)reg}, {0, 0}));