diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..88b6b18 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: LLVM +PointerAlignment: Left +IndentWidth: 4 diff --git a/include/basetype.h b/include/basetype.h index 0eb992f..eedd99d 100644 --- a/include/basetype.h +++ b/include/basetype.h @@ -1,17 +1,14 @@ #ifndef LIBABACUS_BASETYPE_H #define LIBABACUS_BASETYPE_H -#include "vec.h" #include "result.h" +#include "vec.h" /** * An enum that represents the various * types of the basetype parameters. */ -enum libab_basetype_variant_e { - BT_NAME, - BT_LIST -}; +enum libab_basetype_variant_e { BT_NAME, BT_LIST }; /** * A struct that holds information about the basetype @@ -58,8 +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, int n, - const libab_basetype_param params[]); +void libab_basetype_init(libab_basetype* basetype, 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 80abb42..ec23b23 100644 --- a/include/custom.h +++ b/include/custom.h @@ -7,7 +7,7 @@ * A function pointer that is called * to execute a certain type of function. */ -typedef void(*libab_function_ptr)(); +typedef void (*libab_function_ptr)(); /** * The variant of the operator that @@ -22,10 +22,7 @@ enum libab_operator_variant_e { /** * The variant of the implementation of a behavior. */ -enum libab_behavior_variant_e { - BIMPL_INTERNAL, - BIMPL_TREE -}; +enum libab_behavior_variant_e { BIMPL_INTERNAL, BIMPL_TREE }; /** * A struct that represents the implementation of a behavior. diff --git a/include/lexer.h b/include/lexer.h index 8969563..ca9779a 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -90,7 +90,8 @@ libab_result libab_lexer_init(libab_lexer* lexer); * @param lex_into the list which should be populated with matches. * @return the result of the operation. */ -libab_result libab_lexer_lex(libab_lexer* lexer, const char* string, ll* lext_into); +libab_result libab_lexer_lex(libab_lexer* lexer, const char* string, + ll* lext_into); /** * Releases the memory associated with the given lexer, * removing all registered patterns from it. diff --git a/include/libabacus.h b/include/libabacus.h index a5507fa..f1dcdd7 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -1,12 +1,12 @@ #ifndef LIBABACUS_H #define LIBABACUS_H +#include "custom.h" #include "ht.h" #include "lexer.h" -#include "table.h" #include "parser.h" #include "result.h" -#include "custom.h" +#include "table.h" /** * The main struct of libabacus, @@ -50,7 +50,10 @@ libab_result libab_init(libab* ab); * @param func the function that describes the functionality of the operator. * @return the result of the initialization. */ -libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_ref* type, libab_function_ptr func); +libab_result libab_register_operator_infix(libab* ab, const char* op, + int precedence, int associativity, + libab_ref* type, + libab_function_ptr func); /** * Registers an operation with libabacus that appears * before its operand. @@ -60,7 +63,9 @@ libab_result libab_register_operator_infix(libab* ab, const char* op, int preced * @param func the function that describes the functionality of the operator. * @return the result of the registration. */ -libab_result libab_register_operator_prefix(libab* ab, const char* op, libab_ref* type, libab_function_ptr func); +libab_result libab_register_operator_prefix(libab* ab, const char* op, + libab_ref* type, + libab_function_ptr func); /** * Registers an operation with libabacus that appears * after its operand. @@ -70,7 +75,9 @@ libab_result libab_register_operator_prefix(libab* ab, const char* op, libab_ref * @param func the function that describes the functionality of the operator. * @return the result of the registration. */ -libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_ref* type, libab_function_ptr func); +libab_result libab_register_operator_postfix(libab* ab, const char* op, + libab_ref* type, + libab_function_ptr func); /** * Registers a function with libabacus. @@ -80,7 +87,8 @@ libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_re * @param func the function that describes the functionality of the function. * @return the result of the registration. */ -libab_result libab_register_function(libab* ab, const char* name, libab_ref* type, libab_function_ptr func); +libab_result libab_register_function(libab* ab, const char* name, + libab_ref* type, libab_function_ptr func); /** * Registers a base type with abacus. * @param ab the libabacus instance used to keep state. @@ -88,7 +96,8 @@ libab_result libab_register_function(libab* ab, const char* name, libab_ref* typ * @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); +libab_result libab_register_basetype(libab* ab, const char* name, + libab_basetype* basetype); /** * Constructs and resolves a parse type, similarly to how it's done in the * parser. diff --git a/include/parser.h b/include/parser.h index 16bebc6..4810aac 100644 --- a/include/parser.h +++ b/include/parser.h @@ -1,10 +1,10 @@ #ifndef LIBABACUS_PARSER_H #define LIBABACUS_PARSER_H -#include "table.h" #include "ll.h" -#include "tree.h" #include "parsetype.h" +#include "table.h" +#include "tree.h" /** * The parser that is used by libabacus @@ -31,9 +31,9 @@ void libab_parser_init(libab_parser* parser, libab_table* table); * @param store_into tree pointer to store the new data into. */ libab_result libab_parser_parse(libab_parser* parser, ll* tokens, - const char* string, libab_tree** store_into); + const char* string, libab_tree** store_into); libab_result libab_parser_parse_type(libab_parser* parser, ll* tokens, - const char* string, libab_ref* store_into); + const char* string, libab_ref* store_into); /** * Releases the resources allocated by the parser. * @param parser the parser to release. diff --git a/include/parsetype.h b/include/parsetype.h index a94a525..44c5f14 100644 --- a/include/parsetype.h +++ b/include/parsetype.h @@ -1,9 +1,9 @@ #ifndef LIBABACUS_PARSETYPE_H #define LIBABACUS_PARSETYPE_H -#include "result.h" -#include "ref_vec.h" #include "basetype.h" +#include "ref_vec.h" +#include "result.h" #define LIBABACUS_TYPE_F_PARENT (1) #define LIBABACUS_TYPE_F_PLACE (1 << 1) diff --git a/include/ref_trie.h b/include/ref_trie.h index 21532da..4186075 100644 --- a/include/ref_trie.h +++ b/include/ref_trie.h @@ -62,14 +62,16 @@ void libab_ref_trie_init(libab_ref_trie* trie); * @param ref the reference to insert. * @return the result of the insertion. */ -libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, libab_ref* ref); +libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, + libab_ref* ref); /** * Retreives a reference under the given key in the trie. * @param trie the trie to retreive from. * @param key the key to look under. * @return a reference stored under the given key. This can be a NULL reference. */ -const libab_ref* libab_ref_trie_get(const libab_ref_trie* trie, const char* key); +const libab_ref* libab_ref_trie_get(const libab_ref_trie* trie, + const char* key); /** * Releases the trie, decrementing the refcounts of all * the values stored inside. diff --git a/include/ref_vec.h b/include/ref_vec.h index 31afcc0..8da62b9 100644 --- a/include/ref_vec.h +++ b/include/ref_vec.h @@ -1,8 +1,8 @@ #ifndef LIBABACUS_REF_VEC_H #define LIBABACUS_REF_VEC_H -#include "result.h" #include "refcount.h" +#include "result.h" #include #define LIBABACUS_REF_VEC_INITIAL_SIZE 4 @@ -50,10 +50,11 @@ libab_result libab_ref_vec_insert(libab_ref_vec* vec, libab_ref* data); * @param free_func the function called to release the value (besides free) * @return the result of the insertion. */ -libab_result libab_ref_vec_insert_value(libab_ref_vec* vec, void* data, void (*free_func)(void*)); +libab_result libab_ref_vec_insert_value(libab_ref_vec* vec, void* data, + void (*free_func)(void*)); /** - * Returns the value at the given index in the vector, or null or the value doesn't - * exist. + * Returns the value at the given index in the vector, or null or the value + * doesn't exist. * @param vec the vector to get a value from. * @param index the index to look at. * @return the reference stored at the given index. diff --git a/include/refcount.h b/include/refcount.h index 397845c..ee0bea9 100644 --- a/include/refcount.h +++ b/include/refcount.h @@ -53,10 +53,12 @@ typedef struct libab_ref_count_s libab_ref_count; * Creates a new referene, using the given data and free function. * @param ref the reference to initialize with the given data. * @param data the data to reference count. - * @param free_func the function to use to realease the data when refcount reaches 0. + * @param free_func the function to use to realease the data when refcount + * reaches 0. * @return the result of the construction of the reference. */ -libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* data)); +libab_result libab_ref_new(libab_ref* ref, void* data, + void (*free_func)(void* data)); /** * Creates a reference to NULL. This does * not require a memory allocation. diff --git a/include/reserved.h b/include/reserved.h index 8cf52b5..cd2ae3f 100644 --- a/include/reserved.h +++ b/include/reserved.h @@ -1,10 +1,10 @@ #ifndef LIBABACUS_RESERVED_H #define LIBABACUS_RESERVED_H -#include "result.h" -#include "parsetype.h" -#include "tree.h" #include "libabacus.h" +#include "parsetype.h" +#include "result.h" +#include "tree.h" /** * Struct that represents a reserved operator that contains @@ -31,7 +31,8 @@ typedef struct libab_reserved_operator_s libab_reserved_operator; * Attempts to find a reserved operator with the given name. * This function operates under the assumption that there are * few reserved operators in libabacus. As such, it does effectively - * a polynomial time search - strcmp for every element until the operator is found. + * a polynomial time search - strcmp for every element until the operator is + * found. * @param name the name to search for. * @return the reserved operator, if it is found. */ diff --git a/include/table.h b/include/table.h index 6592af0..f0d53c2 100644 --- a/include/table.h +++ b/include/table.h @@ -1,10 +1,10 @@ #ifndef LIBABACUS_TABLE_H #define LIBABACUS_TABLE_H -#include "result.h" -#include "custom.h" -#include "trie.h" #include "basetype.h" +#include "custom.h" +#include "result.h" +#include "trie.h" /** * A struct that represents a structure @@ -72,7 +72,9 @@ void libab_table_init(libab_table* table); * @param compare the comparison function to use. * @return the table entry, or NULL if an entry was not found. */ -libab_table_entry* libab_table_search_filter(libab_table* table, const char* string, void* data, compare_func compare); +libab_table_entry* libab_table_search_filter(libab_table* table, + const char* string, void* data, + compare_func compare); /** * Searches for the given string in the table. * @param table the table to search. @@ -88,7 +90,8 @@ libab_table_entry* libab_table_search(libab_table* table, const char* string); * @param type the type of operator to search for (infix, prefix, postfix) * @return the found operator, or NULL if it was not found. */ -libab_operator* libab_table_search_operator(libab_table* table, const char* string, int type); +libab_operator* libab_table_search_operator(libab_table* table, + const char* string, int type); /** * Searches for the given string in the table, returning a value only * if it is a function. @@ -96,7 +99,8 @@ libab_operator* libab_table_search_operator(libab_table* table, const char* stri * @param string the string to search for. * @return the found function, or NULL if it was not found. */ -libab_function* libab_table_search_function(libab_table* table, const char* string); +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. @@ -104,7 +108,8 @@ libab_function* libab_table_search_function(libab_table* table, const char* stri * @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); +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. @@ -112,7 +117,8 @@ libab_basetype* libab_table_search_basetype(libab_table* table, const char* stri * @param entry the new entry to put into the table. * @return the result of the insertion, which could be LIBAB_MALLOC. */ -libab_result libab_table_put(libab_table* table, const char* string, libab_table_entry* entry); +libab_result libab_table_put(libab_table* table, const char* string, + libab_table_entry* entry); /** * Frees the resources allocated by the * given table. diff --git a/include/tree.h b/include/tree.h index a84fb2b..632413c 100644 --- a/include/tree.h +++ b/include/tree.h @@ -1,8 +1,8 @@ #ifndef LIBABACUS_TREE_H #define LIBABACUS_TREE_H -#include "result.h" #include "parsetype.h" +#include "result.h" #include "vec.h" /** @@ -115,7 +115,7 @@ int libab_tree_has_vector(libab_tree_variant var); /** * Frees the given tree recursively, * deleting the children first and the moving on - * to the parents. + * to the parents. * @param tree the tree to free. */ void libab_tree_free_recursive(libab_tree* tree); diff --git a/include/trie.h b/include/trie.h index 57cfd01..78067e0 100644 --- a/include/trie.h +++ b/include/trie.h @@ -39,12 +39,12 @@ struct libab_trie_s { /** * The first search node in this trie. */ - struct libab_trie_node_s* head; + struct libab_trie_node_s* head; /** * The empty list returned if no value is found. * Note that existing nodes return their own linked * list of values, even if empty. However, for keys - * that don't exist as prefixes in the trie, + * that don't exist as prefixes in the trie, * this list is returned to maintain consistency: * a list is always returned containing the values * of the trie associated with the given key. @@ -58,7 +58,8 @@ typedef struct libab_trie_node_s libab_trie_node; void libab_trie_init(libab_trie* trie); libab_result libab_trie_put(libab_trie* trie, const char* key, void* value); const ll* libab_trie_get(const libab_trie* trie, const char* key); -int libab_trie_foreach(const libab_trie* trie, void* data, compare_func compare, foreach_func foreach); +int libab_trie_foreach(const libab_trie* trie, void* data, compare_func compare, + foreach_func foreach); void libab_trie_free(libab_trie* trie); #endif diff --git a/include/util.h b/include/util.h index 1bba19b..91144a4 100644 --- a/include/util.h +++ b/include/util.h @@ -3,8 +3,8 @@ #include "libds.h" #include "liblex.h" -#include "result.h" #include "parsetype.h" +#include "result.h" #include "table.h" #include @@ -21,15 +21,16 @@ libab_result libab_convert_lex_result(liblex_result to_convert); */ libab_result libab_convert_ds_result(libds_result to_convert); /** - * Copies a range of the given string into a new, null-terminated string allocated - * on the heap. + * Copies a range of the given string into a new, null-terminated string + * allocated on the heap. * @param destination the pointer to populate with the newly created string. * @param source the source from which to pull character information from. * @param from the index (inclusive) at which to begin copying. * @param to the index (exclusive) at which to end copying. * @return the result of the operation. */ -libab_result libab_copy_string_range(char** destination, const char* source, size_t from, size_t to); +libab_result libab_copy_string_range(char** destination, const char* source, + size_t from, size_t to); /** * Copies the given string, starting at 0 and copying length bytes. * @param destination the pointer to populate with the newly created string. @@ -37,7 +38,8 @@ libab_result libab_copy_string_range(char** destination, const char* source, siz * @param length the number of bytes to copy. * @return the result of the operation. */ -libab_result libab_copy_string_size(char** destination, const char* source, size_t length); +libab_result libab_copy_string_size(char** destination, const char* source, + size_t length); /** * Copies the entire string into a null-terminated string allocated * on the heap. @@ -52,6 +54,7 @@ libab_result libab_copy_string(char** destination, const char* source); * @param to_resolve the parsetype to resolve. * @param scope the scope to use for resolving the type info. */ -libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, libab_table* scope); +libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, + libab_table* scope); #endif diff --git a/include/value.h b/include/value.h index 25cac6b..834505a 100644 --- a/include/value.h +++ b/include/value.h @@ -1,8 +1,8 @@ #ifndef LIBABACUS_VALUE_H #define LIBABACUS_VALUE_H -#include "result.h" #include "refcount.h" +#include "result.h" /** * A struct that represents a value. diff --git a/src/basetype.c b/src/basetype.c index 6f64c5c..7aad8ed 100644 --- a/src/basetype.c +++ b/src/basetype.c @@ -2,11 +2,9 @@ #include "util.h" #include -void libab_basetype_init(libab_basetype* basetype, - int n, const libab_basetype_param params[]) { +void libab_basetype_init(libab_basetype* basetype, int n, + const libab_basetype_param params[]) { basetype->params = params; basetype->count = n; } -void libab_basetype_free(libab_basetype* basetype) { - -} +void libab_basetype_free(libab_basetype* basetype) {} diff --git a/src/custom.c b/src/custom.c index 4366193..26ad452 100644 --- a/src/custom.c +++ b/src/custom.c @@ -2,7 +2,7 @@ void libab_behavior_free(libab_behavior* behavior) { libab_ref_free(&behavior->type); - if(behavior->impl.variant == BIMPL_TREE) { + if (behavior->impl.variant == BIMPL_TREE) { libab_ref_free(&behavior->impl.data_u.tree); } } diff --git a/src/debug.c b/src/debug.c index 645eaf7..8d16237 100644 --- a/src/debug.c +++ b/src/debug.c @@ -4,30 +4,15 @@ int _debug_foreach_print_tree(void* data, va_list args); const char* _debug_node_name(libab_tree_variant var) { static const char* names[] = { - "none", - "base", - "id", - "num", - "op", - "reserved_op", - "prefix_op", - "postfix_op", - "block", - "void", - "if", - "while", - "dowhile", - "call", - "fun", - "fun_param", - "return" - }; + "none", "base", "id", "num", "op", "reserved_op", + "prefix_op", "postfix_op", "block", "void", "if", "while", + "dowhile", "call", "fun", "fun_param", "return"}; return names[var]; } void _debug_print_tree_node(libab_tree* tree, FILE* file) { fprintf(file, "%s", _debug_node_name(tree->variant)); - if(libab_tree_has_string(tree->variant)) { + if (libab_tree_has_string(tree->variant)) { fprintf(file, ": %s", tree->string_value); } fprintf(file, "\n"); @@ -35,11 +20,12 @@ void _debug_print_tree_node(libab_tree* tree, FILE* file) { void _debug_print_tree(libab_tree* tree, FILE* file, int depth) { int i = depth; - while(i--) printf(" "); + while (i--) + printf(" "); _debug_print_tree_node(tree, file); - if(libab_tree_has_vector(tree->variant)) { + if (libab_tree_has_vector(tree->variant)) { vec_foreach(&tree->children, NULL, compare_always, - _debug_foreach_print_tree, file, depth + 1); + _debug_foreach_print_tree, file, depth + 1); } } diff --git a/src/lexer.c b/src/lexer.c index 3a2de7a..1964d36 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,49 +1,37 @@ #include "lexer.h" #include "ll.h" #include "util.h" -#include #include +#include libab_result libab_lexer_init(libab_lexer* lexer) { size_t i; libab_result result = LIBAB_SUCCESS; - const char* words[] = { - ".", - "[a-zA-Z][a-zA-Z0-9_]*", - "[0-9]+(\\.[0-9]*)?", - "if", - "else", - "while", - "do", - "->", - "fun", - "return", - "let", - "be" - }; + const char* words[] = {".", + "[a-zA-Z][a-zA-Z0-9_]*", + "[0-9]+(\\.[0-9]*)?", + "if", + "else", + "while", + "do", + "->", + "fun", + "return", + "let", + "be"}; libab_lexer_token tokens[] = { - TOKEN_CHAR, - TOKEN_ID, - TOKEN_NUM, - TOKEN_KW_IF, - TOKEN_KW_ELSE, - TOKEN_KW_WHILE, - TOKEN_KW_DO, - TOKEN_KW_ARROW, - TOKEN_KW_FUN, - TOKEN_KW_RETURN, - TOKEN_KW_LET, - TOKEN_KW_BE - }; - const size_t count = sizeof(tokens)/sizeof(libab_lexer_token); + TOKEN_CHAR, TOKEN_ID, TOKEN_NUM, TOKEN_KW_IF, + TOKEN_KW_ELSE, TOKEN_KW_WHILE, TOKEN_KW_DO, TOKEN_KW_ARROW, + TOKEN_KW_FUN, TOKEN_KW_RETURN, TOKEN_KW_LET, TOKEN_KW_BE}; + const size_t count = sizeof(tokens) / sizeof(libab_lexer_token); eval_config_init(&lexer->config); - for(i = 0; i < count && result == LIBAB_SUCCESS; i++) { + for (i = 0; i < count && result == LIBAB_SUCCESS; i++) { result = libab_convert_lex_result( - eval_config_add(&lexer->config, words[i], tokens[i])); + eval_config_add(&lexer->config, words[i], tokens[i])); } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { eval_config_free(&lexer->config); } @@ -62,19 +50,19 @@ int _lexer_foreach_convert_match(void* data, va_list args) { match* match = data; struct lexer_state* state = va_arg(args, struct lexer_state*); char first_char = state->source[match->from]; - if(isspace(first_char)) { + if (isspace(first_char)) { /* Skip */ - } else if(first_char == '\n') { + } else if (first_char == '\n') { state->line++; state->line_from = match->to; - } else if((new_match = malloc(sizeof(*new_match)))) { + } else if ((new_match = malloc(sizeof(*new_match)))) { new_match->type = match->pattern; new_match->from = match->from; new_match->to = match->to; new_match->line_from = state->line_from; new_match->line = state->line; result = libab_convert_ds_result(ll_append(state->matches, new_match)); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free(new_match); } } else { @@ -83,7 +71,8 @@ int _lexer_foreach_convert_match(void* data, va_list args) { return result; } -libab_result libab_lexer_lex(libab_lexer* lexer, const char* string, ll* lex_into) { +libab_result libab_lexer_lex(libab_lexer* lexer, const char* string, + ll* lex_into) { libab_result result; ll raw_matches; struct lexer_state state; @@ -96,15 +85,16 @@ libab_result libab_lexer_lex(libab_lexer* lexer, const char* string, ll* lex_int state.source = string; result = libab_convert_lex_result( - eval_all(string, 0, &lexer->config, &raw_matches)); + eval_all(string, 0, &lexer->config, &raw_matches)); - if(result == LIBAB_SUCCESS) { - result = (libab_result) ll_foreach(&raw_matches, NULL, compare_always, - _lexer_foreach_convert_match, &state); + if (result == LIBAB_SUCCESS) { + result = (libab_result)ll_foreach(&raw_matches, NULL, compare_always, + _lexer_foreach_convert_match, &state); } - if(result != LIBAB_SUCCESS) { - ll_foreach(lex_into, NULL, compare_always, libab_lexer_foreach_match_free); + if (result != LIBAB_SUCCESS) { + ll_foreach(lex_into, NULL, compare_always, + libab_lexer_foreach_match_free); } ll_foreach(&raw_matches, NULL, compare_always, eval_foreach_match_free); @@ -116,6 +106,6 @@ libab_result libab_lexer_free(libab_lexer* lexer) { return libab_convert_lex_result(eval_config_free(&lexer->config)); } int libab_lexer_foreach_match_free(void* data, va_list args) { - free((libab_lexer_match*) data); + free((libab_lexer_match*)data); return 0; } diff --git a/src/libabacus.c b/src/libabacus.c index 6a2d6d7..315d2e2 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -1,8 +1,8 @@ #include "libabacus.h" -#include -#include "util.h" -#include "reserved.h" #include "lexer.h" +#include "reserved.h" +#include "util.h" +#include libab_result libab_init(libab* ab) { libab_result result; @@ -10,11 +10,11 @@ libab_result libab_init(libab* ab) { libab_parser_init(&ab->parser, &ab->table); result = libab_lexer_init(&ab->lexer); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_register_reserved_operators(&ab->lexer); } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_table_free(&ab->table); libab_parser_free(&ab->parser); libab_lexer_free(&ab->lexer); @@ -25,25 +25,29 @@ libab_result libab_init(libab* ab) { void _sanitize(char* to, const char* from, size_t buffer_size) { size_t index = 0; - while(*from && index < (buffer_size - 2)) { - if(*from == '+' || *from == '*' || *from == '\\') to[index++] = '\\'; + while (*from && index < (buffer_size - 2)) { + if (*from == '+' || *from == '*' || *from == '\\') + to[index++] = '\\'; to[index++] = *(from++); } to[index] = '\0'; } -void _initialize_behavior(libab* ab, libab_behavior* behavior, - libab_ref* type, libab_function_ptr func) { +void _initialize_behavior(libab* ab, libab_behavior* behavior, libab_ref* type, + libab_function_ptr func) { behavior->impl.variant = BIMPL_INTERNAL; behavior->impl.data_u.internal = func; libab_ref_copy(type, &behavior->type); } -libab_result _register_operator(libab* ab, const char* op, libab_operator_variant token_type, int precedence, int associativity, libab_ref* type, libab_function_ptr func) { +libab_result _register_operator(libab* ab, const char* op, + libab_operator_variant token_type, + int precedence, int associativity, + libab_ref* type, libab_function_ptr func) { char op_buffer[8]; libab_result result = LIBAB_SUCCESS; libab_table_entry* new_entry; - if((new_entry = malloc(sizeof(*new_entry)))) { + if ((new_entry = malloc(sizeof(*new_entry)))) { new_entry->variant = ENTRY_OP; new_entry->data_u.op.precedence = precedence; new_entry->data_u.op.associativity = associativity; @@ -53,17 +57,18 @@ libab_result _register_operator(libab* ab, const char* op, libab_operator_varian result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { _sanitize(op_buffer, op, 8); - result = libab_convert_lex_result(eval_config_add(&ab->lexer.config, op_buffer, TOKEN_OP)); + result = libab_convert_lex_result( + eval_config_add(&ab->lexer.config, op_buffer, TOKEN_OP)); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_table_put(&ab->table, op, new_entry); } - if(result != LIBAB_SUCCESS) { - if(new_entry) + if (result != LIBAB_SUCCESS) { + if (new_entry) libab_ref_free(&new_entry->data_u.op.behavior.type); eval_config_remove(&ab->lexer.config, op, TOKEN_OP); free(new_entry); @@ -72,34 +77,44 @@ libab_result _register_operator(libab* ab, const char* op, libab_operator_varian return result; } -libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_ref* type, libab_function_ptr func) { - return _register_operator(ab, op, OPERATOR_INFIX, precedence, associativity, type, func); +libab_result libab_register_operator_infix(libab* ab, const char* op, + int precedence, int associativity, + libab_ref* type, + libab_function_ptr func) { + return _register_operator(ab, op, OPERATOR_INFIX, precedence, associativity, + type, func); } -libab_result libab_register_operator_prefix(libab* ab, const char* op, libab_ref* type, libab_function_ptr func) { +libab_result libab_register_operator_prefix(libab* ab, const char* op, + libab_ref* type, + libab_function_ptr func) { return _register_operator(ab, op, OPERATOR_PREFIX, 0, 0, type, func); } -libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_ref* type, libab_function_ptr func) { +libab_result libab_register_operator_postfix(libab* ab, const char* op, + libab_ref* type, + libab_function_ptr func) { return _register_operator(ab, op, OPERATOR_POSTFIX, 0, 0, type, func); } -libab_result libab_register_function(libab* ab, const char* name, libab_ref* type, libab_function_ptr func) { +libab_result libab_register_function(libab* ab, const char* name, + libab_ref* type, libab_function_ptr func) { libab_result result = LIBAB_SUCCESS; libab_table_entry* new_entry; - if((new_entry = malloc(sizeof(*new_entry)))) { + if ((new_entry = malloc(sizeof(*new_entry)))) { new_entry->variant = ENTRY_FUN; - _initialize_behavior(ab, &(new_entry->data_u.function.behavior), type, func); + _initialize_behavior(ab, &(new_entry->data_u.function.behavior), type, + func); } else { result = LIBAB_MALLOC; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_table_put(&ab->table, name, new_entry); } - if(result != LIBAB_SUCCESS) { - if(new_entry) + if (result != LIBAB_SUCCESS) { + if (new_entry) libab_ref_free(&new_entry->data_u.function.behavior.type); free(new_entry); } @@ -107,19 +122,20 @@ libab_result libab_register_function(libab* ab, const char* name, libab_ref* typ return result; } -libab_result libab_register_basetype(libab* ab, const char* name, libab_basetype* basetype) { +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)))) { + if ((new_entry = malloc(sizeof(*new_entry)))) { new_entry->variant = ENTRY_BASETYPE; new_entry->data_u.basetype = basetype; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_table_put(&ab->table, name, new_entry); } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free(new_entry); } @@ -131,7 +147,7 @@ libab_result libab_create_type(libab* ab, libab_ref* into, const char* type) { ll tokens; ll_init(&tokens); result = libab_lexer_lex(&ab->lexer, type, &tokens); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_parser_parse_type(&ab->parser, &tokens, type, into); } ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free); diff --git a/src/parser.c b/src/parser.c index a4e296a..a6d8ba3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,10 +1,10 @@ #include "parser.h" -#include "util.h" -#include "result.h" #include "lexer.h" +#include "reserved.h" +#include "result.h" +#include "util.h" #include #include -#include "reserved.h" struct parser_state { ll_node* current_node; @@ -20,20 +20,22 @@ struct operator_data { }; /* Utilities */ -#define PARSE_CHILD(result, state, parse_function, parse_into, into) do {\ - result = parse_function(state, &parse_into);\ - if(result == LIBAB_SUCCESS) {\ - result = libab_convert_ds_result(vec_add(into, parse_into));\ - if(result != LIBAB_SUCCESS) {\ - libab_tree_free_recursive(parse_into);\ - }\ - }\ -} while(0); +#define PARSE_CHILD(result, state, parse_function, parse_into, into) \ + do { \ + result = parse_function(state, &parse_into); \ + if (result == LIBAB_SUCCESS) { \ + result = libab_convert_ds_result(vec_add(into, parse_into)); \ + if (result != LIBAB_SUCCESS) { \ + libab_tree_free_recursive(parse_into); \ + } \ + } \ + } while (0); void _parser_extract_token_buffer(struct parser_state* state, char* buffer, - size_t length, libab_lexer_match* match) { + size_t length, libab_lexer_match* match) { size_t token_size = match->to - match->from; - size_t buffer_length = (token_size < (length - 1)) ? token_size : (length - 1); + size_t buffer_length = + (token_size < (length - 1)) ? token_size : (length - 1); strncpy(buffer, state->string + match->from, buffer_length); buffer[buffer_length] = '\0'; } @@ -44,18 +46,21 @@ int _parser_foreach_free_tree(void* data, va_list args) { return 0; } -libab_result _parser_extract_token(struct parser_state* state, char** into, libab_lexer_match* match) { +libab_result _parser_extract_token(struct parser_state* state, char** into, + libab_lexer_match* match) { return libab_copy_string_range(into, state->string, match->from, match->to); } /* State functions */ void _parser_state_update(struct parser_state* state) { - state->current_match = state->current_node ? state->current_node->data : NULL; - if(state->current_match) state->last_match = state->current_match; + state->current_match = + state->current_node ? state->current_node->data : NULL; + if (state->current_match) + state->last_match = state->current_match; } -void _parser_state_init(struct parser_state* state, - ll* tokens, const char* string, libab_table* table) { +void _parser_state_init(struct parser_state* state, ll* tokens, + const char* string, libab_table* table) { state->last_match = NULL; state->current_node = tokens->head; state->string = string; @@ -64,7 +69,7 @@ void _parser_state_init(struct parser_state* state, } void _parser_state_step(struct parser_state* state) { - if(state->current_node) { + if (state->current_node) { state->current_node = state->current_node->next; } _parser_state_update(state); @@ -85,10 +90,10 @@ int _parser_eof(struct parser_state* state) { libab_result _parser_consume_char(struct parser_state* state, char to_consume) { libab_result result = LIBAB_SUCCESS; - if(state->current_match == NULL) { + if (state->current_match == NULL) { result = LIBAB_EOF; - } else if(state->current_match->type != TOKEN_CHAR || - state->string[state->current_match->from] != to_consume) { + } else if (state->current_match->type != TOKEN_CHAR || + state->string[state->current_match->from] != to_consume) { result = LIBAB_UNEXPECTED; } else { _parser_state_step(state); @@ -97,11 +102,11 @@ libab_result _parser_consume_char(struct parser_state* state, char to_consume) { } libab_result _parser_consume_type(struct parser_state* state, - libab_lexer_token to_consume) { + libab_lexer_token to_consume) { libab_result result = LIBAB_SUCCESS; - if(state->current_match == NULL) { + if (state->current_match == NULL) { result = LIBAB_EOF; - } else if(state->current_match->type != to_consume) { + } else if (state->current_match->type != to_consume) { result = LIBAB_UNEXPECTED; } else { _parser_state_step(state); @@ -112,92 +117,101 @@ libab_result _parser_consume_type(struct parser_state* state, /* Basic Tree Constructors */ libab_result _parse_block(struct parser_state*, libab_tree**, int); -libab_result _parse_expression(struct parser_state* state, libab_tree** store_into); +libab_result _parse_expression(struct parser_state* state, + libab_tree** store_into); libab_result _parse_type(struct parser_state* state, libab_ref* ref); -libab_result _parse_braced_block(struct parser_state* state, libab_tree** store_into) { +libab_result _parse_braced_block(struct parser_state* state, + libab_tree** store_into) { return _parse_block(state, store_into, 1); } -libab_result _parser_allocate_type(libab_parsetype** into, const char* source, size_t from, size_t to) { +libab_result _parser_allocate_type(libab_parsetype** into, const char* source, + size_t from, size_t to) { libab_result result = LIBAB_SUCCESS; - if((*into = malloc(sizeof(**into)))) { + if ((*into = malloc(sizeof(**into)))) { (*into)->variant = 0; - result = libab_copy_string_range(&(*into)->data_u.name, source, from, to); + result = + libab_copy_string_range(&(*into)->data_u.name, source, from, to); } else { result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free(*into); *into = NULL; } return result; } -libab_result _parser_append_type(struct parser_state* state, libab_ref_vec* into) { +libab_result _parser_append_type(struct parser_state* state, + libab_ref_vec* into) { libab_result result = LIBAB_SUCCESS; libab_ref temp; result = _parse_type(state, &temp); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_vec_insert(into, &temp); libab_ref_free(&temp); } return result; } -libab_result _parse_type_list(struct parser_state* state, libab_ref_vec* into, char end_char) { +libab_result _parse_type_list(struct parser_state* state, libab_ref_vec* into, + char end_char) { libab_result result = LIBAB_SUCCESS; int is_parenth, is_comma; - while(result == LIBAB_SUCCESS && !_parser_eof(state) && !_parser_is_char(state, end_char)) { + while (result == LIBAB_SUCCESS && !_parser_eof(state) && + !_parser_is_char(state, end_char)) { result = _parser_append_type(state, into); is_parenth = _parser_is_char(state, end_char); is_comma = _parser_is_char(state, ','); - if(result == LIBAB_SUCCESS && !(is_parenth || is_comma)) { + if (result == LIBAB_SUCCESS && !(is_parenth || is_comma)) { result = LIBAB_UNEXPECTED; - } else if(result == LIBAB_SUCCESS && is_comma) { + } else if (result == LIBAB_SUCCESS && is_comma) { _parser_state_step(state); - if(_parser_is_char(state, end_char)) { + if (_parser_is_char(state, end_char)) { result = LIBAB_UNEXPECTED; } } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, end_char); } return result; } -libab_result _parse_type_id(struct parser_state* state, libab_parsetype** into) { +libab_result _parse_type_id(struct parser_state* state, + libab_parsetype** into) { libab_result result; int placeholder_flag = 0; *into = NULL; - if(_parser_is_char(state, '\'')) { + if (_parser_is_char(state, '\'')) { placeholder_flag = LIBABACUS_TYPE_F_PLACE; _parser_state_step(state); } - if(_parser_is_type(state, TOKEN_ID)) { - result = _parser_allocate_type(into, state->string, - state->current_match->from, state->current_match->to); + if (_parser_is_type(state, TOKEN_ID)) { + result = _parser_allocate_type(into, state->string, + state->current_match->from, + state->current_match->to); } else { result = _parser_consume_type(state, TOKEN_ID); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*into)->variant |= placeholder_flag; _parser_state_step(state); } - if(result == LIBAB_SUCCESS && _parser_is_char(state, '(')) { - if(placeholder_flag) { + if (result == LIBAB_SUCCESS && _parser_is_char(state, '(')) { + if (placeholder_flag) { result = LIBAB_UNEXPECTED; } else { result = libab_ref_vec_init(&(*into)->children); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free((*into)->data_u.name); free(*into); *into = NULL; @@ -209,21 +223,21 @@ libab_result _parse_type_id(struct parser_state* state, libab_parsetype** into) } } - if(result != LIBAB_SUCCESS && *into) { + if (result != LIBAB_SUCCESS && *into) { libab_parsetype_free(*into); *into = NULL; } return result; -} +} libab_result _parse_type_function(struct parser_state* state, - libab_parsetype** into) { + libab_parsetype** into) { libab_result result = _parser_allocate_type(into, "function", 0, 8); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*into)->variant |= LIBABACUS_TYPE_F_PARENT; result = libab_ref_vec_init(&(*into)->children); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free((*into)->data_u.name); free(*into); *into = NULL; @@ -232,19 +246,19 @@ libab_result _parse_type_function(struct parser_state* state, } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parse_type_list(state, &(*into)->children, ')'); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_type(state, TOKEN_KW_ARROW); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_append_type(state, &(*into)->children); } - if(result != LIBAB_SUCCESS && *into) { + if (result != LIBAB_SUCCESS && *into) { libab_parsetype_free(*into); *into = NULL; } @@ -253,12 +267,12 @@ libab_result _parse_type_function(struct parser_state* state, } libab_result _parse_type_array(struct parser_state* state, - libab_parsetype** into) { + libab_parsetype** into) { libab_result result = _parser_allocate_type(into, "array", 0, 5); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*into)->variant |= LIBABACUS_TYPE_F_PARENT; result = libab_ref_vec_init(&(*into)->children); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free((*into)->data_u.name); free(*into); *into = NULL; @@ -266,15 +280,15 @@ libab_result _parse_type_array(struct parser_state* state, _parser_state_step(state); } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_append_type(state, &(*into)->children); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ']'); } - if(result != LIBAB_SUCCESS && *into) { + if (result != LIBAB_SUCCESS && *into) { libab_parsetype_free(*into); *into = NULL; } @@ -282,13 +296,14 @@ libab_result _parse_type_array(struct parser_state* state, return result; } -libab_result _parse_type_raw(struct parser_state* state, libab_parsetype** into) { +libab_result _parse_type_raw(struct parser_state* state, + libab_parsetype** into) { libab_result result; - if(_parser_is_type(state, TOKEN_ID) || _parser_is_char(state, '\'')) { + if (_parser_is_type(state, TOKEN_ID) || _parser_is_char(state, '\'')) { result = _parse_type_id(state, into); - } else if(_parser_is_char(state, '(')) { + } else if (_parser_is_char(state, '(')) { result = _parse_type_function(state, into); - } else if(_parser_is_char(state, '[')) { + } else if (_parser_is_char(state, '[')) { result = _parse_type_array(state, into); } else { *into = NULL; @@ -297,16 +312,14 @@ libab_result _parse_type_raw(struct parser_state* state, libab_parsetype** into) return result; } -void _parse_type_free(void* data) { - libab_parsetype_free(data); -} +void _parse_type_free(void* data) { libab_parsetype_free(data); } libab_result _parse_type(struct parser_state* state, libab_ref* into) { libab_parsetype* store_into; libab_result result = _parse_type_raw(state, &store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_new(into, store_into, _parse_type_free); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_parsetype_free(store_into); libab_ref_null(into); } @@ -314,11 +327,12 @@ libab_result _parse_type(struct parser_state* state, libab_ref* into) { return result; } -libab_result _parser_allocate_node(libab_lexer_match* match, libab_tree** into) { +libab_result _parser_allocate_node(libab_lexer_match* match, + libab_tree** into) { libab_result result = LIBAB_SUCCESS; - if(((*into) = malloc(sizeof(**into))) == NULL) { + if (((*into) = malloc(sizeof(**into))) == NULL) { result = LIBAB_MALLOC; - } else if(match) { + } else if (match) { (*into)->from = match->from; (*into)->to = match->to; (*into)->line = match->line; @@ -327,14 +341,16 @@ libab_result _parser_allocate_node(libab_lexer_match* match, libab_tree** into) return result; } -libab_result _parser_construct_node_string(struct parser_state* state, libab_lexer_match* match, libab_tree** into) { +libab_result _parser_construct_node_string(struct parser_state* state, + libab_lexer_match* match, + libab_tree** into) { libab_result result = _parser_allocate_node(match, into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_extract_token(state, &(*into)->string_value, match); } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free(*into); *into = NULL; } @@ -342,14 +358,15 @@ libab_result _parser_construct_node_string(struct parser_state* state, libab_lex return result; } -libab_result _parser_construct_node_vec(libab_lexer_match* match, libab_tree** into) { +libab_result _parser_construct_node_vec(libab_lexer_match* match, + libab_tree** into) { libab_result result = _parser_allocate_node(match, into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(vec_init(&(*into)->children)); } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free(*into); *into = NULL; } @@ -357,11 +374,14 @@ libab_result _parser_construct_node_vec(libab_lexer_match* match, libab_tree** i return result; } -libab_result _parser_construct_node_both(struct parser_state* state, libab_lexer_match* match, libab_tree** store_into) { - libab_result result = _parser_construct_node_string(state, match, store_into); - if(result == LIBAB_SUCCESS) { +libab_result _parser_construct_node_both(struct parser_state* state, + libab_lexer_match* match, + libab_tree** store_into) { + libab_result result = + _parser_construct_node_string(state, match, store_into); + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(vec_init(&(*store_into)->children)); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free((*store_into)->string_value); free(*store_into); *store_into = NULL; @@ -372,7 +392,7 @@ libab_result _parser_construct_node_both(struct parser_state* state, libab_lexer libab_result _parse_void(struct parser_state* state, libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; - if((*store_into = malloc(sizeof(**store_into)))) { + if ((*store_into = malloc(sizeof(**store_into)))) { (*store_into)->variant = TREE_VOID; } else { result = LIBAB_MALLOC; @@ -386,9 +406,9 @@ libab_result _parse_if(struct parser_state* state, libab_tree** store_into) { libab_tree* if_branch = NULL; libab_tree* else_branch = NULL; - if(_parser_is_type(state, TOKEN_KW_IF)) { + if (_parser_is_type(state, TOKEN_KW_IF)) { result = _parser_construct_node_vec(state->current_match, store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_IF; _parser_state_step(state); } @@ -396,77 +416,82 @@ libab_result _parse_if(struct parser_state* state, libab_tree** store_into) { result = LIBAB_UNEXPECTED; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, '('); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, - condition, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, condition, + &(*store_into)->children); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ')'); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, - if_branch, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, if_branch, + &(*store_into)->children); } - if(result == LIBAB_SUCCESS) { - if(_parser_is_type(state, TOKEN_KW_ELSE)) { + if (result == LIBAB_SUCCESS) { + if (_parser_is_type(state, TOKEN_KW_ELSE)) { _parser_state_step(state); - PARSE_CHILD(result, state, _parse_expression, - else_branch, &(*store_into)->children); + PARSE_CHILD(result, state, _parse_expression, else_branch, + &(*store_into)->children); } else { - PARSE_CHILD(result, state, _parse_void, - else_branch, &(*store_into)->children); + PARSE_CHILD(result, state, _parse_void, else_branch, + &(*store_into)->children); } } - if(result != LIBAB_SUCCESS) { - if(*store_into) libab_tree_free_recursive(*store_into); + if (result != LIBAB_SUCCESS) { + if (*store_into) + libab_tree_free_recursive(*store_into); *store_into = NULL; } return result; } -libab_result _parse_fun_param(struct parser_state* state, libab_tree** store_into) { +libab_result _parse_fun_param(struct parser_state* state, + libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; - if(_parser_is_type(state, TOKEN_ID)) { - result = _parser_construct_node_string(state, state->current_match, store_into); + if (_parser_is_type(state, TOKEN_ID)) { + result = _parser_construct_node_string(state, state->current_match, + store_into); } else { result = LIBAB_UNEXPECTED; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { _parser_state_step(state); (*store_into)->variant = TREE_FUN_PARAM; libab_ref_null(&(*store_into)->type); result = _parser_consume_char(state, ':'); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_free(&(*store_into)->type); result = _parse_type(state, &(*store_into)->type); } - if(result != LIBAB_SUCCESS && *store_into) { + if (result != LIBAB_SUCCESS && *store_into) { libab_tree_free_recursive(*store_into); *store_into = NULL; } return result; } -libab_result _parse_def_fun(struct parser_state* state, libab_tree** store_into) { +libab_result _parse_def_fun(struct parser_state* state, + libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; libab_tree* temp; - if(_parser_is_type(state, TOKEN_KW_LET)) { + if (_parser_is_type(state, TOKEN_KW_LET)) { _parser_state_step(state); - if(!_parser_eof(state)) { - result = _parser_construct_node_both(state, state->current_match, store_into); + if (!_parser_eof(state)) { + result = _parser_construct_node_both(state, state->current_match, + store_into); } else { result = LIBAB_UNEXPECTED; } @@ -474,27 +499,28 @@ libab_result _parse_def_fun(struct parser_state* state, libab_tree** store_into) result = LIBAB_UNEXPECTED; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { _parser_state_step(state); libab_ref_null(&(*store_into)->type); (*store_into)->variant = TREE_FUN; result = _parser_consume_char(state, ':'); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_free(&(*store_into)->type); result = _parse_type(state, &(*store_into)->type); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_type(state, TOKEN_KW_BE); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, temp, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, temp, + &(*store_into)->children); } - if(result != LIBAB_SUCCESS && *store_into) { + if (result != LIBAB_SUCCESS && *store_into) { libab_tree_free_recursive(*store_into); *store_into = NULL; } @@ -507,51 +533,55 @@ libab_result _parse_fun(struct parser_state* state, libab_tree** store_into) { int is_parenth, is_comma; libab_tree* temp; result = _parser_consume_type(state, TOKEN_KW_FUN); - if(result == LIBAB_SUCCESS) { - if(_parser_is_type(state, TOKEN_ID)) { - result = _parser_construct_node_both(state, state->current_match, store_into); + if (result == LIBAB_SUCCESS) { + if (_parser_is_type(state, TOKEN_ID)) { + result = _parser_construct_node_both(state, state->current_match, + store_into); } else { result = LIBAB_UNEXPECTED; } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { _parser_state_step(state); libab_ref_null(&(*store_into)->type); (*store_into)->variant = TREE_FUN; result = _parser_consume_char(state, '('); } - while(result == LIBAB_SUCCESS && !_parser_eof(state) && !_parser_is_char(state, ')')) { - PARSE_CHILD(result, state, _parse_fun_param, temp, &(*store_into)->children); + while (result == LIBAB_SUCCESS && !_parser_eof(state) && + !_parser_is_char(state, ')')) { + PARSE_CHILD(result, state, _parse_fun_param, temp, + &(*store_into)->children); is_parenth = _parser_is_char(state, ')'); is_comma = _parser_is_char(state, ','); - if(result == LIBAB_SUCCESS && !(is_parenth || is_comma)) { + if (result == LIBAB_SUCCESS && !(is_parenth || is_comma)) { result = LIBAB_UNEXPECTED; - } else if(result == LIBAB_SUCCESS && is_comma) { + } else if (result == LIBAB_SUCCESS && is_comma) { _parser_state_step(state); - if(_parser_is_char(state, ')')) { + if (_parser_is_char(state, ')')) { result = LIBAB_UNEXPECTED; } } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ')'); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ':'); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_free(&(*store_into)->type); result = _parse_type(state, &(*store_into)->type); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_braced_block, temp, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_braced_block, temp, + &(*store_into)->children); } - if(result != LIBAB_SUCCESS && *store_into) { + if (result != LIBAB_SUCCESS && *store_into) { libab_tree_free_recursive(*store_into); *store_into = NULL; } @@ -559,12 +589,13 @@ libab_result _parse_fun(struct parser_state* state, libab_tree** store_into) { return result; } -libab_result _parse_return(struct parser_state* state, libab_tree** store_into) { +libab_result _parse_return(struct parser_state* state, + libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; libab_tree* child = NULL; - if(_parser_is_type(state, TOKEN_KW_RETURN)) { + if (_parser_is_type(state, TOKEN_KW_RETURN)) { result = _parser_construct_node_vec(state->current_match, store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_RETURN; _parser_state_step(state); } @@ -572,12 +603,14 @@ libab_result _parse_return(struct parser_state* state, libab_tree** store_into) result = LIBAB_UNEXPECTED; } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, child, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, child, + &(*store_into)->children); } - if(result != LIBAB_SUCCESS) { - if(*store_into) libab_tree_free_recursive(*store_into); + if (result != LIBAB_SUCCESS) { + if (*store_into) + libab_tree_free_recursive(*store_into); *store_into = NULL; } @@ -589,9 +622,9 @@ libab_result _parse_while(struct parser_state* state, libab_tree** store_into) { libab_tree* condition = NULL; libab_tree* value = NULL; - if(_parser_is_type(state, TOKEN_KW_WHILE)) { + if (_parser_is_type(state, TOKEN_KW_WHILE)) { result = _parser_construct_node_vec(state->current_match, store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_WHILE; _parser_state_step(state); } @@ -599,38 +632,42 @@ libab_result _parse_while(struct parser_state* state, libab_tree** store_into) { result = LIBAB_UNEXPECTED; } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, '('); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, condition, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, condition, + &(*store_into)->children); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ')'); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, value, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, value, + &(*store_into)->children); } - if(result != LIBAB_SUCCESS) { - if(*store_into) libab_tree_free_recursive(*store_into); + if (result != LIBAB_SUCCESS) { + if (*store_into) + libab_tree_free_recursive(*store_into); *store_into = NULL; } - + return result; } -libab_result _parse_dowhile(struct parser_state* state, libab_tree** store_into) { +libab_result _parse_dowhile(struct parser_state* state, + libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; libab_tree* value = NULL; libab_tree* condition = NULL; - if(_parser_is_type(state, TOKEN_KW_DO)) { + if (_parser_is_type(state, TOKEN_KW_DO)) { result = _parser_construct_node_vec(state->current_match, store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_DOWHILE; _parser_state_step(state); } @@ -638,23 +675,25 @@ libab_result _parse_dowhile(struct parser_state* state, libab_tree** store_into) result = LIBAB_UNEXPECTED; } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, value, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, value, + &(*store_into)->children); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_type(state, TOKEN_KW_WHILE); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, '('); } - if(result == LIBAB_SUCCESS) { - PARSE_CHILD(result, state, _parse_expression, condition, &(*store_into)->children); + if (result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, condition, + &(*store_into)->children); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ')'); } @@ -662,12 +701,12 @@ libab_result _parse_dowhile(struct parser_state* state, libab_tree** store_into) } libab_result _parse_call(struct parser_state* state, libab_tree** store_into) { - libab_result result = LIBAB_SUCCESS; + libab_result result = LIBAB_SUCCESS; libab_tree* temp; - if(_parser_is_char(state, '(')) { + if (_parser_is_char(state, '(')) { result = _parser_construct_node_vec(state->current_match, store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_CALL; } _parser_state_step(state); @@ -675,21 +714,25 @@ libab_result _parse_call(struct parser_state* state, libab_tree** store_into) { result = LIBAB_UNEXPECTED; } - while(result == LIBAB_SUCCESS && !_parser_eof(state) && !_parser_is_char(state, ')')) { - PARSE_CHILD(result, state, _parse_expression, temp, &(*store_into)->children); + while (result == LIBAB_SUCCESS && !_parser_eof(state) && + !_parser_is_char(state, ')')) { + PARSE_CHILD(result, state, _parse_expression, temp, + &(*store_into)->children); - if(result == LIBAB_SUCCESS && !(_parser_is_char(state, ')') || _parser_is_char(state, ','))) { + if (result == LIBAB_SUCCESS && + !(_parser_is_char(state, ')') || _parser_is_char(state, ','))) { result = LIBAB_UNEXPECTED; - } else if(_parser_is_char(state, ',')) { + } else if (_parser_is_char(state, ',')) { _parser_state_step(state); } } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_consume_char(state, ')'); } - if(result != LIBAB_SUCCESS) { - if(*store_into) libab_tree_free_recursive(*store_into); + if (result != LIBAB_SUCCESS) { + if (*store_into) + libab_tree_free_recursive(*store_into); *store_into = NULL; } @@ -700,9 +743,9 @@ libab_result _parser_append_call(struct parser_state* state, ll* append_to) { libab_result result; libab_tree* into; result = _parse_call(state, &into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(ll_append(append_to, into)); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_tree_free_recursive(into); } } @@ -711,25 +754,27 @@ libab_result _parser_append_call(struct parser_state* state, ll* append_to) { libab_result _parse_atom(struct parser_state* state, libab_tree** store_into) { libab_result result; - if(_parser_is_type(state, TOKEN_NUM) || _parser_is_type(state, TOKEN_ID)) { - result = _parser_construct_node_string(state, state->current_match, store_into); - if(result == LIBAB_SUCCESS) { - (*store_into)->variant = (state->current_match->type == TOKEN_NUM) ? TREE_NUM : TREE_ID; + if (_parser_is_type(state, TOKEN_NUM) || _parser_is_type(state, TOKEN_ID)) { + result = _parser_construct_node_string(state, state->current_match, + store_into); + if (result == LIBAB_SUCCESS) { + (*store_into)->variant = + (state->current_match->type == TOKEN_NUM) ? TREE_NUM : TREE_ID; } _parser_state_step(state); - } else if(_parser_is_type(state, TOKEN_KW_IF)) { + } else if (_parser_is_type(state, TOKEN_KW_IF)) { result = _parse_if(state, store_into); - } else if(_parser_is_type(state, TOKEN_KW_WHILE)) { + } else if (_parser_is_type(state, TOKEN_KW_WHILE)) { result = _parse_while(state, store_into); - } else if(_parser_is_type(state, TOKEN_KW_DO)) { + } else if (_parser_is_type(state, TOKEN_KW_DO)) { result = _parse_dowhile(state, store_into); - } else if(_parser_is_char(state, '{')) { + } else if (_parser_is_char(state, '{')) { result = _parse_braced_block(state, store_into); - } else if(_parser_is_type(state, TOKEN_KW_FUN)) { + } else if (_parser_is_type(state, TOKEN_KW_FUN)) { result = _parse_fun(state, store_into); - } else if(_parser_is_type(state, TOKEN_KW_LET)) { + } else if (_parser_is_type(state, TOKEN_KW_LET)) { result = _parse_def_fun(state, store_into); - } else if(_parser_is_type(state, TOKEN_KW_RETURN)) { + } else if (_parser_is_type(state, TOKEN_KW_RETURN)) { result = _parse_return(state, store_into); } else { result = LIBAB_UNEXPECTED; @@ -741,24 +786,25 @@ libab_result _parser_append_atom(struct parser_state* state, ll* append_to) { libab_result result; libab_tree* tree; result = _parse_atom(state, &tree); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(ll_append(append_to, tree)); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_tree_free_recursive(tree); } } return result; } -libab_result _parser_construct_op(struct parser_state* state, libab_lexer_match* match, libab_tree** into) { +libab_result _parser_construct_op(struct parser_state* state, + libab_lexer_match* match, libab_tree** into) { libab_result result = _parser_construct_node_both(state, match, into); - if(result == LIBAB_SUCCESS) { - if(match->type == TOKEN_OP_INFIX) { + if (result == LIBAB_SUCCESS) { + if (match->type == TOKEN_OP_INFIX) { (*into)->variant = TREE_OP; - } else if(match->type == TOKEN_OP_RESERVED) { + } else if (match->type == TOKEN_OP_RESERVED) { (*into)->variant = TREE_RESERVED_OP; - } else if(match->type == TOKEN_OP_PREFIX) { + } else if (match->type == TOKEN_OP_PREFIX) { (*into)->variant = TREE_PREFIX_OP; } else { (*into)->variant = TREE_POSTFIX_OP; @@ -767,13 +813,14 @@ libab_result _parser_construct_op(struct parser_state* state, libab_lexer_match* return result; } -libab_result _parser_append_op_node(struct parser_state* state, libab_lexer_match* match, ll* append_to) { +libab_result _parser_append_op_node(struct parser_state* state, + libab_lexer_match* match, ll* append_to) { libab_result result; libab_tree* new_tree = NULL; result = _parser_construct_op(state, match, &new_tree); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(ll_append(append_to, new_tree)); - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { libab_tree_free(new_tree); free(new_tree); } @@ -781,26 +828,26 @@ libab_result _parser_append_op_node(struct parser_state* state, libab_lexer_matc return result; } - /* Expression-specific utility functions */ int _parser_match_is_op(libab_lexer_match* match) { - return match->type == TOKEN_OP || - match->type == TOKEN_OP_INFIX || - match->type == TOKEN_OP_PREFIX || - match->type == TOKEN_OP_POSTFIX || - match->type == TOKEN_OP_RESERVED; + return match->type == TOKEN_OP || match->type == TOKEN_OP_INFIX || + match->type == TOKEN_OP_PREFIX || match->type == TOKEN_OP_POSTFIX || + match->type == TOKEN_OP_RESERVED; } -libab_result _parser_pop_brackets(struct parser_state* state, ll* pop_from, ll* push_to, char bracket, int* success) { +libab_result _parser_pop_brackets(struct parser_state* state, ll* pop_from, + ll* push_to, char bracket, int* success) { libab_result result = LIBAB_SUCCESS; libab_lexer_match* remaining_match; - while(result == LIBAB_SUCCESS && pop_from->tail && _parser_match_is_op(pop_from->tail->data)) { + while (result == LIBAB_SUCCESS && pop_from->tail && + _parser_match_is_op(pop_from->tail->data)) { libab_lexer_match* new_match = ll_poptail(pop_from); result = _parser_append_op_node(state, new_match, push_to); } remaining_match = (pop_from->tail) ? pop_from->tail->data : NULL; - *success = remaining_match && (remaining_match->type == TOKEN_CHAR) && (state->string[remaining_match->from] == bracket); + *success = remaining_match && (remaining_match->type == TOKEN_CHAR) && + (state->string[remaining_match->from] == bracket); return result; } @@ -815,71 +862,84 @@ enum parser_expression_type { }; int _parser_can_prefix_follow(enum parser_expression_type type) { - return type == EXPR_OPEN_PARENTH || type == EXPR_OP_PREFIX || type == EXPR_OP_INFIX || type == EXPR_NONE; + return type == EXPR_OPEN_PARENTH || type == EXPR_OP_PREFIX || + type == EXPR_OP_INFIX || type == EXPR_NONE; } int _parser_can_postfix_follow(enum parser_expression_type type) { - return type == EXPR_CLOSE_PARENTH || type == EXPR_ATOM || type == EXPR_OP_POSTFIX; + return type == EXPR_CLOSE_PARENTH || type == EXPR_ATOM || + type == EXPR_OP_POSTFIX; } int _parser_can_atom_follow(enum parser_expression_type type) { - return !(type == EXPR_CLOSE_PARENTH || type == EXPR_OP_POSTFIX || type == EXPR_ATOM); + return !(type == EXPR_CLOSE_PARENTH || type == EXPR_OP_POSTFIX || + type == EXPR_ATOM); } -void _parser_find_operator_infix(struct parser_state* state, libab_lexer_match* match, struct operator_data* data) { +void _parser_find_operator_infix(struct parser_state* state, + libab_lexer_match* match, + struct operator_data* data) { char op_buffer[8]; _parser_extract_token_buffer(state, op_buffer, 8, match); - if(match->type != TOKEN_OP_RESERVED) { - libab_operator* operator = libab_table_search_operator(state->base_table, op_buffer, OPERATOR_INFIX); + if (match->type != TOKEN_OP_RESERVED) { + libab_operator* operator= libab_table_search_operator( + state->base_table, op_buffer, OPERATOR_INFIX); data->associativity = operator->associativity; data->precedence = operator->precedence; } else { - const libab_reserved_operator* operator = libab_find_reserved_operator(op_buffer); + const libab_reserved_operator* operator= + libab_find_reserved_operator(op_buffer); data->associativity = operator->associativity; data->precedence = operator->precedence; } } -libab_result _parser_expression_tree(struct parser_state* state, ll* source, libab_tree** into) { +libab_result _parser_expression_tree(struct parser_state* state, ll* source, + libab_tree** into) { libab_result result = LIBAB_SUCCESS; libab_tree* top = ll_poptail(source); - if(top == NULL) { + if (top == NULL) { result = LIBAB_UNEXPECTED; - } else if(top->variant == TREE_OP || top->variant == TREE_RESERVED_OP) { + } else if (top->variant == TREE_OP || top->variant == TREE_RESERVED_OP) { libab_tree* left = NULL; libab_tree* right = NULL; result = _parser_expression_tree(state, source, &right); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = _parser_expression_tree(state, source, &left); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(vec_add(&top->children, left)); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(vec_add(&top->children, right)); } - if(result != LIBAB_SUCCESS) { - if(left) libab_tree_free_recursive(left); - if(right) libab_tree_free_recursive(right); + if (result != LIBAB_SUCCESS) { + if (left) + libab_tree_free_recursive(left); + if (right) + libab_tree_free_recursive(right); libab_tree_free(top); free(top); top = NULL; } - } else if(top->variant == TREE_PREFIX_OP || top->variant == TREE_POSTFIX_OP || top->variant == TREE_CALL) { + } else if (top->variant == TREE_PREFIX_OP || + top->variant == TREE_POSTFIX_OP || top->variant == TREE_CALL) { libab_tree* child = NULL; result = _parser_expression_tree(state, source, &child); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_convert_ds_result(vec_add(&top->children, child)); } - if(result != LIBAB_SUCCESS) { - if(child) libab_tree_free_recursive(child); - if(top->variant == TREE_PREFIX_OP || top->variant == TREE_POSTFIX_OP) { + if (result != LIBAB_SUCCESS) { + if (child) + libab_tree_free_recursive(child); + if (top->variant == TREE_PREFIX_OP || + top->variant == TREE_POSTFIX_OP) { libab_tree_free(top); free(top); } else { @@ -892,41 +952,51 @@ libab_result _parser_expression_tree(struct parser_state* state, ll* source, lib return result; } -int _parser_match_is_postfix_op(struct parser_state* state, libab_lexer_match* match) { +int _parser_match_is_postfix_op(struct parser_state* state, + libab_lexer_match* match) { int is_postfix = 0; libab_operator* operator; char op_buffer[8]; _parser_extract_token_buffer(state, op_buffer, 8, match); - operator = libab_table_search_operator(state->base_table, op_buffer, OPERATOR_POSTFIX); - if(operator) is_postfix = 1; + operator= libab_table_search_operator(state->base_table, op_buffer, + OPERATOR_POSTFIX); + if (operator) + is_postfix = 1; return is_postfix; } -int _parser_match_is_prefix_op(struct parser_state* state, libab_lexer_match* match) { +int _parser_match_is_prefix_op(struct parser_state* state, + libab_lexer_match* match) { int is_prefix = 0; libab_operator* operator; char op_buffer[8]; _parser_extract_token_buffer(state, op_buffer, 8, match); - operator = libab_table_search_operator(state->base_table, op_buffer, OPERATOR_PREFIX); - if(operator) is_prefix = 1; + operator= libab_table_search_operator(state->base_table, op_buffer, + OPERATOR_PREFIX); + if (operator) + is_prefix = 1; return is_prefix; } -int _parser_match_is_infix_op(struct parser_state* state, libab_lexer_match* match) { +int _parser_match_is_infix_op(struct parser_state* state, + libab_lexer_match* match) { int is_infix = 0; - if(match->type == TOKEN_OP_RESERVED) { + if (match->type == TOKEN_OP_RESERVED) { is_infix = 1; } else { libab_operator* operator; char op_buffer[8]; _parser_extract_token_buffer(state, op_buffer, 8, match); - operator = libab_table_search_operator(state->base_table, op_buffer, OPERATOR_INFIX); - if(operator) is_infix = 1; + operator= libab_table_search_operator(state->base_table, op_buffer, + OPERATOR_INFIX); + if (operator) + is_infix = 1; } return is_infix; } -libab_result _parse_expression(struct parser_state* state, libab_tree** store_into) { +libab_result _parse_expression(struct parser_state* state, + libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; struct operator_data operator; struct operator_data other_operator; @@ -939,95 +1009,105 @@ libab_result _parse_expression(struct parser_state* state, libab_tree** store_in ll_init(&op_stack); *store_into = NULL; - while(result == LIBAB_SUCCESS && !_parser_eof(state)) { + while (result == LIBAB_SUCCESS && !_parser_eof(state)) { enum parser_expression_type new_type = EXPR_NONE; libab_lexer_match* new_token = state->current_match; char current_char = state->string[new_token->from]; - if(_parser_is_type(state, TOKEN_CHAR) && current_char != '{') { - if(current_char == '(' && _parser_can_postfix_follow(last_type)) { + if (_parser_is_type(state, TOKEN_CHAR) && current_char != '{') { + if (current_char == '(' && _parser_can_postfix_follow(last_type)) { result = _parser_append_call(state, &out_stack); - if(result != LIBAB_SUCCESS) break; + if (result != LIBAB_SUCCESS) + break; new_type = EXPR_OP_POSTFIX; - } else if(current_char == '(') { - result = libab_convert_ds_result(ll_append(&op_stack, new_token)); - if(result != LIBAB_SUCCESS) break; + } else if (current_char == '(') { + result = + libab_convert_ds_result(ll_append(&op_stack, new_token)); + if (result != LIBAB_SUCCESS) + break; _parser_state_step(state); new_type = EXPR_OPEN_PARENTH; - } else if(current_char == ')') { - result = _parser_pop_brackets(state, &op_stack, &out_stack, '(', &pop_success); - if(result != LIBAB_SUCCESS || !pop_success) break; + } else if (current_char == ')') { + result = _parser_pop_brackets(state, &op_stack, &out_stack, '(', + &pop_success); + if (result != LIBAB_SUCCESS || !pop_success) + break; ll_poptail(&op_stack); _parser_state_step(state); new_type = EXPR_CLOSE_PARENTH; } else { break; } - } else if(_parser_match_is_prefix_op(state, new_token) && - _parser_can_prefix_follow(last_type)) { + } else if (_parser_match_is_prefix_op(state, new_token) && + _parser_can_prefix_follow(last_type)) { new_token->type = TOKEN_OP_PREFIX; result = libab_convert_ds_result(ll_append(&op_stack, new_token)); - if(result != LIBAB_SUCCESS) break; + if (result != LIBAB_SUCCESS) + break; _parser_state_step(state); new_type = EXPR_OP_PREFIX; - } else if(_parser_match_is_postfix_op(state, new_token) && - _parser_can_postfix_follow(last_type)) { + } else if (_parser_match_is_postfix_op(state, new_token) && + _parser_can_postfix_follow(last_type)) { new_token->type = TOKEN_OP_POSTFIX; result = _parser_append_op_node(state, new_token, &out_stack); _parser_state_step(state); new_type = EXPR_OP_POSTFIX; - } else if(_parser_match_is_infix_op(state, new_token)) { - if(new_token->type == TOKEN_OP) + } else if (_parser_match_is_infix_op(state, new_token)) { + if (new_token->type == TOKEN_OP) new_token->type = TOKEN_OP_INFIX; _parser_find_operator_infix(state, new_token, &operator); _parser_state_step(state); - while(result == LIBAB_SUCCESS && op_stack.tail && - _parser_match_is_op(op_stack.tail->data)) { - _parser_find_operator_infix(state, op_stack.tail->data, &other_operator); + while (result == LIBAB_SUCCESS && op_stack.tail && + _parser_match_is_op(op_stack.tail->data)) { + _parser_find_operator_infix(state, op_stack.tail->data, + &other_operator); - if(((libab_lexer_match*)op_stack.tail->data)->type == TOKEN_OP_PREFIX || - (operator.associativity == -1 && - operator.precedence <= other_operator.precedence) || - (operator.associativity == 1 && - operator.precedence < other_operator.precedence)) { + if (((libab_lexer_match*)op_stack.tail->data)->type == + TOKEN_OP_PREFIX || + (operator.associativity == - 1 && + operator.precedence <= other_operator.precedence) || + (operator.associativity == 1 && + operator.precedencecurrent_match, store_into); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_BLOCK; } - if(expect_braces && result == LIBAB_SUCCESS) result = _parser_consume_char(state, '{'); + if (expect_braces && result == LIBAB_SUCCESS) + result = _parser_consume_char(state, '{'); - while(result == LIBAB_SUCCESS && - !_parser_eof(state) && - !(expect_braces && _parser_is_char(state, '}'))) { - PARSE_CHILD(result, state, _parse_expression, temp, &(*store_into)->children); - if(_parser_is_char(state, ';')) { + while (result == LIBAB_SUCCESS && !_parser_eof(state) && + !(expect_braces && _parser_is_char(state, '}'))) { + PARSE_CHILD(result, state, _parse_expression, temp, + &(*store_into)->children); + if (_parser_is_char(state, ';')) { temp = NULL; _parser_state_step(state); } } - if(result == LIBAB_SUCCESS && temp == NULL) { + if (result == LIBAB_SUCCESS && temp == NULL) { PARSE_CHILD(result, state, _parse_void, temp, &(*store_into)->children); } - if(expect_braces && result == LIBAB_SUCCESS) result = _parser_consume_char(state, '}'); + if (expect_braces && result == LIBAB_SUCCESS) + result = _parser_consume_char(state, '}'); - if(result != LIBAB_SUCCESS && *store_into) { + if (result != LIBAB_SUCCESS && *store_into) { libab_tree_free_recursive(*store_into); *store_into = NULL; } @@ -1077,25 +1159,24 @@ void libab_parser_init(libab_parser* parser, libab_table* table) { parser->base_table = table; } libab_result libab_parser_parse(libab_parser* parser, ll* tokens, - const char* string, libab_tree** store_into) { + const char* string, libab_tree** store_into) { libab_result result; struct parser_state state; _parser_state_init(&state, tokens, string, parser->base_table); result = _parse_block(&state, store_into, 0); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { (*store_into)->variant = TREE_BASE; } return result; } libab_result libab_parser_parse_type(libab_parser* parser, ll* tokens, - const char* string, libab_ref* store_into) { + const char* string, + libab_ref* store_into) { struct parser_state state; _parser_state_init(&state, tokens, string, parser->base_table); return _parse_type(&state, store_into); } -void libab_parser_free(libab_parser* parser) { - parser->base_table = NULL; -} +void libab_parser_free(libab_parser* parser) { parser->base_table = NULL; } diff --git a/src/parsetype.c b/src/parsetype.c index 0586f91..ba922ea 100644 --- a/src/parsetype.c +++ b/src/parsetype.c @@ -2,10 +2,10 @@ #include void libab_parsetype_free(libab_parsetype* type) { - if(!(type->variant & LIBABACUS_TYPE_F_RESOLVED)) { + if (!(type->variant & LIBABACUS_TYPE_F_RESOLVED)) { free(type->data_u.name); } - if(type->variant & LIBABACUS_TYPE_F_PARENT) { + if (type->variant & LIBABACUS_TYPE_F_PARENT) { libab_ref_vec_free(&(type->children)); } } diff --git a/src/ref_trie.c b/src/ref_trie.c index 9c1c778..ec320d9 100644 --- a/src/ref_trie.c +++ b/src/ref_trie.c @@ -6,13 +6,14 @@ void libab_ref_trie_init(libab_ref_trie* trie) { trie->head = NULL; } -libab_result _libab_ref_trie_put(libab_ref_trie_node** node, const char* key, libab_ref* ref) { +libab_result _libab_ref_trie_put(libab_ref_trie_node** node, const char* key, + libab_ref* ref) { libab_result result = LIBAB_SUCCESS; - if((*node = malloc(sizeof(**node)))) { + if ((*node = malloc(sizeof(**node)))) { (*node)->key = *key; (*node)->next = NULL; - if(*(key + 1)) { + if (*(key + 1)) { libab_ref_null(&(*node)->ref); result = _libab_ref_trie_put(&(*node)->child, key + 1, ref); } else { @@ -23,8 +24,9 @@ libab_result _libab_ref_trie_put(libab_ref_trie_node** node, const char* key, li result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS) { - if(*node) libab_ref_free(&(*node)->ref); + if (result != LIBAB_SUCCESS) { + if (*node) + libab_ref_free(&(*node)->ref); free(*node); *node = NULL; } @@ -32,17 +34,18 @@ libab_result _libab_ref_trie_put(libab_ref_trie_node** node, const char* key, li return result; } -libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, libab_ref* ref) { +libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, + libab_ref* ref) { libab_result result = LIBAB_SUCCESS; libab_ref_trie_node** current = &trie->head; char search; - while(*key) { + while (*key) { search = *key; - while(*current && (*current)->key != search) { + while (*current && (*current)->key != search) { current = &(*current)->next; } - if(*current) { - if(*(key + 1)) { + if (*current) { + if (*(key + 1)) { current = &(*current)->child; } else { libab_ref_free(&(*current)->ref); @@ -57,15 +60,17 @@ libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key, libab_ref return result; } -const libab_ref* libab_ref_trie_get(const libab_ref_trie* trie, const char* key) { +const libab_ref* libab_ref_trie_get(const libab_ref_trie* trie, + const char* key) { libab_ref_trie_node* current = trie->head; - while(current && *key) { - while(current && current->key != *key) { + while (current && *key) { + while (current && current->key != *key) { current = current->next; } - if(current == NULL) break; + if (current == NULL) + break; - if(*(key + 1)) { + if (*(key + 1)) { current = current->child; key++; } else { @@ -76,7 +81,8 @@ const libab_ref* libab_ref_trie_get(const libab_ref_trie* trie, const char* key) } void _libab_ref_trie_free(libab_ref_trie_node* node) { - if(node == NULL) return; + if (node == NULL) + return; _libab_ref_trie_free(node->next); _libab_ref_trie_free(node->child); libab_ref_free(&node->ref); diff --git a/src/ref_vec.c b/src/ref_vec.c index 0b2c020..6867ff5 100644 --- a/src/ref_vec.c +++ b/src/ref_vec.c @@ -5,9 +5,8 @@ libab_result libab_ref_vec_init(libab_ref_vec* vec) { libab_result result = LIBAB_SUCCESS; vec->capacity = LIBABACUS_REF_VEC_INITIAL_SIZE; vec->size = 0; - vec->data = - malloc(sizeof(*vec->data) * LIBABACUS_REF_VEC_INITIAL_SIZE); - if(vec->data == NULL) { + vec->data = malloc(sizeof(*vec->data) * LIBABACUS_REF_VEC_INITIAL_SIZE); + if (vec->data == NULL) { result = LIBAB_MALLOC; } return result; @@ -15,9 +14,10 @@ libab_result libab_ref_vec_init(libab_ref_vec* vec) { libab_result _libab_ref_vec_try_resize(libab_ref_vec* vec) { libab_result result = LIBAB_SUCCESS; - if(vec->size == vec->capacity) { - libab_ref* new_memory = realloc(vec->data, (vec->capacity *= 2) * sizeof(*vec->data)); - if(new_memory == NULL) { + if (vec->size == vec->capacity) { + libab_ref* new_memory = + realloc(vec->data, (vec->capacity *= 2) * sizeof(*vec->data)); + if (new_memory == NULL) { free(vec->data); result = LIBAB_MALLOC; } @@ -28,30 +28,31 @@ libab_result _libab_ref_vec_try_resize(libab_ref_vec* vec) { libab_result libab_ref_vec_insert(libab_ref_vec* vec, libab_ref* data) { libab_result result = _libab_ref_vec_try_resize(vec); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { libab_ref_copy(data, &vec->data[vec->size++]); } return result; } -libab_result libab_ref_vec_insert_value(libab_ref_vec* vec, void* data, void (*free_func)(void*)) { +libab_result libab_ref_vec_insert_value(libab_ref_vec* vec, void* data, + void (*free_func)(void*)) { libab_result result = _libab_ref_vec_try_resize(vec); - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { result = libab_ref_new(&vec->data[vec->size], data, free_func); } - if(result == LIBAB_SUCCESS) { + if (result == LIBAB_SUCCESS) { vec->size++; } - + return result; } const libab_ref* libab_ref_vec_index(libab_ref_vec* vec, size_t index) { const libab_ref* to_return = NULL; - if(index < vec->size) { + if (index < vec->size) { to_return = &vec->data[index]; } return to_return; @@ -59,7 +60,7 @@ const libab_ref* libab_ref_vec_index(libab_ref_vec* vec, size_t index) { void libab_ref_vec_free(libab_ref_vec* vec) { size_t i = 0; - for(; i < vec->size; i++) { + for (; i < vec->size; i++) { libab_ref_free(&vec->data[i]); } free(vec->data); diff --git a/src/refcount.c b/src/refcount.c index 5e7c643..db7f7c1 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -2,11 +2,12 @@ #include #include -libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* data)) { +libab_result libab_ref_new(libab_ref* ref, void* data, + void (*free_func)(void* data)) { libab_result result = LIBAB_SUCCESS; ref->strong = 1; ref->data = data; - if((ref->count = malloc(sizeof(*(ref->count))))) { + if ((ref->count = malloc(sizeof(*(ref->count))))) { ref->count->strong = ref->count->weak = 1; ref->count->free_func = free_func; } else { @@ -15,11 +16,7 @@ libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* d return result; } -static libab_ref_count null_count = { - NULL, - 0, - 1 -}; +static libab_ref_count null_count = {NULL, 0, 1}; void libab_ref_null(libab_ref* ref) { ref->strong = 0; @@ -29,20 +26,20 @@ void libab_ref_null(libab_ref* ref) { } void _libab_ref_changed(libab_ref* ref) { - if(ref->count->strong == 0) { + if (ref->count->strong == 0) { ref->count->strong--; - if(ref->count->free_func) { + if (ref->count->free_func) { ref->count->free_func(ref->data); } free(ref->data); } - if(ref->count->weak == 0) { + if (ref->count->weak == 0) { free(ref->count); } } void libab_ref_weaken(libab_ref* ref) { - if(ref->strong) { + if (ref->strong) { ref->count->strong--; ref->strong = 0; _libab_ref_changed(ref); @@ -63,7 +60,7 @@ void libab_ref_copy(const libab_ref* ref, libab_ref* into) { void* libab_ref_get(const libab_ref* ref) { void* to_return = NULL; - if(ref->count->strong > 0) { + if (ref->count->strong > 0) { to_return = ref->data; } return to_return; diff --git a/src/reserved.c b/src/reserved.c index 2283272..04983ce 100644 --- a/src/reserved.c +++ b/src/reserved.c @@ -2,20 +2,18 @@ #include "string.h" #include "util.h" -static const libab_reserved_operator libab_reserved_operators[] = { - { - "=", /* Assignment */ - 0, /* Lowest precedence */ - 1 /* Right associative, a = b = 6 should be a = (b = 6) */ - } -}; -static const size_t element_count = +static const libab_reserved_operator libab_reserved_operators[] = {{ + "=", /* Assignment */ + 0, /* Lowest precedence */ + 1 /* Right associative, a = b = 6 should be a = (b = 6) */ +}}; +static const size_t element_count = sizeof(libab_reserved_operators) / sizeof(libab_reserved_operator); const libab_reserved_operator* libab_find_reserved_operator(const char* name) { size_t i; - for(i = 0; i < element_count; i++) { - if(strcmp(name, libab_reserved_operators[i].op) == 0) + for (i = 0; i < element_count; i++) { + if (strcmp(name, libab_reserved_operators[i].op) == 0) return &libab_reserved_operators[i]; } return NULL; @@ -24,9 +22,9 @@ const libab_reserved_operator* libab_find_reserved_operator(const char* name) { libab_result libab_register_reserved_operators(libab_lexer* lexer) { libab_result result = LIBAB_SUCCESS; size_t i; - for(i = 0; i < element_count && result == LIBAB_SUCCESS; i++) { - result = libab_convert_lex_result(eval_config_add(&lexer->config, - libab_reserved_operators[i].op, TOKEN_OP_RESERVED)); + for (i = 0; i < element_count && result == LIBAB_SUCCESS; i++) { + result = libab_convert_lex_result(eval_config_add( + &lexer->config, libab_reserved_operators[i].op, TOKEN_OP_RESERVED)); } return result; } @@ -34,9 +32,9 @@ libab_result libab_register_reserved_operators(libab_lexer* lexer) { libab_result libab_remove_reserved_operators(libab_lexer* lexer) { libab_result result = LIBAB_SUCCESS; size_t i; - for(i = 0; i < element_count && result == LIBAB_SUCCESS; i++) { - result = libab_convert_lex_result(eval_config_remove(&lexer->config, - libab_reserved_operators[i].op, TOKEN_OP_RESERVED)); + for (i = 0; i < element_count && result == LIBAB_SUCCESS; i++) { + result = libab_convert_lex_result(eval_config_remove( + &lexer->config, libab_reserved_operators[i].op, TOKEN_OP_RESERVED)); } return result; } diff --git a/src/table.c b/src/table.c index 86ce5a0..0cd5264 100644 --- a/src/table.c +++ b/src/table.c @@ -1,19 +1,21 @@ #include "table.h" -#include -#include "util.h" #include "lexer.h" +#include "util.h" +#include void libab_table_init(libab_table* table) { libab_trie_init(&table->trie); table->parent = NULL; -} -libab_table_entry* libab_table_search_filter(libab_table* table, const char* string, void* data, compare_func compare) { +} +libab_table_entry* libab_table_search_filter(libab_table* table, + const char* string, void* data, + compare_func compare) { void* to_return = NULL; do { const ll* matches = libab_trie_get(&table->trie, string); to_return = ll_find(matches, data, compare); table = table->parent; - } while(table && to_return == NULL); + } while (table && to_return == NULL); return to_return; } libab_table_entry* libab_table_search(libab_table* table, const char* string) { @@ -21,27 +23,32 @@ libab_table_entry* libab_table_search(libab_table* table, const char* string) { do { to_return = ll_head(libab_trie_get(&table->trie, string)); table = table->parent; - } while(table && to_return == NULL); + } while (table && to_return == NULL); return to_return; } -#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;\ -} +#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, OPERATOR_PREFIX) OP_TYPE_COMPARATOR(_table_compare_infix, OPERATOR_INFIX) OP_TYPE_COMPARATOR(_table_compare_postfix, OPERATOR_POSTFIX) -libab_operator* libab_table_search_operator(libab_table* table, const char* string, int type) { +libab_operator* libab_table_search_operator(libab_table* table, + const char* string, int type) { libab_table_entry* entry = NULL; - if(type == OPERATOR_PREFIX) { - entry = libab_table_search_filter(table, string, NULL, _table_compare_prefix); - } else if(type == OPERATOR_INFIX) { - entry = libab_table_search_filter(table, string, NULL, _table_compare_infix); - } else if(type == OPERATOR_PREFIX) { - entry = libab_table_search_filter(table, string, NULL, _table_compare_postfix); + if (type == OPERATOR_PREFIX) { + entry = libab_table_search_filter(table, string, NULL, + _table_compare_prefix); + } else if (type == OPERATOR_INFIX) { + entry = libab_table_search_filter(table, string, NULL, + _table_compare_infix); + } else if (type == OPERATOR_PREFIX) { + entry = libab_table_search_filter(table, string, NULL, + _table_compare_postfix); } return entry ? &entry->data_u.op : NULL; } @@ -51,8 +58,10 @@ int _table_compare_function(const void* left, const void* right) { return entry->variant == ENTRY_FUN; } -libab_function* libab_table_search_function(libab_table* table, const char* string) { - libab_table_entry* entry = libab_table_search_filter(table, string, NULL, _table_compare_function); +libab_function* libab_table_search_function(libab_table* table, + const char* string) { + libab_table_entry* entry = + libab_table_search_filter(table, string, NULL, _table_compare_function); return entry ? &entry->data_u.function : NULL; } @@ -61,13 +70,15 @@ int _table_compare_basetype(const void* left, const void* 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); +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) { +libab_result libab_table_put(libab_table* table, const char* string, + libab_table_entry* entry) { return libab_trie_put(&table->trie, string, entry); } int _table_foreach_entry_free(void* data, va_list args) { @@ -76,15 +87,16 @@ int _table_foreach_entry_free(void* data, va_list args) { return 0; } void libab_table_free(libab_table* table) { - libab_trie_foreach(&table->trie, NULL, compare_always, _table_foreach_entry_free); + libab_trie_foreach(&table->trie, NULL, compare_always, + _table_foreach_entry_free); libab_trie_free(&table->trie); } void libab_table_entry_free(libab_table_entry* entry) { - if(entry->variant == ENTRY_OP) { + if (entry->variant == ENTRY_OP) { libab_operator_free(&entry->data_u.op); - } else if(entry->variant == ENTRY_FUN) { + } else if (entry->variant == ENTRY_FUN) { libab_function_free(&entry->data_u.function); - } else if(entry->variant == ENTRY_BASETYPE) { + } else if (entry->variant == ENTRY_BASETYPE) { libab_basetype_free(entry->data_u.basetype); } } diff --git a/src/tree.c b/src/tree.c index 58c86fc..da0274a 100644 --- a/src/tree.c +++ b/src/tree.c @@ -3,23 +3,24 @@ int libab_tree_has_vector(libab_tree_variant variant) { return variant == TREE_BASE || variant == TREE_OP || - variant == TREE_PREFIX_OP || variant == TREE_POSTFIX_OP || variant == TREE_BLOCK || - variant == TREE_IF || variant == TREE_CALL || variant == TREE_WHILE || - variant == TREE_DOWHILE || variant == TREE_FUN || - variant == TREE_RETURN || variant == TREE_RESERVED_OP; + variant == TREE_PREFIX_OP || variant == TREE_POSTFIX_OP || + variant == TREE_BLOCK || variant == TREE_IF || + variant == TREE_CALL || variant == TREE_WHILE || + variant == TREE_DOWHILE || variant == TREE_FUN || + variant == TREE_RETURN || variant == TREE_RESERVED_OP; } int libab_tree_has_string(libab_tree_variant variant) { - return variant == TREE_ID || variant == TREE_NUM || - variant == TREE_OP || variant == TREE_PREFIX_OP || variant == TREE_POSTFIX_OP || - variant == TREE_FUN || variant == TREE_FUN_PARAM || - variant == TREE_RESERVED_OP; + return variant == TREE_ID || variant == TREE_NUM || variant == TREE_OP || + variant == TREE_PREFIX_OP || variant == TREE_POSTFIX_OP || + variant == TREE_FUN || variant == TREE_FUN_PARAM || + variant == TREE_RESERVED_OP; } int libab_tree_has_scope(libab_tree_variant variant) { return variant == TREE_BASE || variant == TREE_BLOCK || - variant == TREE_IF || variant == TREE_WHILE || - variant == TREE_DOWHILE || variant == TREE_FUN; + variant == TREE_IF || variant == TREE_WHILE || + variant == TREE_DOWHILE || variant == TREE_FUN; } int libab_tree_has_type(libab_tree_variant variant) { @@ -33,9 +34,11 @@ void libab_tree_free(libab_tree* tree) { free_vector = libab_tree_has_vector(tree->variant); free_string = libab_tree_has_string(tree->variant); free_type = libab_tree_has_type(tree->variant); - if(free_string) free(tree->string_value); - if(free_vector) vec_free(&tree->children); - if(free_type) + if (free_string) + free(tree->string_value); + if (free_vector) + vec_free(&tree->children); + if (free_type) libab_ref_free(&tree->type); } @@ -45,7 +48,7 @@ int _tree_foreach_free(void* data, va_list args) { } void libab_tree_free_recursive(libab_tree* tree) { - if(libab_tree_has_vector(tree->variant)) { + if (libab_tree_has_vector(tree->variant)) { vec_foreach(&tree->children, NULL, compare_always, _tree_foreach_free); } libab_tree_free(tree); diff --git a/src/trie.c b/src/trie.c index 6040f03..e3f4da5 100644 --- a/src/trie.c +++ b/src/trie.c @@ -1,6 +1,6 @@ #include "trie.h" -#include #include "util.h" +#include void libab_trie_init(libab_trie* trie) { trie->head = NULL; @@ -8,31 +8,34 @@ void libab_trie_init(libab_trie* trie) { } void _libab_trie_free(libab_trie_node* to_free) { - if(to_free == NULL) return; + if (to_free == NULL) + return; _libab_trie_free(to_free->next); _libab_trie_free(to_free->child); ll_free(&to_free->values); free(to_free); } -libab_result _libab_trie_put(libab_trie_node** node, const char* key, void* value) { +libab_result _libab_trie_put(libab_trie_node** node, const char* key, + void* value) { libab_result result = LIBAB_SUCCESS; - if((*node = malloc(sizeof(**node)))) { + if ((*node = malloc(sizeof(**node)))) { (*node)->key = *key; (*node)->next = NULL; ll_init(&(*node)->values); - if(*(key + 1)) { + if (*(key + 1)) { result = _libab_trie_put(&(*node)->child, key + 1, value); } else { (*node)->child = NULL; - result = libab_convert_ds_result(ll_append(&(*node)->values, value)); + result = + libab_convert_ds_result(ll_append(&(*node)->values, value)); } } else { result = LIBAB_MALLOC; } - if(result != LIBAB_SUCCESS) { + if (result != LIBAB_SUCCESS) { free(*node); *node = NULL; } @@ -48,13 +51,13 @@ libab_result libab_trie_put(libab_trie* trie, const char* key, void* value) { libab_result result = LIBAB_SUCCESS; libab_trie_node** current = &trie->head; char search; - while(*key) { + while (*key) { search = *key; - while(*current && (*current)->key != search) { + while (*current && (*current)->key != search) { current = &(*current)->next; } - if(*current) { - if(*(key + 1)) { + if (*current) { + if (*(key + 1)) { current = &(*current)->child; } else { result = _libab_trie_add(*current, value); @@ -70,13 +73,14 @@ libab_result libab_trie_put(libab_trie* trie, const char* key, void* value) { const ll* libab_trie_get(const libab_trie* trie, const char* key) { libab_trie_node* current = trie->head; - while(current && *key) { - while(current && current->key != *key) { + while (current && *key) { + while (current && current->key != *key) { current = current->next; } - if(current == NULL) break; - - if(*(key + 1)) { + if (current == NULL) + break; + + if (*(key + 1)) { current = current->child; key++; } else { @@ -86,24 +90,27 @@ const ll* libab_trie_get(const libab_trie* trie, const char* key) { return &trie->empty_list; } -int _libab_trie_foreach(libab_trie_node* node, void* data, compare_func compare, foreach_func foreach) { +int _libab_trie_foreach(libab_trie_node* node, void* data, compare_func compare, + foreach_func foreach) { int return_code; - if(node == NULL) return 0; + if (node == NULL) + return 0; return_code = ll_foreach(&node->values, data, compare, foreach); - if(return_code == 0) { - return_code = _libab_trie_foreach(node->child, data, compare, foreach); + if (return_code == 0) { + return_code = _libab_trie_foreach(node->child, data, compare, foreach); } - if(return_code == 0) { + if (return_code == 0) { return_code = _libab_trie_foreach(node->next, data, compare, foreach); } return return_code; } -int libab_trie_foreach(const libab_trie* trie, void* data, compare_func compare, foreach_func foreach) { +int libab_trie_foreach(const libab_trie* trie, void* data, compare_func compare, + foreach_func foreach) { return _libab_trie_foreach(trie->head, data, compare, foreach); } void libab_trie_free(libab_trie* trie) { - _libab_trie_free(trie->head); + _libab_trie_free(trie->head); trie->head = NULL; } diff --git a/src/types.c b/src/types.c index c523354..6c3f776 100644 --- a/src/types.c +++ b/src/types.c @@ -8,6 +8,4 @@ libab_result libab_array_insert(libab_array* array, libab_ref* value) { return libab_ref_vec_insert(&array->elems, value); } -void libab_array_free(libab_array* array) { - libab_ref_vec_free(&array->elems); -} +void libab_array_free(libab_array* array) { libab_ref_vec_free(&array->elems); } diff --git a/src/util.c b/src/util.c index 3cc12a3..819b912 100644 --- a/src/util.c +++ b/src/util.c @@ -3,26 +3,27 @@ libab_result libab_convert_lex_result(liblex_result to_convert) { libab_result result = LIBAB_SUCCESS; - if(to_convert == LIBLEX_MALLOC) { + if (to_convert == LIBLEX_MALLOC) { result = LIBAB_MALLOC; - } else if(to_convert == LIBLEX_INVALID) { + } else if (to_convert == LIBLEX_INVALID) { result = LIBAB_BAD_PATTERN; - } else if(to_convert == LIBLEX_UNRECOGNIZED) { + } else if (to_convert == LIBLEX_UNRECOGNIZED) { result = LIBAB_FAILED_MATCH; } return result; } libab_result libab_convert_ds_result(libds_result to_convert) { libab_result result = LIBAB_SUCCESS; - if(to_convert == LIBDS_MALLOC) { + if (to_convert == LIBDS_MALLOC) { result = LIBAB_MALLOC; } return result; } -libab_result libab_copy_string_range(char** destination, const char* source, size_t from, size_t to) { +libab_result libab_copy_string_range(char** destination, const char* source, + size_t from, size_t to) { libab_result result = LIBAB_SUCCESS; size_t string_length = to - from; - if((*destination = malloc(string_length + 1)) == NULL) { + if ((*destination = malloc(string_length + 1)) == NULL) { result = LIBAB_MALLOC; } else { strncpy(*destination, source + from, string_length); @@ -30,7 +31,8 @@ libab_result libab_copy_string_range(char** destination, const char* source, siz } return result; } -libab_result libab_copy_string_size(char** destination, const char* source, size_t length) { +libab_result libab_copy_string_size(char** destination, const char* source, + size_t length) { return libab_copy_string_range(destination, source, 0, length); } libab_result libab_copy_string(char** destination, const char* source) { @@ -40,15 +42,18 @@ libab_result _libab_check_parsetype(libab_parsetype* to_check) { libab_result result = LIBAB_SUCCESS; return result; } -libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, libab_table* scope) { +libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, + libab_table* scope) { libab_result result = LIBAB_SUCCESS; int resolve_name, check_parents; size_t index = 0; - resolve_name = !(to_resolve->variant & (LIBABACUS_TYPE_F_RESOLVED | LIBABACUS_TYPE_F_PLACE)); + resolve_name = !(to_resolve->variant & + (LIBABACUS_TYPE_F_RESOLVED | LIBABACUS_TYPE_F_PLACE)); check_parents = !(to_resolve->variant & LIBABACUS_TYPE_F_PLACE); - if(resolve_name) { - libab_basetype* basetype = libab_table_search_basetype(scope, to_resolve->data_u.name); - if(basetype) { + if (resolve_name) { + libab_basetype* basetype = + libab_table_search_basetype(scope, to_resolve->data_u.name); + if (basetype) { free(to_resolve->data_u.name); to_resolve->data_u.base = basetype; to_resolve->variant |= LIBABACUS_TYPE_F_RESOLVED; @@ -57,17 +62,18 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, libab_table* s } } - if(check_parents && result == LIBAB_SUCCESS) { - if(to_resolve->variant & LIBABACUS_TYPE_F_PARENT) { + if (check_parents && result == LIBAB_SUCCESS) { + if (to_resolve->variant & LIBABACUS_TYPE_F_PARENT) { result = _libab_check_parsetype(to_resolve); - } else if(to_resolve->data_u.base->count) { + } else if (to_resolve->data_u.base->count) { result = LIBAB_BAD_TYPE; } } - if(to_resolve->variant & LIBABACUS_TYPE_F_PARENT) { - while(result == LIBAB_SUCCESS && index < to_resolve->children.size) { - result = libab_resolve_parsetype(libab_ref_get(&to_resolve->children.data[index]), scope); + if (to_resolve->variant & LIBABACUS_TYPE_F_PARENT) { + while (result == LIBAB_SUCCESS && index < to_resolve->children.size) { + result = libab_resolve_parsetype( + libab_ref_get(&to_resolve->children.data[index]), scope); index++; } } diff --git a/src/value.c b/src/value.c index 99c5be1..e260073 100644 --- a/src/value.c +++ b/src/value.c @@ -12,5 +12,6 @@ void libab_value_free(libab_value* value) { libab_ref_free(&value->type); value_type = libab_ref_get(&value->type); free_function = value_type->data_u.base->free_function; - if(free_function) free_function(value->data); + if (free_function) + free_function(value->data); }