Add initialization functions for operators and functions.

This commit is contained in:
2018-05-09 15:05:40 -07:00
parent 3b0908125d
commit 8253cd1f69
3 changed files with 99 additions and 12 deletions

View File

@@ -1,16 +1,54 @@
#include "custom.h"
void libab_behavior_init_internal(libab_behavior* behavior, libab_ref* type,
libab_function_ptr func) {
behavior->impl.variant = BIMPL_INTERNAL;
behavior->impl.data_u.internal = func;
libab_ref_copy(type, &behavior->type);
}
void libab_behavior_init_tree(libab_behavior* behavior, libab_ref* type,
libab_tree* tree) {
behavior->impl.variant = BIMPL_TREE;
behavior->impl.data_u.tree = tree;
libab_ref_copy(type, &behavior->type);
}
void libab_behavior_free(libab_behavior* behavior) {
libab_ref_free(&behavior->type);
if (behavior->impl.variant == BIMPL_TREE) {
libab_ref_free(&behavior->impl.data_u.tree);
libab_tree_free_recursive(behavior->impl.data_u.tree);
}
}
void libab_operator_init(libab_operator* op, libab_operator_variant variant,
int precedence, int associativity, libab_ref* type,
libab_function_ptr func) {
op->type = variant;
op->precedence = precedence;
op->associativity = associativity;
libab_behavior_init_internal(&op->behavior, type, func);
}
void libab_operator_free(libab_operator* op) {
libab_behavior_free(&op->behavior);
}
libab_result _function_init(libab_function* function) {
return LIBAB_SUCCESS;
}
libab_result libab_function_init_internal(libab_function* function, libab_ref* type,
libab_function_ptr fun) {
libab_result result = _function_init(function);
libab_behavior_init_internal(&function->behavior, type, fun);
return result;
}
libab_result libab_function_init_tree(libab_function* function, libab_ref* type,
libab_tree* tree) {
libab_result result = _function_init(function);
libab_behavior_init_tree(&function->behavior, type, tree);
return result;
}
void libab_function_free(libab_function* fun) {
libab_behavior_free(&fun->behavior);
}

View File

@@ -78,12 +78,12 @@ libab_result _register_operator(libab* ab, const char* op,
char op_buffer[8];
libab_result result = LIBAB_SUCCESS;
libab_table_entry* new_entry;
libab_operator* new_operator = NULL;
if ((new_entry = malloc(sizeof(*new_entry)))) {
new_entry->variant = ENTRY_OP;
new_entry->data_u.op.precedence = precedence;
new_entry->data_u.op.associativity = associativity;
new_entry->data_u.op.type = token_type;
_initialize_behavior(&(new_entry->data_u.op.behavior), type, func);
new_operator = &(new_entry->data_u.op);
libab_operator_init(new_operator, token_type,
precedence, associativity, type, func);
} else {
result = LIBAB_MALLOC;
}
@@ -99,8 +99,8 @@ libab_result _register_operator(libab* ab, const char* op,
}
if (result != LIBAB_SUCCESS) {
if (new_entry)
libab_ref_free(&new_entry->data_u.op.behavior.type);
if (new_operator)
libab_operator_free(new_operator);
eval_config_remove(&ab->lexer.config, op, TOKEN_OP);
free(new_entry);
}
@@ -131,11 +131,12 @@ libab_result libab_register_operator_postfix(libab* ab, const char* op,
libab_result libab_register_function(libab* ab, const char* name,
libab_ref* type, libab_function_ptr func) {
libab_result result = LIBAB_SUCCESS;
libab_function* new_function = NULL;
libab_table_entry* new_entry;
if ((new_entry = malloc(sizeof(*new_entry)))) {
new_entry->variant = ENTRY_FUN;
_initialize_behavior(&(new_entry->data_u.function.behavior), type,
func);
new_function = &new_entry->data_u.function;
result = libab_function_init_internal(new_function, type, func);
} else {
result = LIBAB_MALLOC;
}
@@ -145,8 +146,8 @@ libab_result libab_register_function(libab* ab, const char* name,
}
if (result != LIBAB_SUCCESS) {
if (new_entry)
libab_ref_free(&new_entry->data_u.function.behavior.type);
if(new_function)
libab_function_free(new_function);
free(new_entry);
}