2018-02-11 21:52:28 -08:00
|
|
|
#include "libabacus.h"
|
2018-02-11 22:50:44 -08:00
|
|
|
#include <stdlib.h>
|
2018-02-11 21:52:28 -08:00
|
|
|
|
|
|
|
libab_result libab_init(libab* ab) {
|
|
|
|
libab_table_init(&ab->table);
|
2018-02-11 22:23:02 -08:00
|
|
|
libab_parser_init(&ab->parser, &ab->table);
|
2018-02-11 21:52:28 -08:00
|
|
|
return libab_lexer_init(&ab->lexer);
|
|
|
|
}
|
|
|
|
|
2018-02-11 22:50:44 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:52:28 -08:00
|
|
|
libab_result libab_free(libab* ab) {
|
|
|
|
libab_table_free(&ab->table);
|
2018-02-11 22:23:02 -08:00
|
|
|
libab_parser_free(&ab->parser);
|
2018-02-11 21:52:28 -08:00
|
|
|
return libab_lexer_free(&ab->lexer);
|
|
|
|
}
|