From 7a4e71bf149ed087d7da72c5b7717932ca3506e3 Mon Sep 17 00:00:00 2001 From: bog Date: Thu, 24 Aug 2023 21:28:54 +0200 Subject: [PATCH] REF: array sub types. --- src/type.c | 77 ++++++++++++++++++++++++++++++++++++++++++------------ src/type.h | 8 +++++- src/vm.c | 4 +-- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/type.c b/src/type.c index 333be75..691cc4a 100644 --- a/src/type.c +++ b/src/type.c @@ -6,7 +6,10 @@ void type_init(type* self, int base_type) { assert(self); self->base_type = base_type; - self->array_type = NULL; + + self->sub_types.size = 0; + self->sub_types.capacity = 1; + self->sub_types.data = malloc(sizeof(type*)); } void type_init_array(type* self, type* array_type) @@ -14,18 +17,41 @@ void type_init_array(type* self, type* array_type) assert(self); assert(array_type); type_init(self, TY_ARRAY); - self->array_type = type_new_clone(array_type); + type_add_sub_type(self, array_type); } void type_free(type* self) { assert(self); - if (self->array_type != NULL) + for (size_t i=0; isub_types.size; i++) { - type_free(self->array_type); - free(self->array_type); + type_free(self->sub_types.data[i]); + free(self->sub_types.data[i]); } + + free(self->sub_types.data); + self->sub_types.size = 0; + self->sub_types.capacity = 0; + self->sub_types.data = NULL; + +} + +void type_add_sub_type(type* self, type* rhs) +{ + assert(self); + assert(rhs); + + if (self->sub_types.size >= self->sub_types.capacity) + { + self->sub_types.capacity *= 2; + self->sub_types.data = + realloc(self->sub_types.data, + sizeof(type*) * self->sub_types.capacity); + } + + self->sub_types.data[self->sub_types.size] = type_new_clone(rhs); + self->sub_types.size++; } type* type_new_clone(type* self) @@ -35,9 +61,9 @@ type* type_new_clone(type* self) type* clone = malloc(sizeof(type)); type_init(clone, self->base_type); - if (self->array_type) + for (size_t i=0; isub_types.size; i++) { - clone->array_type = type_new_clone(self->array_type); + type_add_sub_type(clone, self->sub_types.data[i]); } return clone; @@ -52,16 +78,22 @@ int type_equals(type* self, type* rhs) { return 0; } - - if (self->base_type == TY_ARRAY) + + if (self->sub_types.size != rhs->sub_types.size) { - assert(self->array_type); - assert(rhs->array_type); - return type_equals(self->array_type, rhs->array_type); + return 0; + } + + for (size_t i=0; isub_types.size; i++) + { + if (!type_equals(self->sub_types.data[i], + rhs->sub_types.data[i])) + { + return 0; + } } return 1; - } size_t type_str(type* self, char* buffer, size_t size) @@ -69,11 +101,24 @@ size_t type_str(type* self, char* buffer, size_t size) assert(self); size_t sz = snprintf(buffer, size, "%s", TypesStr[self->base_type] + strlen("TY_")); - - if (self->base_type == TY_ARRAY) + + if (self->sub_types.size > 0) { sz += snprintf(buffer + sz, size - sz, "<"); - sz += type_str(self->array_type, buffer + sz, size - sz); + } + + for (size_t i=0; isub_types.size; i++) + { + if (i > 0) + { + sz += snprintf(buffer + sz, size - sz, ", "); + } + + sz += type_str(self->sub_types.data[i], buffer + sz, size - sz); + } + + if (self->sub_types.size > 0) + { sz += snprintf(buffer + sz, size - sz, ">"); } diff --git a/src/type.h b/src/type.h index 47015be..5a89e09 100644 --- a/src/type.h +++ b/src/type.h @@ -20,13 +20,19 @@ extern char const* TypesStr[]; typedef struct type { int base_type; - struct type* array_type; + struct { + size_t size; + size_t capacity; + struct type** data; + } sub_types; } type; void type_init(type* self, int base_type); void type_init_array(type* self, type* array_type); void type_free(type* self); +void type_add_sub_type(type* self, type* rhs); + type* type_new_clone(type* self); int type_equals(type* self, type* rhs); size_t type_str(type* self, char* buffer, size_t size); diff --git a/src/vm.c b/src/vm.c index 60cf931..175e38d 100644 --- a/src/vm.c +++ b/src/vm.c @@ -634,7 +634,7 @@ void vm_acat(vm* self) array* rhs_arr = rhs->val.array_val; array result_arr; - array_init(&result_arr, lhs->type->array_type); + array_init(&result_arr, lhs->type->sub_types.data[0]); for (size_t i=0; ichildren.size; i++) { @@ -668,7 +668,7 @@ void vm_amul(vm* self) int count = rhs->val.integer; array result_arr; - array_init(&result_arr, lhs->type->array_type); + array_init(&result_arr, lhs->type->sub_types.data[0]); for (int j=0; jchildren.size; i++)