Function definitions
This commit is contained in:
@@ -738,13 +738,61 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
|
||||
return Success(GroundProgram, charptr, gp);
|
||||
}
|
||||
|
||||
ResultType(GroundProgram, charptr) generateDefNode(SolsNode* node, SolsScope* scope) {
|
||||
GroundProgram gp = groundCreateProgram();
|
||||
|
||||
// Generate function signature
|
||||
GroundInstruction signature = groundCreateInstruction(FUN);
|
||||
|
||||
node->accessArg = groundCreateReference(VALREF, node->children.at[0].as.idName);
|
||||
|
||||
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, node->children.at[0].as.idName));
|
||||
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType);
|
||||
if (arg.error) {
|
||||
return Error(GroundProgram, charptr, arg.as.error);
|
||||
}
|
||||
groundAddReferenceToInstruction(&signature, arg.as.success);
|
||||
for (size_t i = 0; i < node->as.type.children.count; i++) {
|
||||
// Add type
|
||||
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(&node->as.type.children.at[i].type);
|
||||
if (arg.error) {
|
||||
return Error(GroundProgram, charptr, arg.as.error);
|
||||
}
|
||||
groundAddReferenceToInstruction(&signature, arg.as.success);
|
||||
|
||||
// Add arg name
|
||||
groundAddReferenceToInstruction(&signature, groundCreateReference(DIRREF, node->as.type.children.at[i].name));
|
||||
}
|
||||
|
||||
groundAddInstructionToProgram(&gp, signature);
|
||||
|
||||
// Create a scope for function arguments
|
||||
SolsScope functionScope = copySolsScope(scope);
|
||||
|
||||
for (size_t i = 0; i < node->as.type.children.count; i++) {
|
||||
addVariableToScope(&functionScope, 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[1], &functionScope);
|
||||
if (bodyCode.error) return bodyCode;
|
||||
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));
|
||||
|
||||
return Success(GroundProgram, charptr, gp);
|
||||
}
|
||||
|
||||
ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope) {
|
||||
|
||||
GroundProgram program = groundCreateProgram();
|
||||
|
||||
SolsScope backupScope = {NULL, 0};
|
||||
|
||||
if (node->type != SNT_IF && node->type != SNT_WHILE && node->type != SNT_LAMBDA) {
|
||||
if (node->type != SNT_IF && node->type != SNT_WHILE && node->type != SNT_LAMBDA && node->type != SNT_DEF) {
|
||||
if (node->type == SNT_CODE_BLOCK) {
|
||||
backupScope = *scope;
|
||||
SolsScope newScope = copySolsScope(scope);
|
||||
@@ -785,6 +833,7 @@ ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope
|
||||
case SNT_IF: generate(If);
|
||||
case SNT_WHILE: generate(While);
|
||||
case SNT_LAMBDA: generate(Lambda);
|
||||
case SNT_DEF: generate(Def);
|
||||
}
|
||||
return Success(GroundProgram, charptr, program);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user