Search in sources :

Example 1 with BCollection

use of org.ballerinalang.model.values.BCollection in project ballerina by ballerina-lang.

the class CPU method execIteratorOperation.

private static void execIteratorOperation(WorkerExecutionContext ctx, WorkerData sf, Instruction instruction) {
    int i, j;
    BCollection collection;
    BIterator iterator;
    InstructionIteratorNext nextInstruction;
    switch(instruction.getOpcode()) {
        case InstructionCodes.ITR_NEW:
            // collection
            i = instruction.getOperands()[0];
            // iterator variable (ref) index.
            j = instruction.getOperands()[1];
            collection = (BCollection) sf.refRegs[i];
            if (collection == null) {
                handleNullRefError(ctx);
                return;
            }
            sf.refRegs[j] = collection.newIterator();
            break;
        case InstructionCodes.ITR_HAS_NEXT:
            // iterator
            i = instruction.getOperands()[0];
            // boolean variable index to store has next result
            j = instruction.getOperands()[1];
            iterator = (BIterator) sf.refRegs[i];
            if (iterator == null) {
                sf.intRegs[j] = 0;
                return;
            }
            sf.intRegs[j] = iterator.hasNext() ? 1 : 0;
            break;
        case InstructionCodes.ITR_NEXT:
            nextInstruction = (InstructionIteratorNext) instruction;
            iterator = (BIterator) sf.refRegs[nextInstruction.iteratorIndex];
            if (iterator == null) {
                return;
            }
            BValue[] values = iterator.getNext(nextInstruction.arity);
            copyValuesToRegistries(nextInstruction.typeTags, nextInstruction.retRegs, values, sf);
            break;
    }
}
Also used : BValue(org.ballerinalang.model.values.BValue) InstructionIteratorNext(org.ballerinalang.util.codegen.Instruction.InstructionIteratorNext) BIterator(org.ballerinalang.model.values.BIterator) BCollection(org.ballerinalang.model.values.BCollection)

Aggregations

BCollection (org.ballerinalang.model.values.BCollection)1 BIterator (org.ballerinalang.model.values.BIterator)1 BValue (org.ballerinalang.model.values.BValue)1 InstructionIteratorNext (org.ballerinalang.util.codegen.Instruction.InstructionIteratorNext)1