Search in sources :

Example 6 with IterableContext

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 7 with IterableContext

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));
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 8 with IterableContext

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)

Example 9 with IterableContext

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 10 with IterableContext

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Aggregations

DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)12 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)8 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)8 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)5 BTupleType (org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)5 ArrayList (java.util.ArrayList)4 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)4 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)4 IterableKind (org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind)3 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)3 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)3 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)3 HashSet (java.util.HashSet)2 IterableContext (org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext)2 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)2 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)2 BLangBracedOrTupleExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)2 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)2