REF: array sub types.

main
bog 2023-08-24 21:28:54 +02:00
parent 1a2edd01f1
commit 7a4e71bf14
3 changed files with 70 additions and 19 deletions

View File

@ -6,7 +6,10 @@ void type_init(type* self, int base_type)
{ {
assert(self); assert(self);
self->base_type = base_type; 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) 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(self);
assert(array_type); assert(array_type);
type_init(self, TY_ARRAY); 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) void type_free(type* self)
{ {
assert(self); assert(self);
if (self->array_type != NULL) for (size_t i=0; i<self->sub_types.size; i++)
{ {
type_free(self->array_type); type_free(self->sub_types.data[i]);
free(self->array_type); 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) type* type_new_clone(type* self)
@ -35,9 +61,9 @@ type* type_new_clone(type* self)
type* clone = malloc(sizeof(type)); type* clone = malloc(sizeof(type));
type_init(clone, self->base_type); type_init(clone, self->base_type);
if (self->array_type) for (size_t i=0; i<self->sub_types.size; i++)
{ {
clone->array_type = type_new_clone(self->array_type); type_add_sub_type(clone, self->sub_types.data[i]);
} }
return clone; return clone;
@ -53,15 +79,21 @@ int type_equals(type* self, type* rhs)
return 0; return 0;
} }
if (self->base_type == TY_ARRAY) if (self->sub_types.size != rhs->sub_types.size)
{ {
assert(self->array_type); return 0;
assert(rhs->array_type); }
return type_equals(self->array_type, rhs->array_type);
for (size_t i=0; i<self->sub_types.size; i++)
{
if (!type_equals(self->sub_types.data[i],
rhs->sub_types.data[i]))
{
return 0;
}
} }
return 1; return 1;
} }
size_t type_str(type* self, char* buffer, size_t size) size_t type_str(type* self, char* buffer, size_t size)
@ -70,10 +102,23 @@ size_t type_str(type* self, char* buffer, size_t size)
size_t sz = snprintf(buffer, size, "%s", size_t sz = snprintf(buffer, size, "%s",
TypesStr[self->base_type] + strlen("TY_")); 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 += snprintf(buffer + sz, size - sz, "<");
sz += type_str(self->array_type, buffer + sz, size - sz); }
for (size_t i=0; i<self->sub_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, ">"); sz += snprintf(buffer + sz, size - sz, ">");
} }

View File

@ -20,13 +20,19 @@ extern char const* TypesStr[];
typedef struct type { typedef struct type {
int base_type; int base_type;
struct type* array_type; struct {
size_t size;
size_t capacity;
struct type** data;
} sub_types;
} type; } type;
void type_init(type* self, int base_type); void type_init(type* self, int base_type);
void type_init_array(type* self, type* array_type); void type_init_array(type* self, type* array_type);
void type_free(type* self); void type_free(type* self);
void type_add_sub_type(type* self, type* rhs);
type* type_new_clone(type* self); type* type_new_clone(type* self);
int type_equals(type* self, type* rhs); int type_equals(type* self, type* rhs);
size_t type_str(type* self, char* buffer, size_t size); size_t type_str(type* self, char* buffer, size_t size);

View File

@ -634,7 +634,7 @@ void vm_acat(vm* self)
array* rhs_arr = rhs->val.array_val; array* rhs_arr = rhs->val.array_val;
array result_arr; 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; i<lhs_arr->children.size; i++) for (size_t i=0; i<lhs_arr->children.size; i++)
{ {
@ -668,7 +668,7 @@ void vm_amul(vm* self)
int count = rhs->val.integer; int count = rhs->val.integer;
array result_arr; 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; j<count; j++) for (int j=0; j<count; j++)
{ {
for (size_t i=0; i<arr->children.size; i++) for (size_t i=0; i<arr->children.size; i++)