Add new way to initialize functions.

This commit is contained in:
Danila Fedorin 2018-06-02 20:56:10 -07:00
parent 62dd41e634
commit cbd57f2585
2 changed files with 28 additions and 0 deletions

View File

@ -116,6 +116,12 @@ void libab_behavior_init_internal(libab_behavior* 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_tree* tree); void libab_behavior_init_tree(libab_behavior* behavior, libab_tree* tree);
/**
* Copies given behavior into a new one.
* @param behavior the behavior to copy.
* @param into the behavior to copy into.
*/
void libab_behavior_copy(libab_behavior* behavior, libab_behavior* into);
/** /**
* Frees the given behavior. * Frees the given behavior.
* @param behavior the behavior to free. * @param behavior the behavior to free.
@ -157,6 +163,16 @@ libab_result libab_function_init_internal(libab_function* function,
libab_result libab_function_init_tree(libab_function* function, libab_result libab_function_init_tree(libab_function* function,
libab_tree* tree, libab_tree* tree,
libab_ref* scope); libab_ref* scope);
/**
* Initializes a function with the given behavior, regardless of type.
* @param function the function to initialize.
* @param behavior the behavior to initialize this function with.
* @param scope the scope this function is in.
* @return the result of the initialization.
*/
libab_result libab_function_init_behavior(libab_function* function,
libab_behavior* behavior,
libab_ref* scope);
/** /**
* Frees the given function. * Frees the given function.
* @param fun the function to free. * @param fun the function to free.

View File

@ -11,6 +11,11 @@ void libab_behavior_init_tree(libab_behavior* behavior, libab_tree* tree) {
behavior->data_u.tree = tree; behavior->data_u.tree = tree;
} }
void libab_behavior_copy(libab_behavior* behavior, libab_behavior* into) {
into->variant = behavior->variant;
into->data_u = behavior->data_u;
}
void libab_behavior_free(libab_behavior* behavior) { void libab_behavior_free(libab_behavior* behavior) {
if (behavior->variant == BIMPL_TREE) { if (behavior->variant == BIMPL_TREE) {
libab_tree_free_recursive(behavior->data_u.tree); libab_tree_free_recursive(behavior->data_u.tree);
@ -47,6 +52,13 @@ libab_result libab_function_init_tree(libab_function* function,
libab_behavior_init_tree(&function->behavior, tree); libab_behavior_init_tree(&function->behavior, tree);
return result; return result;
} }
libab_result libab_function_init_behavior(libab_function* function,
libab_behavior* behavior,
libab_ref* scope) {
libab_result result = _function_init(function, scope);
libab_behavior_copy(behavior, &function->behavior);
return result;
}
void libab_function_free(libab_function* fun) { void libab_function_free(libab_function* fun) {
libab_behavior_free(&fun->behavior); libab_behavior_free(&fun->behavior);
libab_ref_vec_free(&fun->params); libab_ref_vec_free(&fun->params);