Start work on preprocessing structs

This commit is contained in:
2026-07-05 15:08:35 +10:00
parent 3b8bcdf020
commit 338241519e

View File

@@ -1,17 +1,11 @@
#include "../../include/ground.h"
GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* state) {
GroundProgram newProgram = Ground.New.Program();
for (GroundSize i = 0; i < program->len; i++) {
switch (program->at[i].type) {
case GroundInstruction_FUN: {
static void doFunction(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i) {
GroundFunction function = Ground.New.Function(state);
// Parse signature
GroundInstruction* sig = &program->at[i];
GroundInstruction* sig = &program->at[*i];
char* id = sig->args.at[0].as.ref->string;
for (GroundSize j = 1; j < sig->args.len; j++) {
@@ -28,7 +22,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
if (argVar == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
return newProgram;
return;
}
snprintf(argVar->name, 2047, "%s", argId);
argVar->value = Ground.New.Value.Int(0);
@@ -40,13 +34,13 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
GroundSize structCount = 0;
for (;;) {
i++;
if (i >= program->len) {
(*i)++;
if (*i >= program->len) {
Ground.Flags.error = true;
Ground.Log.Error("expecting ENDFUN instruction, reached end of program in Ground.Program.Preprocess");
return newProgram;
return;
}
GroundInstruction* inst = &program->at[i];
GroundInstruction* inst = &program->at[*i];
switch (inst->type) {
case GroundInstruction_FUN: {
funCount++;
@@ -56,7 +50,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
if (funCount < 1) {
Ground.Flags.error = true;
Ground.Log.Error("extra ENDFUN instruction (expecting ENDSTRUCT before) in Ground.Program.Preprocess");
return newProgram;
return;
}
funCount--;
break;
@@ -69,7 +63,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
if (structCount < 1) {
Ground.Flags.error = true;
Ground.Log.Error("extra ENDSTRUCT instruction (expecting ENDFUN before) in Ground.Program.Preprocess");
return newProgram;
return;
}
structCount--;
break;
@@ -88,7 +82,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
// Preprocess function's body
GroundProgram functionBody = Ground.Program.preprocess(function.program.ground, function.closure);
if (Ground.Flags.error) {
return newProgram;
return;
}
Ground.Free.Program(function.program.ground);
@@ -98,7 +92,7 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
if (var == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
return newProgram;
return;
}
snprintf(var->name, 2047, "%s", id);
@@ -109,12 +103,100 @@ GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* stat
// Add instruction to attach closure to function
GroundInstruction inst = Ground.New.Instruction(GroundInstruction_FUN);
Ground.Instruction.append(&inst, Ground.New.Arg.FunctionRef(sig->args.at[0].as.ref));
Ground.Program.append(&newProgram, inst);
Ground.Program.append(newProgram, inst);
}
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);
// The aim is to compile an instruction which looks like this:
// struct &field OP $value &field OP $value &field OP $value...
// where:
// &field is the field to initialise
// OP is a numeric identifier:
// 0 -> none
// 1 -> init
// 2 -> set
// 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
GroundSize structCount = 0;
GroundInstruction output = Ground.New.Instruction(GroundInstruction_STRUCT);
// Parse struct body
bool parsing = true;
while (parsing) {
(*i)++;
if (*i >= program->len) {
Ground.Flags.error = true;
Ground.Log.Error("expecting ENDFUN instruction, reached end of program in Ground.Program.Preprocess");
return;
}
GroundInstruction* instruction = &program->at[*i];
switch (instruction->type) {
case GroundInstruction_SET: {
// 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, Ground.Copy.Arg(&instruction->args.at[1]));
break;
}
case GroundInstruction_INIT: {
// init &field -type -> &field 2 -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, Ground.Copy.Arg(&instruction->args.at[1]));
break;
}
case GroundInstruction_STRUCT: {
// TODO structs in structs
break;
}
case GroundInstruction_EXTERN: {
// TODO external functions as struct members
break;
}
case GroundInstruction_FUN: {
// Parse the function itself
doFunction(program, newProgram, state, i);
// Read and remove the emitted instruction
GroundInstruction* inst = &newProgram->at[newProgram->len - 1];
Ground.Instruction.append(&output, Ground.New.Arg.FunctionRef(Ground.Copy.Identifier(inst->args.at[0].as.ref)));
Ground.Free.Instruction(inst);
newProgram->len--;
// Add 3 to signal addition of closure
Ground.Instruction.append(&output, Ground.New.Arg.Value(Ground.New.Value.Int(3)));
break;
}
case GroundInstruction_ENDSTRUCT: {
parsing = false;
}
default: {
Ground.Log.Error("invalid instruction inside struct in ");
}
}
}
}
GroundProgram _GroundProgramPreprocess(GroundProgram* program, GroundState* state) {
GroundProgram newProgram = Ground.New.Program();
for (GroundSize i = 0; i < program->len; i++) {
switch (program->at[i].type) {
case GroundInstruction_FUN: {
doFunction(program, &newProgram, state, &i);
if (Ground.Flags.error) return newProgram;
break;
}
case GroundInstruction_STRUCT: {
// TODO: Preprocess structs
break;
}
default: {