diff --git a/include/basetype.h b/include/basetype.h index 6754c5a..6cccb0c 100644 --- a/include/basetype.h +++ b/include/basetype.h @@ -55,9 +55,8 @@ typedef struct libab_basetype_s libab_basetype; * @param n the number of type parameters it has. * @param params the parameters this basetype accepts. */ -void libab_basetype_init(libab_basetype* basetype, - void (*free_function)(void*), int n, - const libab_basetype_param params[]); +void libab_basetype_init(libab_basetype* basetype, void (*free_function)(void*), + int n, const libab_basetype_param params[]); /** * Frees the given basetype. * @param basetype the type to free. diff --git a/include/custom.h b/include/custom.h index 46a756d..1a989b2 100644 --- a/include/custom.h +++ b/include/custom.h @@ -2,8 +2,8 @@ #define LIBABACUS_CUSTOM_H #include "parsetype.h" -#include "tree.h" #include "ref_trie.h" +#include "tree.h" /** * A function pointer that is called @@ -101,7 +101,7 @@ typedef struct libab_function_s libab_function; * @param type the type of the behavior. * @param func the function that this behavior calls. */ -void libab_behavior_init_internal(libab_behavior* behavior, +void libab_behavior_init_internal(libab_behavior* behavior, libab_function_ptr func); /** * Initializes a behavior that uses a tree that has been @@ -110,8 +110,7 @@ void libab_behavior_init_internal(libab_behavior* behavior, * @param type the type of the behavior. * @param tree the tree that this behavior uses. */ -void libab_behavior_init_tree(libab_behavior* behavior, - libab_tree* tree); +void libab_behavior_init_tree(libab_behavior* behavior, libab_tree* tree); /** * Frees the given behavior. * @param behavior the behavior to free. @@ -122,13 +121,14 @@ void libab_behavior_free(libab_behavior* behavior); * @param op the operator to initialize. * @param variant the variant of the operator (infix, prefix, etc) * @param precedence the precedence of the operator. - * @param associativity the associativity (left = -1, right = 1) of the operator. + * @param associativity the associativity (left = -1, right = 1) of the + * operator. * @param type the type of the operator. * @param func the function used to implement the operator. */ -void libab_operator_init(libab_operator* op, libab_operator_variant variant, +void libab_operator_init(libab_operator* op, libab_operator_variant variant, int precedence, int associativity, libab_ref* type, - libab_function_ptr func); + libab_function_ptr func); /** * Frees the given operator. * @param op the operator to free. diff --git a/include/interpreter.h b/include/interpreter.h index 307ec74..1749bdf 100644 --- a/include/interpreter.h +++ b/include/interpreter.h @@ -1,10 +1,10 @@ #ifndef LIBABACUS_INTERPRETER_H #define LIBABACUS_INTERPRETER_H -#include "table.h" -#include "tree.h" #include "impl.h" #include "libabacus.h" +#include "table.h" +#include "tree.h" struct libab_s; @@ -15,8 +15,8 @@ struct libab_interpreter_s { typedef struct libab_interpreter_s libab_interpreter; void libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab); -libab_result libab_interpreter_run(libab_interpreter* intr, - libab_tree* tree, libab_ref* into); +libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree, + libab_ref* into); void libab_interpreter_free(libab_interpreter* intr); #endif diff --git a/include/libabacus.h b/include/libabacus.h index 4ae4d54..9a25e20 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -1,14 +1,14 @@ #ifndef LIBABACUS_H #define LIBABACUS_H -#include "interpreter.h" #include "custom.h" #include "ht.h" +#include "impl.h" +#include "interpreter.h" #include "lexer.h" #include "parser.h" #include "result.h" #include "table.h" -#include "impl.h" /** * The main struct of libabacus, diff --git a/include/parsetype.h b/include/parsetype.h index 67c4b80..07c284a 100644 --- a/include/parsetype.h +++ b/include/parsetype.h @@ -47,7 +47,7 @@ typedef struct libab_parsetype_s libab_parsetype; * @return the result of the instantiation. */ libab_result libab_parsetype_init(libab_parsetype* into, libab_basetype* from, - size_t n, ...); + size_t n, ...); /** * Same as _init, but using a pre-initialized va_list. * @param into the reference to store the new type into. @@ -56,8 +56,9 @@ libab_result libab_parsetype_init(libab_parsetype* into, libab_basetype* from, * @param args the list of parameters to this parsetype. * @return the result of the instantiation. */ -libab_result libab_parsetype_init_va(libab_parsetype* into, libab_basetype* from, - size_t n, va_list args); +libab_result libab_parsetype_init_va(libab_parsetype* into, + libab_basetype* from, size_t n, + va_list args); /** * Frees the data associated with this type, ignoring diff --git a/include/ref_trie.h b/include/ref_trie.h index 71b0398..944af9c 100644 --- a/include/ref_trie.h +++ b/include/ref_trie.h @@ -54,7 +54,7 @@ void libab_ref_trie_init(libab_ref_trie* trie); * @param copy_of the trie to copy. * @return the result of the initialization. */ -libab_result libab_ref_trie_init_copy(libab_ref_trie* trie, +libab_result libab_ref_trie_init_copy(libab_ref_trie* trie, const libab_ref_trie* copy_of); /** * Stores a reference counted value into the trie. @@ -73,8 +73,8 @@ libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, * @param key the key to look under. * @return a reference stored under the given key. This can be a NULL reference. */ -void libab_ref_trie_get(const libab_ref_trie* trie, - const char* key, libab_ref* into); +void libab_ref_trie_get(const libab_ref_trie* trie, const char* key, + libab_ref* into); /** * Releases the trie, decrementing the refcounts of all * the values stored inside. diff --git a/include/refcount.h b/include/refcount.h index bd6a046..76a15a4 100644 --- a/include/refcount.h +++ b/include/refcount.h @@ -34,11 +34,11 @@ struct libab_ref_s { /** * Whether this reference is a strong reference. */ - unsigned int strong:1; + unsigned int strong : 1; /** * Whether this reference is a NULL reference. */ - unsigned int null:1; + unsigned int null : 1; /** * The reference count struct keeping track * of how many references are pointing to the value. diff --git a/include/table.h b/include/table.h index 5896a08..6196ece 100644 --- a/include/table.h +++ b/include/table.h @@ -3,9 +3,9 @@ #include "basetype.h" #include "custom.h" +#include "refcount.h" #include "result.h" #include "trie.h" -#include "refcount.h" /** * A struct that represents a structure @@ -29,11 +29,7 @@ struct libab_table_s { * Enum that represents the type of a table * entry. */ -enum libab_table_entry_variant_e { - ENTRY_VALUE, - ENTRY_BASETYPE, - ENTRY_OP -}; +enum libab_table_entry_variant_e { ENTRY_VALUE, ENTRY_BASETYPE, ENTRY_OP }; /** * An entry in the table. @@ -107,9 +103,8 @@ libab_basetype* libab_table_search_basetype(libab_table* table, * @param string the table entry key. * @param ref the reference to store the result into. */ -void libab_table_search_value(libab_table* table, - const char* string, - libab_ref* ref); +void libab_table_search_value(libab_table* table, const char* string, + libab_ref* ref); /** * Stores the given entry in the table under the given key. * @param table the table to store the entry into. diff --git a/include/util.h b/include/util.h index 53bda1a..480373f 100644 --- a/include/util.h +++ b/include/util.h @@ -1,12 +1,12 @@ #ifndef LIBABACUS_UTIL_H #define LIBABACUS_UTIL_H +#include "function_list.h" #include "libds.h" #include "liblex.h" #include "parsetype.h" #include "result.h" #include "table.h" -#include "function_list.h" #include /** @@ -65,7 +65,7 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, * @return the result of the instantiation. */ libab_result libab_instantiate_basetype(libab_basetype* to_instantiate, - libab_ref* into, size_t n, ...); + libab_ref* into, size_t n, ...); /** * Creates a new libab_table, and stores it into the given reference. * @param into the reference to store the table into. @@ -80,7 +80,8 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent); * @param type the type to give the value. * @return the result of necessary allocations. */ -libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, libab_ref* type); +libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, + libab_ref* type); /** * Allocates a new reference counted value with the given type and data. * @param into the reference to store the allocated data into. @@ -88,7 +89,8 @@ libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, libab_ref* * @param type the type to give the value. * @return the result of necessary allocations. */ -libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type); +libab_result libab_create_value_raw(libab_ref* into, void* data, + libab_ref* type); /** * Allocates a function that uses internal code to run. * @param into the reference into which to store the new function. @@ -96,7 +98,8 @@ libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type * @param fun the function implementation. * @return libab_result the result of any necessary allocations. */ -libab_result libab_create_function_internal(libab_ref* into, void (*free_function)(void*), +libab_result libab_create_function_internal(libab_ref* into, + void (*free_function)(void*), libab_function_ptr fun); /** * Allocates a function that uses a tree to run. @@ -105,7 +108,8 @@ libab_result libab_create_function_internal(libab_ref* into, void (*free_functio * @param tree the function implementation. * @return libab_result the result of any necessary allocations. */ -libab_result libab_create_function_tree(libab_ref* into, void (*free_function)(void*), +libab_result libab_create_function_tree(libab_ref* into, + void (*free_function)(void*), libab_tree* tree); /** * Creates a function list object, storing it in to the given reference. @@ -121,7 +125,7 @@ libab_result libab_create_function_list(libab_ref* into, libab_ref* type); * @param value the value to store into the table. * @param result the result of the operation. */ -libab_result libab_put_table_value(libab_table* table, - const char* key, libab_ref* value); +libab_result libab_put_table_value(libab_table* table, const char* key, + libab_ref* value); #endif diff --git a/include/value.h b/include/value.h index f4f8eb6..56a1be5 100644 --- a/include/value.h +++ b/include/value.h @@ -24,19 +24,22 @@ typedef struct libab_value_s libab_value; * Initializes a new value with the given reference counted data * and the given type. * @param value the value to initialize. - * @param data the data for this value. Its refcount is decreased when the value is freed. + * @param data the data for this value. Its refcount is decreased when the value + * is freed. * @param type the type of this value. */ void libab_value_init_ref(libab_value* value, libab_ref* data, libab_ref* type); /** * Initializes a new value with the given raw allocated data, and a type, - * whose basetype's free function is used to release the data when the value is freed. + * whose basetype's free function is used to release the data when the value is + * freed. * @param value the value to initialize. * @param data the data this value holds. * @param type the type of this value. * @return the result of any necessary allocations. */ -libab_result libab_value_init_raw(libab_value* value, void* data, libab_ref* type); +libab_result libab_value_init_raw(libab_value* value, void* data, + libab_ref* type); /** * Frees the given value. * @param value the value to free. diff --git a/src/basetype.c b/src/basetype.c index a383123..a936304 100644 --- a/src/basetype.c +++ b/src/basetype.c @@ -2,9 +2,8 @@ #include "util.h" #include -void libab_basetype_init(libab_basetype* basetype, - void (*free_function)(void*), int n, - const libab_basetype_param params[]) { +void libab_basetype_init(libab_basetype* basetype, void (*free_function)(void*), + int n, const libab_basetype_param params[]) { basetype->params = params; basetype->count = n; basetype->free_function = free_function; diff --git a/src/custom.c b/src/custom.c index 7240fc0..51463dd 100644 --- a/src/custom.c +++ b/src/custom.c @@ -6,8 +6,7 @@ void libab_behavior_init_internal(libab_behavior* behavior, behavior->data_u.internal = func; } -void libab_behavior_init_tree(libab_behavior* behavior, - libab_tree* tree) { +void libab_behavior_init_tree(libab_behavior* behavior, libab_tree* tree) { behavior->variant = BIMPL_TREE; behavior->data_u.tree = tree; } @@ -18,9 +17,9 @@ void libab_behavior_free(libab_behavior* behavior) { } } -void libab_operator_init(libab_operator* op, libab_operator_variant variant, +void libab_operator_init(libab_operator* op, libab_operator_variant variant, int precedence, int associativity, libab_ref* type, - libab_function_ptr func) { + libab_function_ptr func) { op->variant = variant; op->precedence = precedence; op->associativity = associativity; @@ -33,9 +32,7 @@ void libab_operator_free(libab_operator* op) { libab_behavior_free(&op->behavior); } -libab_result _function_init(libab_function* function) { - return LIBAB_SUCCESS; -} +libab_result _function_init(libab_function* function) { return LIBAB_SUCCESS; } libab_result libab_function_init_internal(libab_function* function, libab_function_ptr fun) { libab_result result = _function_init(function); diff --git a/src/interpreter.c b/src/interpreter.c index 5888a3f..fea73ab 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -10,74 +10,75 @@ struct interpreter_state { libab_table* base_table; }; -void _interpreter_init(struct interpreter_state* state, libab_interpreter* intr) { +void _interpreter_init(struct interpreter_state* state, + libab_interpreter* intr) { state->ab = intr->ab; state->base_table = libab_ref_get(&intr->ab->table); } -void _interpreter_free(struct interpreter_state* state) { - -} +void _interpreter_free(struct interpreter_state* state) {} libab_result _interpreter_create_num_val(struct interpreter_state* state, libab_ref* into, const char* from) { void* data; libab_result result = LIBAB_SUCCESS; - if((data = state->ab->impl.parse_num(from))) { + if ((data = state->ab->impl.parse_num(from))) { result = libab_create_value_raw(into, data, &state->ab->type_num); - if(result != LIBAB_SUCCESS) { - ((libab_parsetype*) libab_ref_get(&state->ab->type_num))->data_u.base->free_function(data); + if (result != LIBAB_SUCCESS) { + ((libab_parsetype*)libab_ref_get(&state->ab->type_num)) + ->data_u.base->free_function(data); } } else { result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); } return result; } -libab_result _interpreter_run(struct interpreter_state* state, - libab_tree* tree, libab_ref* into, - libab_ref* scope, int force_scope) { +libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree, + libab_ref* into, libab_ref* scope, + int force_scope) { libab_result result = LIBAB_SUCCESS; libab_ref new_scope; int needs_scope = libab_tree_has_scope(tree->variant) || force_scope; - - if(needs_scope) { + + if (needs_scope) { result = libab_create_table(&new_scope, scope); scope = &new_scope; } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { - } else if(tree->variant == TREE_BASE || tree->variant == TREE_BLOCK) { + } else if (tree->variant == TREE_BASE || tree->variant == TREE_BLOCK) { size_t index = 0; libab_ref_null(into); - while(result == LIBAB_SUCCESS && index < tree->children.size) { + while (result == LIBAB_SUCCESS && index < tree->children.size) { libab_ref_free(into); - result = _interpreter_run(state, vec_index(&tree->children, index), into, scope, 0); + result = _interpreter_run(state, vec_index(&tree->children, index), + into, scope, 0); index++; } - } else if(tree->variant == TREE_NUM) { + } else if (tree->variant == TREE_NUM) { result = _interpreter_create_num_val(state, into, tree->string_value); - } else if(tree->variant == TREE_VOID) { + } else if (tree->variant == TREE_VOID) { libab_ref_null(into); } - if(needs_scope) { + if (needs_scope) { libab_ref_free(&new_scope); } return result; } -libab_result libab_interpreter_run(libab_interpreter* intr, - libab_tree* tree, libab_ref* into) { +libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree, + libab_ref* into) { struct interpreter_state state; libab_result result; @@ -88,6 +89,4 @@ libab_result libab_interpreter_run(libab_interpreter* intr, return result; } -void libab_interpreter_free(libab_interpreter* intr) { - -} +void libab_interpreter_free(libab_interpreter* intr) {} diff --git a/src/libabacus.c b/src/libabacus.c index a234a23..2c55d95 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -10,26 +10,17 @@ void _free_function_list(void* function_list) { free(function_list); } -static libab_basetype _basetype_function_list = { - _free_function_list, - NULL, - 0 -}; +static libab_basetype _basetype_function_list = {_free_function_list, NULL, 0}; void _free_function(void* function) { libab_function_free(function); free(function); } -static libab_basetype_param _basetype_function_params[] = { - { BT_LIST, NULL } -}; +static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}}; -static libab_basetype _basetype_function = { - _free_function, - _basetype_function_params, - 1 -}; +static libab_basetype _basetype_function = {_free_function, + _basetype_function_params, 1}; libab_result _prepare_types(libab* ab, void (*free_function)(void*)); @@ -50,7 +41,7 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), result = _prepare_types(ab, free_function); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { parser_initialized = 1; libab_parser_init(&ab->parser, ab); libab_interpreter_init(&ab->intr, ab); @@ -67,12 +58,12 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), libab_ref_free(&ab->type_num); libab_ref_free(&ab->type_function_list); - if(parser_initialized) { + if (parser_initialized) { libab_parser_free(&ab->parser); libab_interpreter_free(&ab->intr); } - if(lexer_initialized) { + if (lexer_initialized) { libab_lexer_free(&ab->lexer); } } @@ -108,8 +99,8 @@ libab_result _register_operator(libab* ab, const char* op, if ((new_entry = malloc(sizeof(*new_entry)))) { new_entry->variant = ENTRY_OP; new_operator = &(new_entry->data_u.op); - libab_operator_init(new_operator, token_type, - precedence, associativity, type, func); + libab_operator_init(new_operator, token_type, precedence, associativity, + type, func); } else { result = LIBAB_MALLOC; } @@ -157,9 +148,10 @@ libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_result _create_value_function_internal(libab_ref* into, libab_ref* type, libab_function_ptr func) { libab_ref function_ref; - libab_result result = libab_create_function_internal(&function_ref, _free_function, func); + libab_result result = + libab_create_function_internal(&function_ref, _free_function, func); libab_ref_null(into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_free(into); result = libab_create_value_ref(into, &function_ref, type); } @@ -171,7 +163,7 @@ libab_result _create_value_function_list(libab_ref* into, libab_ref* type) { libab_ref list_ref; libab_result result = libab_create_function_list(&list_ref, type); libab_ref_null(into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_free(into); result = libab_create_value_ref(into, &list_ref, type); } @@ -179,7 +171,9 @@ libab_result _create_value_function_list(libab_ref* into, libab_ref* type) { return result; } -libab_result _libab_register_function_existing(libab* ab, libab_table_entry* entry, libab_ref* function_val) { +libab_result _libab_register_function_existing(libab* ab, + libab_table_entry* entry, + libab_ref* function_val) { libab_value* old_value; libab_parsetype* old_type; libab_result result = LIBAB_SUCCESS; @@ -187,20 +181,22 @@ libab_result _libab_register_function_existing(libab* ab, libab_table_entry* ent old_value = libab_ref_get(&entry->data_u.value); old_type = libab_ref_get(&old_value->type); - if(old_type->data_u.base == &_basetype_function_list) { + if (old_type->data_u.base == &_basetype_function_list) { libab_function_list* list = libab_ref_get(&old_value->data); result = libab_function_list_insert(list, function_val); - } else if(old_type->data_u.base == &_basetype_function) { + } else if (old_type->data_u.base == &_basetype_function) { libab_ref new_list; - result = _create_value_function_list(&new_list, &ab->type_function_list); - if(result == LIBAB_SUCCESS) { - libab_function_list* list = libab_ref_get(&((libab_value*) libab_ref_get(&new_list))->data); + result = + _create_value_function_list(&new_list, &ab->type_function_list); + if (result == LIBAB_SUCCESS) { + libab_function_list* list = + libab_ref_get(&((libab_value*)libab_ref_get(&new_list))->data); result = libab_function_list_insert(list, &entry->data_u.value); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_function_list_insert(list, function_val); } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_swap(&entry->data_u.value, &new_list); } libab_ref_free(&new_list); @@ -211,15 +207,16 @@ libab_result _libab_register_function_existing(libab* ab, libab_table_entry* ent return result; } -libab_result _libab_register_function_new(libab* ab, const char* name, libab_ref* function_val) { +libab_result _libab_register_function_new(libab* ab, const char* name, + libab_ref* function_val) { libab_result result = LIBAB_SUCCESS; libab_table_entry* entry; - if((entry = malloc(sizeof(*entry)))) { + if ((entry = malloc(sizeof(*entry)))) { entry->variant = ENTRY_VALUE; libab_ref_copy(function_val, &entry->data_u.value); result = libab_table_put(libab_ref_get(&ab->table), name, entry); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_table_entry_free(entry); free(entry); } @@ -231,17 +228,18 @@ libab_result _libab_register_function_new(libab* ab, const char* name, libab_ref } libab_result libab_register_function(libab* ab, const char* name, - libab_ref* type, libab_function_ptr func) { + libab_ref* type, libab_function_ptr func) { libab_table_entry* existing_entry; libab_ref function_value; - libab_result result = + libab_result result = _create_value_function_internal(&function_value, type, func); - - if(result == LIBAB_SUCCESS) { - existing_entry = - libab_table_search_filter(libab_ref_get(&ab->table), name, NULL, libab_table_compare_value); - if(existing_entry) { - result = _libab_register_function_existing(ab, existing_entry, &function_value); + + if (result == LIBAB_SUCCESS) { + existing_entry = libab_table_search_filter( + libab_ref_get(&ab->table), name, NULL, libab_table_compare_value); + if (existing_entry) { + result = _libab_register_function_existing(ab, existing_entry, + &function_value); } else { result = _libab_register_function_new(ab, name, &function_value); } @@ -283,8 +281,8 @@ libab_result _prepare_types(libab* ab, void (*free_function)(void*)) { if (result == LIBAB_SUCCESS) { libab_ref_free(&ab->type_num); - result = libab_instantiate_basetype(&ab->basetype_num, - &ab->type_num, 0); + result = + libab_instantiate_basetype(&ab->basetype_num, &ab->type_num, 0); } if (result == LIBAB_SUCCESS) { @@ -302,11 +300,11 @@ libab_result _prepare_types(libab* ab, void (*free_function)(void*)) { } if (result == LIBAB_SUCCESS) { - result = libab_register_basetype(ab, "function_list", + result = libab_register_basetype(ab, "function_list", &_basetype_function_list); } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_free(&ab->type_num); libab_ref_free(&ab->type_function_list); libab_ref_null(&ab->type_num); @@ -325,16 +323,15 @@ libab_result libab_create_type(libab* ab, libab_ref* into, const char* type) { result = libab_parser_parse_type(&ab->parser, &tokens, type, into); } if (result == LIBAB_SUCCESS) { - result = libab_resolve_parsetype(libab_ref_get(into), libab_ref_get(&ab->table)); + result = libab_resolve_parsetype(libab_ref_get(into), + libab_ref_get(&ab->table)); } ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free); ll_free(&tokens); return result; } -libab_basetype* libab_get_basetype_num(libab* ab) { - return &ab->basetype_num; -} +libab_basetype* libab_get_basetype_num(libab* ab) { return &ab->basetype_num; } libab_basetype* libab_get_basetype_function(libab* ab) { return &_basetype_function; diff --git a/src/parser.c b/src/parser.c index f4e8a7a..08b0227 100644 --- a/src/parser.c +++ b/src/parser.c @@ -327,7 +327,7 @@ libab_result _parse_type(struct parser_state* state, libab_ref* into) { } } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); } @@ -1169,7 +1169,8 @@ libab_result libab_parser_parse(libab_parser* parser, ll* tokens, const char* string, libab_tree** store_into) { libab_result result; struct parser_state state; - _parser_state_init(&state, tokens, string, libab_ref_get(&parser->ab->table)); + _parser_state_init(&state, tokens, string, + libab_ref_get(&parser->ab->table)); result = _parse_block(&state, store_into, 0); if (result == LIBAB_SUCCESS) { @@ -1182,7 +1183,8 @@ libab_result libab_parser_parse_type(libab_parser* parser, ll* tokens, const char* string, libab_ref* store_into) { struct parser_state state; - _parser_state_init(&state, tokens, string, libab_ref_get(&parser->ab->table)); + _parser_state_init(&state, tokens, string, + libab_ref_get(&parser->ab->table)); return _parse_type(&state, store_into); } diff --git a/src/parsetype.c b/src/parsetype.c index 41b73f3..55a3c2d 100644 --- a/src/parsetype.c +++ b/src/parsetype.c @@ -2,7 +2,7 @@ #include libab_result libab_parsetpe_init(libab_parsetype* type, libab_basetype* from, - size_t n, ...) { + size_t n, ...) { libab_result result; va_list params; va_start(params, n); @@ -11,38 +11,41 @@ libab_result libab_parsetpe_init(libab_parsetype* type, libab_basetype* from, return result; } -libab_result libab_parsetype_init_va(libab_parsetype* type, libab_basetype* from, - size_t n, va_list args) { +libab_result libab_parsetype_init_va(libab_parsetype* type, + libab_basetype* from, size_t n, + va_list args) { libab_result result = LIBAB_SUCCESS; size_t base_index = 0, param_index = 0; int free_vec = 0; - if(from->count > n) result = LIBAB_BAD_TYPE; - while(base_index < from->count && param_index < n) { - if(from->params[base_index].variant == BT_NAME) { + if (from->count > n) + result = LIBAB_BAD_TYPE; + while (base_index < from->count && param_index < n) { + if (from->params[base_index].variant == BT_NAME) { base_index++; } param_index++; } - if(param_index < n) { + if (param_index < n) { result = LIBAB_BAD_TYPE; } type->data_u.base = from; - type->variant = LIBABACUS_TYPE_F_RESOLVED; - if(result == LIBAB_SUCCESS && n) { + type->variant = LIBABACUS_TYPE_F_RESOLVED; + if (result == LIBAB_SUCCESS && n) { type->variant |= LIBABACUS_TYPE_F_PARENT; result = libab_ref_vec_init(&type->children); - if(result == LIBAB_SUCCESS) free_vec = 1; + if (result == LIBAB_SUCCESS) + free_vec = 1; } - while(n-- && result == LIBAB_SUCCESS) { + while (n-- && result == LIBAB_SUCCESS) { libab_ref* ref = va_arg(args, libab_ref*); result = libab_ref_vec_insert(&type->children, ref); } - if(free_vec) { + if (free_vec) { libab_ref_vec_free(&type->children); } diff --git a/src/ref_trie.c b/src/ref_trie.c index a9faab5..1865108 100644 --- a/src/ref_trie.c +++ b/src/ref_trie.c @@ -1,9 +1,7 @@ #include "ref_trie.h" #include -void libab_ref_trie_init(libab_ref_trie* trie) { - trie->head = NULL; -} +void libab_ref_trie_init(libab_ref_trie* trie) { trie->head = NULL; } void _libab_ref_trie_free(libab_ref_trie_node* node) { if (node == NULL) @@ -18,18 +16,18 @@ libab_result _libab_ref_trie_copy(const libab_ref_trie_node* copy_of, libab_ref_trie_node** copy_into) { libab_result result = LIBAB_SUCCESS; - if(copy_of == NULL) { + if (copy_of == NULL) { *copy_into = NULL; - } else if(((*copy_into) = malloc(sizeof(**copy_into)))) { + } else if (((*copy_into) = malloc(sizeof(**copy_into)))) { (*copy_into)->child = NULL; (*copy_into)->next = NULL; result = _libab_ref_trie_copy(copy_of->next, &(*copy_into)->next); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _libab_ref_trie_copy(copy_of->child, &(*copy_into)->child); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*copy_into)->key = copy_of->key; libab_ref_copy(©_of->ref, &(*copy_into)->ref); } @@ -37,7 +35,7 @@ libab_result _libab_ref_trie_copy(const libab_ref_trie_node* copy_of, result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS && *copy_into) { + if (result != LIBAB_SUCCESS && *copy_into) { _libab_ref_trie_free((*copy_into)->next); _libab_ref_trie_free((*copy_into)->child); free(*copy_into); @@ -47,14 +45,14 @@ libab_result _libab_ref_trie_copy(const libab_ref_trie_node* copy_of, return result; } -libab_result libab_ref_trie_init_copy(libab_ref_trie* trie, +libab_result libab_ref_trie_init_copy(libab_ref_trie* trie, const libab_ref_trie* copy_of) { libab_result result = LIBAB_SUCCESS; libab_ref_trie_init(trie); result = _libab_ref_trie_copy(copy_of->head, &trie->head); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_trie_free(trie); } @@ -115,8 +113,8 @@ libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, return result; } -void libab_ref_trie_get(const libab_ref_trie* trie, - const char* key, libab_ref* into) { +void libab_ref_trie_get(const libab_ref_trie* trie, const char* key, + libab_ref* into) { libab_ref_trie_node* current = trie->head; while (current && *key) { while (current && current->key != *key) { diff --git a/src/refcount.c b/src/refcount.c index 92a638f..2c35006 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -17,9 +17,7 @@ libab_result libab_ref_new(libab_ref* ref, void* data, return result; } -void libab_ref_null(libab_ref* ref) { - ref->null = 1; -} +void libab_ref_null(libab_ref* ref) { ref->null = 1; } void _libab_ref_changed(libab_ref* ref) { if (ref->count->strong == 0) { @@ -42,7 +40,7 @@ void libab_ref_weaken(libab_ref* ref) { } void libab_ref_free(libab_ref* ref) { - if(!ref->null) { + if (!ref->null) { ref->count->strong -= ref->strong; ref->count->weak--; _libab_ref_changed(ref); @@ -50,7 +48,7 @@ void libab_ref_free(libab_ref* ref) { } void libab_ref_copy(const libab_ref* ref, libab_ref* into) { - if(!ref->null) { + if (!ref->null) { ref->count->strong++; ref->count->weak++; } diff --git a/src/table.c b/src/table.c index 34f7aa3..987cbb1 100644 --- a/src/table.c +++ b/src/table.c @@ -30,7 +30,7 @@ libab_table_entry* libab_table_search(libab_table* table, const char* string) { #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.variant == TYPE; \ + return entry->variant == ENTRY_OP && entry->data_u.op.variant == TYPE; \ } OP_TYPE_COMPARATOR(libab_table_compare_op_prefix, OPERATOR_PREFIX) @@ -65,17 +65,16 @@ libab_operator* libab_table_search_operator(libab_table* table, libab_basetype* libab_table_search_basetype(libab_table* table, const char* string) { - libab_table_entry* entry = - libab_table_search_filter(table, string, NULL, libab_table_compare_basetype); + libab_table_entry* entry = libab_table_search_filter( + table, string, NULL, libab_table_compare_basetype); return entry ? entry->data_u.basetype : NULL; } -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) { +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) { libab_ref_copy(&entry->data_u.value, ref); } else { libab_ref_null(ref); diff --git a/src/util.c b/src/util.c index 33f65a0..21e167d 100644 --- a/src/util.c +++ b/src/util.c @@ -1,7 +1,7 @@ #include "util.h" #include "value.h" -#include #include +#include libab_result libab_convert_lex_result(liblex_result to_convert) { libab_result result = LIBAB_SUCCESS; @@ -94,27 +94,27 @@ void _libab_parsetype_free(void* parsetype) { } libab_result libab_instantiate_basetype(libab_basetype* to_instantiate, - libab_ref* into, size_t n, ...) { + libab_ref* into, size_t n, ...) { libab_result result = LIBAB_SUCCESS; libab_parsetype* parsetype; va_list params; va_start(params, n); - if((parsetype = malloc(sizeof(*parsetype)))) { + if ((parsetype = malloc(sizeof(*parsetype)))) { result = libab_parsetype_init_va(parsetype, to_instantiate, n, params); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_new(into, parsetype, _libab_parsetype_free); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_parsetype_free(parsetype); } } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); free(parsetype); } @@ -132,19 +132,19 @@ void _free_table(void* data) { libab_result libab_create_table(libab_ref* into, libab_ref* parent) { libab_table* table; libab_result result = LIBAB_SUCCESS; - if((table = malloc(sizeof(*table)))) { + if ((table = malloc(sizeof(*table)))) { libab_table_init(table); libab_table_set_parent(table, parent); result = libab_ref_new(into, table, _free_table); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { _free_table(table); } } else { - result = LIBAB_MALLOC; + result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); } return result; @@ -155,44 +155,46 @@ void _free_value(void* value) { free(value); } -libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, libab_ref* type) { +libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, + libab_ref* type) { libab_value* value; libab_result result = LIBAB_SUCCESS; - if((value = malloc(sizeof(*value)))) { + if ((value = malloc(sizeof(*value)))) { libab_value_init_ref(value, data, type); result = libab_ref_new(into, value, _free_value); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { _free_value(value); } } else { result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); } return result; } -libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type) { +libab_result libab_create_value_raw(libab_ref* into, void* data, + libab_ref* type) { libab_value* value; libab_result result = LIBAB_SUCCESS; - if((value = malloc(sizeof(*value)))) { + if ((value = malloc(sizeof(*value)))) { result = libab_value_init_raw(value, data, type); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_new(into, value, _free_value); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_value_free(value); } } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); free(value); } @@ -200,25 +202,26 @@ libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type return result; } -libab_result libab_create_function_internal(libab_ref* into, void (*free_function)(void*), +libab_result libab_create_function_internal(libab_ref* into, + void (*free_function)(void*), libab_function_ptr fun) { libab_function* new_function; libab_result result = LIBAB_SUCCESS; - if((new_function = malloc(sizeof(*new_function)))) { + if ((new_function = malloc(sizeof(*new_function)))) { result = libab_function_init_internal(new_function, fun); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_new(into, new_function, free_function); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_function_free(new_function); } } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); free(new_function); } @@ -226,25 +229,26 @@ libab_result libab_create_function_internal(libab_ref* into, void (*free_functio return result; } -libab_result libab_create_function_tree(libab_ref* into, void (*free_function)(void*), +libab_result libab_create_function_tree(libab_ref* into, + void (*free_function)(void*), libab_tree* tree) { libab_function* new_function; libab_result result = LIBAB_SUCCESS; - if((new_function = malloc(sizeof(*new_function)))) { + if ((new_function = malloc(sizeof(*new_function)))) { result = libab_function_init_tree(new_function, tree); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_new(into, new_function, free_function); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_function_free(new_function); } } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); free(new_function); } @@ -256,21 +260,22 @@ libab_result libab_create_function_list(libab_ref* into, libab_ref* type) { libab_function_list* list; libab_result result = LIBAB_SUCCESS; - if((list = malloc(sizeof(*list)))) { + if ((list = malloc(sizeof(*list)))) { result = libab_function_list_init(list); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { - result = libab_ref_new(into, list, - ((libab_parsetype*) libab_ref_get(type))->data_u.base->free_function); - if(result != LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { + result = libab_ref_new(into, list, + ((libab_parsetype*)libab_ref_get(type)) + ->data_u.base->free_function); + if (result != LIBAB_SUCCESS) { libab_function_list_free(list); } } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_null(into); free(list); } @@ -278,20 +283,20 @@ libab_result libab_create_function_list(libab_ref* into, libab_ref* type) { return result; } -libab_result libab_put_table_value(libab_table* table, - const char* key, libab_ref* value) { +libab_result libab_put_table_value(libab_table* table, const char* key, + libab_ref* value) { libab_table_entry* entry; libab_result result = LIBAB_SUCCESS; - if((entry = malloc(sizeof(*entry)))) { + if ((entry = malloc(sizeof(*entry)))) { entry->variant = ENTRY_VALUE; libab_ref_copy(value, &entry->data_u.value); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_table_put(table, key, entry); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_ref_free(&entry->data_u.value); free(entry); } diff --git a/src/value.c b/src/value.c index 81f212d..cf3600f 100644 --- a/src/value.c +++ b/src/value.c @@ -1,19 +1,22 @@ #include "value.h" #include "parsetype.h" -void libab_value_init_ref(libab_value* value, libab_ref* data, libab_ref* type) { +void libab_value_init_ref(libab_value* value, libab_ref* data, + libab_ref* type) { libab_ref_copy(data, &value->data); libab_ref_copy(type, &value->type); } -libab_result libab_value_init_raw(libab_value* value, void* data, libab_ref* type) { +libab_result libab_value_init_raw(libab_value* value, void* data, + libab_ref* type) { libab_result result = LIBAB_SUCCESS; libab_ref tmp_ref; - result = libab_ref_new(&tmp_ref, data, - ((libab_parsetype*) libab_ref_get(type))->data_u.base->free_function); + result = libab_ref_new( + &tmp_ref, data, + ((libab_parsetype*)libab_ref_get(type))->data_u.base->free_function); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_value_init_ref(value, &tmp_ref, type); libab_ref_free(&tmp_ref); }