Lambdas now work as expected

This commit is contained in:
2026-02-28 17:16:51 +11:00
parent 3a41b7a9cb
commit 3a3e25c6c7

View File

@@ -692,6 +692,8 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
char* lambdaId = malloc(sizeof(char) * 64); char* lambdaId = malloc(sizeof(char) * 64);
snprintf(lambdaId, 64, "__SOLS_LAMBDA_%zu", scope->tmpCounter++); snprintf(lambdaId, 64, "__SOLS_LAMBDA_%zu", scope->tmpCounter++);
node->accessArg = groundCreateReference(VALREF, lambdaId);
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, lambdaId)); groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, lambdaId));
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType); ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType);
if (arg.error) { if (arg.error) {
@@ -712,8 +714,19 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
groundAddInstructionToProgram(&gp, signature); groundAddInstructionToProgram(&gp, signature);
// Create a scope for lambda arguments
// Lambdas do NOT have access to external state
SolsScope lambdaScope = {
.variables = NULL,
.tmpCounter = 0
};
for (size_t i = 0; i < node->as.type.children.count; i++) {
addVariableToScope(&lambdaScope, node->as.type.children.at[i].name, node->as.type.children.at[i].type);
}
// Generate children and add then to this program // Generate children and add then to this program
ResultType(GroundProgram, charptr) bodyCode = generateCode(&node->children.at[0], scope); ResultType(GroundProgram, charptr) bodyCode = generateCode(&node->children.at[0], &lambdaScope);
if (bodyCode.error) return bodyCode; if (bodyCode.error) return bodyCode;
for (size_t i = 0; i < bodyCode.as.success.size; i++) { for (size_t i = 0; i < bodyCode.as.success.size; i++) {
groundAddInstructionToProgram(&gp, bodyCode.as.success.instructions[i]); groundAddInstructionToProgram(&gp, bodyCode.as.success.instructions[i]);