Coroutine & generator

François Perrad francois.perrad at gadz.org
Sun Apr 11 16:10:01 UTC 2010


2010/4/11 François Perrad <francois.perrad at gadz.org>:
> I try to refactor runtime/parrot/library/TAP/parser.pir with a generator
> (see attached patch)
>
> this naive implementation :
>    .sub '_get_results'
>        .param pmc parser
>        .local pmc result
>        result = new 'ResizablePMCArray'
>      L1:
>        $P0 = parser.'next'()
>        if null $P0 goto L2
>        diag($P0)
>        push result, $P0
>        goto L1
>      L2:
>        .return (result)
>    .end
> cannot work with more than 1 file.
> And fails at the beginning of second file with the message :
>    Cannot resume dead coroutine.
>

This behaviour is fine for me.
I don't like the idea of a silently restart.

> this other implementation with a Coroutine PMC :
>    .sub '_get_results'
>        .param pmc parser
>        .local pmc result
>        result = new 'ResizablePMCArray'
>        $P0 = get_hll_global ['TAP';'Parser'], 'next'
>        .local pmc coro
>        coro = new 'Coroutine', $P0
>      L1:
>        $P0 = coro(parser)
>        if null $P0 goto L2
>        diag($P0)
>        push result, $P0
>        goto L1
>      L2:
>        .return (result)
>    .end
> fails with the message :
>    exists_keyed_str() not implemented in class 'Coroutine'
> Something seems to be broken and parrot lacks of tests for this part.
>

Now, when I instanciate explicity a Coroutine
    const 'Sub' func = 'my_coro'
    $P0 = new 'Coroutine', func
I want each time, a 'new' coroutine in its initial state.

Another idea is cloning the coroutine before using it, and consume
only the clone.
That's work with the attached sample.
But gives a new error with TAP/Parser which uses a lexical 'state'.

    .sub '_get_results'
        .param pmc parser
        .local pmc result
        result = new 'ResizablePMCArray'
        $P0 = get_hll_global ['TAP';'Parser'], 'next'
        .local pmc coro
        coro = clone $P0
      L1:
        $P0 = coro(parser)
        if null $P0 goto L2
        diag($P0)
        push result, $P0
        goto L1
      L2:
        .return (result)
    .end


François

> Any idea or help is welcome.
>
> François
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: coro.pir
Type: application/octet-stream
Size: 790 bytes
Desc: not available
URL: <http://lists.parrot.org/pipermail/parrot-dev/attachments/20100411/8dd8bf7c/attachment.obj>


More information about the parrot-dev mailing list