From 8b13b9a7352116865cbecc3d9d89ecbfde99e6b4 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 11 Feb 2018 22:50:44 -0800 Subject: [PATCH] Implement functions to register operators and functions into libabacus. --- src/libabacus.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/libabacus.c b/src/libabacus.c index f9395df..778a52e 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -1,4 +1,5 @@ #include "libabacus.h" +#include libab_result libab_init(libab* ab) { libab_table_init(&ab->table); @@ -6,6 +7,49 @@ libab_result libab_init(libab* ab) { 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_table_free(&ab->table); libab_parser_free(&ab->parser);