From 32239c8455611bf8e9d69f87c894bd34427bd3ca Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 8 Jun 2015 21:46:28 -0700 Subject: [PATCH] rewrite bin/moon in moonscript --- Makefile | 3 + bin/moon | 214 ++++++++++++++++++++++++++------------------------ bin/moon.moon | 116 +++++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 104 deletions(-) create mode 100644 bin/moon.moon diff --git a/Makefile b/Makefile index 9a44cfe..13a1eba 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ global: compile: 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: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ diff --git a/bin/moon b/bin/moon index b6ef27f..a4892bd 100755 --- a/bin/moon +++ b/bin/moon @@ -1,16 +1,13 @@ #!/usr/bin/env lua - -require "alt_getopt" -local moonscript = require "moonscript.base" - -local util = require "moonscript.util" -local errors = require "moonscript.errors" - +require("alt_getopt") +local moonscript = require("moonscript.base") +local util = require("moonscript.util") +local errors = require("moonscript.errors") local unpack = util.unpack - --- moonloader and repl -local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" }) - +local opts, ind = alt_getopt.get_opts(arg, "cvhd", { + version = "v", + help = "h" +}) local help = [=[Usage: %s [options] [script [args]] -h Print this message @@ -18,99 +15,108 @@ local help = [=[Usage: %s [options] [script [args]] -c Collect and print code coverage -v Print version ]=] - -local function print_err(...) - local msg = table.concat({...}, "\t") - io.stderr:write(msg .. "\n") +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") end - -local function print_help(err) - if err then print("Error: "..err) end - print(help:format(arg[0])) - os.exit() +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 - -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, 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 find file: " .. script_fname) - end - os.exit(1) -end - -util.getfenv(moonscript_chunk).arg = new_arg - -local function run_chunk() - moonscript.insert_loader() - moonscript_chunk(unpack(new_arg)) - moonscript.remove_loader() -end - -if not opts.d then - local err, trace - local cov - - if opts.c then - 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 - print_err(rewritten) - else - -- faield to rewrite, show original - print_err(table.concat({ - err, - util.trim(trace) - }, "\n")) - end - else - if cov then - cov:stop() - cov:print_results() - end - end -else - run_chunk() +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 end +return run() +-- vim: set filetype=lua: diff --git a/bin/moon.moon b/bin/moon.moon new file mode 100644 index 0000000..853cf19 --- /dev/null +++ b/bin/moon.moon @@ -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!