Search in sources :

Example 6 with BLangForeach

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

the class CodeGenerator method visit.

public void visit(BLangForeach foreach) {
    // Calculate temporary scope variables for iteration.
    Operand iteratorVar = getLVIndex(TypeTags.ITERATOR);
    Operand conditionVar = getLVIndex(TypeTags.BOOLEAN);
    // Create new Iterator for given collection.
    this.genNode(foreach.collection, env);
    this.emit(InstructionCodes.ITR_NEW, foreach.collection.regIndex, iteratorVar);
    Operand foreachStartAddress = new Operand(nextIP());
    Operand foreachEndAddress = new Operand(-1);
    Instruction gotoStartInstruction = InstructionFactory.get(InstructionCodes.GOTO, foreachStartAddress);
    Instruction gotoEndInstruction = InstructionFactory.get(InstructionCodes.GOTO, foreachEndAddress);
    // Checks given iterator has a next value.
    this.emit(InstructionCodes.ITR_HAS_NEXT, iteratorVar, conditionVar);
    this.emit(InstructionCodes.BR_FALSE, conditionVar, foreachEndAddress);
    // assign variables.
    generateForeachVarAssignment(foreach, iteratorVar);
    this.loopResetInstructionStack.push(gotoStartInstruction);
    this.loopExitInstructionStack.push(gotoEndInstruction);
    // generate foreach body.
    this.genNode(foreach.body, env);
    this.loopResetInstructionStack.pop();
    this.loopExitInstructionStack.pop();
    // move to next iteration.
    this.emit(gotoStartInstruction);
    foreachEndAddress.value = this.nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) Instruction(org.wso2.ballerinalang.programfile.Instruction)

Example 7 with BLangForeach

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

the class CodeGenerator method generateForeachVarAssignment.

