Search in sources :

Example 1 with BLangBinaryExpr

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;
}
Also used : BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 2 with BLangBinaryExpr

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);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 3 with BLangBinaryExpr

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);
    }
}
Also used : BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 4 with BLangBinaryExpr

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

Example 5 with BLangBinaryExpr

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;
}
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