This commit is contained in:
2026-07-27 21:37:08 +10:00
parent 8b04bf39b1
commit 4d5cdb5b6b
4 changed files with 132 additions and 1 deletions

View File

@@ -138,6 +138,89 @@ static GroundBytecodeValue readValue(FILE* f) {
v.as.Function.closure = NULL; v.as.Function.closure = NULL;
break; break;
} }
case 6: {
v.type.type = GroundType_List;
uint64_t count;
fread(&count, 8, 1, f);
v.as.List.count = count;
v.as.List.capacity = count;
if (count > 0) {
v.as.List.at = malloc(sizeof(GroundBytecodeValue) * count);
if (v.as.List.at == NULL) {
Ground.Log.Error("malloc failed in Ground.Bytecode.load");
Ground.Flags.error = true;
return v;
}
for (uint64_t i = 0; i < count; i++) {
v.as.List.at[i] = readValue(f);
if (Ground.Flags.error) {
return v;
}
}
} else {
v.as.List.at = NULL;
}
break;
}
case 7: {
v.type.type = GroundType_Struct;
uint64_t size;
fread(&size, 8, 1, f);
v.as.Struct.size = size;
v.as.Struct.capacity = size;
if (size > 0) {
v.as.Struct.values = malloc(sizeof(GroundBytecodeValue) * size);
if (v.as.Struct.values == NULL) {
Ground.Log.Error("malloc failed in Ground.Bytecode.load");
Ground.Flags.error = true;
return v;
}
for (uint64_t i = 0; i < size; i++) {
v.as.Struct.values[i] = readValue(f);
if (Ground.Flags.error) {
return v;
}
}
} else {
v.as.Struct.values = NULL;
}
break;
}
case 8: {
v.type.type = GroundType_Object;
uint64_t size;
fread(&size, 8, 1, f);
v.as.Object.size = size;
v.as.Object.capacity = size;
if (size > 0) {
v.as.Object.values = malloc(sizeof(GroundBytecodeValue) * size);
if (v.as.Object.values == NULL) {
Ground.Log.Error("malloc failed in Ground.Bytecode.load");
Ground.Flags.error = true;
return v;
}
for (uint64_t i = 0; i < size; i++) {
v.as.Object.values[i] = readValue(f);
if (Ground.Flags.error) {
return v;
}
}
} else {
v.as.Object.values = NULL;
}
break;
}
case 9: {
v.type.type = GroundType_Undefined;
break;
}
case 10: {
v.type.type = GroundType_CoreType;
uint8_t coreType;
fread(&coreType, 1, 1, f);
v.as.CoreType = (enum GroundTypeType)coreType;
break;
}
default: { default: {
Ground.Log.Error("unknown value type in Ground.Bytecode.load"); Ground.Log.Error("unknown value type in Ground.Bytecode.load");
Ground.Flags.error = true; Ground.Flags.error = true;
@@ -173,7 +256,7 @@ GroundBytecode _GroundBytecodeLoad(const char* path) {
uint64_t count; uint64_t count;
fread(&count, 8, 1, f); fread(&count, 8, 1, f);
if (count > 0) { if (count > 0) {
bc.heap.heap = malloc(sizeof(GroundValue) * count); bc.heap.heap = malloc(sizeof(GroundBytecodeValue) * count);
if (bc.heap.heap == NULL) { if (bc.heap.heap == NULL) {
Ground.Log.Error("malloc failed in Ground.Bytecode.load"); Ground.Log.Error("malloc failed in Ground.Bytecode.load");
Ground.Flags.error = true; Ground.Flags.error = true;

View File

@@ -76,6 +76,48 @@ static void writeValue(FILE* f, GroundBytecodeValue* v) {
// closures are generated at runtime, not persisted // closures are generated at runtime, not persisted
break; break;
} }
case GroundType_List: {
uint8_t tag = 6;
fwrite(&tag, 1, 1, f);
uint64_t count = v->as.List.count;
fwrite(&count, 8, 1, f);
for (GroundSize i = 0; i < count; i++) {
writeValue(f, &v->as.List.at[i]);
}
break;
}
case GroundType_Struct: {
uint8_t tag = 7;
fwrite(&tag, 1, 1, f);
uint64_t size = v->as.Struct.size;
fwrite(&size, 8, 1, f);
for (GroundSize i = 0; i < size; i++) {
writeValue(f, &v->as.Struct.values[i]);
}
break;
}
case GroundType_Object: {
uint8_t tag = 8;
fwrite(&tag, 1, 1, f);
uint64_t size = v->as.Object.size;
fwrite(&size, 8, 1, f);
for (GroundSize i = 0; i < size; i++) {
writeValue(f, &v->as.Object.values[i]);
}
break;
}
case GroundType_Undefined: {
uint8_t tag = 9;
fwrite(&tag, 1, 1, f);
break;
}
case GroundType_CoreType: {
uint8_t tag = 10;
fwrite(&tag, 1, 1, f);
uint8_t coreType = (uint8_t)v->as.CoreType;
fwrite(&coreType, 1, 1, f);
break;
}
default: { default: {
uint8_t tag = 0xFF; uint8_t tag = 0xFF;
fwrite(&tag, 1, 1, f); fwrite(&tag, 1, 1, f);

View File

@@ -66,6 +66,7 @@ static inline GroundBytecodeStruct doStruct(GroundStruct* gs) {
GroundObjectField *s, *tmp; GroundObjectField *s, *tmp;
HASH_ITER(hh, gs->fields, s, tmp) { HASH_ITER(hh, gs->fields, s, tmp) {
fprintf(stderr, "DEBUG doStruct: field='%s' value.type=%d\n", s->name, s->value.type.type);
if (gbs.size >= gbs.capacity) { if (gbs.size >= gbs.capacity) {
GroundBytecodeValue* tmp = malloc(sizeof(GroundBytecodeValue) * gbs.capacity * 2); GroundBytecodeValue* tmp = malloc(sizeof(GroundBytecodeValue) * gbs.capacity * 2);
if (tmp == NULL) { if (tmp == NULL) {

View File

@@ -248,6 +248,11 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string); snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string);
field->offset = fieldCount++; field->offset = fieldCount++;
if (fnVar != NULL && fnVar->value.type.type == GroundType_Function) {
field->value = Ground.Copy.Value(&fnVar->value);
fprintf(stderr, "DEBUG: set field value to Function, field->value.type=%d\n", field->value.type.type);
}
HASH_ADD_STR(gs.fields, name, field); HASH_ADD_STR(gs.fields, name, field);
// Read and remove the emitted instruction // Read and remove the emitted instruction