forked from bakpakin/Fennel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fennel
executable file
·78 lines (70 loc) · 2.21 KB
/
fennel
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
#!/usr/bin/env lua
local fennel_dir = arg[0]:match("(.-)[^\\/]+$")
package.path = fennel_dir .. "?.lua;" .. package.path
local fennel = require('fennel')
local help = [[
Usage: fennel [FLAG] [FILE]
--repl : Launch an interactive repl session
--compile FILES : Compile files and write their Lua to stdout
--help : Display this text
When not given a flag, runs the file given as the first argument.]]
local options = {
sourcemap = true
}
local function dosafe(filename, opts, arg1)
local ok, val = xpcall(function()
return fennel.dofile(filename, opts, arg1)
end, fennel.traceback)
if not ok then
print(val)
os.exit(1)
end
return val
end
local compileOptHandlers = {
['--indent'] = function ()
options.indent = table.remove(arg, 3)
if options.indent == "false" then options.indent = false end
table.remove(arg, 2)
end,
['--sourcemap'] = function ()
options.sourcemap = table.remove(arg, 3)
if options.sourcemap == "false" then options.sourcemap = false end
table.remove(arg, 2)
end,
}
if arg[1] == "--repl" or #arg == 0 then
local ppok, pp = pcall(fennel.dofile, fennel_dir .. "fennelview.fnl", options)
if ppok then
options.pp = pp
end
local initFilename = (os.getenv("HOME") or "") .. "/.fennelrc"
local init = io.open(initFilename, "rb")
if init then
init:close()
-- pass in options so fennerlrc can make changes to it
dosafe(initFilename, options, options)
end
print("Welcome to fennel!")
fennel.repl(options)
elseif arg[1] == "--compile" then
-- Handle options
while compileOptHandlers[arg[2]] do
compileOptHandlers[arg[2]]()
end
for i = 2, #arg do
local f = assert(io.open(arg[i], "rb"))
options.filename=arg[i]
local ok, val = xpcall(function()
return fennel.compileString(f:read("*all"), options)
end, fennel.traceback)
print(val)
if not ok then os.exit(1) end
f:close()
end
elseif #arg >= 1 and arg[1] ~= "--help" then
local filename = table.remove(arg, 1) -- let the script have remaining args
dosafe(filename)
else
print(help)
end