Lambdas now work as expected
This commit is contained in:
@@ -692,6 +692,8 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
|
||||
char* lambdaId = malloc(sizeof(char) * 64);
|
||||
snprintf(lambdaId, 64, "__SOLS_LAMBDA_%zu", scope->tmpCounter++);
|
||||
|
||||
node->accessArg = groundCreateReference(VALREF, lambdaId);
|
||||
|
||||
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, lambdaId));
|
||||
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType);
|
||||
if (arg.error) {
|
||||
@@ -712,8 +714,19 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
|
||||
|
||||
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
|
||||
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;
|
||||
for (size_t i = 0; i < bodyCode.as.success.size; i++) {
|
||||
groundAddInstructionToProgram(&gp, bodyCode.as.success.instructions[i]);
|
||||
|
||||
Reference in New Issue
Block a user