Search in sources :

Example 46 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class Desugar method createPatternMatchBinaryExpr.

private BLangExpression createPatternMatchBinaryExpr(DiagnosticPos pos, BVarSymbol varSymbol, BType patternType) {
    if (patternType == symTable.nullType) {
        BLangSimpleVarRef varRef = ASTBuilderUtil.createVariableRef(pos, varSymbol);
        BLangLiteral bLangLiteral = ASTBuilderUtil.createLiteral(pos, symTable.nullType, null);
        return ASTBuilderUtil.createBinaryExpr(pos, varRef, bLangLiteral, symTable.booleanType, OperatorKind.EQUAL, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.anyType, symTable.nullType));
    } else {
        return createIsAssignableExpression(pos, varSymbol, patternType);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)

Example 47 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class Desugar method createTypeConversionExpr.

private BLangExpression createTypeConversionExpr(BLangExpression expr, BType sourceType, BType targetType) {
    BConversionOperatorSymbol symbol = (BConversionOperatorSymbol) symResolver.resolveConversionOperator(sourceType, targetType);
    BLangTypeConversionExpr conversionExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
    conversionExpr.pos = expr.pos;
    conversionExpr.expr = expr;
    conversionExpr.type = targetType;
    conversionExpr.conversionSymbol = symbol;
    return conversionExpr;
}
Also used : BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)

Example 48 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangTupleDestructure stmt) {
    // var (a, b) = (tuple)
    // 
    // desugar once
    // any[] x = (tuple);
    // a = x[0];
    final BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(stmt.pos);
    BType runTimeType = new BArrayType(symTable.anyType);
    final BLangVariable tuple = ASTBuilderUtil.createVariable(stmt.pos, "", runTimeType, null, new BVarSymbol(0, names.fromString("tuple"), this.env.scope.owner.pkgID, runTimeType, this.env.scope.owner));
    tuple.expr = stmt.expr;
    final BLangVariableDef variableDef = ASTBuilderUtil.createVariableDefStmt(stmt.pos, blockStmt);
    variableDef.var = tuple;
    for (int index = 0; index < stmt.varRefs.size(); index++) {
        BLangExpression varRef = stmt.varRefs.get(index);
        BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(stmt.pos, symTable.intType, (long) index);
        BLangIndexBasedAccess arrayAccess = ASTBuilderUtil.createIndexBasesAccessExpr(stmt.pos, symTable.anyType, tuple.symbol, indexExpr);
        final BLangExpression assignmentExpr;
        if (types.isValueType(varRef.type)) {
            BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
            castExpr.expr = arrayAccess;
            castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, varRef.type);
            castExpr.type = varRef.type;
            assignmentExpr = castExpr;
        } else {
            assignmentExpr = arrayAccess;
        }
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(stmt.pos, blockStmt);
        assignmentStmt.declaredWithVar = stmt.declaredWithVar;
        assignmentStmt.varRefs = Lists.of(varRef);
        assignmentStmt.expr = assignmentExpr;
    }
    result = rewrite(blockStmt, env);
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 49 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class Desugar method addReferenceVariablesToArgs.

private void addReferenceVariablesToArgs(List<BLangExpression> args, List<BLangExpression> varRefs) {
    BLangArrayLiteral localRefs = createArrayLiteralExprNode();
    varRefs.forEach(varRef -> localRefs.exprs.add(rewrite(varRef, env)));
    args.add(localRefs);
}
Also used : BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)

Example 50 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class Desugar method reorderNamedArgs.

private void reorderNamedArgs(BLangInvocation iExpr, BInvokableSymbol invocableSymbol) {
    Map<String, BLangExpression> namedArgs = new HashMap<>();
    iExpr.namedArgs.forEach(expr -> namedArgs.put(((NamedArgNode) expr).getName().value, expr));
    // Re-order the named arguments
    List<BLangExpression> args = new ArrayList<>();
    for (BVarSymbol param : invocableSymbol.defaultableParams) {
        args.add(namedArgs.get(param.name.value));
    }
    iExpr.namedArgs = args;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)101 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)27 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 ArrayList (java.util.ArrayList)22 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)19 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)15 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)15 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)14 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)14 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)14 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)13 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)11 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)11 BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)10 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)10 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)10 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)10