object initialisation

This commit is contained in:
2026-07-08 12:06:18 +10:00
parent b507d79804
commit 6929f055de
4 changed files with 50 additions and 55 deletions

View File

@@ -17,32 +17,6 @@
#define END(res) (struct GroundExecutionResult) { .type = _GER_END, .as.end = res }
#define RETURN(val) (struct GroundExecutionResult) { .type = _GER_RETURN, .as.value = val }
static void printValue(GroundBytecodeValue* val) {
switch (val->type.type) {
case GroundType_Int:
printf("%" PRId64, val->as.Int);
break;
case GroundType_Double:
printf("%f", val->as.Double);
break;
case GroundType_Bool:
printf(val->as.Bool ? "true" : "false");
break;
case GroundType_Char:
printf("%c", val->as.Char);
break;
case GroundType_String:
printf("%s", val->as.String.cstr);
break;
case GroundType_Function:
printf("<function>");
break;
default:
printf("<fixme opt=%d>", val->type.type);
break;
}
}
static ffi_type* ffiTypeFromGroundType(GroundType* type) {
switch (type->type) {
case GroundType_Int: return &ffi_type_sint64;
@@ -101,15 +75,13 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
}
PRINT: {
for (GroundSize i = 0; i < instruction->args.len; i++) {
printValue(HEAP_GET(heap, instruction->args.at[i]));
printf(" ");
printf("%s ", Ground.Stringify.BytecodeValue(HEAP_GET(heap, instruction->args.at[i])));
}
return CONTINUE;
}
PRINTLN: {
for (GroundSize i = 0; i < instruction->args.len; i++) {
printValue(HEAP_GET(heap, instruction->args.at[i]));
printf(" ");
printf("%s ", Ground.Stringify.BytecodeValue(HEAP_GET(heap, instruction->args.at[i])));
}
printf("\n");
return CONTINUE;
@@ -935,7 +907,8 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
for (GroundSize i = 1; i < instruction->args.len; i++) {
GroundSize offset = instruction->args.at[i];
i++;
switch (i) {
GroundSize op = instruction->args.at[i];
switch (op) {
case 0: break;
case 1: {
i++;
@@ -1024,7 +997,16 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
GroundBytecodeValue* type = HEAP_GET(heap, instruction->args.at[1]);
switch (type->type.type) {
case GroundType_Struct: {
// TODO nested structs
// copy the contents of the struct into the object
GroundBytecodeObject object = {
.size = type->as.Struct.size,
.capacity = type->as.Struct.size,
.values = malloc(sizeof(GroundBytecodeValue) * type->as.Struct.size)
};
for (GroundSize i = 0; i < type->as.Struct.size; i++) {
object.values[i] = Ground.Copy.BytecodeValue(&type->as.Struct.values[i]);
}
HEAP_SET(heap, instruction->args.at[0], ((GroundBytecodeValue) {.as.Object = object, .type = {GroundType_Object}}));
break;
}
case GroundType_CoreType: {
@@ -1052,6 +1034,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
default: break; // TODO implement all the other stuff
}
}
default: break; // should not be reached
}
return CONTINUE;
}