Add functions for basetypes and their entries in tables.
This commit is contained in:
parent
b1a113a57d
commit
7b1445a262
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user