From a5429ae2c8dbb4548d9c689fc64998a0020b49e9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 21 Jun 2018 17:23:34 -0700 Subject: [PATCH] Maintain scope between lines executed. --- src/interactive.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/interactive.c b/src/interactive.c index 332aec5..63e0c39 100644 --- a/src/interactive.c +++ b/src/interactive.c @@ -117,13 +117,33 @@ libab_result register_functions(libab* ab) { return result; } -int main() { +libab_result loop(libab* ab, int interaction_count, libab_ref* scope) { + libab_result result = LIBAB_SUCCESS; char input_buffer[2048]; - int interaction_count = INTERACTIONS; libab_ref eval_into; libab_ref call_into; - libab_result result; libab_result eval_result; + + while (interaction_count-- && result == LIBAB_SUCCESS) { + printf("(%d) > ", INTERACTIONS - interaction_count); + fgets(input_buffer, 2048, stdin); + eval_result = libab_run_scoped(ab, input_buffer, scope, &eval_into); + + if (eval_result != LIBAB_SUCCESS) { + printf("Invalid input (error code %d).\n", eval_result); + } else { + result = libab_run_function_scoped(ab, "print", scope, &call_into, 1, &eval_into); + libab_ref_free(&call_into); + } + libab_ref_free(&eval_into); + } + + return result; +} + +int main() { + libab_result result; + libab_ref scope; libab ab; if (libab_init(&ab, impl_parse, impl_free) != LIBAB_SUCCESS) { @@ -132,18 +152,12 @@ int main() { } result = register_functions(&ab); - while (interaction_count-- && result == LIBAB_SUCCESS) { - printf("(%d) > ", INTERACTIONS - interaction_count); - fgets(input_buffer, 2048, stdin); - eval_result = libab_run(&ab, input_buffer, &eval_into); - - if (eval_result != LIBAB_SUCCESS) { - printf("Invalid input (error code %d).\n", eval_result); - } else { - result = libab_run_function(&ab, "print", &call_into, 1, &eval_into); - libab_ref_free(&call_into); - } - libab_ref_free(&eval_into); + if(result == LIBAB_SUCCESS) { + result = libab_create_table(&scope, &ab.table); + } + if(result == LIBAB_SUCCESS) { + loop(&ab, INTERACTIONS, &scope); + libab_ref_free(&scope); } libab_free(&ab);