-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmoonscript.go
75 lines (60 loc) · 1.87 KB
/
gmoonscript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gmoonscript
import (
lua "github.com/yuin/gopher-lua"
)
// Compile compiles Moonscript code into Lua code using a Gopher-Lua
// VM. It is recommended that this method be called using an isolated
// Lua state, as it creates several globals that you may want to keep
// away from your program.
func Compile(state *lua.LState, moonscriptCode string) (string, error) {
moonbundle, err := Asset("moon-bundle.lua")
if err != nil {
return "", err
}
state.SetGlobal("_moonbundle_code", lua.LString(moonbundle))
state.SetGlobal("__moonscript_code", lua.LString(moonscriptCode))
err = state.DoString(`
package.loaded.moonscript = loadstring(_moonbundle_code)()
local moonparse = require("moonscript.parse")
local mooncompile = require("moonscript.compile")
local tree, err = moonparse.string(__moonscript_code)
if not tree then
print("gmoonscript error: unable to parse moonscript, check formatting!")
else
__output_lua_code_, err = mooncompile.tree(tree)
end
-- remove all created modules and vars
package.loaded.moonscript = nil
moonparse = nil
mooncompile = nil
_moonbundle_code = nil
__moonscript_code = nil
collectgarbage()
`)
if err != nil {
return "", err
}
luaOutput := state.GetGlobal("__output_lua_code_")
state.SetGlobal("__output_lua_code", lua.LNil)
return luaOutput.String(), nil
}
// Loader is the default stub used by the Gopher-Lua
// PreloadModule function to import a Go module.
func Loader(state *lua.LState) int {
mod := state.SetFuncs(state.NewTable(), map[string]lua.LGFunction{
"compile": func(L *lua.LState) int {
code := L.CheckString(1)
luaCode, err := Compile(L, code)
if err != nil {
state.Push(lua.LNil)
state.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(luaCode))
return 1
},
})
// returns the module
state.Push(mod)
return 1
}