Remove types from behavior structs.

This commit is contained in:
2018-05-16 15:26:03 -07:00
parent 48f8d09405
commit 47a57d66ee
5 changed files with 23 additions and 39 deletions

View File

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

View File

@@ -95,7 +95,6 @@ void _initialize_behavior(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);
}
libab_result _register_operator(libab* ab, const char* op,
@@ -158,7 +157,7 @@ libab_result libab_register_operator_postfix(libab* ab, const char* op,
libab_result _create_value_function_internal(libab_ref* into, libab_ref* type,
libab_function_ptr func) {
libab_ref function_ref;
libab_result result = libab_create_function_internal(&function_ref, type, func);
libab_result result = libab_create_function_internal(&function_ref, _free_function, func);
libab_ref_null(into);
if(result == LIBAB_SUCCESS) {
libab_ref_free(into);

View File

@@ -200,20 +200,19 @@ libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type
return result;
}
libab_result libab_create_function_internal(libab_ref* into, libab_ref* type,
libab_result libab_create_function_internal(libab_ref* into, void (*free_function)(void*),
libab_function_ptr fun) {
libab_function* new_function;
libab_result result = LIBAB_SUCCESS;
if((new_function = malloc(sizeof(*new_function)))) {
result = libab_function_init_internal(new_function, type, fun);
result = libab_function_init_internal(new_function, fun);
} else {
result = LIBAB_MALLOC;
}
if(result == LIBAB_SUCCESS) {
result = libab_ref_new(into, new_function,
((libab_parsetype*) libab_ref_get(type))->data_u.base->free_function);
result = libab_ref_new(into, new_function, free_function);
if(result != LIBAB_SUCCESS) {
libab_function_free(new_function);
}
@@ -227,19 +226,19 @@ libab_result libab_create_function_internal(libab_ref* into, libab_ref* type,
return result;
}
libab_result libab_create_function_tree(libab_ref* into, libab_ref* type,
libab_result libab_create_function_tree(libab_ref* into, void (*free_function)(void*),
libab_tree* tree) {
libab_function* new_function;
libab_result result = LIBAB_SUCCESS;
if((new_function = malloc(sizeof(*new_function)))) {
result = libab_function_init_tree(new_function, type, tree);
result = libab_function_init_tree(new_function, tree);
} else {
result = LIBAB_MALLOC;
}
if(result == LIBAB_SUCCESS) {
result = libab_ref_new(into, new_function, ((libab_parsetype*) libab_ref_get(type))->data_u.base->free_function);
result = libab_ref_new(into, new_function, free_function);
if(result != LIBAB_SUCCESS) {
libab_function_free(new_function);
}