moonscript/bin/moon
leaf corcoran 340498d5d1 use xpcall instead of coroutine when running code through bin/moon
moon will longer swallow errors if you tried to yield from the top scope
2012-09-07 22:31:11 -07:00

73 lines
1.4 KiB
Lua
Executable File

#!/usr/bin/env lua
require "alt_getopt"
require "moonscript.errors"
require "moonscript"
local util = require "moonscript.util"
-- moonloader and repl
local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" })
local help = [=[Usage: %s [options] [script [args]]
-h Print this message
-d Disable stack trace rewriting
-v Print version
]=]
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
if opts.v then
local v = require "moonscript.version"
v.print_version()
os.exit()
end
local script_fname = arg[ind]
if not script_fname then
print_help("repl not yet supported")
return
end
local new_arg = {
[-1] = arg[0],
[0] = arg[ind],
select(ind + 1, unpack(arg))
}
local moonscript_chunk
local passed, err = pcall(function()
moonscript_chunk = moonscript.loadfile(script_fname)
end)
if not passed then
print(err)
os.exit(1)
end
getfenv(moonscript_chunk).arg = new_arg
if not opts.d then
local err, trace
xpcall(function() moonscript_chunk(unpack(new_arg)) end, function(_err)
err = _err
trace = debug.traceback("", 2)
end)
if err then
local traceback = util.trim(trace)
traceback = moonscript.errors.truncate_traceback(traceback)
print(moonscript.errors.rewrite_traceback(traceback, err))
os.exit(1)
end
else
moonscript_chunk(unpack(new_arg))
end