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/

214
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,99 +15,108 @@ 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
os.exit() print_err(err)
print_err(help)
else
print(help)
end
return os.exit()
end end
local run
if opts.h then print_help() end run = function()
if opts.h then
if opts.v then print_help()
local v = require "moonscript.version" end
v.print_version() if opts.v then
os.exit() require("moonscript.version").print_version()
end os.exit()
end
local script_fname = arg[ind] local script_fname = arg[ind]
if not script_fname then if not (script_fname) then
print_help("repl not yet supported") print_help("repl not yet supported")
return end
end local new_arg = {
[-1] = arg[0],
local new_arg = { [0] = arg[ind],
[-1] = arg[0], select(ind + 1, unpack(arg))
[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, {
local moonscript_chunk, lua_parse_error implicitly_return_root = false
local passed, err = pcall(function() })
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { implicitly_return_root = false }) end)
end) if not (passed) then
print_err(err)
if not passed then os.exit(1)
print_err(err) end
os.exit(1) if not (moonscript_chunk) then
end if lua_parse_error then
print_err(lua_parse_error)
if not moonscript_chunk then else
if lua_parse_error then print_err("Can't file file: " .. tostring(script_fname))
print_err(lua_parse_error) end
else os.exit(1)
print_err("Can't find file: " .. script_fname) end
end util.getfenv(moonscript_chunk).arg = new_arg
os.exit(1) local run_chunk
end run_chunk = function()
moonscript.insert_loader()
util.getfenv(moonscript_chunk).arg = new_arg moonscript_chunk(unpack(new_arg))
return moonscript.remove_loader()
local function run_chunk() end
moonscript.insert_loader() if opts.d then
moonscript_chunk(unpack(new_arg)) return run_chunk()
moonscript.remove_loader() end
end local err, trace, cov
if opts.c then
if not opts.d then print("starting coverage")
local err, trace local coverage = require("moonscript.cmd.coverage")
local cov cov = coverage.CodeCoverage()
cov:start()
if opts.c then end
local coverage = require "moonscript.cmd.coverage" xpcall(run_chunk, function(_err)
cov = coverage.CodeCoverage() err = _err
cov:start() trace = debug.traceback("", 2)
end end)
if err then
xpcall(run_chunk, function(_err) local truncated = errors.truncate_traceback(util.trim(trace))
err = _err local rewritten = errors.rewrite_traceback(truncated, err)
trace = debug.traceback("", 2) if rewritten then
end) return print_err(rewritten)
else
if err then return print_err(table.concat({
local truncated = errors.truncate_traceback(util.trim(trace)) err,
local rewritten = errors.rewrite_traceback(truncated, err) util.trim(trace)
}, "\n"))
if rewritten then end
print_err(rewritten) else
else if cov then
-- faield to rewrite, show original cov:stop()
print_err(table.concat({ return cov:print_results()
err, end
util.trim(trace) end
}, "\n"))
end
else
if cov then
cov:stop()
cov:print_results()
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!