#!/usr/bin/env lua require "alt_getopt" local moonscript = require "moonscript" 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 help = [=[Usage: %s [options] [script [args]] -h Print this message -d Disable stack trace rewriting -v Print version ]=] local function print_err(...) local msg = table.concat({...}, "\t") 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() 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 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 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 end else moonscript_chunk(unpack(new_arg)) end