Search in sources :

Example 1 with IterableKind

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

the class IterableAnalyzer method handlerIterableOperation.

public void handlerIterableOperation(BLangInvocation iExpr, BType expectedType, SymbolEnv env) {
    final IterableContext context;
    if (iExpr.expr.type.tag != TypeTags.INTERMEDIATE_COLLECTION) {
        // This is a new iteration chain.
        context = new IterableContext(iExpr.expr, env);
    } else {
        // Get context from previous invocation.
        context = ((BLangInvocation) iExpr.expr).iContext;
    }
    iExpr.iContext = context;
    final IterableKind iterableKind = IterableKind.getFromString(iExpr.name.value);
    final Operation iOperation = new Operation(iterableKind, iExpr, expectedType);
    iExpr.iContext.addOperation(iOperation);
    if (iterableKind.isLambdaRequired()) {
        handleLambdaBasedIterableOperation(context, iOperation);
    } else {
        handleSimpleTerminalOperations(iOperation);
    }
    validateIterableContext(context);
    if (iOperation.resultType != symTable.errType && context.foreachTypes.isEmpty()) {
        calculateForeachTypes(context);
    }
}
Also used : IterableContext(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)

Example 2 with IterableKind

use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind 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)

Aggregations

IterableKind (org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind)2 IterableContext (org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext)1 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)1 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)1 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)1