Move variable setting code into utilities.

This commit is contained in:
Danila Fedorin 2018-09-13 23:57:33 -07:00
parent ca6075e8d5
commit 06f17f491c
3 changed files with 26 additions and 13 deletions

View File

@ -114,6 +114,17 @@ libab_result libab_overload_function(libab* ab,
libab_table* table, libab_table* table,
const char* name, const char* name,
libab_ref* function); libab_ref* function);
/**
* Sets a value under the given name, overriding
* an existing value if it exists.
* @param table the table to store the value into.
* @param name the name of the variable.
* @param the value of the new variable.
* @return the result of setting the variable.
*/
libab_result libab_set_variable(libab_table* table,
const char* name,
libab_ref* value);
/** /**
* 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.

View File

@ -4,18 +4,6 @@
#include "value.h" #include "value.h"
#include "libabacus.h" #include "libabacus.h"
libab_result _update_entry(libab_table* table, const char* name, libab_ref* value) {
libab_result result = LIBAB_SUCCESS;
libab_table_entry* value_entry = libab_table_search_entry_value(table, name);
if(value_entry) {
libab_ref_free(&value_entry->data_u.value);
libab_ref_copy(value, &value_entry->data_u.value);
} else {
result = libab_put_table_value(table, name, value);
}
return result;
}
libab_result _behavior_assign(libab* ab, libab_ref* scope, libab_result _behavior_assign(libab* ab, libab_ref* scope,
libab_tree* left, libab_tree* right, libab_tree* left, libab_tree* right,
libab_ref* into) { libab_ref* into) {
@ -24,7 +12,7 @@ libab_result _behavior_assign(libab* ab, libab_ref* scope,
if(left->variant == TREE_ID) { if(left->variant == TREE_ID) {
result = libab_run_tree_scoped(ab, right, scope, into); result = libab_run_tree_scoped(ab, right, scope, into);
if(result == LIBAB_SUCCESS) { if(result == LIBAB_SUCCESS) {
result = _update_entry(libab_ref_get(scope), left->string_value, into); result = libab_set_variable(libab_ref_get(scope), left->string_value, into);
} }
if(result != LIBAB_SUCCESS) { if(result != LIBAB_SUCCESS) {

View File

@ -378,6 +378,20 @@ libab_result libab_overload_function(libab* ab,
return result; return result;
} }
libab_result libab_set_variable(libab_table* table,
const char* name,
libab_ref* value) {
libab_result result = LIBAB_SUCCESS;
libab_table_entry* value_entry = libab_table_search_entry_value(table, name);
if(value_entry) {
libab_ref_free(&value_entry->data_u.value);
libab_ref_copy(value, &value_entry->data_u.value);
} else {
result = libab_put_table_value(table, name, value);
}
return result;
}
void _gc_visit_function_children(void* function, libab_visitor_function_ptr visitor, void* data) { void _gc_visit_function_children(void* function, libab_visitor_function_ptr visitor, void* data) {
size_t index = 0; size_t index = 0;
libab_function* func = function; libab_function* func = function;