Search in sources :

Example 11 with IterableContext

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext 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;
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 12 with IterableContext

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

the class IterableCodeDesugar method createForeachVariables.

/* Some Utils methods */
private List<BLangVariable> createForeachVariables(IterableContext ctx, BLangVariable firstOperationArg, BLangFunction funcNode) {
    List<BLangVariable> foreachVariables = new ArrayList<>();
    if (firstOperationArg.type.tag != TypeTags.TUPLE) {
        foreachVariables.add(firstOperationArg);
        defineVariable(firstOperationArg, ctx.env.enclPkg.symbol.pkgID, funcNode);
        return foreachVariables;
    }
    final List<BType> tupleTypes = ((BTupleType) firstOperationArg.type).tupleTypes;
    int index = 0;
    for (BType type : tupleTypes) {
        String varName = VAR_FOREACH_VAL + index++;
        final BLangVariable variable = ASTBuilderUtil.createVariable(funcNode.pos, varName, type);
        foreachVariables.add(variable);
        defineVariable(variable, ctx.env.enclPkg.symbol.pkgID, funcNode);
    }
    return foreachVariables;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 13 with IterableContext

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

the class IterableCodeDesugar method generateDefaultIfEmpty.

/**
 * Generates following.
 *
 * if(count == 0){
 * return;
 * }
 *
 * @param blockStmt target
 * @param ctx       current context
 */
private void generateDefaultIfEmpty(BLangBlockStmt blockStmt, IterableContext ctx) {
    if (ctx.resultVar.symbol.type.tag > TypeTags.TYPEDESC) {
        return;
    }
    final DiagnosticPos pos = blockStmt.pos;
    final BLangBinaryExpr equality = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    equality.pos = pos;
    equality.type = symTable.booleanType;
    equality.opKind = OperatorKind.EQUAL;
    equality.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
    equality.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
    equality.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.intType, symTable.intType);
    final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
    ifNode.expr = equality;
    ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
    if (ctx.resultVar.symbol.type.tag <= TypeTags.FLOAT) {
        final BLangAssignment assign = ASTBuilderUtil.createAssignmentStmt(pos, ifNode.body);
        assign.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
        switch(ctx.resultVar.symbol.type.tag) {
            case TypeTags.INT:
                assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
                break;
            case TypeTags.FLOAT:
                assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, 0D);
                break;
        }
    }
    final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(pos, ifNode.body);
    returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 14 with IterableContext

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

the class IterableCodeDesugar method generateMapAggregator.

/**
 * Generates following.
 *
 * result[key] = value;
 *
 * @param blockStmt target
 * @param ctx       current context
 */
private void generateMapAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
    final DiagnosticPos pos = blockStmt.pos;
    // create assignment result[key] = value
    final BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
    indexAccessNode.type = ctx.iteratorResultVariables.get(1).symbol.type;
    final BLangAssignment valueAssign = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    valueAssign.varRefs.add(indexAccessNode);
    valueAssign.expr = ASTBuilderUtil.generateConversionExpr(ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(1).symbol), symTable.anyType, symResolver);
}
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 15 with IterableContext

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

the class IterableCodeDesugar method generateIteratorFunction.

private void generateIteratorFunction(IterableContext ctx) {
    final Operation firstOperation = ctx.getFirstOperation();
    final DiagnosticPos pos = firstOperation.pos;
    // Create and define function signature.
    final BLangFunction funcNode = ASTBuilderUtil.createFunction(pos, getFunctionName(FUNC_CALLER));
    funcNode.requiredParams.add(ctx.collectionVar);
    if (isReturningIteratorFunction(ctx)) {
        funcNode.retParams.add(ctx.resultVar);
    }
    defineFunction(funcNode, ctx.env.enclPkg);
    ctx.iteratorFuncSymbol = funcNode.symbol;
    LinkedList<Operation> streamableOperations = new LinkedList<>();
    ctx.operations.stream().filter(op -> op.kind.isLambdaRequired()).forEach(streamableOperations::add);
    if (streamableOperations.isEmpty()) {
        // Generate simple iterator function body.
        generateSimpleIteratorBlock(ctx, funcNode);
        return;
    }
    // Generate Caller Function.
    generateStreamingIteratorBlock(ctx, funcNode, streamableOperations);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) Lists(org.wso2.ballerinalang.util.Lists) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) OperatorKind(org.ballerinalang.model.tree.OperatorKind) BLangTupleDestructure(org.wso2.ballerinalang.compiler.tree.statements.BLangTupleDestructure) SymbolResolver(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) List(java.util.List) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) Types(org.wso2.ballerinalang.compiler.semantics.analyzer.Types) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) ArrayList(java.util.ArrayList) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) TreeBuilder(org.ballerinalang.model.TreeBuilder) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) IterableContext(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) LinkedList(java.util.LinkedList)

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