Search in sources :

Example 11 with BLangBinaryExpr

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

the class CodeGenerator method visitOrExpression.

private void visitOrExpression(BLangBinaryExpr binaryExpr) {
    // short-circuit evaluation
    // Code address to jump if the lhs expression gets evaluated to 'true'.
    Operand lExprTrueJumpAddr = getOperand(-1);
    // Code address to jump if the rhs expression gets evaluated to 'false'.
    Operand rExprFalseJumpAddr = getOperand(-1);
    // Generate code for the left hand side
    genNode(binaryExpr.lhsExpr, this.env);
    emit(InstructionCodes.BR_TRUE, binaryExpr.lhsExpr.regIndex, lExprTrueJumpAddr);
    // Generate code for the right hand side
    genNode(binaryExpr.rhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.rhsExpr.regIndex, rExprFalseJumpAddr);
    lExprTrueJumpAddr.value = nextIP();
    RegIndex exprRegIndex = calcAndGetExprRegIndex(binaryExpr);
    emit(InstructionCodes.BCONST_1, exprRegIndex);
    Operand gotoAddr = getOperand(-1);
    emit(InstructionCodes.GOTO, gotoAddr);
    rExprFalseJumpAddr.value = nextIP();
    // Load 'false' if the both conditions are false;
    emit(InstructionCodes.BCONST_0, exprRegIndex);
    gotoAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 12 with BLangBinaryExpr

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

the class CodeGenerator method visitAndExpression.

private void visitAndExpression(BLangBinaryExpr binaryExpr) {
    // Code address to jump if at least one of the expressions get evaluated to false.
    // short-circuit evaluation
    Operand falseJumpAddr = getOperand(-1);
    // Generate code for the left hand side
    genNode(binaryExpr.lhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.lhsExpr.regIndex, falseJumpAddr);
    // Generate code for the right hand side
    genNode(binaryExpr.rhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.rhsExpr.regIndex, falseJumpAddr);
    // If both l and r conditions are true, then load 'true'
    calcAndGetExprRegIndex(binaryExpr);
    emit(InstructionCodes.BCONST_1, binaryExpr.regIndex);
    Operand gotoAddr = getOperand(-1);
    emit(InstructionCodes.GOTO, gotoAddr);
    falseJumpAddr.value = nextIP();
    // Load 'false' if the both conditions are false;
    emit(InstructionCodes.BCONST_0, binaryExpr.regIndex);
    gotoAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand)

Example 13 with BLangBinaryExpr

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

the class ASTBuilderUtil method createBinaryExpr.

static BLangBinaryExpr createBinaryExpr(DiagnosticPos pos, BLangExpression lhsExpr, BLangExpression rhsExpr, BType type, OperatorKind opKind, BOperatorSymbol symbol) {
    final BLangBinaryExpr binaryExpr = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    binaryExpr.pos = pos;
    binaryExpr.lhsExpr = lhsExpr;
    binaryExpr.rhsExpr = rhsExpr;
    binaryExpr.type = type;
    binaryExpr.opKind = opKind;
    binaryExpr.opSymbol = symbol;
    return binaryExpr;
}
Also used : BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 14 with BLangBinaryExpr

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

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

the class IterableCodeDesugar method generateCountAggregator.

/**
 * Generates following.
 *
 * variable = variable + 1;
 *
 * @param blockStmt target
 * @param variable  variable to increment
 */
private void generateCountAggregator(BLangBlockStmt blockStmt, BLangVariable variable) {
    final DiagnosticPos pos = blockStmt.pos;
    // create count = count + 1;
    final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    add.pos = pos;
    add.type = symTable.intType;
    add.opKind = OperatorKind.ADD;
    add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, variable.symbol);
    add.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 1L);
    add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, symTable.intType, symTable.intType);
    final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, variable.symbol));
    countAdd.expr = add;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Aggregations

BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)15 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)5 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)5 BLangStreamingInput (org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput)2 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)2 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)1 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangTernaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr)1 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)1 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)1