Struct definitions #6

Merged
max merged 8 commits from unstable into master 2026-01-18 20:35:09 +11:00
2 changed files with 38 additions and 2 deletions
Showing only changes of commit c51bb82f62 - Show all commits

View File

@@ -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) {

View File

@@ -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);