Live data from Hacker News

A Lua interpreter written in C#

moonsharp.org

11–20 of 20 posts

Re: A Lua interpreter written in C#

#11
I was looking at doing this awhile back by improving Kopilua (which had looked to be abandoned, and now seems to have a little bit of activity again):

https://github.com/NLua/KopiLua

However, other projects came up.

My interest was in a c# implementation of Lua where scripts would run in a cross-platform manner. I'm not sure the "addititions"/"modifications" in Moon# will meet those needs. If I return to said project, I will definitely take a look.

Re: A Lua interpreter written in C#

#12
post #10
post #9

Earlier quoted context omitted.

This project seems to be faithfully implementing Lua, as it is. Apparently, for the same range of uses as for the original Lua. Which includes DSLs and all that. As for the goto being essential: first, FSMs. They cannot be implemented more naturally and efficiently with anything else. Huge switch can be costly. Secondly, higher level DSLs may need goto to represent features not directly available in the host language…

> FSMs. They cannot be implemented more naturally and efficiently with anything else. It depends a bit on what your motivation for using a Lua interpreter embedded into the .NET environment is. Instinctively, it wouldn't be my choice for things that need to be exceedingly fast. But let's say you do want to implement an FSM in this context where performance isn't critical. States and state transitions could very well…

It is all a question of implementation.

I am on mobile, so cannot check easily the current implementation.

However it can be translated into MSIL, make use of the dynamic language runtime or eventually use the upcoming .NET Native infrastructure when it becomes available.

Re: A Lua interpreter written in C#

#13
post #10
post #9

Earlier quoted context omitted.

This project seems to be faithfully implementing Lua, as it is. Apparently, for the same range of uses as for the original Lua. Which includes DSLs and all that. As for the goto being essential: first, FSMs. They cannot be implemented more naturally and efficiently with anything else. Huge switch can be costly. Secondly, higher level DSLs may need goto to represent features not directly available in the host language…

> FSMs. They cannot be implemented more naturally and efficiently with anything else. It depends a bit on what your motivation for using a Lua interpreter embedded into the .NET environment is. Instinctively, it wouldn't be my choice for things that need to be exceedingly fast. But let's say you do want to implement an FSM in this context where performance isn't critical. States and state transitions could very well…

Well, it's not only about efficiency. It's also about semantics. Goto semantics is cleaner in the cases I'm talking about, and any workaround (tables of functions, etc.) would be just clumsy and less maintainable. I know, there is a knee-jerk reaction on goto that it supposedly always leads to unmaintainable spagetty, but in fact it often helps to write a cleaner code, especially if it is not used directly, but generated by a higher level compiler for the high level language entities (FSMs, nested parsing blocks, etc.)

And Lua is not that bad at all for an interpreted language, it's fairly fast (especially the LuaJIT implementation), and it is possible to implement a nice compiled or semi-compiled Lua backend on top of .net. So it is reasonable to expect that it can serve as a backend for compiling higher level languages, as well as for the usual simple scripting.

Re: A Lua interpreter written in C#

#16
This is pretty rad. I had hopes for Kopilua and Aluminum Lua, but neither panned out. If I go back to Unity, I'm gonna use this. Thanks for open-sourcing it!

For now, I actually use CSharpCodeProvider and compile C# as "scripts" during game start (and cache it for future use), searching across attributes to find the right item. When deploying to platforms that don't have CSharpCodeProvider (Xamarin.iOS, for example), I can precompile the scripts, pack the assemblies, and the fallback is trivial.

Re: A Lua interpreter written in C#

#19
post #9
post #8

Earlier quoted context omitted.

Again, I don't think being a target for a higher level DSL is the motivation for this project. But let's talk about content instead: what are, in your opinion, the essential uses of goto in a language that has strong functional features and coroutines?

This project seems to be faithfully implementing Lua, as it is. Apparently, for the same range of uses as for the original Lua. Which includes DSLs and all that. As for the goto being essential: first, FSMs. They cannot be implemented more naturally and efficiently with anything else. Huge switch can be costly. Secondly, higher level DSLs may need goto to represent features not directly available in the host language…

> As for the goto being essential: first, FSMs. They cannot be implemented more naturally and efficiently with anything else.

Lua has proper tail calls, which are the natural way to express FSMs there. Programming in Lua actually uses an FSM as an example when explaining the motivation behind tail calls:

http://www.lua.org/pil/6.3.html

Re: A Lua interpreter written in C#

#20
post #4

Looks nice. What I miss from most libraries that let you use a language within a language, is the ability to blacklist the whole standard library and then whitelist specific parts of it. Does this support that?

It's probably less needed here since Lua's standard library is already pretty lean.
Post reply on HN