use of org.wso2.ballerinalang.compiler.tree.statements.BLangStatement in project ballerina by ballerina-lang.
the class Desugar method rewrite.
@SuppressWarnings("unchecked")
private <E extends BLangStatement> E rewrite(E statement, SymbolEnv env) {
if (statement == null) {
return null;
}
BLangStatementLink link = new BLangStatementLink();
link.parent = currentLink;
currentLink = link;
BLangStatement stmt = (BLangStatement) rewrite((BLangNode) statement, env);
// Link Statements.
link.statement = stmt;
stmt.statementLink = link;
currentLink = link.parent;
return (E) stmt;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangStatement in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangVariableDef varDefNode) {
if (varDefNode.var.expr instanceof BLangRecordLiteral && ((BLangRecordLiteral) varDefNode.var.expr).type.tag == TypeTags.STREAM) {
((BLangRecordLiteral) varDefNode.var.expr).name = varDefNode.var.name;
}
varDefNode.var = rewrite(varDefNode.var, env);
BLangVariable varNode = varDefNode.var;
// Generate default init expression, if rhs expr is null
if (varNode.expr == null) {
varNode.expr = getInitExpr(varNode.type);
}
if (!varNode.safeAssignment) {
result = varDefNode;
return;
}
// Desugar the =? operator with the match statement
//
// e.g.
// var f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
//
// {
// File f;
// match openFile("/tmp/foo.txt") {
// File _$_f1 => f = _$_f1;
// error e => throw e | return e
// }
// }
// Create the pattern to match the success case
BLangMatchStmtPatternClause patternSuccessCase = getSafeAssignSuccessPattern(varNode.pos, varNode.symbol.type, true, varNode.symbol, null);
BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(varNode.pos, varNode.symbol.owner);
// Create the match statement
BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(varNode.expr.pos, varNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {
{
add(patternSuccessCase);
add(patternErrorCase);
}
});
// var f =? foo() -> var f;
varNode.expr = null;
varNode.safeAssignment = false;
BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(varDefNode.pos, new ArrayList<BLangStatement>() {
{
add(varDefNode);
add(matchStmt);
}
});
result = rewrite(safeAssignmentBlock, this.env);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangStatement 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.BLangStatement in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangStruct structNode) {
// Add struct level variables to the init function.
structNode.fields.stream().map(field -> {
// then get the default value literal for that particular struct.
if (field.expr == null) {
field.expr = getInitExpr(field.type);
}
return field;
}).filter(field -> field.expr != null).forEachOrdered(field -> {
if (!structNode.initFunction.initFunctionStmts.containsKey(field.symbol)) {
structNode.initFunction.initFunctionStmts.put(field.symbol, (BLangStatement) createAssignmentStmt(field));
}
});
// Adding init statements to the init function.
BLangStatement[] initStmts = structNode.initFunction.initFunctionStmts.values().toArray(new BLangStatement[0]);
for (int i = 0; i < structNode.initFunction.initFunctionStmts.size(); i++) {
structNode.initFunction.body.stmts.add(i, initStmts[i]);
}
result = structNode;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangStatement 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