Add a utility header to convert error codes.

This commit is contained in:
Danila Fedorin 2018-02-10 16:01:01 -08:00
parent 2db840288d
commit b46dccb70d
4 changed files with 45 additions and 2 deletions

View File

@ -8,7 +8,7 @@ project(libabacus)
add_compile_options(-pedantic -Wall)
add_library(abacus STATIC src/lexer.c)
add_library(abacus STATIC src/lexer.c src/libabacus_util.c)
add_executable(libabacus src/main.c)
add_subdirectory(external/liblex)

View File

@ -7,7 +7,9 @@
*/
enum libab_result {
LIBAB_SUCCESS,
LIBAB_MALLOC
LIBAB_MALLOC,
LIBAB_BAD_PATTERN,
LIBAB_FAILED_MATCH
};
typedef enum libab_result libab_result;

21
include/libabacus_util.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef LIBABACUS_UTIL_H
#define LIBABACUS_UTIL_H
#include "libds.h"
#include "liblex.h"
#include "libabacus.h"
/**
* Converts a result code from liblex to libabacus.
* @param to_convert the code to convert.
* @return the libabacus equivalent of the error code.
*/
libab_result convert_lex_result(liblex_result to_convert);
/**
* Converts a result code from libds to libabacus.
* @param to_convert the code to convert.
* @return the libabacus equivalent of the error code.
*/
libab_result convert_ds_result(libds_result to_convert);
#endif

20
src/libabacus_util.c Normal file
View File

@ -0,0 +1,20 @@
#include "libabacus_util.h"
libab_result convert_lex_result(liblex_result to_convert) {
libab_result result = LIBAB_SUCCESS;
if(to_convert == LIBLEX_MALLOC) {
result = LIBAB_MALLOC;
} else if(to_convert == LIBLEX_INVALID) {
result = LIBAB_BAD_PATTERN;
} else if(to_convert == LIBLEX_UNRECOGNIZED) {
result = LIBAB_FAILED_MATCH;
}
return result;
}
libab_result convert_ds_result(libds_result to_convert) {
libab_result result = LIBAB_SUCCESS;
if(to_convert == LIBDS_MALLOC) {
result = LIBAB_MALLOC;
}
return result;
}