177 lines
5.0 KiB
C
177 lines
5.0 KiB
C
#include "util.h"
|
|
#include "value.h"
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
|
|
libab_result libab_convert_lex_result(liblex_result to_convert) {
|
|
libab_result result = LIBAB_SUCCESS;
|
|
if (to_convert == LIBLEX_MALLOC) {
|
|
result = LIBAB_MALLOC;
|
|
} else if (to_convert == LIBLEX_INVALID) {
|
|
result = LIBAB_BAD_PATTERN;
|
|
} else if (to_convert == LIBLEX_UNRECOGNIZED) {
|
|
result = LIBAB_FAILED_MATCH;
|
|
}
|
|
return result;
|
|
}
|
|
libab_result libab_convert_ds_result(libds_result to_convert) {
|
|
libab_result result = LIBAB_SUCCESS;
|
|
if (to_convert == LIBDS_MALLOC) {
|
|
result = LIBAB_MALLOC;
|
|
}
|
|
return result;
|
|
}
|
|
libab_result libab_copy_string_range(char** destination, const char* source,
|
|
size_t from, size_t to) {
|
|
libab_result result = LIBAB_SUCCESS;
|
|
size_t string_length = to - from;
|
|
if ((*destination = malloc(string_length + 1)) == NULL) {
|
|
result = LIBAB_MALLOC;
|
|
} else {
|
|
strncpy(*destination, source + from, string_length);
|
|
(*destination)[string_length] = '\0';
|
|
}
|
|
return result;
|
|
}
|
|
libab_result libab_copy_string_size(char** destination, const char* source,
|
|
size_t length) {
|
|
return libab_copy_string_range(destination, source, 0, length);
|
|
}
|
|
libab_result libab_copy_string(char** destination, const char* source) {
|
|
return libab_copy_string_range(destination, source, 0, strlen(source));
|
|
}
|
|
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_table* scope) {
|
|
libab_result result = LIBAB_SUCCESS;
|
|
int resolve_name, check_parents;
|
|
size_t index = 0;
|
|
resolve_name = !(to_resolve->variant &
|
|
(LIBABACUS_TYPE_F_RESOLVED | LIBABACUS_TYPE_F_PLACE));
|
|
check_parents = !(to_resolve->variant & LIBABACUS_TYPE_F_PLACE);
|
|
if (resolve_name) {
|
|
libab_basetype* basetype =
|
|
libab_table_search_basetype(scope, to_resolve->data_u.name);
|
|
if (basetype) {
|
|
free(to_resolve->data_u.name);
|
|
to_resolve->data_u.base = basetype;
|
|
to_resolve->variant |= LIBABACUS_TYPE_F_RESOLVED;
|
|
} else {
|
|
result = LIBAB_BAD_TYPE;
|
|
}
|
|
}
|
|
|
|
if (check_parents && result == LIBAB_SUCCESS) {
|
|
if (to_resolve->variant & LIBABACUS_TYPE_F_PARENT) {
|
|
result = _libab_check_parsetype(to_resolve);
|
|
} else if (to_resolve->data_u.base->count) {
|
|
result = LIBAB_BAD_TYPE;
|
|
}
|
|
}
|
|
|
|
if (to_resolve->variant & LIBABACUS_TYPE_F_PARENT) {
|
|
while (result == LIBAB_SUCCESS && index < to_resolve->children.size) {
|
|
result = libab_resolve_parsetype(
|
|
libab_ref_get(&to_resolve->children.data[index]), scope);
|
|
index++;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void _libab_free_parsetype(void* parsetype) {
|
|
libab_parsetype_free(parsetype);
|
|
free(parsetype);
|
|
}
|
|
|
|
void _libab_parsetype_free(void* parsetype) {
|
|
libab_parsetype_free(parsetype);
|
|
free(parsetype);
|
|
}
|
|
|
|
libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
|
|
libab_ref* into, size_t n, ...) {
|
|
libab_result result = LIBAB_SUCCESS;
|
|
libab_parsetype* parsetype;
|
|
va_list params;
|
|
|
|
va_start(params, n);
|
|
|
|
if((parsetype = malloc(sizeof(*parsetype)))) {
|
|
result = libab_parsetype_init_va(parsetype, to_instantiate, n, params);
|
|
} else {
|
|
result = LIBAB_MALLOC;
|
|
}
|
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
result = libab_ref_new(into, parsetype, _libab_parsetype_free);
|
|
if(result != LIBAB_SUCCESS) {
|
|
libab_parsetype_free(parsetype);
|
|
}
|
|
}
|
|
|
|
if(result != LIBAB_SUCCESS) {
|
|
libab_ref_null(into);
|
|
free(parsetype);
|
|
}
|
|
|
|
va_end(params);
|
|
|
|
return result;
|
|
}
|
|
|
|
void _free_table(void* data) {
|
|
libab_table_free(data);
|
|
free(data);
|
|
}
|
|
|
|
libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
|
|
libab_table* table;
|
|
libab_result result = LIBAB_SUCCESS;
|
|
if((table = malloc(sizeof(*table)))) {
|
|
libab_table_init(table);
|
|
libab_table_set_parent(table, parent);
|
|
result = libab_ref_new(into, table, _free_table);
|
|
|
|
if(result != LIBAB_SUCCESS) {
|
|
_free_table(table);
|
|
}
|
|
} else {
|
|
result = LIBAB_MALLOC;
|
|
}
|
|
|
|
if(result != LIBAB_SUCCESS) {
|
|
libab_ref_null(into);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void _free_value(void* value) {
|
|
libab_value_free(value);
|
|
free(value);
|
|
}
|
|
|
|
libab_result libab_create_value(libab_ref* into, void* data, libab_ref* type) {
|
|
libab_value* value;
|
|
libab_result result = LIBAB_SUCCESS;
|
|
if((value = malloc(sizeof(*value)))) {
|
|
libab_value_init(value, data, type);
|
|
result = libab_ref_new(into, value, _free_value);
|
|
|
|
if(result != LIBAB_SUCCESS) {
|
|
_free_value(value);
|
|
}
|
|
} else {
|
|
result = LIBAB_MALLOC;
|
|
}
|
|
|
|
if(result != LIBAB_SUCCESS) {
|
|
libab_ref_null(into);
|
|
}
|
|
return result;
|
|
}
|