From 100944cc1ecf28785e5007644f7a24e49267ba4e Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 17 Jan 2026 19:58:21 +1100 Subject: [PATCH] init instruction, mroe struct stuff --- src/interpreter.c | 105 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index e904467..be2b232 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -251,6 +251,19 @@ GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) { for (size_t i = 0; i < in->size; i++) { switch (in->instructions[i].type) { case SET: { + if (in->instructions[i].args.length < 2) { + runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &in->instructions[i], i + errorOffset); + } + if (in->instructions[i].args.length > 2) { + runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &in->instructions[i], i + errorOffset); + } + if (in->instructions[i].args.args[0].type != DIRREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", &in->instructions[i], i + errorOffset); + } + if (in->instructions[i].args.args[1].type != VALUE) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 2", &in->instructions[i], i + errorOffset); + } + addFieldToStruct(&gstruct, in->instructions[i].args.args[0].value.refName, in->instructions[i].args.args[1].value.value); break; } case INIT: { @@ -259,6 +272,9 @@ GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) { case FUN: { break; } + case ENDSTRUCT: { + break; + } default: { runtimeError(INVALID_INSTRUCTION, "Unsupported instruction while inside struct", &in->instructions[i], errorOffset + i); } @@ -334,7 +350,8 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) { 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; + char* name = malloc(strlen(in->instructions[i].args.args[0].value.refName) + 1); + strcpy(name, in->instructions[i].args.args[0].value.refName); i++; size_t counter = 1; @@ -354,7 +371,13 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) { i++; } - GroundStruct gs = parseStruct(&gp, errorOffset); + GroundValue gv = { + .type = STRUCTVAL, + .data.structVal = malloc(sizeof(GroundStruct)) + }; + *gv.data.structVal = parseStruct(&gp, errorOffset); + + addVariable(scope.variables, name, gv); } } for (size_t i = 0; i < in->size; i++) { @@ -1702,6 +1725,84 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop break; } + case INIT: { + if (in->args.length < 2) { + runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction); + } + if (in->args.length > 2) { + runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction); + } + if (in->args.args[0].type != DIRREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", in, currentInstruction); + } + if (in->args.args[1].type != TYPEREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a TypeRef for arg 2", in, currentInstruction); + } + + GroundValue gv; + + switch (stringToValueType(in->args.args[0].value.refName)) { + case INT: { + gv = createIntGroundValue(0); + break; + } + case DOUBLE: { + gv = createDoubleGroundValue(0); + break; + } + case STRING: { + gv = createStringGroundValue(""); + break; + } + case CHAR: { + gv = createCharGroundValue('\0'); + break; + } + case BOOL: { + gv = createBoolGroundValue(false); + break; + } + case LIST: { + gv = createListGroundValue(createList()); + break; + } + case FUNCTION: { + gv = createFunctionGroundValue(createGroundFunction()); + break; + } + case STRUCTVAL: { + gv.type = STRUCTVAL; + gv.data.structVal = malloc(sizeof(GroundStruct)); + *gv.data.structVal = createStruct(); + break; + } + case CUSTOM: { + GroundVariable* var = findVariable(*scope->variables, in->args.args[1].value.refName); + if (var == NULL) { + runtimeError(UNKNOWN_VARIABLE, "Couldn't find the specified type", in, currentInstruction); + } + if (var->value.type != STRUCTVAL) { + runtimeError(ARG_TYPE_MISMATCH, "TypeRef does not reference a struct", in, currentInstruction); + } + GroundStruct* gstruct = var->value.data.structVal; + gv.type = CUSTOM; + gv.data.customVal = malloc(sizeof(GroundObject)); + *gv.data.customVal = createObject(*gstruct); + break; + } + case NONE: { + gv.type = NONE; + break; + } + default: { + runtimeError(FIXME, "Reached should-be-impossible state", in, currentInstruction); + break; + } + } + + break; + } + case DROP: { if (in->args.length < 1) { runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);