summaryrefslogtreecommitdiff
path: root/lib/fennel
diff options
context:
space:
mode:
authorJakob L. Kreuze <jakob@memeware.net>2018-10-21 08:53:28 -0400
committerJakob L. Kreuze <jakob@memeware.net>2018-10-21 08:53:28 -0400
commit069c31331795e6501fda64ef4bf1d15771a43cfd (patch)
tree0b034fb22e56af9b275b479c83427dfee3f33427 /lib/fennel
Initial commit
Diffstat (limited to 'lib/fennel')
-rwxr-xr-xlib/fennel82
1 files changed, 82 insertions, 0 deletions
diff --git a/lib/fennel b/lib/fennel
new file mode 100755
index 0000000..8fd7652
--- /dev/null
+++ b/lib/fennel
@@ -0,0 +1,82 @@
+#!/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