commit 8872d0f828277a06b7d3cad1c259bdb3b29635a4 Author: Jeremy Penner Date: Sat Dec 3 23:26:07 2022 -0500 Initial commit diff --git a/bencode.lua b/bencode.lua new file mode 100644 index 0000000..4676859 --- /dev/null +++ b/bencode.lua @@ -0,0 +1,96 @@ +-- Based on bencode.lua from the jeejah project by Phil Hagelberg +-- Distributed under the MIT license +-- https://gitlab.com/technomancy/jeejah/ + +local encode, decode + +local function decode_list(str, t, total_len) + -- print("list", str, lume.serialize(t)) + if #str == 0 then error("Incomplete") end + if(str:sub(1,1) == "e") then return t, total_len + 1 end + local value, v_len = decode(str) + table.insert(t, value) + total_len = total_len + v_len + return decode_list(str:sub(v_len + 1), t, total_len) +end + +local function decode_table(str, t, total_len) + -- print("table", str, lume.serialize(t)) + if #str == 0 then error("Incomplete") end + if(str:sub(1,1) == "e") then return t, total_len + 1 end + local key, k_len = decode(str) + local value, v_len = decode(str:sub(k_len+1)) + local end_pos = 1 + k_len + v_len + t[key] = value + total_len = total_len + k_len + v_len + return decode_table(str:sub(end_pos), t, total_len) +end + +function decode(str) + -- print("decoding", str) + if #str == 0 then + error("Incomplete") + elseif(str:sub(1,1) == "l") then + return decode_list(str:sub(2), {}, 1) + elseif(str:sub(1,1) == "d") then + return decode_table(str:sub(2), {}, 1) + elseif(str:sub(1,1) == "i") then + local iend = str:find("e") + if iend == nil then error("Incomplete") end + return(tonumber(str:sub(2, iend - 1))), iend + elseif(str:match("[0-9]+:")) then + local num_str = str:match("[0-9]+") + local beginning_of_string = #num_str + 2 + local str_len = tonumber(num_str) + local total_len = beginning_of_string + str_len - 1 + if #str < total_len then error("Incomplete") end + return str:sub(beginning_of_string, total_len), total_len + else + error("Could not parse "..str) + end +end + +local function encode_str(s) return #s .. ":" .. s end +local function encode_int(n) return "i" .. tostring(n) .. "e" end + +local function encode_table(t) + -- sort keys by encoded value as per bencode spec + -- https://www.bittorrent.org/beps/bep_0003.html#bencoding + -- we assume that sorting the concatenated key-value pairs will result in the same ordering as just the keys, since keys are unique + -- even if this is untrue in some corner cases, the most important thing for our purposes is that the same table always results in the same encoding + local encoded_kvs = {} + for k,v in pairs(t) do table.insert(encoded_kvs, encode(k) .. encode(v)) end + table.sort(encoded_kvs) + table.insert(encoded_kvs, 1, "d") + table.insert(encoded_kvs, "e") + return table.concat(encoded_kvs) +end + +local function encode_list(l) + local s = "l" + for _,x in ipairs(l) do s = s .. encode(x) end + return s .. "e" +end + +local function count(tbl) + local i = 0 + for _ in pairs(tbl) do i = i + 1 end + return i +end + +function encode(x) + local unpack = unpack or table.unpack + if(type(x) == "table" and select("#", unpack(x)) == count(x)) then + return encode_list(x) + elseif(type(x) == "table") then + return encode_table(x) + elseif(type(x) == "number" and math.floor(x) == x) then + return encode_int(x) + elseif(type(x) == "string") then + return encode_str(x) + else + error("Could not encode " .. type(x) .. ": " .. tostring(x)) + end +end + +return {decode=decode, encode=encode} diff --git a/debug.fnl b/debug.fnl new file mode 100644 index 0000000..88ae773 --- /dev/null +++ b/debug.fnl @@ -0,0 +1,38 @@ +(local core (require :core)) +(local style (require :core.style)) +(local util (require :plugins.fennel-xl.util)) +(local repl (require :plugins.fennel-xl.repl)) +(local ReplView (require :plugins.fennel-xl.replview)) +(local replsession (require :plugins.fennel-xl.replsession)) +(require :plugins.fennel-xl.localrepl) +(require :plugins.fennel-xl.nrepl-session) + +(local module (util.hot-table ...)) + +(fn find-existing-inspector-window [name] + (var result nil) + (each [_ view (ipairs (core.root_view.root_node:get_children)) :until result] + (when (= view.inspector-name name) + (set result view))) + result) + +(fn create-inspector-window [name ?value] + (let [node (core.root_view:get_active_node) + conn (repl.new name (if ?value :local nil)) + view (ReplView conn)] + (set view.inspector-name name) + (set view.title name) + (view:append {:draw (fn [_ _ x y] (when (and x y) (renderer.draw_text style.font name x y style.text) (+ (style.font:get_height) style.padding.y)))}) + (when ?value + (view:append (repl.mk-result [?value]))) + (node:split :right view) + view)) + +(lambda module.show [?value ?name] + (let [name (replsession.session-id ?name)] + (or (find-existing-inspector-window name) (create-inspector-window name ?value)))) + +(lambda module.submit [chunk ?name] + (: (module.show ?name) :submit chunk)) + +module.hot diff --git a/dkjson.lua b/dkjson.lua new file mode 100644 index 0000000..fa50b9f --- /dev/null +++ b/dkjson.lua @@ -0,0 +1,714 @@ +-- Module options: +local always_try_using_lpeg = true +local register_global_module_table = false +local global_module_name = 'json' + +--[==[ + +David Kolf's JSON module for Lua 5.1/5.2 + +Version 2.5 + + +For the documentation see the corresponding readme.txt or visit +. + +You can contact the author by sending an e-mail to 'david' at the +domain 'dkolf.de'. + + +Copyright (C) 2010-2013 David Heiko Kolf + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +--]==] + +-- global dependencies: +local pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset = + pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset +local error, require, pcall, select = error, require, pcall, select +local floor, huge = math.floor, math.huge +local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat = + string.rep, string.gsub, string.sub, string.byte, string.char, + string.find, string.len, string.format +local strmatch = string.match +local concat = table.concat + +local json = { version = "dkjson 2.5" } + +if register_global_module_table then + _G[global_module_name] = json +end + +local _ENV = nil -- blocking globals in Lua 5.2 + +pcall (function() + -- Enable access to blocked metatables. + -- Don't worry, this module doesn't change anything in them. + local debmeta = require "debug".getmetatable + if debmeta then getmetatable = debmeta end +end) + +json.null = setmetatable ({}, { + __tojson = function () return "null" end +}) + +local function isarray (tbl) + local max, n, arraylen = 0, 0, 0 + for k,v in pairs (tbl) do + if k == 'n' and type(v) == 'number' then + arraylen = v + if v > max then + max = v + end + else + if type(k) ~= 'number' or k < 1 or floor(k) ~= k then + return false + end + if k > max then + max = k + end + n = n + 1 + end + end + if max > 10 and max > arraylen and max > n * 2 then + return false -- don't create an array with too many holes + end + return true, max +end + +local escapecodes = { + ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f", + ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t" +} + +local function escapeutf8 (uchar) + local value = escapecodes[uchar] + if value then + return value + end + local a, b, c, d = strbyte (uchar, 1, 4) + a, b, c, d = a or 0, b or 0, c or 0, d or 0 + if a <= 0x7f then + value = a + elseif 0xc0 <= a and a <= 0xdf and b >= 0x80 then + value = (a - 0xc0) * 0x40 + b - 0x80 + elseif 0xe0 <= a and a <= 0xef and b >= 0x80 and c >= 0x80 then + value = ((a - 0xe0) * 0x40 + b - 0x80) * 0x40 + c - 0x80 + elseif 0xf0 <= a and a <= 0xf7 and b >= 0x80 and c >= 0x80 and d >= 0x80 then + value = (((a - 0xf0) * 0x40 + b - 0x80) * 0x40 + c - 0x80) * 0x40 + d - 0x80 + else + return "" + end + if value <= 0xffff then + return strformat ("\\u%.4x", value) + elseif value <= 0x10ffff then + -- encode as UTF-16 surrogate pair + value = value - 0x10000 + local highsur, lowsur = 0xD800 + floor (value/0x400), 0xDC00 + (value % 0x400) + return strformat ("\\u%.4x\\u%.4x", highsur, lowsur) + else + return "" + end +end + +local function fsub (str, pattern, repl) + -- gsub always builds a new string in a buffer, even when no match + -- exists. First using find should be more efficient when most strings + -- don't contain the pattern. + if strfind (str, pattern) then + return gsub (str, pattern, repl) + else + return str + end +end + +local function quotestring (value) + -- based on the regexp "escapable" in https://github.com/douglascrockford/JSON-js + value = fsub (value, "[%z\1-\31\"\\\127]", escapeutf8) + if strfind (value, "[\194\216\220\225\226\239]") then + value = fsub (value, "\194[\128-\159\173]", escapeutf8) + value = fsub (value, "\216[\128-\132]", escapeutf8) + value = fsub (value, "\220\143", escapeutf8) + value = fsub (value, "\225\158[\180\181]", escapeutf8) + value = fsub (value, "\226\128[\140-\143\168-\175]", escapeutf8) + value = fsub (value, "\226\129[\160-\175]", escapeutf8) + value = fsub (value, "\239\187\191", escapeutf8) + value = fsub (value, "\239\191[\176-\191]", escapeutf8) + end + return "\"" .. value .. "\"" +end +json.quotestring = quotestring + +local function replace(str, o, n) + local i, j = strfind (str, o, 1, true) + if i then + return strsub(str, 1, i-1) .. n .. strsub(str, j+1, -1) + else + return str + end +end + +-- locale independent num2str and str2num functions +local decpoint, numfilter + +local function updatedecpoint () + decpoint = strmatch(tostring(0.5), "([^05+])") + -- build a filter that can be used to remove group separators + numfilter = "[^0-9%-%+eE" .. gsub(decpoint, "[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%0") .. "]+" +end + +updatedecpoint() + +local function num2str (num) + return replace(fsub(tostring(num), numfilter, ""), decpoint, ".") +end + +local function str2num (str) + local num = tonumber(replace(str, ".", decpoint)) + if not num then + updatedecpoint() + num = tonumber(replace(str, ".", decpoint)) + end + return num +end + +local function addnewline2 (level, buffer, buflen) + buffer[buflen+1] = "\n" + buffer[buflen+2] = strrep (" ", level) + buflen = buflen + 2 + return buflen +end + +function json.addnewline (state) + if state.indent then + state.bufferlen = addnewline2 (state.level or 0, + state.buffer, state.bufferlen or #(state.buffer)) + end +end + +local encode2 -- forward declaration + +local function addpair (key, value, prev, indent, level, buffer, buflen, tables, globalorder, state) + local kt = type (key) + if kt ~= 'string' and kt ~= 'number' then + return nil, "type '" .. kt .. "' is not supported as a key by JSON." + end + if prev then + buflen = buflen + 1 + buffer[buflen] = "," + end + if indent then + buflen = addnewline2 (level, buffer, buflen) + end + buffer[buflen+1] = quotestring (key) + buffer[buflen+2] = ":" + return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder, state) +end + +local function appendcustom(res, buffer, state) + local buflen = state.bufferlen + if type (res) == 'string' then + buflen = buflen + 1 + buffer[buflen] = res + end + return buflen +end + +local function exception(reason, value, state, buffer, buflen, defaultmessage) + defaultmessage = defaultmessage or reason + local handler = state.exception + if not handler then + return nil, defaultmessage + else + state.bufferlen = buflen + local ret, msg = handler (reason, value, state, defaultmessage) + if not ret then return nil, msg or defaultmessage end + return appendcustom(ret, buffer, state) + end +end + +function json.encodeexception(reason, value, state, defaultmessage) + return quotestring("<" .. defaultmessage .. ">") +end + +encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, state) + local valtype = type (value) + local valmeta = getmetatable (value) + valmeta = type (valmeta) == 'table' and valmeta -- only tables + local valtojson = valmeta and valmeta.__tojson + if valtojson then + if tables[value] then + return exception('reference cycle', value, state, buffer, buflen) + end + tables[value] = true + state.bufferlen = buflen + local ret, msg = valtojson (value, state) + if not ret then return exception('custom encoder failed', value, state, buffer, buflen, msg) end + tables[value] = nil + buflen = appendcustom(ret, buffer, state) + elseif value == nil then + buflen = buflen + 1 + buffer[buflen] = "null" + elseif valtype == 'number' then + local s + if value ~= value or value >= huge or -value >= huge then + -- This is the behaviour of the original JSON implementation. + s = "null" + else + s = num2str (value) + end + buflen = buflen + 1 + buffer[buflen] = s + elseif valtype == 'boolean' then + buflen = buflen + 1 + buffer[buflen] = value and "true" or "false" + elseif valtype == 'string' then + buflen = buflen + 1 + buffer[buflen] = quotestring (value) + elseif valtype == 'table' then + if tables[value] then + return exception('reference cycle', value, state, buffer, buflen) + end + tables[value] = true + level = level + 1 + local isa, n = isarray (value) + if n == 0 and valmeta and valmeta.__jsontype == 'object' then + isa = false + end + local msg + if isa then -- JSON array + buflen = buflen + 1 + buffer[buflen] = "[" + for i = 1, n do + buflen, msg = encode2 (value[i], indent, level, buffer, buflen, tables, globalorder, state) + if not buflen then return nil, msg end + if i < n then + buflen = buflen + 1 + buffer[buflen] = "," + end + end + buflen = buflen + 1 + buffer[buflen] = "]" + else -- JSON object + local prev = false + buflen = buflen + 1 + buffer[buflen] = "{" + local order = valmeta and valmeta.__jsonorder or globalorder + if order then + local used = {} + n = #order + for i = 1, n do + local k = order[i] + local v = value[k] + if v then + used[k] = true + buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) + prev = true -- add a seperator before the next element + end + end + for k,v in pairs (value) do + if not used[k] then + buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) + if not buflen then return nil, msg end + prev = true -- add a seperator before the next element + end + end + else -- unordered + for k,v in pairs (value) do + buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) + if not buflen then return nil, msg end + prev = true -- add a seperator before the next element + end + end + if indent then + buflen = addnewline2 (level - 1, buffer, buflen) + end + buflen = buflen + 1 + buffer[buflen] = "}" + end + tables[value] = nil + else + return exception ('unsupported type', value, state, buffer, buflen, + "type '" .. valtype .. "' is not supported by JSON.") + end + return buflen +end + +function json.encode (value, state) + state = state or {} + local oldbuffer = state.buffer + local buffer = oldbuffer or {} + state.buffer = buffer + updatedecpoint() + local ret, msg = encode2 (value, state.indent, state.level or 0, + buffer, state.bufferlen or 0, state.tables or {}, state.keyorder, state) + if not ret then + error (msg, 2) + elseif oldbuffer == buffer then + state.bufferlen = ret + return true + else + state.bufferlen = nil + state.buffer = nil + return concat (buffer) + end +end + +local function loc (str, where) + local line, pos, linepos = 1, 1, 0 + while true do + pos = strfind (str, "\n", pos, true) + if pos and pos < where then + line = line + 1 + linepos = pos + pos = pos + 1 + else + break + end + end + return "line " .. line .. ", column " .. (where - linepos) +end + +local function unterminated (str, what, where) + return nil, strlen (str) + 1, "unterminated " .. what .. " at " .. loc (str, where) +end + +local function scanwhite (str, pos) + while true do + pos = strfind (str, "%S", pos) + if not pos then return nil end + local sub2 = strsub (str, pos, pos + 1) + if sub2 == "\239\187" and strsub (str, pos + 2, pos + 2) == "\191" then + -- UTF-8 Byte Order Mark + pos = pos + 3 + elseif sub2 == "//" then + pos = strfind (str, "[\n\r]", pos + 2) + if not pos then return nil end + elseif sub2 == "/*" then + pos = strfind (str, "*/", pos + 2) + if not pos then return nil end + pos = pos + 2 + else + return pos + end + end +end + +local escapechars = { + ["\""] = "\"", ["\\"] = "\\", ["/"] = "/", ["b"] = "\b", ["f"] = "\f", + ["n"] = "\n", ["r"] = "\r", ["t"] = "\t" +} + +local function unichar (value) + if value < 0 then + return nil + elseif value <= 0x007f then + return strchar (value) + elseif value <= 0x07ff then + return strchar (0xc0 + floor(value/0x40), + 0x80 + (floor(value) % 0x40)) + elseif value <= 0xffff then + return strchar (0xe0 + floor(value/0x1000), + 0x80 + (floor(value/0x40) % 0x40), + 0x80 + (floor(value) % 0x40)) + elseif value <= 0x10ffff then + return strchar (0xf0 + floor(value/0x40000), + 0x80 + (floor(value/0x1000) % 0x40), + 0x80 + (floor(value/0x40) % 0x40), + 0x80 + (floor(value) % 0x40)) + else + return nil + end +end + +local function scanstring (str, pos) + local lastpos = pos + 1 + local buffer, n = {}, 0 + while true do + local nextpos = strfind (str, "[\"\\]", lastpos) + if not nextpos then + return unterminated (str, "string", pos) + end + if nextpos > lastpos then + n = n + 1 + buffer[n] = strsub (str, lastpos, nextpos - 1) + end + if strsub (str, nextpos, nextpos) == "\"" then + lastpos = nextpos + 1 + break + else + local escchar = strsub (str, nextpos + 1, nextpos + 1) + local value + if escchar == "u" then + value = tonumber (strsub (str, nextpos + 2, nextpos + 5), 16) + if value then + local value2 + if 0xD800 <= value and value <= 0xDBff then + -- we have the high surrogate of UTF-16. Check if there is a + -- low surrogate escaped nearby to combine them. + if strsub (str, nextpos + 6, nextpos + 7) == "\\u" then + value2 = tonumber (strsub (str, nextpos + 8, nextpos + 11), 16) + if value2 and 0xDC00 <= value2 and value2 <= 0xDFFF then + value = (value - 0xD800) * 0x400 + (value2 - 0xDC00) + 0x10000 + else + value2 = nil -- in case it was out of range for a low surrogate + end + end + end + value = value and unichar (value) + if value then + if value2 then + lastpos = nextpos + 12 + else + lastpos = nextpos + 6 + end + end + end + end + if not value then + value = escapechars[escchar] or escchar + lastpos = nextpos + 2 + end + n = n + 1 + buffer[n] = value + end + end + if n == 1 then + return buffer[1], lastpos + elseif n > 1 then + return concat (buffer), lastpos + else + return "", lastpos + end +end + +local scanvalue -- forward declaration + +local function scantable (what, closechar, str, startpos, nullval, objectmeta, arraymeta) + local len = strlen (str) + local tbl, n = {}, 0 + local pos = startpos + 1 + if what == 'object' then + setmetatable (tbl, objectmeta) + else + setmetatable (tbl, arraymeta) + end + while true do + pos = scanwhite (str, pos) + if not pos then return unterminated (str, what, startpos) end + local char = strsub (str, pos, pos) + if char == closechar then + return tbl, pos + 1 + end + local val1, err + val1, pos, err = scanvalue (str, pos, nullval, objectmeta, arraymeta) + if err then return nil, pos, err end + pos = scanwhite (str, pos) + if not pos then return unterminated (str, what, startpos) end + char = strsub (str, pos, pos) + if char == ":" then + if val1 == nil then + return nil, pos, "cannot use nil as table index (at " .. loc (str, pos) .. ")" + end + pos = scanwhite (str, pos + 1) + if not pos then return unterminated (str, what, startpos) end + local val2 + val2, pos, err = scanvalue (str, pos, nullval, objectmeta, arraymeta) + if err then return nil, pos, err end + tbl[val1] = val2 + pos = scanwhite (str, pos) + if not pos then return unterminated (str, what, startpos) end + char = strsub (str, pos, pos) + else + n = n + 1 + tbl[n] = val1 + end + if char == "," then + pos = pos + 1 + end + end +end + +scanvalue = function (str, pos, nullval, objectmeta, arraymeta) + pos = pos or 1 + pos = scanwhite (str, pos) + if not pos then + return nil, strlen (str) + 1, "no valid JSON value (reached the end)" + end + local char = strsub (str, pos, pos) + if char == "{" then + return scantable ('object', "}", str, pos, nullval, objectmeta, arraymeta) + elseif char == "[" then + return scantable ('array', "]", str, pos, nullval, objectmeta, arraymeta) + elseif char == "\"" then + return scanstring (str, pos) + else + local pstart, pend = strfind (str, "^%-?[%d%.]+[eE]?[%+%-]?%d*", pos) + if pstart then + local number = str2num (strsub (str, pstart, pend)) + if number then + return number, pend + 1 + end + end + pstart, pend = strfind (str, "^%a%w*", pos) + if pstart then + local name = strsub (str, pstart, pend) + if name == "true" then + return true, pend + 1 + elseif name == "false" then + return false, pend + 1 + elseif name == "null" then + return nullval, pend + 1 + end + end + return nil, pos, "no valid JSON value at " .. loc (str, pos) + end +end + +local function optionalmetatables(...) + if select("#", ...) > 0 then + return ... + else + return {__jsontype = 'object'}, {__jsontype = 'array'} + end +end + +function json.decode (str, pos, nullval, ...) + local objectmeta, arraymeta = optionalmetatables(...) + return scanvalue (str, pos, nullval, objectmeta, arraymeta) +end + +function json.use_lpeg () + local g = require ("lpeg") + + if g.version() == "0.11" then + error "due to a bug in LPeg 0.11, it cannot be used for JSON matching" + end + + local pegmatch = g.match + local P, S, R = g.P, g.S, g.R + + local function ErrorCall (str, pos, msg, state) + if not state.msg then + state.msg = msg .. " at " .. loc (str, pos) + state.pos = pos + end + return false + end + + local function Err (msg) + return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall) + end + + local SingleLineComment = P"//" * (1 - S"\n\r")^0 + local MultiLineComment = P"/*" * (1 - P"*/")^0 * P"*/" + local Space = (S" \n\r\t" + P"\239\187\191" + SingleLineComment + MultiLineComment)^0 + + local PlainChar = 1 - S"\"\\\n\r" + local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars + local HexDigit = R("09", "af", "AF") + local function UTF16Surrogate (match, pos, high, low) + high, low = tonumber (high, 16), tonumber (low, 16) + if 0xD800 <= high and high <= 0xDBff and 0xDC00 <= low and low <= 0xDFFF then + return true, unichar ((high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000) + else + return false + end + end + local function UTF16BMP (hex) + return unichar (tonumber (hex, 16)) + end + local U16Sequence = (P"\\u" * g.C (HexDigit * HexDigit * HexDigit * HexDigit)) + local UnicodeEscape = g.Cmt (U16Sequence * U16Sequence, UTF16Surrogate) + U16Sequence/UTF16BMP + local Char = UnicodeEscape + EscapeSequence + PlainChar + local String = P"\"" * g.Cs (Char ^ 0) * (P"\"" + Err "unterminated string") + local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0)) + local Fractal = P"." * R"09"^0 + local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1 + local Number = (Integer * Fractal^(-1) * Exponent^(-1))/str2num + local Constant = P"true" * g.Cc (true) + P"false" * g.Cc (false) + P"null" * g.Carg (1) + local SimpleValue = Number + String + Constant + local ArrayContent, ObjectContent + + -- The functions parsearray and parseobject parse only a single value/pair + -- at a time and store them directly to avoid hitting the LPeg limits. + local function parsearray (str, pos, nullval, state) + local obj, cont + local npos + local t, nt = {}, 0 + repeat + obj, cont, npos = pegmatch (ArrayContent, str, pos, nullval, state) + if not npos then break end + pos = npos + nt = nt + 1 + t[nt] = obj + until cont == 'last' + return pos, setmetatable (t, state.arraymeta) + end + + local function parseobject (str, pos, nullval, state) + local obj, key, cont + local npos + local t = {} + repeat + key, obj, cont, npos = pegmatch (ObjectContent, str, pos, nullval, state) + if not npos then break end + pos = npos + t[key] = obj + until cont == 'last' + return pos, setmetatable (t, state.objectmeta) + end + + local Array = P"[" * g.Cmt (g.Carg(1) * g.Carg(2), parsearray) * Space * (P"]" + Err "']' expected") + local Object = P"{" * g.Cmt (g.Carg(1) * g.Carg(2), parseobject) * Space * (P"}" + Err "'}' expected") + local Value = Space * (Array + Object + SimpleValue) + local ExpectedValue = Value + Space * Err "value expected" + ArrayContent = Value * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp() + local Pair = g.Cg (Space * String * Space * (P":" + Err "colon expected") * ExpectedValue) + ObjectContent = Pair * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp() + local DecodeValue = ExpectedValue * g.Cp () + + function json.decode (str, pos, nullval, ...) + local state = {} + state.objectmeta, state.arraymeta = optionalmetatables(...) + local obj, retpos = pegmatch (DecodeValue, str, pos, nullval, state) + if state.msg then + return nil, state.pos, state.msg + else + return obj, retpos + end + end + + -- use this function only once: + json.use_lpeg = function () return json end + + json.using_lpeg = true + + return json -- so you can get the module using json = require "dkjson".use_lpeg() +end + +if always_try_using_lpeg then + pcall (json.use_lpeg) +end + +return json + diff --git a/editor.fnl b/editor.fnl new file mode 100644 index 0000000..1786d45 --- /dev/null +++ b/editor.fnl @@ -0,0 +1,130 @@ +(local lume (require :plugins.fennel-xl.lume)) +(local util (require :plugins.fennel-xl.util)) +(local core (require :core)) +(local command (require :core.command)) +(local keymap (require :core.keymap)) +(local common (require :core.common)) + +(fn place-to-line [selection] + (let [selection (lume.clone selection)] + (when (selection:place?) + (set selection.acol 1) + (set selection.bcol math.huge)) + selection)) + +(fn get-selection [] + (let [ldoc core.active_view.doc + (aline acol bline bcol) (ldoc:get_selection)] + {: ldoc : aline : acol : bline : bcol + :place? (fn [self] (and (= self.aline self.bline) (= self.acol self.bcol))) + :rtl? (fn [self] (or (and (= self.aline self.bline) (< self.bcol self.acol)) (< self.bline self.aline))) + :get-text (fn [self] (self.ldoc:get_text self.aline self.acol self.bline self.bcol)) + :replace-text (fn [self text] + (self.ldoc:set_selection self.aline self.acol self.bline self.bcol) + (self.ldoc:text_input text))})) + +(fn to-place [selection ?beginning] + (let [selection (lume.clone selection)] + (if (or (and ?beginning (not (selection:rtl?))) (and (not ?beginning) (selection:rtl?))) + (set (selection.bline selection.bcol) (values selection.aline selection.acol)) + (set (selection.aline selection.acol) (values selection.bline selection.bcol))) + selection)) + +(fn selected-form [] (: (place-to-line (get-selection)) :get-text)) + +(fn find-closest [s pattern i-target] + (var (start end) nil) + (set (start end) (s:find pattern)) + (while (and start (< end (- i-target 1))) + (set (start end) (s:find pattern (+ end 1)))) + (if (and start (<= start i-target)) (values start (+ end 1)) + (values i-target i-target))) + +(local symbol-pattern "[a-zA-Z%!%@%#%$%%%^%&%*%<%>%?%/%~%-%_%=%+][a-zA-Z%!%@%#%$%%%^%&%*%<%>%?%/%~%-%_%=%+0-9%.%:]*") +(fn place-to-symbol [selection] + (let [selection (lume.clone selection)] + (when (selection:place?) + (let [line (: (place-to-line selection) :get-text)] + (set (selection.acol selection.bcol) (find-closest line symbol-pattern selection.acol)))) + selection)) + +(fn selected-symbol [] (: (place-to-symbol (get-selection)) :get-text)) + +(fn inline-eval [eval] (: (to-place (get-selection)) :replace-text (eval (selected-form)))) + +(local {: show : submit} (util.require :plugins.fennel-xl.debug)) +(local replsession (require :plugins.fennel-xl.replsession)) +(local mm (util.require :plugins.fennel-xl.multimethod)) +(local nrepl (require :plugins.fennel-xl.nrepl)) + +(command.add :plugins.fennel-xl.replview { + "repl:submit" #(core.active_view:submit) + "repl:restart" #(replsession.restart core.active_view.conn.session) +}) + +(command.add nil { + "repl:create" #(show) + "repl:switch-connection" + #(core.command_view:enter "Connection Type" + #(when $2.text (set replsession.default-repl $2.text)) + #(mm.keys replsession.submit)) +}) + +(command.add #(nrepl:connected?) { + "nrepl:disconnect" #(nrepl:disconnect) +}) + +(command.add #(not (nrepl:connected?)) { + "nrepl:connect" #(nrepl:connect) +}) + +(fn go-to-definition [symbol] + (fn jump-to-find-result [result] + (when (not (match result + {:vals [loc]} + (let [(filename line) (when loc (loc:match "(.*):([0-9]+)")) + filename (or filename "") + filename (if (filename:find "^%.%.%.") "" (or (filename:match "^%./(.*)") filename)) + line (tonumber (or line 0)) + ldoc (when (> (length filename) 0) (core.open_doc filename))] + (when ldoc + (core.root_view:open_doc ldoc) + (ldoc:set_selection line 1 line 1) + true)))) + (core.log (.. "Unable to find symbol " symbol)))) + (: (replsession.session) :submit (.. ",find " symbol) jump-to-find-result) true) + +(fn replace-selected-symbol [text] (: (place-to-symbol (get-selection)) :replace-text text)) + +(fn autocomplete-results [text] + (var symbols []) + (: (replsession.session) :submit (.. ",complete " text) #(set symbols (or $1.vals [])) true) + (icollect [_ symbol (ipairs symbols)] + (let [item {:text symbol}] + (: (replsession.session) :submit (.. ",doc " symbol) #(when $1.vals (set item.info (. $1.vals 1)))) + item))) + +(fn autocomplete-symbol [callback ?starting-text] + (fn fixup-result [text item] (callback (or (and item item.text) text))) + (core.command_view:enter "Symbol" fixup-result autocomplete-results) + (print "start-text" ?starting-text) + (when ?starting-text + (core.command_view:set_text ?starting-text) + (core.command_view:update_suggestions))) + +(command.add :core.docview { + "fennel:eval" #(submit (selected-form)) + "fennel:go-to-definition-under-cursor" #(go-to-definition (selected-symbol)) + "fennel:go-to-definition" #(autocomplete-symbol #(go-to-definition $1) (selected-symbol)) + "fennel:autocomplete" #(autocomplete-symbol #(replace-selected-symbol $1) (selected-symbol)) +}) + +(keymap.add { + :return "repl:submit" + "alt+e" "fennel:eval" + "alt+d" "fennel:go-to-definition-under-cursor" + "ctrl+space" "fennel:autocomplete" +}) + +{: inline-eval : symbol-pattern} + diff --git a/fennel.lua b/fennel.lua new file mode 100644 index 0000000..bfb8bd0 --- /dev/null +++ b/fennel.lua @@ -0,0 +1,5486 @@ +package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) + local utils = require("fennel.utils") + local parser = require("fennel.parser") + local compiler = require("fennel.compiler") + local specials = require("fennel.specials") + local view = require("fennel.view") + local unpack = (table.unpack or _G.unpack) + local function default_read_chunk(parser_state) + local function _565_() + if (0 < parser_state["stack-size"]) then + return ".." + else + return ">> " + end + end + io.write(_565_()) + io.flush() + local input = io.read() + return (input and (input .. "\n")) + end + local function default_on_values(xs) + io.write(table.concat(xs, "\9")) + return io.write("\n") + end + local function default_on_error(errtype, err, lua_source) + local function _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 (_566_ == "Runtime") then + return (compiler.traceback(tostring(err), 4) .. "\n") + elseif true then + local _ = _566_ + return ("%s error: %s\n"):format(errtype, tostring(err)) + else + return nil + end + end + 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) + local spliced_source = {} + local bind = "local %s = ___replLocals___['%s']" + for line in lua_source:gmatch("([^\n]+)\n?") do + table.insert(spliced_source, line) + end + for name in pairs(env.___replLocals___) do + table.insert(spliced_source, 1, bind:format(name, name)) + end + if ((1 < #spliced_source) and (spliced_source[#spliced_source]):match("^ *return .*$")) then + table.insert(spliced_source, #spliced_source, save_source) + else + end + 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) + 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 + return tbl + 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 + 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 tbl_14_auto + end + local function descend(input, tbl, prefix, add_matches, method_3f) + local splitter + if method_3f then + splitter = "^([^:]+):(.*)" + else + splitter = "^([^.]+)%.(.*)" + end + local head, tail = input:match(splitter) + local raw_head = (scope.manglings[head] or head) + if (type(tbl[raw_head]) == "table") then + stop_looking_3f = true + if method_3f then + return add_partials(tail, tbl[raw_head], (prefix .. head .. ":")) + else + return add_matches(tail, tbl[raw_head], (prefix .. head)) + end + else + return nil + end + end + local function add_matches(input, tbl, prefix) + local prefix0 + if prefix then + prefix0 = (prefix .. ".") + else + prefix0 = "" + end + if (not input:find("%.") and input:find(":")) then + return descend(input, tbl, prefix0, add_matches, true) + elseif not input:find("%.") then + return add_partials(input, tbl, prefix0) + else + return descend(input, tbl, prefix0, add_matches, false) + end + end + for _, source in ipairs({scope.specials, scope.macros, (env.___replLocals___ or {}), env, env._G}) do + if stop_looking_3f then break end + add_matches(input_fragment, source) + end + return matches + end + local commands = {} + local function command_3f(input) + return input:match("^%s*,") + end + local function command_docs() + local _579_ + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for name, f in pairs(commands) do + local val_16_auto = (" ,%s - %s"):format(name, ((compiler.metadata):get(f, "fnl/docstring") or "undocumented")) + 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 + _579_ = tbl_14_auto + end + 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 _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 + local ok, new = pcall(require, module_name) + local new0 + if not ok then + on_values({new}) + new0 = old + else + new0 = new + end + specials["macro-loaded"][module_name] = nil + if ((type(old) == "table") and (type(new0) == "table")) then + for k, v in pairs(new0) do + old[k] = v + end + for k in pairs(old) do + if (nil == (new0)[k]) then + old[k] = nil + else + end + end + package.loaded[module_name] = old + else + end + return on_values({"ok"}) + 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 + else + return nil + end + end + local function run_command(read, on_error, f) + local _590_, _591_, _592_ = pcall(read) + if ((_590_ == true) and (_591_ == true) and (nil ~= _592_)) then + local val = _592_ + return f(val) + 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 _594_(_241) + return reload(tostring(_241), env, on_values, on_error) + end + 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) + env.___replLocals___ = {} + return on_values({"ok"}) + end + do end (compiler.metadata):set(commands.reset, "fnl/docstring", "Erase all repl-local scope.") + commands.complete = function(env, read, on_values, on_error, scope, chars) + local function _595_() + return on_values(completer(env, scope, string.char(unpack(chars)):gsub(",complete +", ""):sub(1, -2))) + end + 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 _596_ = type(subtbl) + if (_596_ == "function") then + if ((prefix .. name)):match(pattern) then + table.insert(names, (prefix .. name)) + else + end + elseif (_596_ == "table") then + if not seen[subtbl] then + local _599_ + do + local _598_ = seen + _598_[subtbl] = true + _599_ = _598_ + end + apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _599_, names) + else + end + else + end + else + end + end + return names + end + local function apropos(pattern) + local names = apropos_2a(pattern, package.loaded, "", {}, {}) + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for _, name in ipairs(names) do + local val_16_auto = name:gsub("^_G%.", "") + 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 tbl_14_auto + end + commands.apropos = function(_env, read, on_values, on_error, _scope) + local function _604_(_241) + return on_values(apropos(tostring(_241))) + end + 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) + local paths + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for p in path:gmatch("[^%.]+") do + local val_16_auto = p + 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 + paths = tbl_14_auto + end + local tgt = package.loaded + for _, path0 in ipairs(paths) do + if (nil == tgt) then break end + local _607_ + do + local _606_ = path0:gsub("%/", ".") + _607_ = _606_ + end + tgt = tgt[_607_] + end + return tgt + end + local function apropos_doc(pattern) + local names = {} + for _, path in ipairs(apropos(".*")) do + local tgt = apropos_follow_path(path) + if ("function" == type(tgt)) then + 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 + end + else + end + else + end + end + return names + end + commands["apropos-doc"] = function(_env, read, on_values, on_error, _scope) + local function _612_(_241) + return on_values(apropos_doc(tostring(_241))) + end + 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) + for _, path in ipairs(apropos(pattern)) do + local tgt = apropos_follow_path(path) + if (("function" == type(tgt)) and (compiler.metadata):get(tgt, "fnl/docstring")) then + on_values(specials.doc(tgt, path)) + on_values() + else + end + end + return nil + end + commands["apropos-show-docs"] = function(_env, read, on_values, on_error) + local function _614_(_241) + return apropos_show_docs(on_values, tostring(_241)) + end + 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, _615_, scope) + local _arg_616_ = _615_ + local ___replLocals___ = _arg_616_["___replLocals___"] + local env = _arg_616_ + local e + local function _617_(_241, _242) + return (___replLocals___[_242] or env[_242]) + end + 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 _624_(_241) + local _625_ + do + local _626_ = utils["sym?"](_241) + if (nil ~= _626_) then + local _627_ = resolve(_626_, env, scope) + if (nil ~= _627_) then + _625_ = debug.getinfo(_627_) + else + _625_ = _627_ + end + else + _625_ = _626_ + end + end + 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_630_ = compiler.sourcemap + if (nil ~= t_630_) then + t_630_ = (t_630_)[source] + else + end + if (nil ~= t_630_) then + t_630_ = (t_630_)[line] + else + end + if (nil ~= t_630_) then + t_630_ = (t_630_)[2] + else + end + fnlsrc = t_630_ + end + return on_values({string.format("%s:%s", src, (fnlsrc or line))}) + elseif (_625_ == nil) then + return on_error("Repl", "Unknown value") + elseif true then + local _ = _625_ + return on_error("Repl", "No source info") + else + return nil + end + end + 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 _635_(_241) + local name = tostring(_241) + 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, _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 _638_ = name:match("^repl%-command%-(.*)") + if (nil ~= _638_) then + local cmd_name = _638_ + commands[cmd_name] = (commands[cmd_name] or f) + else + end + end + end + return nil + end + local function run_command_loop(input, read, loop, env, on_values, on_error, scope, chars) + local command_name = input:match(",([^%s/]+)") + do + local _640_ = commands[command_name] + if (nil ~= _640_) then + local command = _640_ + command(env, read, on_values, on_error, scope, chars) + elseif true then + local _ = _640_ + if ("exit" ~= command_name) then + on_values({"Unknown command", command_name}) + else + end + else + end + end + if ("exit" ~= command_name) then + return loop() + else + return nil + end + end + 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 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) + local pp = (opts.pp or view) + local byte_stream, clear_stream = parser.granulate(read_chunk) + local chars = {} + local read, reset = nil, nil + local function _649_(parser_state) + local c = byte_stream(parser_state) + table.insert(chars, c) + return c + end + read, reset = parser.parser(_649_) + opts.env, opts.scope = env, compiler["make-scope"]() + opts.useMetadata = (opts.useMetadata ~= false) + if (opts.allowedGlobals == nil) then + opts.allowedGlobals = specials["current-global-names"](env) + else + end + if opts.registerCompleter then + local function _653_() + local _651_ = env + local _652_ = opts.scope + local function _654_(...) + return completer(_651_, _652_, ...) + end + return _654_ + end + opts.registerCompleter(_653_()) + else + end + load_plugin_commands(opts.plugins) + if save_locals_3f then + local function newindex(t, k, v) + if opts.scope.unmanglings[k] then + return rawset(t, k, v) + else + return nil + end + end + env.___replLocals___ = setmetatable({}, {__newindex = newindex}) + else + end + local function print_values(...) + local vals = {...} + local out = {} + env._, env.__ = vals[1], vals + for i = 1, select("#", ...) do + table.insert(out, pp(vals[i])) + end + return on_values(out) + end + local function loop() + for k in pairs(chars) do + chars[k] = nil + end + reset() + local ok, not_eof_3f, x = pcall(read) + local src_string = string.char(unpack(chars)) + if not ok then + 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 not_eof_3f then + do + local _658_, _659_ = nil, nil + local function _661_() + local _660_ = opts + _660_["source"] = src_string + return _660_ + end + _658_, _659_ = pcall(compiler.compile, x, _661_()) + if ((_658_ == false) and (nil ~= _659_)) then + local msg = _659_ + clear_stream() + on_error("Compile", msg) + 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 _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 ~= _664_)) then + local _ = _663_ + local chunk = _664_ + local function _665_() + return print_values(chunk()) + end + local function _666_() + local function _667_(...) + return on_error("Runtime", ...) + end + return _667_ + end + xpcall(_665_, _666_()) + else + end + else + end + end + utils.root.options = old_root_options + return loop() + else + return nil + end + end + end + loop() + if readline then + return readline.save_history() + else + return nil + end + end + return repl +end +package.preload["fennel.specials"] = package.preload["fennel.specials"] or function(...) + local utils = require("fennel.utils") + local view = require("fennel.view") + local parser = require("fennel.parser") + local compiler = require("fennel.compiler") + local unpack = (table.unpack or _G.unpack) + local SPECIALS = compiler.scopes.global.specials + local function wrap_env(env) + local function _364_(_, key) + if utils["string?"](key) then + return env[compiler["global-unmangling"](key)] + else + return env[key] + end + end + local function _366_(_, key, value) + if utils["string?"](key) then + env[compiler["global-unmangling"](key)] = value + return nil + else + env[key] = value + return nil + end + end + local function _368_() + local function putenv(k, v) + local _369_ + if utils["string?"](k) then + _369_ = compiler["global-unmangling"](k) + else + _369_ = k + end + return _369_, v + end + return next, utils.kvmap(env, putenv), nil + end + return setmetatable({}, {__index = _364_, __newindex = _366_, __pairs = _368_}) + end + local function current_global_names(_3fenv) + local mt + do + 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 _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 (_371_ == nil) then + mt = (_3fenv or _G) + else + mt = nil + end + end + return (mt and utils.kvmap(mt, compiler["global-unmangling"])) + end + local function load_code(code, _3fenv, _3ffilename) + local env = (_3fenv or rawget(_G, "_ENV") or _G) + if (rawget(_G, "setfenv") and rawget(_G, "loadstring")) then + local f = assert(_G.loadstring(code, _3ffilename)) + local _376_ = f + setfenv(_376_, env) + return _376_ + else + return assert(load(code, _3ffilename, "t", env)) + end + end + local function doc_2a(tgt, name) + if not tgt then + return (name .. " not found") + else + local docstring = (((compiler.metadata):get(tgt, "fnl/docstring") or "#")):gsub("\n$", ""):gsub("\n", "\n ") + local mt = getmetatable(tgt) + if ((type(tgt) == "function") or ((type(mt) == "table") and (type(mt.__call) == "function"))) then + local arglist = table.concat(((compiler.metadata):get(tgt, "fnl/arglist") or {"#"}), " ") + local _378_ + if (#arglist > 0) then + _378_ = " " + else + _378_ = "" + end + return string.format("(%s%s%s)\n %s", name, _378_, arglist, docstring) + else + return string.format("%s\n %s", name, docstring) + end + end + end + local function doc_special(name, arglist, docstring, body_form_3f) + compiler.metadata[SPECIALS[name]] = {["fnl/arglist"] = arglist, ["fnl/docstring"] = docstring, ["fnl/body-form?"] = body_form_3f} + return nil + end + local function compile_do(ast, scope, parent, _3fstart) + local start = (_3fstart or 2) + local len = #ast + local sub_scope = compiler["make-scope"](scope) + for i = start, len do + compiler.compile1(ast[i], sub_scope, parent, {nval = 0}) + end + return nil + end + SPECIALS["do"] = function(ast, scope, parent, opts, _3fstart, _3fchunk, _3fsub_scope, _3fpre_syms) + local start = (_3fstart or 2) + local sub_scope = (_3fsub_scope or compiler["make-scope"](scope)) + local chunk = (_3fchunk or {}) + local len = #ast + local retexprs = {returned = true} + local function compile_body(outer_target, outer_tail, outer_retexprs) + if (len < start) then + compiler.compile1(nil, sub_scope, chunk, {tail = outer_tail, target = outer_target}) + else + for i = start, len do + local subopts = {nval = (((i ~= len) and 0) or opts.nval), tail = (((i == len) and outer_tail) or nil), target = (((i == len) and outer_target) or nil)} + local _ = utils["propagate-options"](opts, subopts) + local subexprs = compiler.compile1(ast[i], sub_scope, chunk, subopts) + if (i ~= len) then + compiler["keep-side-effects"](subexprs, parent, nil, ast[i]) + else + end + end + end + compiler.emit(parent, chunk, ast) + compiler.emit(parent, "end", ast) + utils.hook("do", ast, sub_scope) + return (outer_retexprs or retexprs) + end + if (opts.target or (opts.nval == 0) or opts.tail) then + compiler.emit(parent, "do", ast) + return compile_body(opts.target, opts.tail) + elseif opts.nval then + local syms = {} + for i = 1, opts.nval do + local s = ((_3fpre_syms and (_3fpre_syms)[i]) or compiler.gensym(scope)) + do end (syms)[i] = s + retexprs[i] = utils.expr(s, "sym") + end + local outer_target = table.concat(syms, ", ") + compiler.emit(parent, string.format("local %s", outer_target), ast) + compiler.emit(parent, "do", ast) + return compile_body(outer_target, opts.tail) + else + local fname = compiler.gensym(scope) + local fargs + if scope.vararg then + fargs = "..." + else + fargs = "" + end + compiler.emit(parent, string.format("local function %s(%s)", fname, fargs), ast) + return compile_body(nil, true, utils.expr((fname .. "(" .. fargs .. ")"), "statement")) + end + end + doc_special("do", {"..."}, "Evaluate multiple forms; return last value.", true) + SPECIALS.values = function(ast, scope, parent) + local len = #ast + local exprs = {} + for i = 2, len do + local subexprs = compiler.compile1(ast[i], scope, parent, {nval = ((i ~= len) and 1)}) + table.insert(exprs, subexprs[1]) + if (i == len) then + for j = 2, #subexprs do + table.insert(exprs, subexprs[j]) + end + else + end + end + return exprs + end + 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 _387_ + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for _, v in ipairs(x) do + local val_16_auto = deep_tostring(v) + 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 + _387_ = tbl_14_auto + end + return ("[" .. table.concat(_387_, " ") .. "]") + elseif utils["table?"](x) then + local _389_ + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for k, v in pairs(x) do + local val_16_auto = (deep_tostring(k, true) .. " " .. deep_tostring(v)) + 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 + _389_ = tbl_14_auto + end + return ("{" .. table.concat(_389_, " ") .. "}") + elseif (key_3f and utils["string?"](x) and x:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then + return (":" .. x) + elseif utils["string?"](x) then + return string.format("%q", x):gsub("\\\"", "\\\\\""):gsub("\"", "\\\"") + else + return tostring(x) + end + end + local function set_fn_metadata(arg_list, docstring, parent, fn_name) + if utils.root.options.useMetadata then + local args + local function _392_(_241) + return ("\"%s\""):format(deep_tostring(_241)) + end + args = utils.map(arg_list, _392_) + local meta_fields = {"\"fnl/arglist\"", ("{" .. table.concat(args, ", ") .. "}")} + if docstring then + table.insert(meta_fields, "\"fnl/docstring\"") + table.insert(meta_fields, ("\"" .. docstring:gsub("%s+$", ""):gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\"", "\\\"") .. "\"")) + else + end + local meta_str = ("require(\"%s\").metadata"):format((utils.root.options.moduleName or "fennel")) + return compiler.emit(parent, ("pcall(function() %s:setall(%s, %s) end)"):format(meta_str, fn_name, table.concat(meta_fields, ", "))) + else + return nil + end + end + local function get_fn_name(ast, scope, fn_name, multi) + if (fn_name and (fn_name[1] ~= "nil")) then + local _395_ + if not multi then + _395_ = compiler["declare-local"](fn_name, {}, scope, ast) + else + _395_ = (compiler["symbol-to-expression"](fn_name, scope))[1] + end + 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, 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 _398_ + if local_3f then + _398_ = "local function %s(%s)" + else + _398_ = "%s = function(%s)" + end + 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(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, f_metadata, scope) + local fn_name = compiler.gensym(scope) + return compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, true, arg_name_list, f_metadata) + end + local function 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 _407_ = compiler["make-scope"](scope) + do end (_407_)["vararg"] = false + f_scope = _407_ + end + local f_chunk = {} + local fn_sym = utils["sym?"](ast[2]) + local multi = (fn_sym and utils["multi-sym?"](fn_sym[1])) + local fn_name, local_3f, index = get_fn_name(ast, scope, fn_sym, multi) + local arg_list = compiler.assert(utils["table?"](ast[index]), "expected parameters table", ast) + compiler.assert((not multi or not multi["multi-sym-method-call"]), ("unexpected multi symbol " .. tostring(fn_name)), fn_sym) + local function get_arg_name(arg) + if utils["varg?"](arg) then + compiler.assert((arg == arg_list[#arg_list]), "expected vararg as last parameter", ast) + f_scope.vararg = true + return "..." + elseif (utils["sym?"](arg) and (tostring(arg) ~= "nil") and not utils["multi-sym?"](tostring(arg))) then + return compiler["declare-local"](arg, {}, f_scope, ast) + elseif utils["table?"](arg) then + local raw = utils.sym(compiler.gensym(scope)) + local declared = compiler["declare-local"](raw, {}, f_scope, ast) + compiler.destructure(arg, raw, ast, f_scope, f_chunk, {declaration = true, nomulti = true, symtype = "arg"}) + return declared + else + return compiler.assert(false, ("expected symbol for function parameter: %s"):format(tostring(arg)), ast[index]) + end + end + local arg_name_list = utils.map(arg_list, get_arg_name) + local f_metadata, index0 = get_function_metadata(ast, arg_list, index) + if fn_name then + return compile_named_fn(ast, f_scope, f_chunk, parent, index0, fn_name, local_3f, arg_name_list, f_metadata) + else + return compile_anonymous_fn(ast, f_scope, f_chunk, parent, index0, arg_name_list, f_metadata, scope) + end + end + doc_special("fn", {"name?", "args", "docstring?", "..."}, "Function syntax. May optionally include a name and docstring or a metadata table.\nIf a name is provided, the function will be bound in the current scope.\nWhen called with the wrong number of args, excess args will be discarded\nand lacking args will be nil, use lambda for arity-checked functions.", true) + SPECIALS.lua = function(ast, _, parent) + compiler.assert(((#ast == 2) or (#ast == 3)), "expected 1 or 2 arguments", ast) + local _411_ + do + local _410_ = utils["sym?"](ast[2]) + if (nil ~= _410_) then + _411_ = tostring(_410_) + else + _411_ = _410_ + end + end + if ("nil" ~= _411_) then + table.insert(parent, {ast = ast, leaf = tostring(ast[2])}) + else + end + local _415_ + do + local _414_ = utils["sym?"](ast[3]) + if (nil ~= _414_) then + _415_ = tostring(_414_) + else + _415_ = _414_ + end + end + if ("nil" ~= _415_) then + return tostring(ast[3]) + else + return nil + end + end + local function dot(ast, scope, parent) + compiler.assert((1 < #ast), "expected table argument", ast) + local len = #ast + 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 (utils["string?"](index) and utils["valid-lua-identifier?"](index)) then + table.insert(indices, ("." .. index)) + else + local _let_419_ = compiler.compile1(index, scope, parent, {nval = 1}) + local index0 = _let_419_[1] + table.insert(indices, ("[" .. tostring(index0) .. "]")) + end + end + if (tostring(lhs):find("[{\"0-9]") or ("nil" == tostring(lhs))) then + return ("(" .. tostring(lhs) .. ")" .. table.concat(indices)) + else + return (tostring(lhs) .. table.concat(indices)) + end + end + end + SPECIALS["."] = dot + doc_special(".", {"tbl", "key1", "..."}, "Look up key1 in tbl table. If more args are provided, do a nested lookup.") + SPECIALS.global = function(ast, scope, parent) + compiler.assert((#ast == 3), "expected name and value", ast) + compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceglobal = true, nomulti = true, symtype = "global"}) + return nil + end + doc_special("global", {"name", "val"}, "Set name as a global with val.") + SPECIALS.set = function(ast, scope, parent) + compiler.assert((#ast == 3), "expected name and value", ast) + compiler.destructure(ast[2], ast[3], ast, scope, parent, {noundef = true, symtype = "set"}) + return nil + end + doc_special("set", {"name", "val"}, "Set a local variable to a new value. Only works on locals using var.") + local function set_forcibly_21_2a(ast, scope, parent) + compiler.assert((#ast == 3), "expected name and value", ast) + compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceset = true, symtype = "set"}) + return nil + end + SPECIALS["set-forcibly!"] = set_forcibly_21_2a + local function local_2a(ast, scope, parent) + compiler.assert((#ast == 3), "expected name and value", ast) + compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, nomulti = true, symtype = "local"}) + return nil + end + SPECIALS["local"] = local_2a + doc_special("local", {"name", "val"}, "Introduce new top-level immutable local.") + SPECIALS.var = function(ast, scope, parent) + compiler.assert((#ast == 3), "expected name and value", ast) + compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, isvar = true, nomulti = true, symtype = "var"}) + return nil + end + doc_special("var", {"name", "val"}, "Introduce new mutable local.") + local function kv_3f(t) + local _423_ + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for k in pairs(t) do + local val_16_auto + if not ("number" == type(k)) then + val_16_auto = k + else + val_16_auto = nil + 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 + _423_ = tbl_14_auto + end + return (_423_)[1] + end + SPECIALS.let = function(ast, scope, parent, opts) + local bindings = ast[2] + local pre_syms = {} + compiler.assert((utils["table?"](bindings) and not kv_3f(bindings)), "expected binding sequence", bindings) + compiler.assert(((#bindings % 2) == 0), "expected even number of name/value bindings", ast[2]) + compiler.assert((#ast >= 3), "expected body expression", ast[1]) + for _ = 1, (opts.nval or 0) do + table.insert(pre_syms, compiler.gensym(scope)) + end + local sub_scope = compiler["make-scope"](scope) + local sub_chunk = {} + for i = 1, #bindings, 2 do + compiler.destructure(bindings[i], bindings[(i + 1)], ast, sub_scope, sub_chunk, {declaration = true, nomulti = true, symtype = "let"}) + end + return SPECIALS["do"](ast, scope, parent, opts, 3, sub_chunk, sub_scope, pre_syms) + end + doc_special("let", {"[name1 val1 ... nameN valN]", "..."}, "Introduces a new scope in which a given set of local bindings are used.", true) + local function get_prev_line(parent) + if ("table" == type(parent)) then + return get_prev_line((parent.leaf or parent[#parent])) + else + return (parent or "") + end + end + local function disambiguate_3f(rootstr, parent) + local function _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 _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_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] + local rootstr = tostring(root) + local fmtstr + if disambiguate_3f(rootstr, parent) then + fmtstr = "do end (%s)[%s] = %s" + else + fmtstr = "%s[%s] = %s" + end + return compiler.emit(parent, fmtstr:format(rootstr, table.concat(keys, "]["), tostring(value)), ast) + end + doc_special("tset", {"tbl", "key1", "...", "keyN", "val"}, "Set the value of a table field. Can take additional keys to set\nnested values, but all parents must contain an existing table.") + local function calculate_target(scope, opts) + if not (opts.tail or opts.target or opts.nval) then + return "iife", true, nil + elseif (opts.nval and (opts.nval ~= 0) and not opts.target) then + local accum = {} + local target_exprs = {} + for i = 1, opts.nval do + local s = compiler.gensym(scope) + do end (accum)[i] = s + target_exprs[i] = utils.expr(s, "sym") + end + return "target", opts.tail, table.concat(accum, ", "), target_exprs + else + return "none", opts.tail, opts.target + end + end + local function if_2a(ast, scope, parent, opts) + compiler.assert((2 < #ast), "expected condition and body", ast) + local do_scope = compiler["make-scope"](scope) + local branches = {} + local wrapper, inner_tail, inner_target, target_exprs = calculate_target(scope, opts) + local body_opts = {nval = opts.nval, tail = inner_tail, target = inner_target} + local function compile_body(i) + local chunk = {} + local cscope = compiler["make-scope"](do_scope) + compiler["keep-side-effects"](compiler.compile1(ast[i], cscope, chunk, body_opts), chunk, nil, ast[i]) + return {chunk = chunk, scope = cscope} + end + if (1 == (#ast % 2)) then + table.insert(ast, utils.sym("nil")) + else + end + for i = 2, (#ast - 1), 2 do + local condchunk = {} + local res = compiler.compile1(ast[i], do_scope, condchunk, {nval = 1}) + local cond = res[1] + local branch = compile_body((i + 1)) + branch.cond = cond + branch.condchunk = condchunk + branch.nested = ((i ~= 2) and (next(condchunk, nil) == nil)) + table.insert(branches, branch) + end + local else_branch = compile_body(#ast) + local s = compiler.gensym(scope) + local buffer = {} + local last_buffer = buffer + for i = 1, #branches do + local branch = branches[i] + local fstr + if not branch.nested then + fstr = "if %s then" + else + fstr = "elseif %s then" + end + local cond = tostring(branch.cond) + local cond_line = fstr:format(cond) + if branch.nested then + compiler.emit(last_buffer, branch.condchunk, ast) + else + for _, v in ipairs(branch.condchunk) do + compiler.emit(last_buffer, v, ast) + end + end + compiler.emit(last_buffer, cond_line, ast) + compiler.emit(last_buffer, branch.chunk, ast) + if (i == #branches) then + compiler.emit(last_buffer, "else", ast) + compiler.emit(last_buffer, else_branch.chunk, ast) + compiler.emit(last_buffer, "end", ast) + elseif not (branches[(i + 1)]).nested then + local next_buffer = {} + compiler.emit(last_buffer, "else", ast) + compiler.emit(last_buffer, next_buffer, ast) + compiler.emit(last_buffer, "end", ast) + last_buffer = next_buffer + else + end + end + if (wrapper == "iife") then + local iifeargs = ((scope.vararg and "...") or "") + compiler.emit(parent, ("local function %s(%s)"):format(tostring(s), iifeargs), ast) + compiler.emit(parent, buffer, ast) + compiler.emit(parent, "end", ast) + return utils.expr(("%s(%s)"):format(tostring(s), iifeargs), "statement") + elseif (wrapper == "none") then + for i = 1, #buffer do + compiler.emit(parent, buffer[i], ast) + end + return {returned = true} + else + compiler.emit(parent, ("local %s"):format(inner_target), ast) + for i = 1, #buffer do + compiler.emit(parent, buffer[i], ast) + end + return target_exprs + end + end + SPECIALS["if"] = if_2a + doc_special("if", {"cond1", "body1", "...", "condN", "bodyN"}, "Conditional form.\nTakes any number of condition/body pairs and evaluates the first body where\nthe condition evaluates to truthy. Similar to cond in other lisps.") + local function remove_until_condition(bindings) + if ("until" == bindings[(#bindings - 1)]) then + table.remove(bindings, (#bindings - 1)) + return table.remove(bindings) + else + return nil + end + end + local function compile_until(condition, scope, chunk) + if condition then + 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 + end + end + SPECIALS.each = function(ast, scope, parent) + compiler.assert((#ast >= 3), "expected body expression", ast[1]) + local binding = compiler.assert(utils["table?"](ast[2]), "expected binding table", ast) + local _ = compiler.assert((2 <= #binding), "expected binding and iterator", binding) + local until_condition = remove_until_condition(binding) + local iter = table.remove(binding, #binding) + local destructures = {} + local new_manglings = {} + local sub_scope = compiler["make-scope"](scope) + local function destructure_binding(v) + compiler.assert(not utils["string?"](v), ("unexpected iterator clause " .. tostring(v)), binding) + if utils["sym?"](v) then + return compiler["declare-local"](v, {}, sub_scope, ast, new_manglings) + else + local raw = utils.sym(compiler.gensym(sub_scope)) + do end (destructures)[raw] = v + return compiler["declare-local"](raw, {}, sub_scope, ast) + end + end + local bind_vars = utils.map(binding, destructure_binding) + local vals = compiler.compile1(iter, scope, parent) + local val_names = utils.map(vals, tostring) + local chunk = {} + compiler.emit(parent, ("for %s in %s do"):format(table.concat(bind_vars, ", "), table.concat(val_names, ", ")), ast) + for raw, args in utils.stablepairs(destructures) do + compiler.destructure(args, raw, ast, sub_scope, chunk, {declaration = true, nomulti = true, symtype = "each"}) + end + compiler["apply-manglings"](sub_scope, new_manglings, ast) + compile_until(until_condition, sub_scope, chunk) + compile_do(ast, sub_scope, chunk, 3) + compiler.emit(parent, chunk, ast) + return compiler.emit(parent, "end", ast) + end + doc_special("each", {"[key value (iterator)]", "..."}, "Runs the body once for each set of values provided by the given iterator.\nMost commonly used with ipairs for sequential tables or pairs for undefined\norder, but can be used with any iterator.", true) + local function while_2a(ast, scope, parent) + local len1 = #parent + local condition = (compiler.compile1(ast[2], scope, parent, {nval = 1}))[1] + local len2 = #parent + local sub_chunk = {} + if (len1 ~= len2) then + for i = (len1 + 1), len2 do + table.insert(sub_chunk, parent[i]) + do end (parent)[i] = nil + end + compiler.emit(parent, "while true do", ast) + compiler.emit(sub_chunk, ("if not %s then break end"):format(condition[1]), ast) + else + compiler.emit(parent, ("while " .. tostring(condition) .. " do"), ast) + end + compile_do(ast, compiler["make-scope"](scope), sub_chunk, 3) + compiler.emit(parent, sub_chunk, ast) + return compiler.emit(parent, "end", ast) + end + SPECIALS["while"] = while_2a + doc_special("while", {"condition", "..."}, "The classic while loop. Evaluates body until a condition is non-truthy.", true) + local function for_2a(ast, scope, parent) + local ranges = compiler.assert(utils["table?"](ast[2]), "expected binding table", ast) + local until_condition = remove_until_condition(ast[2]) + local binding_sym = table.remove(ast[2], 1) + local sub_scope = compiler["make-scope"](scope) + local range_args = {} + local chunk = {} + compiler.assert(utils["sym?"](binding_sym), ("unable to bind %s %s"):format(type(binding_sym), tostring(binding_sym)), ast[2]) + compiler.assert((#ast >= 3), "expected body expression", ast[1]) + compiler.assert((#ranges <= 3), "unexpected arguments", ranges[4]) + for i = 1, math.min(#ranges, 3) do + range_args[i] = tostring((compiler.compile1(ranges[i], scope, parent, {nval = 1}))[1]) + end + compiler.emit(parent, ("for %s = %s do"):format(compiler["declare-local"](binding_sym, {}, sub_scope, ast), table.concat(range_args, ", ")), ast) + compile_until(until_condition, sub_scope, chunk) + compile_do(ast, sub_scope, chunk, 3) + compiler.emit(parent, chunk, ast) + return compiler.emit(parent, "end", ast) + end + SPECIALS["for"] = for_2a + doc_special("for", {"[index start stop step?]", "..."}, "Numeric loop construct.\nEvaluates body once for each value between start and stop (inclusive).", true) + local function native_method_call(ast, _scope, _parent, target, args) + local _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)" + else + call_string = "%s:%s(%s)" + end + return utils.expr(string.format(call_string, tostring(target), method_string, table.concat(args, ", ")), "statement") + end + local function nonnative_method_call(ast, scope, parent, target, args) + local method_string = tostring((compiler.compile1(ast[3], scope, parent, {nval = 1}))[1]) + local args0 = {tostring(target), unpack(args)} + return utils.expr(string.format("%s[%s](%s)", tostring(target), method_string, table.concat(args0, ", ")), "statement") + end + local function double_eval_protected_method_call(ast, scope, parent, target, args) + local method_string = tostring((compiler.compile1(ast[3], scope, parent, {nval = 1}))[1]) + local call = "(function(tgt, m, ...) return tgt[m](tgt, ...) end)(%s, %s)" + table.insert(args, 1, method_string) + return utils.expr(string.format(call, tostring(target), table.concat(args, ", ")), "statement") + end + local function method_call(ast, scope, parent) + compiler.assert((2 < #ast), "expected at least 2 arguments", ast) + local _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 _446_ + if (i ~= #ast) then + _446_ = 1 + else + _446_ = nil + end + subexprs = compiler.compile1(ast[i], scope, parent, {nval = _446_}) + utils.map(subexprs, tostring, args) + end + if (utils["string?"](ast[3]) and utils["valid-lua-identifier?"](ast[3])) then + return native_method_call(ast, scope, parent, target, args) + elseif (target.type == "sym") then + return nonnative_method_call(ast, scope, parent, target, args) + else + return double_eval_protected_method_call(ast, scope, parent, target, args) + end + end + SPECIALS[":"] = method_call + doc_special(":", {"tbl", "method-name", "..."}, "Call the named method on tbl with the provided args.\nMethod name doesn't have to be known at compile-time; if it is, use\n(tbl:method-name ...) instead.") + SPECIALS.comment = function(ast, _, parent) + local els = {} + for i = 2, #ast do + table.insert(els, view(ast[i], {["one-line?"] = true})) + end + return compiler.emit(parent, ("--[[ " .. table.concat(els, " ") .. " ]]--"), ast) + end + doc_special("comment", {"..."}, "Comment which will be emitted in Lua output.", true) + local function hashfn_max_used(f_scope, i, max) + local max0 + if f_scope.symmeta[("$" .. i)].used then + max0 = i + else + max0 = max + end + if (i < 9) then + return hashfn_max_used(f_scope, (i + 1), max0) + else + return max0 + end + end + SPECIALS.hashfn = function(ast, scope, parent) + compiler.assert((#ast == 2), "expected one argument", ast) + local f_scope + do + 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) + local symbol = utils.sym(name) + local args = {} + compiler["declare-local"](symbol, {}, scope, ast) + for i = 1, 9 do + args[i] = compiler["declare-local"](utils.sym(("$" .. i)), {}, f_scope, ast) + end + local function walker(idx, node, parent_node) + if (utils["sym?"](node) and (tostring(node) == "$...")) then + parent_node[idx] = utils.varg() + f_scope.vararg = true + return nil + else + return (utils["list?"](node) or utils["table?"](node)) + end + end + utils["walk-tree"](ast[2], walker) + compiler.compile1(ast[2], f_scope, f_chunk, {tail = true}) + local max_used = hashfn_max_used(f_scope, 1, 0) + if f_scope.vararg then + compiler.assert((max_used == 0), "$ and $... in hashfn are mutually exclusive", ast) + else + end + local arg_str + if f_scope.vararg then + arg_str = tostring(utils.varg()) + else + arg_str = table.concat(args, ", ", 1, max_used) + end + compiler.emit(parent, string.format("local function %s(%s)", name, arg_str), ast) + compiler.emit(parent, f_chunk, ast) + compiler.emit(parent, "end", ast) + return utils.expr(name, "sym") + end + doc_special("hashfn", {"..."}, "Function literal shorthand; args are either $... OR $1, $2, etc.") + local function maybe_short_circuit_protect(ast, i, name, _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 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 _459_ = #operands + if (_459_ == 0) then + local _461_ + do + local _460_ = zero_arity + compiler.assert(_460_, "Expected more than 0 arguments", ast) + _461_ = _460_ + end + 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 _ = _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 _467_ + do + local _464_ = (_3flua_name or name) + local _465_ = zero_arity + local _466_ = unary_prefix + local function _468_(...) + return arithmetic_special(_464_, _465_, _466_, ...) + end + _467_ = _468_ + end + 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") + define_arithmetic_special("..", "''") + define_arithmetic_special("^") + define_arithmetic_special("-", nil, "") + define_arithmetic_special("*", "1") + define_arithmetic_special("%") + define_arithmetic_special("/", nil, "1") + define_arithmetic_special("//", nil, "1") + SPECIALS["or"] = function(ast, scope, parent) + return arithmetic_special("or", "false", nil, ast, scope, parent) + end + SPECIALS["and"] = function(ast, scope, parent) + return arithmetic_special("and", "true", nil, ast, scope, parent) + end + doc_special("and", {"a", "b", "..."}, "Boolean operator; works the same as Lua but accepts more arguments.") + doc_special("or", {"a", "b", "..."}, "Boolean operator; works the same as Lua but accepts more arguments.") + local function bitop_special(native_name, lib_name, zero_arity, unary_prefix, ast, scope, parent) + if (#ast == 1) then + return compiler.assert(zero_arity, "Expected more than 0 arguments.", ast) + else + local len = #ast + local operands = {} + local padded_native_name = (" " .. native_name .. " ") + local prefixed_lib_name = ("bit." .. lib_name) + for i = 2, len do + local subexprs + local _469_ + if (i ~= len) then + _469_ = 1 + else + _469_ = nil + end + subexprs = compiler.compile1(ast[i], scope, parent, {nval = _469_}) + utils.map(subexprs, tostring, operands) + end + if (#operands == 1) then + if utils.root.options.useBitLib then + return (prefixed_lib_name .. "(" .. unary_prefix .. ", " .. operands[1] .. ")") + else + return ("(" .. unary_prefix .. padded_native_name .. operands[1] .. ")") + end + else + if utils.root.options.useBitLib then + return (prefixed_lib_name .. "(" .. table.concat(operands, ", ") .. ")") + else + return ("(" .. table.concat(operands, padded_native_name) .. ")") + end + end + end + end + local function define_bitop_special(name, zero_arity, unary_prefix, native) + local _479_ + do + 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 + _479_ = _480_ + end + SPECIALS[name] = _479_ + return nil + end + define_bitop_special("lshift", nil, "1", "<<") + define_bitop_special("rshift", nil, "1", ">>") + define_bitop_special("band", "0", "0", "&") + define_bitop_special("bor", "0", "0", "|") + define_bitop_special("bxor", "0", "0", "~") + doc_special("lshift", {"x", "n"}, "Bitwise logical left shift of x by n bits.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") + doc_special("rshift", {"x", "n"}, "Bitwise logical right shift of x by n bits.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") + doc_special("band", {"x1", "x2", "..."}, "Bitwise AND of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") + doc_special("bor", {"x1", "x2", "..."}, "Bitwise OR of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") + doc_special("bxor", {"x1", "x2", "..."}, "Bitwise XOR of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") + doc_special("..", {"a", "b", "..."}, "String concatenation operator; works the same as Lua but accepts more arguments.") + 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) + local arglist = {} + local comparisons = {} + local vals = {} + local chain = string.format(" %s ", (chain_op or "and")) + for i = 2, #ast do + table.insert(arglist, tostring(compiler.gensym(scope))) + table.insert(vals, tostring((compiler.compile1(ast[i], scope, parent, {nval = 1}))[1])) + end + for i = 1, (#arglist - 1) do + table.insert(comparisons, string.format("(%s %s %s)", arglist[i], op, arglist[(i + 1)])) + end + return string.format("(function(%s) return %s end)(%s)", table.concat(arglist, ","), table.concat(comparisons, chain), table.concat(vals, ",")) + end + local function define_comparator_special(name, _3flua_op, _3fchain_op) + do + local op = (_3flua_op or name) + local function opfn(ast, scope, parent) + compiler.assert((2 < #ast), "expected at least two arguments", ast) + if (3 == #ast) then + return native_comparator(op, ast, scope, parent) + else + return double_eval_protected_comparator(op, _3fchain_op, ast, scope, parent) + end + end + SPECIALS[name] = opfn + end + return doc_special(name, {"a", "b", "..."}, "Comparison operator; works the same as Lua but accepts more arguments.") + end + define_comparator_special(">") + define_comparator_special("<") + define_comparator_special(">=") + define_comparator_special("<=") + define_comparator_special("=", "==") + define_comparator_special("not=", "~=", "or") + local function define_unary_special(op, _3frealop) + local function opfn(ast, scope, parent) + compiler.assert((#ast == 2), "expected one argument", ast) + local tail = compiler.compile1(ast[2], scope, parent, {nval = 1}) + return ((_3frealop or op) .. tostring(tail[1])) + end + SPECIALS[op] = opfn + return nil + end + define_unary_special("not", "not ") + doc_special("not", {"x"}, "Logical operator; works the same as Lua.") + define_unary_special("bnot", "~") + doc_special("bnot", {"x"}, "Bitwise negation; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") + define_unary_special("length", "#") + doc_special("length", {"x"}, "Returns the length of a table or string.") + do end (SPECIALS)["~="] = SPECIALS["not="] + SPECIALS["#"] = SPECIALS.length + SPECIALS.quote = function(ast, scope, parent) + compiler.assert((#ast == 2), "expected one argument", ast) + local runtime, this_scope = true, scope + while this_scope do + this_scope = this_scope.parent + if (this_scope == compiler.scopes.compiler) then + runtime = false + else + end + end + return compiler["do-quote"](ast[2], scope, parent, runtime) + end + doc_special("quote", {"x"}, "Quasiquote the following form. Only works in macro/compiler scope.") + local macro_loaded = {} + local function safe_getmetatable(tbl) + local mt = getmetatable(tbl) + assert((mt ~= getmetatable("")), "Illegal metatable access!") + return mt + end + local safe_require = nil + local function safe_compiler_env() + local _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_490_ = getmetatable(env) + local __index = _let_490_["__index"] + if ("table" == type(__index)) then + for k, v in pairs(__index) do + combined[k] = v + end + else + end + for k, v in next, env, nil do + combined[k] = v + end + return next, combined, nil + end + local function make_compiler_env(ast, scope, parent, _3fopts) + local provided + do + 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(_492_) == "table") and (nil ~= (_492_).compilerEnv)) then + local compilerEnv = (_492_).compilerEnv + provided = compilerEnv + elseif ((_G.type(_492_) == "table") and (nil ~= (_492_)["compiler-env"])) then + local compiler_env = (_492_)["compiler-env"] + provided = compiler_env + elseif true then + local _ = _492_ + provided = safe_compiler_env(false) + else + provided = nil + end + end + local env + local function _494_(base) + return utils.sym(compiler.gensym((compiler.scopes.macro or scope), base)) + end + local function _495_() + return compiler.scopes.macro + end + local function _496_(symbol) + compiler.assert(compiler.scopes.macro, "must call from macro", ast) + return compiler.scopes.macro.manglings[tostring(symbol)] + end + 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 = _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 _499_(...) + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for c in string.gmatch((package.config or ""), "([^\n]+)") do + local val_16_auto = c + 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 tbl_14_auto + end + 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") + end + local function search_module(modulename, _3fpathstring) + local pathsepesc = escapepat(pkg_config.pathsep) + local pattern = ("([^%s]*)%s"):format(pathsepesc, pathsepesc) + local no_dot_module = modulename:gsub("%.", pkg_config.dirsep) + local fullpath = ((_3fpathstring or utils["fennel-module"].path) .. pkg_config.pathsep) + local function try_path(path) + local filename = path:gsub(escapepat(pkg_config.pathmark), no_dot_module) + local filename2 = path:gsub(escapepat(pkg_config.pathmark), modulename) + local _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, _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 + end + return find_in_path(1) + end + local function make_searcher(_3foptions) + 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 _513_, _514_ = search_module(module_name) + if (nil ~= _513_) then + local filename = _513_ + local _517_ + do + local _515_ = filename + local _516_ = opts + local function _518_(...) + return utils["fennel-module"].dofile(_515_, _516_, ...) + end + _517_ = _518_ + end + return _517_, filename + elseif ((_513_ == nil) and (nil ~= _514_)) then + local error = _514_ + return error + else + return nil + end + end + 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 _520_ = utils.copy(utils.root.options) + do end (_520_)["env"] = "_COMPILER" + _520_["requireAsInclude"] = false + _520_["allowedGlobals"] = nil + opts = _520_ + end + 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 + _522_ = _527_ + else + local _528_ = filename + local _529_ = opts + local function _531_(...) + return utils["fennel-module"].dofile(_528_, _529_, ...) + end + _522_ = _531_ + end + return _522_, filename + else + return nil + end + end + local function lua_macro_searcher(module_name) + local _534_ = search_module(module_name, package.path) + if (nil ~= _534_) then + local filename = _534_ + local code + do + local f = io.open(filename) + local function close_handlers_8_auto(ok_9_auto, ...) + f:close() + if ok_9_auto then + return ... + else + return error(..., 0) + end + end + local function _536_() + return assert(f:read("*a")) + end + 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 + else + return nil + end + end + local macro_searchers = {fennel_macro_searcher, lua_macro_searcher} + local function search_macro_module(modname, n) + 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 _ = _539_ + return search_macro_module(modname, (n + 1)) + else + return nil + end + else + return nil + end + end + local function metadata_only_fennel(modname) + if ((modname == "fennel.macros") or (package and package.loaded and ("table" == type(package.loaded[modname])) and (package.loaded[modname].metadata == compiler.metadata))) then + return {metadata = compiler.metadata} + else + return nil + end + end + 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 _545_()) + end + 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 + compiler.assert((type(v) == "function"), "expected each macro to be function", ast) + do end (scope.macros)[k] = v + end + return nil + end + 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) + local modname_chunk = load_code(modexpr) + return modname_chunk(module_name, filename0) + end + SPECIALS["require-macros"] = function(ast, scope, parent, _3freal_ast) + compiler.assert((#ast == 2), "Expected one module name argument", (_3freal_ast or ast)) + local modname = resolve_module_name(ast, scope, parent, {}) + compiler.assert(utils["string?"](modname), "module name must compile to string", (_3freal_ast or ast)) + if not macro_loaded[modname] then + local loader, filename = search_macro_module(modname, 1) + compiler.assert(loader, (modname .. " module not found."), ast) + do end (macro_loaded)[modname] = loader(modname, filename) + else + end + if ("import-macros" == tostring(ast[1])) then + return macro_loaded[modname] + else + return add_macros(macro_loaded[modname], ast, scope, parent) + end + end + doc_special("require-macros", {"macro-module-name"}, "Load given module and use its contents as macro definitions in current scope.\nMacro module should return a table of macro functions with string keys.\nConsider using import-macros instead as it is more flexible.") + local function emit_included_fennel(src, path, opts, sub_chunk) + local subscope = compiler["make-scope"](utils.root.scope.parent) + local forms = {} + if utils.root.options.requireAsInclude then + subscope.specials.require = compiler["require-include"] + else + end + for _, val in parser.parser(parser["string-stream"](src), path) do + table.insert(forms, val) + end + for i = 1, #forms do + local subopts + if (i == #forms) then + subopts = {tail = true} + else + subopts = {nval = 0} + end + utils["propagate-options"](opts, subopts) + compiler.compile1(forms[i], subscope, sub_chunk, subopts) + end + return nil + end + local function include_path(ast, opts, path, mod, fennel_3f) + utils.root.scope.includes[mod] = "fnl/loading" + local src + do + local f = assert(io.open(path)) + local function close_handlers_8_auto(ok_9_auto, ...) + f:close() + if ok_9_auto then + return ... + else + return error(..., 0) + end + end + local function _553_() + return assert(f:read("*all")):gsub("[\13\n]*$", "") + end + 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) + local preload_str = (target .. " = " .. target .. " or function(...)") + local temp_chunk, sub_chunk = {}, {} + compiler.emit(temp_chunk, preload_str, ast) + compiler.emit(temp_chunk, sub_chunk) + compiler.emit(temp_chunk, "end", ast) + for i, v in ipairs(temp_chunk) do + table.insert(utils.root.chunk, i, v) + end + if fennel_3f then + emit_included_fennel(src, path, opts, sub_chunk) + else + compiler.emit(sub_chunk, src, ast) + end + utils.root.scope.includes[mod] = ret + return ret + end + local function include_circular_fallback(mod, modexpr, fallback, ast) + if (utils.root.scope.includes[mod] == "fnl/loading") then + compiler.assert(fallback, "circular include detected", ast) + return fallback(modexpr) + else + return nil + end + end + SPECIALS.include = function(ast, scope, parent, opts) + compiler.assert((#ast == 2), "expected one argument", ast) + local modexpr + do + 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 _ = _556_ + modexpr = (compiler.compile1(ast[2], scope, parent, {nval = 1}))[1] + else + modexpr = nil + end + end + if ((modexpr.type ~= "literal") or ((modexpr[1]):byte() ~= 34)) then + if opts.fallback then + return opts.fallback(modexpr) + else + return compiler.assert(false, "module name must be string literal", ast) + end + else + local mod = load_code(("return " .. modexpr[1]))() + local oldmod = utils.root.options["module-name"] + local _ + utils.root.options["module-name"] = mod + _ = nil + local res + 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 = _560_ + local lua_path = search_module(mod, package.path) + if lua_path then + return include_path(ast, opts, lua_path, mod, false) + elseif opts.fallback then + return opts.fallback(modexpr) + else + return compiler.assert(false, ("module not found " .. mod), ast) + end + else + return nil + end + end + res = ((utils["member?"](mod, (utils.root.options.skipInclude or {})) and opts.fallback(modexpr, true)) or include_circular_fallback(mod, modexpr, opts.fallback, ast) or utils.root.scope.includes[mod] or _561_()) + utils.root.options["module-name"] = oldmod + return res + end + end + doc_special("include", {"module-name-literal"}, "Like require but load the target module during compilation and embed it in the\nLua output. The module must be a string literal and resolvable at compile time.") + local function eval_compiler_2a(ast, scope, parent) + local env = make_compiler_env(ast, scope, parent) + local opts = utils.copy(utils.root.options) + opts.scope = compiler["make-scope"](compiler.scopes.compiler) + opts.allowedGlobals = current_global_names(env) + return load_code(compiler.compile(ast, opts), wrap_env(env))(opts["module-name"], ast.filename) + end + SPECIALS.macros = function(ast, scope, parent) + compiler.assert((#ast == 2), "Expected one table argument", ast) + return add_macros(eval_compiler_2a(ast[2], scope, parent), ast, scope, parent) + end + doc_special("macros", {"{:macro-name-1 (fn [...] ...) ... :macro-name-N macro-body-N}"}, "Define all functions in the given table as macros local to the current scope.") + SPECIALS["eval-compiler"] = function(ast, scope, parent) + local old_first = ast[1] + ast[1] = utils.sym("do") + local val = eval_compiler_2a(ast, scope, parent) + do end (ast)[1] = old_first + return val + end + doc_special("eval-compiler", {"..."}, "Evaluate the body at compile-time. Use the macro system instead if possible.", true) + return {doc = doc_2a, ["current-global-names"] = current_global_names, ["load-code"] = load_code, ["macro-loaded"] = macro_loaded, ["macro-searchers"] = macro_searchers, ["make-compiler-env"] = make_compiler_env, ["search-module"] = search_module, ["make-searcher"] = make_searcher, ["wrap-env"] = wrap_env} +end +package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or function(...) + local utils = require("fennel.utils") + local parser = require("fennel.parser") + local friend = require("fennel.friend") + local unpack = (table.unpack or _G.unpack) + local scopes = {} + local function make_scope(_3fparent) + local parent = (_3fparent or scopes.global) + local _219_ + if parent then + _219_ = ((parent.depth or 0) + 1) + else + _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 = _219_, hashfn = (parent and parent.hashfn), refedglobals = {}, parent = parent} + end + local function assert_msg(ast, msg) + local ast_tbl + if ("table" == type(ast)) then + ast_tbl = ast + else + ast_tbl = {} + end + local m = getmetatable(ast) + local filename = ((m and m.filename) or ast_tbl.filename or "unknown") + local line = ((m and m.line) or ast_tbl.line or "?") + local target = tostring((utils["sym?"](ast_tbl[1]) or ast_tbl[1] or "()")) + return string.format("%s:%s: Compile error in '%s': %s", filename, line, target, msg) + end + local function assert_compile(condition, msg, ast) + if not condition then + 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 + error(assert_msg(ast, msg), 0) + else + friend["assert-compile"](condition, msg, ast, source) + end + else + end + else + end + return condition + end + scopes.global = make_scope() + scopes.global.vararg = true + scopes.compiler = make_scope(scopes.global) + 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 _226_(_241) + return ("\\" .. _241:byte()) + end + 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 _227_(_241) + return string.format("_%02x", _241:byte()) + end + return ("__fnl_global__" .. str:gsub("[^%w]", _227_)) + end + end + local function global_unmangling(identifier) + 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 + _230_ = string.gsub(rest, "_[%da-f][%da-f]", _231_) + return _230_ + elseif true then + local _ = _229_ + return identifier + else + return nil + end + end + local allowed_globals = nil + local function global_allowed_3f(name) + return (not allowed_globals or utils["member?"](name, allowed_globals)) + end + local function unique_mangling(original, mangling, scope, append) + if (scope.unmanglings[mangling] and not scope.gensyms[mangling]) then + return unique_mangling(original, (original .. append), scope, (append + 1)) + else + return mangling + end + end + local function local_mangling(str, scope, ast, _3ftemp_manglings) + assert_compile(not utils["multi-sym?"](str), ("unexpected multi symbol " .. str), ast) + local raw + if ((utils["lua-keywords"])[str] or str:match("^%d")) then + raw = ("_" .. str) + else + raw = str + end + local mangling + local function _235_(_241) + return string.format("_%02x", _241:byte()) + end + mangling = string.gsub(string.gsub(raw, "-", "_"), "[^%w_]", _235_) + local unique = unique_mangling(mangling, mangling, scope, 0) + do end (scope.unmanglings)[unique] = str + do + local manglings = (_3ftemp_manglings or scope.manglings) + do end (manglings)[str] = unique + end + return unique + end + local function apply_manglings(scope, new_manglings, ast) + for raw, mangled in pairs(new_manglings) do + assert_compile(not scope.refedglobals[mangled], ("use of global " .. raw .. " is aliased by a local"), ast) + do end (scope.manglings)[raw] = mangled + end + return nil + end + local function combine_parts(parts, scope) + local ret = (scope.manglings[parts[1]] or global_mangling(parts[1])) + for i = 2, #parts do + if utils["valid-lua-identifier?"](parts[i]) then + if (parts["multi-sym-method-call"] and (i == #parts)) then + ret = (ret .. ":" .. parts[i]) + else + ret = (ret .. "." .. parts[i]) + end + else + ret = (ret .. "[" .. serialize_string(parts[i]) .. "]") + end + end + return ret + end + local function next_append() + utils.root.scope["gensym-append"] = ((utils.root.scope["gensym-append"] or 0) + 1) + return ("_" .. utils.root.scope["gensym-append"] .. "_") + end + local function gensym(scope, _3fbase, _3fsuffix) + local mangling = ((_3fbase or "") .. next_append() .. (_3fsuffix or "")) + while scope.unmanglings[mangling] do + mangling = ((_3fbase or "") .. next_append() .. (_3fsuffix or "")) + end + scope.unmanglings[mangling] = (_3fbase or true) + do end (scope.gensyms)[mangling] = true + return mangling + end + local function autogensym(base, scope) + 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 _ = _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 _239_()) + else + return nil + end + end + local function check_binding_valid(symbol, scope, ast) + local name = tostring(symbol) + 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 + local function declare_local(symbol, meta, scope, ast, _3ftemp_manglings) + check_binding_valid(symbol, scope, ast) + local name = tostring(symbol) + assert_compile(not utils["multi-sym?"](name), ("unexpected multi symbol " .. name), ast) + do end (scope.symmeta)[name] = meta + return local_mangling(name, scope, ast, _3ftemp_manglings) + end + local function hashfn_arg_name(name, multi_sym_parts, scope) + if not scope.hashfn then + return nil + elseif (name == "$") then + return "$1" + elseif multi_sym_parts then + if (multi_sym_parts and (multi_sym_parts[1] == "$")) then + multi_sym_parts[1] = "$1" + else + end + return table.concat(multi_sym_parts, ".") + else + return nil + end + end + local function symbol_to_expression(symbol, scope, _3freference_3f) + utils.hook("symbol-to-expression", symbol, scope, _3freference_3f) + local name = symbol[1] + local multi_sym_parts = utils["multi-sym?"](name) + local name0 = (hashfn_arg_name(name, multi_sym_parts, scope) or name) + local parts = (multi_sym_parts or {name0}) + local etype = (((#parts > 1) and "expression") or "sym") + local local_3f = scope.manglings[parts[1]] + if (local_3f and scope.symmeta[parts[1]]) then + scope.symmeta[parts[1]]["used"] = true + 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 + else + end + return utils.expr(combine_parts(parts, scope), etype) + end + local function emit(chunk, out, _3fast) + if (type(out) == "table") then + return table.insert(chunk, out) + else + return table.insert(chunk, {ast = _3fast, leaf = out}) + end + end + local function peephole(chunk) + if chunk.leaf then + return chunk + elseif ((#chunk >= 3) and ((chunk[(#chunk - 2)]).leaf == "do") and not (chunk[(#chunk - 1)]).leaf and (chunk[#chunk].leaf == "end")) then + local kid = peephole(chunk[(#chunk - 1)]) + local new_chunk = {ast = chunk.ast} + for i = 1, (#chunk - 3) do + table.insert(new_chunk, peephole(chunk[i])) + end + for i = 1, #kid do + table.insert(new_chunk, kid[i]) + end + return new_chunk + else + return utils.map(chunk, peephole) + end + end + local function flatten_chunk_correlated(main_chunk, options) + local function flatten(chunk, out, last_line, file) + local last_line0 = last_line + if chunk.leaf then + out[last_line0] = ((out[last_line0] or "") .. " " .. chunk.leaf) + else + for _, subchunk in ipairs(chunk) do + if (subchunk.leaf or (#subchunk > 0)) then + local source = utils["ast-source"](subchunk.ast) + if (file == source.filename) then + last_line0 = math.max(last_line0, (source.line or 0)) + else + end + last_line0 = flatten(subchunk, out, last_line0, file) + else + end + end + end + return last_line0 + end + local out = {} + local last = flatten(main_chunk, out, 1, options.filename) + for i = 1, last do + if (out[i] == nil) then + out[i] = "" + else + end + end + return table.concat(out, "\n") + end + local function flatten_chunk(sm, chunk, tab, depth) + if chunk.leaf then + local code = chunk.leaf + local info = chunk.ast + if sm then + table.insert(sm, {(info and info.filename), (info and info.line)}) + else + end + return code + else + local tab0 + do + local _252_ = tab + if (_252_ == true) then + tab0 = " " + elseif (_252_ == false) then + tab0 = "" + elseif (_252_ == tab) then + tab0 = tab + elseif (_252_ == nil) then + tab0 = "" + else + tab0 = nil + end + end + local function parter(c) + if (c.leaf or (#c > 0)) then + local sub = flatten_chunk(sm, c, tab0, (depth + 1)) + if (depth > 0) then + return (tab0 .. sub:gsub("\n", ("\n" .. tab0))) + else + return sub + end + else + return nil + end + end + return table.concat(utils.map(chunk, parter), "\n") + end + end + local sourcemap = {} + local function make_short_src(source) + local source0 = source:gsub("\n", " ") + if (#source0 <= 49) then + return ("[fennel \"" .. source0 .. "\"]") + else + return ("[fennel \"" .. source0:sub(1, 46) .. "...\"]") + end + end + local function flatten(chunk, options) + local chunk0 = peephole(chunk) + if options.correlate then + return flatten_chunk_correlated(chunk0, options), {} + else + local sm = {} + local ret = flatten_chunk(sm, chunk0, options.indent, 0) + if sm then + sm.short_src = (options.filename or make_short_src((options.source or ret))) + if options.filename then + sm.key = ("@" .. options.filename) + else + sm.key = ret + end + sourcemap[sm.key] = sm + else + end + return ret, sm + end + end + local function make_metadata() + local function _261_(self, tgt, key) + if self[tgt] then + return self[tgt][key] + else + return nil + end + end + local function _263_(self, tgt, key, value) + self[tgt] = (self[tgt] or {}) + do end (self[tgt])[key] = value + return tgt + end + local function _264_(self, tgt, ...) + local kv_len = select("#", ...) + local kvs = {...} + if ((kv_len % 2) ~= 0) then + error("metadata:setall() expected even number of k/v pairs") + else + end + self[tgt] = (self[tgt] or {}) + for i = 1, kv_len, 2 do + self[tgt][kvs[i]] = kvs[(i + 1)] + end + return tgt + end + return setmetatable({}, {__index = {get = _261_, set = _263_, setall = _264_}, __mode = "k"}) + end + local function exprs1(exprs) + return table.concat(utils.map(exprs, tostring), ", ") + end + local function keep_side_effects(exprs, chunk, start, ast) + local start0 = (start or 1) + for j = start0, #exprs do + local se = exprs[j] + if ((se.type == "expression") and (se[1] ~= "nil")) then + emit(chunk, string.format("do local _ = %s end", tostring(se)), ast) + elseif (se.type == "statement") then + local code = tostring(se) + local disambiguated + if (code:byte() == 40) then + disambiguated = ("do end " .. code) + else + disambiguated = code + end + emit(chunk, disambiguated, ast) + else + end + end + return nil + end + local function handle_compile_opts(exprs, parent, opts, ast) + if opts.nval then + local n = opts.nval + local len = #exprs + if (n ~= len) then + if (len > n) then + keep_side_effects(exprs, parent, (n + 1), ast) + for i = (n + 1), len do + exprs[i] = nil + end + else + for i = (#exprs + 1), n do + exprs[i] = utils.expr("nil", "literal") + end + end + else + end + else + end + if opts.tail then + emit(parent, string.format("return %s", exprs1(exprs)), ast) + else + end + if opts.target then + local result = exprs1(exprs) + local function _272_() + if (result == "") then + return "nil" + else + return result + end + end + emit(parent, string.format("%s = %s", opts.target, _272_()), ast) + else + end + if (opts.tail or opts.target) then + return {returned = true} + else + local _274_ = exprs + _274_["returned"] = true + return _274_ + end + end + local function find_macro(ast, scope, multi_sym_parts) + local function find_in_table(t, i) + if (i <= #multi_sym_parts) then + return find_in_table((utils["table?"](t) and t[multi_sym_parts[i]]), (i + 1)) + else + return t + end + end + local macro_2a = (utils["sym?"](ast[1]) and scope.macros[tostring(ast[1])]) + if (not macro_2a and multi_sym_parts) then + local nested_macro = find_in_table(scope.macros, 1) + assert_compile((not scope.macros[multi_sym_parts[1]] or (type(nested_macro) == "function")), "macro not found in imported macro module", ast) + return nested_macro + else + return macro_2a + end + end + 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 + src.bytestart, src.byteend = bytestart, byteend + else + end + return ("table" == type(node)) + end + local function macroexpand_2a(ast, scope, _3fonce) + local _281_ + if utils["list?"](ast) then + _281_ = find_macro(ast, scope, utils["multi-sym?"](ast[1])) + else + _281_ = nil + end + if (_281_ == false) then + return ast + 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 _283_() + return macro_2a(unpack(ast, 2)) + end + ok, transformed = xpcall(_283_, debug.traceback) + local function _285_() + local _284_ = ast + local function _286_(...) + return propagate_trace_info(_284_, ...) + end + return _286_ + end + utils["walk-tree"](transformed, _285_()) + scopes.macro = old_scope + assert_compile(ok, transformed, ast) + if (_3fonce or not transformed) then + return transformed + else + return macroexpand_2a(transformed, scope) + end + elseif true then + local _ = _281_ + return ast + else + return nil + end + end + local function compile_special(ast, scope, parent, opts, special) + local exprs = (special(ast, scope, parent, opts) or utils.expr("nil", "literal")) + local exprs0 + if ("table" ~= type(exprs)) then + exprs0 = utils.expr(exprs, "expression") + else + exprs0 = exprs + end + local exprs2 + if utils["expr?"](exprs0) then + exprs2 = {exprs0} + else + exprs2 = exprs0 + end + if not exprs2.returned then + return handle_compile_opts(exprs2, parent, opts, ast) + elseif (opts.tail or opts.target) then + return {returned = true} + else + return exprs2 + end + end + local function compile_function_call(ast, scope, parent, opts, compile1, len) + local fargs = {} + local fcallee = (compile1(ast[1], scope, parent, {nval = 1}))[1] + 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 _292_ + if (i ~= len) then + _292_ = 1 + else + _292_ = nil + end + 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 + table.insert(fargs, subexprs[j]) + end + else + keep_side_effects(subexprs, parent, 2, ast[i]) + end + end + local pat + if ("string" == type(ast[1])) then + pat = "(%s)(%s)" + else + pat = "%s(%s)" + end + local call = string.format(pat, tostring(fcallee), exprs1(fargs)) + return handle_compile_opts({utils.expr(call, "statement")}, parent, opts, ast) + end + local function compile_call(ast, scope, parent, opts, compile1) + utils.hook("call", ast, scope) + local len = #ast + local first = ast[1] + local multi_sym_parts = utils["multi-sym?"](first) + local special = (utils["sym?"](first) and scope.specials[tostring(first)]) + assert_compile((len > 0), "expected a function, macro, or special to call", ast) + if special then + return compile_special(ast, scope, parent, opts, special) + 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(":", 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) + 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) + local multi_sym_parts = utils["multi-sym?"](ast) + assert_compile(not (multi_sym_parts and multi_sym_parts["multi-sym-method-call"]), "multisym method calls may only be in call position", ast) + local e + if (ast[1] == "nil") then + e = utils.expr("nil", "literal") + else + e = symbol_to_expression(ast, scope, true) + end + return handle_compile_opts({e}, parent, opts, ast) + end + local function serialize_number(n) + local _300_ = string.gsub(tostring(n), ",", ".") + return _300_ + end + local function compile_scalar(ast, _scope, parent, opts) + local serialize + do + local _301_ = type(ast) + if (_301_ == "nil") then + serialize = tostring + elseif (_301_ == "boolean") then + serialize = tostring + elseif (_301_ == "string") then + serialize = serialize_string + elseif (_301_ == "number") then + serialize = serialize_number + else + serialize = nil + end + end + return handle_compile_opts({utils.expr(serialize(ast), "literal")}, parent, opts) + end + local function compile_table(ast, scope, parent, opts, compile1) + local buffer = {} + local function write_other_values(k) + if ((type(k) ~= "number") or (math.floor(k) ~= k) or (k < 1) or (k > #ast)) then + if ((type(k) == "string") and utils["valid-lua-identifier?"](k)) then + return {k, k} + else + local _let_303_ = compile1(k, scope, parent, {nval = 1}) + local compiled = _let_303_[1] + local kstr = ("[" .. tostring(compiled) .. "]") + return {kstr, k} + end + else + return nil + end + end + do + local keys + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for k, v in utils.stablepairs(ast) do + local val_16_auto = write_other_values(k, v) + 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 + keys = tbl_14_auto + end + 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, _309_, buffer) + end + for i = 1, #ast do + local nval = ((i ~= #ast) and 1) + table.insert(buffer, exprs1(compile1(ast[i], scope, parent, {nval = nval}))) + end + return handle_compile_opts({utils.expr(("{" .. table.concat(buffer, ", ") .. "}"), "expression")}, parent, opts, ast) + end + local function compile1(ast, scope, parent, _3fopts) + local opts = (_3fopts or {}) + local ast0 = macroexpand_2a(ast, scope) + if utils["list?"](ast0) then + return compile_call(ast0, scope, parent, opts, compile1) + elseif utils["varg?"](ast0) then + return compile_varg(ast0, scope, parent, opts) + elseif utils["sym?"](ast0) then + return compile_sym(ast0, scope, parent, opts) + elseif (type(ast0) == "table") then + return compile_table(ast0, scope, parent, opts, compile1) + elseif ((type(ast0) == "nil") or (type(ast0) == "boolean") or (type(ast0) == "number") or (type(ast0) == "string")) then + return compile_scalar(ast0, scope, parent, opts) + else + return assert_compile(false, ("could not compile value of type " .. type(ast0)), ast0) + end + end + local function destructure(to, from, ast, scope, parent, opts) + local opts0 = (opts or {}) + 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 + setter = "local %s = %s" + else + setter = "%s = %s" + end + local new_manglings = {} + local function getname(symbol, up1) + local raw = symbol[1] + assert_compile(not (opts0.nomulti and utils["multi-sym?"](raw)), ("unexpected multi symbol " .. raw), up1) + if declaration then + return declare_local(symbol, nil, scope, symbol, new_manglings) + else + local parts = (utils["multi-sym?"](raw) or {raw}) + local meta = scope.symmeta[parts[1]] + assert_compile(not raw:find(":"), "cannot set method sym", symbol) + if ((#parts == 1) and not forceset) then + assert_compile(not (forceglobal and meta), string.format("global %s conflicts with local", tostring(symbol)), symbol) + assert_compile(not (meta and not meta.var), ("expected var " .. raw), symbol) + assert_compile((meta or not opts0.noundef), ("expected local " .. parts[1]), symbol) + else + end + if forceglobal then + assert_compile(not scope.symmeta[scope.unmanglings[raw]], ("global " .. raw .. " conflicts with local"), symbol) + do end (scope.manglings)[raw] = global_mangling(raw) + do end (scope.unmanglings)[global_mangling(raw)] = raw + if allowed_globals then + table.insert(allowed_globals, raw) + else + end + else + end + return symbol_to_expression(symbol, scope)[1] + end + end + local function compile_top_target(lvalues) + local inits + local function _318_(_241) + if scope.manglings[_241] then + return _241 + else + return "nil" + end + end + inits = utils.map(lvalues, _318_) + local init = table.concat(inits, ", ") + local lvalue = table.concat(lvalues, ", ") + local plen, plast = #parent, parent[#parent] + local ret = compile1(from, scope, parent, {target = lvalue}) + if declaration then + for pi = plen, #parent do + if (parent[pi] == plast) then + plen = pi + else + end + end + if ((#parent == (plen + 1)) and parent[#parent].leaf) then + parent[#parent]["leaf"] = ("local " .. parent[#parent].leaf) + elseif (init == "nil") then + table.insert(parent, (plen + 1), {ast = ast, leaf = ("local " .. lvalue)}) + else + table.insert(parent, (plen + 1), {ast = ast, leaf = ("local " .. lvalue .. " = " .. init)}) + end + else + end + return ret + end + local function destructure_sym(left, rightexprs, up1, top_3f) + local lname = getname(left, up1) + check_binding_valid(left, scope, left) + if top_3f then + compile_top_target({lname}) + else + emit(parent, setter:format(lname, exprs1(rightexprs)), left) + end + if declaration then + scope.symmeta[tostring(left)] = {var = isvar} + return nil + else + return nil + end + end + local function destructure_table(left, rightexprs, top_3f, destructure1) + local s = gensym(scope, symtype0) + local right + do + local _325_ + if top_3f then + _325_ = exprs1(compile1(from, scope, parent)) + else + _325_ = exprs1(rightexprs) + end + if (_325_ == "") then + right = "nil" + elseif (nil ~= _325_) then + local right0 = _325_ + right = right0 + else + right = nil + end + end + emit(parent, string.format("local %s = %s", s, right), left) + for k, v in utils.stablepairs(left) do + if not (("number" == type(k)) and tostring(left[(k - 1)]):find("^&")) then + if (utils["sym?"](v) and (tostring(v) == "&")) then + local unpack_str = "(function (t, k)\n local mt = getmetatable(t)\n if \"table\" == type(mt) and mt.__fennelrest then\n return mt.__fennelrest(t, k)\n else\n return {(table.unpack or unpack)(t, k)}\n end\n end)(%s, %s)" + local formatted = string.format(string.gsub(unpack_str, "\n%s*", " "), s, k) + local subexpr = utils.expr(formatted, "expression") + assert_compile((utils["sequence?"](left) and (nil == left[(k + 2)])), "expected rest argument before last parameter", left) + destructure1(left[(k + 1)], {subexpr}, left) + elseif (utils["sym?"](k) and (tostring(k) == "&as")) then + destructure_sym(v, {utils.expr(tostring(s))}, left) + elseif (utils["sequence?"](left) and (tostring(v) == "&as")) then + local _, next_sym, trailing = select(k, unpack(left)) + assert_compile((nil == trailing), "expected &as argument before last parameter", left) + destructure_sym(next_sym, {utils.expr(tostring(s))}, left) + else + local key + if (type(k) == "string") then + key = serialize_string(k) + else + key = k + end + local subexpr = utils.expr(string.format("%s[%s]", s, key), "expression") + destructure1(v, {subexpr}, left) + end + else + end + end + return nil + end + local function destructure_values(left, up1, top_3f, destructure1) + local left_names, tables = {}, {} + for i, name in ipairs(left) do + if utils["sym?"](name) then + table.insert(left_names, getname(name, up1)) + else + local symname = gensym(scope, symtype0) + table.insert(left_names, symname) + do end (tables)[i] = {name, utils.expr(symname, "sym")} + end + end + assert_compile(top_3f, "can't nest multi-value destructuring", left) + compile_top_target(left_names) + if declaration then + for _, sym in ipairs(left) do + if utils["sym?"](sym) then + scope.symmeta[tostring(sym)] = {var = isvar} + else + end + end + else + end + for _, pair in utils.stablepairs(tables) do + destructure1(pair[1], {pair[2]}, left) + end + return nil + end + local function destructure1(left, rightexprs, up1, top_3f) + if (utils["sym?"](left) and (left[1] ~= "nil")) then + destructure_sym(left, rightexprs, up1, top_3f) + elseif utils["table?"](left) then + destructure_table(left, rightexprs, top_3f, destructure1) + elseif utils["list?"](left) then + destructure_values(left, up1, top_3f, destructure1) + else + assert_compile(false, string.format("unable to bind %s %s", type(left), tostring(left)), (((type((up1)[2]) == "table") and (up1)[2]) or up1)) + end + if top_3f then + return {returned = true} + else + return nil + end + end + local ret = destructure1(to, nil, ast, true) + utils.hook("destructure", from, to, scope) + apply_manglings(scope, new_manglings, ast) + return ret + end + local function require_include(ast, scope, parent, opts) + 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) + end + local function compile_stream(strm, options) + local opts = utils.copy(options) + local old_globals = allowed_globals + local scope = (opts.scope or make_scope(scopes.global)) + local vals = {} + local chunk = {} + do end (function(tgt, m, ...) return tgt[m](tgt, ...) end)(utils.root, "set-reset") + allowed_globals = opts.allowedGlobals + if (opts.indent == nil) then + opts.indent = " " + else + end + if opts.requireAsInclude then + scope.specials.require = require_include + else + end + utils.root.chunk, utils.root.scope, utils.root.options = chunk, scope, opts + for _, val in parser.parser(strm, opts.filename, opts) do + table.insert(vals, val) + end + for i = 1, #vals do + local exprs = compile1(vals[i], scope, chunk, {nval = (((i < #vals) and 0) or nil), tail = (i == #vals)}) + keep_side_effects(exprs, chunk, nil, vals[i]) + if (i == #vals) then + utils.hook("chunk", vals[i], scope) + else + end + end + allowed_globals = old_globals + utils.root.reset() + return flatten(chunk, opts) + end + local function compile_string(str, opts) + return compile_stream(parser["string-stream"](str), (opts or {})) + end + local function compile(ast, opts) + local opts0 = utils.copy(opts) + local old_globals = allowed_globals + local chunk = {} + local scope = (opts0.scope or make_scope(scopes.global)) + do end (function(tgt, m, ...) return tgt[m](tgt, ...) end)(utils.root, "set-reset") + allowed_globals = opts0.allowedGlobals + if (opts0.indent == nil) then + opts0.indent = " " + else + end + if opts0.requireAsInclude then + scope.specials.require = require_include + else + end + utils.root.chunk, utils.root.scope, utils.root.options = chunk, scope, opts0 + local exprs = compile1(ast, scope, chunk, {tail = true}) + keep_side_effects(exprs, chunk, nil, ast) + utils.hook("chunk", ast, scope) + allowed_globals = old_globals + utils.root.reset() + return flatten(chunk, opts0) + end + local function traceback_frame(info) + if ((info.what == "C") and info.name) then + return string.format(" [C]: in function '%s'", info.name) + elseif (info.what == "C") then + return " [C]: in ?" + else + local remap = sourcemap[info.source] + if (remap and remap[info.currentline]) 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 + end + info.currentline = (remap[info.currentline][2] or -1) + else + end + if (info.what == "Lua") then + 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, _344_()) + elseif (info.short_src == "(tail call)") then + return " (tail call)" + else + return string.format(" %s:%d: in main chunk", info.short_src, info.currentline) + end + end + end + 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 (msg:find(":%d+: Compile error") or msg:find(":%d+: Parse error")) then + table.insert(lines, msg) + else + local newmsg = msg:gsub("^[^:]*:%d+:%s+", "runtime error: ") + table.insert(lines, newmsg) + end + table.insert(lines, "stack traceback:") + local done_3f, level = false, (_3fstart or 2) + while not done_3f do + do + local _348_ = debug.getinfo(level, "Sln") + if (_348_ == nil) then + done_3f = true + elseif (nil ~= _348_) then + local info = _348_ + table.insert(lines, traceback_frame(info)) + else + end + end + level = (level + 1) + end + return table.concat(lines, "\n") + end + end + local function entry_transform(fk, fv) + local function _351_(k, v) + if (type(k) == "number") then + return k, fv(v) + else + return fk(k), fv(v) + end + end + return _351_ + end + local function mixed_concat(t, joiner) + local seen = {} + local ret, s = "", "" + for k, v in ipairs(t) do + table.insert(seen, k) + ret = (ret .. s .. v) + s = joiner + end + for k, v in utils.stablepairs(t) do + if not seen[k] then + ret = (ret .. s .. "[" .. k .. "]" .. "=" .. v) + s = joiner + else + end + end + return ret + end + local function do_quote(form, scope, parent, runtime_3f) + local function q(x) + return do_quote(x, scope, parent, runtime_3f) + end + if utils["varg?"](form) then + assert_compile(not runtime_3f, "quoted ... may only be used at compile time", form) + return "_VARARG" + elseif utils["sym?"](form) then + local filename + if form.filename then + filename = string.format("%q", form.filename) + else + filename = "nil" + end + local symstr = tostring(form) + assert_compile(not runtime_3f, "symbols may only be used at compile time", form) + if (symstr:find("#$") or symstr:find("#[:.]")) then + return string.format("sym('%s', {filename=%s, line=%s})", autogensym(symstr, scope), filename, (form.line or "nil")) + else + return string.format("sym('%s', {quoted=true, filename=%s, line=%s})", symstr, filename, (form.line or "nil")) + end + elseif (utils["list?"](form) and utils["sym?"](form[1]) and (tostring(form[1]) == "unquote")) then + local payload = form[2] + local res = unpack(compile1(payload, scope, parent)) + return res[1] + elseif utils["list?"](form) then + local mapped + local function _356_() + return nil + end + mapped = utils.kvmap(form, entry_transform(_356_, q)) + local filename + if form.filename then + filename = string.format("%q", form.filename) + else + filename = "nil" + end + assert_compile(not runtime_3f, "lists may only be used at compile time", form) + return string.format(("setmetatable({filename=%s, line=%s, bytestart=%s, %s}" .. ", getmetatable(list()))"), filename, (form.line or "nil"), (form.bytestart or "nil"), mixed_concat(mapped, ", ")) + elseif utils["sequence?"](form) then + local mapped = utils.kvmap(form, entry_transform(q, q)) + local source = getmetatable(form) + local filename + if source.filename then + filename = string.format("%q", source.filename) + else + filename = "nil" + end + local _359_ + if source then + _359_ = source.line + else + _359_ = "nil" + end + 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) + local filename + if source.filename then + filename = string.format("%q", source.filename) + else + filename = "nil" + end + 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, _362_()) + elseif (type(form) == "string") then + return serialize_string(form) + else + return tostring(form) + end + end + return {compile = compile, compile1 = compile1, ["compile-stream"] = compile_stream, ["compile-string"] = compile_string, emit = emit, destructure = destructure, ["require-include"] = require_include, autogensym = autogensym, gensym = gensym, ["do-quote"] = do_quote, ["global-mangling"] = global_mangling, ["global-unmangling"] = global_unmangling, ["apply-manglings"] = apply_manglings, macroexpand = macroexpand_2a, ["declare-local"] = declare_local, ["make-scope"] = make_scope, ["keep-side-effects"] = keep_side_effects, ["symbol-to-expression"] = symbol_to_expression, assert = assert_compile, scopes = scopes, traceback = traceback, metadata = make_metadata(), sourcemap = sourcemap} +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"}, ["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 + for pat, sug in pairs(suggestions) do + local matches = {msg:match(pat)} + if (0 < #matches) then + if ("table" == type(sug)) then + local out = {} + for _, s in ipairs(sug) do + table.insert(out, s:format(unpack(matches))) + end + suggestion = out + else + suggestion = sug(matches) + end + else + end + end + return suggestion + end + local function read_line_from_file(filename, line) + local bytes = 0 + local f = assert(io.open(filename)) + local _ + for _0 = 1, (line - 1) do + bytes = (bytes + 1 + #f:read()) + end + _ = nil + local codeline = f:read() + f:close() + return codeline, bytes + end + local function read_line_from_string(matcher, target_line, _3fcurrent_line, _3fbytes) + local this_line, newline = matcher() + local current_line = (_3fcurrent_line or 1) + local bytes = ((_3fbytes or 0) + #this_line + #newline) + if (target_line == current_line) then + return this_line, (bytes - #this_line - 1) + elseif this_line then + return read_line_from_string(matcher, target_line, (current_line + 1), bytes) + else + return nil + end + end + local function read_line(filename, line, source) + if source then + return read_line_from_string(string.gmatch((source .. "\n"), "(.-)(\13?\n)"), line) + else + return read_line_from_file(filename, line) + end + end + 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, ""} + if (ok and codeline) then + table.insert(out, codeline) + else + end + if (ok and codeline and bytestart and byteend) then + table.insert(out, (string.rep(" ", (bytestart - bol - 1)) .. "^" .. string.rep("^", math.min((byteend - bytestart), ((bol + #codeline) - bytestart))))) + else + end + if (ok and codeline and bytestart and not byteend) then + table.insert(out, (string.rep("-", (bytestart - bol - 1)) .. "^")) + table.insert(out, "") + else + end + if suggestions0 then + for _, suggestion in ipairs(suggestions0) do + table.insert(out, ("* Try %s."):format(suggestion)) + end + else + end + return table.concat(out, "\n") + end + local function assert_compile(condition, msg, ast, source) + if not condition then + 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 + return condition + end + local function parse_error(msg, filename, line, bytestart, source) + return error(friendly_msg(("Parse error in %s:%s\n %s"):format(filename, line, msg), {filename = filename, line = line, bytestart = bytestart}, source), 0) + end + return {["assert-compile"] = assert_compile, ["parse-error"] = parse_error} +end +package.preload["fennel.parser"] = package.preload["fennel.parser"] or function(...) + local utils = require("fennel.utils") + local friend = require("fennel.friend") + local unpack = (table.unpack or _G.unpack) + local function granulate(getchunk) + local c, index, done_3f = "", 1, false + 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 _165_ = getchunk(parser_state) + local function _166_() + local char = _165_ + return (char ~= "") + end + if ((nil ~= _165_) and _166_()) then + local char = _165_ + c = char + index = 2 + return c:byte() + elseif true then + local _ = _165_ + done_3f = true + return nil + else + return nil + end + end + else + return nil + end + end + local function _170_() + c = "" + return nil + end + return _164_, _170_ + end + local function string_stream(str) + local str0 = str:gsub("^#!", ";;") + local index = 1 + local function _171_() + local r = str0:byte(index) + index = (index + 1) + return r + end + return _171_ + end + local delims = {[40] = 41, [41] = true, [91] = 93, [93] = true, [123] = 125, [125] = true} + local function whitespace_3f(b) + return ((b == 32) or ((b >= 9) and (b <= 13))) + end + local function sym_char_3f(b) + local b0 + if ("number" == type(b)) then + b0 = b + else + b0 = string.byte(b) + end + 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_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 + local lastb = nil + local function ungetb(ub) + if (ub == 10) then + line = (line - 1) + else + end + byteindex = (byteindex - 1) + lastb = ub + return nil + end + local function getb() + local r = nil + if lastb then + r, lastb = lastb, nil + else + r = getbyte({["stack-size"] = #stack}) + end + byteindex = (byteindex + 1) + if (r == 10) then + line = (line + 1) + else + end + return r + end + local function parse_error(msg, byteindex_override) + 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", filename, (line or "?"), msg), 0) + else + return friend["parse-error"](msg, filename, (line or "?"), (byteindex_override or byteindex), source) + end + else + return nil + end + end + local function parse_stream() + local whitespace_since_dispatch, done_3f, retval = true + local function dispatch(v) + local _180_ = stack[#stack] + if (_180_ == nil) then + retval, done_3f, whitespace_since_dispatch = v, true, false + return nil + elseif ((_G.type(_180_) == "table") and (nil ~= (_180_).prefix)) then + local prefix = (_180_).prefix + local source0 + do + local _181_ = table.remove(stack) + do end (_181_)["byteend"] = byteindex + source0 = _181_ + end + 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 ~= _180_) then + local top = _180_ + whitespace_since_dispatch = false + return table.insert(top, v) + else + return nil + end + end + local function badend() + local accum = utils.map(stack, "closer") + local _183_ + if (#stack == 1) then + _183_ = "" + else + _183_ = "s" + end + 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 + whitespace_since_dispatch = true + return skip_whitespace(getb()) + elseif (not b and (#stack > 0)) then + return badend() + else + return b + end + end + local function parse_comment(b, contents) + if (b and (10 ~= b)) then + local function _187_() + local _186_ = contents + table.insert(_186_, string.char(b)) + return _186_ + end + return parse_comment(getb(), _187_()) + elseif comments then + return dispatch(utils.comment(table.concat(contents), {line = (line - 1), filename = filename})) + else + return b + end + end + local function open_table(b) + if not whitespace_since_dispatch then + parse_error(("expected whitespace before opening delimiter " .. string.char(b))) + else + end + 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()))) + end + local function close_sequence(tbl) + local val = utils.sequence(unpack(tbl)) + for k, v in pairs(tbl) do + getmetatable(val)[k] = v + end + return dispatch(val) + end + 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 _ = _190_ + comments0[index] = {node} + return nil + else + return nil + end + end + local function next_noncomment(tbl, i) + if utils["comment?"](tbl[i]) then + return next_noncomment(tbl, (i + 1)) + else + return tbl[i] + end + end + local function extract_comments(tbl) + local comments0 = {keys = {}, values = {}, last = {}} + while utils["comment?"](tbl[#tbl]) do + 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(comments0.values, next_noncomment(tbl, i), node) + else + add_comment_at(comments0.keys, next_noncomment(tbl, i), node) + end + end + for i = #tbl, 1, -1 do + if utils["comment?"](tbl[i]) then + table.remove(tbl, i) + else + end + end + return comments0 + end + local function close_curly_table(tbl) + local comments0 = extract_comments(tbl) + local keys = {} + local val = {} + if ((#tbl % 2) ~= 0) then + byteindex = (byteindex - 1) + parse_error("expected even number of values in table literal") + else + end + setmetatable(val, tbl) + for i = 1, #tbl, 2 do + if ((tostring(tbl[i]) == ":") and utils["sym?"](tbl[(i + 1)]) and utils["sym?"](tbl[i])) then + tbl[i] = tostring(tbl[(i + 1)]) + else + end + val[tbl[i]] = tbl[(i + 1)] + table.insert(keys, tbl[i]) + end + tbl.comments = comments0 + tbl.keys = keys + return dispatch(val) + end + local function close_table(b) + local top = table.remove(stack) + if (top == nil) then + parse_error(("unexpected closing delimiter " .. string.char(b))) + else + end + if (top.closer and (top.closer ~= b)) then + parse_error(("mismatched closing delimiter " .. string.char(b) .. ", expected " .. string.char(top.closer))) + else + end + top.byteend = byteindex + if (b == 41) then + return close_list(top) + elseif (b == 93) then + return close_sequence(top) + else + return close_curly_table(top) + end + end + local function parse_string_loop(chars, b, state) + table.insert(chars, b) + local state0 + do + local _200_ = {state, b} + if ((_G.type(_200_) == "table") and ((_200_)[1] == "base") and ((_200_)[2] == 92)) then + state0 = "backslash" + elseif ((_G.type(_200_) == "table") and ((_200_)[1] == "base") and ((_200_)[2] == 34)) then + state0 = "done" + 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 _ = _200_ + state0 = "base" + else + state0 = nil + end + end + if (b and (state0 ~= "done")) then + return parse_string_loop(chars, getb(), state0) + else + return b + end + end + local function escape_char(c) + return ({[7] = "\\a", [8] = "\\b", [9] = "\\t", [10] = "\\n", [11] = "\\v", [12] = "\\f", [13] = "\\r"})[c:byte()] + end + local function parse_string() + table.insert(stack, {closer = 34}) + local chars = {34} + if not parse_string_loop(chars, getb(), "base") then + badend() + else + end + table.remove(stack) + local raw = string.char(unpack(chars)) + local formatted = raw:gsub("[\7-\13]", escape_char) + local _204_ = (rawget(_G, "loadstring") or load)(("return " .. formatted)) + if (nil ~= _204_) then + local load_fn = _204_ + return dispatch(load_fn()) + 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 = filename, line = line, bytestart = byteindex}) + local nextb = getb() + if (whitespace_3f(nextb) or (true == delims[nextb])) then + if (b ~= 35) then + parse_error("invalid whitespace after quoting prefix") + else + end + table.remove(stack) + dispatch(utils.sym("#")) + else + end + return ungetb(nextb) + end + local function parse_sym_loop(chars, b) + if (b and sym_char_3f(b)) then + table.insert(chars, b) + return parse_sym_loop(chars, getb()) + else + if b then + ungetb(b) + else + end + return chars + end + end + local function parse_number(rawstr) + local number_with_stripped_underscores = (not rawstr:find("^_") and rawstr:gsub("_", "")) + if rawstr:match("^%d") then + dispatch((tonumber(number_with_stripped_underscores) or parse_error(("could not read number \"" .. rawstr .. "\"")))) + return true + else + local _210_ = tonumber(number_with_stripped_underscores) + if (nil ~= _210_) then + local x = _210_ + dispatch(x) + return true + elseif true then + local _ = _210_ + return false + else + return nil + end + end + end + local function check_malformed_sym(rawstr) + if (rawstr:match("^~") and (rawstr ~= "~=")) then + 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 + return parse_error(("malformed multisym: " .. rawstr), ((byteindex - #rawstr) + 1 + rawstr:find("[%.:][%.:]"))) + elseif ((rawstr ~= ":") and rawstr:match(":$")) then + return parse_error(("malformed multisym: " .. rawstr), ((byteindex - #rawstr) + 1 + rawstr:find(":$"))) + elseif rawstr:match(":.+[%.:]") then + return parse_error(("method must be last component of multisym: " .. rawstr), ((byteindex - #rawstr) + rawstr:find(":.+[%.:]"))) + else + return rawstr + end + end + 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(source0)) + elseif rawstr:match("^:.+$") then + return dispatch(rawstr:sub(2)) + elseif not parse_number(rawstr) then + return dispatch(utils.sym(check_malformed_sym(rawstr), source0)) + else + return nil + end + end + local function parse_loop(b) + if not b then + elseif (b == 59) then + parse_comment(getb(), {";"}) + elseif (type(delims[b]) == "number") then + open_table(b) + elseif delims[b] then + close_table(b) + elseif (b == 34) then + parse_string(b) + elseif prefixes[b] then + parse_prefix(b) + 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(("invalid character: " .. string.char(b))) + else + end + if not b then + return nil + elseif done_3f then + return true, retval + else + return parse_loop(skip_whitespace(getb())) + end + end + return parse_loop(skip_whitespace(getb())) + end + local function _217_() + stack, line, byteindex, lastb = {}, 1, 0, nil + return nil + end + 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 +local utils +package.preload["fennel.view"] = package.preload["fennel.view"] or function(...) + local type_order = {number = 1, boolean = 2, string = 3, table = 4, ["function"] = 5, userdata = 6, thread = 7} + local lua_pairs = pairs + local lua_ipairs = ipairs + local function pairs(t) + local _1_ = getmetatable(t) + if ((_G.type(_1_) == "table") and (nil ~= (_1_).__pairs)) then + local p = (_1_).__pairs + return p(t) + elseif true then + local _ = _1_ + return lua_pairs(t) + else + return nil + end + end + local function ipairs(t) + local _3_ = getmetatable(t) + if ((_G.type(_3_) == "table") and (nil ~= (_3_).__ipairs)) then + local i = (_3_).__ipairs + return i(t) + elseif true then + local _ = _3_ + return lua_ipairs(t) + else + return nil + end + end + local function length_2a(t) + local _5_ = getmetatable(t) + if ((_G.type(_5_) == "table") and (nil ~= (_5_).__len)) then + local l = (_5_).__len + return l(t) + elseif true then + local _ = _5_ + return #t + else + return nil + end + end + local function sort_keys(_7_, _9_) + local _arg_8_ = _7_ + local a = _arg_8_[1] + local _arg_10_ = _9_ + local b = _arg_10_[1] + local ta = type(a) + local tb = type(b) + if ((ta == tb) and ((ta == "string") or (ta == "number"))) then + return (a < b) + else + local dta = type_order[ta] + local dtb = type_order[tb] + if (dta and dtb) then + return (dta < dtb) + elseif dta then + return true + elseif dtb then + return false + else + return (ta < tb) + end + end + end + local function max_index_gap(kv) + local gap = 0 + if (length_2a(kv) > 0) then + local i = 0 + for _, _13_ in ipairs(kv) do + local _each_14_ = _13_ + local k = _each_14_[1] + if ((k - i) > gap) then + gap = (k - i) + else + end + i = k + end + else + end + return gap + end + local function fill_gaps(kv) + local missing_indexes = {} + local i = 0 + for _, _17_ in ipairs(kv) do + local _each_18_ = _17_ + local j = _each_18_[1] + i = (i + 1) + while (i < j) do + table.insert(missing_indexes, i) + i = (i + 1) + end + end + for _, k in ipairs(missing_indexes) do + table.insert(kv, k, {k}) + end + return nil + end + local function table_kv_pairs(t, options) + local assoc_3f = false + local kv = {} + local insert = table.insert + for k, v in pairs(t) do + if ((type(k) ~= "number") or (k < 1)) then + assoc_3f = true + else + end + insert(kv, {k, v}) + end + table.sort(kv, sort_keys) + if not assoc_3f then + if (max_index_gap(kv) > options["max-sparse-gap"]) then + assoc_3f = true + else + fill_gaps(kv) + end + else + end + if (length_2a(kv) == 0) then + return kv, "empty" + else + local function _22_() + if assoc_3f then + return "table" + else + return "seq" + end + end + return kv, _22_() + end + end + local function count_table_appearances(t, appearances) + if (type(t) == "table") then + if not appearances[t] then + appearances[t] = 1 + for k, v in pairs(t) do + count_table_appearances(k, appearances) + count_table_appearances(v, appearances) + end + else + appearances[t] = ((appearances[t] or 0) + 1) + end + else + end + return appearances + end + local function save_table(t, seen) + local seen0 = (seen or {len = 0}) + local id = (seen0.len + 1) + if not (seen0)[t] then + seen0[t] = id + seen0.len = id + else + end + return seen0 + end + local function detect_cycle(t, seen, _3fk) + if ("table" == type(t)) then + seen[t] = true + local _27_, _28_ = next(t, _3fk) + if ((nil ~= _27_) and (nil ~= _28_)) then + local k = _27_ + local v = _28_ + return (seen[k] or detect_cycle(k, seen) or seen[v] or detect_cycle(v, seen) or detect_cycle(t, seen, k)) + else + return nil + end + else + return nil + end + end + local function visible_cycle_3f(t, options) + return (options["detect-cycles?"] and detect_cycle(t, {}) and save_table(t, options.seen) and (1 < (options.appearances[t] or 0))) + end + local function table_indent(indent, id) + local opener_length + if id then + opener_length = (length_2a(tostring(id)) + 2) + else + opener_length = 1 + end + return (indent + opener_length) + end + local pp = nil + local function concat_table_lines(elements, options, multiline_3f, indent, table_type, prefix) + local indent_str = ("\n" .. string.rep(" ", indent)) + local open + local function _32_() + if ("seq" == table_type) then + return "[" + else + return "{" + end + end + open = ((prefix or "") .. _32_()) + local close + if ("seq" == table_type) then + close = "]" + else + close = "}" + end + local oneline = (open .. table.concat(elements, " ") .. close) + if (not options["one-line?"] and (multiline_3f or ((indent + length_2a(oneline)) > options["line-length"]))) then + return (open .. table.concat(elements, indent_str) .. close) + else + return oneline + end + end + local function utf8_len(x) + local n = 0 + for _ in string.gmatch(x, "[%z\1-\127\192-\247]") do + n = (n + 1) + end + return n + end + local function pp_associative(t, kv, options, indent) + local multiline_3f = false + local id = options.seen[t] + if (options.level >= options.depth) then + return "{...}" + elseif (id and options["detect-cycles?"]) then + return ("@" .. id .. "{...}") + else + local visible_cycle_3f0 = visible_cycle_3f(t, options) + local id0 = (visible_cycle_3f0 and options.seen[t]) + local indent0 = table_indent(indent, id0) + local slength + if options["utf8?"] then + slength = utf8_len + else + local function _35_(_241) + return #_241 + end + slength = _35_ + end + local prefix + if visible_cycle_3f0 then + prefix = ("@" .. id0) + else + prefix = "" + end + local items + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for _, _38_ in pairs(kv) do + local _each_39_ = _38_ + local k = _each_39_[1] + local v = _each_39_[2] + local val_16_auto + do + local k0 = pp(k, options, (indent0 + 1), true) + local v0 = pp(v, options, (indent0 + slength(k0) + 1)) + multiline_3f = (multiline_3f or k0:find("\n") or v0:find("\n")) + val_16_auto = (k0 .. " " .. v0) + 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 + items = tbl_14_auto + end + return concat_table_lines(items, options, multiline_3f, indent0, "table", prefix) + end + end + local function pp_sequence(t, kv, options, indent) + local multiline_3f = false + local id = options.seen[t] + if (options.level >= options.depth) then + return "[...]" + elseif (id and options["detect-cycles?"]) then + return ("@" .. id .. "[...]") + else + local visible_cycle_3f0 = visible_cycle_3f(t, options) + local id0 = (visible_cycle_3f0 and options.seen[t]) + local indent0 = table_indent(indent, id0) + local prefix + if visible_cycle_3f0 then + prefix = ("@" .. id0) + else + prefix = "" + end + local items + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for _, _43_ in pairs(kv) do + local _each_44_ = _43_ + local _0 = _each_44_[1] + local v = _each_44_[2] + local val_16_auto + do + local v0 = pp(v, options, indent0) + multiline_3f = (multiline_3f or v0:find("\n")) + val_16_auto = v0 + 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 + items = tbl_14_auto + end + return concat_table_lines(items, options, multiline_3f, indent0, "seq", prefix) + end + end + local function concat_lines(lines, options, indent, force_multi_line_3f) + if (length_2a(lines) == 0) then + if options["empty-as-sequence?"] then + return "[]" + else + return "{}" + end + else + local oneline + local _48_ + do + local tbl_14_auto = {} + local i_15_auto = #tbl_14_auto + for _, line in ipairs(lines) do + local val_16_auto = line:gsub("^%s+", "") + 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 + _48_ = tbl_14_auto + end + oneline = table.concat(_48_, " ") + if (not options["one-line?"] and (force_multi_line_3f or oneline:find("\n") or ((indent + length_2a(oneline)) > options["line-length"]))) then + return table.concat(lines, ("\n" .. string.rep(" ", indent))) + else + return oneline + end + end + end + local function pp_metamethod(t, metamethod, options, indent) + if (options.level >= options.depth) then + if options["empty-as-sequence?"] then + return "[...]" + else + return "{...}" + end + else + local _ + local function _53_(_241) + return visible_cycle_3f(_241, options) + end + options["visible-cycle?"] = _53_ + _ = nil + local lines, force_multi_line_3f = metamethod(t, pp, options, indent) + options["visible-cycle?"] = nil + local _54_ = type(lines) + if (_54_ == "string") then + return lines + elseif (_54_ == "table") then + return concat_lines(lines, options, indent, force_multi_line_3f) + elseif true then + local _0 = _54_ + return error("__fennelview metamethod must return a table of lines") + else + return nil + end + end + end + local function pp_table(x, options, indent) + options.level = (options.level + 1) + local x0 + do + local _57_ + if options["metamethod?"] then + local _58_ = x + if (nil ~= _58_) then + local _59_ = getmetatable(_58_) + if (nil ~= _59_) then + _57_ = (_59_).__fennelview + else + _57_ = _59_ + end + else + _57_ = _58_ + end + else + _57_ = nil + end + if (nil ~= _57_) then + local metamethod = _57_ + x0 = pp_metamethod(x, metamethod, options, indent) + elseif true then + local _ = _57_ + local _63_, _64_ = table_kv_pairs(x, options) + if (true and (_64_ == "empty")) then + local _0 = _63_ + if options["empty-as-sequence?"] then + x0 = "[]" + else + x0 = "{}" + end + elseif ((nil ~= _63_) and (_64_ == "table")) then + local kv = _63_ + x0 = pp_associative(x, kv, options, indent) + elseif ((nil ~= _63_) and (_64_ == "seq")) then + local kv = _63_ + x0 = pp_sequence(x, kv, options, indent) + else + x0 = nil + end + else + x0 = nil + end + end + options.level = (options.level - 1) + return x0 + end + local function number__3estring(n) + local _68_ = string.gsub(tostring(n), ",", ".") + return _68_ + end + local function colon_string_3f(s) + return s:find("^[-%w?^_!$%&*+./@|<=>]+$") + end + local utf8_inits = {{["min-byte"] = 0, ["max-byte"] = 127, ["min-code"] = 0, ["max-code"] = 127, len = 1}, {["min-byte"] = 192, ["max-byte"] = 223, ["min-code"] = 128, ["max-code"] = 2047, len = 2}, {["min-byte"] = 224, ["max-byte"] = 239, ["min-code"] = 2048, ["max-code"] = 65535, len = 3}, {["min-byte"] = 240, ["max-byte"] = 247, ["min-code"] = 65536, ["max-code"] = 1114111, len = 4}} + local function utf8_escape(str) + local function validate_utf8(str0, index) + local inits = utf8_inits + local byte = string.byte(str0, index) + local init + do + local ret = nil + for _, init0 in ipairs(inits) do + if ret then break end + ret = (byte and (function(_69_,_70_,_71_) return (_69_ >= _70_) and (_70_ >= _71_) end)(init0["max-byte"],byte,init0["min-byte"]) and init0) + end + init = ret + end + local code + local function _72_() + local code0 + if init then + code0 = (byte - init["min-byte"]) + else + code0 = nil + end + for i = (index + 1), (index + init.len + -1) do + local byte0 = string.byte(str0, i) + code0 = (byte0 and code0 and (function(_74_,_75_,_76_) return (_74_ >= _75_) and (_75_ >= _76_) end)(191,byte0,128) and ((code0 * 64) + (byte0 - 128))) + end + return code0 + end + code = (init and _72_()) + if (code and (function(_77_,_78_,_79_) return (_77_ >= _78_) and (_78_ >= _79_) end)(init["max-code"],code,init["min-code"]) and not (function(_80_,_81_,_82_) return (_80_ >= _81_) and (_81_ >= _82_) end)(57343,code,55296)) then + return init.len + else + return nil + end + end + local index = 1 + local output = {} + while (index <= #str) do + local nexti = (string.find(str, "[\128-\255]", index) or (#str + 1)) + local len = validate_utf8(str, nexti) + table.insert(output, string.sub(str, index, (nexti + (len or 0) + -1))) + if (not len and (nexti <= #str)) then + table.insert(output, string.format("\\%03d", string.byte(str, nexti))) + else + end + if len then + index = (nexti + len) + else + index = (nexti + 1) + end + end + return table.concat(output) + end + local function pp_string(str, options, indent) + local escs + local _86_ + if (options["escape-newlines?"] and (length_2a(str) < (options["line-length"] - indent))) then + _86_ = "\\n" + else + _86_ = "\n" + end + local function _88_(_241, _242) + return ("\\%03d"):format(_242:byte()) + end + escs = setmetatable({["\7"] = "\\a", ["\8"] = "\\b", ["\12"] = "\\f", ["\11"] = "\\v", ["\13"] = "\\r", ["\9"] = "\\t", ["\\"] = "\\\\", ["\""] = "\\\"", ["\n"] = _86_}, {__index = _88_}) + local str0 = ("\"" .. str:gsub("[%c\\\"]", escs) .. "\"") + if options["utf8?"] then + return utf8_escape(str0) + else + return str0 + end + end + local function make_options(t, options) + local defaults = {["line-length"] = 80, ["one-line?"] = false, depth = 128, ["detect-cycles?"] = true, ["empty-as-sequence?"] = false, ["metamethod?"] = true, ["prefer-colon?"] = false, ["escape-newlines?"] = false, ["utf8?"] = true, ["max-sparse-gap"] = 10} + local overrides = {level = 0, appearances = count_table_appearances(t, {}), seen = {len = 0}} + for k, v in pairs((options or {})) do + defaults[k] = v + end + for k, v in pairs(overrides) do + defaults[k] = v + end + return defaults + end + local function _90_(x, options, indent, colon_3f) + local indent0 = (indent or 0) + local options0 = (options or make_options(x)) + local x0 + if options0.preprocess then + x0 = options0.preprocess(x, options0) + else + x0 = x + end + local tv = type(x0) + local function _93_() + local _92_ = getmetatable(x0) + if (nil ~= _92_) then + return (_92_).__fennelview + else + return _92_ + end + end + if ((tv == "table") or ((tv == "userdata") and _93_())) then + return pp_table(x0, options0, indent0) + elseif (tv == "number") then + return number__3estring(x0) + else + local function _95_() + if (colon_3f ~= nil) then + return colon_3f + elseif ("function" == type(options0["prefer-colon?"])) then + return options0["prefer-colon?"](x0) + else + return options0["prefer-colon?"] + end + end + if ((tv == "string") and colon_string_3f(x0) and _95_()) then + return (":" .. x0) + elseif (tv == "string") then + return pp_string(x0, options0, indent0) + elseif ((tv == "boolean") or (tv == "nil")) then + return tostring(x0) + else + return ("#<" .. tostring(x0) .. ">") + end + end + end + pp = _90_ + local function view(x, _3foptions) + return pp(x, make_options(x, _3foptions), 0) + end + return view +end +package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(...) + local view = require("fennel.view") + 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))) + else + return nil + end + end + local function stablepairs(t) + local keys = {} + local used_keys = {} + local succ = {} + if (getmetatable(t) and getmetatable(t).keys) then + for _, k in ipairs(getmetatable(t).keys) do + if used_keys[k] then + for i = #keys, 1, -1 do + if (keys[i] == k) then + table.remove(keys, i) + else + end + end + else + end + used_keys[k] = true + table.insert(keys, k) + end + else + for k in pairs(t) do + table.insert(keys, k) + end + local function _102_(_241, _242) + return (tostring(_241) < tostring(_242)) + end + table.sort(keys, _102_) + end + for i, k in ipairs(keys) do + succ[k] = keys[(i + 1)] + end + local function stablenext(tbl, idx) + local key + if (idx == nil) then + key = keys[1] + else + key = succ[idx] + end + local value + if (key == nil) then + value = nil + else + value = tbl[key] + end + return key, value + end + return stablenext, t, nil + end + local function map(t, f, _3fout) + local out = (_3fout or {}) + local f0 + if (type(f) == "function") then + f0 = f + else + local function _106_(_241) + return (_241)[f] + end + f0 = _106_ + end + for _, x in ipairs(t) do + local _108_ = f0(x) + if (nil ~= _108_) then + local v = _108_ + table.insert(out, v) + else + end + end + return out + end + local function kvmap(t, f, _3fout) + local out = (_3fout or {}) + local f0 + if (type(f) == "function") then + f0 = f + else + local function _110_(_241) + return (_241)[f] + end + f0 = _110_ + end + for k, x in stablepairs(t) do + local _112_, _113_ = f0(k, x) + if ((nil ~= _112_) and (nil ~= _113_)) then + local key = _112_ + local value = _113_ + out[key] = value + elseif (nil ~= _112_) then + local value = _112_ + table.insert(out, value) + else + end + end + return out + end + local function copy(from, _3fto) + local tbl_11_auto = (_3fto or {}) + for k, v in pairs((from or {})) do + 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 tbl_11_auto + end + local function member_3f(x, tbl, _3fn) + local _118_ = tbl[(_3fn or 1)] + if (_118_ == x) then + return true + elseif (_118_ == nil) then + return nil + elseif true then + local _ = _118_ + return member_3f(x, tbl, ((_3fn or 1) + 1)) + else + return nil + end + end + local function allpairs(tbl) + assert((type(tbl) == "table"), "allpairs expects a table") + local t = tbl + local seen = {} + local function allpairs_next(_, state) + local next_state, value = next(t, state) + if seen[next_state] then + return allpairs_next(nil, next_state) + elseif next_state then + seen[next_state] = true + return next_state, value + else + 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) + else + return nil + end + else + return nil + end + end + end + return allpairs_next + end + local function deref(self) + return self[1] + end + local nil_sym = nil + local function list__3estring(self, _3ftostring2) + local safe, max = {}, 0 + for k in pairs(self) do + if ((type(k) == "number") and (k > max)) then + max = k + else + end + end + for i = 1, max do + safe[i] = (((self[i] == nil) and nil_sym) or self[i]) + end + return ("(" .. table.concat(map(safe, (_3ftostring2 or view)), " ", 1, max) .. ")") + end + local function comment_view(c) + return c, true + end + local function sym_3d(a, b) + return ((deref(a) == deref(b)) and (getmetatable(a) == getmetatable(b))) + end + local function sym_3c(a, b) + return (a[1] < tostring(b)) + end + local symbol_mt = {__fennelview = deref, __tostring = deref, __eq = sym_3d, __lt = sym_3c, "SYMBOL"} + local expr_mt + local function _125_(x) + return tostring(deref(x)) + end + 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 varg_mt = {__fennelview = deref, __tostring = deref, "VARARG"} + local getenv + local function _126_() + return nil + end + 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)) + end + local function list(...) + return setmetatable({...}, list_mt) + end + 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(_127_, symbol_mt) + end + nil_sym = sym("nil") + local function sequence(...) + return setmetatable({...}, {sequence = sequence_marker}) + end + local function expr(strcode, etype) + return setmetatable({type = etype, strcode}, expr_mt) + end + local function comment_2a(contents, _3fsource) + 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(_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 ((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) + end + local function sym_3f(x) + return ((type(x) == "table") and (getmetatable(x) == symbol_mt) and x) + end + local function sequence_3f(x) + local mt = ((type(x) == "table") and getmetatable(x)) + return (mt and (mt.sequence == sequence_marker) and x) + end + local function comment_3f(x) + return ((type(x) == "table") and (getmetatable(x) == comment_mt) and x) + end + local function table_3f(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 + return multi_sym_3f(tostring(str)) + elseif (type(str) ~= "string") then + return false + else + local parts = {} + for part in str:gmatch("[^%.%:]+[%.%:]?") do + local last_char = part:sub(( - 1)) + if (last_char == ":") then + parts["multi-sym-method-call"] = true + else + end + if ((last_char == ":") or (last_char == ".")) then + parts[(#parts + 1)] = part:sub(1, ( - 2)) + else + parts[(#parts + 1)] = part + end + end + return ((#parts > 0) and (str:match("%.") or str:match(":")) and not str:match("%.%.") and (str:byte() ~= string.byte(".")) and (str:byte(( - 1)) ~= string.byte(".")) and parts) + end + end + local function quoted_3f(symbol) + return symbol.quoted + end + local function ast_source(ast) + if table_3f(ast) then + return (getmetatable(ast) or {}) + elseif ("table" == type(ast)) then + return ast + else + return {} + end + end + local function walk_tree(root, f, _3fcustom_iterator) + local function walk(iterfn, parent, idx, node) + if f(idx, node, parent) then + for k, v in iterfn(node) do + walk(iterfn, node, k, v) + end + return nil + else + return nil + end + end + walk((_3fcustom_iterator or pairs), nil, nil, root) + return root + end + local lua_keywords = {"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "goto"} + for i, v in ipairs(lua_keywords) do + lua_keywords[v] = i + end + local function valid_lua_identifier_3f(str) + return (str:match("^[%a_][%w_]*$") and not lua_keywords[str]) + end + local propagated_options = {"allowedGlobals", "indent", "correlate", "useMetadata", "env", "compiler-env", "compilerEnv"} + local function propagate_options(options, subopts) + for _, name in ipairs(propagated_options) do + subopts[name] = options[name] + end + return subopts + end + local root + local function _143_() + end + 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 + end + return root.reset + end + local warned = {} + 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)) + else + return nil + end + end + local function hook(event, ...) + local result = nil + if (root.options and root.options.plugins) then + for _, plugin in ipairs(root.options.plugins) do + if result then break end + check_plugin_version(plugin) + local _149_ = plugin[event] + if (nil ~= _149_) then + local f = _149_ + result = f(...) + else + end + end + else + 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, ["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") +local compiler = require("fennel.compiler") +local specials = require("fennel.specials") +local repl = require("fennel.repl") +local view = require("fennel.view") +local function eval_env(env, opts) + if (env == "_COMPILER") then + local env0 = specials["make-compiler-env"](nil, compiler.scopes.compiler, {}, opts) + if (opts.allowedGlobals == nil) then + opts.allowedGlobals = specials["current-global-names"](env0) + else + end + return specials["wrap-env"](env0) + else + return (env and specials["wrap-env"](env)) + end +end +local function eval_opts(options, str) + local opts = utils.copy(options) + if (opts.allowedGlobals == nil) then + opts.allowedGlobals = specials["current-global-names"](opts.env) + else + end + if (not opts.filename and not opts.source) then + opts.source = str + else + end + if (opts.env == "_COMPILER") then + opts.scope = compiler["make-scope"](compiler.scopes.compiler) + else + end + return opts +end +local function eval(str, options, ...) + local opts = eval_opts(options, str) + local env = eval_env(opts.env, opts) + local lua_source = compiler["compile-string"](str, opts) + local loader + local function _678_(...) + if opts.filename then + return ("@" .. opts.filename) + else + return str + end + end + loader = specials["load-code"](lua_source, env, _678_(...)) + opts.filename = nil + return loader(...) +end +local function dofile_2a(filename, options, ...) + local opts = utils.copy(options) + local f = assert(io.open(filename, "rb")) + local source = assert(f:read("*all"), ("Could not read " .. filename)) + f:close() + opts.filename = filename + return eval(source, opts, ...) +end +local function syntax() + local body_3f = {"when", "with-open", "collect", "icollect", "lambda", "\206\187", "macro", "match", "accumulate"} + local binding_3f = {"collect", "icollect", "each", "for", "let", "with-open", "accumulate"} + local define_3f = {"fn", "lambda", "\206\187", "var", "local", "macro", "macros", "global"} + local out = {} + for k, v in pairs(compiler.scopes.global.specials) do + local metadata = (compiler.metadata[v] or {}) + do end (out)[k] = {["special?"] = true, ["body-form?"] = metadata["fnl/body-form?"], ["binding-form?"] = utils["member?"](k, binding_3f), ["define?"] = utils["member?"](k, define_3f)} + end + for k, v in pairs(compiler.scopes.global.macros) do + 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 _679_ = type(v) + if (_679_ == "function") then + out[k] = {["global?"] = true, ["function?"] = true} + 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} + else + end + end + out[k] = {["global?"] = true} + else + end + 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, ["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 = [===[;; These macros are awkward because their definition cannot rely on the any + ;; built-in macros, only special forms. (no when, no icollect, etc) + + (fn copy [t] + (let [out []] + (each [_ v (ipairs t)] (table.insert out v)) + (setmetatable out (getmetatable t)))) + + (fn ->* [val ...] + "Thread-first macro. + Take the first value and splice it into the second form as its first argument. + The value of the second form is spliced into the first arg of the third, etc." + (var x val) + (each [_ e (ipairs [...])] + (let [elt (copy (if (list? e) e (list e)))] + (table.insert elt 2 x) + (set x elt))) + x) + + (fn ->>* [val ...] + "Thread-last macro. + Same as ->, except splices the value into the last position of each form + rather than the first." + (var x val) + (each [_ e (ipairs [...])] + (let [elt (copy (if (list? e) e (list e)))] + (table.insert elt x) + (set x elt))) + x) + + (fn -?>* [val ?e ...] + "Nil-safe thread-first macro. + Same as -> except will short-circuit with nil when it encounters a nil value." + (if (= nil ?e) + val + (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 ,...) + ,tmp))))) + + (fn -?>>* [val ?e ...] + "Nil-safe thread-last macro. + Same as ->> except will short-circuit with nil when it encounters a nil value." + (if (= nil ?e) + val + (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 ,...) + ,tmp))))) + + (fn ?dot [tbl ...] + "Nil-safe table look up. + Same as . (dot), except will short-circuit with nil when it encounters + a nil value in any of subsequent keys." + (let [head (gensym :t) + 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 ifs + (table.insert lookups (# lookups) `(if (not= nil ,head) + (set ,head (. ,head ,k))))) + lookups)) + + (fn doto* [val ...] + "Evaluate val and splice it into the first argument of subsequent forms." + (let [name (gensym) + form `(let [,name ,val])] + (each [_ elt (ipairs [...])] + (let [elt (copy (if (list? elt) elt (list elt)))] + (table.insert elt 2 name) + (table.insert form elt))) + (table.insert form name) + form)) + + (fn when* [condition body1 ...] + "Evaluate body for side-effects only when condition is truthy." + (assert body1 "expected body") + `(if ,condition + (do + ,body1 + ,...))) + + (fn with-open* [closable-bindings ...] + "Like `let`, but invokes (v:close) on each binding after evaluating the body. + The body is evaluated inside `xpcall` so that bound values will be closed upon + encountering an error before propagating it." + (let [bodyfn `(fn [] + ,...) + closer `(fn close-handlers# [ok# ...] + (if ok# ... (error ... 0))) + traceback `(. (or package.loaded.fennel debug) :traceback)] + (for [i 1 (length closable-bindings) 2] + (assert (sym? (. closable-bindings i)) + "with-open only allows symbols in bindings") + (table.insert closer 4 `(: ,(. closable-bindings i) :close))) + `(let ,closable-bindings + ,closer + (close-handlers# (_G.xpcall ,bodyfn ,traceback))))) + + (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 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") + (values into iter-out)) + + (fn collect* [iter-tbl key-expr value-expr ...] + "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. + + For example, + (collect [k v (pairs {:apple \"red\" :orange \"orange\"})] + (values v k)) + returns + {:red \"apple\" :orange \"orange\"} + + Supports an :into clause after the iterator to put results in an existing table. + Supports early termination with an :until clause." + (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 2)) + "expected iterator binding table") + (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)) + (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 ...] + "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 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))) + returns + [1 4 16 25] + + Supports an :into clause after the iterator to put results in an existing table. + Supports early termination with an :until clause." + (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 2)) + "expected iterator binding table") + (assert (not= nil value-expr) "expected table value expression") + (assert (= nil ...) + "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 body ...] + "Accumulation macro. + + 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 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" + (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 4)) + "expected initial value and iterator binding table") + (assert (not= nil body) "expected body expression") + (assert (= nil ...) + "expected exactly one body expression. Wrap multiple expressions with do") + (let [accum-var (. iter-tbl 1) + accum-init (. iter-tbl 2)] + `(do (var ,accum-var ,accum-init) + (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 ...] + "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 (double-eval-safe? arg (type arg)) + (table.insert args arg) + (let [name (gensym)] + (table.insert bindings name) + (table.insert bindings arg) + (table.insert args name)))) + (let [body (list f (unpack args))] + (table.insert body _VARARG) + ;; 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] + "Create a function of arity n that applies its arguments to f. + + For example, + (pick-args 2 func) + expands to + (fn [_0_ _1_] (func _0_ _1_))" + (if (and _G.io _G.io.stderr) + (_G.io.stderr:write + "-- WARNING: pick-args is deprecated and will be removed in the future.\n")) + (assert (and (= (type n) :number) (= n (math.floor n)) (>= n 0)) + (.. "Expected n to be an integer literal >= 0, got " (tostring n))) + (let [bindings []] + (for [i 1 n] + (tset bindings i (gensym))) + `(fn ,bindings + (,f ,(unpack bindings))))) + + (fn pick-values* [n ...] + "Evaluate to exactly n values. + + For example, + (pick-values 2 ...) + expands to + (let [(_0_ _1_) ...] + (values _0_ _1_))" + (assert (and (= :number (type n)) (>= n 0) (= n (math.floor n))) + (.. "Expected n to be an integer >= 0, got " (tostring n))) + (let [let-syms (list) + let-values (if (= 1 (select "#" ...)) ... `(values ,...))] + (for [i 1 n] + (table.insert let-syms (gensym))) + (if (= n 0) `(values) + `(let [,let-syms ,let-values] + (values ,(unpack let-syms)))))) + + (fn lambda* [...] + "Function literal with nil-checked arguments. + Like `fn`, but will throw an exception if a declared argument is passed in as + nil, unless that argument's name begins with a question mark." + (let [args [...] + has-internal-name? (sym? (. args 1)) + arglist (if has-internal-name? (. args 2) (. args 1)) + docstring-position (if has-internal-name? 3 2) + has-docstring? (and (> (length args) docstring-position) + (= :string (type (. args docstring-position)))) + arity-check-position (- 4 (if has-internal-name? 0 1) + (if has-docstring? 0 1)) + empty-body? (< (length args) arity-check-position)] + (fn check! [a] + (if (table? a) + (each [_ a (pairs a)] + (check! a)) + (let [as (tostring a)] + (and (not (as:match "^?")) (not= as "&") (not= as "_") + (not= as "...") (not= as "&as"))) + (table.insert args arity-check-position + `(_G.assert (not= nil ,a) + ,(: "Missing argument %s on %s:%s" :format + (tostring a) + (or a.filename :unknown) + (or a.line "?")))))) + + (assert (= :table (type arglist)) "expected arg list") + (each [_ a (ipairs arglist)] + (check! a)) + (if empty-body? + (table.insert args (sym :nil))) + `(fn ,(unpack args)))) + + (fn macro* [name ...] + "Define a single macro." + (assert (sym? name) "expected symbol for macro name") + (local args [...]) + `(macros {,(tostring name) (fn ,(unpack args))})) + + (fn macrodebug* [form return?] + "Print the resulting form after performing macroexpansion. + With a second argument, returns expanded form as a string instead of printing." + (let [handle (if return? `do `print)] + `(,handle ,(view (macroexpand form _SCOPE))))) + + (fn import-macros* [binding1 module-name1 ...] + "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 + {:macro1 alias : macro2} :proj.macros) ; import by name" + (assert (and binding1 module-name1 (= 0 (% (select "#" ...) 2))) + "expected even number of binding/modulename pairs") + (for [i 1 (select "#" binding1 module-name1 ...) 2] + ;; delegate the actual loading of the macros to the require-macros + ;; special which already knows how to set up the compiler env and stuff. + ;; this is weird because require-macros is deprecated but it works. + (let [(binding modname) (select i binding1 module-name1 ...) + scope (get-scope) + macros* (_SPECIALS.require-macros `(import-macros ,modname) + scope {} binding1)] + (if (sym? binding) + ;; bind whole table of macros to table bound to symbol + (tset scope.macros (. binding 1) macros*) + ;; 1-level table destructuring for importing individual macros + (table? binding) + (each [macro-name [import-key] (pairs binding)] + (assert (= :function (type (. macros* macro-name))) + (.. "macro " macro-name " not found in module " + (tostring modname))) + (tset scope.macros import-key (. macros* macro-name)))))) + nil) + + ;;; Pattern matching + + (fn match-values [vals pattern unifications match-pattern] + (let [condition `(and) + bindings []] + (each [i pat (ipairs pattern)] + (let [(subcondition subbindings) (match-pattern [(. vals i)] pat + unifications)] + (table.insert condition subcondition) + (each [_ b (ipairs subbindings)] + (table.insert bindings b)))) + (values condition bindings))) + + (fn match-table [val pattern unifications match-pattern] + (let [condition `(and (= (_G.type ,val) :table)) + bindings []] + (each [k pat (pairs pattern)] + (if (= pat `&) + (let [rest-pat (. pattern (+ k 1)) + rest-val `(select ,k ((or table.unpack _G.unpack) ,val)) + subcondition (match-table `(pick-values 1 ,rest-val) + rest-pat unifications match-pattern)] + (if (not (sym? rest-pat)) + (table.insert condition subcondition)) + (assert (= nil (. pattern (+ k 2))) + "expected & rest argument before last parameter") + (table.insert bindings rest-pat) + (table.insert bindings [rest-val])) + (= k `&as) + (do + (table.insert bindings pat) + (table.insert bindings val)) + (and (= :number (type k)) (= `&as pat)) + (do + (assert (= nil (. pattern (+ k 2))) + "expected &as argument before last parameter") + (table.insert bindings (. pattern (+ k 1))) + (table.insert bindings val)) + ;; don't process the pattern right after &/&as; already got it + (or (not= :number (type k)) (and (not= `&as (. pattern (- k 1))) + (not= `& (. pattern (- k 1))))) + (let [subval `(. ,val ,k) + (subcondition subbindings) (match-pattern [subval] pat + unifications)] + (table.insert condition subcondition) + (each [_ b (ipairs subbindings)] + (table.insert bindings b))))) + (values condition bindings))) + + (fn match-pattern [vals pattern unifications] + "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 + ;; know we're either in a multi-valued clause (in which case we know the # + ;; of vals) or we're not, in which case we only care about the first one. + (let [[val] vals] + (if (or (and (sym? pattern) ; unification with outer locals (or nil) + (not= "_" (tostring pattern)) ; never unify _ + (or (in-scope? pattern) (= :nil (tostring pattern)))) + (and (multi-sym? pattern) (in-scope? (. (multi-sym? pattern) 1)))) + (values `(= ,val ,pattern) []) + ;; unify a local we've seen already + (and (sym? pattern) (. unifications (tostring pattern))) + (values `(= ,(. unifications (tostring pattern)) ,val) []) + ;; bind a fresh local + (sym? pattern) + (let [wildcard? (: (tostring pattern) :find "^_")] + (if (not wildcard?) (tset unifications (tostring pattern) val)) + (values (if (or wildcard? (string.find (tostring pattern) "^?")) true + `(not= ,(sym :nil) ,val)) [pattern val])) + ;; guard clause + (and (list? pattern) (= (. pattern 2) `?)) + (let [(pcondition bindings) (match-pattern vals (. pattern 1) + unifications) + condition `(and ,(unpack pattern 3))] + (values `(and ,pcondition + (let ,bindings + ,condition)) bindings)) + ;; multi-valued patterns (represented as lists) + (list? pattern) + (match-values vals pattern unifications match-pattern) + ;; table patterns + (= (type pattern) :table) + (match-table val pattern unifications match-pattern) + ;; literal value + (values `(= ,val ,pattern) [])))) + + (fn match-condition [vals clauses] + "Construct the actual `if` AST for the given match values and clauses." + (if (not= 0 (% (length clauses) 2)) ; treat odd final clause as default + (table.insert clauses (length clauses) (sym "_"))) + (let [out `(if)] + (for [i 1 (length clauses) 2] + (let [pattern (. clauses i) + body (. clauses (+ i 1)) + (condition bindings) (match-pattern vals pattern {})] + (table.insert out condition) + (table.insert out `(let ,bindings + ,body)))) + out)) + + (fn match-val-syms [clauses] + "How many multi-valued clauses are there? return a list of that many gensyms." + (let [syms (list (gensym))] + (for [i 1 (length clauses) 2] + (let [clause (if (and (list? (. clauses i)) (= `? (. clauses i 2))) + (. clauses i 1) + (. clauses i))] + (if (list? clause) + (each [valnum (ipairs clause)] + (if (not (. syms valnum)) + (tset syms valnum (gensym))))))) + syms)) + + (fn match* [val ...] + ;; Old implementation of match macro, which doesn't directly support + ;; `where' and `or'. New syntax is implemented in `match-where', + ;; which simply generates old syntax and feeds it to `match*'. + (let [clauses [...] + vals (match-val-syms clauses)] + ;; 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)))) + + ;; Construction of old match syntax from new syntax + + (fn partition-2 [seq] + ;; Partition `seq` by 2. + ;; If `seq` has odd amount of elements, the last one is dropped. + ;; + ;; Input: [1 2 3 4 5] + ;; Output: [[1 2] [3 4]] + (let [firsts [] + seconds [] + res []] + (for [i 1 (length seq) 2] + (let [first (. seq i) + second (. seq (+ i 1))] + (table.insert firsts (if (not= nil first) first `nil)) + (table.insert seconds (if (not= nil second) second `nil)))) + (each [i v1 (ipairs firsts)] + (let [v2 (. seconds i)] + (if (not= nil v2) + (table.insert res [v1 v2])))) + res)) + + (fn transform-or [[_ & pats] guards] + ;; Transforms `(or pat pats*)` lists into match `guard` patterns. + ;; + ;; (or pat1 pat2), guard => [(pat1 ? guard) (pat2 ? guard)] + (let [res []] + (each [_ pat (ipairs pats)] + (table.insert res (list pat `? (unpack guards)))) + res)) + + (fn transform-cond [cond] + ;; Transforms `where` cond into sequence of `match` guards. + ;; + ;; pat => [pat] + ;; (where pat guard) => [(pat ? guard)] + ;; (where (or pat1 pat2) guard) => [(pat1 ? guard) (pat2 ? guard)] + (if (and (list? cond) (= (. cond 1) `where)) + (let [second (. cond 2)] + (if (and (list? second) (= (. second 1) `or)) + (transform-or second [(unpack cond 3)]) + :else + [(list second `? (unpack cond 3))])) + :else + [cond])) + + (fn match-where [val ...] + "Perform pattern matching on val. See reference for details. + + Syntax: + + (match data-expression + 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 [...]) + match-body []] + (each [_ [cond body] (ipairs conds-bodies)] + (each [_ cond (ipairs (transform-cond cond))] + (table.insert match-body cond) + (table.insert match-body body))) + (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)))) + + {:-> ->* + :->> ->>* + :-?> -?>* + :-?>> -?>>* + :?. ?dot + :doto doto* + :when when* + :with-open with-open* + :collect collect* + :icollect icollect* + :accumulate accumulate* + :partial partial* + :lambda lambda* + :pick-args pick-args* + :pick-values pick-values* + :macro macro* + :macrodebug macrodebug* + :import-macros import-macros* + :match match-where + :match-try match-try*} + ]===] + local module_name = "fennel.macros" + local _ + local function _682_() + return mod + end + package.preload[module_name] = _682_ + _ = nil + local env + do + 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 + compiler.scopes.global.macros[k] = v + end + compiler.scopes.global.macros["\206\187"] = compiler.scopes.global.macros.lambda + package.preload[module_name] = nil +end +return mod diff --git a/imgui.fnl b/imgui.fnl new file mode 100644 index 0000000..79365bf --- /dev/null +++ b/imgui.fnl @@ -0,0 +1,390 @@ +(local core (require :core)) +(local config (require :core.config)) +(local command (require :core.command)) +(local keymap (require :core.keymap)) +(local style (require :core.style)) +(local lume (require :plugins.fennel-xl.lume)) + +(fn attach-imstate [view] + (set view.imstate {}) + (fn view.on_mouse_pressed [self button x y clicks] + (tset self.imstate button :pressed) + (self.__index.on_mouse_pressed self button x y clicks)) + (fn view.on_mouse_released [self button x y] + (tset self.imstate button :released) + (self.__index.on_mouse_released self button x y)) + (fn view.on_key_pressed [self key] + (when (= self.imstate.keys nil) + (set self.imstate.keys [])) + (table.insert self.imstate.keys key)) + (fn view.on_text_input [self text] + (set self.imstate.text (.. (or self.imstate.text "") text)) + (self.__index.on_text_input self text)) + (fn view.form [self ?overrides] + (lume.merge {:x (+ self.position.x style.padding.x (- self.scroll.x)) + :y (+ self.position.y style.padding.y (- self.scroll.y)) + :w (- self.size.x (* style.padding.x 2)) + :view self} + (or ?overrides {}))) + (fn view.end-scroll [self {: y : h}] + (let [pin-to-bottom (>= self.scroll.to.y (- self.scrollheight self.size.y))] + (set self.scrollheight (- (+ y (or h 0) style.padding.y) (+ self.position.y style.padding.y (- self.scroll.y)))) + (when pin-to-bottom (set self.scroll.to.y (- self.scrollheight self.size.y))))) + (fn view.draw [self] + (set self.cursor nil) + (self.__index.draw self) + (when self.imstate.postponed + (each [_ action (ipairs self.imstate.postponed)] + (action)) + (set self.imstate.postponed nil)) + (when (= self.cursor nil) (set self.cursor :arrow)) + (set self.imstate.keys nil) + (set self.imstate.text nil) + (when (= self.imstate.left :released) + (set self.imstate.active nil)) + (each [_ button (pairs [:left :middle :right])] + (tset self.imstate button + (match (. self.imstate button) + :pressed :down + :down :down + :released nil))))) + +(fn register-keys [keys] + (local commands {}) + (local keymaps {}) + (each [_ key (ipairs keys)] + (local command-name (.. "imstate:" key)) + (tset commands command-name #(core.active_view:on_key_pressed key)) + (tset keymaps key command-name)) + (command.add #(not= (-?> core.active_view.imstate (. :focus)) nil) commands) + (keymap.add keymaps)) + +(register-keys [:backspace :delete :left :right :shift+left :shift+right :home :end :shift+home :shift+end + :ctrl+left :ctrl+right :ctrl+shift+left :ctrl+shift+right :ctrl+c :ctrl+v]) + +(fn cmd-predicate [p] + (var p-fn p) + (when (= (type p-fn) :string) (set p-fn (require p-fn))) + (when (= (type p-fn) :table) + (local cls p-fn) + (set p-fn (fn [] (core.active_view:is cls)))) + (fn [] (when (= (-?> core.active_view.imstate (. :focus)) nil) + (p-fn)))) + +(fn postpone [view f] + (when (= view.imstate.postponed nil) + (set view.imstate.postponed [])) + (table.insert view.imstate.postponed f)) + +(fn make-tag [tag] + (match (type tag) + :string tag + :table (table.concat tag "::") + _ (tostring tag))) + +(fn mouse-inside [x y w h] + (local (mx my) (values core.root_view.mouse.x core.root_view.mouse.y)) + (and (>= mx x) (<= mx (+ x w)) (>= my y) (<= my (+ y h)))) + +(fn consume-pressed [view button] + (when (= (. view.imstate button) :pressed) + (tset view.imstate button :down) + true)) + +(fn activate [{: view : tag : x : y : w : h}] + (when (and (mouse-inside x y w h) (consume-pressed view :left)) + (set view.imstate.active (make-tag tag)) + true)) + +(fn set-cursor [view cursor] + (when (= view.cursor nil) (set view.cursor cursor))) + +;; styling and layout +(fn form-defaults [form k v ...] + (when (= (. form k) nil) + (let [v (if (= (type v) :function) (v form) v)] + (tset form k v))) + (if (>= (select :# ...) 2) (form-defaults form ...) + (do (when form.tag (set form.tag (make-tag form.tag))) ; fix up tag + form))) + +(fn with-style [form ...] + (form-defaults form :font style.font :color style.text :xpad style.padding.x :ypad style.padding.y ...)) + +(local form-preserved-keys (collect [_ key (ipairs [:view :x :y :font :color :xpad :ypad])] key true)) +(fn reform [form overrides] + (if (and overrides overrides.into (not= overrides.into form)) + (reform (lume.extend (lume.clear overrides.into) form) overrides) + (do (each [key (pairs form)] + (when (= (. form-preserved-keys key) nil) + (tset form key nil))) + (lume.extend form (or overrides {}))))) + +(fn under [form overrides] (reform form (lume.merge (or overrides {}) {:y (+ form.y (or form.h 0) (or form.ypad 0))}))) +(fn right-of [form overrides] (reform form (lume.merge (or overrides {}) {:x (+ form.x (or form.w 0) (or form.xpad 0))}))) + +(fn group-wrapper [orig-form] + (let [group {} + update-dimension + (fn [form coord-key size-key] + (let [coord-group (. group coord-key) size-group (. group size-key) + coord-form (. form coord-key) size-form (. form size-key)] + (if (= size-form nil) ; tried to add an unsized value to the group, ignore + nil + + (= coord-group nil) ; container takes on the size of its first item + (do (tset group coord-key coord-form) + (tset group size-key size-form)) + + (> coord-group coord-form) ; we have an item that is outside the bounds to the left / up; reduce the starting point and extend the size + (do (tset group coord-key coord-form) + (tset group size-key (- (math.max (+ coord-form size-form) (+ coord-group size-group)) coord-form))) + + ; extend the size if the new item is outside the bounds to the right / down + (tset group size-key (- (math.max (+ coord-form size-form) (+ coord-group size-group)) coord-group))) + form)) + update-dimensions (fn [form] (update-dimension form :x :w) (update-dimension form :y :h))] + + (fn [?viewfn-or-form ?form ...] + (match [(type ?viewfn-or-form) ?viewfn-or-form] + [:function viewfn] (let [result [(viewfn ?form ...)]] + (update-dimensions ?form) + (table.unpack result)) + [:table form] (update-dimensions form) + [:nil] (lume.extend orig-form group))))) + +(fn horiz-wrapper [{:x orig-x :w orig-w}] + (fn [{: x : y : w : h : xpad : ypad &as form} overrides] + (if (> (+ x (or w 0) xpad (or w 0)) (+ orig-x orig-w)) + (reform form (lume.merge (or overrides {}) {:x orig-x :y (+ y (or h 0) (or ypad 0))})) + (right-of form overrides)))) + +;; widgets and widget helpers +(fn active? [view tag] (= view.imstate.active (make-tag tag))) +(fn button [{: view : tag : x : y : w : h &as form}] + (when (mouse-inside x y w h) (set-cursor view :hand)) + (activate form) + (and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h))) + +(fn label [form text] + (let [(_ newlines) (text:gsub "\n" "\n") + text-height (fn [font] (* (font:get_height) (+ newlines 1))) + {: x : y : w : h : halign : valign : font : color} + (with-style form + :w #($1.font:get_width text) + :h #(text-height $1.font) + :halign :left + :valign :center) + x (match halign :left x :center (+ x (/ (- w (font:get_width text)) 2)) :right (+ x w (- (font:get_width text)))) + y (match valign :top y :center (+ y (/ (- h (text-height font)) 2)) :bottom (+ y h (- (text-height font))))] + (renderer.draw_text font text x y color))) + +(fn textbutton [form label] + (let [{: x : y : w : h : xpad : ypad : font : color : bg} + (with-style form + :bg style.selection + :tag label + :w #(+ ($1.font:get_width label) $1.xpad) + :h #(+ ($1.font:get_height) $1.ypad))] + (renderer.draw_rect x y w h bg) + (renderer.draw_text font label (+ x (/ xpad 2)) (+ y (/ ypad 2)) color) + (button form))) + +(fn checkbox [form name isset] + (let [{: x : y : w : h : font : color : bg : x-label} + (with-style form + :bg style.background + :tag name + :h (* 12 SCALE) + :x-label #(+ $1.x $1.h $1.xpad) + :w #(+ $1.x-label ($1.font:get_width name)))] + (renderer.draw_rect x y h h color) + (when (not isset) (renderer.draw_rect (+ x 2) (+ y 2) (- h 4) (- h 4))) + (renderer.draw_text font name x-label y color) + (button form))) ; whose idea was this?? should return (not isset) >:/ + +(fn focused? [view tag] (= (make-tag tag) (-?> view.imstate.focus (. :tag)))) +(fn focus [{: view : tag : x : y : w : h &as form} opts] + (if (activate form) + (set view.imstate.focus + (doto (lume.clone (or opts {})) + (tset :tag (make-tag tag)))) + + (and (= view.imstate.left :released) (focused? view tag) (not (mouse-inside x y w h))) + (set view.imstate.focus nil)) + (focused? view tag)) + +(local blink_period 0.8) +(fn x-from-i [s i xLeft font] + (if (or (<= i 1) (= s "")) xLeft + (x-from-i (s:sub 2) (- i 1) (+ xLeft (font:get_width (s:sub 1 1))) font))) +(fn i-from-x [s x xLeft font ?i] + (local i (or ?i 1)) + (local w (font:get_width (s:sub 1 1))) + (local xMid (+ xLeft (/ w 2))) + (if (or (<= x xMid) (= s "")) i + (i-from-x (s:sub 2) x (+ xLeft w) font (+ i 1)))) + +(fn next-match [text i di pred] + (local imax (+ (length text) 1)) + (local inext (+ i di)) + (if (<= inext 1) 1 + (> inext imax) imax + (pred (text:sub inext inext)) (if (< di 0) i inext) + (next-match text inext di pred))) +(fn is-nonword-char [char] (config.non_word_chars:find char nil true)) +(fn next-word [text i di] + (let [iwordboundary (next-match text i di #(is-nonword-char $1))] + (next-match text iwordboundary di #(not (is-nonword-char $1))))) + +(fn textnav [key i text] + (local imax (+ (length text) 1)) + (match key + :left (math.max 1 (- i 1)) + :right (math.min imax (+ i 1)) + :ctrl+left (next-word text i -1) + :ctrl+right (next-word text i 1) + :home 1 + :end imax)) + +(fn selection-span [view] + (let [f view.imstate.focus + iStart (math.min f.i f.iAnchor) + iLim (math.max f.i f.iAnchor)] + (values iStart iLim))) +(fn selection-text [view text] + (local (iStart iLim) (selection-span view)) + (text:sub iStart (- iLim 1))) + +(fn replace-selection [view s replacement ?iStart ?iLim] + (local (iStart iLim) (if ?iLim (values ?iStart ?iLim) (selection-span view))) + (local text + (.. (s:sub 1 (- iStart 1)) + replacement + (s:sub iLim))) + (local iNew (+ iStart (length replacement))) + (set view.imstate.focus.i iNew) + (set view.imstate.focus.iAnchor iNew) + text) + +(fn textbox [form text] + (local {: font : color : w : h : x : y : xpad : ypad : color : bg : view : tag} + (with-style form :h #(+ ($1.font:get_height) $1.ypad) + :bg style.background)) + (var textNew (or text "")) + (local (hText xText yText) (values (font:get_height) (+ x (/ xpad 2)) (+ y (/ ypad 2)))) + (local initial-press (= view.imstate.left :pressed)) + + ; handle key events + (when (focus form {:i 1 :iAnchor 1 :blink (system.get_time)}) + (local f view.imstate.focus) + (when (> f.i (+ (length textNew) 1)) (set f.i (+ (length textNew) 1))) + (when (> f.iAnchor (+ (length textNew) 1)) (set f.iAnchor (+ (length textNew) 1))) + (when view.imstate.text + (set textNew (replace-selection view textNew view.imstate.text))) + (each [_ key (ipairs (or view.imstate.keys []))] + (set view.imstate.focus.blink (system.get_time)) + (if (= key :ctrl+c) (system.set_clipboard (selection-text view textNew)) + (= key :ctrl+v) (set textNew (replace-selection view textNew (system.get_clipboard))) + (key:find "shift%+") (set f.i (or (textnav (key:gsub "shift%+" "") f.i textNew) f.i)) + (let [iNav (textnav key f.i textNew)] + (when iNav + (set f.i iNav) + (set f.iAnchor iNav)) + (when (or (= key :delete) (= key :backspace)) + (local (iStartDel iLimDel) + (if (not= f.i f.iAnchor) (selection-span view) + (= key :delete) (values f.i (+ f.i 1)) + (= key :backspace) (values (math.max 1 (- f.i 1)) f.i))) + (set textNew (replace-selection view textNew "" iStartDel iLimDel))))))) + + ; handle mouse events + (when (mouse-inside x y w h) (set-cursor view :ibeam)) + (when (and (focused? view tag) (active? view tag) (mouse-inside x y w h)) + (local mouse-i (i-from-x textNew core.root_view.mouse.x x style.font)) + (when initial-press + (set view.imstate.focus.iAnchor mouse-i)) + (set view.imstate.focus.i mouse-i)) + + ; draw box + (renderer.draw_rect x y w h color) + (renderer.draw_rect (+ x 1) (+ y 1) (- w 2) (- h 2) bg) + (if (focused? view tag) + ; draw text with selection + caret + (let [(iStart iLim) (selection-span view) + xSelect (renderer.draw_text font (textNew:sub 1 (- iStart 1)) xText yText color) + sSelect (textNew:sub iStart (- iLim 1)) + wSelect (font:get_width sSelect) + xTail (+ xSelect wSelect)] + (when (> wSelect 0) + (renderer.draw_rect xSelect yText wSelect hText style.selection) + (renderer.draw_text font sSelect xSelect yText color)) + (renderer.draw_text font (textNew:sub iLim) xTail yText color) + (when (or (active? view tag) + (< (% (- (system.get_time) view.imstate.focus.blink) (* blink_period 2)) blink_period)) + (renderer.draw_rect (x-from-i textNew view.imstate.focus.i xText font) yText style.caret_width hText style.caret))) + ; just draw the text + (renderer.draw_text font textNew xText yText color)) + textNew) + +(fn textfield [form label text] + (let [{: x : y : w : wlabel : wtext : font : color} + (with-style form :wlabel #(+ ($1.font:get_width label) $1.xpad) + :wtext (* 150 SCALE) + :w #(+ $1.wlabel $1.wtext) + :tag label) + form-textbox (lume.merge form {:w wtext :x (+ x wlabel)}) + _ (renderer.draw_text font label x y color) + text (textbox form-textbox text)] + (set form.h form-textbox.h) + text)) + +(fn option-text [option] + (match (type option) + :string option + :table (or option.label (tostring option)) + _ (tostring option))) + +(fn dropdown [form selection options] + (let [{: x : y : w :h row-h : font : color : bg : xpad : ypad : view : tag} + (with-style form :w (* 150 SCALE) + :h #(+ ($1.font:get_height) $1.ypad) + :bg style.selection)] + (var new-selection nil) + + (renderer.draw_rect x y w row-h bg) + (renderer.draw_text style.font (option-text selection) (+ x xpad) (+ y (/ ypad 2)) color) + (renderer.draw_text style.icon_font "-" (+ x w (- xpad)) (+ y (/ ypad 2)) color) + + (when (focused? view tag) + (var row-y (+ y row-h)) + (each [i option (ipairs options)] + (when (button (lume.merge form {:tag [(make-tag tag) i] :y row-y})) + (set new-selection option)) + (set row-y (+ row-y row-h))) + (postpone view (fn [] + (var row-y (+ y row-h)) + (each [i option (ipairs options)] + (renderer.draw_rect x row-y w row-h bg) + (renderer.draw_text font (option-text option) (+ x xpad) (+ row-y (/ ypad 2)) color) + (set row-y (+ row-y row-h)))))) + (focus form) + (or new-selection selection))) + +(fn labelled-dropdown [form label selection options] + (let [{: x : y : wlabel : wdropdown : font : color} + (with-style form :wlabel #(+ ($1.font:get_width label) $1.xpad) + :wdropdown (* 150 SCALE) + :w #(+ $1.wlabel $1.wdropdown) + :tag label) + form-dropdown (lume.merge form {:x (+ x wlabel) :w wdropdown}) + _ (renderer.draw_text font label x y color) + selection (dropdown form-dropdown selection options)] + (set form.h form-dropdown.h) + selection)) + +{: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active? + : button : checkbox : textbox : textfield : textbutton : dropdown : labelled-dropdown : label + : reform : under : right-of : horiz-wrapper : group-wrapper + : with-style : form-defaults} + diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..63b1333 --- /dev/null +++ b/init.lua @@ -0,0 +1,8 @@ +-- mod-version:2 -- lite-xl 2.0 +local fennel = require "plugins.fennel-xl.fennel" +fennel.path = package.path:gsub("%.lua", ".fnl") +debug.traceback = fennel.traceback +table.insert(package.loaders or package.searchers, fennel.searcher) + +require "plugins.fennel-xl.editor" + diff --git a/inspector.fnl b/inspector.fnl new file mode 100644 index 0000000..7265de8 --- /dev/null +++ b/inspector.fnl @@ -0,0 +1,55 @@ +(local fennel (require :plugins.fennel-xl.fennel)) +(local util (require :plugins.fennel-xl.util)) +(local style (require :core.style)) +(local {: defmulti : defmethod} (util.require :plugins.fennel-xl.multimethod)) +(local {: textbutton : label : under : right-of : reform : group-wrapper } (util.require :plugins.fennel-xl.imgui)) + +(local inspector (util.hot-table ...)) + +(fn inspector.best-inspector [v] + (var best-inspector nil) + (var best-priority -1) + (each [inspector {: priority : predicate} (pairs inspector.inspectors)] + (when (and (> priority best-priority) (predicate v)) + (set best-inspector inspector) + (set best-priority priority))) + best-inspector) + +(set inspector.inspect + (defmulti (fn [form state value] + (when (= state.inspector nil) + (set state.inspector (inspector.best-inspector value))) + state.inspector) :inspect ...)) + +(fn inspector.register [name priority predicate inspect-func] + (when (= inspector.inspectors nil) + (set inspector.inspectors {})) + (tset inspector.inspectors name {: predicate : priority :inspector inspect-func}) + (defmethod inspector.inspect name inspect-func)) + +(inspector.register :default 0 #true (fn [form state value] + (label (reform form {:font style.code_font}) (fennel.view value)))) + +(inspector.register :table 10 + #(and (= (type $1) :table) (not= (next $1) nil)) + (fn [form state tbl] + (let [get-kstate (fn [tbl k state] + (when (= nil state.keys) (set state.keys {})) + (when (= nil (?. state.keys k)) + (util.nested-tset state [:keys k] {:collapsed (= (type (. tbl k)) :table) :children {}})) + (. state.keys k)) + g (group-wrapper form)] + (each [k v (pairs tbl)] + (let [kstate (get-kstate tbl k state)] + ; todo: state assumes an .inspector key + ; todo: inspector swapping + ; todo: edit in place? + (when (g textbutton (under form {:font style.code_font}) (fennel.view k)) + (set kstate.collapsed (not kstate.collapsed))) + (if kstate.collapsed + (g label (right-of form {:color style.syntax.comment :into {}}) "...") + (g #(inspector.inspect $...) (right-of form {:into {}}) kstate.children v)) + (g)))))) + +inspector.hot + diff --git a/localrepl.fnl b/localrepl.fnl new file mode 100644 index 0000000..10e4444 --- /dev/null +++ b/localrepl.fnl @@ -0,0 +1,32 @@ +(local fennel (require :plugins.fennel-xl.fennel)) +(local lume (require :plugins.fennel-xl.lume)) +(local util (require :plugins.fennel-xl.util)) +(local {: defmethod} (require :plugins.fennel-xl.multimethod)) +(local {: submit : restart} (require :plugins.fennel-xl.replsession)) +(local core (require :core)) + +(local localrepl (util.hot-table ...)) + +(fn localrepl.session-run [session] + (fennel.repl {:readChunk coroutine.yield + ; todo: log errors? + :onValues #(pcall session.callback {:vals $1}) + :onError #(pcall session.callback {:errType $1 :err $2 :luaSource $3 :traceback (fennel.traceback)}) + :pp #$1 + :env (lume.clone _G)})) + +(defmethod restart :local (fn [session] + (set session.coro (coroutine.create localrepl.session-run)) + (coroutine.resume session.coro session) + session)) + +(defmethod submit :local (fn [session chunk callback ?suppress-crash] + (assert (= session.callback nil)) + (set session.callback callback) + (match (pcall coroutine.resume session.coro (.. chunk "\n")) + (false err) (do (when (not ?suppress-crash) (core.log (.. "REPL crashed: " err))) + (restart session))) + (assert (= session.callback callback)) + (set session.callback nil))) + +localrepl.hot diff --git a/lume.lua b/lume.lua new file mode 100644 index 0000000..c837216 --- /dev/null +++ b/lume.lua @@ -0,0 +1,780 @@ +-- +-- lume +-- +-- Copyright (c) 2020 rxi +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy of +-- this software and associated documentation files (the "Software"), to deal in +-- the Software without restriction, including without limitation the rights to +-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +-- of the Software, and to permit persons to whom the Software is furnished to do +-- so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. +-- + +local lume = { _version = "2.3.0" } + +local pairs, ipairs = pairs, ipairs +local type, assert, unpack = type, assert, rawget(_G, "unpack") or table.unpack +local tostring, tonumber = tostring, tonumber +local math_floor = math.floor +local math_ceil = math.ceil +local math_atan2 = math.atan2 or math.atan +local math_sqrt = math.sqrt +local math_abs = math.abs + +local noop = function() +end + +local identity = function(x) + return x +end + +local patternescape = function(str) + return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1") +end + +local absindex = function(len, i) + return i < 0 and (len + i + 1) or i +end + +local iscallable = function(x) + if type(x) == "function" then return true end + local mt = getmetatable(x) + return mt and mt.__call ~= nil +end + +local getiter = function(x) + if lume.isarray(x) then + return ipairs + elseif type(x) == "table" then + return pairs + end + error("expected table", 3) +end + +local iteratee = function(x) + if x == nil then return identity end + if iscallable(x) then return x end + if type(x) == "table" then + return function(z) + for k, v in pairs(x) do + if z[k] ~= v then return false end + end + return true + end + end + return function(z) return z[x] end +end + + + +function lume.clamp(x, min, max) + return x < min and min or (x > max and max or x) +end + + +function lume.round(x, increment) + if increment then return lume.round(x / increment) * increment end + return x >= 0 and math_floor(x + .5) or math_ceil(x - .5) +end + + +function lume.sign(x) + return x < 0 and -1 or 1 +end + + +function lume.lerp(a, b, amount) + return a + (b - a) * lume.clamp(amount, 0, 1) +end + + +function lume.smooth(a, b, amount) + local t = lume.clamp(amount, 0, 1) + local m = t * t * (3 - 2 * t) + return a + (b - a) * m +end + + +function lume.pingpong(x) + return 1 - math_abs(1 - x % 2) +end + + +function lume.distance(x1, y1, x2, y2, squared) + local dx = x1 - x2 + local dy = y1 - y2 + local s = dx * dx + dy * dy + return squared and s or math_sqrt(s) +end + + +function lume.angle(x1, y1, x2, y2) + return math_atan2(y2 - y1, x2 - x1) +end + + +function lume.vector(angle, magnitude) + return math.cos(angle) * magnitude, math.sin(angle) * magnitude +end + + +function lume.random(a, b) + if not a then a, b = 0, 1 end + if not b then b = 0 end + return a + math.random() * (b - a) +end + + +function lume.randomchoice(t) + return t[math.random(#t)] +end + + +function lume.weightedchoice(t) + local sum = 0 + for _, v in pairs(t) do + assert(v >= 0, "weight value less than zero") + sum = sum + v + end + assert(sum ~= 0, "all weights are zero") + local rnd = lume.random(sum) + for k, v in pairs(t) do + if rnd < v then return k end + rnd = rnd - v + end +end + + +function lume.isarray(x) + return type(x) == "table" and x[1] ~= nil +end + + +function lume.push(t, ...) + local n = select("#", ...) + for i = 1, n do + t[#t + 1] = select(i, ...) + end + return ... +end + + +function lume.remove(t, x) + local iter = getiter(t) + for i, v in iter(t) do + if v == x then + if lume.isarray(t) then + table.remove(t, i) + break + else + t[i] = nil + break + end + end + end + return x +end + + +function lume.clear(t) + local iter = getiter(t) + for k in iter(t) do + t[k] = nil + end + return t +end + + +function lume.extend(t, ...) + for i = 1, select("#", ...) do + local x = select(i, ...) + if x then + for k, v in pairs(x) do + t[k] = v + end + end + end + return t +end + + +function lume.shuffle(t) + local rtn = {} + for i = 1, #t do + local r = math.random(i) + if r ~= i then + rtn[i] = rtn[r] + end + rtn[r] = t[i] + end + return rtn +end + + +function lume.sort(t, comp) + local rtn = lume.clone(t) + if comp then + if type(comp) == "string" then + table.sort(rtn, function(a, b) return a[comp] < b[comp] end) + else + table.sort(rtn, comp) + end + else + table.sort(rtn) + end + return rtn +end + + +function lume.array(...) + local t = {} + for x in ... do t[#t + 1] = x end + return t +end + + +function lume.each(t, fn, ...) + local iter = getiter(t) + if type(fn) == "string" then + for _, v in iter(t) do v[fn](v, ...) end + else + for _, v in iter(t) do fn(v, ...) end + end + return t +end + + +function lume.map(t, fn) + fn = iteratee(fn) + local iter = getiter(t) + local rtn = {} + for k, v in iter(t) do rtn[k] = fn(v) end + return rtn +end + + +function lume.all(t, fn) + fn = iteratee(fn) + local iter = getiter(t) + for _, v in iter(t) do + if not fn(v) then return false end + end + return true +end + + +function lume.any(t, fn) + fn = iteratee(fn) + local iter = getiter(t) + for _, v in iter(t) do + if fn(v) then return true end + end + return false +end + + +function lume.reduce(t, fn, first) + local started = first ~= nil + local acc = first + local iter = getiter(t) + for _, v in iter(t) do + if started then + acc = fn(acc, v) + else + acc = v + started = true + end + end + assert(started, "reduce of an empty table with no first value") + return acc +end + + +function lume.unique(t) + local rtn = {} + for k in pairs(lume.invert(t)) do + rtn[#rtn + 1] = k + end + return rtn +end + + +function lume.filter(t, fn, retainkeys) + fn = iteratee(fn) + local iter = getiter(t) + local rtn = {} + if retainkeys then + for k, v in iter(t) do + if fn(v) then rtn[k] = v end + end + else + for _, v in iter(t) do + if fn(v) then rtn[#rtn + 1] = v end + end + end + return rtn +end + + +function lume.reject(t, fn, retainkeys) + fn = iteratee(fn) + local iter = getiter(t) + local rtn = {} + if retainkeys then + for k, v in iter(t) do + if not fn(v) then rtn[k] = v end + end + else + for _, v in iter(t) do + if not fn(v) then rtn[#rtn + 1] = v end + end + end + return rtn +end + + +function lume.merge(...) + local rtn = {} + for i = 1, select("#", ...) do + local t = select(i, ...) + local iter = getiter(t) + for k, v in iter(t) do + rtn[k] = v + end + end + return rtn +end + + +function lume.concat(...) + local rtn = {} + for i = 1, select("#", ...) do + local t = select(i, ...) + if t ~= nil then + local iter = getiter(t) + for _, v in iter(t) do + rtn[#rtn + 1] = v + end + end + end + return rtn +end + + +function lume.find(t, value) + local iter = getiter(t) + for k, v in iter(t) do + if v == value then return k end + end + return nil +end + + +function lume.match(t, fn) + fn = iteratee(fn) + local iter = getiter(t) + for k, v in iter(t) do + if fn(v) then return v, k end + end + return nil +end + + +function lume.count(t, fn) + local count = 0 + local iter = getiter(t) + if fn then + fn = iteratee(fn) + for _, v in iter(t) do + if fn(v) then count = count + 1 end + end + else + if lume.isarray(t) then + return #t + end + for _ in iter(t) do count = count + 1 end + end + return count +end + + +function lume.slice(t, i, j) + i = i and absindex(#t, i) or 1 + j = j and absindex(#t, j) or #t + local rtn = {} + for x = i < 1 and 1 or i, j > #t and #t or j do + rtn[#rtn + 1] = t[x] + end + return rtn +end + + +function lume.first(t, n) + if not n then return t[1] end + return lume.slice(t, 1, n) +end + + +function lume.last(t, n) + if not n then return t[#t] end + return lume.slice(t, -n, -1) +end + + +function lume.invert(t) + local rtn = {} + for k, v in pairs(t) do rtn[v] = k end + return rtn +end + + +function lume.pick(t, ...) + local rtn = {} + for i = 1, select("#", ...) do + local k = select(i, ...) + rtn[k] = t[k] + end + return rtn +end + + +function lume.keys(t) + local rtn = {} + local iter = getiter(t) + for k in iter(t) do rtn[#rtn + 1] = k end + return rtn +end + + +function lume.clone(t) + local rtn = {} + for k, v in pairs(t) do rtn[k] = v end + return rtn +end + + +function lume.fn(fn, ...) + assert(iscallable(fn), "expected a function as the first argument") + local args = { ... } + return function(...) + local a = lume.concat(args, { ... }) + return fn(unpack(a)) + end +end + + +function lume.once(fn, ...) + local f = lume.fn(fn, ...) + local done = false + return function(...) + if done then return end + done = true + return f(...) + end +end + + +local memoize_fnkey = {} +local memoize_nil = {} + +function lume.memoize(fn) + local cache = {} + return function(...) + local c = cache + for i = 1, select("#", ...) do + local a = select(i, ...) or memoize_nil + c[a] = c[a] or {} + c = c[a] + end + c[memoize_fnkey] = c[memoize_fnkey] or {fn(...)} + return unpack(c[memoize_fnkey]) + end +end + + +function lume.combine(...) + local n = select('#', ...) + if n == 0 then return noop end + if n == 1 then + local fn = select(1, ...) + if not fn then return noop end + assert(iscallable(fn), "expected a function or nil") + return fn + end + local funcs = {} + for i = 1, n do + local fn = select(i, ...) + if fn ~= nil then + assert(iscallable(fn), "expected a function or nil") + funcs[#funcs + 1] = fn + end + end + return function(...) + for _, f in ipairs(funcs) do f(...) end + end +end + + +function lume.call(fn, ...) + if fn then + return fn(...) + end +end + + +function lume.time(fn, ...) + local start = os.clock() + local rtn = {fn(...)} + return (os.clock() - start), unpack(rtn) +end + + +local lambda_cache = {} + +function lume.lambda(str) + if not lambda_cache[str] then + local args, body = str:match([[^([%w,_ ]-)%->(.-)$]]) + assert(args and body, "bad string lambda") + local s = "return function(" .. args .. ")\nreturn " .. body .. "\nend" + lambda_cache[str] = lume.dostring(s) + end + return lambda_cache[str] +end + + +local serialize + +local serialize_map = { + [ "boolean" ] = tostring, + [ "nil" ] = tostring, + [ "string" ] = function(v) return string.format("%q", v) end, + [ "number" ] = function(v) + if v ~= v then return "0/0" -- nan + elseif v == 1 / 0 then return "1/0" -- inf + elseif v == -1 / 0 then return "-1/0" end -- -inf + return tostring(v) + end, + [ "table" ] = function(t, stk) + stk = stk or {} + if stk[t] then error("circular reference") end + local rtn = {} + stk[t] = true + for k, v in pairs(t) do + rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk) + end + stk[t] = nil + return "{" .. table.concat(rtn, ",") .. "}" + end +} + +setmetatable(serialize_map, { + __index = function(_, k) error("unsupported serialize type: " .. k) end +}) + +serialize = function(x, stk) + return serialize_map[type(x)](x, stk) +end + +function lume.serialize(x) + return serialize(x) +end + + +function lume.deserialize(str) + return lume.dostring("return " .. str) +end + + +function lume.split(str, sep) + if not sep then + return lume.array(str:gmatch("([%S]+)")) + else + assert(sep ~= "", "empty separator") + local psep = patternescape(sep) + return lume.array((str..sep):gmatch("(.-)("..psep..")")) + end +end + + +function lume.trim(str, chars) + if not chars then return str:match("^[%s]*(.-)[%s]*$") end + chars = patternescape(chars) + return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$") +end + + +function lume.wordwrap(str, limit) + limit = limit or 72 + local check + if type(limit) == "number" then + check = function(s) return #s >= limit end + else + check = limit + end + local rtn = {} + local line = "" + for word, spaces in str:gmatch("(%S+)(%s*)") do + local s = line .. word + if check(s) then + table.insert(rtn, line .. "\n") + line = word + else + line = s + end + for c in spaces:gmatch(".") do + if c == "\n" then + table.insert(rtn, line .. "\n") + line = "" + else + line = line .. c + end + end + end + table.insert(rtn, line) + return table.concat(rtn) +end + + +function lume.format(str, vars) + if not vars then return str end + local f = function(x) + return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}") + end + return (str:gsub("{(.-)}", f)) +end + + +function lume.trace(...) + local info = debug.getinfo(2, "Sl") + local t = { info.short_src .. ":" .. info.currentline .. ":" } + for i = 1, select("#", ...) do + local x = select(i, ...) + if type(x) == "number" then + x = string.format("%g", lume.round(x, .01)) + end + t[#t + 1] = tostring(x) + end + print(table.concat(t, " ")) +end + + +function lume.dostring(str) + return assert((loadstring or load)(str))() +end + + +function lume.uuid() + local fn = function(x) + local r = math.random(16) - 1 + r = (x == "x") and (r + 1) or (r % 4) + 9 + return ("0123456789abcdef"):sub(r, r) + end + return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn)) +end + + +function lume.hotswap(modname) + local oldglobal = lume.clone(_G) + local updated = {} + local function update(old, new) + if updated[old] then return end + updated[old] = true + local oldmt, newmt = getmetatable(old), getmetatable(new) + if oldmt and newmt then update(oldmt, newmt) end + for k, v in pairs(new) do + if type(v) == "table" then update(old[k], v) else old[k] = v end + end + end + local err = nil + local function onerror(e) + for k in pairs(_G) do _G[k] = oldglobal[k] end + err = lume.trim(e) + end + local ok, oldmod = pcall(require, modname) + oldmod = ok and oldmod or nil + xpcall(function() + package.loaded[modname] = nil + local newmod = require(modname) + if type(oldmod) == "table" then update(oldmod, newmod) end + for k, v in pairs(oldglobal) do + if v ~= _G[k] and type(v) == "table" then + update(v, _G[k]) + _G[k] = v + end + end + end, onerror) + package.loaded[modname] = oldmod + if err then return nil, err end + return oldmod +end + + +local ripairs_iter = function(t, i) + i = i - 1 + local v = t[i] + if v ~= nil then + return i, v + end +end + +function lume.ripairs(t) + return ripairs_iter, t, (#t + 1) +end + + +function lume.color(str, mul) + mul = mul or 1 + local r, g, b, a + r, g, b = str:match("#(%x%x)(%x%x)(%x%x)") + if r then + r = tonumber(r, 16) / 0xff + g = tonumber(g, 16) / 0xff + b = tonumber(b, 16) / 0xff + a = 1 + elseif str:match("rgba?%s*%([%d%s%.,]+%)") then + local f = str:gmatch("[%d.]+") + r = (f() or 0) / 0xff + g = (f() or 0) / 0xff + b = (f() or 0) / 0xff + a = f() or 1 + else + error(("bad color string '%s'"):format(str)) + end + return r * mul, g * mul, b * mul, a * mul +end + + +local chain_mt = {} +chain_mt.__index = lume.map(lume.filter(lume, iscallable, true), + function(fn) + return function(self, ...) + self._value = fn(self._value, ...) + return self + end + end) +chain_mt.__index.result = function(x) return x._value end + +function lume.chain(value) + return setmetatable({ _value = value }, chain_mt) +end + +setmetatable(lume, { + __call = function(_, ...) + return lume.chain(...) + end +}) + + +return lume diff --git a/multimethod.fnl b/multimethod.fnl new file mode 100644 index 0000000..f108a83 --- /dev/null +++ b/multimethod.fnl @@ -0,0 +1,21 @@ +(local util (require :plugins.fennel-xl.util)) + +(local mm {}) + +(fn mm.__call [{: module : name} ...] + (let [dispatcher (. mm.dispatchers module name) + key (dispatcher ...) + method (or (. mm.methods module name key) (. mm.methods module name :default))] + (method ...))) + +(fn mm.defmulti [dispatcher name module] + (util.nested-tset mm [:dispatchers module name] dispatcher) + (setmetatable {: module : name} mm)) + +(fn mm.defmethod [{: module : name} key method] + (util.nested-tset mm [:methods module name key] method)) + +(fn mm.keys [{: module : name}] + (icollect [key _ (pairs (or (?. mm :methods module name) []))] key)) + +mm diff --git a/nrepl-session.fnl b/nrepl-session.fnl new file mode 100644 index 0000000..f5d9e5c --- /dev/null +++ b/nrepl-session.fnl @@ -0,0 +1,93 @@ +(local Object (require :core.object)) +(local nrepl (require :plugins.fennel-xl.nrepl)) +(local lume (require :plugins.fennel-xl.lume)) +(local {: defmethod} (require :plugins.fennel-xl.multimethod)) +(local {: restart : submit} (require :plugins.fennel-xl.replsession)) + +(local Session (Object:extend)) + +(fn Session.new [self ?handlers] + (set self.queue []) + (set self.in-progress false) + (set self.handlers ?handlers)) + +(fn Session.init-session [self] + (when (nrepl:connected?) + (self:do #(nrepl:new-session + #(do (set self.session $2) + (self:done-msg)) + (self:make-handlers))))) + +(fn Session.shutdown-session [self] + (set self.queue []) + (set self.in-progress false) + (set self.session nil)) + +(fn Session.cleanup-handlers [self] + {:status/done #(self:done-msg) + :status/interrupted #(self:done-msg)}) + +(fn Session.make-handlers [self] + (lume.merge + (or self.handlers {}) + (nrepl:chain-handlers [:status/done :status/interrupted] + (or self.handlers {}) + (self:cleanup-handlers)))) + +(fn Session.coro-handlers [self coro ?handlers] + (lume.merge + (or ?handlers {}) + (nrepl:chain-handlers [:status/done :status/interrupted] + (self:cleanup-handlers) + {:status/done #(coroutine.resume coro) + :status/interrupted #(coroutine.resume coro)}))) + +(fn Session.do [self f] + (if self.in-progress (table.insert self.queue f) + (do (set self.in-progress true) + (f)))) + +(fn Session.done-msg [self] + (if (> (length self.queue) 0) ((table.remove self.queue 1)) + (set self.in-progress false))) + +(fn Session.send [self message ?handlers] + (self:do #(nrepl:send message ?handlers self.session))) + +(fn Session.send-oob [self message ?handlers] + (local handlers + (lume.merge + (nrepl:chain-handlers [:status/done :status/interrupted] + (or self.handlers {})) + (or ?handlers {}))) + (nrepl:send message handlers self.session)) + +(fn Session.eval [self code ?handlers] + (self:send {:op :eval : code} ?handlers)) + +(fn Session.input-handler [self input] + {:status/need-input #(self:send-oob {:op :stdin :stdin input})}) + +(fn Session.eval-input [self code input ?handlers] + (self:send {:op :eval : code} + (lume.merge (or ?handlers {}) (self:input-handler input)))) + +(local fennel (require :plugins.fennel-xl.fennel)) +(fn parse-vals [s] + (let [parser (fennel.parser s)] + (icollect [ok ast (fennel.parser s)] (if ok ast s)))) + +(defmethod restart :nrepl (fn [session] + (when (not (nrepl:connected?)) (error "Must connect nrepl")) + (when session.conn (session.conn:shutdown-session)) + (set session.conn (Session)) + (session.conn:init-session) + session)) + +(defmethod submit :nrepl (fn [session chunk callback ?suppress-crash] + (session.conn:eval chunk + {:out #(callback {:vals [$2]}) + :value #(callback {:vals (parse-vals $2)}) + :ex #(callback {:err $2})}))) + +Session diff --git a/nrepl.fnl b/nrepl.fnl new file mode 100644 index 0000000..8223ace --- /dev/null +++ b/nrepl.fnl @@ -0,0 +1,111 @@ +(local core (require :core)) +(local socket (require :socket)) +; (local socket {:connect #nil}) +(local bencode (require :plugins.fennel-xl.bencode)) +(local lume (require :plugins.fennel-xl.lume)) + +(fn contains? [tbl item] + (or (= tbl item) (lume.find tbl item))) + +(local nrepl + {:active-requests {} + :session-handlers {} + :default-handlers + {:out #(core.log $2) + :value #(core.log $2) + :ex #(core.err $2) + :status/interrupted #($1:done $3.id) + :status/done #($1:done $3.id)} + :merge-handlers + (fn [self message] + (lume.merge self.default-handlers + (or (. self.session-handlers message.session) {}) + (or (. self.active-requests message.id) {}))) + :chain-handlers + (fn [self keys ...] + (local new-handlers {}) + (each [_ key (ipairs keys)] + (each [_ handlers (ipairs [self.default-handlers ...])] + (local next-handler (. handlers key)) + (local prev-handler (. new-handlers key)) + (if (and next-handler prev-handler) + (tset new-handlers key + #(do (prev-handler $1 $2 $3) (next-handler $1 $2 $3))) + next-handler + (tset new-handlers key next-handler)))) + new-handlers) + :counter 1 + :input "" + :parse-input + (fn [self] + (match (pcall #(bencode.decode self.input)) + (true val len) + (do (set self.input (self.input:sub (+ len 1))) + val) + (false :Incomplete) nil + (false _) + (do (set self.input "") + nil))) + :receive + (fn [self] + (when self.connection + (local (data err part) (self.connection:receive "*a")) + (local response (or data part)) + (when (> (response:len) 0) + (set self.input (.. self.input response))) + (match (self:parse-input) nil nil + input (self:handle input)) + (when (= err :closed) + (self:disconnect)))) + :send + (fn [self msg ?handlers ?session] + (when self.connection + (when (not msg.id) + (set msg.id self.counter) + (set self.counter (+ self.counter 1))) + (when (not msg.session) + (set msg.session (or ?session self.default-session))) + (when ?handlers + (tset self.active-requests msg.id ?handlers)) + (self.connection:send (bencode.encode msg)))) + :done + (fn [self msg-id] + (tset self.active-requests msg-id nil)) + :handle + (fn [self response] + (local handlers (self:merge-handlers response)) + (each [prop handler (pairs handlers)] + (local idiv (prop:find :/)) + (local key (if idiv (prop:sub 1 (- idiv 1)) prop)) + (when (and (. response key) + (or (= idiv nil) + (contains? (. response key) (prop:sub (+ idiv 1))))) + (handler self (. response key) response)))) + :disconnect + (fn [self] + (when self.connection + (self.connection:close) + (set self.connection nil) + (set self.default-session nil) + (set self.active-requests {}) + (set self.session-handlers {}))) + :connect + (fn [self ?port ?host] + (when (not self.connection) + (local (port host) (values (or ?port 7888) (or ?host :localhost))) + (set self.connection (assert (socket.connect host port))) + (self.connection:settimeout 0) + (core.add_thread #(while true (self:receive) (coroutine.yield)) self) + (self:send {:op :clone} + {:new-session #(set self.default-session $2)}))) + :connected? + (fn [self] (not= self.default-session nil)) + :new-session + (fn [self callback ?handler] + (self:send {:op :clone} + {:new-session + (fn [self session message] + (tset self.session-handlers session ?handler) + (callback self session message))}))}) + +nrepl diff --git a/repl.fnl b/repl.fnl new file mode 100644 index 0000000..af1bf2c --- /dev/null +++ b/repl.fnl @@ -0,0 +1,40 @@ +(local util (require :plugins.fennel-xl.util)) +(local fennel (require :plugins.fennel-xl.fennel)) +(local style (require :core.style)) +(local lume (require :plugins.fennel-xl.lume)) +(local {: textbutton : under : group-wrapper} (util.require :plugins.fennel-xl.imgui)) +(local {: inspect} (util.require :plugins.fennel-xl.inspector)) +(local replsession (require :plugins.fennel-xl.replsession)) + +(local repl (util.hot-table ...)) + +(fn repl.inspector [{: w &as form} {: vals : states}] + (let [g (group-wrapper form)] + (each [i v (ipairs vals)] + (g #(inspect $...) (under (g) {: w}) (. states i) v)) + (g))) + +(fn repl.notify [listeners line] + (each [_ listener (ipairs listeners)] + (listener:append line))) + +(fn repl.mk-result [vals] + {:draw repl.inspector : vals :states (icollect [_ (ipairs vals)] {})}) + +(fn repl.listen [{: listeners} listener] + (table.insert listeners listener)) + +(fn repl.unlisten [{: listeners} listener] + (lume.remove listeners listener)) + +(fn repl.submit [{: session : listeners} chunk] + (session:submit chunk (fn [{: vals : err : traceback}] (repl.notify listeners (repl.mk-result (or vals [err traceback])))))) + +(fn repl.new [?id ?repl ?opts] + {:listeners [] + :listen #(repl.listen $...) + :unlisten #(repl.unlisten $...) + :submit #(repl.submit $...) + :session (replsession.session ?id ?repl ?opts)}) + +repl.hot diff --git a/replsession.fnl b/replsession.fnl new file mode 100644 index 0000000..0b84afe --- /dev/null +++ b/replsession.fnl @@ -0,0 +1,32 @@ +(local util (require :plugins.fennel-xl.util)) +(local lume (require :plugins.fennel-xl.lume)) +(local {: defmulti} (require :plugins.fennel-xl.multimethod)) + +(local replsession (util.hot-table ...)) + +(set replsession.sessions {}) +(set replsession.default-repl :local) + +(set replsession.submit + (defmulti (fn [session chunk callback ?suppress-crash] session.repl) :submit ...)) +(set replsession.restart (defmulti (fn [session] session.repl) :restart ...)) + +(fn replsession.new-session [?repl ?opts] + (replsession.restart (lume.merge {:repl (or ?repl replsession.default-repl) + :submit replsession.submit} + (or ?opts {})))) + +(fn replsession.activate [id] (set replsession.active-session-id id)) +(fn replsession.session-id [?id ?repl] + (let [repl (or ?repl replsession.default-repl)] + (if (= ?id nil) (or replsession.active-session-id (.. repl ":REPL")) + (.. repl ":" ?id)))) + +(fn replsession.session [?id ?repl ?opts] + (let [id (replsession.session-id ?id ?repl) + session (or (. replsession.sessions id) (replsession.new-session ?repl ?opts))] + (tset replsession.sessions id session) + session)) + +replsession.hot + diff --git a/replview.fnl b/replview.fnl new file mode 100644 index 0000000..9a3dec1 --- /dev/null +++ b/replview.fnl @@ -0,0 +1,60 @@ +(local util (require :plugins.fennel-xl.util)) +(local {: attach-imstate : textbox : textbutton : label : under : reform : group-wrapper : mouse-inside} (util.require :plugins.fennel-xl.imgui)) +(local View (require :core.view)) +(local style (require :core.style)) + +(local ReplView (View:extend)) + +(fn ReplView.new [self conn] + (ReplView.super.new self) + (attach-imstate self) + (set self.conn conn) + (set self.log []) + (set self.cmd "") + (set self.scrollheight math.huge) + (set self.scrollable true) + (set self.title "REPL") + (self.conn:listen self)) + +(fn ReplView.try_close [self do_close] + (self.conn:unlisten self) + (ReplView.super.try_close self do_close)) + +(fn ReplView.get_scrollable_size [self] self.scrollheight) + +(fn ReplView.append [self line] + (table.insert self.log line)) + +(fn ReplView.draw-cmd [{: x : y : w : view &as form} {: cmd} iline] + (label form cmd) + (when (mouse-inside x y w form.h) + (when (textbutton (reform form {:x (+ x w -35) :into {}}) :X) + (table.remove view.log iline) + (table.remove view.log iline)) + (when (textbutton (reform form {:x (+ x w -60) :into {}}) :!) + (view:submit cmd)))) + +(fn ReplView.submit [self ?cmd] + (local cmd (or ?cmd self.cmd)) + (when (= ?cmd nil) + (set self.cmd "")) + (self:append {:draw self.draw-cmd : cmd}) + (self.conn:submit cmd)) + +(fn ReplView.draw [self] + (self:draw_background style.background) + (self:draw_scrollbar) + (let [{: w &as form} (self:form) + g (group-wrapper form)] + ; todo: cache sizes and avoid drawing if offscreen? + ; note: then offscreen items can't be focussed without further effort + ; todo: draw line numbers + (each [i line (ipairs self.log)] + (g line.draw (under (g) {: w}) line i)) + (set self.cmd (g textbox (under (g) {: w :tag :command}) self.cmd)) + (self:end-scroll (g)))) + +(fn ReplView.get_name [self] self.title) + +ReplView + diff --git a/util.fnl b/util.fnl new file mode 100644 index 0000000..8704a34 --- /dev/null +++ b/util.fnl @@ -0,0 +1,116 @@ +(local lume (require :plugins.fennel-xl.lume)) +(local json (require :plugins.fennel-xl.dkjson)) +(local core (require :core)) + +(fn string.fromhex [str] + (str:gsub ".." (fn [cc] (string.char (tonumber cc 16))))) +(fn string.tohex [str] + (str:gsub "." (fn [c] (string.format "%02X" (string.byte c))))) + +(fn splice [bytes offset str] + (.. (bytes:sub 1 offset) + str + (bytes:sub (+ (length str) offset 1)))) + +(fn reload [modname] + (tset package.loaded modname nil) + (require modname)) + +; lume.hotswap really assumes your module is a table +(fn hotswap [modname] + (if (= (type (. package.loaded modname)) :table) + (lume.hotswap modname) + (reload modname))) + +(fn mk-swappable-fn [table k] + (fn [...] ((. table k) ...))) + +(fn swappable [table] + (local s {}) + (each [k v (pairs table)] + (if (= (type v) :function) + (tset s k (mk-swappable-fn table k)) + (tset s k v))) + s) + +(fn swappable-require [modname] + (swappable (require modname))) + +(fn hot-table [modname] + (local tbl {}) + (fn find-table [] + (let [loaded-pkg (. package.loaded modname)] + (if (= (type loaded-pkg) :table) loaded-pkg tbl))) + (setmetatable {:hot tbl} { + :__index (fn [_ key] (. (find-table) key)) + :__newindex (fn [_ key val] (tset (find-table) key val)) + })) + +(fn readjson [filename] + (local f (io.open filename :r)) + (local text (f:read "*a")) + (f:close) + (json.decode text)) + +(fn writejson [filename value] + (local f (io.open filename :w)) + (f:write (json.encode value)) + (f:close)) + +(fn waitfor [pred] + (local coro (coroutine.running)) + (core.add_thread + (fn [] + (while (not (pred)) + (coroutine.yield)) + (coroutine.resume coro)) + coro) + (coroutine.yield)) + +(fn in-coro [f ...] (-> (coroutine.create f) (coroutine.resume ...))) + +(fn multival-next [multival i] + (when (< i multival.n) + (values (+ i 1) (. multival (+ i 1))))) + +(fn multival-ipairs [multival] + (values multival-next multival 0)) + +(fn multival [...] + (local multival {:n (select :# ...) :ipairs multival-ipairs}) + (for [i 1 multival.n] + (tset multival i (select i ...))) + multival) + +(fn nested-tset [t keys value] + (let [next-key (. keys 1)] + (if (= (length keys) 1) (tset t next-key value) + (do (when (= (. t next-key) nil) + (tset t next-key {})) + (nested-tset (. t next-key) (lume.slice keys 2) value))))) + +(fn file-exists [name] + (let [f (io.open name :r)] + (when (not= f nil) (io.close f)) + (not= f nil))) + +(fn pairoff [l] + (fn [_ iprev] (let [i (if iprev (+ iprev 2) 1)] + (when (< i (length l)) (values i (. l i) (. l (+ i 1))))))) + +(fn countiter [minmax ?max ?step] + (let [min (if ?max minmax 1) + max (or ?max minmax) + step (or ?step 1)] + (fn [_ iprev] + (let [i (if iprev (+ iprev step) min)] + (when (if (> step 0) (<= i max) (>= i max)) i))))) + +(fn condlist [...] (let [l []] (lume.push l ...) l)) + +(fn prototype [base] (setmetatable {} {:__index base})) + +{: splice : condlist : prototype + : reload : hotswap : swappable :require swappable-require : hot-table : nested-tset : pairoff : countiter + : readjson : writejson : file-exists : waitfor : in-coro : multival} +