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