From 6929f055de4d001918c8cb1162ae5bf7d31b4ae2 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Wed, 8 Jul 2026 12:06:18 +1000 Subject: [PATCH] object initialisation --- src/Bytecode/Instruction/execute.c | 47 ++++++++++-------------------- src/New/Bytecode.c | 6 ++-- src/Program/preprocess.c | 33 ++++++++++----------- src/Stringify/BytecodeValue.c | 19 ++++++++++-- 4 files changed, 50 insertions(+), 55 deletions(-) diff --git a/src/Bytecode/Instruction/execute.c b/src/Bytecode/Instruction/execute.c index 93e88e8..52d0731 100644 --- a/src/Bytecode/Instruction/execute.c +++ b/src/Bytecode/Instruction/execute.c @@ -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(""); - break; - default: - printf("", 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; } diff --git a/src/New/Bytecode.c b/src/New/Bytecode.c index 7a72f4d..2428128 100644 --- a/src/New/Bytecode.c +++ b/src/New/Bytecode.c @@ -110,12 +110,12 @@ static inline GroundSize doOffsets(GroundProgram* gp, GroundState* state, Ground s->offset = i++; } - // Now go through and add offsets to everything - for (GroundSize j = 0; j < inst->args.len; j++) { + // Now go through and add offsets to everything (skip struct name at index 0) + for (GroundSize j = 1; j < inst->args.len; j++) { // struct field name GroundArg* arg = &inst->args.at[j]; GroundObjectField* field = NULL; - HASH_FIND_STR(state->variables, arg->as.ref->string, field); + HASH_FIND_STR(gs->fields, arg->as.ref->string, field); if (field == NULL) { Ground.Log.Error("unexpected null struct field in Ground.New.Bytecode -> doOffsets"); Ground.Flags.error = true; diff --git a/src/Program/preprocess.c b/src/Program/preprocess.c index 98072cc..7f08d61 100644 --- a/src/Program/preprocess.c +++ b/src/Program/preprocess.c @@ -156,6 +156,11 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt // If we know the value type now, add it to the instruction if (instruction->args.at[1].type == GroundArg_Value) { field->value = Ground.Copy.Value(&instruction->args.at[1].as.value); + HASH_ADD_STR(gs.fields, name, field); + // set &field $value -> &field 2 $value + Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[0])); + Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 2}); + Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[1])); break; } @@ -178,29 +183,21 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string); // If we know the type now, add it to the instruction - if (strcmp(field->name, "int") == 0) { + char* typeName = instruction->args.at[1].as.ref->string; + if (strcmp(typeName, "int") == 0) { field->value = Ground.New.Value.Int(0); - break; - } - if (strcmp(field->name, "double") == 0) { + } else if (strcmp(typeName, "double") == 0) { field->value = Ground.New.Value.Double(0.0); - break; - } - if (strcmp(field->name, "string") == 0) { + } else if (strcmp(typeName, "string") == 0) { field->value = Ground.New.Value.String(Ground.New.String("")); - break; - } - if (strcmp(field->name, "char") == 0) { + } else if (strcmp(typeName, "char") == 0) { field->value = Ground.New.Value.Char(0); - break; - } - if (strcmp(field->name, "bool") == 0) { + } else if (strcmp(typeName, "bool") == 0) { field->value = Ground.New.Value.Bool(false); - break; - } - if (strcmp(field->name, "function") == 0) { + } else if (strcmp(typeName, "function") == 0) { field->value = Ground.New.Value.Function(Ground.New.Function(state)); - break; + } else { + field->value = Ground.New.Value.Int(0); } HASH_ADD_STR(gs.fields, name, field); @@ -229,7 +226,7 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt newProgram->len--; // Add 3 to signal addition of closure - Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1}); + Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 3}); break; } case GroundInstruction_ENDSTRUCT: { diff --git a/src/Stringify/BytecodeValue.c b/src/Stringify/BytecodeValue.c index a8e6deb..ea2c9d5 100644 --- a/src/Stringify/BytecodeValue.c +++ b/src/Stringify/BytecodeValue.c @@ -1,4 +1,5 @@ #include "../../include/ground.h" +#include "../include/estr.h" #include char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) { @@ -53,10 +54,24 @@ char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) { // TODO implement function stringification } case GroundType_Struct: { - // TODO implement struct stringification + Estr str = CREATE_ESTR("as.Struct.size; i++) { + char* field = Ground.Stringify.BytecodeValue(&value->as.Struct.values[i]); + APPEND_ESTR(str, field); + free(field); + } + APPEND_ESTR(str, " }>"); + return str.str; } case GroundType_Object: { - // TODO implement object stringification + Estr str = CREATE_ESTR("as.Struct.size; i++) { + char* field = Ground.Stringify.BytecodeValue(&value->as.Struct.values[i]); + APPEND_ESTR(str, field); + free(field); + } + APPEND_ESTR(str, " }>"); + return str.str; } }