Function calling

This commit is contained in:
2025-12-06 11:50:42 +11:00
parent 571d3bcc34
commit 9553934db5
6 changed files with 100 additions and 39 deletions

View File

@@ -107,11 +107,11 @@ void addVariable(GroundVariable **head, const char *id, GroundValue data) {
HASH_ADD_STR(*head, id, item);
}
GroundFunction createGroundFunction() {
GroundFunction gf;
gf.argSize = 0;
gf.args = malloc(sizeof(GroundFunctionArgs));
gf.program = createGroundProgram();
GroundFunction* createGroundFunction() {
GroundFunction* gf = malloc(sizeof(GroundFunction));
gf->argSize = 0;
gf->args = malloc(sizeof(GroundFunctionArgs));
gf->program = createGroundProgram();
return gf;
}
@@ -155,7 +155,6 @@ GroundValueType stringToValueType(char* in) {
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
GroundLabel* labels = NULL;
GroundVariable* variables = NULL;
GroundFunctionWrapper* functions = NULL;
GroundScope scope;
if (inScope != NULL) {
@@ -163,7 +162,6 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
} else {
scope.labels = &labels;
scope.variables = &variables;
scope.functions = &functions;
}
// Preprocess all labels
for (int i = 0; i < in->size; i++) {
@@ -171,21 +169,20 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
addLabel(scope.labels, in->instructions[i].args.args[0].value.refName, i);
}
if (in->instructions[i].type == FUN) {
char* functionName;
GroundFunction function = createGroundFunction();
GroundFunction* function = createGroundFunction();
if (in->instructions[i].args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", &in->instructions[i], i);
}
if (in->instructions[i].args.args[0].type != FNREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a FunctionRef for arg 1", &in->instructions[i], i);
}
char* functionName = in->instructions[i].args.args[0].value.refName;
if (in->instructions[i].args.length < 2) {
function.returnType = NONE;
function->returnType = NONE;
} else {
if (in->instructions[i].args.args[1].type != TYPEREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a TypeRef for arg 2", &in->instructions[i], i);
}
functionName = in->instructions[i].args.args[0].value.refName;
GroundArg* args = in->instructions[i].args.args;
size_t length = in->instructions[i].args.length;
for (size_t j = 2; j < length; j += 2) {
@@ -198,14 +195,19 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
if (args[j + 1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef after a TypeRef", &in->instructions[i], i);
}
addArgsToGroundFunction(&function, stringToValueType(args[j].value.refName), args[j + 1].value.refName);
addArgsToGroundFunction(function, stringToValueType(args[j].value.refName), args[j + 1].value.refName);
}
}
i++;
while (in->instructions[i].type != ENDFUN) {
addInstructionToProgram(&function.program, in->instructions[i]);
addInstructionToProgram(&function->program, in->instructions[i]);
i++;
}
if (&function->program.size == 0) {
addInstructionToProgram(&function->program, createGroundInstruction(PRINT));
}
addVariable(scope.variables, functionName, createFunctionGroundValue(function));
}
}
while (currentInstruction < in->size) {
@@ -1115,7 +1117,49 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
}
break;
}
/*
* FUNCTIONS
* Allows execution of functions in Ground.
* Instructions:
* fun, return, endfun, pusharg, call
*/
case RETURN: {
break;
}
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);
}
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);
}
GroundVariable* variables = NULL;
GroundLabel* labels = NULL;
GroundScope newScope;
newScope.variables = &variables;
newScope.labels = &labels;
GroundVariable* var = findVariable(*scope->variables, in->args.args[0].value.refName);
if (var == NULL) {
runtimeError(UNKNOWN_VARIABLE, "Function not found", in, currentInstruction);
}
GroundValue* value = &var->value;
if (value->type != FUNCTION) {
runtimeError(UNKNOWN_VARIABLE, "Provided reference does not reference a function", in, currentInstruction);
}
GroundFunction* function = value->data.fnVal;
GroundValue returnValue = interpretGroundProgram(&function->program, &newScope);
if (returnValue.type != function->returnType) {
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from function", in, currentInstruction);
}
}
}
freeGroundInstruction(in);
return createIntGroundValue(0);
}