diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2016-08-22 14:32:38 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2016-08-22 14:32:38 -0400 |
| commit | 25af93b5d1281e7a0f8d8869c5bff0a2ce8cf1bf (patch) | |
| tree | a7b5609cc5c6170b3ecde511ce6a245a1c899113 /src | |
| parent | 5871a659d3436cfd7ed813bd317036d9b45e2e90 (diff) | |
| download | glum-25af93b5d1281e7a0f8d8869c5bff0a2ce8cf1bf.tar.gz glum-25af93b5d1281e7a0f8d8869c5bff0a2ce8cf1bf.tar.bz2 glum-25af93b5d1281e7a0f8d8869c5bff0a2ce8cf1bf.zip | |
Completed minifier, created+completed uglifier
Diffstat (limited to 'src')
| -rw-r--r-- | src/compile.lua | 31 | ||||
| -rw-r--r-- | src/glum.lua | 162 | ||||
| -rw-r--r-- | src/parser.lua | 8 | ||||
| -rw-r--r-- | src/test.lua | 32 |
4 files changed, 130 insertions, 103 deletions
diff --git a/src/compile.lua b/src/compile.lua new file mode 100644 index 0000000..13f652e --- /dev/null +++ b/src/compile.lua @@ -0,0 +1,31 @@ +--[[ + Interprets a .pill file to minify a file structure +]] + +local lfs = require("lfs") + +--Create some common functions +local function getfile(filename) + if file ~= nil then --glua environment + return file.Read(filename) + elseif io.open ~= nil then --Lua environment + return io.open(filename,"r"):read("*a") + else + error("Could not find a way to read from files!") + end +end + +local function putfile(filename,data) + if file ~= nil then --gLua environment + file.Write(filename,data) + elseif io.open ~= nil then --Lua environment + io.open(filename):write(data) + end +end + +local function getdirs(filepath) + local files, directories = file.Find( filepath .. "/*", "LUA" ) +end + +local filepath = arg[1] +local pillfile = io.open(filepath) diff --git a/src/glum.lua b/src/glum.lua index a50382a..7ee37b6 100644 --- a/src/glum.lua +++ b/src/glum.lua @@ -11,34 +11,9 @@ This moudle allows you to minify gLua code print(x.minify(str)) Dependencies: lua-parser + lpeg ]] -local strreps = { - ["\\"] = "\\\\", - ["\a"] = "\\a", - ["\b"] = "\\b", - ["\f"] = "\\f", - ["\n"] = "\\n", - ["\r"] = "\\r", - ["\t"] = "\\t", - ["\v"] = "\\v", - ["\""] = "\\\"" -} -local function safe_str (str) - local tep = {} - --print("--------------Safeing str:" .. str) - for c in str:gmatch(".") do - if strreps[c] ~= nil then - --print(string.format("safeing %s:%s",c,strreps[c])) - end - tep[#tep + 1] = strreps[c] or c - end - local output = table.concat(tep) - --print("-------------Returning string:" .. output) - return output -end - - local parser = dofile("../src/parser.lua") local lpeg = require("lpeg") lpeg.locale(lpeg) @@ -109,12 +84,16 @@ local function stringfor(ast,tbl) if syntax[ast.tag] ~= nil then return syntax[ast.tag](ast,tbl) else + print("Valid tags are:") + for k,v in pairs(syntax) do + print(k) + end error("Attempted to use unknown tag type:" .. ast.tag) end end --Abandon all hope, ye who enter here --Refer to the comments at the top of parser.lua for what each function should do. ---If willox ever decides to add new language features, they need to be added to BOTH parser.lua and here. +--If anyone ever decides to add new language features, they need to be added to BOTH parser.lua and here. syntax = { ["Call"] = function(ast,tbl) local exprname = stringfor(ast[1],tbl) @@ -139,45 +118,24 @@ syntax = { --A short hand if it's a simple thing if ast[2].tag == "String" and #ast[2][1] < (#func + 2) then inv = ast[2][1] - output = output .. ":" .. inv .. "(" + output = table.concat({output, ":", inv, "("}) else inv = stringfor(ast[2],tbl) - output = output .. "[" .. inv .. "](" .. func .. "," + output = table.concat({output, "[", inv, "](", func, ","}) end - output = output .. table.concat(invargs,",") - output = output .. ")" + output = output .. table.concat(invargs,",") .. ")" return output end, ["String"] = function(ast,tbl) + local sop,eop = "\"","\"" if tbl.strings[ast[1]] == nil then - local fsstr = safe_str(ast[1]) - local fstr = string.format("\"%s\"",fsstr) - --fstr = safe_str(fstr) - --local lstr = string.format("return %q",ast[1]) - print("Loading string:" .. fstr) - local rstring = loadstring("return " .. fstr) - --print("Returning string:") - --print(ast[1]) - --print("fsstr") - --print(fsstr) - --print("formated:") - --print(string.format("%q",ast[1])) - local rstrs = rstring() - --assert(rstrs == ast[1],string.format("%q is not equal to %q",rstrs,ast[1])) - --print(string.format("%q is equal to %s:%s", rstrs, ast[1],fsstr)) - --print("Returning " .. fstr .. " from " .. ast[1] .. " out of " .. fsstr) - return fstr - --return string.format("%q",ast[1]) - --return safe_str(string.format("%q",ast[1])) - --return string.format("%q",safe_str(ast[1])) - --[[ - if #ast[1] < 4 then return "\""..ast[1].."\"" end - local nextvar = getnextvarname(tbl.lname) - tbl.lname = nextvar - tbl.strings[ast[1] ] = nextvar - return nextvar - ]] + if string.find(ast[1],"\"") then + sop = "[[" + eop = "]]" + end + return table.concat({sop,ast[1],eop}) end + print("Returning non-catated string") return tbl.strings[ast[1]] end, ["Id"] = function(ast,tbl) @@ -194,7 +152,7 @@ syntax = { return table.concat({globalvar, "[", stringfor(ast[2],tbl), "]"}) end, ["Paren"] = function(ast,tbl) - return table.concat({"(" .. stringfor(ast[1],tbl) .. ")"}) + return table.concat({"(",stringfor(ast[1],tbl),")"}) end, ["Dots"] = function(ast,tbl) return "..." @@ -229,18 +187,18 @@ syntax = { ["ExpList"] = function(ast,tbl) local exprs = {} for k = 1,#ast do - exprs[#exprs + 1] = stringfor(ast[k],tbl) + exprs[k] = stringfor(ast[k],tbl) end return table.concat(exprs,",") end, ["Nil"] = function(ast,tbl) - return "nil" + return " nil " end, ["True"] = function(ast,tbl) - return "true" + return " true " end, ["False"] = function(ast,tbl) - return "false" + return " false " end, ["Return"] = function(ast,tbl) local retargs = {} @@ -316,6 +274,12 @@ syntax = { } local opname = ast[1] if uniop[opname] ~= nil then + --Some special case where the parser messes up, fix it here. + --It translates ~= into not ==, but the order of operations makes it so == is evaluated first, and not second. + if opname == "not" and ast[2]["tag"] == "Op" and ast[2][1] == "eq" then + ast[2][1] = "ne" + return stringfor(ast[2],tbl) + end local rhs = stringfor(ast[2],tbl) return uniop[opname] .. rhs end @@ -468,9 +432,9 @@ local function removespaces(str) end --Compress the string, and adds a little decompression code at the top. -local function compress(str) - -end +--local function compress(str) +-- +--end glum.minify = function(str, name) name = name or "anonymous" @@ -479,13 +443,77 @@ glum.minify = function(str, name) error(error_msg) end local localvar = { + ["numlocals"] = 0, ["strings"] = {}, ["ids"] = {}, ["lname"] = "", ["nids"] = {}, } --printtable(ast) - return --[[removespaces(]]stringfor(ast,localvar)--) + return stringfor(ast,localvar) +end + +glum.uglify = function(str) + local avalchars = {} + local capture_chars = {"%","(","[","\13"} + local skipchars = {} + for k,v in pairs(capture_chars) do skipchars[string.byte(v,1)] = true end + for k = 1, 128 do + if skipchars[k] then goto skip_loop end --Skip the % character + if string.find(str,string.char(k)) then + avalchars[k] = false + else + avalchars[k] = true + end + ::skip_loop:: + end + for k,v in pairs(skipchars) do + avalchars[k] = false + end + local prettifycode = [[ +local function p(s) + local r = { + %s + } + for k,v in pairs(r) do + s = s:gsub(v[2],v[1]) + end + return s +end +]] + local replacementtbl = {} + local cursor = 1 + for k,v in pairs(nonames) do + while not avalchars[cursor] do + cursor = cursor + 1 + end + replacementtbl[v] = cursor + avalchars[cursor] = false + end + assert(cursor < 128, "Unable to uglify file, not enough unused characters!") + local replacementstr = {} + for k,v in pairs(replacementtbl) do + local trs = string.format("{%q,%q}",k,string.char(v)) + replacementstr[#replacementstr + 1] = trs + end + local frepstr = table.concat(replacementstr,",") + local pcd = string.format(prettifycode,frepstr) + for k,v in pairs(replacementtbl) do + str = str:gsub(k,string.char(v)) + end + local numdeepcom = math.random(5) + 5 + return table.concat({ + pcd, + "\n", + "return assert(loadstring(p([", + string.rep("=",numdeepcom), + "[", + str, + "]", + string.rep("=",numdeepcom), + "])))()" + }) + --prettifycode = string.format(prettifycode,) end return glum diff --git a/src/parser.lua b/src/parser.lua index ffcca71..0770bf7 100644 --- a/src/parser.lua +++ b/src/parser.lua @@ -132,8 +132,6 @@ end --Fixes strings local function fix_str (str) - --print("I need to fix string:" .. str) - --print("attempt:" .. string.format("%q",str)) str = string.gsub(str, "\\a", "\a") str = string.gsub(str, "\\b", "\b") str = string.gsub(str, "\\f", "\f") @@ -146,7 +144,6 @@ local function fix_str (str) str = string.gsub(str, "\\'", "'") str = string.gsub(str, '\\"', '"') str = string.gsub(str, '\\\\', '\\') - --print("actual:" .. str) return str end @@ -420,7 +417,10 @@ local G = { V"Lua", function (n) return tonumber(n) end; ShortString = P'"' * C(((P'\\' * P(1)) + (P(1) - P'"'))^0) * P'"' + P"'" * C(((P"\\" * P(1)) + (P(1) - P"'"))^0) * P"'"; - String = V"LongString" + (V"ShortString" / function (s) return fix_str(s) end); + String = V"LongString" + (V"ShortString" / function (s) + --print("instead of " .. s .. " i return " .. fix_str(s)) + return s end); + --return fix_str(s) end); OrOp = kw("or") / "or" + symb("||") / "or"; AndOp = kw("and") / "and" + diff --git a/src/test.lua b/src/test.lua deleted file mode 100644 index 68de568..0000000 --- a/src/test.lua +++ /dev/null @@ -1,32 +0,0 @@ - local function a(b) - b=string.gsub(b,"\\","\\\\"); - b=string.gsub(b,"\a","\\\a"); - b=string.gsub(b,"\b","\\\b"); - b=string.gsub(b,"\f","\\\f"); - b=string.gsub(b,"\n","\n"); - b=string.gsub(b,"\r","\n"); - b=string.gsub(b,"\t","\\\t"); - b=string.gsub(b,"\v","\\\v"); - b=string.gsub(b,"\'","\\\'"); - b=string.gsub(b,"\"","\\\""); - return b -end -local function c(b) - b=string.gsub(b,"\\\a","\a"); - b=string.gsub(b,"\\\b","\b"); - b=string.gsub(b,"\\\f","\f"); - b=string.gsub(b,"\n","\n"); - b=string.gsub(b,"\n","\r"); - b=string.gsub(b,"\\\t","\t"); - b=string.gsub(b,"\\\v","\v"); - b=string.gsub(b,"\\\n","\n"); - b=string.gsub(b,"\\\n","\n"); - b=string.gsub(b,"\\\'","\'"); - b=string.gsub(b,"\\\"","\""); - b=string.gsub(b,"\\\\","\\"); - return b -end -local d= function() - local d={}; - local e=require("lpeg"); - local f= function()local f={};f.lineno= function(g,h) if h==1 then return 1,1 end local i,j=0,"";g=g["sub"](g,1,h).."\n"; for k in g["gmatch"](g,"[^\n]*[\n]") do i=i+1;j=k; end local k=j["len"](j,)-1; return i, not k==0 and k or 1 end ;f.new_scope= function(i) if not i.scope then i.scope=0; else i.scope=i.scope+1; end local f=i.scope;i.maxscope=f;i[f]={};i[f].label={};i[f].local={};i[f].goto={}; end ;f.begin_scope= function(i)i.scope=i.scope+1; end ;f.end_scope= function(i)i.scope=i.scope-1; end ;f.new_function= function(i) if not i.fscope then i.fscope=0; else i.fscope=i.fscope+1; end local j=i.fscope;i.function[j]={}; end ;f.begin_function= function(i)i.fscope=i.fscope+1; end ;f.end_function= function(i)i.fscope=i.fscope-1; end ;f.begin_loop= function(i) if not i.loop then i.loop=1; else i.loop=i.loop+1; end end ;f.end_loop= function(i)i.loop=i.loop-1; end ;f.insideloop= function(i) return i.loop and 0<i.loop end ; return f end ;f=f();e.locale(e)local g,h,i=e.P,e.S,e.V;local j,k,l,m=e.C,e.Carg,e.Cb,e.Cc;local n,o,p,q,r=e.Cf,e.Cg,e.Cmt,e.Cp,e.Ct;local s,t,u=e.alpha,e.digit,e.alnum;local v=e.xdigit;local w=e.space;local x=f.lineno;local y,z=f.new_scope,f.end_scope;local A,B=f.new_function,f.end_function;local C,D=f.begin_loop,f.end_loop;local E=f.insideloop;local F=e.P("/*");local G=e.P("*/");local H=(1-F)^0;local I=(1-G)^0;local J=F*I*G; local function K(L,M,N)local O,P=x(L.subject,M);local Q="%s:%d:%d: syntax error, %s"; return string.format(Q,L.filename,O,P,N) end local function O(P,Q,R) return R.ffp or Q,R end local function S() return p(k(1),O)*(j(i("OneWord"))+m("EOF"))/ function(R,T)R.unexpected=T; return R end end local function T() return S()/ function(R)local U=R.ffp or 1;local N="unexpected \'%s\', expecting %s";N=string.format(N,R.unexpected,R.expected); return nil,K(R,U,N) end end local function U() return T() end local function V(P,Q,R,W) if not R.ffp or R.ffp<Q then R.ffp=Q;R.list={};R.list[W]=W;R.expected="\'"..W.."\'"; elseif Q==R.ffp then if not R.list[W] then R.list[W]=W;R.expected="\'"..W.."\', "..R.expected; end end return false end local function X(Y) return p(k(1)*m(Y),V) end local function Z(aa,Y) return aa*i("Skip")+X(Y)*g(false) end local function ba(b) return Z(g(b),b) end local function ca(b) return Z(g(b)*-i("idRest"),b) end local function da(ea,aa) return r(o(q(),"pos")*o(m(ea),"tag")*aa) end local function fa(ga,ha) return {["tag"]="Op",["pos"]=ha.pos,[1]=ga,[2]=ha} end local function ia(ja,ga,ka) if not ga then return ja elseif ga=="add" or ga=="sub" or ga=="mul" or ga=="div" or ga=="idiv" or ga=="mod" or ga=="pow" or ga=="concat" or ga=="band" or ga=="bor" or ga=="bxor" or ga=="shl" or ga=="shr" or ga=="eq" or ga=="lt" or ga=="le" or ga=="and" or ga=="or" then return {["tag"]="Op",["pos"]=ja.pos,[1]=ga,[2]=ja,[3]=ka} elseif ga=="ne" then return fa("not",{["tag"]="Op",["pos"]=ja.pos,[1]="eq",[2]=ja,[3]=ka}) elseif ga=="gt" then return {["tag"]="Op",["pos"]=ja.pos,[1]="lt",[2]=ka,[3]=ja} elseif ga=="ge" then return {["tag"]="Op",["pos"]=ja.pos,[1]="le",[2]=ka,[3]=ja} end end local function la(aa,ma,na) return n(aa*o(ma*aa)^0,ia)+na end local function oa(aa,ma) return n(aa*o(ma*aa)^0,ia) end local function pa(aa,ma,ea) return da(ea,(aa*(ma*aa)^0)^(-1)) end local function qa(aa,ma,ea) return da(ea,aa*(ma*aa)^0) end local ra={i("Lua"),["Lua"]=i("Shebang")^(-1)*i("Skip")*i("Chunk")*-1+U(),["Chunk"]=i("Block"),["StatList"]=(ba(";")+i("Stat"))^0,["Var"]=i("Id"),["Id"]=da("Id",Z(i("Name"),"Name")),["FunctionDef"]=ca("function")*i("FuncBody"),["FieldSep"]=ba(",")+ba(";"),["Field"]=da("Pair",(ba("[")*i("Expr")*ba("]")*ba("=")*i("Expr"))+(da("String",Z(i("Name"),"Name"))*ba("=")*i("Expr")))+i("Expr"),["FieldList"]=(i("Field")*(i("FieldSep")*i("Field"))^0*i("FieldSep")^(-1))^(-1),["Constructor"]=da("Table",ba("{")*i("FieldList")*ba("}")),["NameList"]=qa(i("Id"),ba(","),"NameList"),["ExpList"]=qa(i("Expr"),ba(","),"ExpList"),["FuncArgs"]=ba("(")*(i("Expr")*(ba(",")*i("Expr"))^0)^(-1)*ba(")")+i("Constructor")+da("String",Z(i("String"),"String")),["Expr"]=i("SubExpr_1"),["SubExpr_1"]=oa(i("SubExpr_2"),i("OrOp")),["SubExpr_2"]=oa(i("SubExpr_3"),i("AndOp")),["SubExpr_3"]=oa(i("SubExpr_4"),i("RelOp")),["SubExpr_4"]=oa(i("SubExpr_5"),i("BOrOp")),["SubExpr_5"]=oa(i("SubExpr_6"),i("BXorOp")),["SubExpr_6"]=oa(i("SubExpr_7"),i("BAndOp")),["SubExpr_7"]=oa(i("SubExpr_8"),i("ShiftOp")),["SubExpr_8"]=i("SubExpr_9")*i("ConOp")*i("SubExpr_8")/ia+i("SubExpr_9"),["SubExpr_9"]=oa(i("SubExpr_10"),i("AddOp")),["SubExpr_10"]=oa(i("SubExpr_11"),i("MulOp")),["SubExpr_11"]=i("UnOp")*i("SubExpr_11")/fa+i("SubExpr_12"),["SubExpr_12"]=i("SimpleExp")*(i("PowOp")*i("SubExpr_11"))^(-1)/ia,["SimpleExp"]=da("Number",Z(i("Number"),"Number"))+da("String",Z(i("String"),"String"))+da("Nil",ca("nil"))+da("False",ca("false"))+da("True",ca("true"))+da("Dots",ba("..."))+i("FunctionDef")+i("Constructor")+i("SuffixedExp"),["SuffixedExp"]=n(i("PrimaryExp")*(da("DotIndex",ba(".")*da("String",Z(i("Name"),"Name")))+da("ArrayIndex",ba("[")*i("Expr")*ba("]"))+da("Invoke",o(ba(":")*da("String",Z(i("Name"),"Name"))*i("FuncArgs")))+da("Call",i("FuncArgs")))^0, function(sa,ta) if ta then if ta.tag=="Call" or ta.tag=="Invoke" then local R={["tag"]=ta.tag,["pos"]=sa.pos,[1]=sa}; for ua,va in ipairs(ta) do table.insert(R,va) end return R else return {["tag"]="Index",["pos"]=sa.pos,[1]=sa,[2]=ta[1]} end end return sa end ),["PrimaryExp"]=i("Var")+da("Paren",ba("(")*i("Expr")*ba(")")),["Block"]=da("Block",i("StatList")*i("RetStat")^(-1)),["IfStat"]=da("If",ca("if")*i("Expr")*ca("then")*i("Block")*(ca("elseif")*i("Expr")*ca("then")*i("Block"))^0*(ca("else")*i("Block"))^(-1)*ca("end")),["WhileStat"]=da("While",ca("while")*i("Expr")*ca("do")*i("Block")*ca("end")),["DoStat"]=ca("do")*i("Block")*ca("end")/ function(R)R.tag="Do"; return R end ,["ForBody"]=ca("do")*i("Block"),["ForNum"]=da("Fornum",i("Id")*ba("=")*i("Expr")*ba(",")*i("Expr")*(ba(",")*i("Expr"))^(-1)*i("ForBody")),["ForGen"]=da("Forin",i("NameList")*ca("in")*i("ExpList")*i("ForBody")),["ForStat"]=ca("for")*(i("ForNum")+i("ForGen"))*ca("end"),["RepeatStat"]=da("Repeat",ca("repeat")*i("Block")*ca("until")*i("Expr")),["FuncName"]=n(i("Id")*(ba(".")*da("String",Z(i("Name"),"Name")))^0, function(sa,ta) if ta then return {["tag"]="Index",["pos"]=sa.pos,[1]=sa,[2]=ta} end return sa end )*(ba(":")*da("String",Z(i("Name"),"Name")))^(-1)/ function(sa,ta) if ta then return {["tag"]="Index",["pos"]=sa.pos,["is_method"]=true,[1]=sa,[2]=ta} end return sa end ,["ParList"]=i("NameList")*(ba(",")*ba("...")*da("Dots",q()))^(-1)/ function(R,ua) if ua then table.insert(R,ua) end return R end +ba("...")*da("Dots",q())/ function(ua) return {ua} end +g(true)/ function() return {} end ,["FuncBody"]=da("Function",ba("(")*i("ParList")*ba(")")*i("Block")*ca("end")),["FuncStat"]=da("Set",ca("function")*i("FuncName")*i("FuncBody"))/ function(R) if R[1].is_method then table.insert(R[2][1],1,{["tag"]="Id",[1]="self"}) end R[1]={R[1]};R[2]={R[2]}; return R end ,["LocalFunc"]=da("Localrec",ca("function")*i("Id")*i("FuncBody"))/ function(R)R[1]={R[1]};R[2]={R[2]}; return R end ,["LocalAssign"]=da("Local",i("NameList")*((ba("=")*i("ExpList"))+r(m()))),["LocalStat"]=ca("local")*(i("LocalFunc")+i("LocalAssign")),["LabelStat"]=da("Label",ba("::")*Z(i("Name"),"Name")*ba("::")),["BreakStat"]=da("Break",ca("break")),["ContinueStat"]=da("Continue",ca("continue")),["GoToStat"]=da("Goto",ca("goto")*Z(i("Name"),"Name")),["RetStat"]=da("Return",ca("return")*(i("Expr")*(ba(",")*i("Expr"))^0)^(-1)*ba(";")^(-1)),["ExprStat"]=p((i("SuffixedExp")*(m( function()local va={...};local wa=va[#va];table.remove(va) for xa,ua in ipairs(va) do if ua.tag=="Id" or ua.tag=="Index" then va[xa]=ua; else return false end end va.tag="VarList";va.pos=va[1].pos; return true,{["tag"]="Set",["pos"]=va.pos,[1]=va,[2]=wa} end )*i("Assignment")))+(i("SuffixedExp")*(m( function(P) if P.tag=="Call" or P.tag=="Invoke" then return true,P end return false end ))), function(P,Q,va,wa,...) return wa(va,...) end ),["Assignment"]=((ba(",")*i("SuffixedExp"))^1)^(-1)*ba("=")*i("ExpList"),["Stat"]=i("IfStat")+i("WhileStat")+i("DoStat")+i("ForStat")+i("RepeatStat")+i("FuncStat")+i("LocalStat")+i("LabelStat")+i("BreakStat")+i("GoToStat")+i("ExprStat")+i("ContinueStat"),["Space"]=w^1,["Equals"]=g("=")^0,["Open"]="["*o(i("Equals"),"init")*"["*g("\n")^(-1),["Close"]="]"*j(i("Equals"))*"]",["CloseEQ"]=p(i("Close")*l("init"), function(P,Q,na,xa) return na==xa end ),["LongString"]=i("Open")*j((g(1)-i("CloseEQ"))^0)*i("Close")/ function(P,ya) return P end ,["Comment"]=g("--")*i("LongString")/ function() return end +g("--")*(g(1)-g("\n"))^0+g("//")*(g(1)-g("\n"))^0+j(J)/ function() return end ,["Skip"]=(i("Space")+i("Comment"))^0,["idStart"]=s+g("_"),["idRest"]=u+g("_"),["Keywords"]=g("and")+"break"+"do"+"elseif"+"else"+"end"+"false"+"for"+"function"+"goto"+"if"+"in"+"local"+"nil"+"not"+"or"+"repeat"+"return"+"then"+"true"+"until"+"while"+"continue",["Reserved"]=i("Keywords")*-i("idRest"),["Identifier"]=i("idStart")*i("idRest")^0,["Name"]=-i("Reserved")*j(i("Identifier"))*-i("idRest"),["Hex"]=(g("0x")+g("0X"))*v^1,["Expo"]=h("eE")*h("+-")^(-1)*t^1,["Float"]=(((t^1*g(".")*t^0)+(g(".")*t^1))*i("Expo")^(-1))+(t^1*i("Expo")),["Int"]=t^1,["Number"]=j(i("Hex")+i("Float")+i("Int"))/ function(W) return tonumber(W) end ,["ShortString"]=g("\"")*j(((g("\\")*g(1))+(g(1)-g("\"")))^0)*g("\"")+g("\'")*j(((g("\\")*g(1))+(g(1)-g("\'")))^0)*g("\'"),["String"]=i("LongString")+(i("ShortString")/ function(P) return c(P) end ),["OrOp"]=ca("or")/"or"+ba("||")/"or",["AndOp"]=ca("and")/"and"+ba("&&")/"and",["RelOp"]=ba("~=")/"ne"+ba("==")/"eq"+ba("<=")/"le"+ba(">=")/"ge"+ba("<")/"lt"+ba(">")/"gt"+ba("!=")/"ne",["BOrOp"]=ba("|")/"bor",["BXorOp"]=ba("~")/"bxor",["BAndOp"]=ba("&")/"band",["ShiftOp"]=ba("<<")/"shl"+ba(">>")/"shr",["ConOp"]=ba("..")/"concat",["AddOp"]=ba("+")/"add"+ba("-")/"sub",["MulOp"]=ba("*")/"mul"+ba("/")/"div"+ba("%")/"mod",["UnOp"]=ca("not")/"not"+ba("-")/"unm"+ba("#")/"len"+ba("~")/"bnot"+ba("!")/"not",["PowOp"]=ba("^")/"pow",["Shebang"]=g("#")*(g(1)-g("\n"))^0*g("\n"),["OneWord"]=i("Name")+i("Number")+i("String")+i("Reserved")+g("...")+g(1)}; local function za(Aa,f,Ba)local Ca=Ba[1]; for P=f,0,-1 do if Aa[P].label[Ca] then return true end end return false end local function Ca(Aa,Da,M)local f=Aa.scope;local Ea=Aa[f].label[Da]; if not Ea then Aa[f].label[Da]={["name"]=Da,["pos"]=M}; return true else local N="label \'%s\' already defined at line %d";local Fa=x(Aa.errorinfo.subject,Ea.pos);N=string.format(N,Da,Fa); return nil,K(Aa.errorinfo,M,N) end end local function Ea(Aa,Ba)local f=Aa.scope;table.insert(Aa[f].goto,Ba) return true end local function Fa(Aa) for P=Aa.maxscope,0,-1 do for Ga,ua in ipairs(Aa[P].goto) do if not za(Aa,P,ua) then local N="no visible label \'%s\' for <goto>";N=string.format(N,ua[1]); return nil,K(Aa.errorinfo,ua.pos,N) end end end return true end local function Ga(Aa,Ha)Aa.function[Aa.fscope].is_vararg=Ha; end local Ia,Ja,Kalocal La,Ma,Na,OaOa= function(Aa,Pa)local Qa=#Pa;local Ha=false; if 0<Qa and Pa[Qa].tag=="Dots" then Ha=true; end Ga(Aa,Ha) return true end ; local function Qa(Aa,Ra)A(Aa)y(Aa)local Sa,N=Oa(Aa,Ra[1]); if not Sa then return Sa,N end Sa,N,Sa,N=La(Aa,Ra[2]); if not Sa then return Sa,N end z(Aa)B(Aa) return true end local function Sa(Aa,Ra)local Ta,N=Ja(Aa,Ra[2]); if not Ta then return Ta,N end if Ra[3] then Ta,N,Ta,N=Ja(Aa,Ra[3]); if not Ta then return Ta,N end end return true end local function Ta(Aa,Ra)local Ua,N=Ja(Aa,Ra[1]); if not Ua then return Ua,N end return true end local function Ua(Aa,Va) for Wa,ua in ipairs(Va) do local ea=ua.tag; if ea=="Pair" then local Xa,N=Ja(Aa,ua[1]); if not Xa then return Xa,N end Xa,N,Xa,N=Ja(Aa,ua[2]); if not Xa then return Xa,N end else local Xa,N=Ja(Aa,ua); if not Xa then return Xa,N end end end return true end local function Wa(Aa,Ra) if not Aa.function[Aa.fscope].is_vararg then local N="cannot use \'...\' outside a vararg function"; return nil,K(Aa.errorinfo,Ra.pos,N) end return true end local function Xa(Aa,Ya)local Za,N=Ja(Aa,Ya[1]); if not Za then return Za,N end for Q=2,#Ya do Za,N,Za,N=Ja(Aa,Ya[Q]); if not Za then return Za,N end end return true end local function Za(Aa,ab)local bb,N=Ja(Aa,ab[1]); if not bb then return bb,N end for Q=3,#ab do bb,N,bb,N=Ja(Aa,ab[Q]); if not bb then return bb,N end end return true end local function bb(Aa,Ba)local cb,N=Na(Aa,Ba[1]); if not cb then return cb,N end cb,N,cb,N=Ma(Aa,Ba[2]); if not cb then return cb,N end return true end local function cb(Aa,Ba) if not E(Aa) then local N="<break> not inside a loop"; return nil,K(Aa.errorinfo,Ba.pos,N) end return true end local function db(Aa,Ba) if not E(Aa) then local N="<continue> not inside a loop"; return nil,K(Aa.errorinfo,Ba.pos,N) end return true end local function eb(Aa,Ba)C(Aa)y(Aa)local fb,N=Ma(Aa,Ba[2]); if not fb then return fb,N end fb,N,fb,N=La(Aa,Ba[3]); if not fb then return fb,N end z(Aa)D(Aa) return true end local function fb(Aa,Ba)local gb,NC(Aa)y(Aa)gb,N,gb,N=Ja(Aa,Ba[2]); if not gb then return gb,N end gb,N,gb,N=Ja(Aa,Ba[3]); if not gb then return gb,N end if Ba[5] then gb,N,gb,N=Ja(Aa,Ba[4]); if not gb then return gb,N end gb,N,gb,N=La(Aa,Ba[5]); if not gb then return gb,N end else gb,N,gb,N=La(Aa,Ba[4]); if not gb then return gb,N end end z(Aa)D(Aa) return true end local function gb(Aa,Ba)local hb,N=Ea(Aa,Ba); if not hb then return hb,N end return true end local function hb(Aa,Ba)local ib=#Ba; if ib%2==0 then for Q=1,ib,2 do local jb,N=Ja(Aa,Ba[Q]); if not jb then return jb,N end jb,N,jb,N=La(Aa,Ba[Q+1]); if not jb then return jb,N end end else for Q=1,ib-1,2 do local jb,N=Ja(Aa,Ba[Q]); if not jb then return jb,N end jb,N,jb,N=La(Aa,Ba[Q+1]); if not jb then return jb,N end end local jb,N=La(Aa,Ba[ib]); if not jb then return jb,N end end return true end local function ib(Aa,Ba)local jb,N=Ca(Aa,Ba[1],Ba.pos); if not jb then return jb,N end return true end local function jb(Aa,Ba)local kb,N=Ma(Aa,Ba[2]); if not kb then return kb,N end return true end local function kb(Aa,Ba)local lb,N=Ja(Aa,Ba[2][1]); if not lb then return lb,N end return true end local function lb(Aa,Ba)C(Aa)local mb,N=La(Aa,Ba[1]); if not mb then return mb,N end mb,N,mb,N=Ja(Aa,Ba[2]); if not mb then return mb,N end D(Aa) return true end local function mb(Aa,Ba)local nb,N=Ma(Aa,Ba); if not nb then return nb,N end return true end local function nb(Aa,Ba)C(Aa)local ob,N=Ja(Aa,Ba[1]); if not ob then return ob,N end ob,N,ob,N=La(Aa,Ba[2]); if not ob then return ob,N end D(Aa) return true end Ka= function(Aa,ob)local ea=ob.tag; if ea=="Id" then return true elseif ea=="Index" then local pb,N=Ja(Aa,ob[1]); if not pb then return pb,N end pb,N,pb,N=Ja(Aa,ob[2]); if not pb then return pb,N end return true else error("expecting a variable, but got a "..ea) end end ;Na= function(Aa,pb) for qb,ua in ipairs(pb) do local rb,N=Ka(Aa,ua); if not rb then return rb,N end end return true end ;Ja= function(Aa,Ra)local ea=Ra.tag; if ea=="Nil" or ea=="True" or ea=="False" or ea=="Number" or ea=="String" then return true elseif ea=="Dots" then return Wa(Aa,Ra) elseif ea=="Function" then return Qa(Aa,Ra) elseif ea=="Table" then return Ua(Aa,Ra) elseif ea=="Op" then return Sa(Aa,Ra) elseif ea=="Paren" then return Ta(Aa,Ra) elseif ea=="Call" then return Xa(Aa,Ra) elseif ea=="Invoke" then return Za(Aa,Ra) elseif ea=="Id" or ea=="Index" then return Ka(Aa,Ra) else error("expecting an expression, but got a "..ea) end end ;Ma= function(Aa,qb) for rb,ua in ipairs(qb) do local sb,N=Ja(Aa,ua); if not sb then return sb,N end end return true end ;Ia= function(Aa,Ba)local ea=Ba.tag; if ea=="Do" then return La(Aa,Ba) elseif ea=="Set" then return bb(Aa,Ba) elseif ea=="While" then return nb(Aa,Ba) elseif ea=="Repeat" then return lb(Aa,Ba) elseif ea=="If" then return hb(Aa,Ba) elseif ea=="Fornum" then return fb(Aa,Ba) elseif ea=="Forin" then return eb(Aa,Ba) elseif ea=="Local" then return jb(Aa,Ba) elseif ea=="Localrec" then return kb(Aa,Ba) elseif ea=="Goto" then return gb(Aa,Ba) elseif ea=="Label" then return ib(Aa,Ba) elseif ea=="Return" then return mb(Aa,Ba) elseif ea=="Break" then return cb(Aa,Ba) elseif ea=="Continue" then return db(Aa,Ba) elseif ea=="Call" then return Xa(Aa,Ba) elseif ea=="Invoke" then return Za(Aa,Ba) else error("expecting a statement, but got a "..ea) end end ;La= function(Aa,rb)local sb={};y(Aa) for tb,ua in ipairs(rb) do local ub,N=Ia(Aa,ua); if not ub then return ub,N end end z(Aa) return true end ; local function sb(tb,L)assert(type(tb)=="table")assert(type(L)=="table")local Aa={["errorinfo"]=L,["function"]={}};A(Aa)Ga(Aa,true)local ub,N=La(Aa,tb); if not ub then return ub,N end B(Aa)ub,N,ub,N=Fa(Aa); if not ub then return ub,N end return tb end d.parse= function(ub,vb)local L={["subject"]=ub,["filename"]=vb};local tb,wb=e.match(ra,ub,nil,L); if not tb then return tb,wb end return sb(tb,L) end ; return d end ;d=d();local e=require("lpeg");e.locale(e)local f={}; local function g(h)local i=type(h);local j if i=="table" then j={}; for k,l in next,h,nil do j[g(k)]=g(l); end setmetatable(j,g(getmetatable(h))) else j=h; end return j end local i={"if","for","end","do","local","then","else","elseif","return","goto","function","nil","false","true","repeat","return","break","and","or","not","in","repeat","until","while","continue"};local j={}; for k,l in ipairs(i) do j[l]=true; end local k="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"; local function l(m)local n=string.find(m,"["..k.."]");local o=string.len(m); if n==nil then return string.rep("a",o+1) else local p=string.byte(m,n);local q=string.char(p+(p<122 and 1 or -57));local r=string.sub(m,n+1,o);local s=string.rep("a",n-1);local t=s..q..r; while j[t] or _G[t] do t=l(t); end return t end end local function n(o,p)p=p or 0; for q,r in pairs(o) do for s=0,p do io.write("\t") end io.write(q..":") if type(r)=="table" then io.write("\n")n(r,p+1) else io.write(tostring(r).."\n") end end end local q={}; local function r(s,o) if not q[s.tag]==nil then return q[s.tag](s,o) else error("Attempted to use unknown tag type:"..s.tag) end end q={["Call"]= function(s,o)local t=r(s[1],o);local u={};local v=2; while not s[v]==nil do u[v-1]=r(s[v],o);v=v+1; end local w=table.concat(u,",");local x=table.concat({t,"(",w,")"}); return x end ,["Invoke"]= function(s,o)local t=r(s[1],o);local u={}; for v=3,#s do u[#u+1]=r(s[v],o); end local w=t;local x if s[2].tag=="String" and #s[2][1]<(#t+2) then x=s[2][1];w=w..":"..x.."("; else x=r(s[2],o);w=w.."["..x.."]("..t..","; end w=w..table.concat(u,",");w=w..")"; return w end ,["String"]= function(s,o) if o.strings[s[1]]==nil then local t=a(s[1]);local u=string.format("\"%s\"",t);print("Returning "..u.." from "..s[1].." out of "..t) return u end return o.strings[s[1]] end ,["Id"]= function(s,o) if o.ids[s[1]]==nil then return s[1] end return o.ids[s[1]] end ,["Index"]= function(s,o)local t=r(s[1],o); if s[2].tag=="String" then return table.concat({t,".",s[2][1]}) end return table.concat({t,"[",r(s[2],o),"]"}) end ,["Paren"]= function(s,o) return table.concat({"("..r(s[1],o)..")"}) end ,["Dots"]= function(s,o) return "..." end ,["Forin"]= function(s,o)local t=g(o);local u=r(s[1],t);local v=r(s[2],t);local w=r(s[3],t);local x=table.concat({" for ",u," in ",v," do ",w," end "}); return x end ,["NameList"]= function(s,o)local t={}; for u=1,#s do if not s[u].tag=="Id" then t[#t+1]=r(s[u]); else if not o.ids[s[u][1]]==nil then t[#t+1]=o.ids[s[u][1]]; else local v=l(o.lname);o.lname=v;o.ids[s[u][1]]=v;t[#t+1]=v; end end end local v=table.concat(t,","); return v end ,["ExpList"]= function(s,o)local t={}; for u=1,#s do t[#t+1]=r(s[u],o); end return table.concat(t,",") end ,["Nil"]= function(s,o) return "nil" end ,["True"]= function(s,o) return "true" end ,["False"]= function(s,o) return "false" end ,["Return"]= function(s,o)local t={}; for u,v in ipairs(s) do t[u]=r(v,o); end return " return "..table.concat(t,",") end ,["If"]= function(s,o)local t=r(s[1],o);local u=r(s[2],o);local v={};v[#v+1]=table.concat({" if ",t," then ",u}); for w=3,#s-1,2 do local x=r(s[w],o);local y=r(s[w+1],o);v[#v+1]=table.concat({" elseif ",x," then ",y}); end if #s%2==1 then local x=r(s[#s],o);v[#v+1]=" else "..x; end v[#v+1]=" end "; return table.concat(v) end ,["Fornum"]= function(s,o)local t if s[1].tag=="Id" then if not o.ids[s[1][1]]==nil then t=o.ids[s[1][1]]; else local u=l(o.lname);o.lname=u;o.ids[s[1][1]]=u;t=u; end else t=r(s[1],o); end local u=r(s[2],o);local v=r(s[3],o);local w=1;local x=""; if not s[4].tag=="Block" then w=r(s[4],o);x=r(s[5],o); else x=r(s[4],o); end local y= not w==1 and (","..w) or "";o[t]=nil; return table.concat({" for ",t,"=",u,",",v,y," do ",x," end "}) end ,["Op"]= function(s,o)local t={["or"]=" or ",["and"]=" and ",["ne"]="~=",["eq"]="==",["le"]="<=",["ge"]=">=",["lt"]="<",["gt"]=">",["bor"]="|",["bxor"]="~",["band"]="&",["shl"]="<<",["shr"]=">>",["concat"]="..",["add"]="+",["sub"]="-",["mul"]="*",["div"]="/",["mod"]="%",["pow"]="^"};local u={["len"]="#",["not"]=" not ",["unm"]="-",["bnot"]="~"};local v=s[1]; if not u[v]==nil then local w=r(s[2],o); return u[v]..w end local w=r(s[2],o);local x=r(s[3],o);local y=table.concat({w,t[v],x}); return y end ,["Pair"]= function(s,o)local t=r(s[1],o);local u=r(s[2],o); return table.concat({"[",t,"]=",u}) end ,["Table"]= function(s,o)local t={}; for u=1,#s do t[#t+1]=r(s[u],o); end local v=table.concat(t,","); return table.concat({"{",v,"}"}) end ,["Number"]= function(s,o) return s[1] end ,["Local"]= function(s,o)local t=o;local u,v=r(s[1],t),nil; if not s[2].tag==nil then v=r(s[2],t); end local w="local "..u; if not s[2].tag==nil then w=w.."="..v..";"; end return w end ,["VarList"]= function(s,o)local t={}; for u=1,#s do t[#t+1]=r(s[u],o); end return table.concat(t,",") end ,["Set"]= function(s,o)local t={};local u= not s[1].tag==nil and s[1] or s[1][1]; for v=1,#s[1] do t[#t+1]=r(u,o); end local w={};local x= not s[2].tag==nil and s[2] or s[2][1]; for v=1,#s[2] do w[#w+1]=r(x,o); end local y=table.concat(t,",");y=y.."="..table.concat(w,","); return y..";" end ,["Label"]= function(s,o) if o.nids[s[1]]==nil then local t=l(o.lname);o.lname=t;o.nids[s[1]]=t; end return "::"..o.nids[s[1]].."::" end ,["Goto"]= function(s,o) if o.nids[s[1]]==nil then local t=l(o.lname);o.lname=t;o.nids[s[1]]=t; end return " goto "..o.nids[s[1]] end ,["Function"]= function(s,o)local t= not s[1].tag==nil and r(s[1],o) or "";local u=r(s[2],o); return table.concat({" function(",t,")",u," end "}) end ,["Localrec"]= function(s,o)local t if not o.ids[s[1][1]]==nil then t=o.ids[s[1][1]]; else local u=l(o.lname);o.lname=u;o.ids[s[1][1][1]]=u;t=u; end local u= not s[2][1][1].tag==nil and r(s[2][1][1],o) or "";local v=r(s[2][1][2],o); return table.concat({" local function ",t,"(",u,")",v," end "}) end ,["Continue"]= function(s,o) return " continue " end ,["While"]= function(s,o)local t=r(s[1],o);local u=r(s[2],o);local v=table.concat({" while ",t," do ",u," end "}); return v end ,["Break"]= function(s,o) return " break " end ,["Block"]= function(s,t)local o=g(t);t.block=true;local u={}; for v=1,#s do u[#u+1]=r(s[v],o); end local w=table.concat(u);local x,y={},{}; for v,z in pairs(o.strings) do if not t.strings[v]==o.strings[v] then x[#x+1]=z;y[#y+1]=string.format("%q",v); end end local z="";local A=" local "..table.concat(x,",");local B=table.concat(y,","); if 0<string.len(B) then z=table.concat({A,"=",B,";"}); end return z..w end }; local function u(b)local v={{"%s*%)%s*","%)"},{"%s*%(%s*","%("},{"%s*;%s*",";"},{"%s*,%s*",","},{";+",";"},{"^%s*",""},{"%s*$",""},{"%s+"," "}}; for w,x in ipairs(v) do b=string.gsub(b,x[1],x[2]); end return b end local function v(b) end f.minify= function(b,w)w=w or "anonymous";local s,x=d.parse(b,w); if not s then error(x) end local y={["strings"]={},["ids"]={},["lname"]="",["nids"]={}}; return r(s,y) end ; return f |
