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();
}
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();
}
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;
}
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));
}
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;
}
Aggregations