From 4d5cdb5b6b4650260b5ad675d9cb6e5b72a332c0 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Mon, 27 Jul 2026 21:37:08 +1000 Subject: [PATCH] Stuff --- src/Bytecode/load.c | 85 +++++++++++++++++++++++++++++++++++++++- src/Bytecode/save.c | 42 ++++++++++++++++++++ src/New/BytecodeValue.c | 1 + src/Program/preprocess.c | 5 +++ 4 files changed, 132 insertions(+), 1 deletion(-) diff --git a/src/Bytecode/load.c b/src/Bytecode/load.c index 60dad9a..9979728 100644 --- a/src/Bytecode/load.c +++ b/src/Bytecode/load.c @@ -138,6 +138,89 @@ static GroundBytecodeValue readValue(FILE* f) { v.as.Function.closure = NULL; 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: { Ground.Log.Error("unknown value type in Ground.Bytecode.load"); Ground.Flags.error = true; @@ -173,7 +256,7 @@ GroundBytecode _GroundBytecodeLoad(const char* path) { uint64_t count; fread(&count, 8, 1, f); if (count > 0) { - bc.heap.heap = malloc(sizeof(GroundValue) * count); + bc.heap.heap = malloc(sizeof(GroundBytecodeValue) * count); if (bc.heap.heap == NULL) { Ground.Log.Error("malloc failed in Ground.Bytecode.load"); Ground.Flags.error = true; diff --git a/src/Bytecode/save.c b/src/Bytecode/save.c index f7efb43..58322be 100644 --- a/src/Bytecode/save.c +++ b/src/Bytecode/save.c @@ -76,6 +76,48 @@ static void writeValue(FILE* f, GroundBytecodeValue* v) { // closures are generated at runtime, not persisted 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: { uint8_t tag = 0xFF; fwrite(&tag, 1, 1, f); diff --git a/src/New/BytecodeValue.c b/src/New/BytecodeValue.c index a5cbca0..dfdf46a 100644 --- a/src/New/BytecodeValue.c +++ b/src/New/BytecodeValue.c @@ -66,6 +66,7 @@ static inline GroundBytecodeStruct doStruct(GroundStruct* gs) { GroundObjectField *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) { GroundBytecodeValue* tmp = malloc(sizeof(GroundBytecodeValue) * gbs.capacity * 2); if (tmp == NULL) { diff --git a/src/Program/preprocess.c b/src/Program/preprocess.c index 75edd2e..4f347f3 100644 --- a/src/Program/preprocess.c +++ b/src/Program/preprocess.c @@ -248,6 +248,11 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string); 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); // Read and remove the emitted instruction