Remove types from behavior structs.

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

View File

@ -55,14 +55,6 @@ struct libab_behavior_s {
* The implementation of this behavior. * The implementation of this behavior.
*/ */
struct libab_behavior_impl_s impl; struct libab_behavior_impl_s impl;
/**
* The type of the function.
*/
libab_ref type;
/**
* The type parameters for the given behavior.
*/
libab_ref_trie type_params;
}; };
/** /**
* A struct that holds informatiion * A struct that holds informatiion
@ -116,7 +108,7 @@ typedef struct libab_function_s libab_function;
* @param func the function that this behavior calls. * @param func the function that this behavior calls.
*/ */
void libab_behavior_init_internal(libab_behavior* behavior, void libab_behavior_init_internal(libab_behavior* behavior,
libab_ref* type, libab_function_ptr func); libab_function_ptr func);
/** /**
* Initializes a behavior that uses a tree that has been * Initializes a behavior that uses a tree that has been
* parsed from the user. * parsed from the user.
@ -124,7 +116,7 @@ void libab_behavior_init_internal(libab_behavior* behavior,
* @param type the type of the behavior. * @param type the type of the behavior.
* @param tree the tree that this behavior uses. * @param tree the tree that this behavior uses.
*/ */
void libab_behavior_init_tree(libab_behavior* behavior, libab_ref* type, void libab_behavior_init_tree(libab_behavior* behavior,
libab_tree* tree); libab_tree* tree);
/** /**
* Frees the given behavior. * Frees the given behavior.
@ -155,7 +147,7 @@ void libab_operator_free(libab_operator* op);
* @param fun the function implementation. * @param fun the function implementation.
* @return the result of the initialization. * @return the result of the initialization.
*/ */
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_function_ptr fun);
/** /**
* Initializes a function with the given tree behavior. * Initializes a function with the given tree behavior.
@ -164,7 +156,7 @@ libab_result libab_function_init_internal(libab_function* function, libab_ref* t
* @param tree the tree that represents the function's behavior. * @param tree the tree that represents the function's behavior.
* @return the result of the initialization. * @return the result of the initialization.
*/ */
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_tree* tree);
/** /**
* Frees the given function. * Frees the given function.

View File

@ -92,20 +92,20 @@ libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type
/** /**
* Allocates a function that uses internal code to run. * Allocates a function that uses internal code to run.
* @param into the reference into which to store the new function. * @param into the reference into which to store the new function.
* @param type the type of the function. * @param free_function the free function used to free function instances.
* @param fun the function implementation. * @param fun the function implementation.
* @return libab_result the result of any necessary allocations. * @return libab_result the result of any necessary allocations.
*/ */
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_ptr fun);
/** /**
* Allocates a function that uses a tree to run. * Allocates a function that uses a tree to run.
* @param into the reference into which to store the new function. * @param into the reference into which to store the new function.
* @param type the type of the function. * @param free_function the free function used to free function instances.
* @param tree the function implementation. * @param tree the function implementation.
* @return libab_result the result of any necessary allocations. * @return libab_result the result of any necessary allocations.
*/ */
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_tree* tree);
/** /**
* Creates a function list object, storing it in to the given reference. * Creates a function list object, storing it in to the given reference.

View File

@ -1,24 +1,18 @@
#include "custom.h" #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) { libab_function_ptr func) {
behavior->impl.variant = BIMPL_INTERNAL; behavior->impl.variant = BIMPL_INTERNAL;
behavior->impl.data_u.internal = func; 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) { libab_tree* tree) {
behavior->impl.variant = BIMPL_TREE; behavior->impl.variant = BIMPL_TREE;
behavior->impl.data_u.tree = 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) { 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) { if (behavior->impl.variant == BIMPL_TREE) {
libab_tree_free_recursive(behavior->impl.data_u.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->type = variant;
op->precedence = precedence; op->precedence = precedence;
op->associativity = associativity; 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) { 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) { libab_result _function_init(libab_function* function) {
return LIBAB_SUCCESS; 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_function_ptr fun) {
libab_result result = _function_init(function); libab_result result = _function_init(function);
libab_behavior_init_internal(&function->behavior, type, fun); libab_behavior_init_internal(&function->behavior, fun);
return result; 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_tree* tree) {
libab_result result = _function_init(function); libab_result result = _function_init(function);
libab_behavior_init_tree(&function->behavior, type, tree); libab_behavior_init_tree(&function->behavior, tree);
return result; return result;
} }
void libab_function_free(libab_function* fun) { 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) { libab_function_ptr func) {
behavior->impl.variant = BIMPL_INTERNAL; behavior->impl.variant = BIMPL_INTERNAL;
behavior->impl.data_u.internal = func; behavior->impl.data_u.internal = func;
libab_ref_copy(type, &behavior->type);
} }
libab_result _register_operator(libab* ab, const char* op, 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_result _create_value_function_internal(libab_ref* into, libab_ref* type,
libab_function_ptr func) { libab_function_ptr func) {
libab_ref function_ref; 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); libab_ref_null(into);
if(result == LIBAB_SUCCESS) { if(result == LIBAB_SUCCESS) {
libab_ref_free(into); 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; 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_ptr fun) {
libab_function* new_function; libab_function* new_function;
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
if((new_function = malloc(sizeof(*new_function)))) { 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 { } else {
result = LIBAB_MALLOC; result = LIBAB_MALLOC;
} }
if(result == LIBAB_SUCCESS) { if(result == LIBAB_SUCCESS) {
result = libab_ref_new(into, new_function, result = libab_ref_new(into, new_function, free_function);
((libab_parsetype*) libab_ref_get(type))->data_u.base->free_function);
if(result != LIBAB_SUCCESS) { if(result != LIBAB_SUCCESS) {
libab_function_free(new_function); libab_function_free(new_function);
} }
@ -227,19 +226,19 @@ libab_result libab_create_function_internal(libab_ref* into, libab_ref* type,
return result; 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_tree* tree) {
libab_function* new_function; libab_function* new_function;
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
if((new_function = malloc(sizeof(*new_function)))) { 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 { } else {
result = LIBAB_MALLOC; result = LIBAB_MALLOC;
} }
if(result == LIBAB_SUCCESS) { 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) { if(result != LIBAB_SUCCESS) {
libab_function_free(new_function); libab_function_free(new_function);
} }