use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause in project ballerina by ballerina-lang.
the class Desugar method createPatternIfCondition.
private BLangExpression createPatternIfCondition(BLangMatchStmtPatternClause patternClause, BVarSymbol varSymbol) {
BLangExpression binaryExpr;
BType patternType = patternClause.variable.type;
BType[] memberTypes;
if (patternType.tag == TypeTags.UNION) {
BUnionType unionType = (BUnionType) patternType;
memberTypes = unionType.memberTypes.toArray(new BType[0]);
} else {
memberTypes = new BType[1];
memberTypes[0] = patternType;
}
if (memberTypes.length == 1) {
binaryExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
} else {
BLangExpression lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
BLangExpression rhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[1]);
binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
for (int i = 2; i < memberTypes.length; i++) {
lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[i]);
rhsExpr = binaryExpr;
binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
}
}
return binaryExpr;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangMatchStmtPatternClause patternClause) {
// If the variable is not equal to '_', then define the variable in the block scope
if (!patternClause.variable.name.value.endsWith(Names.IGNORE.value)) {
SymbolEnv blockEnv = SymbolEnv.createBlockEnv((BLangBlockStmt) patternClause.body, env);
symbolEnter.defineNode(patternClause.variable, blockEnv);
analyzeStmt(patternClause.body, blockEnv);
return;
}
symbolEnter.defineNode(patternClause.variable, this.env);
analyzeStmt(patternClause.body, this.env);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause in project ballerina by ballerina-lang.
the class Desugar method getSafeAssignErrorPattern.
private BLangMatchStmtPatternClause getSafeAssignErrorPattern(DiagnosticPos pos, BSymbol invokableSymbol) {
// From here onwards we assume that this function has only one return type
// Owner of the variable symbol must be an invokable symbol
boolean noRetParams = ((BInvokableType) invokableSymbol.type).retTypes.isEmpty();
boolean returnErrorType = false;
if (!noRetParams) {
BType retType = ((BInvokableType) invokableSymbol.type).retTypes.get(0);
Set<BType> returnTypeSet = retType.tag == TypeTags.UNION ? ((BUnionType) retType).memberTypes : new HashSet<BType>() {
{
add(retType);
}
};
returnErrorType = returnTypeSet.stream().anyMatch(type -> types.isAssignable(type, symTable.errStructType));
}
// Create the pattern to match the error type
// 1) Create the pattern variable
String patternFailureCaseVarName = GEN_VAR_PREFIX.value + "t_failure";
BLangVariable patternFailureCaseVar = ASTBuilderUtil.createVariable(pos, patternFailureCaseVarName, symTable.errStructType, null, new BVarSymbol(0, names.fromString(patternFailureCaseVarName), this.env.scope.owner.pkgID, symTable.errStructType, this.env.scope.owner));
// 2) Create the pattern block
BLangVariableReference patternFailureCaseVarRef = ASTBuilderUtil.createVariableRef(pos, patternFailureCaseVar.symbol);
BLangBlockStmt patternBlockFailureCase = (BLangBlockStmt) TreeBuilder.createBlockNode();
patternBlockFailureCase.pos = pos;
if (noRetParams || !returnErrorType) {
// throw e
BLangThrow throwStmt = (BLangThrow) TreeBuilder.createThrowNode();
throwStmt.pos = pos;
throwStmt.expr = patternFailureCaseVarRef;
patternBlockFailureCase.stmts.add(throwStmt);
} else {
// return e;
BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
returnStmt.pos = pos;
returnStmt.exprs = new ArrayList<BLangExpression>() {
{
add(patternFailureCaseVarRef);
}
};
patternBlockFailureCase.stmts.add(returnStmt);
}
return ASTBuilderUtil.createMatchStatementPattern(pos, patternFailureCaseVar, patternBlockFailureCase);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause 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.BLangMatch.BLangMatchStmtPatternClause 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);
}
Aggregations