Search in sources :

Example 21 with BLangAssignment

use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.

the class Desugar method createAssignmentStmt.

// TODO: Same function is used in symbol enter. Refactor this to reuse the same function.
private StatementNode createAssignmentStmt(BLangVariable variable) {
    BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
    varRef.pos = variable.pos;
    varRef.variableName = variable.name;
    varRef.symbol = variable.symbol;
    varRef.type = variable.type;
    varRef.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
    BLangAssignment assignmentStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
    assignmentStmt.expr = variable.expr;
    assignmentStmt.pos = variable.pos;
    assignmentStmt.addVariable(varRef);
    return assignmentStmt;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 22 with BLangAssignment

use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment 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);
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 23 with BLangAssignment

use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.

the class Desugar method addArgInitExpr.

private void addArgInitExpr(BLangTransformer transformerNode, BLangVariable var) {
    BLangSimpleVarRef varRef = new BLangLocalVarRef(var.symbol);
    varRef.lhsVar = true;
    varRef.pos = var.pos;
    varRef.type = var.type;
    BLangExpression initExpr = null;
    switch(var.type.tag) {
        case TypeTags.MAP:
            initExpr = new BLangMapLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.JSON:
            initExpr = new BLangJSONLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.STRUCT:
            initExpr = new BLangStructLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.INT:
        case TypeTags.FLOAT:
        case TypeTags.STRING:
        case TypeTags.BOOLEAN:
        case TypeTags.BLOB:
        case TypeTags.XML:
            return;
        case TypeTags.TABLE:
        case TypeTags.STREAM:
            // TODO: add this once the able initializing is supported.
            return;
        default:
            return;
    }
    initExpr.pos = var.pos;
    BLangAssignment assignStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
    assignStmt.pos = var.pos;
    assignStmt.addVariable(varRef);
    assignStmt.expr = initExpr;
    transformerNode.body.stmts.add(0, assignStmt);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) ArrayList(java.util.ArrayList) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 24 with BLangAssignment

use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangBind bindNode) {
    bindNode.varRef = rewriteExpr(bindNode.varRef);
    bindNode.expr = rewriteExpr(bindNode.expr);
    result = new BLangAssignment(bindNode.pos, Arrays.asList(bindNode.varRef), bindNode.expr, false);
}
Also used : BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 25 with BLangAssignment

use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateMap.

/**
 * Generates statements for Map/Select operation. Select operation is similar to Map except that Select returns
 * a Table where as Map returns an Array.
 *
 * v3,v4 = lambda(v1,v2);
 *
 * @param blockStmt target
 * @param operation operation instance
 */
private void generateMap(BLangBlockStmt blockStmt, Operation operation) {
    final DiagnosticPos pos = operation.pos;
    final BLangAssignment assignment = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    assignment.varRefs.add(ASTBuilderUtil.createVariableRef(operation.pos, operation.retVar.symbol));
    assignment.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Aggregations

BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)29 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)13 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)11 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)10 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)9 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)7 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)7 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)7 ArrayList (java.util.ArrayList)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)6 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)4 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)3 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)3 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)3 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)3 Arrays (java.util.Arrays)2