.
This commit is contained in:
@@ -42,10 +42,8 @@ static void heap_free_item(HeapValue value)
|
|||||||
break;
|
break;
|
||||||
case TH_ARRAY:
|
case TH_ARRAY:
|
||||||
abort(); // not implemented yet
|
abort(); // not implemented yet
|
||||||
break;
|
|
||||||
case TH_TABLE:
|
case TH_TABLE:
|
||||||
abort(); // not implemented yet
|
abort(); // not implemented yet
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
@@ -75,6 +73,9 @@ HEAP_KEY heap_add_string(Heap* h, const char* value)
|
|||||||
} while (k != kh_end(h->items));
|
} while (k != kh_end(h->items));
|
||||||
|
|
||||||
k = kh_put(HEAP, h->items, key, &ret);
|
k = kh_put(HEAP, h->items, key, &ret);
|
||||||
|
if (ret < 0)
|
||||||
|
out_of_memory();
|
||||||
|
|
||||||
kh_value(h->items, k) = (HeapValue) {
|
kh_value(h->items, k) = (HeapValue) {
|
||||||
.type = TH_STRING,
|
.type = TH_STRING,
|
||||||
.value = { .str = strdup(value) }
|
.value = { .str = strdup(value) }
|
||||||
|
|||||||
42
lib/priv.h
42
lib/priv.h
@@ -7,6 +7,27 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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
|
// UTILS
|
||||||
//
|
//
|
||||||
@@ -20,15 +41,6 @@ void* xrealloc(void* p, size_t n);
|
|||||||
// VALUE
|
// VALUE
|
||||||
//
|
//
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
TYC_TYPE type;
|
|
||||||
union {
|
|
||||||
int32_t i;
|
|
||||||
float f;
|
|
||||||
uint32_t idx;
|
|
||||||
} v;
|
|
||||||
} VALUE;
|
|
||||||
|
|
||||||
TYC_TYPE value_type(VALUE v);
|
TYC_TYPE value_type(VALUE v);
|
||||||
bool type_is_collectable(TYC_TYPE t);
|
bool type_is_collectable(TYC_TYPE t);
|
||||||
|
|
||||||
@@ -46,8 +58,6 @@ VALUE create_value_idx(TYC_TYPE type, uint32_t idx);
|
|||||||
// STACK
|
// STACK
|
||||||
//
|
//
|
||||||
|
|
||||||
typedef struct Stack Stack;
|
|
||||||
|
|
||||||
Stack* stack_new(void);
|
Stack* stack_new(void);
|
||||||
void stack_destroy(Stack* s);
|
void stack_destroy(Stack* s);
|
||||||
|
|
||||||
@@ -71,8 +81,6 @@ size_t stack_collectable_array(Stack const* s, VALUE** values);
|
|||||||
// HEAP ARRAY
|
// HEAP ARRAY
|
||||||
//
|
//
|
||||||
|
|
||||||
typedef struct Array Array;
|
|
||||||
|
|
||||||
Array* array_new(void);
|
Array* array_new(void);
|
||||||
void array_destroy(Array* a);
|
void array_destroy(Array* a);
|
||||||
|
|
||||||
@@ -85,9 +93,7 @@ void array_append(Array* a, VALUE v);
|
|||||||
// HEAP TABLE
|
// HEAP TABLE
|
||||||
//
|
//
|
||||||
|
|
||||||
typedef struct Table Table;
|
Table* table_new(Heap const* heap);
|
||||||
|
|
||||||
Table* table_new(void);
|
|
||||||
void table_destroy(Table* t);
|
void table_destroy(Table* t);
|
||||||
|
|
||||||
size_t table_len(Table* t);
|
size_t table_len(Table* t);
|
||||||
@@ -98,10 +104,6 @@ void table_set(Table* t, VALUE key, VALUE value);
|
|||||||
// HEAP
|
// HEAP
|
||||||
//
|
//
|
||||||
|
|
||||||
typedef struct Heap Heap;
|
|
||||||
|
|
||||||
typedef uint32_t HEAP_KEY;
|
|
||||||
|
|
||||||
Heap* heap_new(void);
|
Heap* heap_new(void);
|
||||||
void heap_destroy(Heap* h);
|
void heap_destroy(Heap* h);
|
||||||
|
|
||||||
|
|||||||
55
lib/table.c
55
lib/table.c
@@ -9,15 +9,17 @@ KHASH_MAP_INIT_STR(TABLE_STR, VALUE)
|
|||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
struct Table {
|
struct Table {
|
||||||
khash_t(TABLE_INT) *tbl_int;
|
khash_t(TABLE_INT)* tbl_int;
|
||||||
khash_t(TABLE_STR) *tbl_str;
|
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));
|
Table* t = xcalloc(1, sizeof(Table));
|
||||||
t->tbl_int = kh_init(TABLE_INT);
|
t->tbl_int = kh_init(TABLE_INT);
|
||||||
t->tbl_str = kh_init(TABLE_STR);
|
t->tbl_str = kh_init(TABLE_STR);
|
||||||
|
t->heap = heap;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,13 +35,13 @@ size_t table_len(Table* t)
|
|||||||
return kh_size(t->tbl_int) + kh_size(t->tbl_str);
|
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)) {
|
switch (value_type(v)) {
|
||||||
case TT_NIL:
|
case TT_NIL:
|
||||||
return 0;
|
return 0;
|
||||||
case TT_INTEGER:
|
case TT_INTEGER:
|
||||||
return value_integer(v);
|
return (uint64_t) value_integer(v);
|
||||||
case TT_REAL: {
|
case TT_REAL: {
|
||||||
uint32_t vv;
|
uint32_t vv;
|
||||||
float f = value_real(v);
|
float f = value_real(v);
|
||||||
@@ -47,15 +49,15 @@ static int64_t value_hash(VALUE v)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TT_STRING_CONST:
|
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:
|
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:
|
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:
|
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:
|
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:
|
case TT_STRING:
|
||||||
default:
|
default:
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
@@ -66,15 +68,42 @@ static int64_t value_hash(VALUE v)
|
|||||||
|
|
||||||
VALUE table_get(Table const* t, VALUE key)
|
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)
|
void table_set(Table* t, VALUE key, VALUE value)
|
||||||
{
|
{
|
||||||
/*
|
int ret;
|
||||||
|
|
||||||
if (value_type(key) == TT_STRING) {
|
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 {
|
} 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;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user