Struct init

This commit is contained in:
2026-01-17 20:04:18 +11:00
parent 100944cc1e
commit 1c5300d27e
2 changed files with 78 additions and 2 deletions

View File

@@ -246,7 +246,7 @@ void groundAddNativeFunction(GroundScope* scope, char* name, NativeGroundFunctio
addVariable(scope->variables, name, createFunctionGroundValue(gf)); addVariable(scope->variables, name, createFunctionGroundValue(gf));
} }
GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) { GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffset) {
GroundStruct gstruct = createStruct(); GroundStruct gstruct = createStruct();
for (size_t i = 0; i < in->size; i++) { for (size_t i = 0; i < in->size; i++) {
switch (in->instructions[i].type) { switch (in->instructions[i].type) {
@@ -267,6 +267,81 @@ GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) {
break; break;
} }
case INIT: { case INIT: {
if (in->instructions[i].args.length < 2) {
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", &in->instructions[i], currentInstruction);
}
if (in->instructions[i].args.length > 2) {
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", &in->instructions[i], currentInstruction);
}
if (in->instructions[i].args.args[0].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", &in->instructions[i], currentInstruction);
}
if (in->instructions[i].args.args[1].type != TYPEREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a TypeRef for arg 2", &in->instructions[i], currentInstruction);
}
GroundValue gv;
switch (stringToValueType(in->instructions[i].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->instructions[i].args.args[1].value.refName);
if (var == NULL) {
runtimeError(UNKNOWN_VARIABLE, "Couldn't find the specified type", &in->instructions[i], currentInstruction);
}
if (var->value.type != STRUCTVAL) {
runtimeError(ARG_TYPE_MISMATCH, "TypeRef does not reference a struct", &in->instructions[i], 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->instructions[i], currentInstruction);
break;
}
}
addFieldToStruct(&gstruct, in->instructions[i].args.args[0].value.refName, gv);
break; break;
} }
case FUN: { case FUN: {
@@ -1799,6 +1874,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
break; break;
} }
} }
addVariable(scope->variables, in->args.args[0].value.refName, gv);
break; break;
} }

View File

@@ -36,7 +36,7 @@ typedef struct GroundDebugInstruction {
char* arg; char* arg;
} GroundDebugInstruction; } GroundDebugInstruction;
GroundStruct parseStruct(GroundProgram* in, size_t errorOffset); GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffset);
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope); GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope);
GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope); GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scope);