use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateSimpleIteratorBlock.
private void generateSimpleIteratorBlock(IterableContext ctx, BLangFunction funcNode) {
final Operation firstOperation = ctx.getFirstOperation();
final DiagnosticPos pos = firstOperation.pos;
// return result;
if (isReturningIteratorFunction(ctx)) {
createCounterVarDefStmt(funcNode, ctx);
createResultVarDefStmt(funcNode, ctx);
}
// Create variables required.
final List<BLangVariable> foreachVariables = createForeachVariables(ctx, ctx.getFirstOperation().argVar, funcNode);
ctx.iteratorResultVariables = foreachVariables;
final BLangForeach foreachStmt = ASTBuilderUtil.createForeach(pos, funcNode.body, ASTBuilderUtil.createVariableRef(pos, ctx.collectionVar.symbol), ASTBuilderUtil.createVariableRefList(pos, foreachVariables), ctx.foreachTypes);
if (isReturningIteratorFunction(ctx)) {
generateAggregator(foreachStmt.body, ctx);
generateFinalResult(funcNode.body, ctx);
}
final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(firstOperation.pos, funcNode.body);
if (isReturningIteratorFunction(ctx)) {
returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class IterableCodeDesugar method createCounterVarDefStmt.
/**
* Generates following.
*
* int count = 0;
*
* @param funcNode functionNode
* @param ctx current context
*/
private void createCounterVarDefStmt(BLangFunction funcNode, IterableContext ctx) {
BLangBlockStmt blockStmt = funcNode.body;
final DiagnosticPos pos = blockStmt.pos;
ctx.countVar = ASTBuilderUtil.createVariable(pos, VAR_COUNT, symTable.intType);
ctx.countVar.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
defineVariable(ctx.countVar, funcNode.symbol.pkgID, funcNode);
final BLangVariableDef variableDefStmt = ASTBuilderUtil.createVariableDefStmt(pos, blockStmt);
variableDefStmt.var = ctx.countVar;
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class IterableCodeDesugar method createResultVarDefStmt.
/**
* Generates following.
*
* array: result =[];
* map: result = {};
*
* @param funcNode functionNode
* @param ctx current context
*/
private void createResultVarDefStmt(BLangFunction funcNode, IterableContext ctx) {
BLangBlockStmt blockStmt = funcNode.body;
final IterableKind kind = ctx.getLastOperation().kind;
if (ctx.resultType.tag != TypeTags.ARRAY && ctx.resultType.tag != TypeTags.MAP && ctx.resultType.tag != TypeTags.TABLE && kind != IterableKind.MAX && kind != IterableKind.MIN) {
return;
}
final DiagnosticPos pos = blockStmt.pos;
final BLangVariableDef defStmt = ASTBuilderUtil.createVariableDefStmt(pos, blockStmt);
defStmt.var = ctx.resultVar;
switch(ctx.resultType.tag) {
case TypeTags.ARRAY:
final BLangArrayLiteral arrayInit = (BLangArrayLiteral) TreeBuilder.createArrayLiteralNode();
arrayInit.pos = pos;
arrayInit.exprs = new ArrayList<>();
arrayInit.type = ctx.resultType;
defStmt.var.expr = arrayInit;
break;
case TypeTags.MAP:
defStmt.var.expr = ASTBuilderUtil.createEmptyRecordLiteral(pos, ctx.resultType);
break;
case TypeTags.TABLE:
BLangVariable retVars = ctx.getFirstOperation().retVar;
BType tableType = new BTableType(TypeTags.TABLE, retVars.type, symTable.tableType.tsymbol);
defStmt.var.expr = ASTBuilderUtil.createEmptyRecordLiteral(pos, tableType);
break;
case TypeTags.INT:
if (kind == IterableKind.MAX) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, Long.MIN_VALUE);
} else if (kind == IterableKind.MIN) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, Long.MAX_VALUE);
}
break;
case TypeTags.FLOAT:
if (kind == IterableKind.MAX) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, Double.MIN_NORMAL);
} else if (kind == IterableKind.MIN) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, Double.MAX_VALUE);
}
break;
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class BLangPackageBuilder method startLambdaFunctionDef.
public void startLambdaFunctionDef(PackageID pkgID) {
startFunctionDef();
BLangFunction lambdaFunction = (BLangFunction) this.invokableNodeStack.peek();
lambdaFunction.setName(createIdentifier(anonymousModelHelper.getNextAnonymousFunctionKey(pkgID)));
lambdaFunction.addFlag(Flag.LAMBDA);
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addLambdaFunctionDef.
public void addLambdaFunctionDef(DiagnosticPos pos, Set<Whitespace> ws, boolean paramsAvail, boolean retParamsAvail, boolean restParamAvail) {
BLangFunction lambdaFunction = (BLangFunction) this.invokableNodeStack.peek();
lambdaFunction.pos = pos;
endCallableUnitSignature(ws, lambdaFunction.getName().value, paramsAvail, retParamsAvail, restParamAvail);
BLangLambdaFunction lambdaExpr = (BLangLambdaFunction) TreeBuilder.createLambdaFunctionNode();
lambdaExpr.function = lambdaFunction;
lambdaExpr.pos = pos;
addExpressionNode(lambdaExpr);
// TODO: is null correct here
endFunctionDef(pos, null, false, false, true, false);
}
Aggregations