From f199a9238db46c9546161be9fa403a84c44fbe84 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sun, 10 Apr 2022 12:24:09 -0400 Subject: [PATCH] Upgrade to Fennel 1.1.0 --- lib/fennel.lua | 1831 ++++++++++++++++++++++++++++-------------------- 1 file changed, 1072 insertions(+), 759 deletions(-) diff --git a/lib/fennel.lua b/lib/fennel.lua index 5ae7c52..bfb8bd0 100644 --- a/lib/fennel.lua +++ b/lib/fennel.lua @@ -6,14 +6,14 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) local view = require("fennel.view") local unpack = (table.unpack or _G.unpack) local function default_read_chunk(parser_state) - local function _519_() + local function _565_() if (0 < parser_state["stack-size"]) then return ".." else return ">> " end end - io.write(_519_()) + io.write(_565_()) io.flush() local input = io.read() return (input and (input .. "\n")) @@ -23,20 +23,20 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return io.write("\n") end local function default_on_error(errtype, err, lua_source) - local function _521_() - local _520_ = errtype - if (_520_ == "Lua Compile") then + local function _567_() + local _566_ = errtype + if (_566_ == "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 (_520_ == "Runtime") then + elseif (_566_ == "Runtime") then return (compiler.traceback(tostring(err), 4) .. "\n") elseif true then - local _ = _520_ + local _ = _566_ return ("%s error: %s\n"):format(errtype, tostring(err)) else return nil end end - return io.write(_521_()) + return io.write(_567_()) end local save_source = table.concat({"local ___i___ = 1", "while true do", " local name, value = debug.getlocal(1, ___i___)", " if(name and name ~= \"___i___\") then", " ___replLocals___[name] = value", " ___i___ = ___i___ + 1", " else break end end"}, "\n") local function splice_save_locals(env, lua_source) @@ -55,30 +55,46 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return table.concat(spliced_source, "\n") 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, method_3f) - for k in utils.allpairs(tbl) do - local k0 - if ((tbl == env) or (tbl == env.___replLocals___)) then - k0 = scope.unmanglings[k] + local function add_partials(input, tbl, prefix) + local scope_first_3f = ((tbl == env) or (tbl == env.___replLocals___)) + local tbl_14_auto = matches + local i_15_auto = #tbl_14_auto + local function _570_() + if scope_first_3f then + return scope.manglings else - k0 = k + return tbl end - if ((#matches < 2000) and (type(k0) == "string") and (input == k0:sub(0, #input)) and (not method_3f or ("function" == type(tbl[k0])))) then - local function _525_() - if method_3f then - return (prefix .. ":" .. k0) - else - return (prefix .. k0) - end + end + for k, is_mangled in utils.allpairs(_570_()) do + if (max_items <= #matches) then break end + local val_16_auto + do + local lookup_k + if scope_first_3f then + lookup_k = is_mangled + else + lookup_k = k end - table.insert(matches, _525_()) + 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_16_auto = (prefix .. k) + else + val_16_auto = nil + end + end + if (nil ~= val_16_auto) then + i_15_auto = (i_15_auto + 1) + do end (tbl_14_auto)[i_15_auto] = val_16_auto else end end - return nil + return tbl_14_auto end local function descend(input, tbl, prefix, add_matches, method_3f) local splitter @@ -92,7 +108,7 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) if (type(tbl[raw_head]) == "table") then stop_looking_3f = true if method_3f then - return add_partials(tail, tbl[raw_head], (prefix .. head), true) + return add_partials(tail, tbl[raw_head], (prefix .. head .. ":")) else return add_matches(tail, tbl[raw_head], (prefix .. head)) end @@ -126,7 +142,7 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return input:match("^%s*,") end local function command_docs() - local _532_ + local _579_ do local tbl_14_auto = {} local i_15_auto = #tbl_14_auto @@ -138,18 +154,18 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) else end end - _532_ = tbl_14_auto + _579_ = tbl_14_auto end - return table.concat(_532_, "\n") + return table.concat(_579_, "\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 ,exit - Leave the repl.\n\nUse ,doc something to see descriptions for individual macros and special forms.\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 _534_, _535_ = pcall(specials["load-code"]("return require(...)", env), module_name) - if ((_534_ == true) and (nil ~= _535_)) then - local old = _535_ + local _581_, _582_ = pcall(specials["load-code"]("return require(...)", env), module_name) + if ((_581_ == true) and (nil ~= _582_)) then + local old = _582_ local _ package.loaded[module_name] = nil _ = nil @@ -161,6 +177,7 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) 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 @@ -175,33 +192,38 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) else end return on_values({"ok"}) - elseif ((_534_ == false) and (nil ~= _535_)) then - local msg = _535_ - local function _540_() - local _539_ = msg:gsub("\n.*", "") - return _539_ + elseif ((_581_ == false) and (nil ~= _582_)) then + local msg = _582_ + if (specials["macro-loaded"])[module_name] then + specials["macro-loaded"][module_name] = nil + return nil + else + local function _587_() + local _586_ = msg:gsub("\n.*", "") + return _586_ + end + return on_error("Runtime", _587_()) end - return on_error("Runtime", _540_()) else return nil end end local function run_command(read, on_error, f) - local _542_, _543_, _544_ = pcall(read) - if ((_542_ == true) and (_543_ == true) and (nil ~= _544_)) then - local val = _544_ + local _590_, _591_, _592_ = pcall(read) + if ((_590_ == true) and (_591_ == true) and (nil ~= _592_)) then + local val = _592_ return f(val) - elseif (_542_ == false) then + elseif (_590_ == false) then return on_error("Parse", "Couldn't parse input.") else return nil end end commands.reload = function(env, read, on_values, on_error) - local function _546_(_241) + local function _594_(_241) return reload(tostring(_241), env, on_values, on_error) end - return run_command(read, on_error, _546_) + return run_command(read, on_error, _594_) end do end (compiler.metadata):set(commands.reload, "fnl/docstring", "Reload the specified module.") commands.reset = function(env, _, on_values) @@ -210,30 +232,30 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) 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 _547_() + local function _595_() return on_values(completer(env, scope, string.char(unpack(chars)):gsub(",complete +", ""):sub(1, -2))) end - return run_command(read, on_error, _547_) + return run_command(read, on_error, _595_) 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 _548_ = type(subtbl) - if (_548_ == "function") then + local _596_ = type(subtbl) + if (_596_ == "function") then if ((prefix .. name)):match(pattern) then table.insert(names, (prefix .. name)) else end - elseif (_548_ == "table") then + elseif (_596_ == "table") then if not seen[subtbl] then - local _551_ + local _599_ do - local _550_ = seen - _550_[subtbl] = true - _551_ = _550_ + local _598_ = seen + _598_[subtbl] = true + _599_ = _598_ end - apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _551_, names) + apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _599_, names) else end else @@ -258,10 +280,10 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return tbl_14_auto end commands.apropos = function(_env, read, on_values, on_error, _scope) - local function _556_(_241) + local function _604_(_241) return on_values(apropos(tostring(_241))) end - return run_command(read, on_error, _556_) + return run_command(read, on_error, _604_) 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) @@ -282,12 +304,12 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) local tgt = package.loaded for _, path0 in ipairs(paths) do if (nil == tgt) then break end - local _559_ + local _607_ do - local _558_ = path0:gsub("%/", ".") - _559_ = _558_ + local _606_ = path0:gsub("%/", ".") + _607_ = _606_ end - tgt = tgt[_559_] + tgt = tgt[_607_] end return tgt end @@ -296,9 +318,9 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) for _, path in ipairs(apropos(".*")) do local tgt = apropos_follow_path(path) if ("function" == type(tgt)) then - local _560_ = (compiler.metadata):get(tgt, "fnl/docstring") - if (nil ~= _560_) then - local docstr = _560_ + local _608_ = (compiler.metadata):get(tgt, "fnl/docstring") + if (nil ~= _608_) then + local docstr = _608_ if docstr:match(pattern) then table.insert(names, path) else @@ -311,10 +333,10 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return names end commands["apropos-doc"] = function(_env, read, on_values, on_error, _scope) - local function _564_(_241) + local function _612_(_241) return on_values(apropos_doc(tostring(_241))) end - return run_command(read, on_error, _564_) + return run_command(read, on_error, _612_) 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) @@ -329,89 +351,112 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return nil end commands["apropos-show-docs"] = function(_env, read, on_values, on_error) - local function _566_(_241) + local function _614_(_241) return apropos_show_docs(on_values, tostring(_241)) end - return run_command(read, on_error, _566_) + return run_command(read, on_error, _614_) 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, _567_, scope) - local _arg_568_ = _567_ - local ___replLocals___ = _arg_568_["___replLocals___"] - local env = _arg_568_ + local function resolve(identifier, _615_, scope) + local _arg_616_ = _615_ + local ___replLocals___ = _arg_616_["___replLocals___"] + local env = _arg_616_ local e - local function _569_(_241, _242) + local function _617_(_241, _242) return (___replLocals___[_242] or env[_242]) end - e = setmetatable({}, {__index = _569_}) - local code = compiler["compile-string"](tostring(identifier), {scope = scope}) - return specials["load-code"](code, e)() + e = setmetatable({}, {__index = _617_}) + local _618_, _619_ = pcall(compiler["compile-string"], tostring(identifier), {scope = scope}) + if ((_618_ == true) and (nil ~= _619_)) then + local code = _619_ + local _620_ = specials["load-code"](code, e)() + local function _621_() + local x = _620_ + return (type(x) == "function") + end + if ((nil ~= _620_) and _621_()) then + local x = _620_ + return x + else + return nil + end + else + return nil + end end commands.find = function(env, read, on_values, on_error, scope) - local function _570_(_241) - local _571_ + local function _624_(_241) + local _625_ do - local _572_ = utils["sym?"](_241) - if (nil ~= _572_) then - local _573_ = resolve(_572_, env, scope) - if (nil ~= _573_) then - _571_ = debug.getinfo(_573_) + local _626_ = utils["sym?"](_241) + if (nil ~= _626_) then + local _627_ = resolve(_626_, env, scope) + if (nil ~= _627_) then + _625_ = debug.getinfo(_627_) else - _571_ = _573_ + _625_ = _627_ end else - _571_ = _572_ + _625_ = _626_ end end - if ((_G.type(_571_) == "table") and (nil ~= (_571_).source) and ((_571_).what == "Lua") and (nil ~= (_571_).linedefined) and (nil ~= (_571_).short_src)) then - local source = (_571_).source - local line = (_571_).linedefined - local src = (_571_).short_src + if ((_G.type(_625_) == "table") and (nil ~= (_625_).source) and (nil ~= (_625_).linedefined) and (nil ~= (_625_).short_src) and ((_625_).what == "Lua")) then + local source = (_625_).source + local line = (_625_).linedefined + local src = (_625_).short_src local fnlsrc do - local t_576_ = compiler.sourcemap - if (nil ~= t_576_) then - t_576_ = (t_576_)[source] + local t_630_ = compiler.sourcemap + if (nil ~= t_630_) then + t_630_ = (t_630_)[source] else end - if (nil ~= t_576_) then - t_576_ = (t_576_)[line] + if (nil ~= t_630_) then + t_630_ = (t_630_)[line] else end - if (nil ~= t_576_) then - t_576_ = (t_576_)[2] + if (nil ~= t_630_) then + t_630_ = (t_630_)[2] else end - fnlsrc = t_576_ + fnlsrc = t_630_ end return on_values({string.format("%s:%s", src, (fnlsrc or line))}) - elseif (_571_ == nil) then + elseif (_625_ == nil) then return on_error("Repl", "Unknown value") elseif true then - local _ = _571_ + local _ = _625_ return on_error("Repl", "No source info") else return nil end end - return run_command(read, on_error, _570_) + return run_command(read, on_error, _624_) 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 _581_(_241) + local function _635_(_241) local name = tostring(_241) - local target = (scope.specials[name] or scope.macros[name] or resolve(name, env, scope)) - return on_values({specials.doc(target, name)}) + local is_ok, target = nil, nil + local function _636_() + return (scope.specials[name] or scope.macros[name] or resolve(name, env, scope)) + end + is_ok, target = pcall(_636_) + if is_ok then + return on_values({specials.doc(target, name)}) + else + return on_error("Repl", "Could not resolve value for docstring lookup") + end end - return run_command(read, on_error, _581_) + return run_command(read, on_error, _635_) end do end (compiler.metadata):set(commands.doc, "fnl/docstring", "Print the docstring and arglist for a function, macro, or special form.") local function load_plugin_commands(plugins) for _, plugin in ipairs((plugins or {})) do for name, f in pairs(plugin) do - local _582_ = name:match("^repl%-command%-(.*)") - if (nil ~= _582_) then - local cmd_name = _582_ + local _638_ = name:match("^repl%-command%-(.*)") + if (nil ~= _638_) then + local cmd_name = _638_ commands[cmd_name] = (commands[cmd_name] or f) else end @@ -422,12 +467,12 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) local function run_command_loop(input, read, loop, env, on_values, on_error, scope, chars) local command_name = input:match(",([^%s/]+)") do - local _584_ = commands[command_name] - if (nil ~= _584_) then - local command = _584_ + local _640_ = commands[command_name] + if (nil ~= _640_) then + local command = _640_ command(env, read, on_values, on_error, scope, chars) elseif true then - local _ = _584_ + local _ = _640_ if ("exit" ~= command_name) then on_values({"Unknown command", command_name}) else @@ -441,11 +486,55 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) return nil end end - local function repl(options) + local function try_readline_21(opts, ok, readline) + if ok then + if readline.set_readline_name then + readline.set_readline_name("fennel") + else + end + readline.set_options({keeplines = 1000, histfile = ""}) + opts.readChunk = function(parser_state) + local prompt + if (0 < parser_state["stack-size"]) then + prompt = ".. " + else + prompt = ">> " + end + local str = readline.readline(prompt) + if str then + return (str .. "\n") + else + return nil + 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 + else + return nil + 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 env = specials["wrap-env"]((options.env or (rawget(_G, "_ENV") or _G))) - local save_locals_3f = ((options.saveLocals ~= false) and env.debug and env.debug.getlocal) - local opts = utils.copy(options) + local opts = ((_3foptions and utils.copy(_3foptions)) or {}) + local readline = (should_use_readline_3f(opts) and try_readline_21(opts, pcall(require, "readline"))) + local env = specials["wrap-env"]((opts.env or rawget(_G, "_ENV") or _G)) + local save_locals_3f = ((opts.saveLocals ~= false) and env.debug and env.debug.getlocal) local read_chunk = (opts.readChunk or default_read_chunk) local on_values = (opts.onValues or default_on_values) local on_error = (opts.onError or default_on_error) @@ -453,28 +542,28 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) local byte_stream, clear_stream = parser.granulate(read_chunk) local chars = {} local read, reset = nil, nil - local function _588_(parser_state) + local function _649_(parser_state) local c = byte_stream(parser_state) table.insert(chars, c) return c end - read, reset = parser.parser(_588_) + read, reset = parser.parser(_649_) opts.env, opts.scope = env, compiler["make-scope"]() - opts.useMetadata = (options.useMetadata ~= false) + opts.useMetadata = (opts.useMetadata ~= false) if (opts.allowedGlobals == nil) then opts.allowedGlobals = specials["current-global-names"](env) else end if opts.registerCompleter then - local function _592_() - local _590_ = env - local _591_ = opts.scope - local function _593_(...) - return completer(_590_, _591_, ...) + local function _653_() + local _651_ = env + local _652_ = opts.scope + local function _654_(...) + return completer(_651_, _652_, ...) end - return _593_ + return _654_ end - opts.registerCompleter(_592_()) + opts.registerCompleter(_653_()) else end load_plugin_commands(opts.plugins) @@ -503,54 +592,54 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) chars[k] = nil end reset() - local ok, parse_ok_3f, x = pcall(read) + local ok, not_eof_3f, x = pcall(read) local src_string = string.char(unpack(chars)) if not ok then - on_error("Parse", parse_ok_3f) + on_error("Parse", __fnl_global__parse_2dok_3f) clear_stream() return loop() elseif command_3f(src_string) then return run_command_loop(src_string, read, loop, env, on_values, on_error, opts.scope, chars) else - if parse_ok_3f then + if not_eof_3f then do - local _597_, _598_ = nil, nil - local function _600_() - local _599_ = opts - _599_["source"] = src_string - return _599_ + local _658_, _659_ = nil, nil + local function _661_() + local _660_ = opts + _660_["source"] = src_string + return _660_ end - _597_, _598_ = pcall(compiler.compile, x, _600_()) - if ((_597_ == false) and (nil ~= _598_)) then - local msg = _598_ + _658_, _659_ = pcall(compiler.compile, x, _661_()) + if ((_658_ == false) and (nil ~= _659_)) then + local msg = _659_ clear_stream() on_error("Compile", msg) - elseif ((_597_ == true) and (nil ~= _598_)) then - local src = _598_ + elseif ((_658_ == true) and (nil ~= _659_)) then + local src = _659_ local src0 if save_locals_3f then src0 = splice_save_locals(env, src, opts.scope) else src0 = src end - local _602_, _603_ = pcall(specials["load-code"], src0, env) - if ((_602_ == false) and (nil ~= _603_)) then - local msg = _603_ + local _663_, _664_ = pcall(specials["load-code"], src0, env) + if ((_663_ == false) and (nil ~= _664_)) then + local msg = _664_ clear_stream() on_error("Lua Compile", msg, src0) - elseif (true and (nil ~= _603_)) then - local _ = _602_ - local chunk = _603_ - local function _604_() + elseif (true and (nil ~= _664_)) then + local _ = _663_ + local chunk = _664_ + local function _665_() return print_values(chunk()) end - local function _605_() - local function _606_(...) + local function _666_() + local function _667_(...) return on_error("Runtime", ...) end - return _606_ + return _667_ end - xpcall(_604_, _605_()) + xpcall(_665_, _666_()) else end else @@ -563,7 +652,12 @@ package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) end end end - return loop() + loop() + if readline then + return readline.save_history() + else + return nil + end end return repl end @@ -575,15 +669,15 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local unpack = (table.unpack or _G.unpack) local SPECIALS = compiler.scopes.global.specials local function wrap_env(env) - local function _345_(_, key) - if (type(key) == "string") then + local function _364_(_, key) + if utils["string?"](key) then return env[compiler["global-unmangling"](key)] else return env[key] end end - local function _347_(_, key, value) - if (type(key) == "string") then + local function _366_(_, key, value) + if utils["string?"](key) then env[compiler["global-unmangling"](key)] = value return nil else @@ -591,38 +685,38 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct return nil end end - local function _349_() + local function _368_() local function putenv(k, v) - local _350_ - if (type(k) == "string") then - _350_ = compiler["global-unmangling"](k) + local _369_ + if utils["string?"](k) then + _369_ = compiler["global-unmangling"](k) else - _350_ = k + _369_ = k end - return _350_, v + return _369_, v end return next, utils.kvmap(env, putenv), nil end - return setmetatable({}, {__index = _345_, __newindex = _347_, __pairs = _349_}) + return setmetatable({}, {__index = _364_, __newindex = _366_, __pairs = _368_}) end local function current_global_names(_3fenv) local mt do - local _352_ = getmetatable(_3fenv) - if ((_G.type(_352_) == "table") and (nil ~= (_352_).__pairs)) then - local mtpairs = (_352_).__pairs + local _371_ = getmetatable(_3fenv) + if ((_G.type(_371_) == "table") and (nil ~= (_371_).__pairs)) then + local mtpairs = (_371_).__pairs local tbl_11_auto = {} for k, v in mtpairs(_3fenv) do - local _353_, _354_ = k, v - if ((nil ~= _353_) and (nil ~= _354_)) then - local k_12_auto = _353_ - local v_13_auto = _354_ + local _372_, _373_ = k, v + if ((nil ~= _372_) and (nil ~= _373_)) then + local k_12_auto = _372_ + local v_13_auto = _373_ tbl_11_auto[k_12_auto] = v_13_auto else end end mt = tbl_11_auto - elseif (_352_ == nil) then + elseif (_371_ == nil) then mt = (_3fenv or _G) else mt = nil @@ -634,9 +728,9 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local env = (_3fenv or rawget(_G, "_ENV") or _G) if (rawget(_G, "setfenv") and rawget(_G, "loadstring")) then local f = assert(_G.loadstring(code, _3ffilename)) - local _357_ = f - setfenv(_357_, env) - return _357_ + local _376_ = f + setfenv(_376_, env) + return _376_ else return assert(load(code, _3ffilename, "t", env)) end @@ -649,13 +743,13 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct 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 {"#"}), " ") - local _359_ + local _378_ if (#arglist > 0) then - _359_ = " " + _378_ = " " else - _359_ = "" + _378_ = "" end - return string.format("(%s%s%s)\n %s", name, _359_, arglist, docstring) + return string.format("(%s%s%s)\n %s", name, _378_, arglist, docstring) else return string.format("%s\n %s", name, docstring) end @@ -744,7 +838,7 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct doc_special("values", {"..."}, "Return multiple values from a function. Must be in tail position.") local function deep_tostring(x, key_3f) if utils["sequence?"](x) then - local _368_ + local _387_ do local tbl_14_auto = {} local i_15_auto = #tbl_14_auto @@ -756,11 +850,11 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct else end end - _368_ = tbl_14_auto + _387_ = tbl_14_auto end - return ("[" .. table.concat(_368_, " ") .. "]") + return ("[" .. table.concat(_387_, " ") .. "]") elseif utils["table?"](x) then - local _370_ + local _389_ do local tbl_14_auto = {} local i_15_auto = #tbl_14_auto @@ -772,12 +866,12 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct else end end - _370_ = tbl_14_auto + _389_ = tbl_14_auto end - return ("{" .. table.concat(_370_, " ") .. "}") - elseif (key_3f and (type(x) == "string") and x:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then + return ("{" .. table.concat(_389_, " ") .. "}") + elseif (key_3f and utils["string?"](x) and x:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then return (":" .. x) - elseif (type(x) == "string") then + elseif utils["string?"](x) then return string.format("%q", x):gsub("\\\"", "\\\\\""):gsub("\"", "\\\"") else return tostring(x) @@ -786,10 +880,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local function set_fn_metadata(arg_list, docstring, parent, fn_name) if utils.root.options.useMetadata then local args - local function _373_(_241) + local function _392_(_241) return ("\"%s\""):format(deep_tostring(_241)) end - args = utils.map(arg_list, _373_) + args = utils.map(arg_list, _392_) local meta_fields = {"\"fnl/arglist\"", ("{" .. table.concat(args, ", ") .. "}")} if docstring then table.insert(meta_fields, "\"fnl/docstring\"") @@ -804,44 +898,76 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end local function get_fn_name(ast, scope, fn_name, multi) if (fn_name and (fn_name[1] ~= "nil")) then - local _376_ + local _395_ if not multi then - _376_ = compiler["declare-local"](fn_name, {}, scope, ast) + _395_ = compiler["declare-local"](fn_name, {}, scope, ast) else - _376_ = (compiler["symbol-to-expression"](fn_name, scope))[1] + _395_ = (compiler["symbol-to-expression"](fn_name, scope))[1] end - return _376_, not multi, 3 + return _395_, 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, arg_list, docstring) + 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 _379_ + local _398_ if local_3f then - _379_ = "local function %s(%s)" + _398_ = "local function %s(%s)" else - _379_ = "%s = function(%s)" + _398_ = "%s = function(%s)" end - compiler.emit(parent, string.format(_379_, fn_name, table.concat(arg_name_list, ", ")), ast) + compiler.emit(parent, string.format(_398_, fn_name, table.concat(arg_name_list, ", ")), ast) compiler.emit(parent, f_chunk, ast) compiler.emit(parent, "end", ast) - set_fn_metadata(arg_list, docstring, parent, fn_name) + set_fn_metadata(f_metadata["fnl/arglist"], f_metadata["fnl/docstring"], 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, arg_list, docstring, scope) + 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, arg_list, docstring) + return compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, true, arg_name_list, f_metadata) + end + local function get_function_metadata(ast, arg_list, index) + local f_metadata = {["fnl/arglist"] = arg_list} + local index_2a = (index + 1) + local expr = ast[index_2a] + if (utils["string?"](expr) and (index_2a < #ast)) then + local _401_ + do + local _400_ = f_metadata + _400_["fnl/docstring"] = expr + _401_ = _400_ + end + return _401_, index_2a + elseif (utils["table?"](expr) and (index_2a < #ast)) then + local _402_ + do + local tbl_11_auto = f_metadata + for k, v in pairs(expr) do + local _403_, _404_ = k, v + if ((nil ~= _403_) and (nil ~= _404_)) then + local k_12_auto = _403_ + local v_13_auto = _404_ + tbl_11_auto[k_12_auto] = v_13_auto + else + end + end + _402_ = tbl_11_auto + end + return _402_, index_2a + else + return f_metadata, index + end end SPECIALS.fn = function(ast, scope, parent) local f_scope do - local _381_ = compiler["make-scope"](scope) - do end (_381_)["vararg"] = false - f_scope = _381_ + local _407_ = compiler["make-scope"](scope) + do end (_407_)["vararg"] = false + f_scope = _407_ end local f_chunk = {} local fn_sym = utils["sym?"](ast[2]) @@ -866,44 +992,39 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end end local arg_name_list = utils.map(arg_list, get_arg_name) - local index0, docstring = nil, nil - if ((type(ast[(index + 1)]) == "string") and ((index + 1) < #ast)) then - index0, docstring = (index + 1), ast[(index + 1)] - else - index0, docstring = index, nil - 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, arg_list, docstring) + 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, arg_list, docstring, scope) + 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.\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) + 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 _386_ + local _411_ do - local _385_ = utils["sym?"](ast[2]) - if (nil ~= _385_) then - _386_ = tostring(_385_) + local _410_ = utils["sym?"](ast[2]) + if (nil ~= _410_) then + _411_ = tostring(_410_) else - _386_ = _385_ + _411_ = _410_ end end - if ("nil" ~= _386_) then + if ("nil" ~= _411_) then table.insert(parent, {ast = ast, leaf = tostring(ast[2])}) else end - local _390_ + local _415_ do - local _389_ = utils["sym?"](ast[3]) - if (nil ~= _389_) then - _390_ = tostring(_389_) + local _414_ = utils["sym?"](ast[3]) + if (nil ~= _414_) then + _415_ = tostring(_414_) else - _390_ = _389_ + _415_ = _414_ end end - if ("nil" ~= _390_) then + if ("nil" ~= _415_) then return tostring(ast[3]) else return nil @@ -912,19 +1033,19 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local function dot(ast, scope, parent) compiler.assert((1 < #ast), "expected table argument", ast) local len = #ast - local _let_393_ = compiler.compile1(ast[2], scope, parent, {nval = 1}) - local lhs = _let_393_[1] + local _let_418_ = compiler.compile1(ast[2], scope, parent, {nval = 1}) + local lhs = _let_418_[1] if (len == 2) then return tostring(lhs) else local indices = {} for i = 3, len do local index = ast[i] - if ((type(index) == "string") and utils["valid-lua-identifier?"](index)) then + if (utils["string?"](index) and utils["valid-lua-identifier?"](index)) then table.insert(indices, ("." .. index)) else - local _let_394_ = compiler.compile1(index, scope, parent, {nval = 1}) - local index0 = _let_394_[1] + local _let_419_ = compiler.compile1(index, scope, parent, {nval = 1}) + local index0 = _let_419_[1] table.insert(indices, ("[" .. tostring(index0) .. "]")) end end @@ -969,7 +1090,7 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end doc_special("var", {"name", "val"}, "Introduce new mutable local.") local function kv_3f(t) - local _398_ + local _423_ do local tbl_14_auto = {} local i_15_auto = #tbl_14_auto @@ -986,9 +1107,9 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct else end end - _398_ = tbl_14_auto + _423_ = tbl_14_auto end - return (_398_)[1] + return (_423_)[1] end SPECIALS.let = function(ast, scope, parent, opts) local bindings = ast[2] @@ -1015,24 +1136,24 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end end local function disambiguate_3f(rootstr, parent) - local function _403_() - local _402_ = get_prev_line(parent) - if (nil ~= _402_) then - local prev_line = _402_ + local function _428_() + local _427_ = get_prev_line(parent) + if (nil ~= _427_) then + local prev_line = _427_ return prev_line:match("%)$") else return nil end end - return (rootstr:match("^{") or _403_()) + return (rootstr:match("^{") or _428_()) end SPECIALS.tset = function(ast, scope, parent) compiler.assert((#ast > 3), "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 _let_405_ = compiler.compile1(ast[i], scope, parent, {nval = 1}) - local key = _let_405_[1] + local _let_430_ = compiler.compile1(ast[i], scope, parent, {nval = 1}) + local key = _let_430_[1] table.insert(keys, tostring(key)) end local value = (compiler.compile1(ast[#ast], scope, parent, {nval = 1}))[1] @@ -1155,8 +1276,8 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end local function compile_until(condition, scope, chunk) if condition then - local _let_414_ = compiler.compile1(condition, scope, chunk, {nval = 1}) - local condition_lua = _let_414_[1] + local _let_439_ = compiler.compile1(condition, scope, chunk, {nval = 1}) + local condition_lua = _let_439_[1] return compiler.emit(chunk, ("if %s then break end"):format(tostring(condition_lua)), utils.expr(condition, "expression")) else return nil @@ -1172,7 +1293,7 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local new_manglings = {} local sub_scope = compiler["make-scope"](scope) local function destructure_binding(v) - compiler.assert(("string" ~= type(v)), ("unexpected iterator clause " .. tostring(v)), binding) + 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 @@ -1239,10 +1360,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct 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 _let_418_ = ast - local _ = _let_418_[1] - local _0 = _let_418_[2] - local method_string = _let_418_[3] + local _let_443_ = ast + local _ = _let_443_[1] + local _0 = _let_443_[2] + local method_string = _let_443_[3] local call_string if ((target.type == "literal") or (target.type == "varg") or (target.type == "expression")) then call_string = "(%s):%s(%s)" @@ -1264,21 +1385,21 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end local function method_call(ast, scope, parent) compiler.assert((2 < #ast), "expected at least 2 arguments", ast) - local _let_420_ = compiler.compile1(ast[2], scope, parent, {nval = 1}) - local target = _let_420_[1] + local _let_445_ = compiler.compile1(ast[2], scope, parent, {nval = 1}) + local target = _let_445_[1] local args = {} for i = 4, #ast do local subexprs - local _421_ + local _446_ if (i ~= #ast) then - _421_ = 1 + _446_ = 1 else - _421_ = nil + _446_ = nil end - subexprs = compiler.compile1(ast[i], scope, parent, {nval = _421_}) + subexprs = compiler.compile1(ast[i], scope, parent, {nval = _446_}) utils.map(subexprs, tostring, args) end - if ((type(ast[3]) == "string") and utils["valid-lua-identifier?"](ast[3])) then + 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) @@ -1313,10 +1434,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct compiler.assert((#ast == 2), "expected one argument", ast) local f_scope do - local _426_ = compiler["make-scope"](scope) - do end (_426_)["vararg"] = false - _426_["hashfn"] = true - f_scope = _426_ + local _451_ = compiler["make-scope"](scope) + do end (_451_)["vararg"] = false + _451_["hashfn"] = true + f_scope = _451_ end local f_chunk = {} local name = compiler.gensym(scope) @@ -1354,52 +1475,63 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct 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, _455_) + local _arg_456_ = _455_ + local mac = _arg_456_["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.sym("do"), ast) + else + return ast + end + end local function arithmetic_special(name, zero_arity, unary_prefix, ast, scope, parent) local len = #ast local operands = {} local padded_op = (" " .. name .. " ") for i = 2, len do - local subexprs = compiler.compile1(ast[i], scope, parent) + 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 _431_ = #operands - if (_431_ == 0) then - local _433_ + local _459_ = #operands + if (_459_ == 0) then + local _461_ do - local _432_ = zero_arity - compiler.assert(_432_, "Expected more than 0 arguments", ast) - _433_ = _432_ + local _460_ = zero_arity + compiler.assert(_460_, "Expected more than 0 arguments", ast) + _461_ = _460_ end - return utils.expr(_433_, "literal") - elseif (_431_ == 1) then + return utils.expr(_461_, "literal") + elseif (_459_ == 1) then if unary_prefix then return ("(" .. unary_prefix .. padded_op .. operands[1] .. ")") else return operands[1] end elseif true then - local _ = _431_ + local _ = _459_ return ("(" .. table.concat(operands, padded_op) .. ")") else return nil end end local function define_arithmetic_special(name, zero_arity, unary_prefix, _3flua_name) - local _439_ + local _467_ do - local _436_ = (_3flua_name or name) - local _437_ = zero_arity - local _438_ = unary_prefix - local function _440_(...) - return arithmetic_special(_436_, _437_, _438_, ...) + local _464_ = (_3flua_name or name) + local _465_ = zero_arity + local _466_ = unary_prefix + local function _468_(...) + return arithmetic_special(_464_, _465_, _466_, ...) end - _439_ = _440_ + _467_ = _468_ end - SPECIALS[name] = _439_ + SPECIALS[name] = _467_ return doc_special(name, {"a", "b", "..."}, "Arithmetic operator; works the same as Lua but accepts more arguments.") end define_arithmetic_special("+", "0") @@ -1428,13 +1560,13 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local prefixed_lib_name = ("bit." .. lib_name) for i = 2, len do local subexprs - local _441_ + local _469_ if (i ~= len) then - _441_ = 1 + _469_ = 1 else - _441_ = nil + _469_ = nil end - subexprs = compiler.compile1(ast[i], scope, parent, {nval = _441_}) + subexprs = compiler.compile1(ast[i], scope, parent, {nval = _469_}) utils.map(subexprs, tostring, operands) end if (#operands == 1) then @@ -1453,18 +1585,18 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end end local function define_bitop_special(name, zero_arity, unary_prefix, native) - local _451_ + local _479_ do - local _447_ = native - local _448_ = name - local _449_ = zero_arity - local _450_ = unary_prefix - local function _452_(...) - return bitop_special(_447_, _448_, _449_, _450_, ...) + local _475_ = native + local _476_ = name + local _477_ = zero_arity + local _478_ = unary_prefix + local function _480_(...) + return bitop_special(_475_, _476_, _477_, _478_, ...) end - _451_ = _452_ + _479_ = _480_ end - SPECIALS[name] = _451_ + SPECIALS[name] = _479_ return nil end define_bitop_special("lshift", nil, "1", "<<") @@ -1478,15 +1610,15 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct 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.") doc_special("..", {"a", "b", "..."}, "String concatenation operator; works the same as Lua but accepts more arguments.") - local function native_comparator(op, _453_, scope, parent) - local _arg_454_ = _453_ - local _ = _arg_454_[1] - local lhs_ast = _arg_454_[2] - local rhs_ast = _arg_454_[3] - local _let_455_ = compiler.compile1(lhs_ast, scope, parent, {nval = 1}) - local lhs = _let_455_[1] - local _let_456_ = compiler.compile1(rhs_ast, scope, parent, {nval = 1}) - local rhs = _let_456_[1] + local function native_comparator(op, _481_, scope, parent) + local _arg_482_ = _481_ + local _ = _arg_482_[1] + local lhs_ast = _arg_482_[2] + local rhs_ast = _arg_482_[3] + local _let_483_ = compiler.compile1(lhs_ast, scope, parent, {nval = 1}) + local lhs = _let_483_[1] + local _let_484_ = compiler.compile1(rhs_ast, scope, parent, {nval = 1}) + local rhs = _let_484_[1] return string.format("(%s %s %s)", tostring(lhs), op, tostring(rhs)) end local function double_eval_protected_comparator(op, chain_op, ast, scope, parent) @@ -1562,12 +1694,21 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end local safe_require = nil local function safe_compiler_env() - return {table = utils.copy(table), math = utils.copy(math), string = utils.copy(string), pairs = pairs, ipairs = ipairs, select = select, tostring = tostring, tonumber = tonumber, bit = rawget(_G, "bit"), pcall = pcall, xpcall = xpcall, next = next, print = print, type = type, assert = assert, error = error, setmetatable = setmetatable, getmetatable = safe_getmetatable, require = safe_require, rawlen = rawget(_G, "rawlen"), rawget = rawget, rawset = rawset, rawequal = rawequal, _VERSION = _VERSION} + local _488_ + do + local _487_ = rawget(_G, "utf8") + if (nil ~= _487_) then + _488_ = utils.copy(_487_) + else + _488_ = _487_ + end + end + return {table = utils.copy(table), math = utils.copy(math), string = utils.copy(string), pairs = pairs, ipairs = ipairs, select = select, tostring = tostring, tonumber = tonumber, bit = rawget(_G, "bit"), pcall = pcall, xpcall = xpcall, next = next, print = print, type = type, assert = assert, error = error, setmetatable = setmetatable, getmetatable = safe_getmetatable, require = safe_require, rawlen = rawget(_G, "rawlen"), rawget = rawget, rawset = rawset, rawequal = rawequal, _VERSION = _VERSION, utf8 = _488_} end local function combined_mt_pairs(env) local combined = {} - local _let_459_ = getmetatable(env) - local __index = _let_459_["__index"] + local _let_490_ = getmetatable(env) + local __index = _let_490_["__index"] if ("table" == type(__index)) then for k, v in pairs(__index) do combined[k] = v @@ -1582,42 +1723,42 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct local function make_compiler_env(ast, scope, parent, _3fopts) local provided do - local _461_ = (_3fopts or utils.root.options) - if ((_G.type(_461_) == "table") and ((_461_)["compiler-env"] == "strict")) then + local _492_ = (_3fopts or utils.root.options) + if ((_G.type(_492_) == "table") and ((_492_)["compiler-env"] == "strict")) then provided = safe_compiler_env() - elseif ((_G.type(_461_) == "table") and (nil ~= (_461_).compilerEnv)) then - local compilerEnv = (_461_).compilerEnv + elseif ((_G.type(_492_) == "table") and (nil ~= (_492_).compilerEnv)) then + local compilerEnv = (_492_).compilerEnv provided = compilerEnv - elseif ((_G.type(_461_) == "table") and (nil ~= (_461_)["compiler-env"])) then - local compiler_env = (_461_)["compiler-env"] + elseif ((_G.type(_492_) == "table") and (nil ~= (_492_)["compiler-env"])) then + local compiler_env = (_492_)["compiler-env"] provided = compiler_env elseif true then - local _ = _461_ + local _ = _492_ provided = safe_compiler_env(false) else provided = nil end end local env - local function _463_(base) + local function _494_(base) return utils.sym(compiler.gensym((compiler.scopes.macro or scope), base)) end - local function _464_() + local function _495_() return compiler.scopes.macro end - local function _465_(symbol) + local function _496_(symbol) compiler.assert(compiler.scopes.macro, "must call from macro", ast) return compiler.scopes.macro.manglings[tostring(symbol)] end - local function _466_(form) + local function _497_(form) compiler.assert(compiler.scopes.macro, "must call from macro", ast) return compiler.macroexpand(form, compiler.scopes.macro) end - env = {_AST = ast, _CHUNK = parent, _IS_COMPILER = true, _SCOPE = scope, _SPECIALS = compiler.scopes.global.specials, _VARARG = utils.varg(), ["macro-loaded"] = macro_loaded, unpack = unpack, ["assert-compile"] = compiler.assert, view = view, version = utils.version, metadata = compiler.metadata, list = utils.list, ["list?"] = utils["list?"], ["table?"] = utils["table?"], sequence = utils.sequence, ["sequence?"] = utils["sequence?"], sym = utils.sym, ["sym?"] = utils["sym?"], ["multi-sym?"] = utils["multi-sym?"], comment = utils.comment, ["comment?"] = utils["comment?"], ["varg?"] = utils["varg?"], gensym = _463_, ["get-scope"] = _464_, ["in-scope?"] = _465_, macroexpand = _466_} + env = {_AST = ast, _CHUNK = parent, _IS_COMPILER = true, _SCOPE = scope, _SPECIALS = compiler.scopes.global.specials, _VARARG = utils.varg(), ["macro-loaded"] = macro_loaded, unpack = unpack, ["assert-compile"] = compiler.assert, view = view, version = utils.version, metadata = compiler.metadata, list = utils.list, ["list?"] = utils["list?"], ["table?"] = utils["table?"], sequence = utils.sequence, ["sequence?"] = utils["sequence?"], sym = utils.sym, ["sym?"] = utils["sym?"], ["multi-sym?"] = utils["multi-sym?"], comment = utils.comment, ["comment?"] = utils["comment?"], ["varg?"] = utils["varg?"], gensym = _494_, ["get-scope"] = _495_, ["in-scope?"] = _496_, macroexpand = _497_} env._G = env return setmetatable(env, {__index = provided, __newindex = provided, __pairs = combined_mt_pairs}) end - local function _468_(...) + local function _499_(...) local tbl_14_auto = {} local i_15_auto = #tbl_14_auto for c in string.gmatch((package.config or ""), "([^\n]+)") do @@ -1630,10 +1771,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end return tbl_14_auto end - local _local_467_ = _468_(...) - local dirsep = _local_467_[1] - local pathsep = _local_467_[2] - local pathmark = _local_467_[3] + local _local_498_ = _499_(...) + local dirsep = _local_498_[1] + local pathsep = _local_498_[2] + local pathmark = _local_498_[3] local pkg_config = {dirsep = (dirsep or "/"), pathmark = (pathmark or ";"), pathsep = (pathsep or "?")} local function escapepat(str) return string.gsub(str, "[^%w]", "%%%1") @@ -1646,20 +1787,48 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct 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 _470_ = (io.open(filename) or io.open(filename2)) - if (nil ~= _470_) then - local file = _470_ + local _501_ = (io.open(filename) or io.open(filename2)) + if (nil ~= _501_) then + local file = _501_ file:close() return filename + elseif true then + local _ = _501_ + return nil, ("no file '" .. filename .. "'") else return nil end end - local function find_in_path(start) - local _472_ = fullpath:match(pattern, start) - if (nil ~= _472_) then - local path = _472_ - return (try_path(path) or find_in_path((start + #path + 1))) + local function find_in_path(start, _3ftried_paths) + local _503_ = fullpath:match(pattern, start) + if (nil ~= _503_) then + local path = _503_ + local _504_, _505_ = try_path(path) + if (nil ~= _504_) then + local filename = _504_ + return filename + elseif ((_504_ == nil) and (nil ~= _505_)) then + local error = _505_ + local function _507_() + local _506_ = (_3ftried_paths or {}) + table.insert(_506_, error) + return _506_ + end + return find_in_path((start + #path + 1), _507_()) + else + return nil + end + elseif true then + local _ = _503_ + local function _509_() + 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, _509_() else return nil end @@ -1667,61 +1836,79 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct return find_in_path(1) end local function make_searcher(_3foptions) - local function _474_(module_name) + local function _512_(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 _475_ = search_module(module_name) - if (nil ~= _475_) then - local filename = _475_ - local _478_ + local _513_, _514_ = search_module(module_name) + if (nil ~= _513_) then + local filename = _513_ + local _517_ do - local _476_ = filename - local _477_ = opts - local function _479_(...) - return utils["fennel-module"].dofile(_476_, _477_, ...) + local _515_ = filename + local _516_ = opts + local function _518_(...) + return utils["fennel-module"].dofile(_515_, _516_, ...) end - _478_ = _479_ + _517_ = _518_ end - return _478_, filename + return _517_, filename + elseif ((_513_ == nil) and (nil ~= _514_)) then + local error = _514_ + return error else return nil end end - return _474_ + return _512_ + 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 do - local _481_ = utils.copy(utils.root.options) - do end (_481_)["env"] = "_COMPILER" - _481_["requireAsInclude"] = false - _481_["allowedGlobals"] = nil - opts = _481_ + local _520_ = utils.copy(utils.root.options) + do end (_520_)["env"] = "_COMPILER" + _520_["requireAsInclude"] = false + _520_["allowedGlobals"] = nil + opts = _520_ end - local _482_ = search_module(module_name, utils["fennel-module"]["macro-path"]) - if (nil ~= _482_) then - local filename = _482_ - local _485_ - do - local _483_ = filename - local _484_ = opts - local function _486_(...) - return utils["fennel-module"].dofile(_483_, _484_, ...) + local _521_ = search_module(module_name, utils["fennel-module"]["macro-path"]) + if (nil ~= _521_) then + local filename = _521_ + local _522_ + if (opts["compiler-env"] == _G) then + local _523_ = fennel_macro_searcher + local _524_ = filename + local _525_ = opts + local function _527_(...) + return dofile_with_searcher(_523_, _524_, _525_, ...) end - _485_ = _486_ + _522_ = _527_ + else + local _528_ = filename + local _529_ = opts + local function _531_(...) + return utils["fennel-module"].dofile(_528_, _529_, ...) + end + _522_ = _531_ end - return _485_, filename + return _522_, filename else return nil end end local function lua_macro_searcher(module_name) - local _488_ = search_module(module_name, package.path) - if (nil ~= _488_) then - local filename = _488_ + local _534_ = search_module(module_name, package.path) + if (nil ~= _534_) then + local filename = _534_ local code do local f = io.open(filename) @@ -1733,10 +1920,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct return error(..., 0) end end - local function _490_() + local function _536_() return assert(f:read("*a")) end - code = close_handlers_8_auto(_G.xpcall(_490_, (package.loaded.fennel or debug).traceback)) + code = close_handlers_8_auto(_G.xpcall(_536_, (package.loaded.fennel or debug).traceback)) end local chunk = load_code(code, make_compiler_env(), filename) return chunk, filename @@ -1746,16 +1933,16 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end local macro_searchers = {fennel_macro_searcher, lua_macro_searcher} local function search_macro_module(modname, n) - local _492_ = macro_searchers[n] - if (nil ~= _492_) then - local f = _492_ - local _493_, _494_ = f(modname) - if ((nil ~= _493_) and true) then - local loader = _493_ - local _3ffilename = _494_ + local _538_ = macro_searchers[n] + if (nil ~= _538_) then + local f = _538_ + local _539_, _540_ = f(modname) + if ((nil ~= _539_) and true) then + local loader = _539_ + local _3ffilename = _540_ return loader, _3ffilename elseif true then - local _ = _493_ + local _ = _539_ return search_macro_module(modname, (n + 1)) else return nil @@ -1771,16 +1958,16 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct return nil end end - local function _498_(modname) - local function _499_() + local function _544_(modname) + local function _545_() local loader, filename = search_macro_module(modname, 1) compiler.assert(loader, (modname .. " module not found.")) do end (macro_loaded)[modname] = loader(modname, filename) return macro_loaded[modname] end - return (macro_loaded[modname] or metadata_only_fennel(modname) or _499_()) + return (macro_loaded[modname] or metadata_only_fennel(modname) or _545_()) end - safe_require = _498_ + safe_require = _544_ 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 @@ -1789,10 +1976,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct end return nil end - local function resolve_module_name(_500_, _scope, _parent, opts) - local _arg_501_ = _500_ - local filename = _arg_501_["filename"] - local second = _arg_501_[2] + local function resolve_module_name(_546_, _scope, _parent, opts) + local _arg_547_ = _546_ + local filename = _arg_547_["filename"] + local second = _arg_547_[2] local filename0 = (filename or (utils["table?"](second) and second.filename)) local module_name = utils.root.options["module-name"] local modexpr = compiler.compile(second, opts) @@ -1802,7 +1989,7 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct 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(("string" == type(modname)), "module name must compile to string", (_3freal_ast or ast)) + 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) @@ -1851,10 +2038,10 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct return error(..., 0) end end - local function _507_() - return f:read("*all"):gsub("[\13\n]*$", "") + local function _553_() + return assert(f:read("*all")):gsub("[\13\n]*$", "") end - src = close_handlers_8_auto(_G.xpcall(_507_, (package.loaded.fennel or debug).traceback)) + src = close_handlers_8_auto(_G.xpcall(_553_, (package.loaded.fennel or debug).traceback)) end local ret = utils.expr(("require(\"" .. mod .. "\")"), "statement") local target = ("package.preload[%q]"):format(mod) @@ -1886,12 +2073,12 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct compiler.assert((#ast == 2), "expected one argument", ast) local modexpr do - local _510_, _511_ = pcall(resolve_module_name, ast, scope, parent, opts) - if ((_510_ == true) and (nil ~= _511_)) then - local modname = _511_ + local _556_, _557_ = pcall(resolve_module_name, ast, scope, parent, opts) + if ((_556_ == true) and (nil ~= _557_)) then + local modname = _557_ modexpr = utils.expr(string.format("%q", modname), "literal") elseif true then - local _ = _510_ + local _ = _556_ modexpr = (compiler.compile1(ast[2], scope, parent, {nval = 1}))[1] else modexpr = nil @@ -1910,13 +2097,13 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct utils.root.options["module-name"] = mod _ = nil local res - local function _515_() - local _514_ = search_module(mod) - if (nil ~= _514_) then - local fennel_path = _514_ + local function _561_() + local _560_ = search_module(mod) + if (nil ~= _560_) then + local fennel_path = _560_ return include_path(ast, opts, fennel_path, mod, true) elseif true then - local _0 = _514_ + local _0 = _560_ local lua_path = search_module(mod, package.path) if lua_path then return include_path(ast, opts, lua_path, mod, false) @@ -1929,7 +2116,7 @@ package.preload["fennel.specials"] = package.preload["fennel.specials"] or funct return nil end end - res = ((utils["member?"](mod, (utils.root.options.skipInclude or {})) and utils.expr("nil --[[SKIPPED INCLUDE]]--", "literal")) or include_circular_fallback(mod, modexpr, opts.fallback, ast) or utils.root.scope.includes[mod] or _515_()) + 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 _561_()) utils.root.options["module-name"] = oldmod return res end @@ -1965,13 +2152,13 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct local scopes = {} local function make_scope(_3fparent) local parent = (_3fparent or scopes.global) - local _203_ + local _219_ if parent then - _203_ = ((parent.depth or 0) + 1) + _219_ = ((parent.depth or 0) + 1) else - _203_ = 0 + _219_ = 0 end - return {includes = setmetatable({}, {__index = (parent and parent.includes)}), macros = setmetatable({}, {__index = (parent and parent.macros)}), manglings = setmetatable({}, {__index = (parent and parent.manglings)}), specials = setmetatable({}, {__index = (parent and parent.specials)}), symmeta = setmetatable({}, {__index = (parent and parent.symmeta)}), unmanglings = setmetatable({}, {__index = (parent and parent.unmanglings)}), gensyms = setmetatable({}, {__index = (parent and parent.gensyms)}), autogensyms = setmetatable({}, {__index = (parent and parent.autogensyms)}), vararg = (parent and parent.vararg), depth = _203_, hashfn = (parent and parent.hashfn), refedglobals = {}, parent = parent} + return {includes = setmetatable({}, {__index = (parent and parent.includes)}), macros = setmetatable({}, {__index = (parent and parent.macros)}), manglings = setmetatable({}, {__index = (parent and parent.manglings)}), specials = setmetatable({}, {__index = (parent and parent.specials)}), symmeta = setmetatable({}, {__index = (parent and parent.symmeta)}), unmanglings = setmetatable({}, {__index = (parent and parent.unmanglings)}), gensyms = setmetatable({}, {__index = (parent and parent.gensyms)}), autogensyms = setmetatable({}, {__index = (parent and parent.autogensyms)}), vararg = (parent and parent.vararg), depth = _219_, hashfn = (parent and parent.hashfn), refedglobals = {}, parent = parent} end local function assert_msg(ast, msg) local ast_tbl @@ -1988,9 +2175,9 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end local function assert_compile(condition, msg, ast) if not condition then - local _let_206_ = (utils.root.options or {}) - local source = _let_206_["source"] - local unfriendly = _let_206_["unfriendly"] + local _let_222_ = (utils.root.options or {}) + local source = _let_222_["source"] + local unfriendly = _let_222_["unfriendly"] if (nil == utils.hook("assert-compile", condition, msg, ast, utils.root.reset)) then utils.root.reset() if (unfriendly or not friend or not _G.io or not _G.io.read) then @@ -2010,33 +2197,33 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct scopes.macro = scopes.global local serialize_subst = {["\7"] = "\\a", ["\8"] = "\\b", ["\9"] = "\\t", ["\n"] = "n", ["\11"] = "\\v", ["\12"] = "\\f"} local function serialize_string(str) - local function _210_(_241) + local function _226_(_241) return ("\\" .. _241:byte()) end - return string.gsub(string.gsub(string.format("%q", str), ".", serialize_subst), "[\128-\255]", _210_) + return string.gsub(string.gsub(string.format("%q", str), ".", serialize_subst), "[\128-\255]", _226_) end local function global_mangling(str) if utils["valid-lua-identifier?"](str) then return str else - local function _211_(_241) + local function _227_(_241) return string.format("_%02x", _241:byte()) end - return ("__fnl_global__" .. str:gsub("[^%w]", _211_)) + return ("__fnl_global__" .. str:gsub("[^%w]", _227_)) end end local function global_unmangling(identifier) - local _213_ = string.match(identifier, "^__fnl_global__(.*)$") - if (nil ~= _213_) then - local rest = _213_ - local _214_ - local function _215_(_241) + local _229_ = string.match(identifier, "^__fnl_global__(.*)$") + if (nil ~= _229_) then + local rest = _229_ + local _230_ + local function _231_(_241) return string.char(tonumber(_241:sub(2), 16)) end - _214_ = string.gsub(rest, "_[%da-f][%da-f]", _215_) - return _214_ + _230_ = string.gsub(rest, "_[%da-f][%da-f]", _231_) + return _230_ elseif true then - local _ = _213_ + local _ = _229_ return identifier else return nil @@ -2062,10 +2249,10 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct raw = str end local mangling - local function _219_(_241) + local function _235_(_241) return string.format("_%02x", _241:byte()) end - mangling = string.gsub(string.gsub(raw, "-", "_"), "[^%w_]", _219_) + mangling = string.gsub(string.gsub(raw, "-", "_"), "[^%w_]", _235_) local unique = unique_mangling(mangling, mangling, scope, 0) do end (scope.unmanglings)[unique] = str do @@ -2110,26 +2297,27 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return mangling end local function autogensym(base, scope) - local _222_ = utils["multi-sym?"](base) - if (nil ~= _222_) then - local parts = _222_ + local _238_ = utils["multi-sym?"](base) + if (nil ~= _238_) then + local parts = _238_ parts[1] = autogensym(parts[1], scope) return table.concat(parts, ((parts["multi-sym-method-call"] and ":") or ".")) elseif true then - local _ = _222_ - local function _223_() + local _ = _238_ + local function _239_() local mangling = gensym(scope, base:sub(1, ( - 2)), "auto") do end (scope.autogensyms)[base] = mangling return mangling end - return (scope.autogensyms[base] or _223_()) + return (scope.autogensyms[base] or _239_()) else return nil end end local function check_binding_valid(symbol, scope, ast) local name = tostring(symbol) - assert_compile(not name:find("&"), "illegal character &") + assert_compile(not name:find("&"), "invalid character: &") + assert_compile(not name:find("^%."), "invalid character: .") assert_compile(not (scope.specials[name] or scope.macros[name]), ("local %s was overshadowed by a special form or macro"):format(name), ast) return assert_compile(not utils["quoted?"](symbol), string.format("macro tried to bind %s without gensym", name), symbol) end @@ -2168,6 +2356,7 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct else end assert_compile(not scope.macros[parts[1]], "tried to reference a macro at runtime", symbol) + assert_compile((not scope.specials[parts[1]] or ("require" == parts[1])), "tried to reference a special form at runtime", symbol) assert_compile((not _3freference_3f or local_3f or ("_ENV" == parts[1]) or global_allowed_3f(parts[1])), ("unknown identifier in strict mode: " .. tostring(parts[1])), symbol) if (allowed_globals and not local_3f and scope.parent) then scope.parent.refedglobals[parts[1]] = true @@ -2241,14 +2430,14 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct else local tab0 do - local _236_ = tab - if (_236_ == true) then + local _252_ = tab + if (_252_ == true) then tab0 = " " - elseif (_236_ == false) then + elseif (_252_ == false) then tab0 = "" - elseif (_236_ == tab) then + elseif (_252_ == tab) then tab0 = tab - elseif (_236_ == nil) then + elseif (_252_ == nil) then tab0 = "" else tab0 = nil @@ -2299,19 +2488,19 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end end local function make_metadata() - local function _245_(self, tgt, key) + local function _261_(self, tgt, key) if self[tgt] then return self[tgt][key] else return nil end end - local function _247_(self, tgt, key, value) + local function _263_(self, tgt, key, value) self[tgt] = (self[tgt] or {}) do end (self[tgt])[key] = value return tgt end - local function _248_(self, tgt, ...) + local function _264_(self, tgt, ...) local kv_len = select("#", ...) local kvs = {...} if ((kv_len % 2) ~= 0) then @@ -2324,7 +2513,7 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end return tgt end - return setmetatable({}, {__index = {get = _245_, set = _247_, setall = _248_}, __mode = "k"}) + return setmetatable({}, {__index = {get = _261_, set = _263_, setall = _264_}, __mode = "k"}) end local function exprs1(exprs) return table.concat(utils.map(exprs, tostring), ", ") @@ -2374,22 +2563,22 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end if opts.target then local result = exprs1(exprs) - local function _256_() + local function _272_() if (result == "") then return "nil" else return result end end - emit(parent, string.format("%s = %s", opts.target, _256_()), ast) + emit(parent, string.format("%s = %s", opts.target, _272_()), ast) else end if (opts.tail or opts.target) then return {returned = true} else - local _258_ = exprs - _258_["returned"] = true - return _258_ + local _274_ = exprs + _274_["returned"] = true + return _274_ end end local function find_macro(ast, scope, multi_sym_parts) @@ -2409,12 +2598,12 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return macro_2a end end - local function propagate_trace_info(_262_, _index, node) - local _arg_263_ = _262_ - local filename = _arg_263_["filename"] - local line = _arg_263_["line"] - local bytestart = _arg_263_["bytestart"] - local byteend = _arg_263_["byteend"] + local function propagate_trace_info(_278_, _index, node) + local _arg_279_ = _278_ + local filename = _arg_279_["filename"] + local line = _arg_279_["line"] + local bytestart = _arg_279_["bytestart"] + local byteend = _arg_279_["byteend"] if (("table" == type(node)) and (filename ~= node.filename)) then local src = utils["ast-source"](node) src.filename, src.line = filename, line @@ -2424,33 +2613,33 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return ("table" == type(node)) end local function macroexpand_2a(ast, scope, _3fonce) - local _265_ + local _281_ if utils["list?"](ast) then - _265_ = find_macro(ast, scope, utils["multi-sym?"](ast[1])) + _281_ = find_macro(ast, scope, utils["multi-sym?"](ast[1])) else - _265_ = nil + _281_ = nil end - if (_265_ == false) then + if (_281_ == false) then return ast - elseif (nil ~= _265_) then - local macro_2a = _265_ + elseif (nil ~= _281_) then + local macro_2a = _281_ local old_scope = scopes.macro local _ scopes.macro = scope _ = nil local ok, transformed = nil, nil - local function _267_() + local function _283_() return macro_2a(unpack(ast, 2)) end - ok, transformed = xpcall(_267_, debug.traceback) - local function _269_() - local _268_ = ast - local function _270_(...) - return propagate_trace_info(_268_, ...) + ok, transformed = xpcall(_283_, debug.traceback) + local function _285_() + local _284_ = ast + local function _286_(...) + return propagate_trace_info(_284_, ...) end - return _270_ + return _286_ end - utils["walk-tree"](transformed, _269_()) + utils["walk-tree"](transformed, _285_()) scopes.macro = old_scope assert_compile(ok, transformed, ast) if (_3fonce or not transformed) then @@ -2459,7 +2648,7 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return macroexpand_2a(transformed, scope) end elseif true then - local _ = _265_ + local _ = _281_ return ast else return nil @@ -2493,13 +2682,13 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct assert_compile((("string" == type(ast[1])) or (fcallee.type ~= "literal")), ("cannot call literal value " .. tostring(ast[1])), ast) for i = 2, len do local subexprs - local _276_ + local _292_ if (i ~= len) then - _276_ = 1 + _292_ = 1 else - _276_ = nil + _292_ = nil end - subexprs = compile1(ast[i], scope, parent, {nval = _276_}) + subexprs = compile1(ast[i], scope, parent, {nval = _292_}) table.insert(fargs, (subexprs[1] or utils.expr("nil", "literal"))) if (i == len) then for j = 2, #subexprs do @@ -2530,14 +2719,20 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct elseif (multi_sym_parts and multi_sym_parts["multi-sym-method-call"]) then local table_with_method = table.concat({unpack(multi_sym_parts, 1, (#multi_sym_parts - 1))}, ".") local method_to_call = multi_sym_parts[#multi_sym_parts] - local new_ast = utils.list(utils.sym(":", nil, scope), utils.sym(table_with_method, nil, scope), method_to_call, select(2, unpack(ast))) + local new_ast = utils.list(utils.sym(":", ast), utils.sym(table_with_method, ast), method_to_call, select(2, unpack(ast))) return compile1(new_ast, scope, parent, opts) else return compile_function_call(ast, scope, parent, opts, compile1, len) end end local function compile_varg(ast, scope, parent, opts) - assert_compile(scope.vararg, "unexpected vararg", ast) + local _297_ + if scope.hashfn then + _297_ = "use $... in hashfn" + else + _297_ = "unexpected vararg" + end + assert_compile(scope.vararg, _297_, ast) return handle_compile_opts({utils.expr("...", "varg")}, parent, opts, ast) end local function compile_sym(ast, scope, parent, opts) @@ -2552,20 +2747,20 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return handle_compile_opts({e}, parent, opts, ast) end local function serialize_number(n) - local _282_ = string.gsub(tostring(n), ",", ".") - return _282_ + local _300_ = string.gsub(tostring(n), ",", ".") + return _300_ end local function compile_scalar(ast, _scope, parent, opts) local serialize do - local _283_ = type(ast) - if (_283_ == "nil") then + local _301_ = type(ast) + if (_301_ == "nil") then serialize = tostring - elseif (_283_ == "boolean") then + elseif (_301_ == "boolean") then serialize = tostring - elseif (_283_ == "string") then + elseif (_301_ == "string") then serialize = serialize_string - elseif (_283_ == "number") then + elseif (_301_ == "number") then serialize = serialize_number else serialize = nil @@ -2580,8 +2775,8 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct if ((type(k) == "string") and utils["valid-lua-identifier?"](k)) then return {k, k} else - local _let_285_ = compile1(k, scope, parent, {nval = 1}) - local compiled = _let_285_[1] + local _let_303_ = compile1(k, scope, parent, {nval = 1}) + local compiled = _let_303_[1] local kstr = ("[" .. tostring(compiled) .. "]") return {kstr, k} end @@ -2604,15 +2799,15 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end keys = tbl_14_auto end - local function _291_(_289_) - local _arg_290_ = _289_ - local k1 = _arg_290_[1] - local k2 = _arg_290_[2] - local _let_292_ = compile1(ast[k2], scope, parent, {nval = 1}) - local v = _let_292_[1] + local function _309_(_307_) + local _arg_308_ = _307_ + local k1 = _arg_308_[1] + local k2 = _arg_308_[2] + local _let_310_ = compile1(ast[k2], scope, parent, {nval = 1}) + local v = _let_310_[1] return string.format("%s = %s", k1, tostring(v)) end - utils.map(keys, _291_, buffer) + utils.map(keys, _309_, buffer) end for i = 1, #ast do local nval = ((i ~= #ast) and 1) @@ -2639,12 +2834,12 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end local function destructure(to, from, ast, scope, parent, opts) local opts0 = (opts or {}) - local _let_294_ = opts0 - local isvar = _let_294_["isvar"] - local declaration = _let_294_["declaration"] - local forceglobal = _let_294_["forceglobal"] - local forceset = _let_294_["forceset"] - local symtype = _let_294_["symtype"] + local _let_312_ = opts0 + local isvar = _let_312_["isvar"] + local declaration = _let_312_["declaration"] + local forceglobal = _let_312_["forceglobal"] + local forceset = _let_312_["forceset"] + local symtype = _let_312_["symtype"] local symtype0 = ("_" .. (symtype or "dst")) local setter if declaration then @@ -2683,14 +2878,14 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end local function compile_top_target(lvalues) local inits - local function _300_(_241) + local function _318_(_241) if scope.manglings[_241] then return _241 else return "nil" end end - inits = utils.map(lvalues, _300_) + inits = utils.map(lvalues, _318_) local init = table.concat(inits, ", ") local lvalue = table.concat(lvalues, ", ") local plen, plast = #parent, parent[#parent] @@ -2732,16 +2927,16 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct local s = gensym(scope, symtype0) local right do - local _307_ + local _325_ if top_3f then - _307_ = exprs1(compile1(from, scope, parent)) + _325_ = exprs1(compile1(from, scope, parent)) else - _307_ = exprs1(rightexprs) + _325_ = exprs1(rightexprs) end - if (_307_ == "") then + if (_325_ == "") then right = "nil" - elseif (nil ~= _307_) then - local right0 = _307_ + elseif (nil ~= _325_) then + local right0 = _325_ right = right0 else right = nil @@ -2826,8 +3021,11 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return ret end local function require_include(ast, scope, parent, opts) - opts.fallback = function(e) - utils.warn(("include module not found, falling back to require: %s"):format(tostring(e))) + opts.fallback = function(e, no_warn) + if (not no_warn and ("literal" == e.type)) then + utils.warn(("include module not found, falling back to require: %s"):format(tostring(e))) + else + end return utils.expr(string.format("require(%s)", tostring(e)), "statement") end return scopes.global.specials.include(ast, scope, parent, opts) @@ -2898,7 +3096,7 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct else local remap = sourcemap[info.source] if (remap and remap[info.currentline]) then - if remap[info.currentline][1] then + if ((remap[info.currentline][1] or "unknown") ~= "unknown") then info.short_src = sourcemap[("@" .. remap[info.currentline][1])].short_src else info.short_src = remap.short_src @@ -2907,14 +3105,14 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct else end if (info.what == "Lua") then - local function _325_() + local function _344_() if info.name then return ("'" .. info.name .. "'") else return "?" end end - return string.format(" %s:%d: in function %s", info.short_src, info.currentline, _325_()) + return string.format(" %s:%d: in function %s", info.short_src, info.currentline, _344_()) elseif (info.short_src == "(tail call)") then return " (tail call)" else @@ -2922,27 +3120,27 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end end end - local function traceback(msg, start) - local msg0 = tostring((msg or "")) - if ((msg0:find("^Compile error") or msg0:find("^Parse error")) and not utils["debug-on?"]("trace")) then - return msg0 + local function traceback(_3fmsg, _3fstart) + local msg = tostring((_3fmsg or "")) + if ((msg:find("^Compile error") or msg:find("^Parse error")) and not utils["debug-on?"]("trace")) then + return msg else local lines = {} - if (msg0:find(":%d+: Compile error") or msg0:find(":%d+: Parse error")) then - table.insert(lines, msg0) + if (msg:find(":%d+: Compile error") or msg:find(":%d+: Parse error")) then + table.insert(lines, msg) else - local newmsg = msg0:gsub("^[^:]*:%d+:%s+", "runtime error: ") + local newmsg = msg:gsub("^[^:]*:%d+:%s+", "runtime error: ") table.insert(lines, newmsg) end table.insert(lines, "stack traceback:") - local done_3f, level = false, (start or 2) + local done_3f, level = false, (_3fstart or 2) while not done_3f do do - local _329_ = debug.getinfo(level, "Sln") - if (_329_ == nil) then + local _348_ = debug.getinfo(level, "Sln") + if (_348_ == nil) then done_3f = true - elseif (nil ~= _329_) then - local info = _329_ + elseif (nil ~= _348_) then + local info = _348_ table.insert(lines, traceback_frame(info)) else end @@ -2953,14 +3151,14 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end end local function entry_transform(fk, fv) - local function _332_(k, v) + local function _351_(k, v) if (type(k) == "number") then return k, fv(v) else return fk(k), fv(v) end end - return _332_ + return _351_ end local function mixed_concat(t, joiner) local seen = {} @@ -3006,10 +3204,10 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct return res[1] elseif utils["list?"](form) then local mapped - local function _337_() + local function _356_() return nil end - mapped = utils.kvmap(form, entry_transform(_337_, q)) + mapped = utils.kvmap(form, entry_transform(_356_, q)) local filename if form.filename then filename = string.format("%q", form.filename) @@ -3027,13 +3225,13 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct else filename = "nil" end - local _340_ + local _359_ if source then - _340_ = source.line + _359_ = source.line else - _340_ = "nil" + _359_ = "nil" end - return string.format("setmetatable({%s}, {filename=%s, line=%s, sequence=%s})", mixed_concat(mapped, ", "), filename, _340_, "(getmetatable(sequence()))['sequence']") + return string.format("setmetatable({%s}, {filename=%s, line=%s, sequence=%s})", mixed_concat(mapped, ", "), filename, _359_, "(getmetatable(sequence()))['sequence']") elseif (type(form) == "table") then local mapped = utils.kvmap(form, entry_transform(q, q)) local source = getmetatable(form) @@ -3043,14 +3241,14 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct else filename = "nil" end - local function _343_() + local function _362_() if source then return source.line else return "nil" end end - return string.format("setmetatable({%s}, {filename=%s, line=%s})", mixed_concat(mapped, ", "), filename, _343_()) + return string.format("setmetatable({%s}, {filename=%s, line=%s})", mixed_concat(mapped, ", "), filename, _362_()) elseif (type(form) == "string") then return serialize_string(form) else @@ -3061,7 +3259,7 @@ package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or funct end package.preload["fennel.friend"] = package.preload["fennel.friend"] or function(...) local utils = require("fennel.utils") - local suggestions = {["unexpected multi symbol (.*)"] = {"removing periods or colons from %s"}, ["use of global (.*) is aliased by a local"] = {"renaming local %s", "refer to the global using _G.%s instead of directly"}, ["local (.*) was overshadowed by a special form or macro"] = {"renaming local %s"}, ["global (.*) conflicts with local"] = {"renaming local %s"}, ["expected var (.*)"] = {"declaring %s using var instead of let/local", "introducing a new local instead of changing the value of %s"}, ["expected macros to be table"] = {"ensuring your macro definitions return a table"}, ["expected each macro to be function"] = {"ensuring that the value for each key in your macros table contains a function", "avoid defining nested macro tables"}, ["macro not found in macro module"] = {"checking the keys of the imported macro module's returned table"}, ["macro tried to bind (.*) without gensym"] = {"changing to %s# when introducing identifiers inside macros"}, ["unknown identifier in strict mode: (.*)"] = {"looking to see if there's a typo", "using the _G table instead, eg. _G.%s if you really want a global", "moving this code to somewhere that %s is in scope", "binding %s as a local in the scope of this code"}, ["expected a function.* to call"] = {"removing the empty parentheses", "using square brackets if you want an empty table"}, ["cannot call literal value"] = {"checking for typos", "checking for a missing function name"}, ["unexpected vararg"] = {"putting \"...\" at the end of the fn parameters if the vararg was intended"}, ["multisym method calls may only be in call position"] = {"using a period instead of a colon to reference a table's fields", "putting parens around this"}, ["unused local (.*)"] = {"renaming the local to _%s if it is meant to be unused", "fixing a typo so %s is used", "disabling the linter which checks for unused locals"}, ["expected parameters"] = {"adding function parameters as a list of identifiers in brackets"}, ["unable to bind (.*)"] = {"replacing the %s with an identifier"}, ["expected rest argument before last parameter"] = {"moving & to right before the final identifier when destructuring"}, ["expected vararg as last parameter"] = {"moving the \"...\" to the end of the parameter list"}, ["expected symbol for function parameter: (.*)"] = {"changing %s to an identifier instead of a literal value"}, ["could not compile value of type "] = {"debugging the macro you're calling to return a list or table"}, ["expected local"] = {"looking for a typo", "looking for a local which is used out of its scope"}, ["expected body expression"] = {"putting some code in the body of this form after the bindings"}, ["expected binding and iterator"] = {"making sure you haven't omitted a local name or iterator"}, ["expected binding sequence"] = {"placing a table here in square brackets containing identifiers to bind"}, ["expected even number of name/value bindings"] = {"finding where the identifier or value is missing"}, ["may only be used at compile time"] = {"moving this to inside a macro if you need to manipulate symbols/lists", "using square brackets instead of parens to construct a table"}, ["unexpected closing delimiter (.)"] = {"deleting %s", "adding matching opening delimiter earlier"}, ["mismatched closing delimiter (.), expected (.)"] = {"replacing %s with %s", "deleting %s", "adding matching opening delimiter earlier"}, ["expected even number of values in table literal"] = {"removing a key", "adding a value"}, ["expected whitespace before opening delimiter"] = {"adding whitespace"}, ["illegal character: (.)"] = {"deleting or replacing %s", "avoiding reserved characters like \", \\, ', ~, ;, @, `, and comma"}, ["could not read number (.*)"] = {"removing the non-digit character", "beginning the identifier with a non-digit if it is not meant to be a number"}, ["can't start multisym segment with a digit"] = {"removing the digit", "adding a non-digit before the digit"}, ["malformed multisym"] = {"ensuring each period or colon is not followed by another period or colon"}, ["method must be last component"] = {"using a period instead of a colon for field access", "removing segments after the colon", "making the method call, then looking up the field on the result"}, ["$ and $... in hashfn are mutually exclusive"] = {"modifying the hashfn so it only contains $... or $, $1, $2, $3, etc"}, ["tried to reference a macro at runtime"] = {"renaming the macro so as not to conflict with locals"}, ["expected even number of pattern/body pairs"] = {"checking that every pattern has a body to go with it", "adding _ before the final body"}, ["unexpected arguments"] = {"removing an argument", "checking for typos"}, ["unexpected iterator clause"] = {"removing an argument", "checking for typos"}} + local suggestions = {["unexpected multi symbol (.*)"] = {"removing periods or colons from %s"}, ["use of global (.*) is aliased by a local"] = {"renaming local %s", "refer to the global using _G.%s instead of directly"}, ["local (.*) was overshadowed by a special form or macro"] = {"renaming local %s"}, ["global (.*) conflicts with local"] = {"renaming local %s"}, ["expected var (.*)"] = {"declaring %s using var instead of let/local", "introducing a new local instead of changing the value of %s"}, ["expected macros to be table"] = {"ensuring your macro definitions return a table"}, ["expected each macro to be function"] = {"ensuring that the value for each key in your macros table contains a function", "avoid defining nested macro tables"}, ["macro not found in macro module"] = {"checking the keys of the imported macro module's returned table"}, ["macro tried to bind (.*) without gensym"] = {"changing to %s# when introducing identifiers inside macros"}, ["unknown identifier in strict mode: (.*)"] = {"looking to see if there's a typo", "using the _G table instead, eg. _G.%s if you really want a global", "moving this code to somewhere that %s is in scope", "binding %s as a local in the scope of this code"}, ["expected a function.* to call"] = {"removing the empty parentheses", "using square brackets if you want an empty table"}, ["cannot call literal value"] = {"checking for typos", "checking for a missing function name"}, ["unexpected vararg"] = {"putting \"...\" at the end of the fn parameters if the vararg was intended"}, ["multisym method calls may only be in call position"] = {"using a period instead of a colon to reference a table's fields", "putting parens around this"}, ["unused local (.*)"] = {"renaming the local to _%s if it is meant to be unused", "fixing a typo so %s is used", "disabling the linter which checks for unused locals"}, ["expected parameters"] = {"adding function parameters as a list of identifiers in brackets"}, ["unable to bind (.*)"] = {"replacing the %s with an identifier"}, ["expected rest argument before last parameter"] = {"moving & to right before the final identifier when destructuring"}, ["expected vararg as last parameter"] = {"moving the \"...\" to the end of the parameter list"}, ["expected symbol for function parameter: (.*)"] = {"changing %s to an identifier instead of a literal value"}, ["could not compile value of type "] = {"debugging the macro you're calling to return a list or table"}, ["expected local"] = {"looking for a typo", "looking for a local which is used out of its scope"}, ["expected body expression"] = {"putting some code in the body of this form after the bindings"}, ["expected binding and iterator"] = {"making sure you haven't omitted a local name or iterator"}, ["expected binding sequence"] = {"placing a table here in square brackets containing identifiers to bind"}, ["expected even number of name/value bindings"] = {"finding where the identifier or value is missing"}, ["may only be used at compile time"] = {"moving this to inside a macro if you need to manipulate symbols/lists", "using square brackets instead of parens to construct a table"}, ["unexpected closing delimiter (.)"] = {"deleting %s", "adding matching opening delimiter earlier"}, ["mismatched closing delimiter (.), expected (.)"] = {"replacing %s with %s", "deleting %s", "adding matching opening delimiter earlier"}, ["expected even number of values in table literal"] = {"removing a key", "adding a value"}, ["expected whitespace before opening delimiter"] = {"adding whitespace"}, ["invalid character: (.)"] = {"deleting or replacing %s", "avoiding reserved characters like \", \\, ', ~, ;, @, `, and comma"}, ["could not read number (.*)"] = {"removing the non-digit character", "beginning the identifier with a non-digit if it is not meant to be a number"}, ["can't start multisym segment with a digit"] = {"removing the digit", "adding a non-digit before the digit"}, ["malformed multisym"] = {"ensuring each period or colon is not followed by another period or colon"}, ["method must be last component"] = {"using a period instead of a colon for field access", "removing segments after the colon", "making the method call, then looking up the field on the result"}, ["$ and $... in hashfn are mutually exclusive"] = {"modifying the hashfn so it only contains $... or $, $1, $2, $3, etc"}, ["tried to reference a macro at runtime"] = {"renaming the macro so as not to conflict with locals"}, ["tried to reference a special form at runtime"] = {"wrapping the special in a function if you need it to be first class"}, ["expected even number of pattern/body pairs"] = {"checking that every pattern has a body to go with it", "adding _ before the final body"}, ["unexpected arguments"] = {"removing an argument", "checking for typos"}, ["unexpected iterator clause"] = {"removing an argument", "checking for typos"}} local unpack = (table.unpack or _G.unpack) local function suggest(msg) local suggestion = nil @@ -3113,12 +3311,12 @@ package.preload["fennel.friend"] = package.preload["fennel.friend"] or function( return read_line_from_file(filename, line) end end - local function friendly_msg(msg, _142_, source) - local _arg_143_ = _142_ - local filename = _arg_143_["filename"] - local line = _arg_143_["line"] - local bytestart = _arg_143_["bytestart"] - local byteend = _arg_143_["byteend"] + local function friendly_msg(msg, _156_, source) + local _arg_157_ = _156_ + local filename = _arg_157_["filename"] + local line = _arg_157_["line"] + local bytestart = _arg_157_["bytestart"] + local byteend = _arg_157_["byteend"] local ok, codeline, bol = pcall(read_line, filename, line, source) local suggestions0 = suggest(msg) local out = {msg, ""} @@ -3145,9 +3343,9 @@ package.preload["fennel.friend"] = package.preload["fennel.friend"] or function( end local function assert_compile(condition, msg, ast, source) if not condition then - local _let_148_ = utils["ast-source"](ast) - local filename = _let_148_["filename"] - local line = _let_148_["line"] + local _let_162_ = utils["ast-source"](ast) + local filename = _let_162_["filename"] + local line = _let_162_["line"] error(friendly_msg(("Compile error in %s:%s\n %s"):format((filename or "unknown"), (line or "?"), msg), utils["ast-source"](ast), source), 0) else end @@ -3164,25 +3362,25 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( local unpack = (table.unpack or _G.unpack) local function granulate(getchunk) local c, index, done_3f = "", 1, false - local function _150_(parser_state) + local function _164_(parser_state) if not done_3f then if (index <= #c) then local b = c:byte(index) index = (index + 1) return b else - local _151_ = getchunk(parser_state) - local function _152_() - local char = _151_ + local _165_ = getchunk(parser_state) + local function _166_() + local char = _165_ return (char ~= "") end - if ((nil ~= _151_) and _152_()) then - local char = _151_ + if ((nil ~= _165_) and _166_()) then + local char = _165_ c = char index = 2 return c:byte() elseif true then - local _ = _151_ + local _ = _165_ done_3f = true return nil else @@ -3193,21 +3391,21 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( return nil end end - local function _156_() + local function _170_() c = "" return nil end - return _150_, _156_ + return _164_, _170_ end local function string_stream(str) local str0 = str:gsub("^#!", ";;") local index = 1 - local function _157_() + local function _171_() local r = str0:byte(index) index = (index + 1) return r end - return _157_ + return _171_ end local delims = {[40] = 41, [41] = true, [91] = 93, [93] = true, [123] = 125, [125] = true} local function whitespace_3f(b) @@ -3223,7 +3421,11 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( return ((b0 > 32) and not delims[b0] and (b0 ~= 127) and (b0 ~= 34) and (b0 ~= 39) and (b0 ~= 126) and (b0 ~= 59) and (b0 ~= 44) and (b0 ~= 64) and (b0 ~= 96)) end local prefixes = {[35] = "hashfn", [39] = "quote", [44] = "unquote", [96] = "quote"} - local function parser(getbyte, _3ffilename, _3foptions) + local function parser_fn(getbyte, filename, _173_) + local _arg_174_ = _173_ + local source = _arg_174_["source"] + local unfriendly = _arg_174_["unfriendly"] + local comments = _arg_174_["comments"] local stack = {} local line = 1 local byteindex = 0 @@ -3251,17 +3453,13 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end return r end - assert(((nil == _3ffilename) or ("string" == type(_3ffilename))), "expected filename as second argument to parser") local function parse_error(msg, byteindex_override) - local _let_162_ = (_3foptions or utils.root.options or {}) - local source = _let_162_["source"] - local unfriendly = _let_162_["unfriendly"] - if (nil == utils.hook("parse-error", msg, (_3ffilename or "unknown"), (line or "?"), (byteindex_override or byteindex), source, utils.root.reset)) then + if (nil == utils.hook("parse-error", msg, filename, (line or "?"), (byteindex_override or byteindex), source, utils.root.reset)) then utils.root.reset() if (unfriendly or not friend or not _G.io or not _G.io.read) then - return error(string.format("%s:%s: Parse error: %s", (_3ffilename or "unknown"), (line or "?"), msg), 0) + return error(string.format("%s:%s: Parse error: %s", filename, (line or "?"), msg), 0) else - return friend["parse-error"](msg, (_3ffilename or "unknown"), (line or "?"), (byteindex_override or byteindex), source) + return friend["parse-error"](msg, filename, (line or "?"), (byteindex_override or byteindex), source) end else return nil @@ -3270,25 +3468,25 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( local function parse_stream() local whitespace_since_dispatch, done_3f, retval = true local function dispatch(v) - local _165_ = stack[#stack] - if (_165_ == nil) then + local _180_ = stack[#stack] + if (_180_ == nil) then retval, done_3f, whitespace_since_dispatch = v, true, false return nil - elseif ((_G.type(_165_) == "table") and (nil ~= (_165_).prefix)) then - local prefix = (_165_).prefix - local source + elseif ((_G.type(_180_) == "table") and (nil ~= (_180_).prefix)) then + local prefix = (_180_).prefix + local source0 do - local _166_ = table.remove(stack) - do end (_166_)["byteend"] = byteindex - source = _166_ + local _181_ = table.remove(stack) + do end (_181_)["byteend"] = byteindex + source0 = _181_ end - local list = utils.list(utils.sym(prefix, source), v) - for k, v0 in pairs(source) do + local list = utils.list(utils.sym(prefix, source0), v) + for k, v0 in pairs(source0) do list[k] = v0 end return dispatch(list) - elseif (nil ~= _165_) then - local top = _165_ + elseif (nil ~= _180_) then + local top = _180_ whitespace_since_dispatch = false return table.insert(top, v) else @@ -3297,13 +3495,13 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end local function badend() local accum = utils.map(stack, "closer") - local _168_ + local _183_ if (#stack == 1) then - _168_ = "" + _183_ = "" else - _168_ = "s" + _183_ = "s" end - return parse_error(string.format("expected closing delimiter%s %s", _168_, string.char(unpack(accum)))) + return parse_error(string.format("expected closing delimiter%s %s", _183_, string.char(unpack(accum)))) end local function skip_whitespace(b) if (b and whitespace_3f(b)) then @@ -3317,14 +3515,14 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end local function parse_comment(b, contents) if (b and (10 ~= b)) then - local function _172_() - local _171_ = contents - table.insert(_171_, string.char(b)) - return _171_ + local function _187_() + local _186_ = contents + table.insert(_186_, string.char(b)) + return _186_ end - return parse_comment(getb(), _172_()) - elseif (_3foptions and _3foptions.comments) then - return dispatch(utils.comment(table.concat(contents), {line = (line - 1), filename = _3ffilename})) + return parse_comment(getb(), _187_()) + elseif comments then + return dispatch(utils.comment(table.concat(contents), {line = (line - 1), filename = filename})) else return b end @@ -3334,7 +3532,7 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( parse_error(("expected whitespace before opening delimiter " .. string.char(b))) else end - return table.insert(stack, {bytestart = byteindex, closer = delims[b], filename = _3ffilename, line = line}) + return table.insert(stack, {bytestart = byteindex, closer = delims[b], filename = filename, line = line}) end local function close_list(list) return dispatch(setmetatable(list, getmetatable(utils.list()))) @@ -3346,14 +3544,14 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end return dispatch(val) end - local function add_comment_at(comments, index, node) - local _175_ = comments[index] - if (nil ~= _175_) then - local existing = _175_ + local function add_comment_at(comments0, index, node) + local _190_ = (comments0)[index] + if (nil ~= _190_) then + local existing = _190_ return table.insert(existing, node) elseif true then - local _ = _175_ - comments[index] = {node} + local _ = _190_ + comments0[index] = {node} return nil else return nil @@ -3367,18 +3565,18 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end end local function extract_comments(tbl) - local comments = {keys = {}, values = {}, last = {}} + local comments0 = {keys = {}, values = {}, last = {}} while utils["comment?"](tbl[#tbl]) do - table.insert(comments.last, 1, table.remove(tbl)) + table.insert(comments0.last, 1, table.remove(tbl)) end local last_key_3f = false for i, node in ipairs(tbl) do if not utils["comment?"](node) then last_key_3f = not last_key_3f elseif last_key_3f then - add_comment_at(comments.values, next_noncomment(tbl, i), node) + add_comment_at(comments0.values, next_noncomment(tbl, i), node) else - add_comment_at(comments.keys, next_noncomment(tbl, i), node) + add_comment_at(comments0.keys, next_noncomment(tbl, i), node) end end for i = #tbl, 1, -1 do @@ -3387,10 +3585,10 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( else end end - return comments + return comments0 end local function close_curly_table(tbl) - local comments = extract_comments(tbl) + local comments0 = extract_comments(tbl) local keys = {} local val = {} if ((#tbl % 2) ~= 0) then @@ -3407,7 +3605,7 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( val[tbl[i]] = tbl[(i + 1)] table.insert(keys, tbl[i]) end - tbl.comments = comments + tbl.comments = comments0 tbl.keys = keys return dispatch(val) end @@ -3434,16 +3632,16 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( table.insert(chars, b) local state0 do - local _185_ = {state, b} - if ((_G.type(_185_) == "table") and ((_185_)[1] == "base") and ((_185_)[2] == 92)) then + local _200_ = {state, b} + if ((_G.type(_200_) == "table") and ((_200_)[1] == "base") and ((_200_)[2] == 92)) then state0 = "backslash" - elseif ((_G.type(_185_) == "table") and ((_185_)[1] == "base") and ((_185_)[2] == 34)) then + elseif ((_G.type(_200_) == "table") and ((_200_)[1] == "base") and ((_200_)[2] == 34)) then state0 = "done" - elseif ((_G.type(_185_) == "table") and ((_185_)[1] == "backslash") and ((_185_)[2] == 10)) then + elseif ((_G.type(_200_) == "table") and ((_200_)[1] == "backslash") and ((_200_)[2] == 10)) then table.remove(chars, (#chars - 1)) state0 = "base" elseif true then - local _ = _185_ + local _ = _200_ state0 = "base" else state0 = nil @@ -3468,18 +3666,18 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( table.remove(stack) local raw = string.char(unpack(chars)) local formatted = raw:gsub("[\7-\13]", escape_char) - local _189_ = (rawget(_G, "loadstring") or load)(("return " .. formatted)) - if (nil ~= _189_) then - local load_fn = _189_ + local _204_ = (rawget(_G, "loadstring") or load)(("return " .. formatted)) + if (nil ~= _204_) then + local load_fn = _204_ return dispatch(load_fn()) - elseif (_189_ == nil) then + elseif (_204_ == nil) then return parse_error(("Invalid string: " .. raw)) else return nil end end local function parse_prefix(b) - table.insert(stack, {prefix = prefixes[b], filename = _3ffilename, line = line, bytestart = byteindex}) + table.insert(stack, {prefix = prefixes[b], filename = filename, line = line, bytestart = byteindex}) local nextb = getb() if (whitespace_3f(nextb) or (true == delims[nextb])) then if (b ~= 35) then @@ -3510,13 +3708,13 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( dispatch((tonumber(number_with_stripped_underscores) or parse_error(("could not read number \"" .. rawstr .. "\"")))) return true else - local _195_ = tonumber(number_with_stripped_underscores) - if (nil ~= _195_) then - local x = _195_ + local _210_ = tonumber(number_with_stripped_underscores) + if (nil ~= _210_) then + local x = _210_ dispatch(x) return true elseif true then - local _ = _195_ + local _ = _210_ return false else return nil @@ -3525,7 +3723,7 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end local function check_malformed_sym(rawstr) if (rawstr:match("^~") and (rawstr ~= "~=")) then - return parse_error("illegal character: ~") + return parse_error("invalid character: ~") elseif rawstr:match("%.[0-9]") then return parse_error(("can't start multisym segment with a digit: " .. rawstr), (((byteindex - #rawstr) + rawstr:find("%.[0-9]")) + 1)) elseif (rawstr:match("[%.:][%.:]") and (rawstr ~= "..") and (rawstr ~= "$...")) then @@ -3541,16 +3739,17 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( local function parse_sym(b) local bytestart = byteindex local rawstr = string.char(unpack(parse_sym_loop({b}, getb()))) + local source0 = {byteend = byteindex, bytestart = bytestart, filename = filename, line = line} if (rawstr == "true") then return dispatch(true) elseif (rawstr == "false") then return dispatch(false) elseif (rawstr == "...") then - return dispatch(utils.varg()) + return dispatch(utils.varg(source0)) elseif rawstr:match("^:.+$") then return dispatch(rawstr:sub(2)) elseif not parse_number(rawstr) then - return dispatch(utils.sym(check_malformed_sym(rawstr), {byteend = byteindex, bytestart = bytestart, filename = _3ffilename, line = line})) + return dispatch(utils.sym(check_malformed_sym(rawstr), source0)) else return nil end @@ -3570,7 +3769,7 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( elseif (sym_char_3f(b) or (b == string.byte("~"))) then parse_sym(b) elseif not utils.hook("illegal-char", b, getb, ungetb, dispatch) then - parse_error(("illegal character: " .. string.char(b))) + parse_error(("invalid character: " .. string.char(b))) else end if not b then @@ -3583,11 +3782,21 @@ package.preload["fennel.parser"] = package.preload["fennel.parser"] or function( end return parse_loop(skip_whitespace(getb())) end - local function _202_() + local function _217_() stack, line, byteindex, lastb = {}, 1, 0, nil return nil end - return parse_stream, _202_ + return parse_stream, _217_ + end + local function parser(stream_or_string, _3ffilename, _3foptions) + local filename = (_3ffilename or "unknown") + local options = (_3foptions or utils.root.options or {}) + assert(("string" == type(filename)), "expected filename as second argument to parser") + if ("string" == type(stream_or_string)) then + return parser_fn(string_stream(stream_or_string), filename, options) + else + return parser_fn(stream_or_string, filename, options) + end end return {granulate = granulate, parser = parser, ["string-stream"] = string_stream, ["sym-char?"] = sym_char_3f} end @@ -4150,7 +4359,37 @@ package.preload["fennel.view"] = package.preload["fennel.view"] or function(...) end package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(...) local view = require("fennel.view") - local version = "1.0.0" + local version = "1.1.0" + local function luajit_vm_3f() + return ((nil ~= jit) and (type(jit) == "table") and (nil ~= jit.on) and (nil ~= jit.off) and (type(jit.version_num) == "number")) + end + local function luajit_vm_version() + local jit_os + if (jit.os == "OSX") then + jit_os = "macOS" + else + jit_os = jit.os + end + return (jit.version .. " " .. jit_os .. "/" .. jit.arch) + end + local function fengari_vm_3f() + return ((nil ~= fengari) and (type(fengari) == "table") and (nil ~= fengari.VERSION) and (type(fengari.VERSION_NUM) == "number")) + end + local function fengari_vm_version() + return (fengari.RELEASE .. " (" .. _VERSION .. ")") + end + local function lua_vm_version() + if luajit_vm_3f() then + return luajit_vm_version() + elseif fengari_vm_3f() then + return fengari_vm_version() + else + return ("PUC " .. _VERSION) + end + end + local function runtime_version() + return ("Fennel " .. version .. " on " .. lua_vm_version()) + end local function warn(message) if (_G.io and _G.io.stderr) then return (_G.io.stderr):write(("--WARNING: %s\n"):format(tostring(message))) @@ -4180,10 +4419,10 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. for k in pairs(t) do table.insert(keys, k) end - local function _100_(_241, _242) + local function _102_(_241, _242) return (tostring(_241) < tostring(_242)) end - table.sort(keys, _100_) + table.sort(keys, _102_) end for i, k in ipairs(keys) do succ[k] = keys[(i + 1)] @@ -4211,15 +4450,15 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. if (type(f) == "function") then f0 = f else - local function _104_(_241) + local function _106_(_241) return (_241)[f] end - f0 = _104_ + f0 = _106_ end for _, x in ipairs(t) do - local _106_ = f0(x) - if (nil ~= _106_) then - local v = _106_ + local _108_ = f0(x) + if (nil ~= _108_) then + local v = _108_ table.insert(out, v) else end @@ -4232,19 +4471,19 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. if (type(f) == "function") then f0 = f else - local function _108_(_241) + local function _110_(_241) return (_241)[f] end - f0 = _108_ + f0 = _110_ end for k, x in stablepairs(t) do - local _110_, _111_ = f0(k, x) - if ((nil ~= _110_) and (nil ~= _111_)) then - local key = _110_ - local value = _111_ + local _112_, _113_ = f0(k, x) + if ((nil ~= _112_) and (nil ~= _113_)) then + local key = _112_ + local value = _113_ out[key] = value - elseif (nil ~= _110_) then - local value = _110_ + elseif (nil ~= _112_) then + local value = _112_ table.insert(out, value) else end @@ -4252,20 +4491,26 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. return out end local function copy(from, _3fto) - local to = (_3fto or {}) + local tbl_11_auto = (_3fto or {}) for k, v in pairs((from or {})) do - to[k] = v + local _115_, _116_ = k, v + if ((nil ~= _115_) and (nil ~= _116_)) then + local k_12_auto = _115_ + local v_13_auto = _116_ + tbl_11_auto[k_12_auto] = v_13_auto + else + end end - return to + return tbl_11_auto end local function member_3f(x, tbl, _3fn) - local _113_ = tbl[(_3fn or 1)] - if (_113_ == x) then + local _118_ = tbl[(_3fn or 1)] + if (_118_ == x) then return true - elseif (_113_ == nil) then + elseif (_118_ == nil) then return nil elseif true then - local _ = _113_ + local _ = _118_ return member_3f(x, tbl, ((_3fn or 1) + 1)) else return nil @@ -4283,9 +4528,9 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. seen[next_state] = true return next_state, value else - local _115_ = getmetatable(t) - if ((_G.type(_115_) == "table") and true) then - local __index = (_115_).__index + local _120_ = getmetatable(t) + if ((_G.type(_120_) == "table") and true) then + local __index = (_120_).__index if ("table" == type(__index)) then t = __index return allpairs_next(t) @@ -4327,19 +4572,19 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. end local symbol_mt = {__fennelview = deref, __tostring = deref, __eq = sym_3d, __lt = sym_3c, "SYMBOL"} local expr_mt - local function _120_(x) + local function _125_(x) return tostring(deref(x)) end - expr_mt = {__tostring = _120_, "EXPR"} + expr_mt = {__tostring = _125_, "EXPR"} local list_mt = {__fennelview = list__3estring, __tostring = list__3estring, "LIST"} local comment_mt = {__fennelview = comment_view, __tostring = deref, __eq = sym_3d, __lt = sym_3c, "COMMENT"} local sequence_marker = {"SEQUENCE"} - local vararg = setmetatable({"..."}, {__fennelview = deref, __tostring = deref, "VARARG"}) + local varg_mt = {__fennelview = deref, __tostring = deref, "VARARG"} local getenv - local function _121_() + local function _126_() return nil end - getenv = ((os and os.getenv) or _121_) + getenv = ((os and os.getenv) or _126_) local function debug_on_3f(flag) local level = (getenv("FENNEL_DEBUG") or "") return ((level == "all") or level:find(flag)) @@ -4347,15 +4592,27 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. local function list(...) return setmetatable({...}, list_mt) end - local function sym(str, _3fsource, _3fscope) - local s = {["?scope"] = _3fscope, str} - for k, v in pairs((_3fsource or {})) do - if (type(k) == "string") then - s[k] = v - else + local function sym(str, _3fsource) + local _127_ + do + local tbl_11_auto = {str} + for k, v in pairs((_3fsource or {})) do + local _128_, _129_ = nil, nil + if (type(k) == "string") then + _128_, _129_ = k, v + else + _128_, _129_ = nil + end + if ((nil ~= _128_) and (nil ~= _129_)) then + local k_12_auto = _128_ + local v_13_auto = _129_ + tbl_11_auto[k_12_auto] = v_13_auto + else + end end + _127_ = tbl_11_auto end - return setmetatable(s, symbol_mt) + return setmetatable(_127_, symbol_mt) end nil_sym = sym("nil") local function sequence(...) @@ -4365,19 +4622,38 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. return setmetatable({type = etype, strcode}, expr_mt) end local function comment_2a(contents, _3fsource) - local _let_123_ = (_3fsource or {}) - local filename = _let_123_["filename"] - local line = _let_123_["line"] + local _let_132_ = (_3fsource or {}) + local filename = _let_132_["filename"] + local line = _let_132_["line"] return setmetatable({filename = filename, line = line, contents}, comment_mt) end - local function varg() - return vararg + local function varg(_3fsource) + local _133_ + do + local tbl_11_auto = {"..."} + for k, v in pairs((_3fsource or {})) do + local _134_, _135_ = nil, nil + if (type(k) == "string") then + _134_, _135_ = k, v + else + _134_, _135_ = nil + end + if ((nil ~= _134_) and (nil ~= _135_)) then + local k_12_auto = _134_ + local v_13_auto = _135_ + tbl_11_auto[k_12_auto] = v_13_auto + else + end + end + _133_ = tbl_11_auto + end + return setmetatable(_133_, varg_mt) end local function expr_3f(x) return ((type(x) == "table") and (getmetatable(x) == expr_mt) and x) end local function varg_3f(x) - return ((x == vararg) and x) + return ((type(x) == "table") and (getmetatable(x) == varg_mt) and x) end local function list_3f(x) return ((type(x) == "table") and (getmetatable(x) == list_mt) and x) @@ -4393,7 +4669,10 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. return ((type(x) == "table") and (getmetatable(x) == comment_mt) and x) end local function table_3f(x) - return ((type(x) == "table") and (x ~= vararg) and (getmetatable(x) ~= list_mt) and (getmetatable(x) ~= symbol_mt) and not comment_3f(x) and x) + return ((type(x) == "table") and not varg_3f(x) and (getmetatable(x) ~= list_mt) and (getmetatable(x) ~= symbol_mt) and not comment_3f(x) and x) + end + local function string_3f(x) + return (type(x) == "string") end local function multi_sym_3f(str) if sym_3f(str) then @@ -4458,15 +4737,15 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. return subopts end local root - local function _129_() + local function _143_() end - root = {chunk = nil, scope = nil, options = nil, reset = _129_} - root["set-reset"] = function(_130_) - local _arg_131_ = _130_ - local chunk = _arg_131_["chunk"] - local scope = _arg_131_["scope"] - local options = _arg_131_["options"] - local reset = _arg_131_["reset"] + root = {chunk = nil, scope = nil, options = nil, reset = _143_} + root["set-reset"] = function(_144_) + local _arg_145_ = _144_ + local chunk = _arg_145_["chunk"] + local scope = _arg_145_["scope"] + local options = _arg_145_["options"] + local reset = _arg_145_["reset"] root.reset = function() root.chunk, root.scope, root.options, root.reset = chunk, scope, options, reset return nil @@ -4474,11 +4753,11 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. return root.reset end local warned = {} - local function check_plugin_version(_132_) - local _arg_133_ = _132_ - local name = _arg_133_["name"] - local versions = _arg_133_["versions"] - local plugin = _arg_133_ + local function check_plugin_version(_146_) + local _arg_147_ = _146_ + local name = _arg_147_["name"] + local versions = _arg_147_["versions"] + local plugin = _arg_147_ if (not member_3f(version:gsub("-dev", ""), (versions or {})) and not warned[plugin]) then warned[plugin] = true return warn(string.format("plugin %s does not support Fennel version %s", (name or "unknown"), version)) @@ -4492,9 +4771,9 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. for _, plugin in ipairs(root.options.plugins) do if result then break end check_plugin_version(plugin) - local _135_ = plugin[event] - if (nil ~= _135_) then - local f = _135_ + local _149_ = plugin[event] + if (nil ~= _149_) then + local f = _149_ result = f(...) else end @@ -4503,7 +4782,7 @@ package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(.. end return result end - return {warn = warn, allpairs = allpairs, stablepairs = stablepairs, copy = copy, kvmap = kvmap, map = map, ["walk-tree"] = walk_tree, ["member?"] = member_3f, list = list, sequence = sequence, sym = sym, varg = varg, expr = expr, comment = comment_2a, ["comment?"] = comment_3f, ["expr?"] = expr_3f, ["list?"] = list_3f, ["multi-sym?"] = multi_sym_3f, ["sequence?"] = sequence_3f, ["sym?"] = sym_3f, ["table?"] = table_3f, ["varg?"] = varg_3f, ["quoted?"] = quoted_3f, ["valid-lua-identifier?"] = valid_lua_identifier_3f, ["lua-keywords"] = lua_keywords, hook = hook, ["propagate-options"] = propagate_options, root = root, ["debug-on?"] = debug_on_3f, ["ast-source"] = ast_source, version = version, path = table.concat({"./?.fnl", "./?/init.fnl", getenv("FENNEL_PATH")}, ";"), ["macro-path"] = table.concat({"./?.fnl", "./?/init-macros.fnl", "./?/init.fnl", getenv("FENNEL_MACRO_PATH")}, ";")} + return {warn = warn, allpairs = allpairs, stablepairs = stablepairs, copy = copy, kvmap = kvmap, map = map, ["walk-tree"] = walk_tree, ["member?"] = member_3f, list = list, sequence = sequence, sym = sym, varg = varg, expr = expr, comment = comment_2a, ["comment?"] = comment_3f, ["expr?"] = expr_3f, ["list?"] = list_3f, ["multi-sym?"] = multi_sym_3f, ["sequence?"] = sequence_3f, ["sym?"] = sym_3f, ["table?"] = table_3f, ["varg?"] = varg_3f, ["quoted?"] = quoted_3f, ["string?"] = string_3f, ["valid-lua-identifier?"] = valid_lua_identifier_3f, ["lua-keywords"] = lua_keywords, hook = hook, ["propagate-options"] = propagate_options, root = root, ["debug-on?"] = debug_on_3f, ["ast-source"] = ast_source, version = version, ["runtime-version"] = runtime_version, path = table.concat({"./?.fnl", "./?/init.fnl", getenv("FENNEL_PATH")}, ";"), ["macro-path"] = table.concat({"./?.fnl", "./?/init-macros.fnl", "./?/init.fnl", getenv("FENNEL_MACRO_PATH")}, ";")} end utils = require("fennel.utils") local parser = require("fennel.parser") @@ -4544,14 +4823,14 @@ local function eval(str, options, ...) local env = eval_env(opts.env, opts) local lua_source = compiler["compile-string"](str, opts) local loader - local function _616_(...) + local function _678_(...) if opts.filename then return ("@" .. opts.filename) else return str end end - loader = specials["load-code"](lua_source, env, _616_(...)) + loader = specials["load-code"](lua_source, env, _678_(...)) opts.filename = nil return loader(...) end @@ -4576,10 +4855,10 @@ local function syntax() out[k] = {["macro?"] = true, ["body-form?"] = utils["member?"](k, body_3f), ["binding-form?"] = utils["member?"](k, binding_3f), ["define?"] = utils["member?"](k, define_3f)} end for k, v in pairs(_G) do - local _617_ = type(v) - if (_617_ == "function") then + local _679_ = type(v) + if (_679_ == "function") then out[k] = {["global?"] = true, ["function?"] = true} - elseif (_617_ == "table") then + elseif (_679_ == "table") then for k2, v2 in pairs(v) do if (("function" == type(v2)) and (k ~= "_G")) then out[(k .. "." .. k2)] = {["function?"] = true, ["global?"] = true} @@ -4592,18 +4871,16 @@ local function syntax() end return out end -local mod = {list = utils.list, ["list?"] = utils["list?"], sym = utils.sym, ["sym?"] = utils["sym?"], sequence = utils.sequence, ["sequence?"] = utils["sequence?"], comment = utils.comment, ["comment?"] = utils["comment?"], varg = utils.varg, path = utils.path, ["macro-path"] = utils["macro-path"], ["sym-char?"] = parser["sym-char?"], parser = parser.parser, granulate = parser.granulate, ["string-stream"] = parser["string-stream"], compile = compiler.compile, ["compile-string"] = compiler["compile-string"], ["compile-stream"] = compiler["compile-stream"], compile1 = compiler.compile1, traceback = compiler.traceback, mangle = compiler["global-mangling"], unmangle = compiler["global-unmangling"], metadata = compiler.metadata, scope = compiler["make-scope"], gensym = compiler.gensym, ["load-code"] = specials["load-code"], ["macro-loaded"] = specials["macro-loaded"], ["macro-searchers"] = specials["macro-searchers"], ["search-module"] = specials["search-module"], ["make-searcher"] = specials["make-searcher"], makeSearcher = specials["make-searcher"], searcher = specials["make-searcher"](), doc = specials.doc, view = view, eval = eval, dofile = dofile_2a, version = utils.version, repl = repl, syntax = syntax, loadCode = specials["load-code"], make_searcher = specials["make-searcher"], searchModule = specials["search-module"], macroLoaded = specials["macro-loaded"], compileStream = compiler["compile-stream"], compileString = compiler["compile-string"], stringStream = parser["string-stream"]} +local mod = {list = utils.list, ["list?"] = utils["list?"], sym = utils.sym, ["sym?"] = utils["sym?"], sequence = utils.sequence, ["sequence?"] = utils["sequence?"], comment = utils.comment, ["comment?"] = utils["comment?"], varg = utils.varg, ["varg?"] = utils["varg?"], ["sym-char?"] = parser["sym-char?"], parser = parser.parser, compile = compiler.compile, ["compile-string"] = compiler["compile-string"], ["compile-stream"] = compiler["compile-stream"], eval = eval, repl = repl, view = view, dofile = dofile_2a, ["load-code"] = specials["load-code"], doc = specials.doc, metadata = compiler.metadata, traceback = compiler.traceback, version = utils.version, ["runtime-version"] = utils["runtime-version"], path = utils.path, ["macro-path"] = utils["macro-path"], ["macro-loaded"] = specials["macro-loaded"], ["macro-searchers"] = specials["macro-searchers"], ["search-module"] = specials["search-module"], ["make-searcher"] = specials["make-searcher"], searcher = specials["make-searcher"](), syntax = syntax, gensym = compiler.gensym, scope = compiler["make-scope"], mangle = compiler["global-mangling"], unmangle = compiler["global-unmangling"], compile1 = compiler.compile1, ["string-stream"] = parser["string-stream"], granulate = parser.granulate, loadCode = specials["load-code"], make_searcher = specials["make-searcher"], makeSearcher = specials["make-searcher"], searchModule = specials["search-module"], macroPath = utils["macro-path"], macroSearchers = specials["macro-searchers"], macroLoaded = specials["macro-loaded"], compileStream = compiler["compile-stream"], compileString = compiler["compile-string"], stringStream = parser["string-stream"], runtimeVersion = utils["runtime-version"]} utils["fennel-module"] = mod do - local builtin_macros = [===[;; This module contains all the built-in Fennel macros. Unlike all the other - ;; modules that are loaded by the old bootstrap compiler, this runs in the - ;; compiler scope of the version of the compiler being defined. + local builtin_macros = [===[;; These macros are awkward because their definition cannot rely on the any + ;; built-in macros, only special forms. (no when, no icollect, etc) - ;; The code for these macros is somewhat idiosyncratic because it cannot use any - ;; macros which have not yet been defined. - - ;; TODO: some of these macros modify their arguments; we should stop doing that, - ;; but in a way that preserves file/line metadata. + (fn copy [t] + (let [out []] + (each [_ v (ipairs t)] (table.insert out v)) + (setmetatable out (getmetatable t)))) (fn ->* [val ...] "Thread-first macro. @@ -4611,7 +4888,7 @@ do The value of the second form is spliced into the first arg of the third, etc." (var x val) (each [_ e (ipairs [...])] - (let [elt (if (list? e) e (list e))] + (let [elt (copy (if (list? e) e (list e)))] (table.insert elt 2 x) (set x elt))) x) @@ -4622,39 +4899,37 @@ do rather than the first." (var x val) (each [_ e (ipairs [...])] - (let [elt (if (list? e) e (list e))] + (let [elt (copy (if (list? e) e (list e)))] (table.insert elt x) (set x elt))) x) - (fn -?>* [val ...] + (fn -?>* [val ?e ...] "Nil-safe thread-first macro. Same as -> except will short-circuit with nil when it encounters a nil value." - (if (= 0 (select "#" ...)) + (if (= nil ?e) val - (let [els [...] - e (table.remove els 1) + (let [e (copy ?e) el (if (list? e) e (list e)) tmp (gensym)] (table.insert el 2 tmp) `(let [,tmp ,val] (if (not= nil ,tmp) - (-?> ,el ,(unpack els)) + (-?> ,el ,...) ,tmp))))) - (fn -?>>* [val ...] + (fn -?>>* [val ?e ...] "Nil-safe thread-last macro. Same as ->> except will short-circuit with nil when it encounters a nil value." - (if (= 0 (select "#" ...)) + (if (= nil ?e) val - (let [els [...] - e (table.remove els 1) + (let [e (copy ?e) el (if (list? e) e (list e)) tmp (gensym)] (table.insert el tmp) `(let [,tmp ,val] (if (not= ,tmp nil) - (-?>> ,el ,(unpack els)) + (-?>> ,el ,...) ,tmp))))) (fn ?dot [tbl ...] @@ -4665,17 +4940,17 @@ do lookups `(do (var ,head ,tbl) ,head)] (each [_ k (ipairs [...])] ;; Kinda gnarly to reassign in place like this, but it emits the best lua. - ;; With this impl, it emits a flat, concise, and readable set of if blocks. + ;; With this impl, it emits a flat, concise, and readable set of ifs (table.insert lookups (# lookups) `(if (not= nil ,head) (set ,head (. ,head ,k))))) lookups)) (fn doto* [val ...] - "Evaluates val and splices it into the first argument of subsequent forms." + "Evaluate val and splice it into the first argument of subsequent forms." (let [name (gensym) form `(let [,name ,val])] (each [_ elt (ipairs [...])] - (let [elt (if (list? elt) elt (list elt))] + (let [elt (copy (if (list? elt) elt (list elt)))] (table.insert elt 2 name) (table.insert form elt))) (table.insert form name) @@ -4706,30 +4981,28 @@ do ,closer (close-handlers# (_G.xpcall ,bodyfn ,traceback))))) - (fn into-val [iter-tbl] - (var into nil) + (fn extract-into [iter-tbl] + (var (into iter-out found?) (values [] (copy iter-tbl))) (for [i (length iter-tbl) 2 -1] (if (= :into (. iter-tbl i)) - (do (assert (not into) "expected only one :into clause") - (set into (table.remove iter-tbl (+ i 1))) - (table.remove iter-tbl i)))) - (assert (or (not into) - (sym? into) - (table? into) - (list? into)) + (do (assert (not found?) "expected only one :into clause") + (set found? true) + (set into (. iter-tbl (+ i 1))) + (table.remove iter-out i) + (table.remove iter-out i)))) + (assert (or (not found?) (sym? into) (table? into) (list? into)) "expected table, function call, or symbol in :into clause") - (or into [])) + (values into iter-out)) (fn collect* [iter-tbl key-expr value-expr ...] - "Returns a table made by running an iterator and evaluating an expression that + "Return a table made by running an iterator and evaluating an expression that returns key-value pairs to be inserted sequentially into the table. This can - be thought of as a table comprehension. The body should provide two - expressions (used as key and value) or nil, which causes it to be omitted from - the resulting table. + be thought of as a table comprehension. The body should provide two expressions + (used as key and value) or nil, which causes it to be omitted. For example, (collect [k v (pairs {:apple \"red\" :orange \"orange\"})] - v k) + (values v k)) returns {:red \"apple\" :orange \"orange\"} @@ -4740,21 +5013,24 @@ do (assert (not= nil key-expr) "expected key and value expression") (assert (= nil ...) "expected 1 or 2 body expressions; wrap multiple expressions with do") - (let [kv-expr (if (= nil value-expr) key-expr `(values ,key-expr ,value-expr))] - `(let [tbl# ,(into-val iter-tbl)] - (each ,iter-tbl + (let [kv-expr (if (= nil value-expr) key-expr `(values ,key-expr ,value-expr)) + (into iter) (extract-into iter-tbl)] + `(let [tbl# ,into] + (each ,iter (match ,kv-expr (k# v#) (tset tbl# k# v#))) tbl#))) (fn icollect* [iter-tbl value-expr ...] - "Returns a sequential table made by running an iterator and evaluating an + "Return a sequential table made by running an iterator and evaluating an expression that returns values to be inserted sequentially into the table. - This can be thought of as a \"list comprehension\". If the body returns nil - that element is omitted from the resulting table. + This can be thought of as a table comprehension. If the body evaluates to nil + that element is omitted. For example, - (icollect [_ v (ipairs [1 2 3 4 5])] (when (not= v 3) (* v v))) + (icollect [_ v (ipairs [1 2 3 4 5])] + (when (not= v 3) + (* v v))) returns [1 4 16 25] @@ -4764,56 +5040,60 @@ do "expected iterator binding table") (assert (not= nil value-expr) "expected table value expression") (assert (= nil ...) - "expected exactly one body expression. Wrap multiple expressions with do") - `(let [tbl# ,(into-val iter-tbl)] - ;; believe it or not, using a var here has a pretty good performance boost: - ;; https://p.hagelb.org/icollect-performance.html - (var i# (length tbl#)) - (each ,iter-tbl - (let [val# ,value-expr] - (when (not= nil val#) - (set i# (+ i# 1)) - (tset tbl# i# val#)))) - tbl#)) + "expected exactly one body expression. Wrap multiple expressions in do") + (let [(into iter) (extract-into iter-tbl)] + `(let [tbl# ,into] + ;; believe it or not, using a var here has a pretty good performance + ;; boost: https://p.hagelb.org/icollect-performance.html + (var i# (length tbl#)) + (each ,iter + (let [val# ,value-expr] + (when (not= nil val#) + (set i# (+ i# 1)) + (tset tbl# i# val#)))) + tbl#))) - (fn accumulate* [iter-tbl accum-expr ...] + (fn accumulate* [iter-tbl body ...] "Accumulation macro. - It takes a binding table and an expression as its arguments. - In the binding table, the first symbol is bound to the second value, being an - initial accumulator variable. The rest are an iterator binding table in the - format `each` takes. + + It takes a binding table and an expression as its arguments. In the binding + table, the first form starts out bound to the second value, which is an initial + accumulator. The rest are an iterator binding table in the format `each` takes. + It runs through the iterator in each step of which the given expression is - evaluated, and its returned value updates the accumulator variable. - It eventually returns the final value of the accumulator variable. + evaluated, and the accumulator is set to the value of the expression. It + eventually returns the final value of the accumulator. For example, (accumulate [total 0 _ n (pairs {:apple 2 :orange 3})] (+ total n)) - returns - 5" + returns 5" (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 4)) "expected initial value and iterator binding table") - (assert (not= nil accum-expr) "expected accumulating expression") + (assert (not= nil body) "expected body expression") (assert (= nil ...) "expected exactly one body expression. Wrap multiple expressions with do") - (let [accum-var (table.remove iter-tbl 1) - accum-init (table.remove iter-tbl 1)] + (let [accum-var (. iter-tbl 1) + accum-init (. iter-tbl 2)] `(do (var ,accum-var ,accum-init) - (each ,iter-tbl - (set ,accum-var ,accum-expr)) - ,accum-var))) + (each ,[(unpack iter-tbl 3)] + (set ,accum-var ,body)) + ,(if (list? accum-var) + (list (sym :values) (unpack accum-var)) + accum-var)))) + + (fn double-eval-safe? [x type] + (or (= :number type) (= :string type) (= :boolean type) + (and (sym? x) (not (multi-sym? x))))) (fn partial* [f ...] - "Returns a function with all arguments partially applied to f." + "Return a function with all arguments partially applied to f." (assert f "expected a function to partially apply") (let [bindings [] args []] (each [_ arg (ipairs [...])] - (if (or (= :number (type arg)) - (= :string (type arg)) - (= :boolean (type arg)) - (= `nil arg)) + (if (double-eval-safe? arg (type arg)) (table.insert args arg) (let [name (gensym)] (table.insert bindings name) @@ -4821,12 +5101,14 @@ do (table.insert args name)))) (let [body (list f (unpack args))] (table.insert body _VARARG) - `(let ,bindings - (fn [,_VARARG] - ,body))))) + ;; only use the extra let if we need double-eval protection + (if (= 0 (length bindings)) + `(fn [,_VARARG] ,body) + `(let ,bindings + (fn [,_VARARG] ,body)))))) (fn pick-args* [n f] - "Creates a function of arity n that applies its arguments to f. + "Create a function of arity n that applies its arguments to f. For example, (pick-args 2 func) @@ -4844,7 +5126,7 @@ do (,f ,(unpack bindings))))) (fn pick-values* [n ...] - "Like the `values` special, but emits exactly n values. + "Evaluate to exactly n values. For example, (pick-values 2 ...) @@ -4908,7 +5190,7 @@ do `(,handle ,(view (macroexpand form _SCOPE))))) (fn import-macros* [binding1 module-name1 ...] - "Binds a table of macros from each macro module according to a binding form. + "Bind a table of macros from each macro module according to a binding form. Each binding form can be either a symbol or a k/v destructuring table. Example: (import-macros mymacros :my-macros ; bind to symbol @@ -4985,7 +5267,7 @@ do (values condition bindings))) (fn match-pattern [vals pattern unifications] - "Takes the AST of values and a single pattern and returns a condition + "Take the AST of values and a single pattern and returns a condition to determine if it matches as well as a list of bindings to introduce for the duration of the body if it does match." ;; we have to assume we're matching against multiple values here until we @@ -5056,8 +5338,6 @@ do ;; which simply generates old syntax and feeds it to `match*'. (let [clauses [...] vals (match-val-syms clauses)] - (assert (= 0 (math.fmod (length clauses) 2)) - "expected even number of pattern/body pairs") ;; protect against multiple evaluation of the value, bind against as ;; many values as we ever match against in the clauses. (list `let [vals val] (match-condition vals clauses)))) @@ -5117,18 +5397,50 @@ do pattern body (where pattern guard guards*) body (where (or pattern patterns*) guard guards*) body)" + (assert (= 0 (math.fmod (select :# ...) 2)) + "expected even number of pattern/body pairs") (let [conds-bodies (partition-2 [...]) - else-branch (if (not= 0 (% (select "#" ...) 2)) - (select (select "#" ...) ...)) match-body []] (each [_ [cond body] (ipairs conds-bodies)] (each [_ cond (ipairs (transform-cond cond))] (table.insert match-body cond) (table.insert match-body body))) - (if else-branch - (table.insert match-body else-branch)) (match* val (unpack match-body)))) + (fn match-try-step [expr else pattern body ...] + (if (= nil pattern body) + expr + ;; unlike regular match, we can't know how many values the value + ;; might evaluate to, so we have to capture them all in ... via IIFE + ;; to avoid double-evaluation. + `((fn [...] + (match ... + ,pattern ,(match-try-step body else ...) + ,(unpack else))) + ,expr))) + + (fn match-try* [expr pattern body ...] + "Perform chained pattern matching for a sequence of steps which might fail. + + The values from the initial expression are matched against the first pattern. + If they match, the first body is evaluated and its values are matched against + the second pattern, etc. + + If there is a (catch pat1 body1 pat2 body2 ...) form at the end, any mismatch + from the steps will be tried against these patterns in sequence as a fallback + just like a normal match. If there is no catch, the mismatched values will be + returned as the value of the entire expression." + (let [clauses [pattern body ...] + last (. clauses (length clauses)) + catch (if (= `catch (and (= :table (type last)) (. last 1))) + (let [[_ & e] (table.remove clauses)] e) ; remove `catch sym + [`_# `...])] + (assert (= 0 (math.fmod (length clauses) 2)) + "expected every pattern to have a body") + (assert (= 0 (math.fmod (length catch) 2)) + "expected every catch pattern to have a body") + (match-try-step expr catch (unpack clauses)))) + {:-> ->* :->> ->>* :-?> -?>* @@ -5147,21 +5459,22 @@ do :macro macro* :macrodebug macrodebug* :import-macros import-macros* - :match match-where} + :match match-where + :match-try match-try*} ]===] local module_name = "fennel.macros" local _ - local function _620_() + local function _682_() return mod end - package.preload[module_name] = _620_ + package.preload[module_name] = _682_ _ = nil local env do - local _621_ = specials["make-compiler-env"](nil, compiler.scopes.compiler, {}) - do end (_621_)["utils"] = utils - _621_["fennel"] = mod - env = _621_ + local _683_ = specials["make-compiler-env"](nil, compiler.scopes.compiler, {}) + do end (_683_)["utils"] = utils + _683_["fennel"] = mod + env = _683_ end local built_ins = eval(builtin_macros, {env = env, scope = compiler.scopes.compiler, allowedGlobals = false, useMetadata = true, filename = "src/fennel/macros.fnl", moduleName = module_name}) for k, v in pairs(built_ins) do