apfl/src/value.c

493 lines
12 KiB
C
Raw Normal View History

#include <assert.h>
#include "apfl.h"
#include "internal.h"
#include "hashmap.h"
struct apfl_dict_data {
unsigned refcount;
apfl_hashmap map;
};
static apfl_hash value_hash(const struct apfl_value);
static bool
dict_keys_eq(void *opaque, const void *a, const void *b)
{
(void)opaque;
return apfl_value_eq(*((struct apfl_value *) a), *((struct apfl_value *) b));
}
static apfl_hash
dict_calc_hash(void *opaque, const void *key)
{
(void)opaque;
return value_hash(*(const struct apfl_value *)key);
}
static void
dict_destroy_key_or_value(void *opaque, void *kv)
{
(void)opaque;
apfl_value_deinit(kv);
}
static bool
dict_copy_key_or_value(void *opaque, void *dest, const void *src)
{
(void)opaque;
return apfl_value_copy(dest, *(struct apfl_value *)src);
}
static const struct apfl_hashmap_callbacks dict_hashmap_callbacks = {
.opaque = NULL,
.keys_eq = dict_keys_eq,
.calc_hash = dict_calc_hash,
.destroy_key = dict_destroy_key_or_value,
.destroy_value = dict_destroy_key_or_value,
.copy_key = dict_copy_key_or_value,
.copy_value = dict_copy_key_or_value,
};
static apfl_hashmap
new_hashmap_for_dict(void)
{
return apfl_hashmap_new(
dict_hashmap_callbacks,
sizeof(struct apfl_value),
sizeof(struct apfl_value)
);
}
void
apfl_dict_unref(apfl_dict dict)
{
if (dict == NULL || !apfl_refcount_dec(&dict->refcount)) {
return;
}
apfl_hashmap_destroy(dict->map);
free(dict);
}
static void
print(unsigned indent, FILE *out, struct apfl_value value, bool skip_first_indent)
{
struct apfl_string_view sv;
unsigned first_indent = skip_first_indent ? 0 : indent;
switch (value.type) {
case APFL_VALUE_NIL:
apfl_print_indented(first_indent, out, "nil");
return;
case APFL_VALUE_BOOLEAN:
apfl_print_indented(first_indent, out, value.boolean ? "true" : "false");
return;
case APFL_VALUE_NUMBER:
apfl_print_indented(first_indent, out, "%f", value.number);
return;
case APFL_VALUE_STRING:
sv = apfl_string_view_from(value.string);
apfl_print_indented(first_indent, out, "\"" APFL_STR_FMT "\"", APFL_STR_FMT_ARGS(sv));
return;
case APFL_VALUE_LIST:
if (value.list->len == 0) {
apfl_print_indented(first_indent, out, "[]");
return;
}
apfl_print_indented(first_indent, out, "[\n");
for (size_t i = 0; i < value.list->len; i++) {
print(indent+1, out, value.list->items[i], false);
apfl_print_indented(indent, out, "\n");
}
apfl_print_indented(indent, out, "]");
return;
case APFL_VALUE_DICT:
if (apfl_hashmap_count(value.dict->map) == 0) {
apfl_print_indented(first_indent, out, "[->]");
return;
}
apfl_hashmap_cursor cur = apfl_hashmap_get_cursor(value.dict->map);
if (cur == NULL) {
return; // TODO: Handle failure in a better way
}
apfl_print_indented(first_indent, out, "[\n");
for (; !apfl_hashmap_cursor_is_end(cur); apfl_hashmap_cursor_next(cur)) {
struct apfl_value k, v;
if (!apfl_hashmap_cursor_get_key(cur, &k)) {
apfl_hashmap_cursor_destroy(cur);
return; // TODO: Handle failure in a better way
}
if (!apfl_hashmap_cursor_get_value(cur, &v)) {
apfl_hashmap_cursor_destroy(cur);
apfl_value_deinit(&k);
return; // TODO: Handle failure in a better way
}
print(indent+1, out, k, false);
apfl_value_deinit(&k);
fprintf(out, " -> ");
print(indent+1, out, v, true);
apfl_value_deinit(&v);
fprintf(out, "\n");
}
apfl_hashmap_cursor_destroy(cur);
apfl_print_indented(indent, out, "]");
return;
}
fprintf(out, "Unknown value? (%d)\n", (int)value.type);
}
struct apfl_value
apfl_value_move(struct apfl_value *src)
{
struct apfl_value out = *src;
src->type = APFL_VALUE_NIL;
return out;
}
bool
apfl_value_copy(struct apfl_value *dst, struct apfl_value src)
{
*dst = src;
switch (src.type) {
case APFL_VALUE_NIL:
case APFL_VALUE_BOOLEAN:
case APFL_VALUE_NUMBER:
// Nothing to do
goto ok;
case APFL_VALUE_STRING:
dst->string = apfl_refcounted_string_incref(src.string);
goto ok;
case APFL_VALUE_LIST:
assert(src.list != NULL);
assert(dst->list == src.list);
src.list->refcount++;
goto ok;
case APFL_VALUE_DICT:
assert(src.dict != NULL);
assert(dst->dict == src.dict);
src.dict->refcount++;
goto ok;
}
assert(false);
ok:
return true;
}
void
apfl_value_print(struct apfl_value value, FILE *out)
{
print(0, out, value, false);
fprintf(out, "\n");
}
static bool
list_eq(const struct apfl_list *a, const struct apfl_list *b)
{
if (a == b) {
return true;
}
if (a->len != b->len) {
return true;
}
for (size_t i = 0; i < a->len; i++) {
if (!apfl_value_eq(a->items[i], b->items[i])) {
return false;
}
}
return true;
}
static bool
dict_eq_inner(apfl_hashmap_cursor cur, const apfl_dict b)
{
size_t total = 0;
for (; !apfl_hashmap_cursor_is_end(cur); apfl_hashmap_cursor_next(cur)) {
struct apfl_value key;
struct apfl_value val;
// TODO: It's rather ugly that we simply return false when getting key/value fails.
if (!apfl_hashmap_cursor_get_key(cur, &key)) {
assert(false);
return false;
}
if (!apfl_hashmap_cursor_get_value(cur, &val)) {
apfl_value_deinit(&key);
assert(false);
return false;
}
struct apfl_value other_val;
if (!apfl_hashmap_get(b->map, &key, &other_val)) {
apfl_value_deinit(&key);
apfl_value_deinit(&val);
return false;
}
bool eq = apfl_value_eq(val, other_val);
apfl_value_deinit(&key);
apfl_value_deinit(&val);
apfl_value_deinit(&other_val);
if (!eq) {
return false;
}
total++;
}
return total == apfl_hashmap_count(b->map);
}
static bool
dict_eq(const apfl_dict a, const apfl_dict b)
{
if (a == b) {
return true;
}
apfl_hashmap_cursor cur = apfl_hashmap_get_cursor(a->map);
if (cur == NULL) {
assert(false); // TODO: It's rather ugly that this can fail and that we return false then!
return false;
}
bool eq = dict_eq_inner(cur, b);
apfl_hashmap_cursor_destroy(cur);
return eq;
}
bool
apfl_value_eq(const struct apfl_value a, const struct apfl_value b)
{
if (a.type != b.type) {
return false;
}
switch (a.type) {
case APFL_VALUE_NIL:
return true;
case APFL_VALUE_BOOLEAN:
return a.boolean == b.boolean;
case APFL_VALUE_NUMBER:
return a.number == b.number;
case APFL_VALUE_STRING:
return apfl_string_eq(a.string, b.string);
case APFL_VALUE_LIST:
return list_eq(a.list, b.list);
case APFL_VALUE_DICT:
return dict_eq(a.dict, b.dict);
}
assert(false);
return false;
}
struct apfl_list *
apfl_list_new(void)
{
struct apfl_list *list = ALLOC(struct apfl_list);
if (list == NULL) {
return NULL;
}
*list = (struct apfl_list) {
.refcount = 1,
.items = NULL,
.len = 0,
.cap = 0,
};
return list;
}
void
apfl_list_deinit(struct apfl_list *list)
{
DEINIT_LIST(list->items, list->len, apfl_value_deinit);
list->refcount = 0;
list->cap = 0;
}
struct apfl_editable_dict_data {
apfl_hashmap map;
};
apfl_editable_dict
apfl_editable_dict_new(void)
{
apfl_editable_dict ed = ALLOC(struct apfl_editable_dict_data);
if (ed == NULL) {
return NULL;
}
if ((ed->map = new_hashmap_for_dict()) == NULL) {
free(ed);
return NULL;
}
return ed;
}
apfl_editable_dict
apfl_editable_dict_new_from_dict(apfl_dict dict)
{
if (dict->refcount == 0) {
return NULL;
}
apfl_editable_dict ed = ALLOC(struct apfl_editable_dict_data);
if (ed == NULL) {
return NULL;
}
if (dict->refcount == 1) {
// We're the only one having a reference to the dict. We can directly use it's hashmap!
MOVEPTR(ed->map, dict->map);
} else {
// There are other places referencing the dict, we need to create a shallow copy first.
if ((ed->map = apfl_hashmap_copy(dict->map)) == NULL) {
free(ed);
return NULL;
}
}
apfl_dict_unref(dict);
return ed;
}
bool
apfl_editable_dict_set(apfl_editable_dict ed, struct apfl_value key, struct apfl_value value)
{
if (ed == NULL || ed->map == NULL) {
return false;
}
bool ok = apfl_hashmap_set(ed->map, &key, &value);
apfl_value_deinit(&key);
apfl_value_deinit(&value);
return ok;
}
void
apfl_editable_dict_delete(apfl_editable_dict ed, struct apfl_value key)
{
if (ed == NULL || ed->map == NULL) {
return;
}
apfl_hashmap_delete(ed->map, &key);
apfl_value_deinit(&key);
}
apfl_dict
apfl_editable_dict_finalize(apfl_editable_dict ed)
{
if (ed == NULL || ed->map == NULL) {
return NULL;
}
apfl_dict dict = ALLOC(struct apfl_dict_data);
if (dict == NULL) {
apfl_editable_dict_destroy(ed);
return NULL;
}
dict->refcount = 1;
MOVEPTR(dict->map, ed->map);
free(ed);
return dict;
}
void
apfl_editable_dict_destroy(apfl_editable_dict ed)
{
if (ed == NULL) {
return;
}
apfl_hashmap_destroy(ed->map);
free(ed);
}
void
apfl_value_deinit(struct apfl_value *value)
{
switch (value->type) {
case APFL_VALUE_NIL:
case APFL_VALUE_BOOLEAN:
case APFL_VALUE_NUMBER:
goto ok;
case APFL_VALUE_STRING:
apfl_refcounted_string_unref(value->string);
value->string = NULL;
goto ok;
case APFL_VALUE_LIST:
if (apfl_refcount_dec(&value->list->refcount)) {
DESTROY(value->list, apfl_list_deinit);
}
goto ok;
case APFL_VALUE_DICT:
apfl_dict_unref(value->dict);
value->dict = NULL;
goto ok;
}
assert(false);
ok:
value->type = APFL_VALUE_NIL;
}
static apfl_hash
value_hash(const struct apfl_value value)
{
apfl_hash hash = apfl_hash_fnv1a(&value.type, sizeof(enum apfl_value_type));
struct apfl_string_view sv;
switch (value.type) {
case APFL_VALUE_NIL:
goto ok;
case APFL_VALUE_BOOLEAN:
hash = apfl_hash_fnv1a_add(&value.boolean, sizeof(bool), hash);
goto ok;
case APFL_VALUE_NUMBER:
hash = apfl_hash_fnv1a_add(&value.number, sizeof(apfl_number), hash);
goto ok;
case APFL_VALUE_STRING:
sv = apfl_string_view_from(value.string);
hash = apfl_hash_fnv1a_add(sv.bytes, sv.len, hash);
goto ok;
case APFL_VALUE_LIST:
for (size_t i = 0; i < value.list->len; i++) {
apfl_hash item_hash = value_hash(value.list->items[i]);
hash = apfl_hash_fnv1a_add(&item_hash, sizeof(apfl_hash), hash);
}
goto ok;
case APFL_VALUE_DICT:
// TODO: This results in all dictionaries having the same hash. Since
// it's rather unusual to have dictionaries as keys, this is fine
// for now, but should be improved nonetheless!
goto ok;
}
assert(false);
ok:
return hash;
}