Continue struct parsing work

This commit is contained in:
2026-01-16 17:45:48 +11:00
parent 18579830ab
commit c51bb82f62
2 changed files with 38 additions and 2 deletions

View File

@@ -234,6 +234,11 @@ void groundAddNativeFunction(GroundScope* scope, char* name, NativeGroundFunctio
addVariable(scope->variables, name, createFunctionGroundValue(gf)); addVariable(scope->variables, name, createFunctionGroundValue(gf));
} }
GroundStruct parseStruct(GroundProgram* in) {
GroundStruct gstruct = createStruct();
return gstruct;
}
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) { GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
GroundLabel* labels = NULL; GroundLabel* labels = NULL;
GroundVariable* variables = NULL; GroundVariable* variables = NULL;
@@ -246,7 +251,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
scope.labels = &labels; scope.labels = &labels;
scope.variables = &variables; scope.variables = &variables;
} }
// Preprocess all labels and functions // Preprocess all labels, structs and functions
for (int i = 0; i < in->size; i++) { for (int i = 0; i < in->size; i++) {
if (in->instructions[i].type == CREATELABEL) { if (in->instructions[i].type == CREATELABEL) {
addLabel(scope.labels, in->instructions[i].args.args[0].value.refName, i); addLabel(scope.labels, in->instructions[i].args.args[0].value.refName, i);
@@ -290,6 +295,36 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
} }
addVariable(scope.variables, functionName, createFunctionGroundValue(function)); addVariable(scope.variables, functionName, createFunctionGroundValue(function));
} }
if (in->instructions[i].type == STRUCT) {
if (in->instructions[i].args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", &in->instructions[i], i);
}
if (in->instructions[i].args.length > 1) {
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg", &in->instructions[i], i);
}
if (in->instructions[i].args.args[0].type != TYPEREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expected arg 1 to be a typeref", &in->instructions[i], i);
}
char* name = in->instructions[i].args.args[0].value.refName;
i++;
size_t counter = 1;
GroundProgram gp;
while (counter > 0) {
if (i >= in->size) {
runtimeError(PREMATURE_EOF, "Reached end of scope before struct definition ended", &in->instructions[i - 1], i - 1);
}
if (in->instructions[i].type == STRUCT) {
counter++;
}
if (in->instructions[i].type == ENDSTRUCT) {
counter--;
}
addInstructionToProgram(&gp, in->instructions[i]);
}
GroundStruct gs = parseStruct(&gp);
}
} }
for (int i = 0; i < in->size; i++) { for (int i = 0; i < in->size; i++) {
if (in->instructions[i].type == FUN) { if (in->instructions[i].type == FUN) {

View File

@@ -7,7 +7,7 @@
#include "include/uthash.h" #include "include/uthash.h"
typedef enum GroundRuntimeError { typedef enum GroundRuntimeError {
ARG_TYPE_MISMATCH, TOO_FEW_ARGS, TOO_MANY_ARGS, UNKNOWN_LABEL, UNKNOWN_VARIABLE, LIST_ERROR, STRING_ERROR, MATH_ERROR, RETURN_TYPE_MISMATCH, FIXME ARG_TYPE_MISMATCH, TOO_FEW_ARGS, TOO_MANY_ARGS, UNKNOWN_LABEL, UNKNOWN_VARIABLE, LIST_ERROR, STRING_ERROR, MATH_ERROR, RETURN_TYPE_MISMATCH, PREMATURE_EOF, FIXME
} GroundRuntimeError; } GroundRuntimeError;
typedef enum GroundDebugInstructionType { typedef enum GroundDebugInstructionType {
@@ -36,6 +36,7 @@ typedef struct GroundDebugInstruction {
char* arg; char* arg;
} GroundDebugInstruction; } GroundDebugInstruction;
GroundStruct parseStruct(GroundProgram* in);
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope); GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope);
GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope); GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope);