diff --git a/include/libabacus.h b/include/libabacus.h index ad015e3..077add3 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -216,6 +216,14 @@ void libab_get_true_value(libab* ab, libab_ref* into); * @param into the reference into which to store the false value. */ void libab_get_false_value(libab* ab, libab_ref* into); +/** + * Get the boolean value corresponding to val from this + * libab instance. + * @param ab the instance to get the value from. + * @param val the true or false value to represent. + * @param into the reference into which to store the value. + */ +void libab_get_bool_value(libab* ab, int val, libab_ref* into); /** * Executes the given string of code. diff --git a/include/util.h b/include/util.h index cf48fb2..9a509fe 100644 --- a/include/util.h +++ b/include/util.h @@ -178,5 +178,12 @@ void* libab_unwrap_value(libab_ref* ref); * @return the value at the given index. */ void* libab_unwrap_param(libab_ref_vec* vec, size_t index); +/** + * Sanitizes a string to avoid liblex compilation errors. + * @param to the buffer to store the sanitized string to. + * @param from the string to sanitize. + * @param buffer_size the size of the to buffer. + */ +void libab_sanitize(char* to, const char* from, size_t buffer_size); #endif diff --git a/src/libabacus.c b/src/libabacus.c index f83ddd9..757591d 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -84,18 +84,6 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*), return result; } -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 == '\\' || - *from == '|' || *from == '[' || *from == ']' || *from == '(' || - *from == ')') - to[index++] = '\\'; - to[index++] = *(from++); - } - to[index] = '\0'; -} - void _initialize_behavior(libab_behavior* behavior, libab_ref* type, libab_function_ptr func) { behavior->variant = BIMPL_INTERNAL; @@ -120,7 +108,7 @@ libab_result _register_operator(libab* ab, const char* op, } if (result == LIBAB_SUCCESS) { - _sanitize(op_buffer, op, 8); + libab_sanitize(op_buffer, op, 8); result = libab_convert_lex_result( eval_config_add(&ab->lexer.config, op_buffer, TOKEN_OP)); } @@ -412,6 +400,10 @@ void libab_get_false_value(libab* ab, libab_ref* into) { libab_interpreter_false_value(&ab->intr, into); } +void libab_get_bool_value(libab* ab, int val, libab_ref* into) { + val ? libab_get_true_value(ab, into) : libab_get_false_value(ab, into); +} + libab_result _create_tree(libab* ab, const char* string, libab_tree** into) { libab_result result = LIBAB_SUCCESS; ll tokens; diff --git a/src/util.c b/src/util.c index cc93e3e..287b4f1 100644 --- a/src/util.c +++ b/src/util.c @@ -416,3 +416,15 @@ void* libab_unwrap_param(libab_ref_vec* vec, size_t index) { libab_ref_free(&temp); return data; } + +void libab_sanitize(char* to, const char* from, size_t buffer_size) { + size_t index = 0; + while (*from && index < (buffer_size - 2)) { + if (*from == '+' || *from == '*' || *from == '\\' || + *from == '|' || *from == '[' || *from == ']' || *from == '(' || + *from == ')') + to[index++] = '\\'; + to[index++] = *(from++); + } + to[index] = '\0'; +}