Compare commits

...

5 Commits

6 changed files with 148 additions and 59 deletions

View File

@ -13,31 +13,31 @@
* Frees a libab_function.
* @param func the function to free.
*/
void free_function(void* func);
void libab_free_function(void* func);
/**
* Frees a libab_function_list.
* @param func_list the function list to free.
*/
void free_function_list(void* func_list);
void libab_free_function_list(void* func_list);
/**
* Frees a unit. This is a no-op.
* @param unit the unit to free.
*/
void free_unit(void* unit);
void libab_free_unit(void* unit);
/**
* Frees a parsetype.
* @param parsetype the parsetype to free.
*/
void free_parsetype(void* parsetype);
void libab_free_parsetype(void* parsetype);
/**
* Frees a table.
* @param table the table to free.
*/
void free_table(void* table);
void libab_free_table(void* table);
/**
* Frees a value.
* @param value the value to free.
*/
void free_value(void* value);
void libab_free_value(void* value);
#endif

View File

@ -55,8 +55,18 @@ libab_result libab_copy_string(char** destination, const char* source);
* @param to_resolve the parsetype to resolve.
* @param scope the scope to use for resolving the type info.
*/
libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
libab_result libab_resolve_parsetype_inplace(libab_parsetype* to_resolve,
libab_table* scope);
/**
* Resolves the given parsetype, looking through the scope to find
* all referenced base types, and creates a copy with these basetypes.
* @param to_resolve the parsetype to resolve.
* @param scope the scope to use for resolving the type info.
* @return the result of the operation.
*/
libab_result libab_resolve_parsetype_copy(libab_parsetype* to_resolve,
libab_table* scope,
libab_ref* into);
/**
* Creates a new type instance, and stores it into the given reference.
* @param to_instantiate the basetype to instantiate.
@ -143,6 +153,13 @@ libab_result libab_create_function_list(libab_ref* into, libab_ref* type);
*/
libab_result libab_put_table_value(libab_table* table, const char* key,
libab_ref* value);
/**
* Gets the basetype of a parsetype.
* @param type the parsetype to get the basetype of.
* @param scope the scope to use for searches.
* @return the basetype.
*/
libab_basetype* libab_get_basetype(libab_parsetype* type, libab_table* scope);
/**
* Returns the data stored in the given reference to a libab_value.
* This is not the same as libab_ref_get: libab_ref_get will return directly

View File

@ -6,26 +6,26 @@
#include "value.h"
#include <stdlib.h>
void free_function(void* func) {
void libab_free_function(void* func) {
libab_function_free(func);
free(func);
}
void free_function_list(void* function_list) {
void libab_free_function_list(void* function_list) {
libab_function_list_free(function_list);
free(function_list);
}
void free_unit(void* unit) {
void libab_free_unit(void* unit) {
}
void free_parsetype(void* parsetype) {
void libab_free_parsetype(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
}
void free_table(void* table) {
void libab_free_table(void* table) {
libab_table_free(table);
free(table);
}
void free_value(void* value) {
void libab_free_value(void* value) {
libab_value_free(value);
free(value);
}

View File

@ -147,6 +147,7 @@ libab_result _interpreter_compare_types(libab_ref* left_type,
size_t index = 0;
libab_ref temp_left;
libab_ref temp_right;
result = (left->data_u.base == right->data_u.base)
? LIBAB_SUCCESS
: LIBAB_MISMATCHED_TYPE;
@ -223,9 +224,9 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type,
}
if (result == LIBAB_SUCCESS) {
result = libab_ref_new(into, copy, free_parsetype);
result = libab_ref_new(into, copy, libab_free_parsetype);
if (result != LIBAB_SUCCESS) {
free_parsetype(copy);
libab_free_parsetype(copy);
}
}
}
@ -997,31 +998,30 @@ int _interpreter_compare_function_param(
return data->variant == TREE_FUN_PARAM;
}
int _interpreter_foreach_resolve_function_param(
void* data, va_list args) {
libab_tree* tree = data;
libab_ref* scope = va_arg(args, libab_ref*);
libab_parsetype* type = libab_ref_get(&tree->type);
return libab_resolve_parsetype(type, libab_ref_get(scope));
}
libab_result _interpreter_resolve_function_params(
struct interpreter_state* state, libab_tree* tree,
libab_ref* scope) {
return vec_foreach(&tree->children, NULL, _interpreter_compare_function_param,
_interpreter_foreach_resolve_function_param, scope);
libab_result _interpreter_resolve_and_insert_param(
libab_parsetype* type, libab_table* scope, libab_ref_vec* into) {
libab_result result = LIBAB_SUCCESS;
libab_ref copy;
result = libab_resolve_parsetype_copy(type, scope, &copy);
if(result == LIBAB_SUCCESS) {
result = libab_ref_vec_insert(into, &copy);
}
libab_ref_free(&copy);
return result;
}
int _interpreter_foreach_insert_function_param(
void* data, va_list args) {
libab_tree* tree = data;
libab_ref_vec* into = va_arg(args, libab_ref_vec*);
return libab_ref_vec_insert(into, &tree->type);
libab_ref* scope = va_arg(args, libab_ref*);
return _interpreter_resolve_and_insert_param(libab_ref_get(&tree->type),
libab_ref_get(scope),
into);
}
libab_result _interpreter_create_function_type(
struct interpreter_state* state, libab_tree* tree, libab_parsetype** type) {
struct interpreter_state* state, libab_tree* tree, libab_ref* scope, libab_parsetype** type) {
libab_result result = LIBAB_SUCCESS;
libab_basetype* funciton_type = libab_get_basetype_function(state->ab);
@ -1035,9 +1035,10 @@ libab_result _interpreter_create_function_type(
if(result == LIBAB_SUCCESS) {
result = vec_foreach(&tree->children, NULL, _interpreter_compare_function_param,
_interpreter_foreach_insert_function_param, &(*type)->children);
_interpreter_foreach_insert_function_param, &(*type)->children, scope);
if(result == LIBAB_SUCCESS) {
result = libab_ref_vec_insert(&(*type)->children, &tree->type);
result = _interpreter_resolve_and_insert_param(libab_ref_get(&tree->type),
libab_ref_get(scope), &(*type)->children);
}
if(result != LIBAB_SUCCESS) {
libab_ref_vec_free(&(*type)->children);
@ -1053,12 +1054,12 @@ libab_result _interpreter_create_function_type(
}
libab_result _interpreter_wrap_function_type(
struct interpreter_state* state, libab_tree* tree, libab_ref* into) {
struct interpreter_state* state, libab_tree* tree, libab_ref* scope, libab_ref* into) {
libab_parsetype* type;
libab_result result = _interpreter_create_function_type(state, tree, &type);
libab_result result = _interpreter_create_function_type(state, tree, scope, &type);
if(result == LIBAB_SUCCESS) {
result = libab_ref_new(into, type, free_parsetype);
result = libab_ref_new(into, type, libab_free_parsetype);
}
if(result != LIBAB_SUCCESS) {
@ -1075,11 +1076,11 @@ libab_result _interpreter_create_function_value(
libab_ref_null(&type);
libab_ref_null(into);
result = libab_create_function_tree(&function, free_function, tree, scope);
result = libab_create_function_tree(&function, libab_free_function, tree, scope);
if(result == LIBAB_SUCCESS) {
libab_ref_free(&type);
result = _interpreter_wrap_function_type(state, tree, &type);
result = _interpreter_wrap_function_type(state, tree, scope, &type);
}
if(result == LIBAB_SUCCESS) {

View File

@ -7,14 +7,14 @@
#include <stdlib.h>
#include "free_functions.h"
static libab_basetype _basetype_function_list = {free_function_list, NULL, 0};
static libab_basetype _basetype_function_list = {libab_free_function_list, NULL, 0};
static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}};
static libab_basetype _basetype_function = {free_function,
static libab_basetype _basetype_function = {libab_free_function,
_basetype_function_params, 1};
static libab_basetype _basetype_unit = { free_unit, NULL, 0 };
static libab_basetype _basetype_unit = { libab_free_unit, NULL, 0 };
libab_result _prepare_types(libab* ab, void (*free_function)(void*));
@ -154,7 +154,7 @@ libab_result _create_value_function_internal(libab_ref* into, libab_ref* type,
libab_ref* scope) {
libab_ref function_ref;
libab_result result =
libab_create_function_internal(&function_ref, free_function, func, scope);
libab_create_function_internal(&function_ref, libab_free_function, func, scope);
libab_ref_null(into);
if (result == LIBAB_SUCCESS) {
libab_ref_free(into);
@ -340,7 +340,7 @@ libab_result libab_create_type(libab* ab, libab_ref* into, const char* type) {
result = libab_parser_parse_type(&ab->parser, &tokens, type, into);
}
if (result == LIBAB_SUCCESS) {
result = libab_resolve_parsetype(libab_ref_get(into),
result = libab_resolve_parsetype_inplace(libab_ref_get(into),
libab_ref_get(&ab->table));
}
ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free);

View File

@ -45,7 +45,7 @@ libab_result _libab_check_parsetype(libab_parsetype* to_check) {
libab_result result = LIBAB_SUCCESS;
return result;
}
libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
libab_result libab_resolve_parsetype_inplace(libab_parsetype* to_resolve,
libab_table* scope) {
libab_result result = LIBAB_SUCCESS;
int resolve_name, check_parents;
@ -61,7 +61,7 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
if (resolve_name && result == LIBAB_SUCCESS) {
libab_basetype* basetype =
libab_table_search_basetype(scope, to_resolve->data_u.name);
libab_get_basetype(to_resolve, scope);
if (basetype) {
free(to_resolve->data_u.name);
to_resolve->data_u.base = basetype;
@ -81,7 +81,7 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
if (to_resolve->variant & LIBABACUS_TYPE_F_PARENT) {
while (result == LIBAB_SUCCESS && index < to_resolve->children.size) {
result = libab_resolve_parsetype(
result = libab_resolve_parsetype_inplace(
libab_ref_get(&to_resolve->children.data[index]), scope);
index++;
}
@ -90,14 +90,69 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
return result;
}
void _libab_free_parsetype(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
}
libab_result libab_resolve_parsetype_copy(libab_parsetype* to_resolve,
libab_table* scope,
libab_ref* into) {
libab_result result = LIBAB_SUCCESS;
libab_parsetype* parsetype;
int is_placeholer = to_resolve->variant & LIBABACUS_TYPE_F_PLACE;
if((parsetype = malloc(sizeof(*parsetype)))) {
parsetype->variant = to_resolve->variant;
if(!is_placeholer) {
parsetype->variant |= LIBABACUS_TYPE_F_RESOLVED;
}
} else {
result = LIBAB_MALLOC;
}
void _libab_parsetype_free(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
if(result == LIBAB_SUCCESS) {
if(!is_placeholer) {
libab_basetype* parent_basetype = libab_get_basetype(to_resolve, scope);
if(parent_basetype) {
parsetype->data_u.base = parent_basetype;
} else {
result = LIBAB_UNKNOWN_TYPE;
}
} else {
result = libab_copy_string(&parsetype->data_u.name, to_resolve->data_u.name);
}
}
if(result == LIBAB_SUCCESS && (to_resolve->variant & LIBABACUS_TYPE_F_PARENT)) {
result = libab_ref_vec_init(&parsetype->children);
}
if(result == LIBAB_SUCCESS && (to_resolve->variant & LIBABACUS_TYPE_F_PARENT)) {
size_t i;
libab_ref temp;
libab_ref new_type;
for(i = 0; i < to_resolve->children.size && result == LIBAB_SUCCESS; i++) {
libab_ref_vec_index(&to_resolve->children, i, &temp);
result = libab_resolve_parsetype_copy(libab_ref_get(&temp), scope, &new_type);
if(result == LIBAB_SUCCESS) {
libab_ref_vec_insert(&parsetype->children, &new_type);
}
libab_ref_free(&new_type);
libab_ref_free(&temp);
}
if(result != LIBAB_SUCCESS) {
libab_ref_vec_free(&parsetype->children);
}
}
if(result == LIBAB_SUCCESS) {
result = libab_ref_new(into, parsetype, libab_free_parsetype);
if(result != LIBAB_SUCCESS) libab_parsetype_free(parsetype);
}
if(result != LIBAB_SUCCESS) {
free(parsetype);
libab_ref_null(into);
}
return result;
}
libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
@ -115,7 +170,7 @@ libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
}
if (result == LIBAB_SUCCESS) {
result = libab_ref_new(into, parsetype, _libab_parsetype_free);
result = libab_ref_new(into, parsetype, libab_free_parsetype);
if (result != LIBAB_SUCCESS) {
libab_parsetype_free(parsetype);
}
@ -137,10 +192,10 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
if ((table = malloc(sizeof(*table)))) {
libab_table_init(table);
libab_table_set_parent(table, parent);
result = libab_ref_new(into, table, free_table);
result = libab_ref_new(into, table, libab_free_table);
if (result != LIBAB_SUCCESS) {
free_table(table);
libab_free_table(table);
}
} else {
result = LIBAB_MALLOC;
@ -158,10 +213,10 @@ libab_result libab_create_value_ref(libab_ref* into, libab_ref* data,
libab_result result = LIBAB_SUCCESS;
if ((value = malloc(sizeof(*value)))) {
libab_value_init_ref(value, data, type);
result = libab_ref_new(into, value, free_value);
result = libab_ref_new(into, value, libab_free_value);
if (result != LIBAB_SUCCESS) {
free_value(value);
libab_free_value(value);
}
} else {
result = LIBAB_MALLOC;
@ -185,7 +240,7 @@ libab_result libab_create_value_raw(libab_ref* into, void* data,
}
if (result == LIBAB_SUCCESS) {
result = libab_ref_new(into, value, free_value);
result = libab_ref_new(into, value, libab_free_value);
if (result != LIBAB_SUCCESS) {
libab_value_free(value);
}
@ -332,6 +387,22 @@ libab_result libab_put_table_value(libab_table* table, const char* key,
return result;
}
libab_basetype* libab_get_basetype(libab_parsetype* type, libab_table* scope) {
libab_ref type_param;
libab_basetype* to_return;
if(type->variant & LIBABACUS_TYPE_F_RESOLVED) {
to_return = type->data_u.base;
} else {
to_return = libab_table_search_basetype(scope, type->data_u.name);
if(to_return == NULL) {
libab_table_search_type_param(scope, type->data_u.name, &type_param);
to_return = ((libab_parsetype*)libab_ref_get(&type_param))->data_u.base;
libab_ref_free(&type_param);
}
}
return to_return;
}
void* libab_unwrap_value(libab_ref* ref) {
libab_value* value = libab_ref_get(ref);
return libab_ref_get(&value->data);