Add functions for basetypes and their entries in tables.
This commit is contained in:
@@ -7,4 +7,6 @@ void libab_basetype_init(libab_basetype* basetype,
|
||||
basetype->params = params;
|
||||
basetype->count = n;
|
||||
}
|
||||
void libab_basetype_free(libab_basetype* basetype) {
|
||||
|
||||
}
|
||||
|
||||
@@ -126,6 +126,25 @@ libab_result libab_register_function(libab* ab, const char* name, const char* ty
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result libab_register_basetype(libab* ab, const char* name, libab_basetype* basetype) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_table_entry* new_entry;
|
||||
if((new_entry = malloc(sizeof(*new_entry)))) {
|
||||
new_entry->variant = ENTRY_BASETYPE;
|
||||
new_entry->data_u.basetype = basetype;
|
||||
}
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = libab_table_put(&ab->table, name, new_entry);
|
||||
}
|
||||
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
free(new_entry);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result libab_free(libab* ab) {
|
||||
libab_table_free(&ab->table);
|
||||
libab_parser_free(&ab->parser);
|
||||
|
||||
14
src/table.c
14
src/table.c
@@ -55,6 +55,18 @@ libab_function* libab_table_search_function(libab_table* table, const char* stri
|
||||
libab_table_entry* entry = libab_table_search_filter(table, string, NULL, _table_compare_function);
|
||||
return entry ? &entry->data_u.function : NULL;
|
||||
}
|
||||
|
||||
int _table_compare_basetype(const void* left, const void* right) {
|
||||
const libab_table_entry* entry = right;
|
||||
return entry->variant == ENTRY_BASETYPE;
|
||||
}
|
||||
|
||||
libab_basetype* libab_table_search_basetype(libab_table* table, const char* string) {
|
||||
libab_table_entry* entry = libab_table_search_filter(table, string, NULL, _table_compare_basetype);
|
||||
return entry ? entry->data_u.basetype : NULL;
|
||||
}
|
||||
|
||||
|
||||
libab_result libab_table_put(libab_table* table, const char* string, libab_table_entry* entry) {
|
||||
return libab_trie_put(&table->trie, string, entry);
|
||||
}
|
||||
@@ -72,5 +84,7 @@ void libab_table_entry_free(libab_table_entry* entry) {
|
||||
libab_operator_free(&entry->data_u.op);
|
||||
} else if(entry->variant == ENTRY_FUN) {
|
||||
libab_function_free(&entry->data_u.function);
|
||||
} else if(entry->variant == ENTRY_BASETYPE) {
|
||||
libab_basetype_free(entry->data_u.basetype);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user