Implement functions to register operators and functions into libabacus.

This commit is contained in:
Danila Fedorin 2018-02-11 22:50:44 -08:00
parent 72be209f0f
commit 8b13b9a735

View File

@ -1,4 +1,5 @@
#include "libabacus.h" #include "libabacus.h"
#include <stdlib.h>
libab_result libab_init(libab* ab) { libab_result libab_init(libab* ab) {
libab_table_init(&ab->table); libab_table_init(&ab->table);
@ -6,6 +7,49 @@ libab_result libab_init(libab* ab) {
return libab_lexer_init(&ab->lexer); return libab_lexer_init(&ab->lexer);
} }
libab_result libab_register_operator(libab* ab, const char* op, int precedence, libab_function_ptr func) {
libab_result result = LIBAB_SUCCESS;
libab_table_entry* new_entry;
if((new_entry = malloc(sizeof(*new_entry)))) {
new_entry->variant = ENTRY_OPERATOR;
new_entry->data_u.op.function = func;
new_entry->data_u.op.precedence = precedence;
} else {
result = LIBAB_MALLOC;
}
if(result == LIBAB_SUCCESS) {
result = libab_table_put(&ab->table, op, new_entry);
}
if(result != LIBAB_SUCCESS) {
free(new_entry);
}
return result;
}
libab_result libab_register_function(libab* ab, const char* name, libab_function_ptr func) {
libab_result result = LIBAB_SUCCESS;
libab_table_entry* new_entry;
if((new_entry = malloc(sizeof(*new_entry)))) {
new_entry->variant = ENTRY_FUNCTION;
new_entry->data_u.function.function = func;
} else {
result = LIBAB_MALLOC;
}
if(result == LIBAB_SUCCESS) {
result = libab_table_put(&ab->table, name, new_entry);
}
if(result != LIBAB_SUCCESS) {
free(new_entry);
}
return result;
}
libab_result libab_free(libab* ab) { libab_result libab_free(libab* ab) {
libab_table_free(&ab->table); libab_table_free(&ab->table);
libab_parser_free(&ab->parser); libab_parser_free(&ab->parser);