implemented nullable type notation

This commit is contained in:
cozis
2022-12-04 18:39:42 +01:00
parent d610a973f4
commit 7a07a1eb3c
12 changed files with 156 additions and 9 deletions
+62
View File
@@ -0,0 +1,62 @@
#include "../common/defs.h"
#include "objects.h"
static void walk(Object *self, void (*callback)(Object **referer, void *userp), void *userp);
static bool istypeof(Object *self, Object *other);
typedef struct {
Object base;
Object *item;
} NullableObject;
static TypeObject t_nullable = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = TYPENAME_NULLABLE,
.size = sizeof(NullableObject),
.init = NULL,
.free = NULL,
.hash = NULL,
.copy = NULL,
.call = NULL,
.print = NULL,
.select = NULL,
.delete = NULL,
.insert = NULL,
.count = NULL,
.istypeof = istypeof,
.op_eql = NULL,
.walk = walk,
.walkexts = NULL,
};
TypeObject *Object_GetNullableType()
{
return &t_nullable;
}
Object *Object_NewNullable(Object *item, Heap *heap, Error *error)
{
NullableObject *nullable = (NullableObject*) Heap_Malloc(heap, &t_nullable, error);
if (nullable != NULL)
nullable->item = item;
return (Object*) nullable;
}
static bool
istypeof(Object *self,
Object *other)
{
NullableObject *nullable = (NullableObject*) self;
if (Object_IsNone(other))
return true;
return Object_IsTypeOf(nullable->item, other);
}
static void walk(Object *self, void (*callback)(Object **referer, void *userp), void *userp)
{
NullableObject *nullable = (NullableObject*) self;
callback(&nullable->item, userp);
}