Search in sources :

Example 6 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar 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);
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 7 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar in project ballerina by ballerina-lang.

the class ParserUtils method loadBuiltInPackage.

/**
 * Loading builtin packages.
 *
 * @param context compiler context
 * @return {BLangPackage} builtIn package
 */
private static BLangPackage loadBuiltInPackage(CompilerContext context) {
    PackageLoader pkgLoader = PackageLoader.getInstance(context);
    SymbolTable symbolTable = SymbolTable.getInstance(context);
    SemanticAnalyzer semAnalyzer = SemanticAnalyzer.getInstance(context);
    CodeAnalyzer codeAnalyzer = CodeAnalyzer.getInstance(context);
    Desugar desugar = Desugar.getInstance(context);
    BLangPackage builtInPkg = desugar.perform(codeAnalyzer.analyze(semAnalyzer.analyze(pkgLoader.loadAndDefinePackage(Names.BUILTIN_ORG.getValue(), Names.BUILTIN_PACKAGE.getValue()))));
    symbolTable.builtInPackageSymbol = builtInPkg.symbol;
    return builtInPkg;
}
Also used : CodeAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer) LSPackageLoader(org.ballerinalang.langserver.LSPackageLoader) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) Desugar(org.wso2.ballerinalang.compiler.desugar.Desugar) SemanticAnalyzer(org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer)

Aggregations

BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 BLangMatch (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch)2 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 LSPackageLoader (org.ballerinalang.langserver.LSPackageLoader)1 PackageLoader (org.wso2.ballerinalang.compiler.PackageLoader)1 Desugar (org.wso2.ballerinalang.compiler.desugar.Desugar)1 CodeAnalyzer (org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer)1 SemanticAnalyzer (org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer)1 SymbolTable (org.wso2.ballerinalang.compiler.semantics.model.SymbolTable)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)1