garden/fennel.lua

6140 lines
229 KiB
Lua

-- SPDX-License-Identifier: MIT
-- SPDX-FileCopyrightText: Calvin Rose and contributors
package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...)
local utils = require("fennel.utils")
local parser = require("fennel.parser")
local compiler = require("fennel.compiler")
local specials = require("fennel.specials")
local view = require("fennel.view")
local unpack = (table.unpack or _G.unpack)
local depth = 0
local function prompt_for(top_3f)
if top_3f then
return (string.rep(">", (depth + 1)) .. " ")
else
return (string.rep(".", (depth + 1)) .. " ")
end
end
local function default_read_chunk(parser_state)
io.write(prompt_for((0 == parser_state["stack-size"])))
io.flush()
local input = io.read()
return (input and (input .. "\n"))
end
local function default_on_values(xs)
io.write(table.concat(xs, "\9"))
return io.write("\n")
end
local function default_on_error(errtype, err, lua_source)
local function _612_()
local _611_0 = errtype
if (_611_0 == "Lua Compile") then
return ("Bad code generated - likely a bug with the compiler:\n" .. "--- Generated Lua Start ---\n" .. lua_source .. "--- Generated Lua End ---\n")
elseif (_611_0 == "Runtime") then
return (compiler.traceback(tostring(err), 4) .. "\n")
else
local _ = _611_0
return ("%s error: %s\n"):format(errtype, tostring(err))
end
end
return io.write(_612_())
end
local function splice_save_locals(env, lua_source, scope)
local saves = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for name in pairs(env.___replLocals___) do
local val_19_ = ("local %s = ___replLocals___[%q]"):format((scope.manglings[name] or name), name)
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
saves = tbl_17_
end
local binds = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for raw, name in pairs(scope.manglings) do
local val_19_ = nil
if not scope.gensyms[name] then
val_19_ = ("___replLocals___[%q] = %s"):format(raw, name)
else
val_19_ = nil
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
binds = tbl_17_
end
local gap = nil
if lua_source:find("\n") then
gap = "\n"
else
gap = " "
end
local function _618_()
if next(saves) then
return (table.concat(saves, " ") .. gap)
else
return ""
end
end
local function _621_()
local _619_0, _620_0 = lua_source:match("^(.*)[\n ](return .*)$")
if ((nil ~= _619_0) and (nil ~= _620_0)) then
local body = _619_0
local _return = _620_0
return (body .. gap .. table.concat(binds, " ") .. gap .. _return)
else
local _ = _619_0
return lua_source
end
end
return (_618_() .. _621_())
end
local function completer(env, scope, text)
local max_items = 2000
local seen = {}
local matches = {}
local input_fragment = text:gsub(".*[%s)(]+", "")
local stop_looking_3f = false
local function add_partials(input, tbl, prefix)
local scope_first_3f = ((tbl == env) or (tbl == env.___replLocals___))
local tbl_17_ = matches
local i_18_ = #tbl_17_
local function _623_()
if scope_first_3f then
return scope.manglings
else
return tbl
end
end
for k, is_mangled in utils.allpairs(_623_()) do
if (max_items <= #matches) then break end
local val_19_ = nil
do
local lookup_k = nil
if scope_first_3f then
lookup_k = is_mangled
else
lookup_k = k
end
if ((type(k) == "string") and (input == k:sub(0, #input)) and not seen[k] and ((":" ~= prefix:sub(-1)) or ("function" == type(tbl[lookup_k])))) then
seen[k] = true
val_19_ = (prefix .. k)
else
val_19_ = nil
end
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
local function descend(input, tbl, prefix, add_matches, method_3f)
local splitter = nil
if method_3f then
splitter = "^([^:]+):(.*)"
else
splitter = "^([^.]+)%.(.*)"
end
local head, tail = input:match(splitter)
local raw_head = (scope.manglings[head] or head)
if (type(tbl[raw_head]) == "table") then
stop_looking_3f = true
if method_3f then
return add_partials(tail, tbl[raw_head], (prefix .. head .. ":"))
else
return add_matches(tail, tbl[raw_head], (prefix .. head))
end
end
end
local function add_matches(input, tbl, prefix)
local prefix0 = nil
if prefix then
prefix0 = (prefix .. ".")
else
prefix0 = ""
end
if (not input:find("%.") and input:find(":")) then
return descend(input, tbl, prefix0, add_matches, true)
elseif not input:find("%.") then
return add_partials(input, tbl, prefix0)
else
return descend(input, tbl, prefix0, add_matches, false)
end
end
for _, source in ipairs({scope.specials, scope.macros, (env.___replLocals___ or {}), env, env._G}) do
if stop_looking_3f then break end
add_matches(input_fragment, source)
end
return matches
end
local commands = {}
local function command_3f(input)
return input:match("^%s*,")
end
local function command_docs()
local _632_
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for name, f in pairs(commands) do
local val_19_ = (" ,%s - %s"):format(name, ((compiler.metadata):get(f, "fnl/docstring") or "undocumented"))
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
_632_ = tbl_17_
end
return table.concat(_632_, "\n")
end
commands.help = function(_, _0, on_values)
return on_values({("Welcome to Fennel.\nThis is the REPL where you can enter code to be evaluated.\nYou can also run these repl commands:\n\n" .. command_docs() .. "\n ,return FORM - Evaluate FORM and return its value to the REPL's caller.\n ,exit - Leave the repl.\n\nUse ,doc something to see descriptions for individual macros and special forms.\nValues from previous inputs are kept in *1, *2, and *3.\n\nFor more information about the language, see https://fennel-lang.org/reference")})
end
do end (compiler.metadata):set(commands.help, "fnl/docstring", "Show this message.")
local function reload(module_name, env, on_values, on_error)
local _634_0, _635_0 = pcall(specials["load-code"]("return require(...)", env), module_name)
if ((_634_0 == true) and (nil ~= _635_0)) then
local old = _635_0
local _ = nil
package.loaded[module_name] = nil
_ = nil
local ok, new = pcall(require, module_name)
local new0 = nil
if not ok then
on_values({new})
new0 = old
else
new0 = new
end
specials["macro-loaded"][module_name] = nil
if ((type(old) == "table") and (type(new0) == "table")) then
for k, v in pairs(new0) do
old[k] = v
end
for k in pairs(old) do
if (nil == new0[k]) then
old[k] = nil
end
end
package.loaded[module_name] = old
end
return on_values({"ok"})
elseif ((_634_0 == false) and (nil ~= _635_0)) then
local msg = _635_0
if msg:match("loop or previous error loading module") then
package.loaded[module_name] = nil
return reload(module_name, env, on_values, on_error)
elseif specials["macro-loaded"][module_name] then
specials["macro-loaded"][module_name] = nil
return nil
else
local function _640_()
local _639_0 = msg:gsub("\n.*", "")
return _639_0
end
return on_error("Runtime", _640_())
end
end
end
local function run_command(read, on_error, f)
local _643_0, _644_0, _645_0 = pcall(read)
if ((_643_0 == true) and (_644_0 == true) and (nil ~= _645_0)) then
local val = _645_0
local _646_0, _647_0 = pcall(f, val)
if ((_646_0 == false) and (nil ~= _647_0)) then
local msg = _647_0
return on_error("Runtime", msg)
end
elseif (_643_0 == false) then
return on_error("Parse", "Couldn't parse input.")
end
end
commands.reload = function(env, read, on_values, on_error)
local function _650_(_241)
return reload(tostring(_241), env, on_values, on_error)
end
return run_command(read, on_error, _650_)
end
do end (compiler.metadata):set(commands.reload, "fnl/docstring", "Reload the specified module.")
commands.reset = function(env, _, on_values)
env.___replLocals___ = {}
return on_values({"ok"})
end
do end (compiler.metadata):set(commands.reset, "fnl/docstring", "Erase all repl-local scope.")
commands.complete = function(env, read, on_values, on_error, scope, chars)
local function _651_()
return on_values(completer(env, scope, table.concat(chars):gsub(",complete +", ""):sub(1, -2)))
end
return run_command(read, on_error, _651_)
end
do end (compiler.metadata):set(commands.complete, "fnl/docstring", "Print all possible completions for a given input symbol.")
local function apropos_2a(pattern, tbl, prefix, seen, names)
for name, subtbl in pairs(tbl) do
if (("string" == type(name)) and (package ~= subtbl)) then
local _652_0 = type(subtbl)
if (_652_0 == "function") then
if ((prefix .. name)):match(pattern) then
table.insert(names, (prefix .. name))
end
elseif (_652_0 == "table") then
if not seen[subtbl] then
local _654_
do
seen[subtbl] = true
_654_ = seen
end
apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _654_, names)
end
end
end
end
return names
end
local function apropos(pattern)
local names = apropos_2a(pattern, package.loaded, "", {}, {})
local tbl_17_ = {}
local i_18_ = #tbl_17_
for _, name in ipairs(names) do
local val_19_ = name:gsub("^_G%.", "")
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
commands.apropos = function(_env, read, on_values, on_error, _scope)
local function _659_(_241)
return on_values(apropos(tostring(_241)))
end
return run_command(read, on_error, _659_)
end
do end (compiler.metadata):set(commands.apropos, "fnl/docstring", "Print all functions matching a pattern in all loaded modules.")
local function apropos_follow_path(path)
local paths = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for p in path:gmatch("[^%.]+") do
local val_19_ = p
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
paths = tbl_17_
end
local tgt = package.loaded
for _, path0 in ipairs(paths) do
if (nil == tgt) then break end
local _662_
do
local _661_0 = path0:gsub("%/", ".")
_662_ = _661_0
end
tgt = tgt[_662_]
end
return tgt
end
local function apropos_doc(pattern)
local tbl_17_ = {}
local i_18_ = #tbl_17_
for _, path in ipairs(apropos(".*")) do
local val_19_ = nil
do
local tgt = apropos_follow_path(path)
if ("function" == type(tgt)) then
local _663_0 = (compiler.metadata):get(tgt, "fnl/docstring")
if (nil ~= _663_0) then
local docstr = _663_0
val_19_ = (docstr:match(pattern) and path)
else
val_19_ = nil
end
else
val_19_ = nil
end
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
commands["apropos-doc"] = function(_env, read, on_values, on_error, _scope)
local function _667_(_241)
return on_values(apropos_doc(tostring(_241)))
end
return run_command(read, on_error, _667_)
end
do end (compiler.metadata):set(commands["apropos-doc"], "fnl/docstring", "Print all functions that match the pattern in their docs")
local function apropos_show_docs(on_values, pattern)
for _, path in ipairs(apropos(pattern)) do
local tgt = apropos_follow_path(path)
if (("function" == type(tgt)) and (compiler.metadata):get(tgt, "fnl/docstring")) then
on_values({specials.doc(tgt, path)})
on_values({})
end
end
return nil
end
commands["apropos-show-docs"] = function(_env, read, on_values, on_error)
local function _669_(_241)
return apropos_show_docs(on_values, tostring(_241))
end
return run_command(read, on_error, _669_)
end
do end (compiler.metadata):set(commands["apropos-show-docs"], "fnl/docstring", "Print all documentations matching a pattern in function name")
local function resolve(identifier, _670_0, scope)
local _671_ = _670_0
local env = _671_
local ___replLocals___ = _671_["___replLocals___"]
local e = nil
local function _672_(_241, _242)
return (___replLocals___[scope.unmanglings[_242]] or env[_242])
end
e = setmetatable({}, {__index = _672_})
local function _673_(...)
local _674_0, _675_0 = ...
if ((_674_0 == true) and (nil ~= _675_0)) then
local code = _675_0
local function _676_(...)
local _677_0, _678_0 = ...
if ((_677_0 == true) and (nil ~= _678_0)) then
local val = _678_0
return val
else
local _ = _677_0
return nil
end
end
return _676_(pcall(specials["load-code"](code, e)))
else
local _ = _674_0
return nil
end
end
return _673_(pcall(compiler["compile-string"], tostring(identifier), {scope = scope}))
end
commands.find = function(env, read, on_values, on_error, scope)
local function _681_(_241)
local _682_0 = nil
do
local _683_0 = utils["sym?"](_241)
if (nil ~= _683_0) then
local _684_0 = resolve(_683_0, env, scope)
if (nil ~= _684_0) then
_682_0 = debug.getinfo(_684_0)
else
_682_0 = _684_0
end
else
_682_0 = _683_0
end
end
if ((_G.type(_682_0) == "table") and (nil ~= _682_0.linedefined) and (nil ~= _682_0.short_src) and (nil ~= _682_0.source) and (_682_0.what == "Lua")) then
local line = _682_0.linedefined
local src = _682_0.short_src
local source = _682_0.source
local fnlsrc = nil
do
local _687_0 = compiler.sourcemap
if (nil ~= _687_0) then
_687_0 = _687_0[source]
end
if (nil ~= _687_0) then
_687_0 = _687_0[line]
end
if (nil ~= _687_0) then
_687_0 = _687_0[2]
end
fnlsrc = _687_0
end
return on_values({string.format("%s:%s", src, (fnlsrc or line))})
elseif (_682_0 == nil) then
return on_error("Repl", "Unknown value")
else
local _ = _682_0
return on_error("Repl", "No source info")
end
end
return run_command(read, on_error, _681_)
end
do end (compiler.metadata):set(commands.find, "fnl/docstring", "Print the filename and line number for a given function")
commands.doc = function(env, read, on_values, on_error, scope)
local function _692_(_241)
local name = tostring(_241)
local path = (utils["multi-sym?"](name) or {name})
local ok_3f, target = nil, nil
local function _693_()
return (utils["get-in"](scope.specials, path) or utils["get-in"](scope.macros, path) or resolve(name, env, scope))
end
ok_3f, target = pcall(_693_)
if ok_3f then
return on_values({specials.doc(target, name)})
else
return on_error("Repl", ("Could not find " .. name .. " for docs."))
end
end
return run_command(read, on_error, _692_)
end
do end (compiler.metadata):set(commands.doc, "fnl/docstring", "Print the docstring and arglist for a function, macro, or special form.")
commands.compile = function(env, read, on_values, on_error, scope)
local function _695_(_241)
local allowedGlobals = specials["current-global-names"](env)
local ok_3f, result = pcall(compiler.compile, _241, {allowedGlobals = allowedGlobals, env = env, scope = scope})
if ok_3f then
return on_values({result})
else
return on_error("Repl", ("Error compiling expression: " .. result))
end
end
return run_command(read, on_error, _695_)
end
do end (compiler.metadata):set(commands.compile, "fnl/docstring", "compiles the expression into lua and prints the result.")
local function load_plugin_commands(plugins)
for i = #(plugins or {}), 1, -1 do
for name, f in pairs(plugins[i]) do
local _697_0 = name:match("^repl%-command%-(.*)")
if (nil ~= _697_0) then
local cmd_name = _697_0
commands[cmd_name] = f
end
end
end
return nil
end
local function run_command_loop(input, read, loop, env, on_values, on_error, scope, chars)
local command_name = input:match(",([^%s/]+)")
do
local _699_0 = commands[command_name]
if (nil ~= _699_0) then
local command = _699_0
command(env, read, on_values, on_error, scope, chars)
else
local _ = _699_0
if ((command_name ~= "exit") and (command_name ~= "return")) then
on_values({"Unknown command", command_name})
end
end
end
if ("exit" ~= command_name) then
return loop((command_name == "return"))
end
end
local function try_readline_21(opts, ok, readline)
if ok then
if readline.set_readline_name then
readline.set_readline_name("fennel")
end
readline.set_options({histfile = "", keeplines = 1000})
opts.readChunk = function(parser_state)
local prompt = nil
if (0 < parser_state["stack-size"]) then
prompt = ".. "
else
prompt = ">> "
end
local str = readline.readline(prompt)
if str then
return (str .. "\n")
end
end
local completer0 = nil
opts.registerCompleter = function(repl_completer)
completer0 = repl_completer
return nil
end
local function repl_completer(text, from, to)
if completer0 then
readline.set_completion_append_character("")
return completer0(text:sub(from, to))
else
return {}
end
end
readline.set_complete_function(repl_completer)
return readline
end
end
local function should_use_readline_3f(opts)
return (("dumb" ~= os.getenv("TERM")) and not opts.readChunk and not opts.registerCompleter)
end
local function repl(_3foptions)
local old_root_options = utils.root.options
local _708_ = utils.copy(_3foptions)
local opts = _708_
local _3ffennelrc = _708_["fennelrc"]
local _ = nil
opts.fennelrc = nil
_ = nil
local readline = (should_use_readline_3f(opts) and try_readline_21(opts, pcall(require, "readline")))
local _0 = nil
if _3ffennelrc then
_0 = _3ffennelrc()
else
_0 = nil
end
local env = specials["wrap-env"]((opts.env or rawget(_G, "_ENV") or _G))
local callbacks = {env = env, onError = (opts.onError or default_on_error), onValues = (opts.onValues or default_on_values), pp = (opts.pp or view), readChunk = (opts.readChunk or default_read_chunk)}
local save_locals_3f = (opts.saveLocals ~= false)
local byte_stream, clear_stream = nil, nil
local function _710_(_241)
return callbacks.readChunk(_241)
end
byte_stream, clear_stream = parser.granulate(_710_)
local chars = {}
local read, reset = nil, nil
local function _711_(parser_state)
local b = byte_stream(parser_state)
if b then
table.insert(chars, string.char(b))
end
return b
end
read, reset = parser.parser(_711_)
depth = (depth + 1)
if opts.message then
callbacks.onValues({opts.message})
end
env.___repl___ = callbacks
opts.env, opts.scope = env, compiler["make-scope"]()
opts.useMetadata = (opts.useMetadata ~= false)
if (opts.allowedGlobals == nil) then
opts.allowedGlobals = specials["current-global-names"](env)
end
if opts.init then
opts.init(opts, depth)
end
if opts.registerCompleter then
local function _717_()
local _716_0 = opts.scope
local function _718_(...)
return completer(env, _716_0, ...)
end
return _718_
end
opts.registerCompleter(_717_())
end
load_plugin_commands(opts.plugins)
if save_locals_3f then
local function newindex(t, k, v)
if opts.scope.manglings[k] then
return rawset(t, k, v)
end
end
env.___replLocals___ = setmetatable({}, {__newindex = newindex})
end
local function print_values(...)
local vals = {...}
local out = {}
local pp = callbacks.pp
env._, env.__ = vals[1], vals
for i = 1, select("#", ...) do
table.insert(out, pp(vals[i]))
end
return callbacks.onValues(out)
end
local function save_value(...)
env.___replLocals___["*3"] = env.___replLocals___["*2"]
env.___replLocals___["*2"] = env.___replLocals___["*1"]
env.___replLocals___["*1"] = ...
return ...
end
opts.scope.manglings["*1"], opts.scope.unmanglings._1 = "_1", "*1"
opts.scope.manglings["*2"], opts.scope.unmanglings._2 = "_2", "*2"
opts.scope.manglings["*3"], opts.scope.unmanglings._3 = "_3", "*3"
local function loop(exit_next_3f)
for k in pairs(chars) do
chars[k] = nil
end
reset()
local ok, parser_not_eof_3f, form = pcall(read)
local src_string = table.concat(chars)
local readline_not_eof_3f = (not readline or (src_string ~= "(null)"))
local not_eof_3f = (readline_not_eof_3f and parser_not_eof_3f)
if not ok then
callbacks.onError("Parse", not_eof_3f)
clear_stream()
return loop()
elseif command_3f(src_string) then
return run_command_loop(src_string, read, loop, env, callbacks.onValues, callbacks.onError, opts.scope, chars)
else
if not_eof_3f then
local function _722_(...)
local _723_0, _724_0 = ...
if ((_723_0 == true) and (nil ~= _724_0)) then
local src = _724_0
local function _725_(...)
local _726_0, _727_0 = ...
if ((_726_0 == true) and (nil ~= _727_0)) then
local chunk = _727_0
local function _728_()
return print_values(save_value(chunk()))
end
local function _729_(...)
return callbacks.onError("Runtime", ...)
end
return xpcall(_728_, _729_)
elseif ((_726_0 == false) and (nil ~= _727_0)) then
local msg = _727_0
clear_stream()
return callbacks.onError("Compile", msg)
end
end
local function _732_(...)
local src0 = nil
if save_locals_3f then
src0 = splice_save_locals(env, src, opts.scope)
else
src0 = src
end
return pcall(specials["load-code"], src0, env)
end
return _725_(_732_(...))
elseif ((_723_0 == false) and (nil ~= _724_0)) then
local msg = _724_0
clear_stream()
return callbacks.onError("Compile", msg)
end
end
local function _734_()
opts["source"] = src_string
return opts
end
_722_(pcall(compiler.compile, form, _734_()))
utils.root.options = old_root_options
if exit_next_3f then
return env.___replLocals___["*1"]
else
return loop()
end
end
end
end
local value = loop()
depth = (depth - 1)
if readline then
readline.save_history()
end
if opts.exit then
opts.exit(opts, depth)
end
return value
end
return repl
end
package.preload["fennel.specials"] = package.preload["fennel.specials"] or function(...)
local utils = require("fennel.utils")
local view = require("fennel.view")
local parser = require("fennel.parser")
local compiler = require("fennel.compiler")
local unpack = (table.unpack or _G.unpack)
local SPECIALS = compiler.scopes.global.specials
local function wrap_env(env)
local function _417_(_, key)
if utils["string?"](key) then
return env[compiler["global-unmangling"](key)]
else
return env[key]
end
end
local function _419_(_, key, value)
if utils["string?"](key) then
env[compiler["global-unmangling"](key)] = value
return nil
else
env[key] = value
return nil
end
end
local function _421_()
local function putenv(k, v)
local _422_
if utils["string?"](k) then
_422_ = compiler["global-unmangling"](k)
else
_422_ = k
end
return _422_, v
end
return next, utils.kvmap(env, putenv), nil
end
return setmetatable({}, {__index = _417_, __newindex = _419_, __pairs = _421_})
end
local function current_global_names(_3fenv)
local mt = nil
do
local _424_0 = getmetatable(_3fenv)
if ((_G.type(_424_0) == "table") and (nil ~= _424_0.__pairs)) then
local mtpairs = _424_0.__pairs
local tbl_14_ = {}
for k, v in mtpairs(_3fenv) do
local k_15_, v_16_ = k, v
if ((k_15_ ~= nil) and (v_16_ ~= nil)) then
tbl_14_[k_15_] = v_16_
end
end
mt = tbl_14_
elseif (_424_0 == nil) then
mt = (_3fenv or _G)
else
mt = nil
end
end
return (mt and utils.kvmap(mt, compiler["global-unmangling"]))
end
local function load_code(code, _3fenv, _3ffilename)
local env = (_3fenv or rawget(_G, "_ENV") or _G)
local _427_0, _428_0 = rawget(_G, "setfenv"), rawget(_G, "loadstring")
if ((nil ~= _427_0) and (nil ~= _428_0)) then
local setfenv = _427_0
local loadstring = _428_0
local f = assert(loadstring(code, _3ffilename))
setfenv(f, env)
return f
else
local _ = _427_0
return assert(load(code, _3ffilename, "t", env))
end
end
local function doc_2a(tgt, name)
if not tgt then
return (name .. " not found")
else
local docstring = (((compiler.metadata):get(tgt, "fnl/docstring") or "#<undocumented>")):gsub("\n$", ""):gsub("\n", "\n ")
local mt = getmetatable(tgt)
if ((type(tgt) == "function") or ((type(mt) == "table") and (type(mt.__call) == "function"))) then
local arglist = table.concat(((compiler.metadata):get(tgt, "fnl/arglist") or {"#<unknown-arguments>"}), " ")
local _430_
if (0 < #arglist) then
_430_ = " "
else
_430_ = ""
end
return string.format("(%s%s%s)\n %s", name, _430_, arglist, docstring)
else
return string.format("%s\n %s", name, docstring)
end
end
end
local function doc_special(name, arglist, docstring, body_form_3f)
compiler.metadata[SPECIALS[name]] = {["fnl/arglist"] = arglist, ["fnl/body-form?"] = body_form_3f, ["fnl/docstring"] = docstring}
return nil
end
local function compile_do(ast, scope, parent, _3fstart)
local start = (_3fstart or 2)
local len = #ast
local sub_scope = compiler["make-scope"](scope)
for i = start, len do
compiler.compile1(ast[i], sub_scope, parent, {nval = 0})
end
return nil
end
SPECIALS["do"] = function(ast, scope, parent, opts, _3fstart, _3fchunk, _3fsub_scope, _3fpre_syms)
local start = (_3fstart or 2)
local sub_scope = (_3fsub_scope or compiler["make-scope"](scope))
local chunk = (_3fchunk or {})
local len = #ast
local retexprs = {returned = true}
local function compile_body(outer_target, outer_tail, outer_retexprs)
for i = start, len do
local subopts = {nval = (((i ~= len) and 0) or opts.nval), tail = (((i == len) and outer_tail) or nil), target = (((i == len) and outer_target) or nil)}
local _ = utils["propagate-options"](opts, subopts)
local subexprs = compiler.compile1(ast[i], sub_scope, chunk, subopts)
if (i ~= len) then
compiler["keep-side-effects"](subexprs, parent, nil, ast[i])
end
end
compiler.emit(parent, chunk, ast)
compiler.emit(parent, "end", ast)
utils.hook("do", ast, sub_scope)
return (outer_retexprs or retexprs)
end
if (opts.target or (opts.nval == 0) or opts.tail) then
compiler.emit(parent, "do", ast)
return compile_body(opts.target, opts.tail)
elseif opts.nval then
local syms = {}
for i = 1, opts.nval do
local s = ((_3fpre_syms and _3fpre_syms[i]) or compiler.gensym(scope))
syms[i] = s
retexprs[i] = utils.expr(s, "sym")
end
local outer_target = table.concat(syms, ", ")
compiler.emit(parent, string.format("local %s", outer_target), ast)
compiler.emit(parent, "do", ast)
return compile_body(outer_target, opts.tail)
else
local fname = compiler.gensym(scope)
local fargs = nil
if scope.vararg then
fargs = "..."
else
fargs = ""
end
compiler.emit(parent, string.format("local function %s(%s)", fname, fargs), ast)
return compile_body(nil, true, utils.expr((fname .. "(" .. fargs .. ")"), "statement"))
end
end
doc_special("do", {"..."}, "Evaluate multiple forms; return last value.", true)
SPECIALS.values = function(ast, scope, parent)
local len = #ast
local exprs = {}
for i = 2, len do
local subexprs = compiler.compile1(ast[i], scope, parent, {nval = ((i ~= len) and 1)})
table.insert(exprs, subexprs[1])
if (i == len) then
for j = 2, #subexprs do
table.insert(exprs, subexprs[j])
end
end
end
return exprs
end
doc_special("values", {"..."}, "Return multiple values from a function. Must be in tail position.")
local function __3estack(stack, tbl)
for k, v in pairs(tbl) do
table.insert(stack, k)
table.insert(stack, v)
end
return stack
end
local function literal_3f(val)
local res = true
if utils["list?"](val) then
res = false
elseif utils["table?"](val) then
local stack = __3estack({}, val)
for _, elt in ipairs(stack) do
if not res then break end
if utils["list?"](elt) then
res = false
elseif utils["table?"](elt) then
__3estack(stack, elt)
end
end
end
return res
end
local function compile_value(v)
local opts = {nval = 1, tail = false}
local scope = compiler["make-scope"]()
local chunk = {}
local _440_ = compiler.compile1(v, scope, chunk, opts)
local _441_ = _440_[1]
local v0 = _441_[1]
return v0
end
local function insert_meta(meta, k, v)
local view_opts = {["escape-newlines?"] = true, ["line-length"] = math.huge, ["one-line?"] = true}
compiler.assert((type(k) == "string"), ("expected string keys in metadata table, got: %s"):format(view(k, view_opts)))
compiler.assert(literal_3f(v), ("expected literal value in metadata table, got: %s %s"):format(view(k, view_opts), view(v, view_opts)))
table.insert(meta, view(k))
local function _442_()
if ("string" == type(v)) then
return view(v, view_opts)
else
return compile_value(v)
end
end
table.insert(meta, _442_())
return meta
end
local function insert_arglist(meta, arg_list)
local view_opts = {["escape-newlines?"] = true, ["line-length"] = math.huge, ["one-line?"] = true}
table.insert(meta, "\"fnl/arglist\"")
local function _443_(_241)
return view(view(_241, view_opts))
end
table.insert(meta, ("{" .. table.concat(utils.map(arg_list, _443_), ", ") .. "}"))
return meta
end
local function set_fn_metadata(f_metadata, parent, fn_name)
if utils.root.options.useMetadata then
local meta_fields = {}
for k, v in utils.stablepairs(f_metadata) do
if (k == "fnl/arglist") then
insert_arglist(meta_fields, v)
else
insert_meta(meta_fields, k, v)
end
end
local meta_str = ("require(\"%s\").metadata"):format((utils.root.options.moduleName or "fennel"))
return compiler.emit(parent, ("pcall(function() %s:setall(%s, %s) end)"):format(meta_str, fn_name, table.concat(meta_fields, ", ")))
end
end
local function get_fn_name(ast, scope, fn_name, multi)
if (fn_name and (fn_name[1] ~= "nil")) then
local _446_
if not multi then
_446_ = compiler["declare-local"](fn_name, {}, scope, ast)
else
_446_ = compiler["symbol-to-expression"](fn_name, scope)[1]
end
return _446_, not multi, 3
else
return nil, true, 2
end
end
local function compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, local_3f, arg_name_list, f_metadata)
for i = (index + 1), #ast do
compiler.compile1(ast[i], f_scope, f_chunk, {nval = (((i ~= #ast) and 0) or nil), tail = (i == #ast)})
end
local _449_
if local_3f then
_449_ = "local function %s(%s)"
else
_449_ = "%s = function(%s)"
end
compiler.emit(parent, string.format(_449_, fn_name, table.concat(arg_name_list, ", ")), ast)
compiler.emit(parent, f_chunk, ast)
compiler.emit(parent, "end", ast)
set_fn_metadata(f_metadata, parent, fn_name)
utils.hook("fn", ast, f_scope)
return utils.expr(fn_name, "sym")
end
local function compile_anonymous_fn(ast, f_scope, f_chunk, parent, index, arg_name_list, f_metadata, scope)
local fn_name = compiler.gensym(scope)
return compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, true, arg_name_list, f_metadata)
end
local function maybe_metadata(ast, pred, handler, mt, index)
local index_2a = (index + 1)
local index_2a_before_ast_end_3f = (index_2a < #ast)
local expr = ast[index_2a]
if (index_2a_before_ast_end_3f and pred(expr)) then
return handler(mt, expr), index_2a
else
return mt, index
end
end
local function get_function_metadata(ast, arg_list, index)
local function _452_(_241, _242)
local tbl_14_ = _241
for k, v in pairs(_242) do
local k_15_, v_16_ = k, v
if ((k_15_ ~= nil) and (v_16_ ~= nil)) then
tbl_14_[k_15_] = v_16_
end
end
return tbl_14_
end
local function _454_(_241, _242)
_241["fnl/docstring"] = _242
return _241
end
return maybe_metadata(ast, utils["kv-table?"], _452_, maybe_metadata(ast, utils["string?"], _454_, {["fnl/arglist"] = arg_list}, index))
end
SPECIALS.fn = function(ast, scope, parent)
local f_scope = nil
do
local _455_0 = compiler["make-scope"](scope)
_455_0["vararg"] = false
f_scope = _455_0
end
local f_chunk = {}
local fn_sym = utils["sym?"](ast[2])
local multi = (fn_sym and utils["multi-sym?"](fn_sym[1]))
local fn_name, local_3f, index = get_fn_name(ast, scope, fn_sym, multi)
local arg_list = compiler.assert(utils["table?"](ast[index]), "expected parameters table", ast)
compiler.assert((not multi or not multi["multi-sym-method-call"]), ("unexpected multi symbol " .. tostring(fn_name)), fn_sym)
local function destructure_arg(arg)
local raw = utils.sym(compiler.gensym(scope))
local declared = compiler["declare-local"](raw, {}, f_scope, ast)
compiler.destructure(arg, raw, ast, f_scope, f_chunk, {declaration = true, nomulti = true, symtype = "arg"})
return declared
end
local function destructure_amp(i)
compiler.assert((i == (#arg_list - 1)), "expected rest argument before last parameter", arg_list[(i + 1)], arg_list)
f_scope.vararg = true
compiler.destructure(arg_list[#arg_list], {utils.varg()}, ast, f_scope, f_chunk, {declaration = true, nomulti = true, symtype = "arg"})
return "..."
end
local function get_arg_name(arg, i)
if f_scope.vararg then
return nil
elseif utils["varg?"](arg) then
compiler.assert((arg == arg_list[#arg_list]), "expected vararg as last parameter", ast)
f_scope.vararg = true
return "..."
elseif utils["sym?"](arg, "&") then
return destructure_amp(i)
elseif (utils["sym?"](arg) and (tostring(arg) ~= "nil") and not utils["multi-sym?"](tostring(arg))) then
return compiler["declare-local"](arg, {}, f_scope, ast)
elseif utils["table?"](arg) then
return destructure_arg(arg)
else
return compiler.assert(false, ("expected symbol for function parameter: %s"):format(tostring(arg)), ast[index])
end
end
local arg_name_list = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for i, a in ipairs(arg_list) do
local val_19_ = get_arg_name(a, i)
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
arg_name_list = tbl_17_
end
local f_metadata, index0 = get_function_metadata(ast, arg_list, index)
if fn_name then
return compile_named_fn(ast, f_scope, f_chunk, parent, index0, fn_name, local_3f, arg_name_list, f_metadata)
else
return compile_anonymous_fn(ast, f_scope, f_chunk, parent, index0, arg_name_list, f_metadata, scope)
end
end
doc_special("fn", {"name?", "args", "docstring?", "..."}, "Function syntax. May optionally include a name and docstring or a metadata table.\nIf a name is provided, the function will be bound in the current scope.\nWhen called with the wrong number of args, excess args will be discarded\nand lacking args will be nil, use lambda for arity-checked functions.", true)
SPECIALS.lua = function(ast, _, parent)
compiler.assert(((#ast == 2) or (#ast == 3)), "expected 1 or 2 arguments", ast)
local _460_
do
local _459_0 = utils["sym?"](ast[2])
if (nil ~= _459_0) then
_460_ = tostring(_459_0)
else
_460_ = _459_0
end
end
if ("nil" ~= _460_) then
table.insert(parent, {ast = ast, leaf = tostring(ast[2])})
end
local _464_
do
local _463_0 = utils["sym?"](ast[3])
if (nil ~= _463_0) then
_464_ = tostring(_463_0)
else
_464_ = _463_0
end
end
if ("nil" ~= _464_) then
return tostring(ast[3])
end
end
local function dot(ast, scope, parent)
compiler.assert((1 < #ast), "expected table argument", ast)
local len = #ast
local _467_ = compiler.compile1(ast[2], scope, parent, {nval = 1})
local lhs = _467_[1]
if (len == 2) then
return tostring(lhs)
else
local indices = {}
for i = 3, len do
local index = ast[i]
if (utils["string?"](index) and utils["valid-lua-identifier?"](index)) then
table.insert(indices, ("." .. index))
else
local _468_ = compiler.compile1(index, scope, parent, {nval = 1})
local index0 = _468_[1]
table.insert(indices, ("[" .. tostring(index0) .. "]"))
end
end
if (not (utils["sym?"](ast[2]) or utils["list?"](ast[2])) or ("nil" == tostring(lhs))) then
return ("(" .. tostring(lhs) .. ")" .. table.concat(indices))
else
return (tostring(lhs) .. table.concat(indices))
end
end
end
SPECIALS["."] = dot
doc_special(".", {"tbl", "key1", "..."}, "Look up key1 in tbl table. If more args are provided, do a nested lookup.")
SPECIALS.global = function(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceglobal = true, nomulti = true, symtype = "global"})
return nil
end
doc_special("global", {"name", "val"}, "Set name as a global with val.")
SPECIALS.set = function(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {noundef = true, symtype = "set"})
return nil
end
doc_special("set", {"name", "val"}, "Set a local variable to a new value. Only works on locals using var.")
local function set_forcibly_21_2a(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceset = true, symtype = "set"})
return nil
end
SPECIALS["set-forcibly!"] = set_forcibly_21_2a
local function local_2a(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, nomulti = true, symtype = "local"})
return nil
end
SPECIALS["local"] = local_2a
doc_special("local", {"name", "val"}, "Introduce new top-level immutable local.")
SPECIALS.var = function(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, isvar = true, nomulti = true, symtype = "var"})
return nil
end
doc_special("var", {"name", "val"}, "Introduce new mutable local.")
local function kv_3f(t)
local _472_
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for k in pairs(t) do
local val_19_ = nil
if ("number" ~= type(k)) then
val_19_ = k
else
val_19_ = nil
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
_472_ = tbl_17_
end
return _472_[1]
end
SPECIALS.let = function(ast, scope, parent, opts)
local bindings = ast[2]
local pre_syms = {}
compiler.assert((utils["table?"](bindings) and not kv_3f(bindings)), "expected binding sequence", bindings)
compiler.assert(((#bindings % 2) == 0), "expected even number of name/value bindings", ast[2])
compiler.assert((3 <= #ast), "expected body expression", ast[1])
for _ = 1, (opts.nval or 0) do
table.insert(pre_syms, compiler.gensym(scope))
end
local sub_scope = compiler["make-scope"](scope)
local sub_chunk = {}
for i = 1, #bindings, 2 do
compiler.destructure(bindings[i], bindings[(i + 1)], ast, sub_scope, sub_chunk, {declaration = true, nomulti = true, symtype = "let"})
end
return SPECIALS["do"](ast, scope, parent, opts, 3, sub_chunk, sub_scope, pre_syms)
end
doc_special("let", {"[name1 val1 ... nameN valN]", "..."}, "Introduces a new scope in which a given set of local bindings are used.", true)
local function get_prev_line(parent)
if ("table" == type(parent)) then
return get_prev_line((parent.leaf or parent[#parent]))
else
return (parent or "")
end
end
local function disambiguate_3f(rootstr, parent)
local function _477_()
local _476_0 = get_prev_line(parent)
if (nil ~= _476_0) then
local prev_line = _476_0
return prev_line:match("%)$")
end
end
return (rootstr:match("^{") or rootstr:match("^%(") or _477_())
end
SPECIALS.tset = function(ast, scope, parent)
compiler.assert((3 < #ast), "expected table, key, and value arguments", ast)
local root = compiler.compile1(ast[2], scope, parent, {nval = 1})[1]
local keys = {}
for i = 3, (#ast - 1) do
local _479_ = compiler.compile1(ast[i], scope, parent, {nval = 1})
local key = _479_[1]
table.insert(keys, tostring(key))
end
local value = compiler.compile1(ast[#ast], scope, parent, {nval = 1})[1]
local rootstr = tostring(root)
local fmtstr = nil
if disambiguate_3f(rootstr, parent) then
fmtstr = "do end (%s)[%s] = %s"
else
fmtstr = "%s[%s] = %s"
end
return compiler.emit(parent, fmtstr:format(rootstr, table.concat(keys, "]["), tostring(value)), ast)
end
doc_special("tset", {"tbl", "key1", "...", "keyN", "val"}, "Set the value of a table field. Can take additional keys to set\nnested values, but all parents must contain an existing table.")
local function calculate_if_target(scope, opts)
if not (opts.tail or opts.target or opts.nval) then
return "iife", true, nil
elseif (opts.nval and (opts.nval ~= 0) and not opts.target) then
local accum = {}
local target_exprs = {}
for i = 1, opts.nval do
local s = compiler.gensym(scope)
accum[i] = s
target_exprs[i] = utils.expr(s, "sym")
end
return "target", opts.tail, table.concat(accum, ", "), target_exprs
else
return "none", opts.tail, opts.target
end
end
local function if_2a(ast, scope, parent, opts)
compiler.assert((2 < #ast), "expected condition and body", ast)
if ((1 == (#ast % 2)) and (ast[(#ast - 1)] == true)) then
table.remove(ast, (#ast - 1))
end
if (1 == (#ast % 2)) then
table.insert(ast, utils.sym("nil"))
end
if (#ast == 2) then
return SPECIALS["do"](utils.list(utils.sym("do"), ast[2]), scope, parent, opts)
else
local do_scope = compiler["make-scope"](scope)
local branches = {}
local wrapper, inner_tail, inner_target, target_exprs = calculate_if_target(scope, opts)
local body_opts = {nval = opts.nval, tail = inner_tail, target = inner_target}
local function compile_body(i)
local chunk = {}
local cscope = compiler["make-scope"](do_scope)
compiler["keep-side-effects"](compiler.compile1(ast[i], cscope, chunk, body_opts), chunk, nil, ast[i])
return {chunk = chunk, scope = cscope}
end
for i = 2, (#ast - 1), 2 do
local condchunk = {}
local res = compiler.compile1(ast[i], do_scope, condchunk, {nval = 1})
local cond = res[1]
local branch = compile_body((i + 1))
branch.cond = cond
branch.condchunk = condchunk
branch.nested = ((i ~= 2) and (next(condchunk, nil) == nil))
table.insert(branches, branch)
end
local else_branch = compile_body(#ast)
local s = compiler.gensym(scope)
local buffer = {}
local last_buffer = buffer
for i = 1, #branches do
local branch = branches[i]
local fstr = nil
if not branch.nested then
fstr = "if %s then"
else
fstr = "elseif %s then"
end
local cond = tostring(branch.cond)
local cond_line = fstr:format(cond)
if branch.nested then
compiler.emit(last_buffer, branch.condchunk, ast)
else
for _, v in ipairs(branch.condchunk) do
compiler.emit(last_buffer, v, ast)
end
end
compiler.emit(last_buffer, cond_line, ast)
compiler.emit(last_buffer, branch.chunk, ast)
if (i == #branches) then
compiler.emit(last_buffer, "else", ast)
compiler.emit(last_buffer, else_branch.chunk, ast)
compiler.emit(last_buffer, "end", ast)
elseif not branches[(i + 1)].nested then
local next_buffer = {}
compiler.emit(last_buffer, "else", ast)
compiler.emit(last_buffer, next_buffer, ast)
compiler.emit(last_buffer, "end", ast)
last_buffer = next_buffer
end
end
if (wrapper == "iife") then
local iifeargs = ((scope.vararg and "...") or "")
compiler.emit(parent, ("local function %s(%s)"):format(tostring(s), iifeargs), ast)
compiler.emit(parent, buffer, ast)
compiler.emit(parent, "end", ast)
return utils.expr(("%s(%s)"):format(tostring(s), iifeargs), "statement")
elseif (wrapper == "none") then
for i = 1, #buffer do
compiler.emit(parent, buffer[i], ast)
end
return {returned = true}
else
compiler.emit(parent, ("local %s"):format(inner_target), ast)
for i = 1, #buffer do
compiler.emit(parent, buffer[i], ast)
end
return target_exprs
end
end
end
SPECIALS["if"] = if_2a
doc_special("if", {"cond1", "body1", "...", "condN", "bodyN"}, "Conditional form.\nTakes any number of condition/body pairs and evaluates the first body where\nthe condition evaluates to truthy. Similar to cond in other lisps.")
local function remove_until_condition(bindings)
local last_item = bindings[(#bindings - 1)]
if ((utils["sym?"](last_item) and (tostring(last_item) == "&until")) or ("until" == last_item)) then
table.remove(bindings, (#bindings - 1))
return table.remove(bindings)
end
end
local function compile_until(condition, scope, chunk)
if condition then
local _490_ = compiler.compile1(condition, scope, chunk, {nval = 1})
local condition_lua = _490_[1]
return compiler.emit(chunk, ("if %s then break end"):format(tostring(condition_lua)), utils.expr(condition, "expression"))
end
end
SPECIALS.each = function(ast, scope, parent)
compiler.assert((3 <= #ast), "expected body expression", ast[1])
compiler.assert(utils["table?"](ast[2]), "expected binding table", ast)
local binding = setmetatable(utils.copy(ast[2]), getmetatable(ast[2]))
local until_condition = remove_until_condition(binding)
local iter = table.remove(binding, #binding)
local destructures = {}
local new_manglings = {}
local sub_scope = compiler["make-scope"](scope)
local function destructure_binding(v)
compiler.assert(not utils["string?"](v), ("unexpected iterator clause " .. tostring(v)), binding)
if utils["sym?"](v) then
return compiler["declare-local"](v, {}, sub_scope, ast, new_manglings)
else
local raw = utils.sym(compiler.gensym(sub_scope))
destructures[raw] = v
return compiler["declare-local"](raw, {}, sub_scope, ast)
end
end
local bind_vars = utils.map(binding, destructure_binding)
local vals = compiler.compile1(iter, scope, parent)
local val_names = utils.map(vals, tostring)
local chunk = {}
compiler.assert(bind_vars[1], "expected binding and iterator", ast)
compiler.emit(parent, ("for %s in %s do"):format(table.concat(bind_vars, ", "), table.concat(val_names, ", ")), ast)
for raw, args in utils.stablepairs(destructures) do
compiler.destructure(args, raw, ast, sub_scope, chunk, {declaration = true, nomulti = true, symtype = "each"})
end
compiler["apply-manglings"](sub_scope, new_manglings, ast)
compile_until(until_condition, sub_scope, chunk)
compile_do(ast, sub_scope, chunk, 3)
compiler.emit(parent, chunk, ast)
return compiler.emit(parent, "end", ast)
end
doc_special("each", {"[key value (iterator)]", "..."}, "Runs the body once for each set of values provided by the given iterator.\nMost commonly used with ipairs for sequential tables or pairs for undefined\norder, but can be used with any iterator.", true)
local function while_2a(ast, scope, parent)
local len1 = #parent
local condition = compiler.compile1(ast[2], scope, parent, {nval = 1})[1]
local len2 = #parent
local sub_chunk = {}
if (len1 ~= len2) then
for i = (len1 + 1), len2 do
table.insert(sub_chunk, parent[i])
parent[i] = nil
end
compiler.emit(parent, "while true do", ast)
compiler.emit(sub_chunk, ("if not %s then break end"):format(condition[1]), ast)
else
compiler.emit(parent, ("while " .. tostring(condition) .. " do"), ast)
end
compile_do(ast, compiler["make-scope"](scope), sub_chunk, 3)
compiler.emit(parent, sub_chunk, ast)
return compiler.emit(parent, "end", ast)
end
SPECIALS["while"] = while_2a
doc_special("while", {"condition", "..."}, "The classic while loop. Evaluates body until a condition is non-truthy.", true)
local function for_2a(ast, scope, parent)
compiler.assert(utils["table?"](ast[2]), "expected binding table", ast)
local ranges = setmetatable(utils.copy(ast[2]), getmetatable(ast[2]))
local until_condition = remove_until_condition(ranges)
local binding_sym = table.remove(ranges, 1)
local sub_scope = compiler["make-scope"](scope)
local range_args = {}
local chunk = {}
compiler.assert(utils["sym?"](binding_sym), ("unable to bind %s %s"):format(type(binding_sym), tostring(binding_sym)), ast[2])
compiler.assert((3 <= #ast), "expected body expression", ast[1])
compiler.assert((#ranges <= 3), "unexpected arguments", ranges)
compiler.assert((1 < #ranges), "expected range to include start and stop", ranges)
for i = 1, math.min(#ranges, 3) do
range_args[i] = tostring(compiler.compile1(ranges[i], scope, parent, {nval = 1})[1])
end
compiler.emit(parent, ("for %s = %s do"):format(compiler["declare-local"](binding_sym, {}, sub_scope, ast), table.concat(range_args, ", ")), ast)
compile_until(until_condition, sub_scope, chunk)
compile_do(ast, sub_scope, chunk, 3)
compiler.emit(parent, chunk, ast)
return compiler.emit(parent, "end", ast)
end
SPECIALS["for"] = for_2a
doc_special("for", {"[index start stop step?]", "..."}, "Numeric loop construct.\nEvaluates body once for each value between start and stop (inclusive).", true)
local function native_method_call(ast, _scope, _parent, target, args)
local _494_ = ast
local _ = _494_[1]
local _0 = _494_[2]
local method_string = _494_[3]
local call_string = nil
if ((target.type == "literal") or (target.type == "varg") or (target.type == "expression")) then
call_string = "(%s):%s(%s)"
else
call_string = "%s:%s(%s)"
end
return utils.expr(string.format(call_string, tostring(target), method_string, table.concat(args, ", ")), "statement")
end
local function nonnative_method_call(ast, scope, parent, target, args)
local method_string = tostring(compiler.compile1(ast[3], scope, parent, {nval = 1})[1])
local args0 = {tostring(target), unpack(args)}
return utils.expr(string.format("%s[%s](%s)", tostring(target), method_string, table.concat(args0, ", ")), "statement")
end
local function double_eval_protected_method_call(ast, scope, parent, target, args)
local method_string = tostring(compiler.compile1(ast[3], scope, parent, {nval = 1})[1])
local call = "(function(tgt, m, ...) return tgt[m](tgt, ...) end)(%s, %s)"
table.insert(args, 1, method_string)
return utils.expr(string.format(call, tostring(target), table.concat(args, ", ")), "statement")
end
local function method_call(ast, scope, parent)
compiler.assert((2 < #ast), "expected at least 2 arguments", ast)
local _496_ = compiler.compile1(ast[2], scope, parent, {nval = 1})
local target = _496_[1]
local args = {}
for i = 4, #ast do
local subexprs = nil
local _497_
if (i ~= #ast) then
_497_ = 1
else
_497_ = nil
end
subexprs = compiler.compile1(ast[i], scope, parent, {nval = _497_})
utils.map(subexprs, tostring, args)
end
if (utils["string?"](ast[3]) and utils["valid-lua-identifier?"](ast[3])) then
return native_method_call(ast, scope, parent, target, args)
elseif (target.type == "sym") then
return nonnative_method_call(ast, scope, parent, target, args)
else
return double_eval_protected_method_call(ast, scope, parent, target, args)
end
end
SPECIALS[":"] = method_call
doc_special(":", {"tbl", "method-name", "..."}, "Call the named method on tbl with the provided args.\nMethod name doesn't have to be known at compile-time; if it is, use\n(tbl:method-name ...) instead.")
SPECIALS.comment = function(ast, _, parent)
local c = nil
local _500_
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for i, elt in ipairs(ast) do
local val_19_ = nil
if (i ~= 1) then
val_19_ = view(elt, {["one-line?"] = true})
else
val_19_ = nil
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
_500_ = tbl_17_
end
c = table.concat(_500_, " "):gsub("%]%]", "]\\]")
return compiler.emit(parent, ("--[[ " .. c .. " ]]"), ast)
end
doc_special("comment", {"..."}, "Comment which will be emitted in Lua output.", true)
local function hashfn_max_used(f_scope, i, max)
local max0 = nil
if f_scope.symmeta[("$" .. i)].used then
max0 = i
else
max0 = max
end
if (i < 9) then
return hashfn_max_used(f_scope, (i + 1), max0)
else
return max0
end
end
SPECIALS.hashfn = function(ast, scope, parent)
compiler.assert((#ast == 2), "expected one argument", ast)
local f_scope = nil
do
local _505_0 = compiler["make-scope"](scope)
_505_0["vararg"] = false
_505_0["hashfn"] = true
f_scope = _505_0
end
local f_chunk = {}
local name = compiler.gensym(scope)
local symbol = utils.sym(name)
local args = {}
compiler["declare-local"](symbol, {}, scope, ast)
for i = 1, 9 do
args[i] = compiler["declare-local"](utils.sym(("$" .. i)), {}, f_scope, ast)
end
local function walker(idx, node, _3fparent_node)
if utils["sym?"](node, "$...") then
f_scope.vararg = true
if _3fparent_node then
_3fparent_node[idx] = utils.varg()
return nil
else
return utils.varg()
end
else
return ((utils["list?"](node) and (not _3fparent_node or not utils["sym?"](node[1], "hashfn"))) or utils["table?"](node))
end
end
utils["walk-tree"](ast, walker)
compiler.compile1(ast[2], f_scope, f_chunk, {tail = true})
local max_used = hashfn_max_used(f_scope, 1, 0)
if f_scope.vararg then
compiler.assert((max_used == 0), "$ and $... in hashfn are mutually exclusive", ast)
end
local arg_str = nil
if f_scope.vararg then
arg_str = tostring(utils.varg())
else
arg_str = table.concat(args, ", ", 1, max_used)
end
compiler.emit(parent, string.format("local function %s(%s)", name, arg_str), ast)
compiler.emit(parent, f_chunk, ast)
compiler.emit(parent, "end", ast)
return utils.expr(name, "sym")
end
doc_special("hashfn", {"..."}, "Function literal shorthand; args are either $... OR $1, $2, etc.")
local function maybe_short_circuit_protect(ast, i, name, _510_0)
local _511_ = _510_0
local mac = _511_["macros"]
local call = (utils["list?"](ast) and tostring(ast[1]))
if ((("or" == name) or ("and" == name)) and (1 < i) and (mac[call] or ("set" == call) or ("tset" == call) or ("global" == call))) then
return utils.list(utils.list(utils.sym("fn"), utils.sequence(utils.varg()), ast))
else
return ast
end
end
local function operator_special(name, zero_arity, unary_prefix, ast, scope, parent)
local len = #ast
local operands = {}
local padded_op = (" " .. name .. " ")
for i = 2, len do
local subast = maybe_short_circuit_protect(ast[i], i, name, scope)
local subexprs = compiler.compile1(subast, scope, parent)
if (i == len) then
utils.map(subexprs, tostring, operands)
else
table.insert(operands, tostring(subexprs[1]))
end
end
local _514_0 = #operands
if (_514_0 == 0) then
local _515_
do
compiler.assert(zero_arity, "Expected more than 0 arguments", ast)
_515_ = zero_arity
end
return utils.expr(_515_, "literal")
elseif (_514_0 == 1) then
if utils["varg?"](ast[2]) then
return compiler.assert(false, "tried to use vararg with operator", ast)
elseif unary_prefix then
return ("(" .. unary_prefix .. padded_op .. operands[1] .. ")")
else
return operands[1]
end
else
local _ = _514_0
return ("(" .. table.concat(operands, padded_op) .. ")")
end
end
local function define_arithmetic_special(name, zero_arity, unary_prefix, _3flua_name)
local _519_
do
local _518_0 = (_3flua_name or name)
local function _520_(...)
return operator_special(_518_0, zero_arity, unary_prefix, ...)
end
_519_ = _520_
end
SPECIALS[name] = _519_
return doc_special(name, {"a", "b", "..."}, "Arithmetic operator; works the same as Lua but accepts more arguments.")
end
define_arithmetic_special("+", "0")
define_arithmetic_special("..", "''")
define_arithmetic_special("^")
define_arithmetic_special("-", nil, "")
define_arithmetic_special("*", "1")
define_arithmetic_special("%")
define_arithmetic_special("/", nil, "1")
define_arithmetic_special("//", nil, "1")
SPECIALS["or"] = function(ast, scope, parent)
return operator_special("or", "false", nil, ast, scope, parent)
end
SPECIALS["and"] = function(ast, scope, parent)
return operator_special("and", "true", nil, ast, scope, parent)
end
doc_special("and", {"a", "b", "..."}, "Boolean operator; works the same as Lua but accepts more arguments.")
doc_special("or", {"a", "b", "..."}, "Boolean operator; works the same as Lua but accepts more arguments.")
local function bitop_special(native_name, lib_name, zero_arity, unary_prefix, ast, scope, parent)
if (#ast == 1) then
return compiler.assert(zero_arity, "Expected more than 0 arguments.", ast)
else
local len = #ast
local operands = {}
local padded_native_name = (" " .. native_name .. " ")
local prefixed_lib_name = ("bit." .. lib_name)
for i = 2, len do
local subexprs = nil
local _521_
if (i ~= len) then
_521_ = 1
else
_521_ = nil
end
subexprs = compiler.compile1(ast[i], scope, parent, {nval = _521_})
utils.map(subexprs, tostring, operands)
end
if (#operands == 1) then
if utils.root.options.useBitLib then
return (prefixed_lib_name .. "(" .. unary_prefix .. ", " .. operands[1] .. ")")
else
return ("(" .. unary_prefix .. padded_native_name .. operands[1] .. ")")
end
else
if utils.root.options.useBitLib then
return (prefixed_lib_name .. "(" .. table.concat(operands, ", ") .. ")")
else
return ("(" .. table.concat(operands, padded_native_name) .. ")")
end
end
end
end
local function define_bitop_special(name, zero_arity, unary_prefix, native)
local function _527_(...)
return bitop_special(native, name, zero_arity, unary_prefix, ...)
end
SPECIALS[name] = _527_
return nil
end
define_bitop_special("lshift", nil, "1", "<<")
define_bitop_special("rshift", nil, "1", ">>")
define_bitop_special("band", "0", "0", "&")
define_bitop_special("bor", "0", "0", "|")
define_bitop_special("bxor", "0", "0", "~")
doc_special("lshift", {"x", "n"}, "Bitwise logical left shift of x by n bits.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
doc_special("rshift", {"x", "n"}, "Bitwise logical right shift of x by n bits.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
doc_special("band", {"x1", "x2", "..."}, "Bitwise AND of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
doc_special("bor", {"x1", "x2", "..."}, "Bitwise OR of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
doc_special("bxor", {"x1", "x2", "..."}, "Bitwise XOR of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
SPECIALS.bnot = function(ast, scope, parent)
compiler.assert((#ast == 2), "expected one argument", ast)
local _528_ = compiler.compile1(ast[2], scope, parent, {nval = 1})
local value = _528_[1]
if utils.root.options.useBitLib then
return ("bit.bnot(" .. tostring(value) .. ")")
else
return ("~(" .. tostring(value) .. ")")
end
end
doc_special("bnot", {"x"}, "Bitwise negation; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
doc_special("..", {"a", "b", "..."}, "String concatenation operator; works the same as Lua but accepts more arguments.")
local function native_comparator(op, _530_0, scope, parent)
local _531_ = _530_0
local _ = _531_[1]
local lhs_ast = _531_[2]
local rhs_ast = _531_[3]
local _532_ = compiler.compile1(lhs_ast, scope, parent, {nval = 1})
local lhs = _532_[1]
local _533_ = compiler.compile1(rhs_ast, scope, parent, {nval = 1})
local rhs = _533_[1]
return string.format("(%s %s %s)", tostring(lhs), op, tostring(rhs))
end
local function idempotent_comparator(op, chain_op, ast, scope, parent)
local vals = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for i = 2, #ast do
local val_19_ = tostring(compiler.compile1(ast[i], scope, parent, {nval = 1})[1])
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
vals = tbl_17_
end
local comparisons = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for i = 1, (#vals - 1) do
local val_19_ = string.format("(%s %s %s)", vals[i], op, vals[(i + 1)])
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
comparisons = tbl_17_
end
local chain = string.format(" %s ", (chain_op or "and"))
return ("(" .. table.concat(comparisons, chain) .. ")")
end
local function double_eval_protected_comparator(op, chain_op, ast, scope, parent)
local arglist = {}
local comparisons = {}
local vals = {}
local chain = string.format(" %s ", (chain_op or "and"))
for i = 2, #ast do
table.insert(arglist, tostring(compiler.gensym(scope)))
table.insert(vals, tostring(compiler.compile1(ast[i], scope, parent, {nval = 1})[1]))
end
do
local tbl_17_ = comparisons
local i_18_ = #tbl_17_
for i = 1, (#arglist - 1) do
local val_19_ = string.format("(%s %s %s)", arglist[i], op, arglist[(i + 1)])
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
end
return string.format("(function(%s) return %s end)(%s)", table.concat(arglist, ","), table.concat(comparisons, chain), table.concat(vals, ","))
end
local function define_comparator_special(name, _3flua_op, _3fchain_op)
do
local op = (_3flua_op or name)
local function opfn(ast, scope, parent)
compiler.assert((2 < #ast), "expected at least two arguments", ast)
if (3 == #ast) then
return native_comparator(op, ast, scope, parent)
elseif utils["every?"]({unpack(ast, 2)}, utils["idempotent-expr?"]) then
return idempotent_comparator(op, _3fchain_op, ast, scope, parent)
else
return double_eval_protected_comparator(op, _3fchain_op, ast, scope, parent)
end
end
SPECIALS[name] = opfn
end
return doc_special(name, {"a", "b", "..."}, "Comparison operator; works the same as Lua but accepts more arguments.")
end
define_comparator_special(">")
define_comparator_special("<")
define_comparator_special(">=")
define_comparator_special("<=")
define_comparator_special("=", "==")
define_comparator_special("not=", "~=", "or")
local function define_unary_special(op, _3frealop)
local function opfn(ast, scope, parent)
compiler.assert((#ast == 2), "expected one argument", ast)
local tail = compiler.compile1(ast[2], scope, parent, {nval = 1})
return ((_3frealop or op) .. tostring(tail[1]))
end
SPECIALS[op] = opfn
return nil
end
define_unary_special("not", "not ")
doc_special("not", {"x"}, "Logical operator; works the same as Lua.")
define_unary_special("length", "#")
doc_special("length", {"x"}, "Returns the length of a table or string.")
SPECIALS["~="] = SPECIALS["not="]
SPECIALS["#"] = SPECIALS.length
SPECIALS.quote = function(ast, scope, parent)
compiler.assert((#ast == 2), "expected one argument", ast)
local runtime, this_scope = true, scope
while this_scope do
this_scope = this_scope.parent
if (this_scope == compiler.scopes.compiler) then
runtime = false
end
end
return compiler["do-quote"](ast[2], scope, parent, runtime)
end
doc_special("quote", {"x"}, "Quasiquote the following form. Only works in macro/compiler scope.")
local macro_loaded = {}
local function safe_getmetatable(tbl)
local mt = getmetatable(tbl)
assert((mt ~= getmetatable("")), "Illegal metatable access!")
return mt
end
local safe_require = nil
local function safe_compiler_env()
local _540_
do
local _539_0 = rawget(_G, "utf8")
if (nil ~= _539_0) then
_540_ = utils.copy(_539_0)
else
_540_ = _539_0
end
end
return {_VERSION = _VERSION, assert = assert, bit = rawget(_G, "bit"), error = error, getmetatable = safe_getmetatable, ipairs = ipairs, math = utils.copy(math), next = next, pairs = utils.stablepairs, pcall = pcall, print = print, rawequal = rawequal, rawget = rawget, rawlen = rawget(_G, "rawlen"), rawset = rawset, require = safe_require, select = select, setmetatable = setmetatable, string = utils.copy(string), table = utils.copy(table), tonumber = tonumber, tostring = tostring, type = type, utf8 = _540_, xpcall = xpcall}
end
local function combined_mt_pairs(env)
local combined = {}
local _542_ = getmetatable(env)
local __index = _542_["__index"]
if ("table" == type(__index)) then
for k, v in pairs(__index) do
combined[k] = v
end
end
for k, v in next, env, nil do
combined[k] = v
end
return next, combined, nil
end
local function make_compiler_env(ast, scope, parent, _3fopts)
local provided = nil
do
local _544_0 = (_3fopts or utils.root.options)
if ((_G.type(_544_0) == "table") and (_544_0["compiler-env"] == "strict")) then
provided = safe_compiler_env()
elseif ((_G.type(_544_0) == "table") and (nil ~= _544_0.compilerEnv)) then
local compilerEnv = _544_0.compilerEnv
provided = compilerEnv
elseif ((_G.type(_544_0) == "table") and (nil ~= _544_0["compiler-env"])) then
local compiler_env = _544_0["compiler-env"]
provided = compiler_env
else
local _ = _544_0
provided = safe_compiler_env()
end
end
local env = nil
local function _546_()
return compiler.scopes.macro
end
local function _547_(symbol)
compiler.assert(compiler.scopes.macro, "must call from macro", ast)
return compiler.scopes.macro.manglings[tostring(symbol)]
end
local function _548_(base)
return utils.sym(compiler.gensym((compiler.scopes.macro or scope), base))
end
local function _549_(form)
compiler.assert(compiler.scopes.macro, "must call from macro", ast)
return compiler.macroexpand(form, compiler.scopes.macro)
end
env = {["assert-compile"] = compiler.assert, ["ast-source"] = utils["ast-source"], ["comment?"] = utils["comment?"], ["get-scope"] = _546_, ["in-scope?"] = _547_, ["list?"] = utils["list?"], ["macro-loaded"] = macro_loaded, ["multi-sym?"] = utils["multi-sym?"], ["sequence?"] = utils["sequence?"], ["sym?"] = utils["sym?"], ["table?"] = utils["table?"], ["varg?"] = utils["varg?"], _AST = ast, _CHUNK = parent, _IS_COMPILER = true, _SCOPE = scope, _SPECIALS = compiler.scopes.global.specials, _VARARG = utils.varg(), comment = utils.comment, gensym = _548_, list = utils.list, macroexpand = _549_, sequence = utils.sequence, sym = utils.sym, unpack = unpack, version = utils.version, view = view}
env._G = env
return setmetatable(env, {__index = provided, __newindex = provided, __pairs = combined_mt_pairs})
end
local function _550_(...)
local tbl_17_ = {}
local i_18_ = #tbl_17_
for c in string.gmatch((package.config or ""), "([^\n]+)") do
local val_19_ = c
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
local _552_ = _550_(...)
local dirsep = _552_[1]
local pathsep = _552_[2]
local pathmark = _552_[3]
local pkg_config = {dirsep = (dirsep or "/"), pathmark = (pathmark or "?"), pathsep = (pathsep or ";")}
local function escapepat(str)
return string.gsub(str, "[^%w]", "%%%1")
end
local function search_module(modulename, _3fpathstring)
local pathsepesc = escapepat(pkg_config.pathsep)
local pattern = ("([^%s]*)%s"):format(pathsepesc, pathsepesc)
local no_dot_module = modulename:gsub("%.", pkg_config.dirsep)
local fullpath = ((_3fpathstring or utils["fennel-module"].path) .. pkg_config.pathsep)
local function try_path(path)
local filename = path:gsub(escapepat(pkg_config.pathmark), no_dot_module)
local filename2 = path:gsub(escapepat(pkg_config.pathmark), modulename)
local _553_0 = (io.open(filename) or io.open(filename2))
if (nil ~= _553_0) then
local file = _553_0
file:close()
return filename
else
local _ = _553_0
return nil, ("no file '" .. filename .. "'")
end
end
local function find_in_path(start, _3ftried_paths)
local _555_0 = fullpath:match(pattern, start)
if (nil ~= _555_0) then
local path = _555_0
local _556_0, _557_0 = try_path(path)
if (nil ~= _556_0) then
local filename = _556_0
return filename
elseif ((_556_0 == nil) and (nil ~= _557_0)) then
local error = _557_0
local function _559_()
local _558_0 = (_3ftried_paths or {})
table.insert(_558_0, error)
return _558_0
end
return find_in_path((start + #path + 1), _559_())
end
else
local _ = _555_0
local function _561_()
local tried_paths = table.concat((_3ftried_paths or {}), "\n\9")
if (_VERSION < "Lua 5.4") then
return ("\n\9" .. tried_paths)
else
return tried_paths
end
end
return nil, _561_()
end
end
return find_in_path(1)
end
local function make_searcher(_3foptions)
local function _564_(module_name)
local opts = utils.copy(utils.root.options)
for k, v in pairs((_3foptions or {})) do
opts[k] = v
end
opts["module-name"] = module_name
local _565_0, _566_0 = search_module(module_name)
if (nil ~= _565_0) then
local filename = _565_0
local function _567_(...)
return utils["fennel-module"].dofile(filename, opts, ...)
end
return _567_, filename
elseif ((_565_0 == nil) and (nil ~= _566_0)) then
local error = _566_0
return error
end
end
return _564_
end
local function dofile_with_searcher(fennel_macro_searcher, filename, opts, ...)
local searchers = (package.loaders or package.searchers or {})
local _ = table.insert(searchers, 1, fennel_macro_searcher)
local m = utils["fennel-module"].dofile(filename, opts, ...)
table.remove(searchers, 1)
return m
end
local function fennel_macro_searcher(module_name)
local opts = nil
do
local _569_0 = utils.copy(utils.root.options)
_569_0["module-name"] = module_name
_569_0["env"] = "_COMPILER"
_569_0["requireAsInclude"] = false
_569_0["allowedGlobals"] = nil
opts = _569_0
end
local _570_0 = search_module(module_name, utils["fennel-module"]["macro-path"])
if (nil ~= _570_0) then
local filename = _570_0
local _571_
if (opts["compiler-env"] == _G) then
local function _572_(...)
return dofile_with_searcher(fennel_macro_searcher, filename, opts, ...)
end
_571_ = _572_
else
local function _573_(...)
return utils["fennel-module"].dofile(filename, opts, ...)
end
_571_ = _573_
end
return _571_, filename
end
end
local function lua_macro_searcher(module_name)
local _576_0 = search_module(module_name, package.path)
if (nil ~= _576_0) then
local filename = _576_0
local code = nil
do
local f = io.open(filename)
local function close_handlers_10_(ok_11_, ...)
f:close()
if ok_11_ then
return ...
else
return error(..., 0)
end
end
local function _578_()
return assert(f:read("*a"))
end
code = close_handlers_10_(_G.xpcall(_578_, (package.loaded.fennel or debug).traceback))
end
local chunk = load_code(code, make_compiler_env(), filename)
return chunk, filename
end
end
local macro_searchers = {fennel_macro_searcher, lua_macro_searcher}
local function search_macro_module(modname, n)
local _580_0 = macro_searchers[n]
if (nil ~= _580_0) then
local f = _580_0
local _581_0, _582_0 = f(modname)
if ((nil ~= _581_0) and true) then
local loader = _581_0
local _3ffilename = _582_0
return loader, _3ffilename
else
local _ = _581_0
return search_macro_module(modname, (n + 1))
end
end
end
local function sandbox_fennel_module(modname)
if ((modname == "fennel.macros") or (package and package.loaded and ("table" == type(package.loaded[modname])) and (package.loaded[modname].metadata == compiler.metadata))) then
local function _585_(_, ...)
return (compiler.metadata):setall(...)
end
return {metadata = {setall = _585_}, view = view}
end
end
local function _587_(modname)
local function _588_()
local loader, filename = search_macro_module(modname, 1)
compiler.assert(loader, (modname .. " module not found."))
macro_loaded[modname] = loader(modname, filename)
return macro_loaded[modname]
end
return (macro_loaded[modname] or sandbox_fennel_module(modname) or _588_())
end
safe_require = _587_
local function add_macros(macros_2a, ast, scope)
compiler.assert(utils["table?"](macros_2a), "expected macros to be table", ast)
for k, v in pairs(macros_2a) do
compiler.assert((type(v) == "function"), "expected each macro to be function", ast)
compiler["check-binding-valid"](utils.sym(k), scope, ast, {["macro?"] = true})
scope.macros[k] = v
end
return nil
end
local function resolve_module_name(_589_0, _scope, _parent, opts)
local _590_ = _589_0
local second = _590_[2]
local filename = _590_["filename"]
local filename0 = (filename or (utils["table?"](second) and second.filename))
local module_name = utils.root.options["module-name"]
local modexpr = compiler.compile(second, opts)
local modname_chunk = load_code(modexpr)
return modname_chunk(module_name, filename0)
end
SPECIALS["require-macros"] = function(ast, scope, parent, _3freal_ast)
compiler.assert((#ast == 2), "Expected one module name argument", (_3freal_ast or ast))
local modname = resolve_module_name(ast, scope, parent, {})
compiler.assert(utils["string?"](modname), "module name must compile to string", (_3freal_ast or ast))
if not macro_loaded[modname] then
local loader, filename = search_macro_module(modname, 1)
compiler.assert(loader, (modname .. " module not found."), ast)
macro_loaded[modname] = compiler.assert(utils["table?"](loader(modname, filename)), "expected macros to be table", (_3freal_ast or ast))
end
if ("import-macros" == tostring(ast[1])) then
return macro_loaded[modname]
else
return add_macros(macro_loaded[modname], ast, scope)
end
end
doc_special("require-macros", {"macro-module-name"}, "Load given module and use its contents as macro definitions in current scope.\nMacro module should return a table of macro functions with string keys.\nConsider using import-macros instead as it is more flexible.")
local function emit_included_fennel(src, path, opts, sub_chunk)
local subscope = compiler["make-scope"](utils.root.scope.parent)
local forms = {}
if utils.root.options.requireAsInclude then
subscope.specials.require = compiler["require-include"]
end
for _, val in parser.parser(parser["string-stream"](src), path) do
table.insert(forms, val)
end
for i = 1, #forms do
local subopts = nil
if (i == #forms) then
subopts = {tail = true}
else
subopts = {nval = 0}
end
utils["propagate-options"](opts, subopts)
compiler.compile1(forms[i], subscope, sub_chunk, subopts)
end
return nil
end
local function include_path(ast, opts, path, mod, fennel_3f)
utils.root.scope.includes[mod] = "fnl/loading"
local src = nil
do
local f = assert(io.open(path))
local function close_handlers_10_(ok_11_, ...)
f:close()
if ok_11_ then
return ...
else
return error(..., 0)
end
end
local function _596_()
return assert(f:read("*all")):gsub("[\13\n]*$", "")
end
src = close_handlers_10_(_G.xpcall(_596_, (package.loaded.fennel or debug).traceback))
end
local ret = utils.expr(("require(\"" .. mod .. "\")"), "statement")
local target = ("package.preload[%q]"):format(mod)
local preload_str = (target .. " = " .. target .. " or function(...)")
local temp_chunk, sub_chunk = {}, {}
compiler.emit(temp_chunk, preload_str, ast)
compiler.emit(temp_chunk, sub_chunk)
compiler.emit(temp_chunk, "end", ast)
for _, v in ipairs(temp_chunk) do
table.insert(utils.root.chunk, v)
end
if fennel_3f then
emit_included_fennel(src, path, opts, sub_chunk)
else
compiler.emit(sub_chunk, src, ast)
end
utils.root.scope.includes[mod] = ret
return ret
end
local function include_circular_fallback(mod, modexpr, fallback, ast)
if (utils.root.scope.includes[mod] == "fnl/loading") then
compiler.assert(fallback, "circular include detected", ast)
return fallback(modexpr)
end
end
SPECIALS.include = function(ast, scope, parent, opts)
compiler.assert((#ast == 2), "expected one argument", ast)
local modexpr = nil
do
local _599_0, _600_0 = pcall(resolve_module_name, ast, scope, parent, opts)
if ((_599_0 == true) and (nil ~= _600_0)) then
local modname = _600_0
modexpr = utils.expr(string.format("%q", modname), "literal")
else
local _ = _599_0
modexpr = compiler.compile1(ast[2], scope, parent, {nval = 1})[1]
end
end
if ((modexpr.type ~= "literal") or ((modexpr[1]):byte() ~= 34)) then
if opts.fallback then
return opts.fallback(modexpr)
else
return compiler.assert(false, "module name must be string literal", ast)
end
else
local mod = load_code(("return " .. modexpr[1]))()
local oldmod = utils.root.options["module-name"]
local _ = nil
utils.root.options["module-name"] = mod
_ = nil
local res = nil
local function _604_()
local _603_0 = search_module(mod)
if (nil ~= _603_0) then
local fennel_path = _603_0
return include_path(ast, opts, fennel_path, mod, true)
else
local _0 = _603_0
local lua_path = search_module(mod, package.path)
if lua_path then
return include_path(ast, opts, lua_path, mod, false)
elseif opts.fallback then
return opts.fallback(modexpr)
else
return compiler.assert(false, ("module not found " .. mod), ast)
end
end
end
res = ((utils["member?"](mod, (utils.root.options.skipInclude or {})) and opts.fallback(modexpr, true)) or include_circular_fallback(mod, modexpr, opts.fallback, ast) or utils.root.scope.includes[mod] or _604_())
utils.root.options["module-name"] = oldmod
return res
end
end
doc_special("include", {"module-name-literal"}, "Like require but load the target module during compilation and embed it in the\nLua output. The module must be a string literal and resolvable at compile time.")
local function eval_compiler_2a(ast, scope, parent)
local env = make_compiler_env(ast, scope, parent)
local opts = utils.copy(utils.root.options)
opts.scope = compiler["make-scope"](compiler.scopes.compiler)
opts.allowedGlobals = current_global_names(env)
return assert(load_code(compiler.compile(ast, opts), wrap_env(env)))(opts["module-name"], ast.filename)
end
SPECIALS.macros = function(ast, scope, parent)
compiler.assert((#ast == 2), "Expected one table argument", ast)
local macro_tbl = eval_compiler_2a(ast[2], scope, parent)
compiler.assert(utils["table?"](macro_tbl), "Expected one table argument", ast)
return add_macros(macro_tbl, ast, scope)
end
doc_special("macros", {"{:macro-name-1 (fn [...] ...) ... :macro-name-N macro-body-N}"}, "Define all functions in the given table as macros local to the current scope.")
SPECIALS["tail!"] = function(ast, scope, _parent, _608_0)
local _609_ = _608_0
local tail = _609_["tail"]
compiler.assert((#ast == 2), "Expected one argument", ast)
compiler.assert(utils["list?"](ast[2]), "Expected a call as argument", ast)
compiler.assert(tail, "Must be in tail position", ast)
return compiler.compile(ast[2], {nval = 1, scope = scope})
end
doc_special("tail!", {"body"}, "Assert that the body being called is in tail position.")
SPECIALS["eval-compiler"] = function(ast, scope, parent)
local old_first = ast[1]
ast[1] = utils.sym("do")
local val = eval_compiler_2a(ast, scope, parent)
ast[1] = old_first
return val
end
doc_special("eval-compiler", {"..."}, "Evaluate the body at compile-time. Use the macro system instead if possible.", true)
SPECIALS.unquote = function(ast)
return compiler.assert(false, "tried to use unquote outside quote", ast)
end
doc_special("unquote", {"..."}, "Evaluate the argument even if it's in a quoted form.")
return {["current-global-names"] = current_global_names, ["load-code"] = load_code, ["macro-loaded"] = macro_loaded, ["macro-searchers"] = macro_searchers, ["make-compiler-env"] = make_compiler_env, ["make-searcher"] = make_searcher, ["search-module"] = search_module, ["wrap-env"] = wrap_env, doc = doc_2a}
end