Search in sources :

Example 26 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class IterableCodeDesugar method createIteratorResultVariables.

private List<BLangVariable> createIteratorResultVariables(IterableContext ctx, BLangVariable lastOperationArg, BLangFunction funcNode) {
    List<BLangVariable> resultVariables = new ArrayList<>();
    if (lastOperationArg.type.tag != TypeTags.TUPLE) {
        resultVariables.add(lastOperationArg);
        defineVariable(lastOperationArg, ctx.env.enclPkg.symbol.pkgID, funcNode);
        return resultVariables;
    }
    final List<BType> tupleTypes = ((BTupleType) lastOperationArg.type).tupleTypes;
    int index = 0;
    for (BType type : tupleTypes) {
        String varName = VAR_RESULT_VAL + index++;
        final BLangVariable variable = ASTBuilderUtil.createVariable(funcNode.pos, varName, type);
        resultVariables.add(variable);
        defineVariable(variable, ctx.env.enclPkg.symbol.pkgID, funcNode);
    }
    return resultVariables;
}
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 27 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable 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 28 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable 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 29 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class SiddhiQueryBuilder method visit.

public void visit(BLangForever foreverStatement) {
    siddhiQuery = new StringBuilder();
    streamDefinitionQuery = new StringBuilder();
    streamIds = new HashSet<>();
    inStreamRefs = new ArrayList<>();
    outStreamRefs = new ArrayList<>();
    inTableRefs = new ArrayList<>();
    outTableRefs = new ArrayList<>();
    binaryExpr = null;
    setExpr = null;
    orderByClause = null;
    whereClause = null;
    windowClause = null;
    joinStreamingInputClause = null;
    streamingInputClause = null;
    selectExprClause = null;
    selectExpr = null;
    setAssignmentClause = null;
    groupByClause = null;
    havingClause = null;
    patternStreamingClause = null;
    streamActionClause = null;
    intRangeExpr = null;
    List<VariableNode> globalVariables = foreverStatement.getGlobalVariables();
    if (globalVariables != null) {
        for (VariableNode variable : globalVariables) {
            ((BLangVariable) variable).accept(this);
        }
    }
    List<VariableSymbol> functionVariables = foreverStatement.getFunctionVariables();
    if (functionVariables != null) {
        for (VariableSymbol variable : functionVariables) {
            getStreamDefintionForFuntionVariable((BVarSymbol) variable);
        }
    }
    List<? extends StatementNode> statementNodes = foreverStatement.gettreamingQueryStatements();
    for (StatementNode statementNode : statementNodes) {
        ((BLangStatement) statementNode).accept(this);
    }
    foreverStatement.setSiddhiQuery(this.getSiddhiQuery());
    foreverStatement.setStreamIdsAsString(String.join(",", streamIds));
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) VariableSymbol(org.ballerinalang.model.symbols.VariableSymbol) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 30 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addConstVariable.

public void addConstVariable(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean publicVar, boolean safeAssignment) {
    BLangVariable var = (BLangVariable) this.generateBasicVarNode(pos, ws, identifier, true);
    var.flagSet.add(Flag.CONST);
    if (publicVar) {
        var.flagSet.add(Flag.PUBLIC);
    }
    var.docTag = DocTag.VARIABLE;
    var.safeAssignment = safeAssignment;
    attachAnnotations(var);
    attachDocumentations(var);
    attachDeprecatedNode(var);
    this.compUnit.addTopLevelNode(var);
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)77 ArrayList (java.util.ArrayList)21 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)20 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)18 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)18 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)16 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)15 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)14 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)10 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)10 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)9 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)8 Whitespace (org.ballerinalang.model.Whitespace)7 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)7 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)7 Name (org.wso2.ballerinalang.compiler.util.Name)7 HashMap (java.util.HashMap)6 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)6