use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr in project ballerina by ballerina-lang.
the class SemanticAnalyzer method getBinaryExpr.
private BLangExpression getBinaryExpr(BLangExpression lExpr, BLangExpression rExpr, OperatorKind opKind, BSymbol opSymbol) {
BLangBinaryExpr binaryExpressionNode = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
binaryExpressionNode.lhsExpr = lExpr;
binaryExpressionNode.rhsExpr = rExpr;
binaryExpressionNode.pos = rExpr.pos;
binaryExpressionNode.opKind = opKind;
if (opSymbol != symTable.notFoundSymbol) {
binaryExpressionNode.type = opSymbol.type.getReturnTypes().get(0);
binaryExpressionNode.opSymbol = (BOperatorSymbol) opSymbol;
} else {
binaryExpressionNode.type = symTable.errType;
}
return binaryExpressionNode;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangBinaryExpr binaryExpr) {
BType lhsType = checkExpr(binaryExpr.lhsExpr, env).get(0);
BType rhsType = checkExpr(binaryExpr.rhsExpr, env).get(0);
// Set error type as the actual type.
BType actualType = symTable.errType;
// Look up operator symbol if both rhs and lhs types are error types
if (lhsType != symTable.errType && rhsType != symTable.errType) {
BSymbol opSymbol = symResolver.resolveBinaryOperator(binaryExpr.opKind, lhsType, rhsType);
if (opSymbol == symTable.notFoundSymbol) {
dlog.error(binaryExpr.pos, DiagnosticCode.BINARY_OP_INCOMPATIBLE_TYPES, binaryExpr.opKind, lhsType, rhsType);
} else {
binaryExpr.opSymbol = (BOperatorSymbol) opSymbol;
actualType = opSymbol.type.getReturnTypes().get(0);
}
}
resultTypes = types.checkTypes(binaryExpr, Lists.of(actualType), expTypes);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangBinaryExpr binaryExpr) {
if (OperatorKind.AND.equals(binaryExpr.opKind)) {
visitAndExpression(binaryExpr);
} else if (OperatorKind.OR.equals(binaryExpr.opKind)) {
visitOrExpression(binaryExpr);
} else if (binaryExpr.opSymbol.opcode == InstructionCodes.REQ_NULL || binaryExpr.opSymbol.opcode == InstructionCodes.RNE_NULL || binaryExpr.opSymbol.opcode == InstructionCodes.SEQ_NULL || binaryExpr.opSymbol.opcode == InstructionCodes.SNE_NULL) {
BLangExpression expr = (binaryExpr.lhsExpr.type.tag == TypeTags.NULL) ? binaryExpr.rhsExpr : binaryExpr.lhsExpr;
genNode(expr, this.env);
emit(binaryExpr.opSymbol.opcode, expr.regIndex, calcAndGetExprRegIndex(binaryExpr));
} else {
genNode(binaryExpr.lhsExpr, this.env);
genNode(binaryExpr.rhsExpr, this.env);
RegIndex regIndex = calcAndGetExprRegIndex(binaryExpr);
emit(binaryExpr.opSymbol.opcode, binaryExpr.lhsExpr.regIndex, binaryExpr.rhsExpr.regIndex, regIndex);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCalculateAverage.
/**
* Generates following.
*
* result = result / count
*
* @param blockStmt target
* @param ctx current context
*/
private void generateCalculateAverage(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr divide = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
divide.pos = pos;
divide.type = ctx.resultVar.symbol.type;
divide.opKind = OperatorKind.ADD;
divide.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
divide.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
divide.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.DIV, divide.type, ctx.countVar.symbol.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = divide;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateSumAggregator.
/**
* Generates following.
*
* result = result + value
*
* @param blockStmt target
* @param ctx current context
*/
private void generateSumAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
add.pos = pos;
add.type = ctx.resultVar.symbol.type;
add.opKind = OperatorKind.ADD;
add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
add.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, add.type, add.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = add;
}
Aggregations