moonscript/bin/moon

123 lines
2.8 KiB
Plaintext
Raw Normal View History

2011-08-13 04:07:44 +00:00
#!/usr/bin/env lua
2015-06-09 04:46:28 +00:00
require("alt_getopt")
local moonscript = require("moonscript.base")
local util = require("moonscript.util")
local errors = require("moonscript.errors")
2013-01-03 01:55:43 +00:00
local unpack = util.unpack
2015-06-09 04:46:28 +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
]=]
2015-06-09 04:46:28 +00:00
local print_err
print_err = function(...)
local msg = table.concat((function(...)
local _accum_0 = { }
local _len_0 = 1
local _list_0 = {
...
}
for _index_0 = 1, #_list_0 do
local v = _list_0[_index_0]
_accum_0[_len_0] = tostring(v)
_len_0 = _len_0 + 1
end
return _accum_0
end)(...), "\t")
return io.stderr:write(msg .. "\n")
2011-06-20 01:47:47 +00:00
end
2015-06-09 04:46:28 +00:00
local print_help
print_help = function(err)
help = help:format(arg[0])
if err then
print_err(err)
print_err(help)
else
print(help)
end
return os.exit()
end
2015-06-09 04:46:28 +00:00
local run
run = function()
if opts.h then
print_help()
end
if opts.v then
require("moonscript.version").print_version()
os.exit()
end
local script_fname = arg[ind]
if not (script_fname) then
print_help("repl not yet supported")
end
local new_arg = {
[-1] = arg[0],
[0] = arg[ind],
select(ind + 1, unpack(arg))
}
local moonscript_chunk, lua_parse_error
local passed, err = pcall(function()
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, {
implicitly_return_root = false
})
end)
if not (passed) then
print_err(err)
os.exit(1)
end
if not (moonscript_chunk) then
if lua_parse_error then
print_err(lua_parse_error)
else
print_err("Can't file file: " .. tostring(script_fname))
end
os.exit(1)
end
util.getfenv(moonscript_chunk).arg = new_arg
local run_chunk
run_chunk = function()
moonscript.insert_loader()
moonscript_chunk(unpack(new_arg))
return moonscript.remove_loader()
end
if opts.d then
return run_chunk()
end
local err, trace, cov
if opts.c then
print("starting coverage")
local coverage = require("moonscript.cmd.coverage")
cov = coverage.CodeCoverage()
cov:start()
end
xpcall(run_chunk, function(_err)
err = _err
trace = debug.traceback("", 2)
end)
if err then
local truncated = errors.truncate_traceback(util.trim(trace))
local rewritten = errors.rewrite_traceback(truncated, err)
if rewritten then
return print_err(rewritten)
else
return print_err(table.concat({
err,
util.trim(trace)
}, "\n"))
end
else
if cov then
cov:stop()
return cov:print_results()
end
end
2011-07-04 16:02:17 +00:00
end
2015-06-09 04:46:28 +00:00
return run()
-- vim: set filetype=lua: