Require scope in interpreter calls.

This commit is contained in:
Danila Fedorin 2018-06-21 16:11:39 -07:00
parent 13ccea10e4
commit 7dfc55154e
3 changed files with 15 additions and 8 deletions

View File

@ -43,23 +43,27 @@ libab_result libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab)
* Uses the interpreter to run the given parse tree.
* @param intr the interpreter to use to run the code.
* @param tree the tree to run.
* @param scope the parent scope to use for running the tree.
* @param mode the scope mode to use.
* @param into the reference into which the result of the execution will be
* stored.
* @return the result of the execution.
*/
libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree,
libab_ref* scope,
libab_interpreter_scope_mode mode,
libab_ref* into);
/**
* Calls a function with the given parameters.
* @param intr the interpreter to use to call the function.
* @param scope the scope in which the function should be searched for.
* @param function the function to call.
* @param params the parameters to pass to the function.
* @param into the reference to store the result into.
* @return the result of the call.
*/
libab_result libab_interpreter_run_function(libab_interpreter* intr,
libab_ref* scope,
const char* function,
libab_ref_vec* params,
libab_ref* into);

View File

@ -21,9 +21,10 @@ struct interpreter_state {
};
void _interpreter_init(struct interpreter_state* state,
libab_interpreter* intr) {
libab_interpreter* intr,
libab_ref* scope) {
state->ab = intr->ab;
state->base_table = libab_ref_get(&intr->ab->table);
state->base_table = libab_ref_get(scope);
}
void _interpreter_free(struct interpreter_state* state) {}
@ -1174,19 +1175,21 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
}
libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree,
libab_ref* scope,
libab_interpreter_scope_mode mode,
libab_ref* into) {
struct interpreter_state state;
libab_result result;
_interpreter_init(&state, intr);
result = _interpreter_run(&state, tree, into, &state.ab->table, mode);
_interpreter_init(&state, intr, scope);
result = _interpreter_run(&state, tree, into, scope, mode);
_interpreter_free(&state);
return result;
}
libab_result libab_interpreter_run_function(libab_interpreter* intr,
libab_ref* scope,
const char* function,
libab_ref_vec* params,
libab_ref* into) {
@ -1194,10 +1197,10 @@ libab_result libab_interpreter_run_function(libab_interpreter* intr,
libab_ref function_value;
libab_result result;
_interpreter_init(&state, intr);
_interpreter_init(&state, intr, scope);
libab_ref_null(into);
result = _interpreter_require_value(&state.ab->table,
result = _interpreter_require_value(scope,
function, &function_value);
if(result == LIBAB_SUCCESS) {
libab_ref_free(into);

View File

@ -394,7 +394,7 @@ libab_result libab_run(libab* ab, const char* string, libab_ref* value) {
if (result == LIBAB_SUCCESS) {
libab_ref_free(value);
result = libab_interpreter_run(&ab->intr, root, SCOPE_NORMAL, value);
result = libab_interpreter_run(&ab->intr, root, &ab->table, SCOPE_NORMAL, value);
libab_tree_free_recursive(root);
}
@ -421,7 +421,7 @@ libab_result libab_run_function(libab* ab, const char* function,
if(result == LIBAB_SUCCESS) {
libab_ref_free(into);
result = libab_interpreter_run_function(&ab->intr, function, &params, into);
result = libab_interpreter_run_function(&ab->intr, &ab->table, function, &params, into);
}
libab_ref_vec_free(&params);