Search in sources :

Example 1 with BLangBracedOrTupleExpr

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

the class BLangPackageBuilder method createBracedOrTupleExpression.

public void createBracedOrTupleExpression(DiagnosticPos pos, Set<Whitespace> ws, int numberOfExpressions) {
    final BLangBracedOrTupleExpr expr = (BLangBracedOrTupleExpr) TreeBuilder.createBracedOrTupleExpression();
    expr.pos = pos;
    expr.addWS(ws);
    for (int i = 0; i < numberOfExpressions; i++) {
        expr.expressions.add(0, (BLangExpression) exprNodeStack.pop());
    }
    addExpressionNode(expr);
}
Also used : BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)

Example 2 with BLangBracedOrTupleExpr

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

the class TypeChecker method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Handle Tuple Expression.
    if (!expTypes.isEmpty() && expTypes.get(0).tag == TypeTags.TUPLE) {
        BTupleType tupleType = (BTupleType) this.expTypes.get(0);
        // Fix this.
        List<BType> expTypes = getListWithErrorTypes(bracedOrTupleExpr.expressions.size());
        if (tupleType.tupleTypes.size() != bracedOrTupleExpr.expressions.size()) {
            dlog.error(bracedOrTupleExpr.pos, DiagnosticCode.SYNTAX_ERROR, "tuple and expression size does not match");
        } else {
            expTypes = tupleType.tupleTypes;
        }
        List<BType> results = new ArrayList<>();
        for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
            results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(expTypes.get(i))).get(0));
        }
        resultTypes = Lists.of(new BTupleType(results));
    } else if (bracedOrTupleExpr.expressions.size() > 1) {
        // This is a tuple.
        List<BType> results = new ArrayList<>();
        for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
            results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(symTable.noType)).get(0));
        }
        resultTypes = Lists.of(new BTupleType(results));
    } else {
        // This is a braced expression.
        bracedOrTupleExpr.isBracedExpr = true;
        final BLangExpression expr = bracedOrTupleExpr.expressions.get(0);
        final BType actualType = checkExpr(expr, env, Lists.of(symTable.noType)).get(0);
        types.setImplicitCastExpr(expr, actualType, expTypes.get(0));
        resultTypes = Lists.of(actualType);
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) ArrayList(java.util.ArrayList) List(java.util.List) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 3 with BLangBracedOrTupleExpr

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

the class CodeGenerator method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Emit create array instruction
    RegIndex exprRegIndex = calcAndGetExprRegIndex(bracedOrTupleExpr);
    Operand typeCPIndex = getTypeCPIndex(bracedOrTupleExpr.type);
    emit(InstructionCodes.RNEWARRAY, exprRegIndex, typeCPIndex);
    // Emit instructions populate initial array values;
    for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
        BLangExpression argExpr = bracedOrTupleExpr.expressions.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = argExpr.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        emit(InstructionCodes.RASTORE, exprRegIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 4 with BLangBracedOrTupleExpr

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr 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

BTupleType (org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangBracedOrTupleExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)2 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangAnnotationAttachmentPoint (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)1 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)1 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)1 BLangTupleDestructure (org.wso2.ballerinalang.compiler.tree.statements.BLangTupleDestructure)1 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)1