From c51bb82f621126438afc5d45544209ecc6d74607 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Fri, 16 Jan 2026 17:45:48 +1100 Subject: [PATCH] Continue struct parsing work --- src/interpreter.c | 37 ++++++++++++++++++++++++++++++++++++- src/interpreter.h | 3 ++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index f08d3af..2548776 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -234,6 +234,11 @@ void groundAddNativeFunction(GroundScope* scope, char* name, NativeGroundFunctio addVariable(scope->variables, name, createFunctionGroundValue(gf)); } +GroundStruct parseStruct(GroundProgram* in) { + GroundStruct gstruct = createStruct(); + return gstruct; +} + GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) { GroundLabel* labels = NULL; GroundVariable* variables = NULL; @@ -246,7 +251,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) { scope.labels = &labels; scope.variables = &variables; } - // Preprocess all labels and functions + // Preprocess all labels, structs and functions for (int i = 0; i < in->size; i++) { if (in->instructions[i].type == CREATELABEL) { 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)); } + 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++) { if (in->instructions[i].type == FUN) { diff --git a/src/interpreter.h b/src/interpreter.h index b3a3db6..fd6f990 100644 --- a/src/interpreter.h +++ b/src/interpreter.h @@ -7,7 +7,7 @@ #include "include/uthash.h" 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; typedef enum GroundDebugInstructionType { @@ -36,6 +36,7 @@ typedef struct GroundDebugInstruction { char* arg; } GroundDebugInstruction; +GroundStruct parseStruct(GroundProgram* in); GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope); GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope);