added "any" type

This commit is contained in:
cozis
2022-12-05 17:02:13 +01:00
parent ea428fdca2
commit 8f2723c3aa
15 changed files with 172 additions and 149 deletions
+2
View File
@@ -68,6 +68,7 @@ static TypeObject t_staticmap = {
.copy = copy,
.hash = hash,
.select = select_,
#warning "Need to walk the references"
};
static Object *copy(Object *self, Heap *heap, Error *err)
@@ -137,6 +138,7 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *error)
case SM_SMAP: return Object_NewStaticMap(slot.as_smap, NULL, map->runt, error);
case SM_NONE: return Object_NewNone(heap, error);
case SM_TYPE: return (Object*) slot.as_type;
case SM_OBJECT: return slot.as_object;
default: assert(0); break;
}
return obj;
+44
View File
@@ -826,6 +826,50 @@ static _Bool step(Runtime *runtime, Error *error)
return 1;
}
case OPCODE_CHECKTYPE:
{
ASSERT(opc == 2);
ASSERT(ops[0].type == OPTP_INT);
ASSERT(ops[1].type == OPTP_STRING);
const char *arg_name;
int arg_index;
arg_index = ops[0].as_int;
arg_name = ops[1].as_string;
if(runtime->frame->used < 2)
{
Error_Report(error, 1, "Frame doesn't own enough objects to execute CHECKTYPE");
return 0;
}
Object *typ = Stack_Top(runtime->stack, 0);
Object *arg = Stack_Top(runtime->stack, -1);
ASSERT(typ != NULL);
ASSERT(arg != NULL);
// Pop type
if(!Runtime_Pop(runtime, error, 1))
return 0;
if (!Object_IsTypeOf(typ, arg, runtime->heap, error)) {
char provided[512];
char allowed[512];
FILE *provided_fp = fmemopen(provided, sizeof(provided), "wb");
FILE *allowed_fp = fmemopen(allowed, sizeof(allowed), "wb");
// TODO: Check for errors from [fmemopen]
Object_Print(typ, allowed_fp);
Object_Print(arg, provided_fp);
fclose(allowed_fp);
fclose(provided_fp);
Error_Report(error, 0, "Argument %d \"%s\" has an unallowed type. Was expected something with type %s but was provided %s",
arg_index+1, arg_name, allowed, provided);
return 0;
}
return 1;
}
case OPCODE_CALL:
{
ASSERT(opc == 2);
+2
View File
@@ -68,6 +68,7 @@ typedef enum {
SM_SMAP,
SM_NONE,
SM_TYPE,
SM_OBJECT,
} StaticMapSlotKind;
typedef struct StaticMapSlot StaticMapSlot;
@@ -83,6 +84,7 @@ struct StaticMapSlot {
double as_float;
int (*as_funct)(Runtime*, Object**, unsigned int, Object*[static MAX_RETS], Error*);
TypeObject *as_type;
Object *as_object;
};
union { int argc; int length; };
};