开发者

CPS compiler for coroutine implementation

I used to work on IronLua in my spare time. Lexing and parsing is currently done. I kind of stopped working on it out of frustration since implementing Lua coroutines in .NET without resorting to dirty threading hacks is not easy. This is tied to how I compile Lua functions, and it's a problem I need to solve early while designing the compiler.

I've been researching coroutine implementations, and it turns out that my initial feelings about continuations were correct.

Since coroutine creation, yield and other operations are not language keywords, but functions in the "coroutine" table, I cannot statically switch to CPS-style compilation as the coroutine table might have been overwritten by a previous script. While I understand that scripts overwriting the coroutine table are a rare occurrence, I'd like to be on the safe side and approach the issue as cleanly as possible.

My plan is to use continuation-passing style for every expression, no matter if we're in a coroutine or not. Everything would be followed by a continuation.

Besides the obvious difficulty of w开发者_Python百科riting a compiler in the first place, and adding CPS transform on top of it, I'm troubled by this design decision and its performance implications.

I'm looking for advice about Lua coroutine implementation in .NET.

Thanks for your time.


I'm not familiar with the details of lua co-routines. But I think the only way you can get co-routine support on a similar level to what lua itself supports requires that all local variables are allocated on the heap in some form.

Your problem isn't only that the coroutine functions might be replaced, but that any lua function you call might yield.

You should also look into the async CTP which implements very similar behavior in C#. The main difference I see is that you need to make all methods async, while the C# feature is opt-in.

If your .net integration is good(and I expect it to be good if build on the DLR) then I wouldn't worry to much about performance. It's easy to write the performance critical parts in C# and use lua for glue code.


You may want to look more at the work done for Scala on .NET. Scala supports continuations as a compiler plugin.

Caveat: I have no clue on whether the continuations support works in .NET, or indeed how advanced the work on the compiler itself is. The person responsible for it is Miguel Garcia, you can find his address on the Scala Lang: The Scala Development Team

The following references may be useful:

Scala comes to .Net

The Scala Compiler Corner, for .NET and Mono fans

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