-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathmoon.moon
More file actions
116 lines (86 loc) · 2.15 KB
/
moon.moon
File metadata and controls
116 lines (86 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
alt_getopt = require "alt_getopt"
moonscript = require "moonscript.base"
util = require "moonscript.util"
errors = require "moonscript.errors"
unpack = util.unpack
opts, ind = alt_getopt.get_opts _G.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 _G.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 = _G.arg[ind]
unless script_fname
print_help "repl not yet supported"
new_arg = {
[-1]: _G.arg[0],
[0]: _G.arg[ind],
select ind + 1, unpack _G.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!