Keep working on structs

This commit is contained in:
2026-07-05 18:01:09 +10:00
parent 338241519e
commit 05733c693c
9 changed files with 187 additions and 59 deletions

View File

@@ -109,6 +109,7 @@ static void doFunction(GroundProgram* program, GroundProgram* newProgram, Ground
static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i);
static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i) {
GroundIdentifier* structName = Ground.Copy.Identifier(program->at[*i].args.at[0].as.ref);
GroundStruct gs = Ground.New.Struct();
// The aim is to compile an instruction which looks like this:
// struct &field OP $value &field OP $value &field OP $value...
@@ -121,10 +122,13 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
// 3 -> attach closure to function
// $value is either a type reference (OP=0), a value reference (OP=1), or none (OP=2)
// Functions will be moved to the end of the struct, and a closure will be attached to all of them
//
// Oh yeah, we also need to make a GroundStruct value
GroundSize structCount = 0;
GroundInstruction output = Ground.New.Instruction(GroundInstruction_STRUCT);
Ground.Instruction.append(&output, Ground.New.Arg.TypeRef(structName));
// Parse struct body
bool parsing = true;
@@ -139,16 +143,71 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
GroundInstruction* instruction = &program->at[*i];
switch (instruction->type) {
case GroundInstruction_SET: {
GroundObjectField* field = malloc(sizeof(GroundObjectField));
if (field == NULL) {
Ground.Log.Error("malloc failed in Ground.Program.preprocess -> doStruct");
Ground.Flags.error = true;
return;
}
snprintf(field->name, 2047, "%s", instruction->args.at[0].as.ref->string);
// 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);
break;
}
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, Ground.New.Arg.Value(Ground.New.Value.Int(2)));
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 2});
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[1]));
break;
}
case GroundInstruction_INIT: {
// init &field -type -> &field 2 -type
GroundObjectField* field = malloc(sizeof(GroundObjectField));
if (field == NULL) {
Ground.Log.Error("malloc failed in Ground.Program.preprocess -> doStruct");
Ground.Flags.error = true;
return;
}
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) {
field->value = Ground.New.Value.Int(0);
break;
}
if (strcmp(field->name, "double") == 0) {
field->value = Ground.New.Value.Double(0.0);
break;
}
if (strcmp(field->name, "string") == 0) {
field->value = Ground.New.Value.String(Ground.New.String(""));
break;
}
if (strcmp(field->name, "char") == 0) {
field->value = Ground.New.Value.Char(0);
break;
}
if (strcmp(field->name, "bool") == 0) {
field->value = Ground.New.Value.Bool(false);
break;
}
if (strcmp(field->name, "function") == 0) {
field->value = Ground.New.Value.Function(Ground.New.Function(state));
break;
}
HASH_ADD_STR(gs.fields, name, field);
// init &field -type -> &field 1 -type
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[0]));
Ground.Instruction.append(&output, Ground.New.Arg.Value(Ground.New.Value.Int(1)));
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1});
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[1]));
break;
}
@@ -170,7 +229,7 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
newProgram->len--;
// Add 3 to signal addition of closure
Ground.Instruction.append(&output, Ground.New.Arg.Value(Ground.New.Value.Int(3)));
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1});
break;
}
case GroundInstruction_ENDSTRUCT: {
@@ -183,6 +242,18 @@ static void doStruct(GroundProgram* program, GroundProgram* newProgram, GroundSt
}
GroundVariable* variable = malloc(sizeof(GroundVariable));
if (variable == NULL) {
Ground.Log.Error("malloc failed in Ground.Program.Preprocess -> doStruct");
Ground.Flags.error = true;
return;
}
snprintf(variable->name, 2047, "%s", structName->string);
variable->value = Ground.New.Value.Struct(gs);
HASH_ADD_STR(state->variables, name, variable);
}
GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* state) {
@@ -197,6 +268,8 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
break;
}
case GroundInstruction_STRUCT: {
doStruct(program, &newProgram, state, &i);
if (Ground.Flags.error) return newProgram;
break;
}
default: {