From 9397dcda075029a12f8a338c2161a7dfd5bddfb5 Mon Sep 17 00:00:00 2001 From: Harry Gallagher Date: Sat, 29 Jan 2022 23:59:42 -0600 Subject: [PATCH] add support for injecting additional macros (#66) --- fnl/aniseed/compile.fnl | 24 +++++++++--------- lua/aniseed/compile.lua | 55 +++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/fnl/aniseed/compile.fnl b/fnl/aniseed/compile.fnl index 12fe193..7fb94cf 100644 --- a/fnl/aniseed/compile.fnl +++ b/fnl/aniseed/compile.fnl @@ -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_") diff --git a/lua/aniseed/compile.lua b/lua/aniseed/compile.lua index acb93e4..9fcc104 100644 --- a/lua/aniseed/compile.lua +++ b/lua/aniseed/compile.lua @@ -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_" @@ -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