diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b95a68..3794a22 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/libabacus_util.c src/table.c src/parser.c) +add_library(abacus STATIC src/lexer.c src/libabacus_util.c src/table.c src/parser.c src/libabacus.c) add_executable(libabacus src/main.c) add_subdirectory(external/liblex) diff --git a/include/lexer.h b/include/lexer.h index b0fcf0b..bded969 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -2,7 +2,7 @@ #define LIBABACUS_LEXER_H #include "eval.h" -#include "libabacus.h" +#include "libabacus_result.h" /** * The lexer used for reading diff --git a/include/libabacus.h b/include/libabacus.h index 2ae8e91..0cbb079 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -1,19 +1,97 @@ #ifndef LIBABACUS_H #define LIBABACUS_H +#include "ht.h" +#include "lexer.h" +#include "table.h" +#include "libabacus_result.h" + /** - * An enum that represents the outcomes of - * libabacus functions that can fail. + * A function pointer that is called + * to execute a certain type of function. */ -enum libab_result_e { - LIBAB_SUCCESS, - LIBAB_MALLOC, - LIBAB_BAD_PATTERN, - LIBAB_FAILED_MATCH, - LIBAB_EOF, - LIBAB_UNEXPECTED +typedef void(*libab_function_ptr)(); + +/** + * A struct that holds informatiion + * about an operator that has been + * registered with libabacus. + */ +struct libab_operator_s { + /** + * The precedence of the operator. + */ + int precedence; + /** + * The functionality of the operator. + */ + libab_function_ptr function; }; -typedef enum libab_result_e libab_result; +/** + * A struct that holds information + * about an function that has been + * registered with libabacus. + */ +struct libab_function_s { + /** + * The functionality of the function. + */ + libab_function_ptr function; +}; + + +/** + * The main struct of libabacus, + * which essentially holds all the informatiom + * for the library's state and operation. + */ +struct libab_s { + /** + * The lexer used to convert a string + * to tokens. + */ + libab_lexer lexer; + /** + * The table used to store top-level + * things like functions and operators. + */ + libab_table table; +}; + +typedef struct libab_operator_s libab_operator; +typedef struct libab_function_s libab_function; +typedef struct libab_s libab; + +/** + * Initializes the libabacus struct as well + * as all its internal structures such as the lexer. + * @param ab the libabacus instance used to keep state. + * @return the result of the initialization. + */ +libab_result libab_init(libab* ab); +/** + * Registers an operator with libabacus. + * @param ab the libabacus instance to reigster the operator with. + * @param op the operator string to register. + * @param precedence the precedence of the operator. + * @param func the function that describes the functionality of the operator. + * @return the result of the initialization. + */ +libab_result libab_register_operator(libab* ab, const char* op, int precedence, libab_function_ptr func); +/** + * Registers a function with libabacus. + * @param ab the libabacus instance used to keep state. + * @param name the name of the function. + * @param func the function that describes the functionality of the function. + * @return the result of the initialization. + */ +libab_result libab_register_function(libab* ab, const char* name, libab_function_ptr func); +/** + * Releases all the resources allocated by libabacus. + * @param ab the libabacus instance to release. + * @return the result of the initialization. + */ +libab_result libab_free(libab* ab); #endif diff --git a/include/libabacus_result.h b/include/libabacus_result.h new file mode 100644 index 0000000..edc70e3 --- /dev/null +++ b/include/libabacus_result.h @@ -0,0 +1,19 @@ +#ifndef LIBABACUS_RESULT_H +#define LIBABACUS_RESULT_H + +/** + * An enum that represents the outcomes of + * libabacus functions that can fail. + */ +enum libab_result_e { + LIBAB_SUCCESS, + LIBAB_MALLOC, + LIBAB_BAD_PATTERN, + LIBAB_FAILED_MATCH, + LIBAB_EOF, + LIBAB_UNEXPECTED +}; + +typedef enum libab_result_e libab_result; + +#endif diff --git a/include/libabacus_util.h b/include/libabacus_util.h index 6c91b46..9ed2780 100644 --- a/include/libabacus_util.h +++ b/include/libabacus_util.h @@ -3,7 +3,7 @@ #include "libds.h" #include "liblex.h" -#include "libabacus.h" +#include "libabacus_result.h" /** * Converts a result code from liblex to libabacus. diff --git a/include/table.h b/include/table.h index 8d921ee..a598fbb 100644 --- a/include/table.h +++ b/include/table.h @@ -2,7 +2,7 @@ #define LIBABACUS_TABLE_H #include "ht.h" -#include "libabacus.h" +#include "libabacus_result.h" /** * A struct that represents a structure diff --git a/src/libabacus.c b/src/libabacus.c new file mode 100644 index 0000000..4a3ee1b --- /dev/null +++ b/src/libabacus.c @@ -0,0 +1,11 @@ +#include "libabacus.h" + +libab_result libab_init(libab* ab) { + libab_table_init(&ab->table); + return libab_lexer_init(&ab->lexer); +} + +libab_result libab_free(libab* ab) { + libab_table_free(&ab->table); + return libab_lexer_free(&ab->lexer); +}