fennel 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env lua
  2. local fennel_dir = arg[0]:match("(.-)[^\\/]+$")
  3. package.path = fennel_dir .. "?.lua;" .. package.path
  4. local fennel = require('fennel')
  5. local help = [[
  6. Usage: fennel [FLAG] [FILE]
  7. --repl : Launch an interactive repl session
  8. --compile FILES : Compile files and write their Lua to stdout
  9. --help : Display this text
  10. When not given a flag, runs the file given as the first argument.]]
  11. local options = {
  12. sourcemap = true,
  13. }
  14. local function dosafe(filename, opts, arg1)
  15. local ok, val = xpcall(function()
  16. return fennel.dofile(filename, opts, arg1)
  17. end, fennel.traceback)
  18. if not ok then
  19. print(val)
  20. os.exit(1)
  21. end
  22. return val
  23. end
  24. local compileOptHandlers = {
  25. ['--indent'] = function ()
  26. options.indent = table.remove(arg, 3)
  27. if options.indent == "false" then options.indent = false end
  28. table.remove(arg, 2)
  29. end,
  30. ['--sourcemap'] = function ()
  31. options.sourcemap = table.remove(arg, 3)
  32. if options.sourcemap == "false" then options.sourcemap = false end
  33. table.remove(arg, 2)
  34. end,
  35. ['--correlate'] = function ()
  36. options.correlate = true
  37. table.remove(arg, 2)
  38. end,
  39. }
  40. if arg[1] == "--repl" or #arg == 0 then
  41. local ppok, pp = pcall(fennel.dofile, fennel_dir .. "fennelview.fnl", options)
  42. if ppok then
  43. options.pp = pp
  44. end
  45. local initFilename = (os.getenv("HOME") or "") .. "/.fennelrc"
  46. local init = io.open(initFilename, "rb")
  47. if init then
  48. init:close()
  49. -- pass in options so fennerlrc can make changes to it
  50. dosafe(initFilename, options, options)
  51. end
  52. print("Welcome to fennel!")
  53. fennel.repl(options)
  54. elseif arg[1] == "--compile" then
  55. -- Handle options
  56. while compileOptHandlers[arg[2]] do
  57. compileOptHandlers[arg[2]]()
  58. end
  59. for i = 2, #arg do
  60. local f = assert(io.open(arg[i], "rb"))
  61. options.filename=arg[i]
  62. local ok, val = xpcall(function()
  63. return fennel.compileString(f:read("*all"), options)
  64. end, fennel.traceback)
  65. print(val)
  66. if not ok then os.exit(1) end
  67. f:close()
  68. end
  69. elseif #arg >= 1 and arg[1] ~= "--help" then
  70. local filename = table.remove(arg, 1) -- let the script have remaining args
  71. dosafe(filename)
  72. else
  73. print(help)
  74. end