melee-roguelite/lib/fennel

83 lines
2.3 KiB
Plaintext
Raw Normal View History

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]
--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,
['--correlate'] = function ()
options.correlate = true
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