diff --git a/CMakeLists.txt b/CMakeLists.txt index 9718608..0ac7456 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(libabacus) add_compile_options(-pedantic -Wall) -add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/ref_vec.c src/ref_trie.c src/basetype.c src/value.c src/custom.c src/interpreter.c src/function_list.c) +add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/ref_vec.c src/ref_trie.c src/basetype.c src/value.c src/custom.c src/interpreter.c src/function_list.c src/free_functions.c) add_executable(libabacus src/main.c) add_executable(interactive src/interactive.c) add_subdirectory(external/liblex) diff --git a/include/free_functions.h b/include/free_functions.h new file mode 100644 index 0000000..b549f2a --- /dev/null +++ b/include/free_functions.h @@ -0,0 +1,43 @@ +#ifndef LIBABACUS_FREE_FUNCTIONS_H +#define LIBABACUS_FREE_FUNCTIONS_H + +/** + * Free functions. Because a lot of the reference + * counting operations require free functions, + * and redeclaring them in mutliple files makes no + * sense (also, it doesn't link :^) ), we + * put them all here. + */ + +/** + * Frees a libab_function. + * @param func the function to free. + */ +void free_function(void* func); +/** + * Frees a libab_function_list. + * @param func_list the function list to free. + */ +void free_function_list(void* func_list); +/** + * Frees a unit. This is a no-op. + * @param unit the unit to free. + */ +void free_unit(void* unit); +/** + * Frees a parsetype. + * @param parsetype the parsetype to free. + */ +void free_parsetype(void* parsetype); +/** + * Frees a table. + * @param table the table to free. + */ +void free_table(void* table); +/** + * Frees a value. + * @param value the value to free. + */ +void free_value(void* value); + +#endif diff --git a/src/free_functions.c b/src/free_functions.c new file mode 100644 index 0000000..1474d56 --- /dev/null +++ b/src/free_functions.c @@ -0,0 +1,31 @@ +#include "free_functions.h" +#include "custom.h" +#include "function_list.h" +#include "parsetype.h" +#include "table.h" +#include "value.h" +#include + +void free_function(void* func) { + libab_function_free(func); + free(func); +} +void free_function_list(void* function_list) { + libab_function_list_free(function_list); + free(function_list); +} +void free_unit(void* unit) { + +} +void free_parsetype(void* parsetype) { + libab_parsetype_free(parsetype); + free(parsetype); +} +void free_table(void* table) { + libab_table_free(table); + free(table); +} +void free_value(void* value) { + libab_value_free(value); + free(value); +} diff --git a/src/interpreter.c b/src/interpreter.c index 587e379..ed38629 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1,6 +1,7 @@ #include "libabacus.h" #include "util.h" #include "value.h" +#include "free_functions.h" libab_result libab_interpreter_init(libab_interpreter* intr, libab* ab) { libab_result result; @@ -159,11 +160,6 @@ libab_result _interpreter_compare_types(libab_ref* left_type, return result; } -void _free_parsetype(void* parsetype) { - libab_parsetype_free(parsetype); - free(parsetype); -} - /** * Copies a type, substituting type parameters for their copies * from the parameter trie. @@ -208,9 +204,9 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type, } if (result == LIBAB_SUCCESS) { - result = libab_ref_new(into, copy, _free_parsetype); + result = libab_ref_new(into, copy, free_parsetype); if (result != LIBAB_SUCCESS) { - _free_parsetype(copy); + free_parsetype(copy); } } } diff --git a/src/libabacus.c b/src/libabacus.c index 9c58913..6a21c78 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -5,29 +5,16 @@ #include "util.h" #include "value.h" #include +#include "free_functions.h" -void _free_function_list(void* function_list) { - libab_function_list_free(function_list); - free(function_list); -} - -static libab_basetype _basetype_function_list = {_free_function_list, NULL, 0}; - -void _free_function(void* function) { - libab_function_free(function); - free(function); -} +static libab_basetype _basetype_function_list = {free_function_list, NULL, 0}; static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}}; -static libab_basetype _basetype_function = {_free_function, +static libab_basetype _basetype_function = {free_function, _basetype_function_params, 1}; -void _free_unit(void* unit) { - -} - -static libab_basetype _basetype_unit = { _free_unit, NULL, 0 }; +static libab_basetype _basetype_unit = { free_unit, NULL, 0 }; libab_result _prepare_types(libab* ab, void (*free_function)(void*)); @@ -167,7 +154,7 @@ libab_result _create_value_function_internal(libab_ref* into, libab_ref* type, libab_ref* scope) { libab_ref function_ref; libab_result result = - libab_create_function_internal(&function_ref, _free_function, func, scope); + libab_create_function_internal(&function_ref, free_function, func, scope); libab_ref_null(into); if (result == LIBAB_SUCCESS) { libab_ref_free(into); diff --git a/src/util.c b/src/util.c index 0d79857..5738e41 100644 --- a/src/util.c +++ b/src/util.c @@ -2,6 +2,7 @@ #include "value.h" #include #include +#include "free_functions.h" libab_result libab_convert_lex_result(liblex_result to_convert) { libab_result result = LIBAB_SUCCESS; @@ -130,21 +131,16 @@ libab_result libab_instantiate_basetype(libab_basetype* to_instantiate, return result; } -void _free_table(void* data) { - libab_table_free(data); - free(data); -} - libab_result libab_create_table(libab_ref* into, libab_ref* parent) { libab_table* table; libab_result result = LIBAB_SUCCESS; if ((table = malloc(sizeof(*table)))) { libab_table_init(table); libab_table_set_parent(table, parent); - result = libab_ref_new(into, table, _free_table); + result = libab_ref_new(into, table, free_table); if (result != LIBAB_SUCCESS) { - _free_table(table); + free_table(table); } } else { result = LIBAB_MALLOC; @@ -156,21 +152,16 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent) { return result; } -void _free_value(void* value) { - libab_value_free(value); - free(value); -} - libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, libab_ref* type) { libab_value* value; libab_result result = LIBAB_SUCCESS; if ((value = malloc(sizeof(*value)))) { libab_value_init_ref(value, data, type); - result = libab_ref_new(into, value, _free_value); + result = libab_ref_new(into, value, free_value); if (result != LIBAB_SUCCESS) { - _free_value(value); + free_value(value); } } else { result = LIBAB_MALLOC; @@ -194,7 +185,7 @@ libab_result libab_create_value_raw(libab_ref* into, void* data, } if (result == LIBAB_SUCCESS) { - result = libab_ref_new(into, value, _free_value); + result = libab_ref_new(into, value, free_value); if (result != LIBAB_SUCCESS) { libab_value_free(value); }