Fix type safety issue

This commit is contained in:
2025-12-11 13:07:37 +11:00
parent 2ca3789024
commit 4c6aa97fd5
5 changed files with 54 additions and 10 deletions

View File

@@ -244,6 +244,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a TypeRef for arg 2", &in->instructions[i], i);
}
GroundArg* args = in->instructions[i].args.args;
function->returnType = stringToValueType(args[1].value.refName);
size_t length = in->instructions[i].args.length;
for (size_t j = 2; j < length; j += 2) {
if (args[j].type != TYPEREF) {
@@ -591,6 +592,9 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("custom"));
break;
}
case FUNCTION: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("function"));
}
case NONE: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("none"));
}
@@ -617,7 +621,6 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
} else {
addVariable(scope->variables, in->args.args[1].value.refName, createBoolGroundValue(false));
}
break;
}
case SETLIST: {
@@ -1318,16 +1321,13 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
}
case CALL: {
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);
runtimeError(TOO_FEW_ARGS, "Expecting 2 or more args", in, currentInstruction);
}
if (in->args.args[0].type != FNREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a FunctionRef for arg 1", in, currentInstruction);
}
if (in->args.args[1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
if (in->args.args[in->args.length - 1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef as the last arg", in, currentInstruction);
}
GroundVariable* variables = NULL;
GroundLabel* labels = NULL;
@@ -1343,13 +1343,28 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(UNKNOWN_VARIABLE, "Provided reference does not reference a function", in, currentInstruction);
}
GroundFunction* function = value->data.fnVal;
if (function->argSize < in->args.length - 2) {
runtimeError(TOO_FEW_ARGS, "Incorrect amount of arguments provided for function", in, currentInstruction);
}
if (function->argSize > in->args.length - 2) {
runtimeError(TOO_MANY_ARGS, "Incorrect amount of arguments provided for function", in, currentInstruction);
}
for (size_t i = 0; i < function->argSize; i++) {
if (in->args.args[i + 1].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction);
}
if (in->args.args[i + 1].value.value.type != function->args[i].type) {
runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction);
}
addVariable(newScope.variables, function->args[i].name, in->args.args[i + 1].value.value);
}
size_t currentCurrentInstruction = currentInstruction;
currentInstruction = function->startLine;
GroundValue returnValue = interpretGroundProgram(&function->program, &newScope);
if (returnValue.type != function->returnType) {
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from function", in, currentInstruction);
}
addVariable(scope->variables, in->args.args[1].value.refName, returnValue);
addVariable(scope->variables, in->args.args[in->args.length - 1].value.refName, returnValue);
currentInstruction = currentCurrentInstruction;
break;
}