added sum types

This commit is contained in:
cozis
2022-12-05 11:36:19 +01:00
parent 7a07a1eb3c
commit ea428fdca2
13 changed files with 206 additions and 25 deletions
+27
View File
@@ -48,6 +48,7 @@ static void walk(Object *self, void (*callback)(Object **referer, void *userp),
static void walkexts(Object *self, void (*callback)(void **referer, unsigned int size, void *userp), void *userp);
static Object *copy(Object *self, Heap *heap, Error *err);
static int hash(Object *self);
static bool istypeof(Object *self, Object *other, Heap *heap, Error *error);
static TypeObject t_map = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
@@ -59,10 +60,36 @@ static TypeObject t_map = {
.insert = insert,
.count = count,
.print = print,
.istypeof = istypeof,
.walk = walk,
.walkexts = walkexts,
};
static bool
istypeof(Object *self,
Object *other,
Heap *heap,
Error *error)
{
MapObject *map = (MapObject*) self;
if (!Object_IsMap(other))
return false;
for (int i = 0; i < map->count; i++) {
Object *key = map->keys[i];
Object *val = map->vals[i];
Object *val2 = Object_Select(other, key, heap, error);
if (val2 == NULL)
return false;
if (!Object_IsTypeOf(val, val2, heap, error))
return false;
}
return true;
}
static inline int calc_capacity(int mapper_size)
{
return mapper_size * 2.0 / 3.0;