Commit 1415e583 authored by cota's avatar cota

Add squish

Squish is a tool to pack many individual Lua scripts and their
modules into a single Lua script.

Squish has its homepage at

	http://matthewwild.co.uk/projects/squish/home

Squish is MIT-licensed. Note that the MIT license is GPL-compatible.

Currently we are generating wbgen2 in a non-portable way. Adding
squish is a first step towards doing this properly.
Signed-off-by: Emilio G. Cota's avatarEmilio G. Cota <cota@braap.org>

git-svn-id: http://svn.ohwr.org/wishbone-gen@6 4537843c-45c2-4d80-8546-c3283569414f
parent 3610f404
#!/usr/bin/env lua
package.preload['optlex']=(function(...)
local h=_G
local c=require"string"
module"optlex"
local i=c.match
local e=c.sub
local d=c.find
local l=c.rep
local m
error=h.error
warn={}
local n,o,r
local j={
TK_KEYWORD=true,
TK_NAME=true,
TK_NUMBER=true,
TK_STRING=true,
TK_LSTRING=true,
TK_OP=true,
TK_EOS=true,
}
local k={
TK_COMMENT=true,
TK_LCOMMENT=true,
TK_EOL=true,
TK_SPACE=true,
}
local s
local function b(e)
local t=n[e-1]
if e<=1 or t=="TK_EOL"then
return true
elseif t==""then
return b(e-1)
end
return false
end
local function v(e)
local t=n[e+1]
if e>=#n or t=="TK_EOL"or t=="TK_EOS"then
return true
elseif t==""then
return v(e+1)
end
return false
end
local function E(a)
local t=#i(a,"^%-%-%[=*%[")
local a=e(a,t+1,-(t-1))
local e,t=1,0
while true do
local o,n,i,a=d(a,"([\r\n])([\r\n]?)",e)
if not o then break end
e=o+1
t=t+1
if#a>0 and i~=a then
e=e+1
end
end
return t
end
local function g(s,h)
local a=i
local t,e=n[s],n[h]
if t=="TK_STRING"or t=="TK_LSTRING"or
e=="TK_STRING"or e=="TK_LSTRING"then
return""
elseif t=="TK_OP"or e=="TK_OP"then
if(t=="TK_OP"and(e=="TK_KEYWORD"or e=="TK_NAME"))or
(e=="TK_OP"and(t=="TK_KEYWORD"or t=="TK_NAME"))then
return""
end
if t=="TK_OP"and e=="TK_OP"then
local t,e=o[s],o[h]
if(a(t,"^%.%.?$")and a(e,"^%."))or
(a(t,"^[~=<>]$")and e=="=")or
(t=="["and(e=="["or e=="="))then
return" "
end
return""
end
local t=o[s]
if e=="TK_OP"then t=o[h]end
if a(t,"^%.%.?%.?$")then
return" "
end
return""
else
return" "
end
end
local function q()
local h,s,i={},{},{}
local e=1
for t=1,#n do
local a=n[t]
if a~=""then
h[e],s[e],i[e]=a,o[t],r[t]
e=e+1
end
end
n,o,r=h,s,i
end
local function _(d)
local t=o[d]
local t=t
local n
if i(t,"^0[xX]")then
local e=h.tostring(h.tonumber(t))
if#e<=#t then
t=e
else
return
end
end
if i(t,"^%d+%.?0*$")then
t=i(t,"^(%d+)%.?0*$")
if t+0>0 then
t=i(t,"^0*([1-9]%d*)$")
local a=#i(t,"0*$")
local o=h.tostring(a)
if a>#o+1 then
t=e(t,1,#t-a).."e"..o
end
n=t
else
n="0"
end
elseif not i(t,"[eE]")then
local a,t=i(t,"^(%d*)%.(%d+)$")
if a==""then a=0 end
if t+0==0 and a==0 then
n="0"
else
local o=#i(t,"0*$")
if o>0 then
t=e(t,1,#t-o)
end
if a+0>0 then
n=a.."."..t
else
n="."..t
local a=#i(t,"^0*")
local o=#t-a
local a=h.tostring(#t)
if o+2+#a<1+#t then
n=e(t,-o).."e-"..a
end
end
end
else
local t,a=i(t,"^([^eE]+)[eE]([%+%-]?%d+)$")
a=h.tonumber(a)
local o,s=i(t,"^(%d*)%.(%d*)$")
if o then
a=a-#s
t=o..s
end
if t+0==0 then
n="0"
else
local o=#i(t,"^0*")
t=e(t,o+1)
o=#i(t,"0*$")
if o>0 then
t=e(t,1,#t-o)
a=a+o
end
local i=h.tostring(a)
if a==0 then
n=t
elseif a>0 and(a<=1+#i)then
n=t..l("0",a)
elseif a<0 and(a>=-#t)then
o=#t+a
n=e(t,1,o).."."..e(t,o+1)
elseif a<0 and(#i>=-a-#t)then
o=-a-#t
n="."..l("0",o)..t
else
n=t.."e"..a
end
end
end
if n and n~=o[d]then
if s then
m("<number> (line "..r[d]..") "..o[d].." -> "..n)
s=s+1
end
o[d]=n
end
end
local function x(l)
local t=o[l]
local h=e(t,1,1)
local f=(h=="'")and'"'or"'"
local t=e(t,2,-2)
local a=1
local u,n=0,0
while a<=#t do
local m=e(t,a,a)
if m=="\\"then
local o=a+1
local r=e(t,o,o)
local s=d("abfnrtv\\\n\r\"\'0123456789",r,1,true)
if not s then
t=e(t,1,a-1)..e(t,o)
a=a+1
elseif s<=8 then
a=a+2
elseif s<=10 then
local i=e(t,o,o+1)
if i=="\r\n"or i=="\n\r"then
t=e(t,1,a).."\n"..e(t,o+2)
elseif s==10 then
t=e(t,1,a).."\n"..e(t,o+1)
end
a=a+2
elseif s<=12 then
if r==h then
u=u+1
a=a+2
else
n=n+1
t=e(t,1,a-1)..e(t,o)
a=a+1
end
else
local i=i(t,"^(%d%d?%d?)",o)
o=a+1+#i
local l=i+0
local s=c.char(l)
local r=d("\a\b\f\n\r\t\v",s,1,true)
if r then
i="\\"..e("abfnrtv",r,r)
elseif l<32 then
i="\\"..l
elseif s==h then
i="\\"..s
u=u+1
elseif s=="\\"then
i="\\\\"
else
i=s
if s==f then
n=n+1
end
end
t=e(t,1,a-1)..i..e(t,o)
a=a+#i
end
else
a=a+1
if m==f then
n=n+1
end
end
end
if u>n then
a=1
while a<=#t do
local o,n,i=d(t,"([\'\"])",a)
if not o then break end
if i==h then
t=e(t,1,o-2)..e(t,o)
a=o
else
t=e(t,1,o-1).."\\"..e(t,o)
a=o+2
end
end
h=f
end
t=h..t..h
if t~=o[l]then
if s then
m("<string> (line "..r[l]..") "..o[l].." -> "..t)
s=s+1
end
o[l]=t
end
end
local function z(s)
local t=o[s]
local h=i(t,"^%[=*%[")
local a=#h
local c=e(t,-a,-1)
local u=e(t,a+1,-(a+1))
local n=""
local t=1
while true do
local a,o,d,h=d(u,"([\r\n])([\r\n]?)",t)
local o
if not a then
o=e(u,t)
elseif a>=t then
o=e(u,t,a-1)
end
if o~=""then
if i(o,"%s+$")then
warn.lstring="trailing whitespace in long string near line "..r[s]
end
n=n..o
end
if not a then
break
end
t=a+1
if a then
if#h>0 and d~=h then
t=t+1
end
if not(t==1 and t==a)then
n=n.."\n"
end
end
end
if a>=3 then
local e,t=a-1
while e>=2 do
local a="%]"..l("=",e-2).."%]"
if not i(n,a)then t=e end
e=e-1
end
if t then
a=l("=",t-2)
h,c="["..a.."[","]"..a.."]"
end
end
o[s]=h..n..c
end
local function w(r)
local a=o[r]
local s=i(a,"^%-%-%[=*%[")
local t=#s
local u=e(a,-t,-1)
local h=e(a,t+1,-(t-1))
local n=""
local a=1
while true do
local o,t,r,s=d(h,"([\r\n])([\r\n]?)",a)
local t
if not o then
t=e(h,a)
elseif o>=a then
t=e(h,a,o-1)
end
if t~=""then
local a=i(t,"%s*$")
if#a>0 then t=e(t,1,-(a+1))end
n=n..t
end
if not o then
break
end
a=o+1
if o then
if#s>0 and r~=s then
a=a+1
end
n=n.."\n"
end
end
t=t-2
if t>=3 then
local e,a=t-1
while e>=2 do
local t="%]"..l("=",e-2).."%]"
if not i(n,t)then a=e end
e=e-1
end
if a then
t=l("=",a-2)
s,u="--["..t.."[","]"..t.."]"
end
end
o[r]=s..n..u
end
local function p(n)
local t=o[n]
local a=i(t,"%s*$")
if#a>0 then
t=e(t,1,-(a+1))
end
o[n]=t
end
local function I(o,t)
if not o then return false end
local a=i(t,"^%-%-%[=*%[")
local a=#a
local i=e(t,-a,-1)
local e=e(t,a+1,-(a-1))
if d(e,o,1,true)then
return true
end
end
function optimize(t,a,d,i)
local c=t["opt-comments"]
local u=t["opt-whitespace"]
local f=t["opt-emptylines"]
local y=t["opt-eols"]
local A=t["opt-strings"]
local T=t["opt-numbers"]
local O=t.KEEP
s=t.DETAILS and 0
m=m or h.print
if y then
c=true
u=true
f=true
end
n,o,r
=a,d,i
local t=1
local a,d
local h
local function i(i,a,e)
e=e or t
n[e]=i or""
o[e]=a or""
end
while true do
a,d=n[t],o[t]
local s=b(t)
if s then h=nil end
if a=="TK_EOS"then
break
elseif a=="TK_KEYWORD"or
a=="TK_NAME"or
a=="TK_OP"then
h=t
elseif a=="TK_NUMBER"then
if T then
_(t)
end
h=t
elseif a=="TK_STRING"or
a=="TK_LSTRING"then
if A then
if a=="TK_STRING"then
x(t)
else
z(t)
end
end
h=t
elseif a=="TK_COMMENT"then
if c then
if t==1 and e(d,1,1)=="#"then
p(t)
else
i()
end
elseif u then
p(t)
end
elseif a=="TK_LCOMMENT"then
if I(O,d)then
if u then
w(t)
end
h=t
elseif c then
local e=E(d)
if k[n[t+1]]then
i()
a=""
else
i("TK_SPACE"," ")
end
if not f and e>0 then
i("TK_EOL",l("\n",e))
end
if u and a~=""then
t=t-1
end
else
if u then
w(t)
end
h=t
end
elseif a=="TK_EOL"then
if s and f then
i()
elseif d=="\r\n"or d=="\n\r"then
i("TK_EOL","\n")
end
elseif a=="TK_SPACE"then
if u then
if s or v(t)then
i()
else
local a=n[h]
if a=="TK_LCOMMENT"then
i()
else
local e=n[t+1]
if k[e]then
if(e=="TK_COMMENT"or e=="TK_LCOMMENT")and
a=="TK_OP"and o[h]=="-"then
else
i()
end
else
local e=g(h,t+1)
if e==""then
i()
else
i("TK_SPACE"," ")
end
end
end
end
end
else
error("unidentified token encountered")
end
t=t+1
end
q()
if y then
t=1
if n[1]=="TK_COMMENT"then
t=3
end
while true do
a,d=n[t],o[t]
if a=="TK_EOS"then
break
elseif a=="TK_EOL"then
local e,a=n[t-1],n[t+1]
if j[e]and j[a]then
local e=g(t-1,t+1)
if e==""then
i()
end
end
end
t=t+1
end
q()
end
if s and s>0 then m()end
return n,o,r
end
end)
package.preload['optparser']=(function(...)
local e=_G
local a=require"string"
local w=require"table"
module"optparser"
local n="etaoinshrdlucmfwypvbgkqjxz_ETAOINSHRDLUCMFWYPVBGKQJXZ"
local r="etaoinshrdlucmfwypvbgkqjxz_0123456789ETAOINSHRDLUCMFWYPVBGKQJXZ"
local y={}
for e in a.gmatch([[
and break do else elseif end false for function if in
local nil not or repeat return then true until while
self]],"%S+")do
y[e]=true
end
local d,f,
u,o,
c,p,
l,
h
local function m(e)
local i={}
for n=1,#e do
local e=e[n]
local o=e.name
if not i[o]then
i[o]={
decl=0,token=0,size=0,
}
end
local t=i[o]
t.decl=t.decl+1
local i=e.xref
local a=#i
t.token=t.token+a
t.size=t.size+a*#o
if e.decl then
e.id=n
e.xcount=a
if a>1 then
e.first=i[2]
e.last=i[a]
end
else
t.id=n
end
end
return i
end
local function v(t)
local i=a.byte
local s=a.char
local e={
TK_KEYWORD=true,TK_NAME=true,TK_NUMBER=true,
TK_STRING=true,TK_LSTRING=true,
}
if not t["opt-comments"]then
e.TK_COMMENT=true
e.TK_LCOMMENT=true
end
local a={}
for e=1,#d do
a[e]=f[e]
end
for e=1,#o do
local e=o[e]
local t=e.xref
for e=1,e.xcount do
local e=t[e]
a[e]=""
end
end
local t={}
for e=0,255 do t[e]=0 end
for o=1,#d do
local o,a=d[o],a[o]
if e[o]then
for e=1,#a do
local e=i(a,e)
t[e]=t[e]+1
end
end
end
local function a(a)
local e={}
for o=1,#a do
local a=i(a,o)
e[o]={c=a,freq=t[a],}
end
w.sort(e,
function(e,t)
return e.freq>t.freq
end
)
local t={}
for a=1,#e do
t[a]=s(e[a].c)
end
return w.concat(t)
end
n=a(n)
r=a(r)
end
local function b()
local t
local s,h=#n,#r
local e=l
if e<s then
e=e+1
t=a.sub(n,e,e)
else
local i,o=s,1
repeat
e=e-i
i=i*h
o=o+1
until i>e
local i=e%s
e=(e-i)/s
i=i+1
t=a.sub(n,i,i)
while o>1 do
local i=e%h
e=(e-i)/h
i=i+1
t=t..a.sub(r,i,i)
o=o-1
end
end
l=l+1
return t,c[t]~=nil
end
function optimize(e,t,a,i,n)
d,f,u,o
=t,a,i,n
l=0
h={}
c=m(u)
p=m(o)
if e["opt-entropy"]then
v(e)
end
local e={}
for t=1,#o do
e[t]=o[t]
end
w.sort(e,
function(e,t)
return e.xcount>t.xcount
end
)
local a,t,r={},1,false
for o=1,#e do
local e=e[o]
if not e.isself then
a[t]=e
t=t+1
else
r=true
end
end
e=a
local s=#e
while s>0 do
local n,a
repeat
n,a=b()
until not y[n]
h[#h+1]=n
local t=s
if a then
local i=u[c[n].id].xref
local n=#i
for a=1,s do
local a=e[a]
local s,e=a.act,a.rem
while e<0 do
e=o[-e].rem
end
local o
for t=1,n do
local t=i[t]
if t>=s and t<=e then o=true end
end
if o then
a.skip=true
t=t-1
end
end
end
while t>0 do
local a=1
while e[a].skip do
a=a+1
end
t=t-1
local i=e[a]
a=a+1
i.newname=n
i.skip=true