Search in sources :

Example 46 with BLangBlockStmt

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

the class Desugar method visit.

@Override
public void visit(BLangMatch matchStmt) {
    // Here we generate an if-else statement for the match statement
    // Here is an example match statement
    // 
    // match expr {
    // int k => io:println("int value: " + k);
    // string s => io:println("string value: " + s);
    // json j => io:println("json value: " + s);
    // 
    // }
    // 
    // Here is how we convert the match statement to an if-else statement. The last clause should always be the
    // else clause
    // 
    // string | int | json | any _$$_matchexpr = expr;
    // if ( _$$_matchexpr isassignable int ){
    // int k = (int) _$$_matchexpr; // unbox
    // io:println("int value: " + k);
    // 
    // } else if (_$$_matchexpr isassignable string ) {
    // string s = (string) _$$_matchexpr; // unbox
    // io:println("string value: " + s);
    // 
    // } else if ( _$$_matchexpr isassignable float ||    // should we consider json[] as well
    // _$$_matchexpr isassignable boolean ||
    // _$$_matchexpr isassignable json) {
    // 
    // } else {
    // // handle the last pattern
    // any case..
    // }
    // 
    // First create a block statement to hold generated statements
    BLangBlockStmt matchBlockStmt = (BLangBlockStmt) TreeBuilder.createBlockNode();
    matchBlockStmt.pos = matchStmt.pos;
    // Create a variable definition to store the value of the match expression
    String matchExprVarName = GEN_VAR_PREFIX.value;
    BLangVariable matchExprVar = ASTBuilderUtil.createVariable(matchStmt.expr.pos, matchExprVarName, matchStmt.expr.type, matchStmt.expr, new BVarSymbol(0, names.fromString(matchExprVarName), this.env.scope.owner.pkgID, matchStmt.expr.type, this.env.scope.owner));
    // Now create a variable definition node
    BLangVariableDef matchExprVarDef = ASTBuilderUtil.createVariableDef(matchBlockStmt.pos, matchExprVar);
    // Add the var def statement to the block statement
    // string | int _$$_matchexpr = expr;
    matchBlockStmt.stmts.add(matchExprVarDef);
    // Create if/else blocks with typeof binary expressions for each pattern
    matchBlockStmt.stmts.add(generateIfElseStmt(matchStmt, matchExprVar));
    rewrite(matchBlockStmt, this.env);
    result = matchBlockStmt;
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 47 with BLangBlockStmt

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

the class Desugar method visit.

// Statements
@Override
public void visit(BLangBlockStmt block) {
    SymbolEnv blockEnv = SymbolEnv.createBlockEnv(block, env);
    block.stmts = rewriteStmt(block.stmts, blockEnv);
    result = block;
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 48 with BLangBlockStmt

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

the class Desugar method getSafeAssignSuccessPattern.

private BLangMatchStmtPatternClause getSafeAssignSuccessPattern(DiagnosticPos pos, BType lhsType, boolean isVarDef, BVarSymbol varSymbol, BLangExpression lhsExpr) {
    // File _$_f1 => f = _$_f1;
    // 1) Create the pattern variable
    String patternSuccessCaseVarName = GEN_VAR_PREFIX.value + "t_match";
    BLangVariable patternSuccessCaseVar = ASTBuilderUtil.createVariable(pos, patternSuccessCaseVarName, lhsType, null, new BVarSymbol(0, names.fromString(patternSuccessCaseVarName), this.env.scope.owner.pkgID, lhsType, this.env.scope.owner));
    // 2) Create the pattern body
    BLangExpression varRefExpr;
    if (isVarDef) {
        varRefExpr = ASTBuilderUtil.createVariableRef(pos, varSymbol);
    } else {
        varRefExpr = lhsExpr;
    }
    BLangVariableReference patternSuccessCaseVarRef = ASTBuilderUtil.createVariableRef(pos, patternSuccessCaseVar.symbol);
    BLangAssignment assignmentStmtSuccessCase = ASTBuilderUtil.createAssignmentStmt(pos, new ArrayList<BLangExpression>() {

        {
            add(varRefExpr);
        }
    }, patternSuccessCaseVarRef, false);
    BLangBlockStmt patternBlockSuccessCase = ASTBuilderUtil.createBlockStmt(pos, new ArrayList<BLangStatement>() {

        {
            add(assignmentStmtSuccessCase);
        }
    });
    return ASTBuilderUtil.createMatchStatementPattern(pos, patternSuccessCaseVar, patternBlockSuccessCase);
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 49 with BLangBlockStmt

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

the class Desugar method addTransformerReturn.

private void addTransformerReturn(BLangTransformer transformerNode) {
    // This will only check whether last statement is a return and just add a return statement.
    // This won't analyze if else blocks etc to see whether return statements are present
    BLangBlockStmt blockStmt = transformerNode.body;
    if (transformerNode.workers.size() == 0 && (blockStmt.stmts.size() < 1 || blockStmt.stmts.get(blockStmt.stmts.size() - 1).getKind() != NodeKind.RETURN)) {
        BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
        DiagnosticPos invPos = transformerNode.pos;
        DiagnosticPos pos = new DiagnosticPos(invPos.src, invPos.eLine, invPos.eLine, invPos.sCol, invPos.sCol);
        returnStmt.pos = pos;
        if (!transformerNode.retParams.isEmpty()) {
            returnStmt.namedReturnVariables = transformerNode.retParams;
        }
        blockStmt.addStatement(returnStmt);
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)

Example 50 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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)

Aggregations

BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)33 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)22 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)19 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)16 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)7 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)6 Name (org.wso2.ballerinalang.compiler.util.Name)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)5 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)5 ArrayList (java.util.ArrayList)4 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 Whitespace (org.ballerinalang.model.Whitespace)3