Add a way to construct a parsetype form existing types and a basetype.

This commit is contained in:
Danila Fedorin 2018-04-22 20:44:08 -07:00
parent b12f6d7a31
commit db84019846
2 changed files with 68 additions and 0 deletions

View File

@ -39,6 +39,26 @@ struct libab_parsetype_s {
typedef struct libab_parsetype_s libab_parsetype;
/**
* Creates a new type instance.
* @param into the reference to store the new type into.
* @param from the basetype to instantiate.
* @param n the number of type parameters.
* @return the result of the instantiation.
*/
libab_result libab_parsetype_init(libab_parsetype* into, libab_basetype* from,
size_t n, ...);
/**
* Same as _init, but using a pre-initialized va_list.
* @param into the reference to store the new type into.
* @param from the basetype to instantiate.
* @param n the number of type parameters.
* @param args the list of parameters to this parsetype.
* @return the result of the instantiation.
*/
libab_result libab_parsetype_init_va(libab_parsetype* into, libab_basetype* from,
size_t n, va_list args);
/**
* Frees the data associated with this type, ignoring
* its children.

View File

@ -1,6 +1,54 @@
#include "parsetype.h"
#include <stdlib.h>
libab_result libab_parsetpe_init(libab_parsetype* type, libab_basetype* from,
size_t n, ...) {
libab_result result;
va_list params;
va_start(params, n);
result = libab_parsetype_init_va(type, from, n, params);
va_end(params);
return result;
}
libab_result libab_parsetype_init_va(libab_parsetype* type, libab_basetype* from,
size_t n, va_list args) {
libab_result result = LIBAB_SUCCESS;
size_t base_index = 0, param_index = 0;
int free_vec = 0;
if(from->count > n) result = LIBAB_BAD_TYPE;
while(base_index < from->count && param_index < n) {
if(from->params[base_index].variant == BT_NAME) {
base_index++;
}
param_index++;
}
if(param_index < n) {
result = LIBAB_BAD_TYPE;
}
type->data_u.base = from;
type->variant = LIBABACUS_TYPE_F_RESOLVED;
if(result == LIBAB_SUCCESS && n) {
type->variant |= LIBABACUS_TYPE_F_PARENT;
result = libab_ref_vec_init(&type->children);
if(result == LIBAB_SUCCESS) free_vec = 1;
}
while(n-- && result == LIBAB_SUCCESS) {
libab_ref* ref = va_arg(args, libab_ref*);
result = libab_ref_vec_insert(&type->children, ref);
}
if(free_vec) {
libab_ref_vec_free(&type->children);
}
return result;
}
void libab_parsetype_free(libab_parsetype* type) {
if (!(type->variant & LIBABACUS_TYPE_F_RESOLVED)) {
free(type->data_u.name);