Search in sources :

Example 6 with BLangIf

use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.

the class Desugar method generateIfElseStmt.

/**
 * Generate an if-else statement from the given match statement.
 *
 * @param patternClause match pattern statement node
 * @param matchExprVar  variable node of the match expression
 * @return if else statement node
 */
private BLangIf generateIfElseStmt(BLangMatchStmtPatternClause patternClause, BLangVariable matchExprVar) {
    BLangExpression patternIfCondition = createPatternIfCondition(patternClause, matchExprVar.symbol);
    BLangBlockStmt patternBody = getMatchPatternBody(patternClause, matchExprVar);
    return ASTBuilderUtil.createIfElseStmt(patternClause.pos, patternIfCondition, patternBody, null);
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 7 with BLangIf

use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf 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 8 with BLangIf

use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateFilter.

/**
 * Generates statements for filter operation.
 *
 * if(!lambda(...)){
 * next;
 * }
 *
 * @param blockStmt target
 * @param operation operation instance
 */
private void generateFilter(BLangBlockStmt blockStmt, Operation operation) {
    final DiagnosticPos pos = operation.pos;
    final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
    final BLangUnaryExpr notExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
    notExpr.pos = pos;
    notExpr.operator = OperatorKind.NOT;
    notExpr.opSymbol = (BOperatorSymbol) symResolver.resolveUnaryOperator(pos, notExpr.operator, symTable.booleanType);
    notExpr.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
    notExpr.type = symTable.booleanType;
    ifNode.expr = notExpr;
    ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
    ASTBuilderUtil.createNextStmt(pos, ifNode.body);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf)

Example 9 with BLangIf

use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.

the class BLangPackageBuilder method startIfElseNode.

public void startIfElseNode(DiagnosticPos pos) {
    BLangIf ifNode = (BLangIf) TreeBuilder.createIfElseStatementNode();
    ifNode.pos = pos;
    ifElseStatementStack.push(ifNode);
    startBlock();
}
Also used : BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf)

Example 10 with BLangIf

use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.

the class Desugar method generateIfElseStmt.

private BLangStatement generateIfElseStmt(BLangMatch matchStmt, BLangVariable matchExprVar) {
    List<BLangMatchStmtPatternClause> patterns = matchStmt.patternClauses;
    if (patterns.size() == 1) {
        return getMatchPatternBody(patterns.get(0), matchExprVar);
    }
    BLangIf parentIfNode = generateIfElseStmt(patterns.get(0), matchExprVar);
    BLangIf currentIfNode = parentIfNode;
    for (int i = 1; i < patterns.size(); i++) {
        if (i == patterns.size() - 1) {
            // This is the last pattern
            currentIfNode.elseStmt = getMatchPatternBody(patterns.get(i), matchExprVar);
        } else {
            currentIfNode.elseStmt = generateIfElseStmt(patterns.get(i), matchExprVar);
            currentIfNode = (BLangIf) currentIfNode.elseStmt;
        }
    }
    // 
    return parentIfNode;
}
Also used : BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)2 Node (org.ballerinalang.model.tree.Node)1 StatementNode (org.ballerinalang.model.tree.statements.StatementNode)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)1 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)1 BLangUnaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)1 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)1 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)1 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1 BLangWhile (org.wso2.ballerinalang.compiler.tree.statements.BLangWhile)1 Name (org.wso2.ballerinalang.compiler.util.Name)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1