Search in sources :

Example 6 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.

the class Desugar method createPatternIfCondition.

private BLangExpression createPatternIfCondition(BLangMatchStmtPatternClause patternClause, BVarSymbol varSymbol) {
    BLangExpression binaryExpr;
    BType patternType = patternClause.variable.type;
    BType[] memberTypes;
    if (patternType.tag == TypeTags.UNION) {
        BUnionType unionType = (BUnionType) patternType;
        memberTypes = unionType.memberTypes.toArray(new BType[0]);
    } else {
        memberTypes = new BType[1];
        memberTypes[0] = patternType;
    }
    if (memberTypes.length == 1) {
        binaryExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
    } else {
        BLangExpression lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
        BLangExpression rhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[1]);
        binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
        for (int i = 2; i < memberTypes.length; i++) {
            lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[i]);
            rhsExpr = binaryExpr;
            binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
        }
    }
    return binaryExpr;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 7 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol 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 8 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol 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)

Example 9 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol 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 10 with BOperatorSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangUnaryExpr unaryExpr) {
    BType exprType = null;
    BType actualType = symTable.errType;
    if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
        // Handle typeof operator separately
        if (unaryExpr.expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
            BLangSimpleVarRef varRef = (BLangSimpleVarRef) unaryExpr.expr;
            Name varRefName = names.fromIdNode((varRef).variableName);
            Name pkgAlias = names.fromIdNode((varRef).pkgAlias);
            // Resolve symbol for BLangSimpleVarRef
            BSymbol varRefSybmol = symResolver.lookupSymbolInPackage(unaryExpr.pos, env, pkgAlias, varRefName, SymTag.VARIABLE);
            if (varRefSybmol == symTable.notFoundSymbol) {
                // Resolve symbol for User Defined Type ( converted from BLangSimpleVarRef )
                BLangTypeofExpr typeAccessExpr = getTypeAccessExpression(varRef);
                unaryExpr.expr = typeAccessExpr;
                actualType = typeAccessExpr.type;
                resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
                return;
            } else {
                // Check type if resolved as BLangSimpleVarRef
                exprType = checkExpr(unaryExpr.expr, env).get(0);
            }
        } else {
            // Check type if resolved as non BLangSimpleVarRef Expression
            exprType = checkExpr(unaryExpr.expr, env).get(0);
        }
        if (exprType != symTable.errType) {
            unaryExpr.opSymbol = Symbols.createTypeofOperatorSymbol(exprType, types, symTable, names);
            actualType = unaryExpr.opSymbol.type.getReturnTypes().get(0);
        }
    } else {
        exprType = checkExpr(unaryExpr.expr, env).get(0);
        if (exprType != symTable.errType) {
            BSymbol symbol = symResolver.resolveUnaryOperator(unaryExpr.pos, unaryExpr.operator, exprType);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(unaryExpr.pos, DiagnosticCode.UNARY_OP_INCOMPATIBLE_TYPES, unaryExpr.operator, exprType);
            } else {
                unaryExpr.opSymbol = (BOperatorSymbol) symbol;
                actualType = symbol.type.getReturnTypes().get(0);
            }
        }
    }
    resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)6 BOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)5 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)3 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BLangUnaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)2 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)2 ArrayList (java.util.ArrayList)1 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)1 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangIsAssignableExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIsAssignableExpr)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1