2011-08-13 04:07:44 +00:00
|
|
|
#!/usr/bin/env lua
|
2011-06-20 01:47:47 +00:00
|
|
|
|
|
|
|
require "alt_getopt"
|
2013-10-06 19:17:17 +00:00
|
|
|
local moonscript = require "moonscript.base"
|
2011-06-20 01:47:47 +00:00
|
|
|
|
2012-09-08 05:25:17 +00:00
|
|
|
local util = require "moonscript.util"
|
2012-11-28 08:24:58 +00:00
|
|
|
local errors = require "moonscript.errors"
|
2012-09-08 05:25:17 +00:00
|
|
|
|
2013-01-03 01:55:43 +00:00
|
|
|
local unpack = util.unpack
|
|
|
|
|
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
|
2013-10-08 07:14:14 +00:00
|
|
|
-c Collect and print code coverage
|
2011-07-19 06:53:43 +00:00
|
|
|
-v Print version
|
2011-06-20 01:47:47 +00:00
|
|
|
]=]
|
|
|
|
|
2012-09-08 17:34:14 +00:00
|
|
|
local function print_err(...)
|
2012-10-31 03:23:36 +00:00
|
|
|
local msg = table.concat({...}, "\t")
|
2012-09-08 17:34:14 +00:00
|
|
|
io.stderr:write(msg .. "\n")
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2012-09-08 17:37:50 +00:00
|
|
|
local moonscript_chunk, lua_parse_error
|
2011-11-05 01:10:16 +00:00
|
|
|
local passed, err = pcall(function()
|
2012-10-31 03:23:36 +00:00
|
|
|
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { implicitly_return_root = false })
|
2011-11-05 01:10:16 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
if not passed then
|
2012-09-08 17:34:14 +00:00
|
|
|
print_err(err)
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not moonscript_chunk then
|
2012-09-08 17:37:50 +00:00
|
|
|
if lua_parse_error then
|
|
|
|
print_err(lua_parse_error)
|
|
|
|
else
|
|
|
|
print_err("Can't find file: " .. script_fname)
|
|
|
|
end
|
2012-09-08 05:25:17 +00:00
|
|
|
os.exit(1)
|
2011-11-05 01:10:16 +00:00
|
|
|
end
|
|
|
|
|
2013-01-03 01:32:00 +00:00
|
|
|
util.getfenv(moonscript_chunk).arg = new_arg
|
2012-09-08 05:25:17 +00:00
|
|
|
|
2013-10-06 19:17:17 +00:00
|
|
|
local function run_chunk()
|
|
|
|
moonscript.insert_loader()
|
|
|
|
moonscript_chunk(unpack(new_arg))
|
|
|
|
moonscript.remove_loader()
|
|
|
|
end
|
|
|
|
|
2012-09-08 05:25:17 +00:00
|
|
|
if not opts.d then
|
|
|
|
local err, trace
|
2013-10-08 07:14:14 +00:00
|
|
|
local cov
|
|
|
|
|
|
|
|
if opts.c then
|
|
|
|
local coverage = require "moonscript.cmd.coverage"
|
|
|
|
cov = coverage.CodeCoverage()
|
|
|
|
cov:start()
|
|
|
|
end
|
2012-10-31 03:23:36 +00:00
|
|
|
|
2013-10-06 19:17:17 +00:00
|
|
|
xpcall(run_chunk, function(_err)
|
2012-09-08 05:25:17 +00:00
|
|
|
err = _err
|
|
|
|
trace = debug.traceback("", 2)
|
|
|
|
end)
|
2011-07-04 16:02:17 +00:00
|
|
|
|
2012-09-08 05:25:17 +00:00
|
|
|
if err then
|
2012-11-28 08:24:58 +00:00
|
|
|
local truncated = errors.truncate_traceback(util.trim(trace))
|
|
|
|
local rewritten = errors.rewrite_traceback(truncated, err)
|
2012-10-31 03:23:36 +00:00
|
|
|
|
|
|
|
if rewritten then
|
|
|
|
print_err(rewritten)
|
|
|
|
else
|
|
|
|
-- faield to rewrite, show original
|
|
|
|
print_err(table.concat({
|
|
|
|
err,
|
|
|
|
util.trim(trace)
|
|
|
|
}, "\n"))
|
|
|
|
end
|
2013-10-08 07:14:14 +00:00
|
|
|
else
|
|
|
|
if cov then
|
|
|
|
cov:stop()
|
|
|
|
end
|
2011-07-19 04:07:03 +00:00
|
|
|
end
|
2012-09-08 05:25:17 +00:00
|
|
|
else
|
2013-10-06 19:17:17 +00:00
|
|
|
run_chunk()
|
2011-07-04 16:02:17 +00:00
|
|
|
end
|