2018-02-10 17:53:07 -08:00
|
|
|
#include "table.h"
|
|
|
|
#include <stdlib.h>
|
2018-02-11 22:50:08 -08:00
|
|
|
#include "util.h"
|
2018-03-17 17:39:44 -07:00
|
|
|
#include "lexer.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-02-10 17:53:07 -08:00
|
|
|
ht_init(&table->table);
|
|
|
|
table->parent = NULL;
|
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
libab_table_entry* libab_table_search_filter(libab_table* table, const char* string, void* data, compare_func compare) {
|
|
|
|
void* to_return = NULL;
|
|
|
|
do {
|
|
|
|
to_return = ht_get_filter(&table->table, string, data, compare);
|
|
|
|
table = table->parent;
|
|
|
|
} while(table && to_return == NULL);
|
|
|
|
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 {
|
|
|
|
to_return = ht_get(&table->table, string);
|
|
|
|
table = table->parent;
|
|
|
|
} while(table && to_return == NULL);
|
|
|
|
return to_return;
|
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
|
|
|
|
#define OP_TYPE_COMPARATOR(NAME, TYPE) int NAME(const void* left, const void* right) {\
|
|
|
|
const libab_table_entry* entry = right;\
|
|
|
|
return entry->variant == ENTRY_OP && entry->data_u.op.type == TYPE;\
|
|
|
|
}
|
|
|
|
|
|
|
|
OP_TYPE_COMPARATOR(_table_compare_prefix, TOKEN_OP_PREFIX)
|
|
|
|
OP_TYPE_COMPARATOR(_table_compare_infix, TOKEN_OP_INFIX)
|
|
|
|
OP_TYPE_COMPARATOR(_table_compare_postfix, TOKEN_OP_POSTFIX)
|
|
|
|
|
|
|
|
libab_operator* libab_table_search_operator(libab_table* table, const char* string, int type) {
|
|
|
|
libab_table_entry* entry = NULL;
|
|
|
|
if(type == TOKEN_OP_PREFIX) {
|
|
|
|
entry = libab_table_search_filter(table, string, NULL, _table_compare_prefix);
|
|
|
|
} else if(type == TOKEN_OP_INFIX) {
|
|
|
|
entry = libab_table_search_filter(table, string, NULL, _table_compare_infix);
|
|
|
|
} else if(type == TOKEN_OP_POSTFIX) {
|
|
|
|
entry = libab_table_search_filter(table, string, NULL, _table_compare_postfix);
|
2018-02-11 23:00:07 -08:00
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
return entry ? &entry->data_u.op : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _table_compare_function(const void* left, const void* right) {
|
|
|
|
const libab_table_entry* entry = right;
|
|
|
|
return entry->variant == ENTRY_FUN;
|
2018-02-11 23:00:07 -08:00
|
|
|
}
|
2018-03-17 17:39:44 -07:00
|
|
|
|
2018-02-11 23:00:07 -08:00
|
|
|
libab_function* libab_table_search_function(libab_table* table, const char* string) {
|
2018-03-17 17:39:44 -07:00
|
|
|
libab_table_entry* entry = libab_table_search_filter(table, string, NULL, _table_compare_function);
|
|
|
|
return entry ? &entry->data_u.function : NULL;
|
2018-02-11 23:00:07 -08:00
|
|
|
}
|
2018-02-11 22:50:08 -08:00
|
|
|
libab_result libab_table_put(libab_table* table, const char* string, libab_table_entry* entry) {
|
|
|
|
return libab_convert_ds_result(ht_put(&table->table, string, entry));
|
|
|
|
}
|
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-02-17 19:08:35 -08:00
|
|
|
void libab_table_free(libab_table* table) {
|
2018-02-17 19:56:18 -08:00
|
|
|
ht_foreach(&table->table, NULL, compare_always, _table_foreach_entry_free);
|
2018-02-10 17:53:07 -08:00
|
|
|
ht_free(&table->table);
|
|
|
|
}
|
2018-02-17 19:53:33 -08:00
|
|
|
void libab_table_entry_free(libab_table_entry* entry) {
|
2018-03-15 19:41:11 -07:00
|
|
|
if(entry->variant == ENTRY_OP) {
|
|
|
|
libab_parsetype_free_recursive(entry->data_u.op.behavior.type);
|
|
|
|
} else if(entry->variant == ENTRY_FUN) {
|
|
|
|
libab_parsetype_free_recursive(entry->data_u.function.behavior.type);
|
|
|
|
}
|
2018-02-17 19:53:33 -08:00
|
|
|
}
|