Search in sources :

Example 16 with IterableContext

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.

the class IterableCodeDesugar method defineRequiredVariables.

private void defineRequiredVariables(IterableContext ctx, LinkedList<Operation> streamOperations, List<BLangVariable> foreachVariables, BLangFunction funcNode) {
    Set<BLangVariable> notDefinedVars = new HashSet<>();
    streamOperations.forEach(operation -> {
        notDefinedVars.add(operation.argVar);
        if (operation.kind != IterableKind.FILTER && operation.retVar != null) {
            notDefinedVars.add(operation.retVar);
        }
    });
    notDefinedVars.addAll(ctx.iteratorResultVariables);
    notDefinedVars.removeAll(foreachVariables);
    notDefinedVars.forEach(var -> defineVariable(var, ctx.env.enclPkg.symbol.pkgID, funcNode));
    notDefinedVars.forEach(var -> {
        BLangVariableDef variableDefStmt = ASTBuilderUtil.createVariableDefStmt(funcNode.pos, funcNode.body);
        variableDefStmt.var = var;
    });
}
Also used : BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) HashSet(java.util.HashSet)

Example 17 with IterableContext

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateArrayAggregator.

/**
 * Generates following.
 *
 * result[count] = value;
 * count = count + 1;
 *
 * @param blockStmt target
 * @param ctx       current context
 */
private void generateArrayAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
    final DiagnosticPos pos = blockStmt.pos;
    // create assignment result[count] = value;
    final BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
    indexAccessNode.type = ctx.iteratorResultVariables.get(0).symbol.type;
    final BLangAssignment valueAssign = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    valueAssign.varRefs.add(indexAccessNode);
    valueAssign.expr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
    // create count = count + 1;
    generateCountAggregator(blockStmt, ctx.countVar);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 18 with IterableContext

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateStreamingIteratorBlock.

private void generateStreamingIteratorBlock(IterableContext ctx, BLangFunction funcNode, LinkedList<Operation> streamOperations) {
    final Operation firstOperation = ctx.getFirstOperation();
    final DiagnosticPos pos = firstOperation.pos;
    // Generate streaming based function Body.
    if (isReturningIteratorFunction(ctx)) {
        if (ctx.resultType.tag != TypeTags.TABLE) {
            // This should be the select operation. No need of a count variable.
            createCounterVarDefStmt(funcNode, ctx);
        }
        createResultVarDefStmt(funcNode, ctx);
        ctx.iteratorResultVariables = createIteratorResultVariables(ctx, streamOperations.getLast().retVar, funcNode);
    } else {
        ctx.iteratorResultVariables = Collections.emptyList();
    }
    // Create variables required.
    final List<BLangVariable> foreachVariables = createForeachVariables(ctx, ctx.getFirstOperation().argVar, funcNode);
    // Define all undefined variables.
    defineRequiredVariables(ctx, streamOperations, foreachVariables, funcNode);
    // Generate foreach iteration.
    final BLangForeach foreachStmt = ASTBuilderUtil.createForeach(pos, funcNode.body, ASTBuilderUtil.createVariableRef(pos, ctx.collectionVar.symbol), ASTBuilderUtil.createVariableRefList(pos, foreachVariables), ctx.foreachTypes);
    if (foreachVariables.size() > 1) {
        // Create tuple, for lambda invocation.
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(pos, foreachStmt.body);
        assignmentStmt.declaredWithVar = true;
        assignmentStmt.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.getFirstOperation().argVar.symbol));
        final BLangBracedOrTupleExpr tupleExpr = (BLangBracedOrTupleExpr) TreeBuilder.createBracedOrTupleExpression();
        for (BLangVariable foreachVariable : foreachVariables) {
            tupleExpr.expressions.add(ASTBuilderUtil.createVariableRef(pos, foreachVariable.symbol));
        }
        tupleExpr.isBracedExpr = foreachVariables.size() == 1;
        tupleExpr.type = new BTupleType(getTupleTypeList(ctx.getFirstOperation().inputType));
        assignmentStmt.expr = tupleExpr;
    }
    // Generate Operations related
    ctx.operations.forEach(operation -> generateOperationCode(foreachStmt.body, operation));
    // Generate aggregator and result
    if (isReturningIteratorFunction(ctx)) {
        if (ctx.iteratorResultVariables.size() > 1) {
            // Destructure return Values.
            final BLangTupleDestructure tupleAssign = (BLangTupleDestructure) TreeBuilder.createTupleDestructureStatementNode();
            tupleAssign.pos = pos;
            tupleAssign.declaredWithVar = true;
            foreachStmt.body.addStatement(tupleAssign);
            tupleAssign.expr = ASTBuilderUtil.createVariableRef(pos, ctx.getLastOperation().retVar.symbol);
            tupleAssign.varRefs.addAll(ASTBuilderUtil.createVariableRefList(pos, ctx.iteratorResultVariables));
        }
        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) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) BLangTupleDestructure(org.wso2.ballerinalang.compiler.tree.statements.BLangTupleDestructure) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)

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