rewrite bin/moon in moonscript

This commit is contained in:
leaf corcoran 2015-06-08 21:46:28 -07:00
parent e46fb70d70
commit 32239c8455
3 changed files with 229 additions and 104 deletions

View File

@ -14,6 +14,9 @@ global:
compile: compile:
lua5.1 bin/moonc moon/ moonscript/ lua5.1 bin/moonc moon/ moonscript/
echo "#!/usr/bin/env lua" > bin/moon
lua5.1 bin/moonc -p bin/moon.moon >> bin/moon
echo "-- vim: set filetype=lua:" >> bin/moon
watch: watch:
moonc moon/ moonscript/ && moonc -w moon/ moonscript/ moonc moon/ moonscript/ && moonc -w moon/ moonscript/

144
bin/moon
View File

@ -1,16 +1,13 @@
#!/usr/bin/env lua #!/usr/bin/env lua
require("alt_getopt")
require "alt_getopt" local moonscript = require("moonscript.base")
local moonscript = require "moonscript.base" local util = require("moonscript.util")
local errors = require("moonscript.errors")
local util = require "moonscript.util"
local errors = require "moonscript.errors"
local unpack = util.unpack local unpack = util.unpack
local opts, ind = alt_getopt.get_opts(arg, "cvhd", {
-- moonloader and repl version = "v",
local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" }) help = "h"
})
local help = [=[Usage: %s [options] [script [args]] local help = [=[Usage: %s [options] [script [args]]
-h Print this message -h Print this message
@ -18,89 +15,98 @@ local help = [=[Usage: %s [options] [script [args]]
-c Collect and print code coverage -c Collect and print code coverage
-v Print version -v Print version
]=] ]=]
local print_err
local function print_err(...) print_err = function(...)
local msg = table.concat({...}, "\t") local msg = table.concat((function(...)
io.stderr:write(msg .. "\n") 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")
end end
local print_help
local function print_help(err) print_help = function(err)
if err then print("Error: "..err) end help = help:format(arg[0])
print(help:format(arg[0])) if err then
print_err(err)
print_err(help)
else
print(help)
end
return os.exit()
end
local run
run = function()
if opts.h then
print_help()
end
if opts.v then
require("moonscript.version").print_version()
os.exit() os.exit()
end end
local script_fname = arg[ind]
if opts.h then print_help() end if not (script_fname) then
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") print_help("repl not yet supported")
return end
end local new_arg = {
local new_arg = {
[-1] = arg[0], [-1] = arg[0],
[0] = arg[ind], [0] = arg[ind],
select(ind + 1, unpack(arg)) select(ind + 1, unpack(arg))
} }
local moonscript_chunk, lua_parse_error
local moonscript_chunk, lua_parse_error local passed, err = pcall(function()
local passed, err = pcall(function() moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, {
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { implicitly_return_root = false }) implicitly_return_root = false
end) })
end)
if not passed then if not (passed) then
print_err(err) print_err(err)
os.exit(1) os.exit(1)
end end
if not (moonscript_chunk) then
if not moonscript_chunk then
if lua_parse_error then if lua_parse_error then
print_err(lua_parse_error) print_err(lua_parse_error)
else else
print_err("Can't find file: " .. script_fname) print_err("Can't file file: " .. tostring(script_fname))
end end
os.exit(1) os.exit(1)
end end
util.getfenv(moonscript_chunk).arg = new_arg
util.getfenv(moonscript_chunk).arg = new_arg local run_chunk
run_chunk = function()
local function run_chunk()
moonscript.insert_loader() moonscript.insert_loader()
moonscript_chunk(unpack(new_arg)) moonscript_chunk(unpack(new_arg))
moonscript.remove_loader() return moonscript.remove_loader()
end end
if opts.d then
if not opts.d then return run_chunk()
local err, trace end
local cov local err, trace, cov
if opts.c then if opts.c then
local coverage = require "moonscript.cmd.coverage" print("starting coverage")
local coverage = require("moonscript.cmd.coverage")
cov = coverage.CodeCoverage() cov = coverage.CodeCoverage()
cov:start() cov:start()
end end
xpcall(run_chunk, function(_err) xpcall(run_chunk, function(_err)
err = _err err = _err
trace = debug.traceback("", 2) trace = debug.traceback("", 2)
end) end)
if err then if err then
local truncated = errors.truncate_traceback(util.trim(trace)) local truncated = errors.truncate_traceback(util.trim(trace))
local rewritten = errors.rewrite_traceback(truncated, err) local rewritten = errors.rewrite_traceback(truncated, err)
if rewritten then if rewritten then
print_err(rewritten) return print_err(rewritten)
else else
-- faield to rewrite, show original return print_err(table.concat({
print_err(table.concat({
err, err,
util.trim(trace) util.trim(trace)
}, "\n")) }, "\n"))
@ -108,9 +114,9 @@ if not opts.d then
else else
if cov then if cov then
cov:stop() cov:stop()
cov:print_results() return cov:print_results()
end end
end end
else
run_chunk()
end end
return run()
-- vim: set filetype=lua:

116
bin/moon.moon Normal file
View File

@ -0,0 +1,116 @@
require "alt_getopt"
moonscript = require "moonscript.base"
util = require "moonscript.util"
errors = require "moonscript.errors"
unpack = util.unpack
opts, ind = alt_getopt.get_opts arg, "cvhd", {
version: "v"
help: "h"
}
help = [=[Usage: %s [options] [script [args]]
-h Print this message
-d Disable stack trace rewriting
-c Collect and print code coverage
-v Print version
]=]
print_err = (...) ->
msg = table.concat [tostring v for v in *{...}], "\t"
io.stderr\write msg .. "\n"
print_help = (err) ->
help = help\format arg[0]
if err
print_err err
print_err help
else
print help
os.exit!
run = ->
if opts.h
print_help!
if opts.v
require("moonscript.version").print_version!
os.exit!
script_fname = arg[ind]
unless script_fname
print_help "repl not yet supported"
new_arg = {
[-1]: arg[0],
[0]: arg[ind],
select ind + 1, unpack arg
}
local moonscript_chunk, lua_parse_error
passed, err = pcall ->
moonscript_chunk, lua_parse_error = moonscript.loadfile script_fname, {
implicitly_return_root: false
}
unless passed
print_err err
os.exit 1
unless moonscript_chunk
if lua_parse_error
print_err lua_parse_error
else
print_err "Can't file file: #{script_fname}"
os.exit 1
util.getfenv(moonscript_chunk).arg = new_arg
run_chunk = ->
moonscript.insert_loader!
moonscript_chunk unpack new_arg
moonscript.remove_loader!
if opts.d
return run_chunk!
local err, trace, cov
if opts.c
print "starting coverage"
coverage = require "moonscript.cmd.coverage"
cov = coverage.CodeCoverage!
cov\start!
xpcall run_chunk, (_err) ->
err = _err
trace = debug.traceback "", 2
if err
truncated = errors.truncate_traceback util.trim trace
rewritten = errors.rewrite_traceback truncated, err
if rewritten
print_err rewritten
else
-- failed to rewrite, show original
print_err table.concat {
err,
util.trim trace
}, "\n"
else
if cov
cov\stop!
cov\print_results!
run!