leafo / moonscript

:crescent_moon: A language that compiles to Lua
https://moonscript.org
3.19k stars 190 forks source link

Embedding? #349

Open ghost opened 6 years ago

ghost commented 6 years ago

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 commented 6 years ago

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.

leafo commented 6 years ago

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

ghost commented 6 years ago

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

leafo commented 6 years ago

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 commented 6 years ago

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.

ghost commented 6 years ago

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 commented 6 years ago

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

ghost commented 6 years ago

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

ghost commented 6 years ago

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 commented 6 years ago

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]: ?