diff --git a/include/basetype.h b/include/basetype.h index 1ccbade..e63c406 100644 --- a/include/basetype.h +++ b/include/basetype.h @@ -61,5 +61,10 @@ typedef struct libab_basetype_s libab_basetype; */ void libab_basetype_init(libab_basetype* basetype, int n, const libab_basetype_param params[]); +/** + * Frees the given basetype. + * @param basetype the type to free. + */ +void libab_basetype_free(libab_basetype* basetype); #endif diff --git a/include/libabacus.h b/include/libabacus.h index da9124d..f9f8f59 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -78,9 +78,17 @@ libab_result libab_register_operator_postfix(libab* ab, const char* op, const ch * @param name the name of the function. * @param type the type of this operator. * @param func the function that describes the functionality of the function. - * @return the result of the initialization. + * @return the result of the registration. */ libab_result libab_register_function(libab* ab, const char* name, const char* type, libab_function_ptr func); +/** + * Registers a base type with abacus. + * @param ab the libabacus instance used to keep state. + * @param name the name to register the basetype under. + * @param basetype the basetype to register. + * @return the result of the registration. + */ +libab_result libab_register_basetype(libab* ab, const char* name, libab_basetype* basetype); /** * Releases all the resources allocated by libabacus. * @param ab the libabacus instance to release. diff --git a/include/table.h b/include/table.h index 0b9edad..6592af0 100644 --- a/include/table.h +++ b/include/table.h @@ -4,6 +4,7 @@ #include "result.h" #include "custom.h" #include "trie.h" +#include "basetype.h" /** * A struct that represents a structure @@ -29,7 +30,7 @@ struct libab_table_s { */ enum libab_table_entry_variant_e { ENTRY_VALUE, - ENTRY_TYPE, + ENTRY_BASETYPE, ENTRY_OP, ENTRY_FUN }; @@ -49,6 +50,7 @@ struct libab_table_entry_s { union { libab_operator op; libab_function function; + libab_basetype* basetype; } data_u; }; @@ -95,6 +97,14 @@ libab_operator* libab_table_search_operator(libab_table* table, const char* stri * @return the found function, or NULL if it was not found. */ libab_function* libab_table_search_function(libab_table* table, const char* string); +/** + * Searches for the given basetype in the table, returning a value + * only if it's a basetype. + * @param table the table to search. + * @param string the string to search for. + * @return the found basetype, or NULL if it was not found. + */ +libab_basetype* libab_table_search_basetype(libab_table* table, const char* string); /** * Stores the given entry in the table under the given key. * @param table the table to store the entry into. diff --git a/src/basetype.c b/src/basetype.c index 2b121a1..6f64c5c 100644 --- a/src/basetype.c +++ b/src/basetype.c @@ -7,4 +7,6 @@ void libab_basetype_init(libab_basetype* basetype, basetype->params = params; basetype->count = n; } +void libab_basetype_free(libab_basetype* basetype) { +} diff --git a/src/libabacus.c b/src/libabacus.c index a857d92..b437944 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -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); diff --git a/src/table.c b/src/table.c index bb88261..86ce5a0 100644 --- a/src/table.c +++ b/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); } }