fennelc.fnl (1407B)
1 (local { : view &as fennel } ( require :fennel)) 2 3 (fn assoc [tbl k v & more] 4 (tset tbl k v) 5 (case more 6 [k v] (assoc tbl k v) 7 _ tbl)) 8 9 (fn parse-args [args] 10 (case args 11 ["--correlate" & rest] 12 (assoc (parse-args rest) 13 :correlate true) 14 15 ["--main" main-fn & rest] 16 (assoc (parse-args rest) 17 :main main-fn) 18 19 ["-o" output-file & rest] 20 (assoc (parse-args rest) 21 :output-file output-file) 22 23 [source] { :source-file source })) 24 25 (fn output-name [opts] 26 (or opts.output-file 27 (string.gsub opts.source-file "%.fnl$" ""))) 28 29 (let [luapath (os.getenv "LUA_PATH") 30 luacpath (os.getenv "LUA_CPATH") 31 path (os.getenv "PATH") 32 opts (parse-args arg) 33 output-file (output-name opts)] 34 (with-open [o (io.open output-file :w)] 35 (o:write "#!/usr/bin/env lua\n") 36 (and luapath 37 (o:write (string.format "package.path = %q .. \";\" .. package.path\n" luapath))) 38 (and luacpath 39 (o:write (string.format "package.cpath = %q .. \";\" .. package.cpath\n" luacpath))) 40 (let [(ok? msg) 41 (pcall 42 fennel.compile-string 43 (: (io.open opts.source-file :r) :read "*a") 44 {:filename opts.source-file 45 :correlate opts.correlate})] 46 (when (not ok?) 47 (error (.. "error: " msg))) 48 (o:write msg) 49 (os.execute (string.format "chmod +x %q" output-file)))))