KoboldAI-Client/extern/lualibs/pl/func.lua

11 lines
5.5 KiB
Lua

-- Penlight 1.11.0-1 | /lua/pl/func.lua | https://github.com/lunarmodules/Penlight | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (C) 2009-2016 Steve Donovan, David Manura.
-- 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 type,setmetatable,getmetatable,rawset=type,setmetatable,getmetatable,rawset;local a,b=table.concat,table.insert;local tostring=tostring;local c=require'pl.utils'local pairs,rawget,d,e=pairs,rawget,c.unpack,c.pack;local f=require'pl.tablex'local g=f.map;local h=rawget(_G,'_DEBUG')local i=c.assert_arg;local j={}local k={}local function l(m)setmetatable(m,k)return m end;j.PE=l;local function n(o)return getmetatable(o)==k end;j.isPE=n;local function p(q)return l{op='X',repr='_'..q,index=q}end;local function r(q)return l{op='X',repr='_C'..q,index=q}end;j._1,j._2,j._3,j._4,j._5=p(1),p(2),p(3),p(4),p(5)j._0=l{op='X',repr='...',index=0}function j.Var(s)local t=c.split(s,'[%s,]+')local u={}for v=1,#t do b(u,l{op='X',repr=t[v],index=0})end;return d(u)end;function j._(w)return l{op='X',repr=w,index='wrap'}end;local x;j.Nil=j.Var'nil'function k.__index(o,y)return l{op='[]',o,y}end;function k.__call(z,...)return l{op='()',z,...}end;function k.__tostring(A)return x(A)end;function k.__unm(B)return l{op='unm',B}end;function j.Not(B)return l{op='not',B}end;function j.Len(B)return l{op='#',B}end;local function C(D,m)for s,E in pairs(m)do rawset(D,s,function(F,G)return l{op=E,F,G}end)end end;local function H(s,z,D)rawset(D,s,function(...)return l{op='()',z,...}end)end;local I={}local function J(K)return type(_G[K])=='table'end;function j.import(L,D)i(1,L,'string',J,'arg# 1: not a name of a global table')local m=_G[L]D=D or _G;for s,z in pairs(m)do H(s,z,D)I[z]=s end end;function j.register(z,s)i(1,z,'function')if s then i(2,s,'string')I[z]=s end;return function(...)return l{op='()',z,...}end end;function j.lookup_imported_name(z)return I[z]end;local function M(...)return...end;function j.Args(...)return l{op='()',M,...}end;local N={['or']=0,['and']=2,['==']=4,['~=']=4,['<']=4,['>']=4,['<=']=4,['>=']=4,['..']=6,['+']=8,['-']=8,['*']=10,['/']=10,['%']=10,['^']=14}local O={['not']=12,['#']=12,['unm']=12}C(j,{And='and',Or='or',Eq='==',Lt='<',Gt='>',Le='<=',Ge='>='})C(k,{__add='+',__sub='-',__mul='*',__div='/',__mod='%',__pow='^',__concat='..'})C(k,{__eq='=='})function j.tail(t)i(1,t,'table')local u={}for v=2,#t do b(u,t[v])end;return u end;function x(A,P)local Q=j.tail;if n(A)then local R=N[A.op]or O[A.op]if R then local S;if N[A.op]then local T=R;local U=R;if A.op=='..'or A.op=='^'then T=T+1 else U=U+1 end;local V=x(A[1],T)local W=x(A[2],U)S=V..' '..A.op..' '..W else local E=A.op=='unm'and'-'or A.op;S=E..' '..x(A[1],R)end;if P and P>R then S='('..S..')'end;return S else local t=g(x,A)if A.op=='[]'then return t[1]..'['..t[2]..']'elseif A.op=='()'then local X;if t[1]~=nil then X=t[1]else X=''end;return X..'('..a(Q(t),',')..')'else return A.repr end end elseif type(A)=='string'then return'"'..A..'"'elseif type(A)=='function'then local s=j.lookup_imported_name(A)if s then return s else return tostring(A)end else return tostring(A)end end;j.repr=x;local Y;function Y(A,Z)if n(A)then if A.op~='X'then local _=0;for v=1,#A do local a0=A[v]local a1=n(a0)if a1 then if a0.op=='X'and a0.index=='wrap'then a0=a0.repr;a1=false else _=math.max(_,Y(a0,Z))end end;if not a1 then b(Z,a0)A[v]=r(#Z)end end;return _ else return A.index end else return 0 end end;j.collect_values=Y;function j.instantiate(A)local a2,a3,a4={},{},{}local a5,a6,z;local K=j.collect_values(A,a3)for v=1,#a3 do b(a2,'_C'..v)if h then print(v,a3[v])end end;for v=1,K do b(a4,'_'..v)end;a2=a(a2,',')a4=a(a4,',')a5=x(A)local a7=('return function(%s) return function(%s) return %s end end'):format(a2,a4,a5)if h then print(a7)end;z,a6=c.load(a7,'fun')if not z then return nil,a6 end;z=z()z=z(d(a3))A.__PE_function=z;return z end;function j.I(A)if rawget(A,'__PE_function')then return A.__PE_function else return j.instantiate(A)end end;c.add_function_factory(k,j.I)j.bind1=c.bind1;j.curry=j.bind1;function j.compose(a8,a9)return function(...)return a8(a9(...))end end;function j.bind(X,...)local aa=e(...)local ab,a4,ac,a3={},{},{'fn'},{}local ad,ae,af=1,0,false;for v=1,aa.n do local ag=aa[v]if n(ag)and ag.op=='X'then b(ab,ag.repr)ae=math.max(ae,ag.index)if ag.index==0 then af=true end else local ah='_v'..ad;b(ac,ah)b(ab,ah)b(a3,ag)ad=ad+1 end end;for ai=1,ae do b(a4,'_'..ai)end;if af then b(a4,'...')end;ac=a(ac,',')a4=a(a4,',')ab=a(ab,',')local a7=([[
return function (%s)
return function(%s) return fn(%s) end
end
]]):format(ac,a4,ab)if h then print(a7)end;local u=c.load(a7)u=u()return u(X,d(a3))end;return j