At 18:52 07/10/2006 -0400, Bob Rogers wrote:
   From: François PERRAD <[EMAIL PROTECTED]>
   Date: Wed, 04 Oct 2006 08:55:34 +0200

   I've tried without success to implement coroutine in language Lua . . .
   Help is welcome.

   François.

I am not surprised that you have had difficulty.  I can't even get a
simple recursive coroutine to work in PIR.  See the first attachment for
an attempt.  I think this is a fundamental problem; I know nothing about
Lua, but I strongly suspect Parrot coroutines would have to be
redesigned to support what the Lua manual says about them [1].

   But that's not a show-stopper; continuations alone are powerful
enough to do most of what you need.  The second attachment is a demo
implementation based loosely on the Lua definitions of coroutine.create,
coroutine.resume, and coroutine.yield.  The first third of the code
defines PIR near-equivalents to these, and the rest includes several
demos.  The demo that runs by default (i.e. without editing the code) is
a solution to the "same fringe" problem [2] using a pair of recursive
coroutines to enumerate two trees simultaneously.

   However, the abstraction is poor.  This is partly because I was too
lazy to figure out whether an object-oriented solution would fail due to
the "continuation barrier" problem, and partly because I was too lazy to
make the "current coroutine" implicit, as it is in Lua [3].  Still, I
hope you find this useful.  Probably it ought to get turned into an
example . . .

Thank for your long response.

I included your code in languages/lua/lib/luacoroutine.pir (r14877).
I encountered 2 problems :
1) coroutine_yield needs a current coroutine (not surprising)
2) I want replace the LuaThread PMC by a PIR classe.
        So, I create a classe :
                $P0 = subclass 'FixedPMCArray', 'thread'
        But when I replace in coroutine_create
                coro = new .FixedPMCArray
                coro = 4
        by :
                find_type $I0, 'thread'
                coro = new $I0
                coro = 4
some test of languages/lua/t/coroutine.t fail with the following message :
                FixedPMCArray: index out of bounds!

François.

   Also, the "[oops; got 4 and X]" lines in the output seem to suggest
that Parrot may be getting confused about parameters.  The "4" is really
the length of one of the coro structures, which must be getting passed
as the first arg.  Also, it fails with "too many args" if you don't
accept at least two values from the coroutine_yield.  I suspect Parrot
is reusing some old parameter information; I'll try to nail this down if
I get a chance.

                                        -- Bob Rogers
                                           http://rgrjr.dyndns.org/

[1]  http://www.lua.org/manual/5.1/manual.html#2.11

[2]  http://www.nsl.com/papers/samefringe.htm, e.g.

[3]  This also depends on how the "current coroutine" should be scoped.
     Dynamic scoping seems reasonable, but that's not obvious from [1].


Reply via email to