forked from franko/luajit-lang-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 16
/
run.lua
74 lines (66 loc) · 1.75 KB
/
run.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
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
local function usage()
io.stderr:write[[
LuaJIT Language Toolkit usage: luajit [options]... [script [args]...].
Available options are:
-b ... Save or list bytecode.
-c ... Generate Lua code and run.
If followed by the "v" option the generated Lua code
will be printed.
]]
os.exit(1)
end
local function check(success, result)
if not success then
io.stderr:write(result .. "\n")
os.exit(1)
else
return result
end
end
local filename
local args = {...}
local opt = {}
local k = 1
while args[k] do
local a = args[k]
if string.sub(args[k], 1, 1) == "-" then
if string.sub(a, 2, 2) == "b" then
local j = 1
if #a > 2 then
args[j] = "-" .. string.sub(a, 3)
j = j + 1
else
table.remove(args, j)
end
require("lang.bcsave").start(unpack(args))
os.exit(0)
elseif string.sub(a, 2, 2) == "c" then
opt.code = true
local copt = string.sub(a, 3, 3)
if copt == "v" then
opt.debug = true
elseif copt ~= "" then
print("Invalid Lua code option: ", copt)
usage()
end
elseif string.sub(a, 2, 2) == "v" then
opt.debug = true
else
print("Invalid option: ", args[k])
usage()
end
else
filename = args[k]
end
k = k + 1
end
if not filename then usage() end
local compile = require("lang.compile")
-- Compute the bytecode string for the given filename.
local luacode = check(compile.file(filename, opt))
if opt.debug then
print(luacode)
print('\n\nOutput:')
end
local fn = assert(loadstring(luacode))
fn()