Search in sources :

Example 56 with BLangBlockStmt

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

the class IterableCodeDesugar method generateCountAggregator.

/**
 * Generates following.
 *
 * variable = variable + 1;
 *
 * @param blockStmt target
 * @param variable  variable to increment
 */
private void generateCountAggregator(BLangBlockStmt blockStmt, BLangVariable variable) {
    final DiagnosticPos pos = blockStmt.pos;
    // create count = count + 1;
    final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
    add.pos = pos;
    add.type = symTable.intType;
    add.opKind = OperatorKind.ADD;
    add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, variable.symbol);
    add.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 1L);
    add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, symTable.intType, symTable.intType);
    final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, variable.symbol));
    countAdd.expr = add;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 57 with BLangBlockStmt

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

the class IterableCodeDesugar method generateMapAggregator.

/**
 * Generates following.
 *
 * result[key] = value;
 *
 * @param blockStmt target
 * @param ctx       current context
 */
private void generateMapAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
    final DiagnosticPos pos = blockStmt.pos;
    // create assignment result[key] = value
    final BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
    indexAccessNode.type = ctx.iteratorResultVariables.get(1).symbol.type;
    final BLangAssignment valueAssign = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    valueAssign.varRefs.add(indexAccessNode);
    valueAssign.expr = ASTBuilderUtil.generateConversionExpr(ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(1).symbol), symTable.anyType, symResolver);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 58 with BLangBlockStmt

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

the class IterableCodeDesugar method generateArrayAggregator.

/**
 * Generates following.
 *
 * result[count] = value;
 * count = count + 1;
 *
 * @param blockStmt target
 * @param ctx       current context
 */
private void generateArrayAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
    final DiagnosticPos pos = blockStmt.pos;
    // create assignment result[count] = value;
    final BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
    indexAccessNode.type = ctx.iteratorResultVariables.get(0).symbol.type;
    final BLangAssignment valueAssign = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    valueAssign.varRefs.add(indexAccessNode);
    valueAssign.expr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
    // create count = count + 1;
    generateCountAggregator(blockStmt, ctx.countVar);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 59 with BLangBlockStmt

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

the class IterableCodeDesugar method generateForeach.

/**
 * Generates statements for foreach operation.
 *
 * lambda(...)
 *
 * @param blockStmt target
 * @param operation operation instance
 */
private void generateForeach(BLangBlockStmt blockStmt, Operation operation) {
    final DiagnosticPos pos = operation.pos;
    final BLangExpressionStmt exprStmt = ASTBuilderUtil.createExpressionStmt(pos, blockStmt);
    exprStmt.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)

Example 60 with BLangBlockStmt

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

the class BLangPackageBuilder method addTryClause.

public void addTryClause(DiagnosticPos pos) {
    BLangBlockStmt tryBlock = (BLangBlockStmt) this.blockNodeStack.pop();
    tryBlock.pos = pos;
    tryCatchFinallyNodesStack.peek().tryBody = tryBlock;
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)

Aggregations

BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)33 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)22 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)19 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)16 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)7 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)6 Name (org.wso2.ballerinalang.compiler.util.Name)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)5 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)5 ArrayList (java.util.ArrayList)4 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 Whitespace (org.ballerinalang.model.Whitespace)3