diff --git a/include/util.h b/include/util.h index 91144a4..c1b1a30 100644 --- a/include/util.h +++ b/include/util.h @@ -56,5 +56,14 @@ libab_result libab_copy_string(char** destination, const char* source); */ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, libab_table* scope); +/** + * Creates a new type instance, and stores it into the given reference. + * @param to_instantiate the basetype to instantiate. + * @param into the reference to store the new type into. + * @param n the number of type parameters. + * @return the result of the instantiation. + */ +libab_result libab_instantiate_basetype(libab_basetype* to_instantiate, + libab_ref* into, size_t n, ...); #endif diff --git a/src/util.c b/src/util.c index 819b912..58167d9 100644 --- a/src/util.c +++ b/src/util.c @@ -1,5 +1,6 @@ #include "util.h" #include +#include libab_result libab_convert_lex_result(liblex_result to_convert) { libab_result result = LIBAB_SUCCESS; @@ -80,3 +81,43 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, 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) { + free(parsetype); + } + + va_end(params); + + return result; +}