// private helper methods of visitors.
private void generateForeachVarAssignment(BLangForeach foreach, Operand iteratorIndex) {
    List<BLangVariableReference> variables = foreach.varRefs.stream().map(expr -> (BLangVariableReference) expr).collect(Collectors.toList());
    // create Local variable Info entries.
    variables.stream().filter(// Ignoring ignored ("_") variables.
    v -> v.type.tag != TypeTags.NONE).forEach(varRef -> visitVarSymbol(varRef.symbol, lvIndexes, localVarAttrInfo));
    List<Operand> nextOperands = new ArrayList<>();
    nextOperands.add(iteratorIndex);
    nextOperands.add(new Operand(variables.size()));
    foreach.varTypes.forEach(v -> nextOperands.add(new Operand(v.tag)));
    nextOperands.add(new Operand(variables.size()));
    for (int i = 0; i < variables.size(); i++) {
        BLangVariableReference varRef = variables.get(i);
        nextOperands.add(Optional.ofNullable(varRef.symbol.varIndex).orElse(getRegIndex(foreach.varTypes.get(i).tag)));
    }
    this.emit(InstructionCodes.ITR_NEXT, nextOperands.toArray(new Operand[0]));
}
Also used : BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) REF_OFFSET(org.wso2.ballerinalang.programfile.ProgramFileConstants.REF_OFFSET) UTF8CPEntry(org.wso2.ballerinalang.programfile.cpentries.UTF8CPEntry) FloatCPEntry(org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangXMLTextLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral) Map(java.util.Map) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) PackageVarInfo(org.wso2.ballerinalang.programfile.PackageVarInfo) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) LocalVariableInfo(org.wso2.ballerinalang.programfile.LocalVariableInfo) AttributeInfo(org.wso2.ballerinalang.programfile.attributes.AttributeInfo) TypeDescriptor(org.wso2.ballerinalang.compiler.util.TypeDescriptor) PackageID(org.ballerinalang.model.elements.PackageID) ImportPackageInfo(org.wso2.ballerinalang.programfile.ImportPackageInfo) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangXMLNSStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangXMLNSStatement) BLangArrayAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangArrayAccessExpr) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode) ForkJoinCPEntry(org.wso2.ballerinalang.programfile.cpentries.ForkJoinCPEntry) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangAnnotAttachmentAttributeValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangAnnotAttachmentAttributeValue) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangXMLCommentLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangFunctionVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFunctionVarRef) LineNumberTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LineNumberTableAttributeInfo) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) TreeBuilder(org.ballerinalang.model.TreeBuilder) ErrorTableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ErrorTableAttributeInfo) ActionRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.ActionRefCPEntry) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) PackageFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.PackageFile) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) REG(org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.REG) BLangPackageXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangPackageXMLNS) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) BLangStructFieldAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess.BLangStructFieldAccessExpr) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) WorkerInfo(org.wso2.ballerinalang.programfile.WorkerInfo) FIELD(org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD) BOOL_OFFSET(org.wso2.ballerinalang.programfile.ProgramFileConstants.BOOL_OFFSET) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BLangAttachedFunctionInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BLangAttachedFunctionInvocation) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) ForkjoinInfo(org.wso2.ballerinalang.programfile.ForkjoinInfo) Name(org.ballerinalang.model.Name) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) PACKAGE(org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangTransformerInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BLangTransformerInvocation) BLangIsAssignableExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIsAssignableExpr) IntegerCPEntry(org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) ProgramFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) NodeKind(org.ballerinalang.model.tree.NodeKind) InstructionCodes(org.wso2.ballerinalang.programfile.InstructionCodes) BLangActionInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BLangActionInvocation) Instruction(org.wso2.ballerinalang.programfile.Instruction) Entry(java.util.Map.Entry) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangXMLSequenceLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral) AttributeInfoPool(org.wso2.ballerinalang.programfile.attributes.AttributeInfoPool) TransformerRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.TransformerRefCPEntry) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) Stack(java.util.Stack) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangJSONArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral.BLangJSONArrayLiteral) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) InstructionFactory(org.wso2.ballerinalang.programfile.InstructionFactory) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) WorkerDataChannelRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.WorkerDataChannelRefCPEntry) BLangFieldVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangFieldVarRef) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) FieldType(org.wso2.ballerinalang.compiler.util.FieldType) AttachedFunctionInfo(org.wso2.ballerinalang.programfile.AttachedFunctionInfo) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) ConnectorInfo(org.wso2.ballerinalang.programfile.ConnectorInfo) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) StringCPEntry(org.wso2.ballerinalang.programfile.cpentries.StringCPEntry) Arrays(java.util.Arrays) BLangStreamLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangTableLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BConnectorType(org.wso2.ballerinalang.compiler.semantics.model.types.BConnectorType) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BLangAnnotAttachmentAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangAnnotAttachmentAttribute) StructInfo(org.wso2.ballerinalang.programfile.StructInfo) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr) BLangEnumeratorAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess.BLangEnumeratorAccessExpr) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) STRING_OFFSET(org.wso2.ballerinalang.programfile.ProgramFileConstants.STRING_OFFSET) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) ErrorTableEntry(org.wso2.ballerinalang.programfile.ErrorTableEntry) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) ConstantPool(org.wso2.ballerinalang.programfile.cpentries.ConstantPool) ResourceInfo(org.wso2.ballerinalang.programfile.ResourceInfo) CodeAttributeInfo(org.wso2.ballerinalang.programfile.attributes.CodeAttributeInfo) ParamDefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) BLangLocalXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS) ServiceInfo(org.wso2.ballerinalang.programfile.ServiceInfo) BLangPackageVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangPackageVarRef) ArrayList(java.util.ArrayList) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) StructFieldInfo(org.wso2.ballerinalang.programfile.StructFieldInfo) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLProcInsLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral) EnumInfo(org.wso2.ballerinalang.programfile.EnumInfo) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) ActionInfo(org.wso2.ballerinalang.programfile.ActionInfo) BLangJSONAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangJSONAccessExpr) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) EnumeratorInfo(org.wso2.ballerinalang.programfile.EnumeratorInfo) BLangXMLAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangXMLAccessExpr) PackageInfo(org.wso2.ballerinalang.programfile.PackageInfo) INT_OFFSET(org.wso2.ballerinalang.programfile.ProgramFileConstants.INT_OFFSET) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) WorkerDataChannelInfo(org.wso2.ballerinalang.programfile.WorkerDataChannelInfo) FunctionFlags(org.ballerinalang.util.FunctionFlags) BLangXMLAttributeAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttributeAccess) LineNumberInfo(org.wso2.ballerinalang.programfile.LineNumberInfo) TransactionStatus(org.ballerinalang.util.TransactionStatus) TransformerInfo(org.wso2.ballerinalang.programfile.TransformerInfo) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) OperatorKind(org.ballerinalang.model.tree.OperatorKind) BLangFail(org.wso2.ballerinalang.compiler.tree.statements.BLangFail) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) LocalVariableAttributeInfo(org.wso2.ballerinalang.programfile.attributes.LocalVariableAttributeInfo) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) DefaultValueAttributeInfo(org.wso2.ballerinalang.programfile.attributes.DefaultValueAttributeInfo) BLOB_OFFSET(org.wso2.ballerinalang.programfile.ProgramFileConstants.BLOB_OFFSET) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) List(java.util.List) FLOAT_OFFSET(org.wso2.ballerinalang.programfile.ProgramFileConstants.FLOAT_OFFSET) DefaultValue(org.wso2.ballerinalang.programfile.DefaultValue) Optional(java.util.Optional) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) FunctionRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.FunctionRefCPEntry) LOCAL(org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BFunctionPointerInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation.BFunctionPointerInvocation) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) CallableUnitInfo(org.wso2.ballerinalang.programfile.CallableUnitInfo) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) PackageRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.PackageRefCPEntry) BLangStringTemplateLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangIntRangeExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangIntRangeExpression) FunctionInfo(org.wso2.ballerinalang.programfile.FunctionInfo) XMLConstants(javax.xml.XMLConstants) BLangMapAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangMapAccessExpr) VarTypeCountAttributeInfo(org.wso2.ballerinalang.programfile.attributes.VarTypeCountAttributeInfo) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangAwaitExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangAwaitExpr) BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) TypeRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.TypeRefCPEntry) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) ArrayList(java.util.ArrayList) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 8 with BLangForeach

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

