forked from richardhundt/shine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.lua
44 lines (35 loc) · 977 Bytes
/
compiler.lua
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
local parser = require('parser')
local transformer = require('transformer')
local generator = require('generator')
local util = require('util')
local function compile(src, name, opts)
local srctree = parser.parse(src)
if opts and opts['-p'] then
print("AST:", util.dump(srctree))
end
local dsttree = transformer.transform(srctree, src)
if opts and opts['-t'] then
print("DST:", util.dump(dsttree))
end
local luacode
if opts and opts['-s'] then
luacode = generator.source(dsttree, name)
print(luacode)
else
luacode = generator.bytecode(dsttree, name)
end
if opts and opts['-o'] then
local outfile = io.open(opts['-o'], "w+")
outfile:write(luacode)
outfile:close()
end
if opts and opts['-b'] then
local jbc = require("jit.bc")
local fn = assert(loadstring(luacode))
jbc.dump(fn, nil, true)
end
return luacode
end
return {
compile = compile
}