Start the overarching library code, and move result enum elsewhere.

This commit is contained in:
Danila Fedorin 2018-02-11 21:52:28 -08:00
parent e5351c17a2
commit 37a6f31b5f
7 changed files with 122 additions and 14 deletions

View File

@ -8,7 +8,7 @@ project(libabacus)
add_compile_options(-pedantic -Wall) 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_executable(libabacus src/main.c)
add_subdirectory(external/liblex) add_subdirectory(external/liblex)

View File

@ -2,7 +2,7 @@
#define LIBABACUS_LEXER_H #define LIBABACUS_LEXER_H
#include "eval.h" #include "eval.h"
#include "libabacus.h" #include "libabacus_result.h"
/** /**
* The lexer used for reading * The lexer used for reading

View File

@ -1,19 +1,97 @@
#ifndef LIBABACUS_H #ifndef LIBABACUS_H
#define 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 * A function pointer that is called
* libabacus functions that can fail. * to execute a certain type of function.
*/ */
enum libab_result_e { typedef void(*libab_function_ptr)();
LIBAB_SUCCESS,
LIBAB_MALLOC, /**
LIBAB_BAD_PATTERN, * A struct that holds informatiion
LIBAB_FAILED_MATCH, * about an operator that has been
LIBAB_EOF, * registered with libabacus.
LIBAB_UNEXPECTED */
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 #endif

View File

@ -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

View File

@ -3,7 +3,7 @@
#include "libds.h" #include "libds.h"
#include "liblex.h" #include "liblex.h"
#include "libabacus.h" #include "libabacus_result.h"
/** /**
* Converts a result code from liblex to libabacus. * Converts a result code from liblex to libabacus.

View File

@ -2,7 +2,7 @@
#define LIBABACUS_TABLE_H #define LIBABACUS_TABLE_H
#include "ht.h" #include "ht.h"
#include "libabacus.h" #include "libabacus_result.h"
/** /**
* A struct that represents a structure * A struct that represents a structure

11
src/libabacus.c Normal file
View File

@ -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);
}