From aefc2ff9ad1b2071225393755fd22089098e2600 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Wed, 13 May 2026 11:24:33 -0500 Subject: [PATCH] . --- lib/heap.c | 5 +++-- lib/priv.h | 42 +++++++++++++++++++++------------------- lib/table.c | 55 ++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/lib/heap.c b/lib/heap.c index 3017e93..4d4f99c 100644 --- a/lib/heap.c +++ b/lib/heap.c @@ -42,10 +42,8 @@ static void heap_free_item(HeapValue value) break; case TH_ARRAY: abort(); // not implemented yet - break; case TH_TABLE: abort(); // not implemented yet - break; default: __builtin_unreachable(); } @@ -75,6 +73,9 @@ HEAP_KEY heap_add_string(Heap* h, const char* value) } while (k != kh_end(h->items)); k = kh_put(HEAP, h->items, key, &ret); + if (ret < 0) + out_of_memory(); + kh_value(h->items, k) = (HeapValue) { .type = TH_STRING, .value = { .str = strdup(value) } diff --git a/lib/priv.h b/lib/priv.h index 8c34d60..7d1b02d 100644 --- a/lib/priv.h +++ b/lib/priv.h @@ -7,6 +7,27 @@ #include #include +// +// TYPE DECLARATION +// + +typedef struct { + TYC_TYPE type; + union { + int32_t i; + float f; + uint32_t idx; + } v; +} VALUE; + +typedef struct Stack Stack; +typedef struct Array Array; +typedef struct Table Table; +typedef struct Heap Heap; + +typedef uint32_t HEAP_KEY; +typedef uint64_t TABLE_HASH; + // // UTILS // @@ -20,15 +41,6 @@ void* xrealloc(void* p, size_t n); // VALUE // -typedef struct { - TYC_TYPE type; - union { - int32_t i; - float f; - uint32_t idx; - } v; -} VALUE; - TYC_TYPE value_type(VALUE v); bool type_is_collectable(TYC_TYPE t); @@ -46,8 +58,6 @@ VALUE create_value_idx(TYC_TYPE type, uint32_t idx); // STACK // -typedef struct Stack Stack; - Stack* stack_new(void); void stack_destroy(Stack* s); @@ -71,8 +81,6 @@ size_t stack_collectable_array(Stack const* s, VALUE** values); // HEAP ARRAY // -typedef struct Array Array; - Array* array_new(void); void array_destroy(Array* a); @@ -85,9 +93,7 @@ void array_append(Array* a, VALUE v); // HEAP TABLE // -typedef struct Table Table; - -Table* table_new(void); +Table* table_new(Heap const* heap); void table_destroy(Table* t); size_t table_len(Table* t); @@ -98,10 +104,6 @@ void table_set(Table* t, VALUE key, VALUE value); // HEAP // -typedef struct Heap Heap; - -typedef uint32_t HEAP_KEY; - Heap* heap_new(void); void heap_destroy(Heap* h); diff --git a/lib/table.c b/lib/table.c index af330a1..d52cc3f 100644 --- a/lib/table.c +++ b/lib/table.c @@ -9,15 +9,17 @@ KHASH_MAP_INIT_STR(TABLE_STR, VALUE) #pragma GCC diagnostic pop struct Table { - khash_t(TABLE_INT) *tbl_int; - khash_t(TABLE_STR) *tbl_str; + khash_t(TABLE_INT)* tbl_int; + khash_t(TABLE_STR)* tbl_str; + Heap const* heap; }; -Table* table_new(void) +Table* table_new(Heap const* heap) { Table* t = xcalloc(1, sizeof(Table)); t->tbl_int = kh_init(TABLE_INT); t->tbl_str = kh_init(TABLE_STR); + t->heap = heap; return t; } @@ -33,13 +35,13 @@ size_t table_len(Table* t) return kh_size(t->tbl_int) + kh_size(t->tbl_str); } -static int64_t value_hash(VALUE v) +static TABLE_HASH value_hash(VALUE v) { switch (value_type(v)) { case TT_NIL: return 0; case TT_INTEGER: - return value_integer(v); + return (uint64_t) value_integer(v); case TT_REAL: { uint32_t vv; float f = value_real(v); @@ -47,15 +49,15 @@ static int64_t value_hash(VALUE v) break; } case TT_STRING_CONST: - return (int64_t) value_idx(v) | ((int64_t) 1 << 33); + return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 33); case TT_ARRAY: - return (int64_t) value_idx(v) | ((int64_t) 1 << 34); + return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 34); case TT_TABLE: - return (int64_t) value_idx(v) | ((int64_t) 1 << 35); + return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 35); case TT_FUNCTION: - return (int64_t) value_idx(v) | ((int64_t) 1 << 36); + return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 36); case TT_NATIVE_PTR: - return (int64_t) value_idx(v) | ((int64_t) 1 << 37); + return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 37); case TT_STRING: default: __builtin_unreachable(); @@ -66,15 +68,42 @@ static int64_t value_hash(VALUE v) VALUE table_get(Table const* t, VALUE key) { + if (value_type(key) == TT_STRING) { + const char* skey; + if (heap_get_string(t->heap, value_idx(key), &skey) != T_OK) + abort(); + khiter_t k = kh_get(TABLE_STR, t->tbl_str, skey); + if (k == kh_end(t->tbl_str)) + __builtin_unreachable(); + return kh_value(t->tbl_str, k); + + } else { + TABLE_HASH hash = value_hash(key); + khiter_t k = kh_get(TABLE_INT, t->tbl_int, hash); + if (k == kh_end(t->tbl_int)) + __builtin_unreachable(); + return kh_value(t->tbl_int, k); + } } void table_set(Table* t, VALUE key, VALUE value) { - /* + int ret; + if (value_type(key) == TT_STRING) { - khiter_t k = kh_get(TABLE_STR, t->tbl_str, value_); + const char* skey; + if (heap_get_string(t->heap, value_idx(key), &skey) != T_OK) + abort(); + khiter_t k = kh_put(TABLE_STR, t->tbl_str, skey, &ret); + if (ret < 0) + out_of_memory(); + kh_value(t->tbl_str, k) = value; } else { + TABLE_HASH hash = value_hash(key); + khiter_t k = kh_put(TABLE_INT, t->tbl_int, hash, &ret); + if (ret < 0) + out_of_memory(); + kh_value(t->tbl_int, k) = value; } - */ }