Format code.
This commit is contained in:
@@ -43,7 +43,8 @@ size_t libab_function_list_size(libab_function_list* list);
|
|||||||
* @param index the index at which to get the value.
|
* @param index the index at which to get the value.
|
||||||
* @param into the reference to store the function into.
|
* @param into the reference to store the function into.
|
||||||
*/
|
*/
|
||||||
void libab_function_list_index(libab_function_list* list, size_t index, libab_ref* into);
|
void libab_function_list_index(libab_function_list* list, size_t index,
|
||||||
|
libab_ref* into);
|
||||||
/**
|
/**
|
||||||
* Frees the given function list.
|
* Frees the given function list.
|
||||||
* @param list the list to free.
|
* @param list the list to free.
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ void libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab);
|
|||||||
* Uses the interpreter to run the given parse tree.
|
* Uses the interpreter to run the given parse tree.
|
||||||
* @param intr the interpreter to use to run the code.
|
* @param intr the interpreter to use to run the code.
|
||||||
* @param tree the tree to run.
|
* @param tree the tree to run.
|
||||||
* @param into the reference into which the result of the execution will be stored.
|
* @param into the reference into which the result of the execution will be
|
||||||
|
* stored.
|
||||||
* @return the result of the execution.
|
* @return the result of the execution.
|
||||||
*/
|
*/
|
||||||
libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree,
|
libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree,
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ libab_result libab_ref_vec_init(libab_ref_vec* vec);
|
|||||||
* @param copy_of the vector to make a copy of.
|
* @param copy_of the vector to make a copy of.
|
||||||
* @return the result of the initialization.
|
* @return the result of the initialization.
|
||||||
*/
|
*/
|
||||||
libab_result libab_ref_vec_init_copy(libab_ref_vec* vec, libab_ref_vec* copy_of);
|
libab_result libab_ref_vec_init_copy(libab_ref_vec* vec,
|
||||||
|
libab_ref_vec* copy_of);
|
||||||
/**
|
/**
|
||||||
* Inserts an existing reference counted value into the vector.
|
* Inserts an existing reference counted value into the vector.
|
||||||
* This bumps the reference's refcount, thereby preventing its
|
* This bumps the reference's refcount, thereby preventing its
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ size_t libab_function_list_size(libab_function_list* list) {
|
|||||||
return list->functions.size;
|
return list->functions.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void libab_function_list_index(libab_function_list* list, size_t index, libab_ref* into) {
|
void libab_function_list_index(libab_function_list* list, size_t index,
|
||||||
|
libab_ref* into) {
|
||||||
libab_ref_vec_index(&list->functions, index, into);
|
libab_ref_vec_index(&list->functions, index, into);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#include "libabacus.h"
|
#include "libabacus.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include "value.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "value.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define TRY(expression) if(result == LIBAB_SUCCESS) result = expression;
|
#define TRY(expression) \
|
||||||
|
if (result == LIBAB_SUCCESS) \
|
||||||
|
result = expression;
|
||||||
#define INTERACTIONS 5
|
#define INTERACTIONS 5
|
||||||
|
|
||||||
void* impl_parse(const char* string) {
|
void* impl_parse(const char* string) {
|
||||||
@@ -14,9 +16,7 @@ void* impl_parse(const char* string) {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void impl_free(void* data) {
|
void impl_free(void* data) { free(data); }
|
||||||
free(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
libab_result function_atan(libab_ref_vec* params, libab_ref* into) {
|
libab_result function_atan(libab_ref_vec* params, libab_ref* into) {
|
||||||
printf("atan called\n");
|
printf("atan called\n");
|
||||||
@@ -71,10 +71,14 @@ libab_result register_functions(libab* ab) {
|
|||||||
|
|
||||||
TRY(libab_register_function(ab, "atan", &trig_type, function_atan));
|
TRY(libab_register_function(ab, "atan", &trig_type, function_atan));
|
||||||
TRY(libab_register_function(ab, "atan2", &atan2_type, function_atan2));
|
TRY(libab_register_function(ab, "atan2", &atan2_type, function_atan2));
|
||||||
TRY(libab_register_operator_infix(ab, "+", 0, -1, &atan2_type, function_operator));
|
TRY(libab_register_operator_infix(ab, "+", 0, -1, &atan2_type,
|
||||||
TRY(libab_register_operator_infix(ab, "-", 0, -1, &atan2_type, function_operator));
|
function_operator));
|
||||||
TRY(libab_register_operator_infix(ab, "*", 1, -1, &atan2_type, function_operator));
|
TRY(libab_register_operator_infix(ab, "-", 0, -1, &atan2_type,
|
||||||
TRY(libab_register_operator_infix(ab, "/", 1, -1, &atan2_type, function_operator));
|
function_operator));
|
||||||
|
TRY(libab_register_operator_infix(ab, "*", 1, -1, &atan2_type,
|
||||||
|
function_operator));
|
||||||
|
TRY(libab_register_operator_infix(ab, "/", 1, -1, &atan2_type,
|
||||||
|
function_operator));
|
||||||
|
|
||||||
libab_ref_free(&trig_type);
|
libab_ref_free(&trig_type);
|
||||||
libab_ref_free(&atan2_type);
|
libab_ref_free(&atan2_type);
|
||||||
|
|||||||
@@ -58,8 +58,10 @@ int _interpreter_type_contains_placeholders(libab_ref* type) {
|
|||||||
return placeholder;
|
return placeholder;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_compare_types(libab_ref* left_type, libab_ref* right_type,
|
libab_result _interpreter_compare_types(libab_ref* left_type,
|
||||||
libab_ref_trie* left_params, libab_ref_trie* right_params) {
|
libab_ref* right_type,
|
||||||
|
libab_ref_trie* left_params,
|
||||||
|
libab_ref_trie* right_params) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
int left_placeholder;
|
int left_placeholder;
|
||||||
int right_placeholder;
|
int right_placeholder;
|
||||||
@@ -102,17 +104,27 @@ libab_result _interpreter_compare_types(libab_ref* left_type, libab_ref* right_t
|
|||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
libab_ref temp_left;
|
libab_ref temp_left;
|
||||||
libab_ref temp_right;
|
libab_ref temp_right;
|
||||||
result = (left->data_u.base == right->data_u.base) ? LIBAB_SUCCESS : LIBAB_MISMATCHED_TYPE;
|
result = (left->data_u.base == right->data_u.base)
|
||||||
|
? LIBAB_SUCCESS
|
||||||
|
: LIBAB_MISMATCHED_TYPE;
|
||||||
if (result == LIBAB_SUCCESS &&
|
if (result == LIBAB_SUCCESS &&
|
||||||
(left->variant & LIBABACUS_TYPE_F_PARENT || right->variant & LIBABACUS_TYPE_F_PARENT)) {
|
(left->variant & LIBABACUS_TYPE_F_PARENT ||
|
||||||
result = (left->variant & right->variant & LIBABACUS_TYPE_F_PARENT) ? LIBAB_SUCCESS : LIBAB_MISMATCHED_TYPE;
|
right->variant & LIBABACUS_TYPE_F_PARENT)) {
|
||||||
|
result =
|
||||||
|
(left->variant & right->variant & LIBABACUS_TYPE_F_PARENT)
|
||||||
|
? LIBAB_SUCCESS
|
||||||
|
: LIBAB_MISMATCHED_TYPE;
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = (left->children.size == right->children.size) ? LIBAB_SUCCESS : LIBAB_MISMATCHED_TYPE;
|
result = (left->children.size == right->children.size)
|
||||||
|
? LIBAB_SUCCESS
|
||||||
|
: LIBAB_MISMATCHED_TYPE;
|
||||||
}
|
}
|
||||||
for(; index < left->children.size && result == LIBAB_SUCCESS; index++) {
|
for (; index < left->children.size && result == LIBAB_SUCCESS;
|
||||||
|
index++) {
|
||||||
libab_ref_vec_index(&left->children, index, &temp_left);
|
libab_ref_vec_index(&left->children, index, &temp_left);
|
||||||
libab_ref_vec_index(&right->children, index, &temp_right);
|
libab_ref_vec_index(&right->children, index, &temp_right);
|
||||||
result = _interpreter_compare_types(&temp_left, &temp_right, left_params, right_params);
|
result = _interpreter_compare_types(
|
||||||
|
&temp_left, &temp_right, left_params, right_params);
|
||||||
libab_ref_free(&temp_left);
|
libab_ref_free(&temp_left);
|
||||||
libab_ref_free(&temp_right);
|
libab_ref_free(&temp_right);
|
||||||
}
|
}
|
||||||
@@ -128,7 +140,9 @@ void _free_parsetype(void* parsetype) {
|
|||||||
free(parsetype);
|
free(parsetype);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_copy_resolved_type(libab_ref* type, libab_ref_trie* params, libab_ref* into) {
|
libab_result _interpreter_copy_resolved_type(libab_ref* type,
|
||||||
|
libab_ref_trie* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_parsetype* copy;
|
libab_parsetype* copy;
|
||||||
libab_parsetype* original;
|
libab_parsetype* original;
|
||||||
@@ -144,9 +158,11 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type, libab_ref_trie* pa
|
|||||||
libab_ref child_copy;
|
libab_ref child_copy;
|
||||||
libab_ref temp_child;
|
libab_ref temp_child;
|
||||||
result = libab_ref_vec_init(©->children);
|
result = libab_ref_vec_init(©->children);
|
||||||
for(; index < original->children.size && result == LIBAB_SUCCESS; index++) {
|
for (; index < original->children.size && result == LIBAB_SUCCESS;
|
||||||
|
index++) {
|
||||||
libab_ref_vec_index(&original->children, index, &temp_child);
|
libab_ref_vec_index(&original->children, index, &temp_child);
|
||||||
result = _interpreter_copy_resolved_type(&temp_child, params, &child_copy);
|
result = _interpreter_copy_resolved_type(&temp_child, params,
|
||||||
|
&child_copy);
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = libab_ref_vec_insert(©->children, &child_copy);
|
result = libab_ref_vec_insert(©->children, &child_copy);
|
||||||
}
|
}
|
||||||
@@ -177,7 +193,9 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type, libab_ref_trie* pa
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_resolve_type_params(libab_ref* type, libab_ref_trie* params, libab_ref* into) {
|
libab_result _interpreter_resolve_type_params(libab_ref* type,
|
||||||
|
libab_ref_trie* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
if (_interpreter_type_contains_placeholders(type)) {
|
if (_interpreter_type_contains_placeholders(type)) {
|
||||||
result = _interpreter_copy_resolved_type(type, params, into);
|
result = _interpreter_copy_resolved_type(type, params, into);
|
||||||
@@ -188,7 +206,8 @@ libab_result _interpreter_resolve_type_params(libab_ref* type, libab_ref_trie* p
|
|||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_check_types(struct interpreter_state* state,
|
libab_result _interpreter_check_types(struct interpreter_state* state,
|
||||||
libab_ref_vec* reference_types, libab_ref_vec* params,
|
libab_ref_vec* reference_types,
|
||||||
|
libab_ref_vec* params,
|
||||||
libab_ref_vec* types) {
|
libab_ref_vec* types) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_ref_trie function_params;
|
libab_ref_trie function_params;
|
||||||
@@ -209,12 +228,14 @@ libab_result _interpreter_check_types(struct interpreter_state* state,
|
|||||||
|
|
||||||
libab_ref_vec_index(reference_types, index, &left_temp);
|
libab_ref_vec_index(reference_types, index, &left_temp);
|
||||||
libab_ref_vec_index(params, index, &right_value_temp);
|
libab_ref_vec_index(params, index, &right_value_temp);
|
||||||
right_temp = &((libab_value*) libab_ref_get(&right_value_temp))->type;
|
right_temp =
|
||||||
result = _interpreter_compare_types(&left_temp,
|
&((libab_value*)libab_ref_get(&right_value_temp))->type;
|
||||||
right_temp, &function_params, &child_params);
|
result = _interpreter_compare_types(
|
||||||
|
&left_temp, right_temp, &function_params, &child_params);
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = _interpreter_resolve_type_params(right_temp, &child_params, &produced_type);
|
result = _interpreter_resolve_type_params(
|
||||||
|
right_temp, &child_params, &produced_type);
|
||||||
if (result != LIBAB_SUCCESS) {
|
if (result != LIBAB_SUCCESS) {
|
||||||
libab_ref_free(&produced_type);
|
libab_ref_free(&produced_type);
|
||||||
}
|
}
|
||||||
@@ -240,8 +261,10 @@ libab_result _interpreter_check_types(struct interpreter_state* state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_find_match(struct interpreter_state* state,
|
libab_result _interpreter_find_match(struct interpreter_state* state,
|
||||||
libab_function_list* function_values, libab_ref_vec* params,
|
libab_function_list* function_values,
|
||||||
libab_ref_vec* new_types, libab_ref* match, int partial) {
|
libab_ref_vec* params,
|
||||||
|
libab_ref_vec* new_types, libab_ref* match,
|
||||||
|
int partial) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
size_t list_size = libab_function_list_size(function_values);
|
size_t list_size = libab_function_list_size(function_values);
|
||||||
@@ -255,12 +278,16 @@ libab_result _interpreter_find_match(struct interpreter_state* state,
|
|||||||
|
|
||||||
for (; index < list_size && result == LIBAB_SUCCESS; index++) {
|
for (; index < list_size && result == LIBAB_SUCCESS; index++) {
|
||||||
libab_function_list_index(function_values, index, &temp_function_value);
|
libab_function_list_index(function_values, index, &temp_function_value);
|
||||||
temp_function_type = libab_ref_get(&((libab_value*) libab_ref_get(&temp_function_value))->type);
|
temp_function_type = libab_ref_get(
|
||||||
|
&((libab_value*)libab_ref_get(&temp_function_value))->type);
|
||||||
|
|
||||||
if(((temp_function_type->children.size == params->size + 1) && !partial) ||
|
if (((temp_function_type->children.size == params->size + 1) &&
|
||||||
((temp_function_type->children.size > params->size + 1) && partial)) {
|
!partial) ||
|
||||||
|
((temp_function_type->children.size > params->size + 1) &&
|
||||||
|
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(state, &temp_function_type->children, params, &temp_new_types);
|
result = _interpreter_check_types(
|
||||||
|
state, &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;
|
||||||
@@ -268,8 +295,8 @@ libab_result _interpreter_find_match(struct interpreter_state* state,
|
|||||||
/* Function matched; now, check for other matching calls.
|
/* Function matched; now, check for other matching calls.
|
||||||
* More than one matching calls = ambigous call. */
|
* More than one matching calls = ambigous call. */
|
||||||
if (!found_match) {
|
if (!found_match) {
|
||||||
/* We haven't found a match previously. Copy data into new_types,
|
/* We haven't found a match previously. Copy data into
|
||||||
* and use new memory for temp list. */
|
* new_types, and use new memory for temp list. */
|
||||||
found_match = 1;
|
found_match = 1;
|
||||||
*new_types = temp_new_types;
|
*new_types = temp_new_types;
|
||||||
libab_ref_free(match);
|
libab_ref_free(match);
|
||||||
@@ -279,8 +306,8 @@ libab_result _interpreter_find_match(struct interpreter_state* state,
|
|||||||
libab_ref_vec_free(new_types);
|
libab_ref_vec_free(new_types);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* We've found a match previously. So, new_types are initialized,
|
/* We've found a match previously. So, new_types are
|
||||||
* and the call is ambigous. Free all data. */
|
* initialized, and the call is ambigous. Free all data. */
|
||||||
libab_ref_vec_free(new_types);
|
libab_ref_vec_free(new_types);
|
||||||
libab_ref_vec_free(&temp_new_types);
|
libab_ref_vec_free(&temp_new_types);
|
||||||
result = LIBAB_AMBIGOUS_CALL;
|
result = LIBAB_AMBIGOUS_CALL;
|
||||||
@@ -288,7 +315,8 @@ libab_result _interpreter_find_match(struct interpreter_state* state,
|
|||||||
} else {
|
} else {
|
||||||
/* Something bad happened. Free data as best as we can. */
|
/* Something bad happened. Free data as best as we can. */
|
||||||
libab_ref_vec_free(&temp_new_types);
|
libab_ref_vec_free(&temp_new_types);
|
||||||
if(found_match) libab_ref_vec_free(new_types);
|
if (found_match)
|
||||||
|
libab_ref_vec_free(new_types);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,7 +335,8 @@ libab_result _interpreter_find_match(struct interpreter_state* state,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_cast_param(libab_ref* param, libab_ref* type, libab_ref_vec* into) {
|
libab_result _interpreter_cast_param(libab_ref* param, libab_ref* type,
|
||||||
|
libab_ref_vec* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_value* old_value = libab_ref_get(param);
|
libab_value* old_value = libab_ref_get(param);
|
||||||
libab_ref new_value;
|
libab_ref new_value;
|
||||||
@@ -321,7 +350,9 @@ libab_result _interpreter_cast_param(libab_ref* param, libab_ref* type, libab_re
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_cast_params(libab_ref_vec* params, libab_ref_vec* new_types, libab_ref_vec* into) {
|
libab_result _interpreter_cast_params(libab_ref_vec* params,
|
||||||
|
libab_ref_vec* new_types,
|
||||||
|
libab_ref_vec* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
libab_ref temp_param;
|
libab_ref temp_param;
|
||||||
@@ -340,12 +371,15 @@ libab_result _interpreter_cast_params(libab_ref_vec* params, libab_ref_vec* new_
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_call_tree(libab_tree* tree, libab_ref_vec* params, libab_ref* into) {
|
libab_result _interpreter_call_tree(libab_tree* tree, libab_ref_vec* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_call_behavior(libab_behavior* behavior, libab_ref_vec* params, libab_ref* into) {
|
libab_result _interpreter_call_behavior(libab_behavior* behavior,
|
||||||
|
libab_ref_vec* params,
|
||||||
|
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(params, into);
|
||||||
@@ -355,7 +389,9 @@ libab_result _interpreter_call_behavior(libab_behavior* behavior, libab_ref_vec*
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_perform_function_call(libab_value* to_call, libab_ref_vec* params, libab_ref* into) {
|
libab_result _interpreter_perform_function_call(libab_value* to_call,
|
||||||
|
libab_ref_vec* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_function* function;
|
libab_function* function;
|
||||||
libab_parsetype* function_type;
|
libab_parsetype* function_type;
|
||||||
@@ -369,9 +405,8 @@ libab_result _interpreter_perform_function_call(libab_value* to_call, libab_ref_
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_cast_and_perform_function_call(libab_ref* to_call,
|
libab_result _interpreter_cast_and_perform_function_call(
|
||||||
libab_ref_vec* params,
|
libab_ref* to_call, libab_ref_vec* params, libab_ref_vec* new_types,
|
||||||
libab_ref_vec* new_types,
|
|
||||||
libab_ref* into) {
|
libab_ref* into) {
|
||||||
libab_result result;
|
libab_result result;
|
||||||
libab_ref_vec new_params;
|
libab_ref_vec new_params;
|
||||||
@@ -385,7 +420,8 @@ libab_result _interpreter_cast_and_perform_function_call(libab_ref* to_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, &new_params, into);
|
result = _interpreter_perform_function_call(function_value,
|
||||||
|
&new_params, into);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_ref_vec_free(&new_params);
|
libab_ref_vec_free(&new_params);
|
||||||
@@ -394,16 +430,20 @@ libab_result _interpreter_cast_and_perform_function_call(libab_ref* to_call,
|
|||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_call_function_list(struct interpreter_state* state,
|
libab_result _interpreter_call_function_list(struct interpreter_state* state,
|
||||||
libab_function_list* list, libab_ref_vec* params, libab_ref* into) {
|
libab_function_list* list,
|
||||||
|
libab_ref_vec* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_ref_vec new_types;
|
libab_ref_vec new_types;
|
||||||
libab_ref to_call;
|
libab_ref to_call;
|
||||||
libab_ref_null(into);
|
libab_ref_null(into);
|
||||||
|
|
||||||
result = _interpreter_find_match(state, list, params, &new_types, &to_call, 0);
|
result =
|
||||||
|
_interpreter_find_match(state, 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, &to_call, 1);
|
result = _interpreter_find_match(state, list, params, &new_types,
|
||||||
|
&to_call, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,7 +453,8 @@ 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, &new_types, into);
|
result = _interpreter_cast_and_perform_function_call(&to_call, params,
|
||||||
|
&new_types, into);
|
||||||
libab_ref_vec_free(&new_types);
|
libab_ref_vec_free(&new_types);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,7 +463,10 @@ libab_result _interpreter_call_function_list(struct interpreter_state* state,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_call_function(struct interpreter_state* state, libab_ref* function, libab_ref_vec* params, libab_ref* into) {
|
libab_result _interpreter_call_function(struct interpreter_state* state,
|
||||||
|
libab_ref* function,
|
||||||
|
libab_ref_vec* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_ref_vec temp_new_types;
|
libab_ref_vec temp_new_types;
|
||||||
libab_value* function_value;
|
libab_value* function_value;
|
||||||
@@ -434,11 +478,13 @@ libab_result _interpreter_call_function(struct interpreter_state* state, libab_r
|
|||||||
|
|
||||||
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, params, &temp_new_types);
|
result = _interpreter_check_types(state, &function_type->children,
|
||||||
|
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(function, params, &temp_new_types, into);
|
result = _interpreter_cast_and_perform_function_call(
|
||||||
|
function, params, &temp_new_types, into);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_ref_vec_free(&temp_new_types);
|
libab_ref_vec_free(&temp_new_types);
|
||||||
@@ -447,8 +493,9 @@ libab_result _interpreter_call_function(struct interpreter_state* state, libab_r
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_try_call(struct interpreter_state* state, libab_ref* value,
|
libab_result _interpreter_try_call(struct interpreter_state* state,
|
||||||
libab_ref_vec* params, libab_ref* into) {
|
libab_ref* value, libab_ref_vec* params,
|
||||||
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_value* callee_value;
|
libab_value* callee_value;
|
||||||
libab_parsetype* callee_type;
|
libab_parsetype* callee_type;
|
||||||
@@ -458,11 +505,10 @@ libab_result _interpreter_try_call(struct interpreter_state* state, libab_ref* v
|
|||||||
callee_basetype = callee_type->data_u.base;
|
callee_basetype = callee_type->data_u.base;
|
||||||
|
|
||||||
if (callee_basetype == libab_get_basetype_function_list(state->ab)) {
|
if (callee_basetype == libab_get_basetype_function_list(state->ab)) {
|
||||||
result = _interpreter_call_function_list(state,
|
result = _interpreter_call_function_list(
|
||||||
libab_ref_get(&callee_value->data), params, into);
|
state, libab_ref_get(&callee_value->data), params, into);
|
||||||
} else if (callee_basetype == libab_get_basetype_function(state->ab)) {
|
} else if (callee_basetype == libab_get_basetype_function(state->ab)) {
|
||||||
result = _interpreter_call_function(state,
|
result = _interpreter_call_function(state, value, params, into);
|
||||||
value, params, into);
|
|
||||||
} else {
|
} else {
|
||||||
libab_ref_null(into);
|
libab_ref_null(into);
|
||||||
result = LIBAB_BAD_CALL;
|
result = LIBAB_BAD_CALL;
|
||||||
@@ -471,9 +517,8 @@ libab_result _interpreter_try_call(struct interpreter_state* state, libab_ref* v
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_cast_and_perform_operator_call(libab_operator* to_call,
|
libab_result _interpreter_cast_and_perform_operator_call(
|
||||||
libab_ref_vec* params,
|
libab_operator* to_call, libab_ref_vec* params, libab_ref_vec* new_types,
|
||||||
libab_ref_vec* new_types,
|
|
||||||
libab_ref* into) {
|
libab_ref* into) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_ref_vec new_params;
|
libab_ref_vec new_params;
|
||||||
@@ -485,7 +530,8 @@ libab_result _interpreter_cast_and_perform_operator_call(libab_operator* to_call
|
|||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
libab_ref_free(into);
|
libab_ref_free(into);
|
||||||
result = _interpreter_call_behavior(&to_call->behavior, params, into);
|
result =
|
||||||
|
_interpreter_call_behavior(&to_call->behavior, params, into);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_ref_vec_free(&new_params);
|
libab_ref_vec_free(&new_params);
|
||||||
@@ -499,7 +545,9 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
|||||||
int force_scope);
|
int force_scope);
|
||||||
|
|
||||||
libab_result _interpreter_call_operator(struct interpreter_state* state,
|
libab_result _interpreter_call_operator(struct interpreter_state* state,
|
||||||
libab_operator* to_call, libab_ref* into, libab_ref* scope, ...) {
|
libab_operator* to_call,
|
||||||
|
libab_ref* into, libab_ref* scope,
|
||||||
|
...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_ref_vec params;
|
libab_ref_vec params;
|
||||||
@@ -519,7 +567,8 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = _interpreter_run(state, va_arg(args, libab_tree*), &temp, scope, 0);
|
result =
|
||||||
|
_interpreter_run(state, va_arg(args, libab_tree*), &temp, scope, 0);
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = libab_ref_vec_insert(¶ms, &temp);
|
result = libab_ref_vec_insert(¶ms, &temp);
|
||||||
@@ -528,7 +577,8 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
|
|||||||
libab_ref_free(&temp);
|
libab_ref_free(&temp);
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS && to_call->variant == OPERATOR_INFIX) {
|
if (result == LIBAB_SUCCESS && to_call->variant == OPERATOR_INFIX) {
|
||||||
result = _interpreter_run(state, va_arg(args, libab_tree*), &temp, scope, 0);
|
result = _interpreter_run(state, va_arg(args, libab_tree*), &temp,
|
||||||
|
scope, 0);
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = libab_ref_vec_insert(¶ms, &temp);
|
result = libab_ref_vec_insert(¶ms, &temp);
|
||||||
@@ -538,11 +588,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, ¶ms, &new_types);
|
result = _interpreter_check_types(state, &operator_type->children,
|
||||||
|
¶ms, &new_types);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = _interpreter_cast_and_perform_operator_call(to_call, ¶ms, &new_types, into);
|
result = _interpreter_cast_and_perform_operator_call(
|
||||||
|
to_call, ¶ms, &new_types, into);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_ref_vec_free(¶ms);
|
libab_ref_vec_free(¶ms);
|
||||||
@@ -554,8 +606,8 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
||||||
libab_tree* tree,
|
libab_tree* tree, libab_ref* into,
|
||||||
libab_ref* into, libab_ref* scope) {
|
libab_ref* scope) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_ref param;
|
libab_ref param;
|
||||||
libab_ref callee;
|
libab_ref callee;
|
||||||
@@ -565,7 +617,8 @@ libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
|||||||
|
|
||||||
libab_ref_null(¶m);
|
libab_ref_null(¶m);
|
||||||
result = libab_ref_vec_init(¶ms);
|
result = libab_ref_vec_init(¶ms);
|
||||||
for(; count < tree->children.size - 1 && result == LIBAB_SUCCESS; count++) {
|
for (; count < tree->children.size - 1 && result == LIBAB_SUCCESS;
|
||||||
|
count++) {
|
||||||
libab_ref_free(¶m);
|
libab_ref_free(¶m);
|
||||||
child = vec_index(&tree->children, count);
|
child = vec_index(&tree->children, count);
|
||||||
result = _interpreter_run(state, child, ¶m, scope, 0);
|
result = _interpreter_run(state, child, ¶m, scope, 0);
|
||||||
@@ -581,10 +634,9 @@ libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
|||||||
libab_ref_free(¶m);
|
libab_ref_free(¶m);
|
||||||
|
|
||||||
if (result == LIBAB_SUCCESS) {
|
if (result == LIBAB_SUCCESS) {
|
||||||
result = _interpreter_run(state,
|
result = _interpreter_run(
|
||||||
vec_index(&tree->children,
|
state, vec_index(&tree->children, tree->children.size - 1), &callee,
|
||||||
tree->children.size - 1),
|
scope, 0);
|
||||||
&callee, scope, 0);
|
|
||||||
if (result != LIBAB_SUCCESS) {
|
if (result != LIBAB_SUCCESS) {
|
||||||
libab_ref_vec_free(¶ms);
|
libab_ref_vec_free(¶ms);
|
||||||
}
|
}
|
||||||
@@ -629,23 +681,27 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
|||||||
} else if (tree->variant == TREE_VOID) {
|
} else if (tree->variant == TREE_VOID) {
|
||||||
libab_ref_null(into);
|
libab_ref_null(into);
|
||||||
} else if (tree->variant == TREE_ID) {
|
} else if (tree->variant == TREE_ID) {
|
||||||
libab_table_search_value(libab_ref_get(scope), tree->string_value, into);
|
libab_table_search_value(libab_ref_get(scope), tree->string_value,
|
||||||
|
into);
|
||||||
if (libab_ref_get(into) == NULL) {
|
if (libab_ref_get(into) == NULL) {
|
||||||
result = LIBAB_UNEXPECTED;
|
result = LIBAB_UNEXPECTED;
|
||||||
}
|
}
|
||||||
} else if (tree->variant == TREE_CALL) {
|
} else if (tree->variant == TREE_CALL) {
|
||||||
result = _interpreter_run_function_node(state, tree, into, scope);
|
result = _interpreter_run_function_node(state, tree, into, scope);
|
||||||
} else if (tree->variant == TREE_OP) {
|
} else if (tree->variant == TREE_OP) {
|
||||||
libab_operator* to_call = libab_table_search_operator(libab_ref_get(scope), tree->string_value, OPERATOR_INFIX);
|
libab_operator* to_call = libab_table_search_operator(
|
||||||
|
libab_ref_get(scope), tree->string_value, OPERATOR_INFIX);
|
||||||
result = _interpreter_call_operator(state, to_call, into, scope,
|
result = _interpreter_call_operator(state, to_call, into, scope,
|
||||||
vec_index(&tree->children, 0),
|
vec_index(&tree->children, 0),
|
||||||
vec_index(&tree->children, 1));
|
vec_index(&tree->children, 1));
|
||||||
} else if (tree->variant == TREE_PREFIX_OP) {
|
} else if (tree->variant == TREE_PREFIX_OP) {
|
||||||
libab_operator* to_call = libab_table_search_operator(libab_ref_get(scope), tree->string_value, OPERATOR_PREFIX);
|
libab_operator* to_call = libab_table_search_operator(
|
||||||
|
libab_ref_get(scope), tree->string_value, OPERATOR_PREFIX);
|
||||||
result = _interpreter_call_operator(state, to_call, into, scope,
|
result = _interpreter_call_operator(state, to_call, into, scope,
|
||||||
vec_index(&tree->children, 0));
|
vec_index(&tree->children, 0));
|
||||||
} else if (tree->variant == TREE_POSTFIX_OP) {
|
} else if (tree->variant == TREE_POSTFIX_OP) {
|
||||||
libab_operator* to_call = libab_table_search_operator(libab_ref_get(scope), tree->string_value, OPERATOR_POSTFIX);
|
libab_operator* to_call = libab_table_search_operator(
|
||||||
|
libab_ref_get(scope), tree->string_value, OPERATOR_POSTFIX);
|
||||||
result = _interpreter_call_operator(state, to_call, into, scope,
|
result = _interpreter_call_operator(state, to_call, into, scope,
|
||||||
vec_index(&tree->children, 0));
|
vec_index(&tree->children, 0));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#include "libabacus.h"
|
#include "libabacus.h"
|
||||||
|
#include "debug.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "reserved.h"
|
#include "reserved.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "value.h"
|
#include "value.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "debug.h"
|
|
||||||
|
|
||||||
void _free_function_list(void* function_list) {
|
void _free_function_list(void* function_list) {
|
||||||
libab_function_list_free(function_list);
|
libab_function_list_free(function_list);
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ libab_result libab_ref_vec_init(libab_ref_vec* vec) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result libab_ref_vec_init_copy(libab_ref_vec* vec, libab_ref_vec* copy_of) {
|
libab_result libab_ref_vec_init_copy(libab_ref_vec* vec,
|
||||||
|
libab_ref_vec* copy_of) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
if ((vec->data = malloc(sizeof(*vec->data) * copy_of->capacity))) {
|
if ((vec->data = malloc(sizeof(*vec->data) * copy_of->capacity))) {
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
|
|||||||
(LIBABACUS_TYPE_F_RESOLVED | LIBABACUS_TYPE_F_PLACE));
|
(LIBABACUS_TYPE_F_RESOLVED | LIBABACUS_TYPE_F_PLACE));
|
||||||
check_parents = !(to_resolve->variant & LIBABACUS_TYPE_F_PLACE);
|
check_parents = !(to_resolve->variant & LIBABACUS_TYPE_F_PLACE);
|
||||||
|
|
||||||
if ((to_resolve->variant & LIBABACUS_TYPE_F_PLACE) && (to_resolve->variant & LIBABACUS_TYPE_F_PARENT)) {
|
if ((to_resolve->variant & LIBABACUS_TYPE_F_PLACE) &&
|
||||||
|
(to_resolve->variant & LIBABACUS_TYPE_F_PARENT)) {
|
||||||
result = LIBAB_UNEXPECTED;
|
result = LIBAB_UNEXPECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user