-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathmoonc
More file actions
executable file
·263 lines (219 loc) · 5.71 KB
/
moonc
File metadata and controls
executable file
·263 lines (219 loc) · 5.71 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env lua
local alt_getopt = require "alt_getopt"
local lfs = require "lfs"
local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", {
print = "p", tree = "T", version = "v", help = "h", lint = "l"
})
local read_stdin = arg[1] == "--"
local help = [[Usage: %s [options] files/directories...
-h Print this message
-w Watch file/directory
-t path Specify where to place compiled files
-o file Write output to file
-p Write output to standard out
-T Write parse tree instead of code (to stdout)
-X Write line rewrite map instead of code (to stdout)
-l Perform lint on the file instead of compiling
-b Dump parse and compile time (doesn't write output)
-v Print version
-n Keep line numbers so that every output line maps a input line
-- Read from standard in, print to standard out
(Must be first and only argument)
]]
if opts.v then
local v = require "moonscript.version"
v.print_version()
os.exit()
end
function print_help(err)
local help_msg = help:format(arg[0])
if err then
io.stderr:write("Error: ".. err .. "\n")
io.stderr:write(help_msg .. "\n")
os.exit(1)
else
print(help_msg)
os.exit(0)
end
end
function log_msg(...)
if not opts.p then
io.stderr:write(table.concat({...}, " ") .. "\n")
end
end
local moonc = require("moonscript.cmd.moonc")
local util = require "moonscript.util"
local mkdir = moonc.mkdir
local normalize_dir = moonc.normalize_dir
local parse_dir = moonc.parse_dir
local parse_file = moonc.parse_file
local compile_and_write = moonc.compile_and_write
local path_to_target = moonc.path_to_target
local function scan_directory(root, collected)
root = normalize_dir(root)
collected = collected or {}
for fname in lfs.dir(root) do
if not fname:match("^%.") then
local full_path = root..fname
if lfs.attributes(full_path, "mode") == "directory" then
scan_directory(full_path, collected)
elseif fname:match("%.moon$") then
table.insert(collected, full_path)
end
end
end
return collected
end
local function remove_dups(tbl, key_fn)
local hash = {}
local final = {}
for _, v in ipairs(tbl) do
local dup_key = key_fn and key_fn(v) or v
if not hash[dup_key] then
table.insert(final, v)
hash[dup_key] = true
end
end
return final
end
-- creates tuples of input and target
local function get_files(fname, files)
files = files or {}
if lfs.attributes(fname, "mode") == "directory" then
for _, sub_fname in ipairs(scan_directory(fname)) do
table.insert(files, {
sub_fname,
path_to_target(sub_fname, opts.t, fname)
})
end
else
table.insert(files, {
fname,
path_to_target(fname, opts.t)
})
end
return files
end
if opts.h then
print_help()
end
if read_stdin then
local parse = require "moonscript.parse"
local compile = require "moonscript.compile"
local text = io.stdin:read("*a")
local tree, err = parse.string(text)
if not tree then error(err) end
local code, err, pos = compile.tree(tree)
if not code then
error(compile.format_error(err, pos, text))
end
print(code)
os.exit()
end
local inputs = {}
for i = ind, #arg do
table.insert(inputs, arg[i])
end
if #inputs == 0 then
print_help("No files specified")
end
local files = {}
for _, input in ipairs(inputs) do
get_files(input, files)
end
files = remove_dups(files, function(f)
return f[2]
end)
if opts.o and #files > 1 then
print_help("-o can not be used with multiple input files")
end
-- returns an iterator that returns files that have been updated
local function create_watcher(files)
local watchers = require("moonscript.cmd.watchers")
if watchers.InotifyWacher:available() then
return watchers.InotifyWacher(files):each_update()
end
return watchers.SleepWatcher(files):each_update()
end
if opts.w then
-- build function to check for lint or compile in watch
local handle_file
if opts.l then
local lint = require "moonscript.cmd.lint"
handle_file = lint.lint_file
else
handle_file = compile_and_write
end
local watcher = create_watcher(files)
-- catches interrupt error for ctl-c
local protected = function()
local status, file = true, watcher()
if status then
return file
elseif file ~= "interrupted!" then
error(file)
end
end
for fname in protected do
local target = path_to_target(fname, opts.t)
if opts.o then
target = opts.o
end
local success, err = handle_file(fname, target)
if opts.l then
if success then
io.stderr:write(success .. "\n\n")
elseif err then
io.stderr:write(fname .. "\n" .. err .. "\n\n")
end
elseif not success then
io.stderr:write(table.concat({
"",
"Error: " .. fname,
err,
"\n",
}, "\n"))
elseif success == "build" then
log_msg("Built", fname, "->", target)
end
end
io.stderr:write("\nQuitting...\n")
elseif opts.l then
local has_linted_with_error;
local lint = require "moonscript.cmd.lint"
for _, tuple in pairs(files) do
local fname = tuple[1]
local res, err = lint.lint_file(fname)
if res then
has_linted_with_error = true
io.stderr:write(res .. "\n\n")
elseif err then
has_linted_with_error = true
io.stderr:write(fname .. "\n" .. err.. "\n\n")
end
end
if has_linted_with_error then
os.exit(1)
end
else
for _, tuple in ipairs(files) do
local fname, target = util.unpack(tuple)
if opts.o then
target = opts.o
end
local success, err = compile_and_write(fname, target, {
print = opts.p,
fname = fname,
benchmark = opts.b,
show_posmap = opts.X,
show_parse_tree = opts.T,
keep_line_number = opts.n
})
if not success then
io.stderr:write(fname .. "\t" .. err .. "\n")
os.exit(1)
elseif success == "build" then
log_msg("Built", fname)
end
end
end