Add utility function for executing code.

This commit is contained in:
Danila Fedorin 2018-05-18 14:15:15 -07:00
parent 03cce8d1fb
commit a6029d7ef4
2 changed files with 37 additions and 0 deletions

View File

@ -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.

View File

@ -4,6 +4,7 @@
#include "util.h"
#include "value.h"
#include <stdlib.h>
#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);