2018-02-10 17:53:07 -08:00
|
|
|
#include "table.h"
|
2018-03-17 17:39:44 -07:00
|
|
|
#include "lexer.h"
|
2018-04-21 14:09:01 -07:00
|
|
|
#include "util.h"
|
|
|
|
#include <stdlib.h>
|
2018-02-10 17:53:07 -08:00
|
|
|
|
2018-02-17 19:08:35 -08:00
|
|
|
void libab_table_init(libab_table* table) {
|
2018-03-24 20:47:34 -07:00
|
|
|
libab_trie_init(&table->trie);
|
2018-04-24 11:06:21 -07:00
|
|
|
libab_ref_null(&table->parent);
|
2018-04-21 14:09:01 -07:00
|
|
|
}
|
|
|
|
libab_table_entry* libab_table_search_filter(libab_table* table,
|
|
|
|
const char* string, void* data,
|
|
|
|
compare_func compare) {
|
2018-03-17 17:39:44 -07:00
|
|
|
void* to_return = NULL;
|
|
|
|
do {
|
2018-03-24 20:47:34 -07:00
|
|
|
const ll* matches = libab_trie_get(&table->trie, string);
|
|
|
|
to_return = ll_find(matches, data, compare);
|
2018-04-24 11:06:21 -07:00
|
|
|
table = libab_ref_get(&table->parent);
|
2018-04-21 14:09:01 -07:00
|
|
|
} while (table && to_return == NULL);
|
2018-03-17 17:39:44 -07:00
|
|
|
return to_return;
|
|
|
|
}
|
|
|
|
libab_table_entry* libab_table_search(libab_table* table, const char* string) {
|
2018-02-10 17:57:24 -08:00
|
|
|
void* to_return = NULL;
|
|
|
|
do {
|
2018-03-24 20:47:34 -07:00
|
|
|
to_return = ll_head(libab_trie_get(&table->trie, string));
|
2018-04-24 11:06:21 -07:00
|
|
|
table = libab_ref_get(&table->parent);
|
2018-04-21 14:09:01 -07:00
|
|
|
} while (table && to_return == NULL);
|
2018-02-10 17:57:24 -08:00
|
|
|
return to_return;
|
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
#define OP_TYPE_COMPARATOR(NAME, TYPE) \
|
|
|
|
int NAME(const void* left, const void* right) { \
|
|
|
|
const libab_table_entry* entry = right; \
|
2018-05-17 14:53:48 -07:00
|
|
|
return entry->variant == ENTRY_OP && entry->data_u.op.variant == TYPE; \
|
2018-04-21 14:09:01 -07:00
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
|
2018-05-16 14:05:35 -07:00
|
|
|
OP_TYPE_COMPARATOR(libab_table_compare_op_prefix, OPERATOR_PREFIX)
|
|
|
|
OP_TYPE_COMPARATOR(libab_table_compare_op_infix, OPERATOR_INFIX)
|
|
|
|
OP_TYPE_COMPARATOR(libab_table_compare_op_postfix, OPERATOR_POSTFIX)
|
|
|
|
|
|
|
|
int libab_table_compare_value(const void* left, const void* right) {
|
|
|
|
const libab_table_entry* entry = right;
|
|
|
|
return entry->variant == ENTRY_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int libab_table_compare_basetype(const void* left, const void* right) {
|
|
|
|
const libab_table_entry* entry = right;
|
|
|
|
return entry->variant == ENTRY_BASETYPE;
|
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
|
2018-05-27 23:10:59 -07:00
|
|
|
int libab_table_compare_type_param(const void* left, const void* right) {
|
|
|
|
const libab_table_entry* entry = right;
|
|
|
|
return entry->variant == ENTRY_TYPE_PARAM;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_operator* libab_table_search_operator(libab_table* table,
|
|
|
|
const char* string, int type) {
|
2018-03-17 17:39:44 -07:00
|
|
|
libab_table_entry* entry = NULL;
|
2018-04-21 14:09:01 -07:00
|
|
|
if (type == OPERATOR_PREFIX) {
|
|
|
|
entry = libab_table_search_filter(table, string, NULL,
|
2018-05-16 14:05:35 -07:00
|
|
|
libab_table_compare_op_prefix);
|
2018-04-21 14:09:01 -07:00
|
|
|
} else if (type == OPERATOR_INFIX) {
|
|
|
|
entry = libab_table_search_filter(table, string, NULL,
|
2018-05-16 14:05:35 -07:00
|
|
|
libab_table_compare_op_infix);
|
2018-04-21 14:09:01 -07:00
|
|
|
} else if (type == OPERATOR_PREFIX) {
|
|
|
|
entry = libab_table_search_filter(table, string, NULL,
|
2018-05-16 14:05:35 -07:00
|
|
|
libab_table_compare_op_postfix);
|
2018-02-11 23:00:07 -08:00
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
return entry ? &entry->data_u.op : NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_basetype* libab_table_search_basetype(libab_table* table,
|
|
|
|
const char* string) {
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_table_entry* entry = libab_table_search_filter(
|
|
|
|
table, string, NULL, libab_table_compare_basetype);
|
2018-04-17 12:07:22 -07:00
|
|
|
return entry ? entry->data_u.basetype : NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_table_search_value(libab_table* table, const char* string,
|
|
|
|
libab_ref* ref) {
|
|
|
|
libab_table_entry* entry = libab_table_search_filter(
|
|
|
|
table, string, NULL, libab_table_compare_value);
|
|
|
|
if (entry) {
|
2018-04-26 20:14:21 -07:00
|
|
|
libab_ref_copy(&entry->data_u.value, ref);
|
|
|
|
} else {
|
|
|
|
libab_ref_null(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 23:10:59 -07:00
|
|
|
void libab_table_search_type_param(libab_table* table, const char* string,
|
|
|
|
libab_ref* ref) {
|
|
|
|
libab_table_entry* entry = libab_table_search_filter(
|
|
|
|
table, string, NULL, libab_table_compare_type_param);
|
|
|
|
if (entry) {
|
|
|
|
libab_ref_copy(&entry->data_u.type_param, ref);
|
|
|
|
} else {
|
|
|
|
libab_ref_null(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_table_put(libab_table* table, const char* string,
|
|
|
|
libab_table_entry* entry) {
|
2018-03-24 20:47:34 -07:00
|
|
|
return libab_trie_put(&table->trie, string, entry);
|
2018-02-11 22:50:08 -08:00
|
|
|
}
|
2018-02-17 19:56:18 -08:00
|
|
|
int _table_foreach_entry_free(void* data, va_list args) {
|
|
|
|
libab_table_entry_free(data);
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-24 11:06:21 -07:00
|
|
|
void libab_table_set_parent(libab_table* table, libab_ref* parent) {
|
|
|
|
libab_ref_free(&table->parent);
|
|
|
|
libab_ref_copy(parent, &table->parent);
|
|
|
|
}
|
2018-05-31 19:29:14 -07:00
|
|
|
void libab_table_clear(libab_table* table) {
|
|
|
|
libab_trie_clear(&table->trie);
|
|
|
|
}
|
2018-02-17 19:08:35 -08:00
|
|
|
void libab_table_free(libab_table* table) {
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_trie_foreach(&table->trie, NULL, compare_always,
|
|
|
|
_table_foreach_entry_free);
|
2018-03-24 20:47:34 -07:00
|
|
|
libab_trie_free(&table->trie);
|
2018-04-24 11:06:21 -07:00
|
|
|
libab_ref_free(&table->parent);
|
2018-02-10 17:53:07 -08:00
|
|
|
}
|
2018-02-17 19:53:33 -08:00
|
|
|
void libab_table_entry_free(libab_table_entry* entry) {
|
2018-04-21 14:09:01 -07:00
|
|
|
if (entry->variant == ENTRY_OP) {
|
2018-04-17 11:22:48 -07:00
|
|
|
libab_operator_free(&entry->data_u.op);
|
2018-04-21 14:09:01 -07:00
|
|
|
} else if (entry->variant == ENTRY_BASETYPE) {
|
2018-04-17 12:07:22 -07:00
|
|
|
libab_basetype_free(entry->data_u.basetype);
|
2018-04-26 20:14:21 -07:00
|
|
|
} else if (entry->variant == ENTRY_VALUE) {
|
|
|
|
libab_ref_free(&entry->data_u.value);
|
2018-05-27 23:10:59 -07:00
|
|
|
} else if (entry->variant == ENTRY_TYPE_PARAM) {
|
|
|
|
libab_ref_free(&entry->data_u.type_param);
|
2018-03-15 19:41:11 -07:00
|
|
|
}
|
2018-02-17 19:53:33 -08:00
|
|
|
}
|