Add scopes to functions.

This commit is contained in:
Danila Fedorin 2018-05-28 01:18:06 -07:00
parent 031e6434c5
commit 34b97e42ac
5 changed files with 35 additions and 16 deletions

View File

@ -94,6 +94,10 @@ struct libab_function_s {
* if it was created via partial application. * if it was created via partial application.
*/ */
libab_ref_vec params; libab_ref_vec params;
/**
* The scope in which this function was declared.
*/
libab_ref scope;
}; };
typedef enum libab_operator_variant_e libab_operator_variant; typedef enum libab_operator_variant_e libab_operator_variant;
@ -143,18 +147,22 @@ void libab_operator_free(libab_operator* op);
* Initializes a function with the given internal behavior. * Initializes a function with the given internal behavior.
* @param function the function to initialize. * @param function the function to initialize.
* @param fun the function implementation. * @param fun the function implementation.
* @param the parent scope in which this function was declared.
* @return the result of the initialization. * @return the result of the initialization.
*/ */
libab_result libab_function_init_internal(libab_function* function, libab_result libab_function_init_internal(libab_function* function,
libab_function_ptr fun); libab_function_ptr fun,
libab_ref* scope);
/** /**
* Initializes a function with the given tree behavior. * Initializes a function with the given tree behavior.
* @param function the function to initialize. * @param function the function to initialize.
* @param tree the tree that represents the function's behavior. * @param tree the tree that represents the function's behavior.
* @param scope the scope in which this function was declared.
* @return the result of the initialization. * @return the result of the initialization.
*/ */
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);
/** /**
* Frees the given function. * Frees the given function.
* @param fun the function to free. * @param fun the function to free.

View File

@ -96,21 +96,25 @@ libab_result libab_create_value_raw(libab_ref* into, void* data,
* @param into the reference into which to store the new function. * @param into the reference into which to store the new function.
* @param free_function the free function used to free function instances. * @param free_function the free function used to free function instances.
* @param fun the function implementation. * @param fun the function implementation.
* @param scope the scope in which this function was declared.
* @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_result libab_create_function_internal(libab_ref* into,
void (*free_function)(void*), void (*free_function)(void*),
libab_function_ptr fun); libab_function_ptr fun,
libab_ref* scope);
/** /**
* 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 free_function the free function used to free function instances. * @param free_function the free function used to free function instances.
* @param tree the function implementation. * @param tree the function implementation.
* @param scope the scope in which this function was declared.
* @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_result libab_create_function_tree(libab_ref* into,
void (*free_function)(void*), void (*free_function)(void*),
libab_tree* tree); libab_tree* tree,
libab_ref* scope);
/** /**
* Creates a function list object, storing it in to the given reference. * Creates a function list object, storing it in to the given reference.
* @param into the reference to store into. * @param into the reference to store into.

View File

@ -32,22 +32,26 @@ void libab_operator_free(libab_operator* op) {
libab_behavior_free(&op->behavior); libab_behavior_free(&op->behavior);
} }
libab_result _function_init(libab_function* function) { libab_result _function_init(libab_function* function, libab_ref* scope) {
libab_ref_copy(scope, &function->scope);
return libab_ref_vec_init(&function->params); return libab_ref_vec_init(&function->params);
} }
libab_result libab_function_init_internal(libab_function* function, libab_result libab_function_init_internal(libab_function* function,
libab_function_ptr fun) { libab_function_ptr fun,
libab_result result = _function_init(function); libab_ref* scope) {
libab_result result = _function_init(function, scope);
libab_behavior_init_internal(&function->behavior, fun); libab_behavior_init_internal(&function->behavior, fun);
return result; return result;
} }
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_result result = _function_init(function); libab_ref* scope) {
libab_result result = _function_init(function, scope);
libab_behavior_init_tree(&function->behavior, 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) {
libab_behavior_free(&fun->behavior); libab_behavior_free(&fun->behavior);
libab_ref_vec_free(&fun->params); libab_ref_vec_free(&fun->params);
libab_ref_free(&fun->scope);
} }

View File

@ -147,10 +147,11 @@ 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* scope) {
libab_ref function_ref; libab_ref function_ref;
libab_result result = libab_result result =
libab_create_function_internal(&function_ref, _free_function, func); libab_create_function_internal(&function_ref, _free_function, func, scope);
libab_ref_null(into); libab_ref_null(into);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
libab_ref_free(into); libab_ref_free(into);
@ -233,7 +234,7 @@ libab_result libab_register_function(libab* ab, const char* name,
libab_table_entry* existing_entry; libab_table_entry* existing_entry;
libab_ref function_value; libab_ref function_value;
libab_result result = libab_result result =
_create_value_function_internal(&function_value, type, func); _create_value_function_internal(&function_value, type, func, &ab->table);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
existing_entry = libab_table_search_filter( existing_entry = libab_table_search_filter(

View File

@ -210,12 +210,13 @@ libab_result libab_create_value_raw(libab_ref* into, void* data,
libab_result libab_create_function_internal(libab_ref* into, libab_result libab_create_function_internal(libab_ref* into,
void (*free_function)(void*), void (*free_function)(void*),
libab_function_ptr fun) { libab_function_ptr fun,
libab_ref* scope) {
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, fun); result = libab_function_init_internal(new_function, fun, scope);
} else { } else {
result = LIBAB_MALLOC; result = LIBAB_MALLOC;
} }
@ -237,12 +238,13 @@ libab_result libab_create_function_internal(libab_ref* into,
libab_result libab_create_function_tree(libab_ref* into, libab_result libab_create_function_tree(libab_ref* into,
void (*free_function)(void*), void (*free_function)(void*),
libab_tree* tree) { libab_tree* tree,
libab_ref* scope) {
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, tree); result = libab_function_init_tree(new_function, tree, scope);
} else { } else {
result = LIBAB_MALLOC; result = LIBAB_MALLOC;
} }