Search in sources :

Example 1 with BLangTernaryExpr

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

the class TypeChecker method visit.

public void visit(BLangTernaryExpr ternaryExpr) {
    BType expType = checkExpr(ternaryExpr.expr, env, Lists.of(this.symTable.booleanType)).get(0);
    BType thenType = checkExpr(ternaryExpr.thenExpr, env, expTypes).get(0);
    BType elseType = checkExpr(ternaryExpr.elseExpr, env, expTypes).get(0);
    if (expType == symTable.errType || thenType == symTable.errType || elseType == symTable.errType) {
        resultTypes = Lists.of(symTable.errType);
    } else if (expTypes.get(0) == symTable.noType) {
        if (thenType == elseType) {
            resultTypes = Lists.of(thenType);
        } else {
            dlog.error(ternaryExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, thenType, elseType);
            resultTypes = Lists.of(symTable.errType);
        }
    } else {
        resultTypes = expTypes;
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 2 with BLangTernaryExpr

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

the class IterableCodeDesugar method generateCompareAggregator.

/**
 * Generates following.
 *
 * result = result (Operator) value ? result : value
 *
 * @param blockStmt target
 * @param ctx       current context
 * @param operator  compare operator
 */
private void generateCompareAggregator(BLangBlockStmt blockStmt, IterableContext ctx, OperatorKind operator) {
    final DiagnosticPos pos = blockStmt.pos;
    final BLangSimpleVarRef resultVar = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
    final BLangSimpleVarRef valueVar = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
    final BLangBinaryExpr compare = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    compare.pos = pos;
    compare.type = symTable.booleanType;
    compare.opKind = operator;
    compare.lhsExpr = resultVar;
    compare.rhsExpr = valueVar;
    compare.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(operator, resultVar.symbol.type, valueVar.symbol.type);
    final BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
    ternaryExpr.pos = pos;
    ternaryExpr.expr = compare;
    ternaryExpr.thenExpr = resultVar;
    ternaryExpr.elseExpr = valueVar;
    ternaryExpr.type = compare.type;
    final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    countAdd.varRefs.add(resultVar);
    countAdd.expr = ternaryExpr;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 3 with BLangTernaryExpr

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

the class BLangPackageBuilder method createTernaryExpr.

public void createTernaryExpr(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
    ternaryExpr.pos = pos;
    ternaryExpr.addWS(ws);
    ternaryExpr.elseExpr = (BLangExpression) exprNodeStack.pop();
    ternaryExpr.thenExpr = (BLangExpression) exprNodeStack.pop();
    ternaryExpr.expr = (BLangExpression) exprNodeStack.pop();
    if (ternaryExpr.expr.getKind() == NodeKind.TERNARY_EXPR) {
        // Re-organizing ternary expression tree if there nested ternary expressions.
        BLangTernaryExpr root = (BLangTernaryExpr) ternaryExpr.expr;
        BLangTernaryExpr parent = root;
        while (parent.elseExpr.getKind() == NodeKind.TERNARY_EXPR) {
            parent = (BLangTernaryExpr) parent.elseExpr;
        }
        ternaryExpr.expr = parent.elseExpr;
        parent.elseExpr = ternaryExpr;
        ternaryExpr = root;
    }
    addExpressionNode(ternaryExpr);
}
Also used : BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr)

Example 4 with BLangTernaryExpr

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

the class CodeGenerator method visit.

public void visit(BLangTernaryExpr ternaryExpr) {
    // Determine the reg index of the ternary expression and this reg index will be used by both then and else
    // expressions to store their result
    RegIndex ternaryExprRegIndex = calcAndGetExprRegIndex(ternaryExpr);
    // Generate code for the condition
    this.genNode(ternaryExpr.expr, this.env);
    Operand ifFalseJumpAddr = getOperand(-1);
    this.emit(InstructionCodes.BR_FALSE, ternaryExpr.expr.regIndex, ifFalseJumpAddr);
    // Generate code for the then expression
    ternaryExpr.thenExpr.regIndex = createLHSRegIndex(ternaryExprRegIndex);
    this.genNode(ternaryExpr.thenExpr, this.env);
    Operand endJumpAddr = getOperand(-1);
    this.emit(InstructionCodes.GOTO, endJumpAddr);
    ifFalseJumpAddr.value = nextIP();
    // Generate code for the then expression
    ternaryExpr.elseExpr.regIndex = createLHSRegIndex(ternaryExprRegIndex);
    this.genNode(ternaryExpr.elseExpr, this.env);
    endJumpAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

BLangTernaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr)2 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)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