the class ASTBuilderUtil method createForeach.

static BLangForeach createForeach(DiagnosticPos pos, BLangBlockStmt target, BLangSimpleVarRef collectionVarRef, List<BLangSimpleVarRef> varRefs, List<BType> inputTypes) {
    final BLangForeach foreach = (BLangForeach) TreeBuilder.createForeachNode();
    foreach.pos = pos;
    target.addStatement(foreach);
    foreach.body = ASTBuilderUtil.createBlockStmt(pos);
    foreach.collection = collectionVarRef;
    foreach.varRefs.addAll(varRefs);
    foreach.varTypes = inputTypes;
    return foreach;
}
Also used : BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)

Example 9 with BLangForeach

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

the class IterableCodeDesugar method generateStreamingIteratorBlock.

private void generateStreamingIteratorBlock(IterableContext ctx, BLangFunction funcNode, LinkedList<Operation> streamOperations) {
    final Operation firstOperation = ctx.getFirstOperation();
    final DiagnosticPos pos = firstOperation.pos;
    // Generate streaming based function Body.
    if (isReturningIteratorFunction(ctx)) {
        if (ctx.resultType.tag != TypeTags.TABLE) {
            // This should be the select operation. No need of a count variable.
            createCounterVarDefStmt(funcNode, ctx);
        }
        createResultVarDefStmt(funcNode, ctx);
        ctx.iteratorResultVariables = createIteratorResultVariables(ctx, streamOperations.getLast().retVar, funcNode);
    } else {
        ctx.iteratorResultVariables = Collections.emptyList();
    }
    // Create variables required.
    final List<BLangVariable> foreachVariables = createForeachVariables(ctx, ctx.getFirstOperation().argVar, funcNode);
    // Define all undefined variables.
    defineRequiredVariables(ctx, streamOperations, foreachVariables, funcNode);
    // Generate foreach iteration.
    final BLangForeach foreachStmt = ASTBuilderUtil.createForeach(pos, funcNode.body, ASTBuilderUtil.createVariableRef(pos, ctx.collectionVar.symbol), ASTBuilderUtil.createVariableRefList(pos, foreachVariables), ctx.foreachTypes);
    if (foreachVariables.size() > 1) {
        // Create tuple, for lambda invocation.
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(pos, foreachStmt.body);
        assignmentStmt.declaredWithVar = true;
        assignmentStmt.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.getFirstOperation().argVar.symbol));
        final BLangBracedOrTupleExpr tupleExpr = (BLangBracedOrTupleExpr) TreeBuilder.createBracedOrTupleExpression();
        for (BLangVariable foreachVariable : foreachVariables) {
            tupleExpr.expressions.add(ASTBuilderUtil.createVariableRef(pos, foreachVariable.symbol));
        }
        tupleExpr.isBracedExpr = foreachVariables.size() == 1;
        tupleExpr.type = new BTupleType(getTupleTypeList(ctx.getFirstOperation().inputType));
        assignmentStmt.expr = tupleExpr;
    }
    // Generate Operations related
    ctx.operations.forEach(operation -> generateOperationCode(foreachStmt.body, operation));
    // Generate aggregator and result
    if (isReturningIteratorFunction(ctx)) {
        if (ctx.iteratorResultVariables.size() > 1) {
            // Destructure return Values.
            final BLangTupleDestructure tupleAssign = (BLangTupleDestructure) TreeBuilder.createTupleDestructureStatementNode();
            tupleAssign.pos = pos;
            tupleAssign.declaredWithVar = true;
            foreachStmt.body.addStatement(tupleAssign);
            tupleAssign.expr = ASTBuilderUtil.createVariableRef(pos, ctx.getLastOperation().retVar.symbol);
            tupleAssign.varRefs.addAll(ASTBuilderUtil.createVariableRefList(pos, ctx.iteratorResultVariables));
        }
        generateAggregator(foreachStmt.body, ctx);
        generateFinalResult(funcNode.body, ctx);
    }
    final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(firstOperation.pos, funcNode.body);
    if (isReturningIteratorFunction(ctx)) {
        returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) BLangTupleDestructure(org.wso2.ballerinalang.compiler.tree.statements.BLangTupleDestructure) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)

Example 10 with BLangForeach

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

the class BLangPackageBuilder method addForeachStatement.

public void addForeachStatement(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangForeach foreach = (BLangForeach) TreeBuilder.createForeachNode();
    foreach.addWS(ws);
    foreach.pos = pos;
    foreach.setCollection(exprNodeStack.pop());
    foreach.addWS(commaWsStack.pop());
    List<ExpressionNode> lExprList = exprNodeListStack.pop();
    lExprList.forEach(expressionNode -> foreach.addVariable((BLangVariableReference) expressionNode));
    BLangBlockStmt foreachBlock = (BLangBlockStmt) this.blockNodeStack.pop();
    foreachBlock.pos = pos;
    foreach.setBody(foreachBlock);
    addStmtToCurrentBlock(foreach);
}
Also used : BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)

Aggregations

BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)3 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)3 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)3 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)3 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangBracedOrTupleExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 Instruction (org.wso2.ballerinalang.programfile.Instruction)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1