Struct definitions #6
@@ -251,6 +251,19 @@ GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) {
|
|||||||
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) {
|
||||||
case SET: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case INIT: {
|
case INIT: {
|
||||||
@@ -259,6 +272,9 @@ GroundStruct parseStruct(GroundProgram* in, size_t errorOffset) {
|
|||||||
case FUN: {
|
case FUN: {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case ENDSTRUCT: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
runtimeError(INVALID_INSTRUCTION, "Unsupported instruction while inside struct", &in->instructions[i], errorOffset + i);
|
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) {
|
if (in->instructions[i].args.args[0].type != TYPEREF) {
|
||||||
runtimeError(ARG_TYPE_MISMATCH, "Expected arg 1 to be a typeref", &in->instructions[i], i);
|
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++;
|
i++;
|
||||||
|
|
||||||
size_t counter = 1;
|
size_t counter = 1;
|
||||||
@@ -354,7 +371,13 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
|
|||||||
i++;
|
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++) {
|
for (size_t i = 0; i < in->size; i++) {
|
||||||
@@ -1702,6 +1725,84 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
|||||||
break;
|
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: {
|
case DROP: {
|
||||||
if (in->args.length < 1) {
|
if (in->args.length < 1) {
|
||||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);
|
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);
|
||||||
|
|||||||
Reference in New Issue
Block a user