Files
ground-rewrite/src/Program/preprocess.c

284 lines
10 KiB
C
Raw Normal View History

2026-07-02 11:35:56 +10:00
#include "../../include/ground.h"
2026-07-05 15:08:35 +10:00
static void doFunction(GroundProgram* program, GroundProgram* newProgram, GroundState* state, GroundSize* i) {
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
GroundFunction function = Ground.New.Function(state);
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
// Parse signature
GroundInstruction* sig = &program->at[*i];
char* id = sig->args.at[0].as.ref->string;
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
for (GroundSize j = 1; j < sig->args.len; j++) {
GroundArg* arg = &sig->args.at[j];
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
// Ignore type annotations, this can be taken care of in another function
if (arg->type == GroundArg_TypeRef) {
continue;
}
char* argId = arg->as.ref->string;
Ground.Function.appendArg(&function, argId);
GroundVariable* argVar = malloc(sizeof(GroundVariable));
if (argVar == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
return;
}
snprintf(argVar->name, 2047, "%s", argId);
argVar->value = Ground.New.Value.Int(0);
HASH_ADD_STR(function.closure->variables, name, argVar);
}
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
// Add instructions to function
GroundSize funCount = 1;
GroundSize structCount = 0;
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
for (;;) {
(*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* inst = &program->at[*i];
switch (inst->type) {
case GroundInstruction_FUN: {
funCount++;
break;
}
case GroundInstruction_ENDFUN: {
if (funCount < 1) {
2026-07-02 11:35:56 +10:00
Ground.Flags.error = true;
2026-07-05 15:08:35 +10:00
Ground.Log.Error("extra ENDFUN instruction (expecting ENDSTRUCT before) in Ground.Program.Preprocess");
return;
2026-07-02 11:35:56 +10:00
}
2026-07-05 15:08:35 +10:00
funCount--;
break;
}
case GroundInstruction_STRUCT: {
structCount++;
break;
}
case GroundInstruction_ENDSTRUCT: {
if (structCount < 1) {
Ground.Flags.error = true;
Ground.Log.Error("extra ENDSTRUCT instruction (expecting ENDFUN before) in Ground.Program.Preprocess");
return;
}
structCount--;
break;
}
default: break;
}
if (funCount == 0 && structCount == 0) {
// We done
break;
}
Ground.Function.appendInstruction(&function, *inst);
}
// Preprocess function's body
GroundProgram functionBody = Ground.Program.preprocess(function.program.ground, function.closure);
if (Ground.Flags.error) {
return;
}
Ground.Free.Program(function.program.ground);
*function.program.ground = functionBody;
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
GroundVariable* var = malloc(sizeof(GroundVariable));
if (var == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Program.Preprocess");
return;
}
snprintf(var->name, 2047, "%s", id);
var->value = Ground.New.Value.Function(function);
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
HASH_ADD_STR(state->variables, name, var);
2026-07-02 11:35:56 +10:00
2026-07-05 15:08:35 +10:00
// 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);
}
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);
2026-07-05 18:01:09 +10:00
GroundStruct gs = Ground.New.Struct();
2026-07-05 15:08:35 +10:00
// 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
2026-07-05 18:01:09 +10:00
//
// Oh yeah, we also need to make a GroundStruct value
2026-07-05 15:08:35 +10:00
GroundSize structCount = 0;
GroundInstruction output = Ground.New.Instruction(GroundInstruction_STRUCT);
2026-07-05 18:01:09 +10:00
Ground.Instruction.append(&output, Ground.New.Arg.TypeRef(structName));
2026-07-05 15:08:35 +10:00
// 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;
}
2026-07-03 16:51:04 +10:00
2026-07-05 15:08:35 +10:00
GroundInstruction* instruction = &program->at[*i];
switch (instruction->type) {
case GroundInstruction_SET: {
2026-07-05 18:01:09 +10:00
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);
2026-07-05 15:08:35 +10:00
// set &field $value -> &field 2 $value
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[0]));
2026-07-05 18:01:09 +10:00
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 2});
2026-07-05 15:08:35 +10:00
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[1]));
break;
}
case GroundInstruction_INIT: {
2026-07-05 18:01:09 +10:00
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
2026-07-05 15:08:35 +10:00
Ground.Instruction.append(&output, Ground.Copy.Arg(&instruction->args.at[0]));
2026-07-05 18:01:09 +10:00
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1});
2026-07-05 15:08:35 +10:00
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
2026-07-05 18:01:09 +10:00
Ground.Instruction.append(&output, (GroundArg) {.type = GroundArg_DirectRef, ._offset = 1});
2026-07-05 15:08:35 +10:00
break;
}
case GroundInstruction_ENDSTRUCT: {
parsing = false;
}
default: {
Ground.Log.Error("invalid instruction inside struct in ");
}
}
}
2026-07-05 18:01:09 +10:00
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);
2026-07-05 15:08:35 +10:00
}
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;
2026-07-02 11:35:56 +10:00
break;
}
case GroundInstruction_STRUCT: {
2026-07-05 18:01:09 +10:00
doStruct(program, &newProgram, state, &i);
if (Ground.Flags.error) return newProgram;
2026-07-02 11:35:56 +10:00
break;
}
default: {
Ground.Program.append(&newProgram, program->at[i]);
}
}
}
return newProgram;
}