From 06f17f491c4d1be1f4936e36cda81c4f1e13cf49 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 13 Sep 2018 23:57:33 -0700 Subject: [PATCH] Move variable setting code into utilities. --- include/util.h | 11 +++++++++++ src/reserved.c | 14 +------------- src/util.c | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/include/util.h b/include/util.h index e99f3ff..98c883c 100644 --- a/include/util.h +++ b/include/util.h @@ -114,6 +114,17 @@ libab_result libab_overload_function(libab* ab, libab_table* table, const char* name, 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. * @param into the reference into which to store the new function. diff --git a/src/reserved.c b/src/reserved.c index 5752d00..a04357a 100644 --- a/src/reserved.c +++ b/src/reserved.c @@ -4,18 +4,6 @@ #include "value.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_tree* left, libab_tree* right, libab_ref* into) { @@ -24,7 +12,7 @@ libab_result _behavior_assign(libab* ab, libab_ref* scope, if(left->variant == TREE_ID) { result = libab_run_tree_scoped(ab, right, scope, into); 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) { diff --git a/src/util.c b/src/util.c index e4fa226..45ce081 100644 --- a/src/util.c +++ b/src/util.c @@ -378,6 +378,20 @@ libab_result libab_overload_function(libab* ab, 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) { size_t index = 0; libab_function* func = function;