use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause 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.BLangMatch.BLangMatchStmtPatternClause in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addMatchStmtPattern.
public void addMatchStmtPattern(DiagnosticPos pos, Set<Whitespace> ws, String identifier) {
BLangMatchStmtPatternClause patternClause = (BLangMatchStmtPatternClause) TreeBuilder.createMatchStatementPattern();
patternClause.pos = pos;
Set<Whitespace> varDefWS = removeNthFromStart(ws, 0);
patternClause.addWS(ws);
// Create a variable node
identifier = identifier == null ? Names.IGNORE.value : identifier;
BLangVariable var = (BLangVariable) TreeBuilder.createVariableNode();
var.pos = pos;
var.setName(this.createIdentifier(identifier));
var.setTypeNode(this.typeNodeStack.pop());
var.addWS(varDefWS);
patternClause.variable = var;
patternClause.body = (BLangBlockStmt) blockNodeStack.pop();
patternClause.body.pos = pos;
this.matchStmtStack.peekFirst().patternClauses.add(patternClause);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause 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;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause in project ballerina by ballerina-lang.
the class Desugar method addConversionExprIfRequired.
private BLangExpression addConversionExprIfRequired(BLangSimpleVarRef matchExprVarRef, BLangMatchStmtPatternClause patternClause) {
BType lhsType = patternClause.variable.type;
BType rhsType = matchExprVarRef.type;
if (types.isSameType(rhsType, lhsType)) {
return matchExprVarRef;
}
types.setImplicitCastExpr(matchExprVarRef, rhsType, lhsType);
if (matchExprVarRef.impConversionExpr != null) {
return matchExprVarRef;
}
BConversionOperatorSymbol conversionSymbol;
if (types.isValueType(lhsType) && !(rhsType.tag == TypeTags.JSON)) {
conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, lhsType);
} else if (lhsType.tag == TypeTags.UNION || rhsType.tag == TypeTags.UNION) {
conversionSymbol = Symbols.createConversionOperatorSymbol(rhsType, lhsType, symTable.errStructType, false, true, InstructionCodes.NOP, null, null);
} else {
conversionSymbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(rhsType, lhsType);
}
// Create a type cast expression
BLangTypeConversionExpr conversionExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
conversionExpr.expr = matchExprVarRef;
conversionExpr.targetType = lhsType;
conversionExpr.conversionSymbol = conversionSymbol;
conversionExpr.type = lhsType;
return conversionExpr;
}
Aggregations