Function calling now works, only need return

This commit is contained in:
2026-03-01 13:39:27 +11:00
parent dc09883e46
commit bf71a0dfac
3 changed files with 59 additions and 27 deletions

View File

@@ -756,12 +756,31 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
ResultType(GroundProgram, charptr) generateDefNode(SolsNode* node, SolsScope* scope) {
GroundProgram gp = groundCreateProgram();
// Register the function in the current scope so calls can resolve it
// node->children.at[0] is the identifier node for the function name
if (node->children.count < 2 || node->children.at[0].type != SNT_IDENTIFIER) {
return Error(GroundProgram, charptr, "Invalid def node shape (expected name + body)");
}
char* fnName = node->children.at[0].as.idName;
SolsVariable* existing = findSolsVariable(scope, fnName);
if (existing == NULL) {
addVariableToScope(scope, fnName, node->as.type);
} else {
if (existing->typeinfo.type != STT_FUN) {
return Error(GroundProgram, charptr, "A non-function variable already exists with this name");
}
if (compareTypes(&existing->typeinfo, &node->as.type) == false) {
return Error(GroundProgram, charptr, "Function already exists with a different type signature");
}
}
// Generate function signature
GroundInstruction signature = groundCreateInstruction(FUN);
node->accessArg = groundCreateReference(VALREF, node->children.at[0].as.idName);
node->accessArg = groundCreateReference(VALREF, fnName);
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, node->children.at[0].as.idName));
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, fnName));
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType);
if (arg.error) {
return Error(GroundProgram, charptr, arg.as.error);
@@ -794,7 +813,7 @@ ResultType(GroundProgram, charptr) generateDefNode(SolsNode* node, SolsScope* sc
for (size_t i = 0; i < bodyCode.as.success.size; i++) {
groundAddInstructionToProgram(&gp, bodyCode.as.success.instructions[i]);
}
// End the function
groundAddInstructionToProgram(&gp, groundCreateInstruction(ENDFUN));