2018-10-17 18:32:39 -07:00
|
|
|
#!/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]
|
|
|
|
|
2019-04-20 18:08:03 -07:00
|
|
|
--repl : Launch an interactive repl session
|
|
|
|
--compile FILES : Compile files and write their Lua to stdout
|
|
|
|
--help : Display this text
|
2018-10-17 18:32:39 -07:00
|
|
|
|
2019-04-20 18:08:03 -07:00
|
|
|
When not given a flag, runs the file given as the first argument.]]
|
2018-10-17 18:32:39 -07:00
|
|
|
|
|
|
|
local options = {
|
2019-04-20 18:08:03 -07:00
|
|
|
sourcemap = true,
|
2018-10-17 18:32:39 -07:00
|
|
|
}
|
|
|
|
|
2019-04-20 18:08:03 -07:00
|
|
|
local function dosafe(filename, opts, arg1)
|
2018-10-17 18:32:39 -07:00
|
|
|
local ok, val = xpcall(function()
|
2019-04-20 18:08:03 -07:00
|
|
|
return fennel.dofile(filename, opts, arg1)
|
2018-10-17 18:32:39 -07:00
|
|
|
end, fennel.traceback)
|
|
|
|
if not ok then
|
2019-04-20 18:08:03 -07:00
|
|
|
print(val)
|
2018-10-17 18:32:39 -07:00
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
2019-04-20 18:08:03 -07:00
|
|
|
local compileOptHandlers = {
|
|
|
|
['--indent'] = function ()
|
|
|
|
options.indent = table.remove(arg, 3)
|
2018-10-17 18:32:39 -07:00
|
|
|
if options.indent == "false" then options.indent = false end
|
2019-04-20 18:08:03 -07:00
|
|
|
table.remove(arg, 2)
|
|
|
|
end,
|
|
|
|
['--sourcemap'] = function ()
|
|
|
|
options.sourcemap = table.remove(arg, 3)
|
2018-10-17 18:32:39 -07:00
|
|
|
if options.sourcemap == "false" then options.sourcemap = false end
|
2019-04-20 18:08:03 -07:00
|
|
|
table.remove(arg, 2)
|
|
|
|
end,
|
|
|
|
['--correlate'] = function ()
|
|
|
|
options.correlate = true
|
|
|
|
table.remove(arg, 2)
|
|
|
|
end,
|
|
|
|
}
|
2018-10-17 18:32:39 -07:00
|
|
|
|
|
|
|
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
|
2019-04-20 18:08:03 -07:00
|
|
|
-- Handle options
|
|
|
|
while compileOptHandlers[arg[2]] do
|
|
|
|
compileOptHandlers[arg[2]]()
|
|
|
|
end
|
2018-10-17 18:32:39 -07:00
|
|
|
for i = 2, #arg do
|
2019-04-20 18:08:03 -07:00
|
|
|
local f = assert(io.open(arg[i], "rb"))
|
2018-10-17 18:32:39 -07:00
|
|
|
options.filename=arg[i]
|
|
|
|
local ok, val = xpcall(function()
|
|
|
|
return fennel.compileString(f:read("*all"), options)
|
|
|
|
end, fennel.traceback)
|
2019-04-20 18:08:03 -07:00
|
|
|
print(val)
|
|
|
|
if not ok then os.exit(1) end
|
2018-10-17 18:32:39 -07:00
|
|
|
f:close()
|
|
|
|
end
|
2019-04-20 18:08:03 -07:00
|
|
|
elseif #arg >= 1 and arg[1] ~= "--help" then
|
2018-10-17 18:32:39 -07:00
|
|
|
local filename = table.remove(arg, 1) -- let the script have remaining args
|
2019-04-20 18:08:03 -07:00
|
|
|
dosafe(filename)
|
2018-10-17 18:32:39 -07:00
|
|
|
else
|
|
|
|
print(help)
|
|
|
|
end
|