From a6029d7ef4544c828d73c0eb3a46922bd420c09d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 18 May 2018 14:15:15 -0700 Subject: [PATCH] Add utility function for executing code. --- include/libabacus.h | 9 +++++++++ src/libabacus.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/libabacus.h b/include/libabacus.h index 9a25e20..41fb301 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -159,6 +159,15 @@ libab_basetype* libab_get_basetype_function(libab* ab); */ libab_basetype* libab_get_basetype_function_list(libab* ab); +/** + * Executes the given string of code. + * @param ab the libabacus instance to use for executing code. + * @param string the string to execute. + * @param value the reference into which to store the result. + * @return the result of the computation. + */ +libab_result libab_run(libab* ab, const char* string, libab_ref* value); + /** * Releases all the resources allocated by libabacus. * @param ab the libabacus instance to release. diff --git a/src/libabacus.c b/src/libabacus.c index 2c55d95..ba400c6 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -4,6 +4,7 @@ #include "util.h" #include "value.h" #include +#include "debug.h" void _free_function_list(void* function_list) { libab_function_list_free(function_list); @@ -341,6 +342,33 @@ libab_basetype* libab_get_basetype_function_list(libab* ab) { return &_basetype_function_list; } +libab_result libab_run(libab* ab, const char* string, libab_ref* value) { + libab_result result = LIBAB_SUCCESS; + ll tokens; + libab_tree* root; + + ll_init(&tokens); + libab_ref_null(value); + + result = libab_lexer_lex(&ab->lexer, string, &tokens); + + if(result == LIBAB_SUCCESS) { + result = libab_parser_parse(&ab->parser, &tokens, string, &root); + } + + if(result == LIBAB_SUCCESS) { + libab_debug_print_tree(root); + libab_ref_free(value); + result = libab_interpreter_run(&ab->intr, root, value); + libab_tree_free_recursive(root); + } + + ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free); + ll_free(&tokens); + + return result; +} + libab_result libab_free(libab* ab) { libab_ref_free(&ab->table); libab_ref_free(&ab->type_num);