Search in sources :

Example 11 with BLangMatchStmtPatternClause

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

Example 12 with BLangMatchStmtPatternClause

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);
}
Also used : BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) Whitespace(org.ballerinalang.model.Whitespace) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 13 with BLangMatchStmtPatternClause

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

Example 14 with BLangMatchStmtPatternClause

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

Aggregations

BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)7 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)6 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)6 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)4 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 BLangMatch (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch)4 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)3 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)3 ArrayList (java.util.ArrayList)2 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)2 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)2 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)2 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)2 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)2 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)2 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2