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