moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Embedding?

Open ghost opened this issue 7 years ago • 10 comments

I'm using Lua for a Go-based project with a Go-based interpreter (so pure Lua only) and I found out about Moonscript.... I would love to use this syntax for my project but I would like to determine a simple way to embed the compiler or Lua library (preferably as a simple lua file) to simplify the build and distribution of this project. Any ideas?

ghost avatar Dec 20 '17 16:12 ghost

So I played with Amalgamation and other tools in attempt to compress this library into a single lua script. I ended up with something that works for lua and luajit but not for Gopher-Lua due to this projects dependency on LPeg (C based :-1:)... Would love to find a way around this. The goal is really to use a Lua runtime to run moonscript-based plugins without the developer having to touch any Lua code or a compilation toolchain.

ghost avatar Dec 21 '17 20:12 ghost

There's a lua version of lpeg you can try: https://github.com/pygy/LuLPeg

leafo avatar Dec 21 '17 20:12 leafo

@leafo Oh nice. I assume I would just need to manually edit the src to use this instead? P.S. Thx

ghost avatar Dec 21 '17 20:12 ghost

I don't know your specific requirements, but for all of my projects I build moonscript ahead of time and bundle the Lua code ready to be executed.

leafo avatar Dec 21 '17 20:12 leafo

Oh nice. I assume I would just need to manually edit the src to use this instead?

You may not have to edit anything, you could do package.loaded.lpeg = require("lulpeg") before any of MoonScript's code is loaded.

leafo avatar Dec 21 '17 20:12 leafo

I can understand that. Originally I thought that could work. The problem is that I am building a custom plugin system that runs these scripts. They aren't exactly native Lua applications, they use hooks and libraries provided by native Go code etc. This makes for a complex development process when attempting to use the native moon/lua toolchains.

ghost avatar Dec 21 '17 20:12 ghost

Fair point.. (I am still a bit new to Lua... I know more of the static languages)

ghost avatar Dec 21 '17 20:12 ghost

I'm going to try to get this working, Ill be back with updates. Thanks for all the help @leafo!

ghost avatar Dec 21 '17 20:12 ghost

I got this working for plain Lua and LuaJIT! Here is a zero-dep (I hope) moonscript compiler bundle. moon-bundle.zip

Here's a usage example:

package.loaded.moonscript = require("moon-bundle")

parse = require("moonscript.parse")
compile = require("moonscript.compile")

local f = assert(io.open("test.moon", "rb"))
local content = f:read("*all")
f:close()

tree, err = parse.string(content)
lua_code, err = compile.tree(tree)

print(lua_code)
luajit test.lua | luajit

ghost avatar Dec 21 '17 21:12 ghost

So this is where I am getting stuck with Gopher Lua... Not sure if this is something that has to do with features in Lua 5.3 vs 5.1 (which is supported by Gopher Lua)

# plugin git:master ● [^._.^]ノ彡ミ
$ glua testers.lua 
@/usr/share/lua/5.3/lulpeg.lua:1577: bad argument #1 to load (function expected, got string)
stack traceback:
	[G]: in function 'load'
	@/usr/share/lua/5.3/lulpeg.lua:1577: in function 'printers'
	@/usr/share/lua/5.3/lulpeg.lua:2839: in function 'LuLPeg'
	@/usr/share/lua/5.3/lulpeg.lua:2848: in function <@/usr/share/lua/5.3/lulpeg.lua:2794>
	@/usr/share/lua/5.3/lulpeg.lua:22: in function <@/usr/share/lua/5.3/lulpeg.lua:15>
	(tailcall): ?
	[G]: in function 'require'
	./moon-bundle.lua:2970: in function <./moon-bundle.lua:0>
	[G]: in function 'require'
	testers.lua:1: in main chunk
	[G]: ?

ghost avatar Dec 21 '17 22:12 ghost