2011-08-13 04:07:44 +00:00
|
|
|
#!/usr/bin/env lua
|
2011-06-20 01:47:47 +00:00
|
|
|
|
|
|
|
require "alt_getopt"
|
2011-07-19 06:17:45 +00:00
|
|
|
require "moonscript.errors"
|
|
|
|
require "moonscript"
|
2011-06-20 01:47:47 +00:00
|
|
|
|
|
|
|
-- moonloader and repl
|
2011-07-19 06:53:43 +00:00
|
|
|
local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" })
|
2011-06-20 01:47:47 +00:00
|
|
|
|
|
|
|
local help = [=[Usage: %s [options] [script [args]]
|
|
|
|
|
|
|
|
-h Print this message
|
2011-07-19 04:07:03 +00:00
|
|
|
-d Disable stack trace rewriting
|
2011-07-19 06:53:43 +00:00
|
|
|
-v Print version
|
2011-06-20 01:47:47 +00:00
|
|
|
]=]
|
|
|
|
|
|
|
|
local function print_help(err)
|
|
|
|
if err then print("Error: "..err) end
|
|
|
|
print(help:format(arg[0]))
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.h then print_help() end
|
|
|
|
|
2011-07-19 06:53:43 +00:00
|
|
|
if opts.v then
|
|
|
|
local v = require "moonscript.version"
|
|
|
|
v.print_version()
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-07-19 06:17:45 +00:00
|
|
|
local script_fname = arg[ind]
|
|
|
|
if not script_fname then
|
2011-06-20 01:47:47 +00:00
|
|
|
print_help("repl not yet supported")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2011-06-20 02:14:20 +00:00
|
|
|
local new_arg = {
|
|
|
|
[-1] = arg[0],
|
|
|
|
[0] = arg[ind],
|
|
|
|
select(ind + 1, unpack(arg))
|
|
|
|
}
|
2011-06-20 01:47:47 +00:00
|
|
|
|
2011-11-05 01:10:16 +00:00
|
|
|
local chunk
|
|
|
|
local passed, err = pcall(function()
|
|
|
|
chunk = moonscript.loadfile(script_fname)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if not passed then
|
|
|
|
print(err)
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
2011-06-20 01:47:47 +00:00
|
|
|
getfenv(chunk).arg = new_arg
|
2011-07-04 16:02:17 +00:00
|
|
|
|
|
|
|
local runner = coroutine.create(chunk)
|
|
|
|
local success, err = coroutine.resume(runner, unpack(new_arg))
|
|
|
|
if not success then
|
2011-07-19 04:07:03 +00:00
|
|
|
local trace = debug.traceback(runner)
|
|
|
|
if not opts.d then
|
2011-07-19 06:17:45 +00:00
|
|
|
print(moonscript.errors.rewrite_traceback(trace, err))
|
2011-07-19 04:07:03 +00:00
|
|
|
else
|
|
|
|
print(trace)
|
|
|
|
end
|
2011-07-04 16:02:17 +00:00
|
|
|
end
|