melee-roguelite/lib/fennel

162 lines
5.1 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')
2019-04-20 17:38:53 -07:00
local unpack = unpack or table.unpack
2018-10-17 18:32:39 -07:00
local help = [[
Usage: fennel [FLAG] [FILE]
2019-04-20 17:38:53 -07:00
Run fennel, a lisp programming language for the Lua runtime.
2018-10-17 18:32:39 -07:00
2019-04-20 17:38:53 -07:00
--repl : Launch an interactive repl session
--compile FILES : Compile files and write their Lua to stdout
--no-searcher : Skip installing package.searchers entry
--indent VAL : Indent compiler output with VAL
--add-package-path PATH : Add PATH to package.path for finding Lua modules
--add-fennel-path PATH : Add PATH to fennel.path for finding Fennel modules
--globals G1[,G2...] : Allow these globals in addition to standard ones
--globals-only G1[,G2] : Same as above, but exclude standard ones
--help : Display this text
--version : Show version
When not given a flag, runs the file given as the first argument.
When given neither flag nor file, launches a repl.
If ~/.fennelrc exists, loads it before launching a repl.]]
2018-10-17 18:32:39 -07:00
local options = {
2019-04-20 17:38:53 -07:00
sourcemap = true
2018-10-17 18:32:39 -07:00
}
2019-04-20 17:38:53 -07:00
local function dosafe(filename, opts, args)
2018-10-17 18:32:39 -07:00
local ok, val = xpcall(function()
2019-04-20 17:38:53 -07:00
return fennel.dofile(filename, opts, unpack(args))
2018-10-17 18:32:39 -07:00
end, fennel.traceback)
if not ok then
2019-04-20 17:38:53 -07:00
io.stderr:write(val .. "\n")
2018-10-17 18:32:39 -07:00
os.exit(1)
end
return val
end
2019-04-20 17:38:53 -07:00
local function allowGlobals(globalNames)
options.allowedGlobals = {}
for g in globalNames:gmatch("([^,]+),?") do
table.insert(options.allowedGlobals, g)
end
end
for i=#arg, 1, -1 do
if arg[i] == "--no-searcher" then
options.no_searcher = true
table.remove(arg, i)
elseif arg[i] == "--indent" then
options.indent = table.remove(arg, i+1)
2018-10-17 18:32:39 -07:00
if options.indent == "false" then options.indent = false end
2019-04-20 17:38:53 -07:00
table.remove(arg, i)
elseif arg[i] == "--add-package-path" then
local entry = table.remove(arg, i+1)
package.path = entry .. ";" .. package.path
table.remove(arg, i)
elseif arg[i] == "--add-fennel-path" then
local entry = table.remove(arg, i+1)
fennel.path = entry .. ";" .. fennel.path
table.remove(arg, i)
elseif arg[i] == "--sourcemap" then
options.sourcemap = table.remove(arg, i+1)
2018-10-17 18:32:39 -07:00
if options.sourcemap == "false" then options.sourcemap = false end
2019-04-20 17:38:53 -07:00
table.remove(arg, i)
elseif arg[i] == "--globals" then
allowGlobals(table.remove(arg, i+1))
for globalName in pairs(_G) do
table.insert(options.allowedGlobals, globalName)
end
table.remove(arg, i)
elseif arg[i] == "--globals-only" then
allowGlobals(table.remove(arg, i+1))
table.remove(arg, i)
end
end
if not options.no_searcher then
table.insert((package.loaders or package.searchers),
fennel.make_searcher({correlate = true}))
end
-- Try to load readline library
local function tryReadline(opts)
local ok, readline = pcall(require, "readline")
if ok then
readline.set_options({
keeplines = 1000
})
function opts.readChunk(parserState)
local prompt = parserState.stackSize > 0 and '.. ' or '>> '
local str = readline.readline(prompt)
if str then
return str .. "\n"
end
end
-- completer is registered by the repl, until then returns empty list
local completer
function opts.registerCompleter(replCompleter)
completer = replCompleter
end
local function replCompleter(text, from, to)
if completer then return completer(text, from, to) else return {} end
end
readline.set_complete_function(replCompleter)
end
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
2019-04-20 17:38:53 -07:00
else
ppok, pp = pcall(require, "fennelview")
if ppok then
options.pp = pp
end
2018-10-17 18:32:39 -07:00
end
local initFilename = (os.getenv("HOME") or "") .. "/.fennelrc"
local init = io.open(initFilename, "rb")
2019-04-20 17:38:53 -07:00
tryReadline(options)
2018-10-17 18:32:39 -07:00
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
for i = 2, #arg do
2019-04-20 17:38:53 -07:00
local f = arg[i] == "-" and io.stdin or 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 17:38:53 -07:00
if ok then
print(val)
else
io.stderr:write(val .. "\n")
os.exit(1)
end
2018-10-17 18:32:39 -07:00
f:close()
end
2019-04-20 17:38:53 -07:00
elseif arg[1] == "--version" or arg[1] == "-v" then
print("Fennel " .. fennel.version)
elseif #arg >= 1 and arg[1] ~= "--help" and arg[1] ~= "-h" then
2018-10-17 18:32:39 -07:00
local filename = table.remove(arg, 1) -- let the script have remaining args
2019-04-20 17:38:53 -07:00
dosafe(filename, nil, arg)
2018-10-17 18:32:39 -07:00
else
print(help)
end