CPS compiler for coroutine implementation

I worked at IronLua in my free time. Lexing and parsing are currently performed. I somehow stopped working on this because of disappointment, since the implementation of Lua coroutines in .NET, without resorting to dirty streaming hacks, is not easy. This is due to the way I compile Lua functions, and this is a problem that I have to solve early when developing the compiler.

I study the implementation of coroutine, and it turns out that my initial feelings about the continuation were correct.

Since creating coroutine, yield, and other operations are not language keywords, but functions in the "coroutine" table, I cannot statically switch to CPS-style compilation, since the coprocessor table could be overwritten by the previous script. Although I understand that scripts rewriting a coroutine table are rare, I would like to be safe and approach the problem as clean as possible.

My plan is to use a continuation style for each expression, regardless of whether we are in a coroutine or not. It will be followed by a sequel.

Besides the obvious difficulty of writing a compiler in the first place and adding a CPS transformation on top of it, I am concerned about this design decision and its performance implications.

I am looking for advice on implementing Lua coroutine in .NET.

Thank you for your time.

+6
source share
2 answers

I am not familiar with the details of lua co-routines. But I think that the only way to get collaborative support at the same level as lua support itself requires that all local variables be allocated on the heap in one form or another.

Your problem is not only that coroutine functions can be replaced, but that any lua function that you call can give.

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

If your .net integration is good (and I expect it to be good if you build on DLR), then I would not worry about performance. It is easy to write critical performance components in C # and use lua for glue code.

+1
source

You can take a closer look at the work done for Scala on .NET. Scala supports sequels as a compiler plugin.

Caveat: I don't know if support for continuation in .NET supports it, or really how advanced the work on the compiler itself is. Miguel Garcia is responsible for this, you can find his address on Scala Lang: Scala Development Team

The following links are useful:

Scala comes to .Net

Scala Compiler Angle for .NET and Mono Fans

0
source

Source: https://habr.com/ru/post/890752/


All Articles