use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCalculateAverage.
/**
* Generates following.
*
* result = result / count
*
* @param blockStmt target
* @param ctx current context
*/
private void generateCalculateAverage(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr divide = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
divide.pos = pos;
divide.type = ctx.resultVar.symbol.type;
divide.opKind = OperatorKind.ADD;
divide.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
divide.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
divide.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.DIV, divide.type, ctx.countVar.symbol.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = divide;
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext 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.semantics.model.iterable.IterableContext 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.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateSumAggregator.
/**
* Generates following.
*
* result = result + value
*
* @param blockStmt target
* @param ctx current context
*/
private void generateSumAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
add.pos = pos;
add.type = ctx.resultVar.symbol.type;
add.opKind = OperatorKind.ADD;
add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
add.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, add.type, add.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = add;
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCompareAggregator.
/**
* Generates following.
*
* result = result (Operator) value ? result : value
*
* @param blockStmt target
* @param ctx current context
* @param operator compare operator
*/
private void generateCompareAggregator(BLangBlockStmt blockStmt, IterableContext ctx, OperatorKind operator) {
final DiagnosticPos pos = blockStmt.pos;
final BLangSimpleVarRef resultVar = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
final BLangSimpleVarRef valueVar = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
final BLangBinaryExpr compare = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
compare.pos = pos;
compare.type = symTable.booleanType;
compare.opKind = operator;
compare.lhsExpr = resultVar;
compare.rhsExpr = valueVar;
compare.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(operator, resultVar.symbol.type, valueVar.symbol.type);
final BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
ternaryExpr.pos = pos;
ternaryExpr.expr = compare;
ternaryExpr.thenExpr = resultVar;
ternaryExpr.elseExpr = valueVar;
ternaryExpr.type = compare.type;
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(resultVar);
countAdd.expr = ternaryExpr;
}
Aggregations