Add libabacus parameter to function calls.

This commit is contained in:
Danila Fedorin 2018-05-27 00:02:20 -07:00
parent f4de244c41
commit 1f7294ce29
3 changed files with 26 additions and 22 deletions

View File

@ -5,11 +5,13 @@
#include "ref_trie.h" #include "ref_trie.h"
#include "tree.h" #include "tree.h"
struct libab_s;
/** /**
* A function pointer that is called * A function pointer that is called
* to execute a certain type of function. * to execute a certain type of function.
*/ */
typedef libab_result (*libab_function_ptr)(libab_ref_vec*, libab_ref*); typedef libab_result (*libab_function_ptr)(struct libab_s*, libab_ref_vec*, libab_ref*);
/** /**
* The variant of the operator that * The variant of the operator that

View File

@ -18,19 +18,19 @@ void* impl_parse(const char* string) {
void impl_free(void* data) { free(data); } void impl_free(void* data) { free(data); }
libab_result function_atan(libab_ref_vec* params, libab_ref* into) { libab_result function_atan(libab* ab, libab_ref_vec* params, libab_ref* into) {
printf("atan called\n"); printf("atan called\n");
libab_ref_null(into); libab_ref_null(into);
return LIBAB_SUCCESS; return LIBAB_SUCCESS;
} }
libab_result function_atan2(libab_ref_vec* params, libab_ref* into) { libab_result function_atan2(libab* ab, libab_ref_vec* params, libab_ref* into) {
printf("atan2 called\n"); printf("atan2 called\n");
libab_ref_null(into); libab_ref_null(into);
return LIBAB_SUCCESS; return LIBAB_SUCCESS;
} }
libab_result function_operator(libab_ref_vec* params, libab_ref* into) { libab_result function_operator(libab* ab, libab_ref_vec* params, libab_ref* into) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
libab_ref left_ref; libab_ref left_ref;
libab_value* left_value; libab_value* left_value;

View File

@ -205,8 +205,7 @@ libab_result _interpreter_resolve_type_params(libab_ref* type,
return result; return result;
} }
libab_result _interpreter_check_types(struct interpreter_state* state, libab_result _interpreter_check_types(libab_ref_vec* reference_types,
libab_ref_vec* reference_types,
libab_ref_vec* params, libab_ref_vec* params,
libab_ref_vec* types) { libab_ref_vec* types) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
@ -260,8 +259,7 @@ libab_result _interpreter_check_types(struct interpreter_state* state,
return result; return result;
} }
libab_result _interpreter_find_match(struct interpreter_state* state, libab_result _interpreter_find_match(libab_function_list* function_values,
libab_function_list* function_values,
libab_ref_vec* params, libab_ref_vec* params,
libab_ref_vec* new_types, libab_ref* match, libab_ref_vec* new_types, libab_ref* match,
int partial) { int partial) {
@ -287,7 +285,7 @@ libab_result _interpreter_find_match(struct interpreter_state* state,
partial)) { partial)) {
/* We found a function that has the correct number of parameters. */ /* We found a function that has the correct number of parameters. */
result = _interpreter_check_types( result = _interpreter_check_types(
state, &temp_function_type->children, params, &temp_new_types); &temp_function_type->children, params, &temp_new_types);
if (result == LIBAB_MISMATCHED_TYPE) { if (result == LIBAB_MISMATCHED_TYPE) {
/* Mismatch is OK. */ /* Mismatch is OK. */
result = LIBAB_SUCCESS; result = LIBAB_SUCCESS;
@ -377,19 +375,21 @@ libab_result _interpreter_call_tree(libab_tree* tree, libab_ref_vec* params,
return result; return result;
} }
libab_result _interpreter_call_behavior(libab_behavior* behavior, libab_result _interpreter_call_behavior(struct interpreter_state* state,
libab_behavior* behavior,
libab_ref_vec* params, libab_ref_vec* params,
libab_ref* into) { libab_ref* into) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
if (behavior->variant == BIMPL_INTERNAL) { if (behavior->variant == BIMPL_INTERNAL) {
result = behavior->data_u.internal(params, into); result = behavior->data_u.internal(state->ab, params, into);
} else { } else {
result = _interpreter_call_tree(behavior->data_u.tree, params, into); result = _interpreter_call_tree(behavior->data_u.tree, params, into);
} }
return result; return result;
} }
libab_result _interpreter_perform_function_call(libab_value* to_call, libab_result _interpreter_perform_function_call(struct interpreter_state* state,
libab_value* to_call,
libab_ref_vec* params, libab_ref_vec* params,
libab_ref* into) { libab_ref* into) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
@ -400,12 +400,13 @@ libab_result _interpreter_perform_function_call(libab_value* to_call,
function_type = libab_ref_get(&to_call->type); function_type = libab_ref_get(&to_call->type);
new_params = params->size - function->params.size; new_params = params->size - function->params.size;
if (function_type->children.size - new_params == 1) { if (function_type->children.size - new_params == 1) {
_interpreter_call_behavior(&function->behavior, params, into); _interpreter_call_behavior(state, &function->behavior, params, into);
} }
return result; return result;
} }
libab_result _interpreter_cast_and_perform_function_call( libab_result _interpreter_cast_and_perform_function_call(
struct interpreter_state* state,
libab_ref* to_call, libab_ref_vec* params, libab_ref_vec* new_types, libab_ref* to_call, libab_ref_vec* params, libab_ref_vec* new_types,
libab_ref* into) { libab_ref* into) {
libab_result result; libab_result result;
@ -420,7 +421,7 @@ libab_result _interpreter_cast_and_perform_function_call(
result = _interpreter_cast_params(params, new_types, &new_params); result = _interpreter_cast_params(params, new_types, &new_params);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
result = _interpreter_perform_function_call(function_value, result = _interpreter_perform_function_call(state, function_value,
&new_params, into); &new_params, into);
} }
@ -439,10 +440,10 @@ libab_result _interpreter_call_function_list(struct interpreter_state* state,
libab_ref_null(into); libab_ref_null(into);
result = result =
_interpreter_find_match(state, list, params, &new_types, &to_call, 0); _interpreter_find_match(list, params, &new_types, &to_call, 0);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
if (libab_ref_get(&to_call) == NULL) { if (libab_ref_get(&to_call) == NULL) {
result = _interpreter_find_match(state, list, params, &new_types, result = _interpreter_find_match(list, params, &new_types,
&to_call, 1); &to_call, 1);
} }
} }
@ -453,7 +454,7 @@ libab_result _interpreter_call_function_list(struct interpreter_state* state,
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
libab_ref_free(into); libab_ref_free(into);
result = _interpreter_cast_and_perform_function_call(&to_call, params, result = _interpreter_cast_and_perform_function_call(state, &to_call, params,
&new_types, into); &new_types, into);
libab_ref_vec_free(&new_types); libab_ref_vec_free(&new_types);
} }
@ -478,13 +479,13 @@ libab_result _interpreter_call_function(struct interpreter_state* state,
result = libab_ref_vec_init(&temp_new_types); result = libab_ref_vec_init(&temp_new_types);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
result = _interpreter_check_types(state, &function_type->children, result = _interpreter_check_types(&function_type->children,
params, &temp_new_types); params, &temp_new_types);
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
libab_ref_free(into); libab_ref_free(into);
result = _interpreter_cast_and_perform_function_call( result = _interpreter_cast_and_perform_function_call(
function, params, &temp_new_types, into); state, function, params, &temp_new_types, into);
} }
libab_ref_vec_free(&temp_new_types); libab_ref_vec_free(&temp_new_types);
@ -518,6 +519,7 @@ libab_result _interpreter_try_call(struct interpreter_state* state,
} }
libab_result _interpreter_cast_and_perform_operator_call( libab_result _interpreter_cast_and_perform_operator_call(
struct interpreter_state* state,
libab_operator* to_call, libab_ref_vec* params, libab_ref_vec* new_types, libab_operator* to_call, libab_ref_vec* params, libab_ref_vec* new_types,
libab_ref* into) { libab_ref* into) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
@ -531,7 +533,7 @@ libab_result _interpreter_cast_and_perform_operator_call(
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
libab_ref_free(into); libab_ref_free(into);
result = result =
_interpreter_call_behavior(&to_call->behavior, params, into); _interpreter_call_behavior(state, &to_call->behavior, params, into);
} }
libab_ref_vec_free(&new_params); libab_ref_vec_free(&new_params);
@ -588,13 +590,13 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
} }
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
result = _interpreter_check_types(state, &operator_type->children, result = _interpreter_check_types(&operator_type->children,
&params, &new_types); &params, &new_types);
} }
if (result == LIBAB_SUCCESS) { if (result == LIBAB_SUCCESS) {
result = _interpreter_cast_and_perform_operator_call( result = _interpreter_cast_and_perform_operator_call(
to_call, &params, &new_types, into); state, to_call, &params, &new_types, into);
} }
libab_ref_vec_free(&params); libab_ref_vec_free(&params);