Skip to content

Commit

Permalink
add support for injecting additional macros (Olical#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrygallagher4 committed Jan 30, 2022
1 parent f64ef9b commit 9397dcd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
24 changes: 13 additions & 11 deletions fnl/aniseed/compile.fnl
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
(module aniseed.compile
{autoload {a aniseed.core
s aniseed.string
fs aniseed.fs
nvim aniseed.nvim
fennel aniseed.fennel}})

(defn macros-strs [mods]
(->> (if (a.string? mods) [mods] mods)
(a.concat [:aniseed.macros])
(a.map #(string.format "(require-macros \"%s\")" $))
(s.join "\n")))

(defn macros-prefix [code opts]
(let [macros-module :aniseed.macros
(let [macros-modules (macros-strs (a.get opts :macros))
filename (-?> (a.get opts :filename)
(string.gsub
(.. (nvim.fn.getcwd) fs.path-sep)
""))]
(.. "(local *file* "
(if filename
(.. "\"" (string.gsub filename "\\" "\\\\") "\"")
"nil")
")"
"(require-macros \"" macros-module "\")\n"
"(wrap-module-body " (or code "") ")")))
(string.gsub (.. (nvim.fn.getcwd) fs.path-sep) "")
(string.gsub "\\" "\\\\"))]
(string.format
"(local *file* \"%s\")\n%s\n(wrap-module-body\n%s\n)"
(or filename "nil") macros-modules (or code ""))))

;; Magic strings from the macros that allow us to emit clean code.
(def marker-prefix "ANISEED_")
Expand Down
55 changes: 34 additions & 21 deletions lua/aniseed/compile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,43 @@ do
_2amodule_locals_2a = (_2amodule_2a)["aniseed/locals"]
end
local autoload = (require("aniseed.autoload")).autoload
local a, fennel, fs, nvim = autoload("aniseed.core"), autoload("aniseed.fennel"), autoload("aniseed.fs"), autoload("aniseed.nvim")
local a, fennel, fs, nvim, s = autoload("aniseed.core"), autoload("aniseed.fennel"), autoload("aniseed.fs"), autoload("aniseed.nvim"), autoload("aniseed.string")
do end (_2amodule_locals_2a)["a"] = a
_2amodule_locals_2a["fennel"] = fennel
_2amodule_locals_2a["fs"] = fs
_2amodule_locals_2a["nvim"] = nvim
local function macros_prefix(code, opts)
local macros_module = "aniseed.macros"
local filename
do
local _1_ = a.get(opts, "filename")
if (nil ~= _1_) then
filename = string.gsub(_1_, (nvim.fn.getcwd() .. fs["path-sep"]), "")
_2amodule_locals_2a["s"] = s
local function macros_strs(mods)
local function _1_(_241)
return string.format("(require-macros \"%s\")", _241)
end
local function _2_()
if a["string?"](mods) then
return {mods}
else
filename = _1_
return mods
end
end
local function _3_()
if filename then
return ("\"" .. string.gsub(filename, "\\", "\\\\") .. "\"")
return s.join("\n", a.map(_1_, a.concat({"aniseed.macros"}, _2_())))
end
_2amodule_2a["macros-strs"] = macros_strs
local function macros_prefix(code, opts)
local macros_modules = macros_strs(a.get(opts, "macros"))
local filename
do
local _3_ = a.get(opts, "filename")
if (nil ~= _3_) then
local _4_ = string.gsub(_3_, (nvim.fn.getcwd() .. fs["path-sep"]), "")
if (nil ~= _4_) then
filename = string.gsub(_4_, "\\", "\\\\")
else
filename = _4_
end
else
return "nil"
filename = _3_
end
end
return ("(local *file* " .. _3_() .. ")" .. "(require-macros \"" .. macros_module .. "\")\n" .. "(wrap-module-body " .. (code or "") .. ")")
return string.format("(local *file* \"%s\")\n%s\n(wrap-module-body\n%s\n)", (filename or "nil"), macros_modules, (code or ""))
end
_2amodule_2a["macros-prefix"] = macros_prefix
local marker_prefix = "ANISEED_"
Expand All @@ -46,20 +59,20 @@ do end (_2amodule_locals_2a)["delete-marker-pat"] = delete_marker_pat
local function str(code, opts)
ANISEED_STATIC_MODULES = (true == a.get(opts, "static?"))
local fnl = fennel.impl()
local function _4_()
local function _7_()
return string.gsub(string.gsub(fnl.compileString(macros_prefix(code, opts), a["merge!"]({allowedGlobals = false, compilerEnv = _G}, opts)), (delete_marker_pat .. "\n"), "\n"), (delete_marker_pat .. "$"), "")
end
return xpcall(_4_, fnl.traceback)
return xpcall(_7_, fnl.traceback)
end
_2amodule_2a["str"] = str
local function file(src, dest, opts)
local code = a.slurp(src)
local _5_, _6_ = str(code, a["merge!"]({filename = src, ["static?"] = true}, opts))
if ((_5_ == false) and (nil ~= _6_)) then
local err = _6_
local _8_, _9_ = str(code, a["merge!"]({filename = src, ["static?"] = true}, opts))
if ((_8_ == false) and (nil ~= _9_)) then
local err = _9_
return nvim.err_writeln(err)
elseif ((_5_ == true) and (nil ~= _6_)) then
local result = _6_
elseif ((_8_ == true) and (nil ~= _9_)) then
local result = _9_
fs.mkdirp(fs.basename(dest))
return a.spit(dest, result)
else
Expand Down

0 comments on commit 9397dcd

Please sign in to comment.