libabacus/src/util.c

125 lines
3.8 KiB
C
Raw Normal View History

2018-02-11 22:32:42 -08:00
#include "util.h"
#include <stdlib.h>
#include <stdarg.h>
libab_result libab_convert_lex_result(liblex_result to_convert) {
libab_result result = LIBAB_SUCCESS;
2018-04-21 14:09:01 -07:00
if (to_convert == LIBLEX_MALLOC) {
result = LIBAB_MALLOC;
2018-04-21 14:09:01 -07:00
} else if (to_convert == LIBLEX_INVALID) {
result = LIBAB_BAD_PATTERN;
2018-04-21 14:09:01 -07:00
} 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;
2018-04-21 14:09:01 -07:00
if (to_convert == LIBDS_MALLOC) {
result = LIBAB_MALLOC;
}
return result;
}
2018-04-21 14:09:01 -07:00
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;
2018-04-21 14:09:01 -07:00
if ((*destination = malloc(string_length + 1)) == NULL) {
result = LIBAB_MALLOC;
} else {
strncpy(*destination, source + from, string_length);
(*destination)[string_length] = '\0';
}
return result;
}
2018-04-21 14:09:01 -07:00
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));
}
2018-04-19 23:17:54 -07:00
libab_result _libab_check_parsetype(libab_parsetype* to_check) {
libab_result result = LIBAB_SUCCESS;
return result;
}
2018-04-21 14:09:01 -07:00
libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
libab_table* scope) {
2018-04-19 23:17:54 -07:00
libab_result result = LIBAB_SUCCESS;
int resolve_name, check_parents;
size_t index = 0;
2018-04-21 14:09:01 -07:00
resolve_name = !(to_resolve->variant &
(LIBABACUS_TYPE_F_RESOLVED | LIBABACUS_TYPE_F_PLACE));
2018-04-19 23:17:54 -07:00
check_parents = !(to_resolve->variant & LIBABACUS_TYPE_F_PLACE);
2018-04-21 14:09:01 -07:00
if (resolve_name) {
libab_basetype* basetype =
libab_table_search_basetype(scope, to_resolve->data_u.name);
if (basetype) {
2018-04-19 23:17:54 -07:00
free(to_resolve->data_u.name);
to_resolve->data_u.base = basetype;
to_resolve->variant |= LIBABACUS_TYPE_F_RESOLVED;
} else {
result = LIBAB_BAD_TYPE;
}
}
2018-04-21 14:09:01 -07:00
if (check_parents && result == LIBAB_SUCCESS) {
if (to_resolve->variant & LIBABACUS_TYPE_F_PARENT) {
2018-04-19 23:17:54 -07:00
result = _libab_check_parsetype(to_resolve);
2018-04-21 14:09:01 -07:00
} else if (to_resolve->data_u.base->count) {
2018-04-19 23:17:54 -07:00
result = LIBAB_BAD_TYPE;
}
}
2018-04-21 14:09:01 -07:00
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);
2018-04-19 23:17:54 -07:00
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;
}