diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2016-11-26 09:44:09 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2016-11-26 09:44:09 -0500 |
| commit | f96040eb138efdc8f13521bade5bf8bd299bb9fb (patch) | |
| tree | 3e294ca3dd504028960a72cb44b17450d6f61266 /src | |
| parent | 81fb5ea551c3dd8cfcb8e30c0dfed32a9000eea1 (diff) | |
| download | glum-f96040eb138efdc8f13521bade5bf8bd299bb9fb.tar.gz glum-f96040eb138efdc8f13521bade5bf8bd299bb9fb.tar.bz2 glum-f96040eb138efdc8f13521bade5bf8bd299bb9fb.zip | |
Started to remove extraneous spaces
Diffstat (limited to 'src')
| -rw-r--r-- | src/compile.lua | 31 | ||||
| -rw-r--r-- | src/glum.lua | 387 | ||||
| -rw-r--r-- | src/parser_min.lua | 554 | ||||
| -rw-r--r-- | src/uglify.lua | 81 |
4 files changed, 892 insertions, 161 deletions
diff --git a/src/compile.lua b/src/compile.lua deleted file mode 100644 index 13f652e..0000000 --- a/src/compile.lua +++ /dev/null @@ -1,31 +0,0 @@ ---[[ - 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 969dc27..f4385c0 100644 --- a/src/glum.lua +++ b/src/glum.lua @@ -14,6 +14,11 @@ This moudle allows you to minify gLua code lpeg ]] +--Abandon all hope, ye who enter here +--Refer to the comments at the top of parser.lua for what each function should do. +--If anyone ever decides to add new language features, they need to be added to BOTH parser.lua and here. +--Someone should rewrite this much cleaner. + local parser local msg if include ~= nil then @@ -44,6 +49,10 @@ local function deepcopy(orig) return copy end +--Is the last character in the built-so-far string a character? +--Used to know if we should insert a space after it +local last = false + --A list of reserved words that cannot be used as variable names local nonames = {"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 reservednames = {} @@ -130,49 +139,64 @@ local function stringfor(ast,tbl) 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 anyone ever decides to add new language features, they need to be added to BOTH parser.lua and here. + syntax = { ["Call"] = function(ast,tbl) + print("ast for call is") + printtable(ast) local exprname = stringfor(ast[1],tbl) + print("String for expname is",exprname) + last = false local argnames = {} local cursor = 2 while ast[cursor] ~= nil do argnames[cursor-1] = stringfor(ast[cursor],tbl) cursor = cursor + 1 + last = false end local argstring = table.concat(argnames,",") local ostring = table.concat({exprname,"(",argstring,")"}) + last = false return ostring end, ["Invoke"] = function(ast,tbl) - local func = stringfor(ast[1],tbl) - local invargs = {} - for k = 3,#ast do - invargs[#invargs + 1] = stringfor(ast[k],tbl) - end - local output = func - local inv - --A short hand if it's a simple thing + local ret = {} + ret[1] = stringfor(ast[1],tbl) -- The table + last = false + --If it's a . then use oo notation if ast[2].tag == "String" and ast[2][1]:find(" ") == nil and tbl.strings[ast[2][1]] == nil then - inv = ast[2][1] - output = table.concat({output, ":", inv, "("}) + ret[2] = ":" + ret[3] = ast[2][1] + ret[4] = "(" elseif tbl.strings[ast[2][1]] ~= nil then - output = table.concat({output,"[", tbl.strings[ast[2][1]], "]","("}) + ret[2] = "[" + ret[3] = tbl.strings[ast[2][1]] + ret[4] = "](" else - inv = stringfor(ast[2],tbl) - output = table.concat({output, "[", inv, "](", func, ","}) + last = false + ret[2] = "[" + ret[3] = stringfor(ast[2],tbl) + ret[4] = "](" + ret[5] = ast[1] + ret[6] = "," + end + last = false + local args = {} + for k = 3,#ast do + args[#args + 1] = stringfor(ast[k],tbl) + last = false end - output = output .. table.concat(invargs,",") .. ")" - return output + ret[#ret+1] = table.concat(args,",") + ret[#ret+1] = ")" + last = false + return table.concat(ret) end, ["String"] = function(ast,tbl) local sop,eop = "\"","\"" - print("looking for",ast[1],"in") - printtable(tbl.strings) + --print("looking for",ast[1],"in") + --printtable(tbl.strings) if tbl.strings[ast[1]] then - print("Found it, it is", tbl.strings[ast[1]]) + --print("Found it, it is", tbl.strings[ast[1]]) return tbl.strings[ast[1]] end if tbl.strings[ast[1]] == nil then @@ -182,41 +206,70 @@ syntax = { end return table.concat({sop,ast[1],eop}) end - print("Returning non-catated string") + --print("Returning non-catated string") + last = false return tbl.strings[ast[1]] end, ["Id"] = function(ast,tbl) + local ret + if last then ret = " " else ret = "" end + if ast[1] == "pairs" then print("in id, ast1 is pairs") printtable(tbl.ids) end if tbl.ids[ast[1]] == nil then - return ast[1] + ret = ret .. ast[1] + last = true + return ret end - return tbl.ids[ast[1]] + ret = ret .. tbl.ids[ast[1]] + last = true + return ret end, ["Index"] = function(ast,tbl) local globalvar = stringfor(ast[1],tbl) if ast[2].tag == "String" then - return table.concat({globalvar, ".", ast[2][1]}) + last = true + return table.concat({globalvar, ".", ast[2][1]}) end - return table.concat({globalvar, "[", stringfor(ast[2],tbl), "]"}) + last = false + local ret = table.concat({globalvar, "[", stringfor(ast[2],tbl), "]"}) + last = false + return ret end, ["Paren"] = function(ast,tbl) + last = false return table.concat({"(",stringfor(ast[1],tbl),")"}) end, ["Dots"] = function(ast,tbl) + last = false return "..." end, ["Forin"] = function(ast,tbl) + print("At forin, ast is") + printtable(ast) + local codetbl = {} + if last then codetbl[1] = " for" else codetbl[1] = "for" end + last = true local nadd = deepcopy(tbl) local nl = stringfor(ast[1],nadd) + codetbl[2] = nl + if last then codetbl[3] = " in" else codetbl[3] = "in" end + last = true nadd.numlocals = nadd.numlocals + #ast[1] - print("Found",#ast[1],"locals as Forin") local el = stringfor(ast[2],nadd) + codetbl[4] = el + if last then codetbl[5] = " do" else codetbl[5] = "do" end + last = true local code = stringfor(ast[3],nadd) - local output = table.concat({" for ", nl, " in ", el, " do ", code, " end "}) + codetbl[6] = code + if last then codetbl[7] = " end" else codetbl[7] = "end" end + last = true + local output = table.concat(codetbl) tbl.numlocals = nadd.numlocals return output end, ["NameList"] = function(ast,tbl) local outputtbl = {} + local bef + if last then bef = " " else bef = "" end for k = 1,#ast do if ast[k].tag ~= "Id" then outputtbl[#outputtbl + 1] = stringfor(ast[k]) @@ -229,68 +282,121 @@ syntax = { tbl.ids[ast[k][1]] = newvar outputtbl[#outputtbl + 1] = newvar end + last = false end end - local output = table.concat(outputtbl, ",") + local output = bef .. table.concat(outputtbl, ",") + last = true return output end, ["ExpList"] = function(ast,tbl) local exprs = {} + local bef + if last then bef = " " else bef = "" end for k = 1,#ast do exprs[k] = stringfor(ast[k],tbl) + last = false end + last = true return table.concat(exprs,",") end, ["Nil"] = function(ast,tbl) - return " nil " + local ret + if last then ret = " nil" else ret = "nil" end + last = true + return ret end, ["True"] = function(ast,tbl) - return " true " + local ret + if last then ret = " true" else ret = "true" end + last = true + return ret end, ["False"] = function(ast,tbl) - return " false " + local ret + if last then ret = " false" else ret = "false" end + last = true + return ret end, ["Return"] = function(ast,tbl) local retargs = {} + local ccat + if last then + ccat = " return" + else + ccat = "return" + end + last = true for k,v in ipairs(ast) do retargs[k] = stringfor(v,tbl) + last = false end - return " return " .. table.concat(retargs,",") + last = true + return ccat .. table.concat(retargs,",") end, ["Do"] = function(ast,tbl) local ntbl = deepcopy(tbl) + local argparts = {} + if last then argparts[1] = " do" else argparts[1] = "do" end + last = true local allst = {} for k = 1,#ast do allst[k] = stringfor(ast[k],ntbl) end local code = table.concat(allst,";") + argparts[2] = code tbl.numlocals = ntbl.numlocals - return table.concat({" do ", code," end "}) + if last then argparts[3] = " end" else argparts[3] = "end" end + last = true + return table.concat(argparts) end, ["If"] = function(ast,tbl) - + local exparts = {} + if last then exparts[1] = " if" else exparts[1] = "if" end + last = true local expr1 = stringfor(ast[1],tbl) + exparts[2] = expr1 + if last then exparts[3] = " then" else exparts[3] = "then" end + last = true local block1 = stringfor(ast[2],tbl) + exparts[4] = block1 local codeblocks = {} - codeblocks[#codeblocks + 1] = table.concat({" if ",expr1," then ",block1}) + codeblocks[#codeblocks + 1] = table.concat(exparts) for k = 3,#ast-1,2 do + local efargs = {} + if last then efargs[1] = " elseif" else efargs[1] = "elseif" end + last = true local expr = stringfor(ast[k],tbl) + efargs[2] = expr + if last then efargs[3] = " then" else efargs[3] = "then" end + last = true local block = stringfor(ast[k + 1],tbl) - codeblocks[#codeblocks + 1] = table.concat({" elseif " , expr , " then " , block}) + codeblocks[#codeblocks + 1] = table.concat(efargs) end if #ast % 2 == 1 then local block = stringfor(ast[#ast],tbl) if block ~= "" then --If for some reason there's an empty else block, forget about it. - codeblocks[#codeblocks + 1] = " else " .. block + if last then + codeblocks[#codeblocks + 1] = " else" .. block + else + codeblocks[#codeblocks + 1] = "else" .. block + end end end - codeblocks[#codeblocks + 1] = " end " + local estr + if last then estr = " end" else estr = "end" end + codeblocks[#codeblocks + 1] = estr + last = true return table.concat(codeblocks) end, ["Fornum"] = function(ast,tbl) + local spargs = {} + if last then spargs[1] = " for " else spargs[1] = "for " end + last = false local var + assert(ast[1].tag == "Id","Oh no, I was expecting an ID!") if ast[1].tag == "Id" then if tbl.ids[ast[1][1]] ~= nil then var = tbl.ids[ast[1][1]] @@ -300,28 +406,52 @@ syntax = { tbl.ids[ast[1][1]] = newvar var = newvar end + --[[ else var = stringfor(ast[1],tbl) + ]] end + spargs[2] = var + spargs[3] = "=" + last = false local start = stringfor(ast[2],tbl) + spargs[4] = start + spargs[5] = "," + last = false local endnum = stringfor(ast[3],tbl) + spargs[6] = endnum local incrementer = 1 local code = "" + spargs[7] = "" if ast[4].tag ~= "Block" then -- incrementer incrementer = stringfor(ast[4],tbl) + if incrementer ~= 1 then + spargs[7] = "," .. incrementer + end + if last then spargs[8] = " do" else spargs[8] = "do" end + last = true code = stringfor(ast[5],tbl) + spargs[9] = code + if last then spargs[10] = " end" else spargs[10] = "end" end + last = true else + if last then spargs[8] = " do" else spargs[8] = "do" end + last = true code = stringfor(ast[4],tbl) + spargs[9] = code + if last then spargs[10] = " end" else spargs[10] = "end" end + last = true end local incstr = incrementer ~= 1 and ("," .. incrementer) or "" tbl[var] = nil tbl.numlocals = tbl.numlocals + 1 - print("Found 1 locals as Fornum") - return table.concat({" for ",var,"=",start,",",endnum,incstr," do ",code," end "}) + --print("Found 1 locals as Fornum") + return table.concat(spargs) end, ["Op"] = function(ast,tbl) + --NOTE: Bitwise operators << and >> are not supported in LuaJIT (lua 5.1) and were introduced in lua 5.3, if the operators are ever supported, stuff should just work. local binop = { - ["or"] = " or ", ["and"] = " and ", + ["or"] = "or", ["and"] = "and", ["ne"] = "~=", ["eq"] = "==", ["le"] = "<=", ["ge"] = ">=", ["lt"] = "<", ["gt"] = ">", @@ -333,7 +463,7 @@ syntax = { ["mod"] = "%", ["pow"] = "^", } local uniop = { - ["len"] = "#", ["not"] = " not ", + ["len"] = "#", ["not"] = "not", ["unm"] = "-", ["bnot"] = "~", } local opname = ast[1] @@ -342,14 +472,28 @@ syntax = { --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) + local ret = stringfor(ast[2],tbl) + return ret end local rhs = stringfor(ast[2],tbl) - return uniop[opname] .. rhs + local bef + if last then bef = " " else bef = "" end + return bef .. uniop[opname] .. rhs end + local sargs = {} local lhs = stringfor(ast[2],tbl) + sargs[1] = lhs + if last and (opname == "or" or opname == "and") then + sargs[2] = " " + last = true + else + sargs[2] = "" + last = false + end + sargs[3] = binop[opname] local rhs = stringfor(ast[3],tbl) - local output = table.concat({lhs,binop[opname],rhs}) + sargs[4] = rhs + local output = table.concat(sargs) return output end, ["Pair"] = function(ast,tbl) @@ -359,25 +503,33 @@ syntax = { end, ["Table"] = function(ast,tbl) local fields = {} + last = false for k = 1, #ast do fields[#fields + 1] = stringfor(ast[k],tbl) + last = false end local fieldstr = table.concat(fields,",") + last = false return table.concat({"{",fieldstr,"}"}) end, ["Number"] = function(ast,tbl) - return ast[1] + local ret + if last then ret = " " .. ast[1] else ret = ast[1] end + last = true + return ret end, ["Local"] = function(ast,tbl) + local bef + if last then bef = " " else bef = "" end + last = true local lhs,rhs = stringfor(ast[1],tbl),nil tbl.numlocals = tbl.numlocals + #ast[1] - print("Found",#ast[1],"locals as Local") + --print("Found",#ast[1],"locals as Local") + local output = bef .. "local" .. lhs if ast[2].tag ~= nil then + last = false rhs = stringfor(ast[2],tbl) - end - local output = "local " .. lhs - if ast[2].tag ~= nil then - output = output .. "=" .. rhs .. ";" + output = output .. "=" .. rhs end return output end, @@ -385,7 +537,9 @@ syntax = { local vars = {} for k = 1,#ast do vars[#vars + 1] = stringfor(ast[k],tbl) + last = false end + last = true return table.concat(vars,",") end, ["Set"] = function(ast,tbl) @@ -393,15 +547,17 @@ syntax = { local a1 = ast[1].tag ~= nil and ast[1] or ast[1][1] for k = 1,#ast[1] do lhs[#lhs + 1] = stringfor(a1,tbl) + last = false end local rhs = {} local a2 = ast[2].tag ~= nil and ast[2] or ast[2][1] for k = 1,#ast[2] do + last = false rhs[#rhs + 1] = stringfor(a2,tbl) end local ostring = table.concat(lhs,",") ostring = ostring .. "=" .. table.concat(rhs,",") - return ostring .. ";" + return ostring end, ["Label"] = function(ast,tbl) if tbl.nids[ast[1]] == nil then @@ -409,6 +565,7 @@ syntax = { tbl.lname = nextvar tbl.nids[ast[1]] = nextvar end + last = false return "::" .. tbl.nids[ast[1]] .. "::" end, ["Goto"] = function(ast,tbl) @@ -417,12 +574,20 @@ syntax = { tbl.lname = nextvar tbl.nids[ast[1]] = nextvar end - return " goto " .. tbl.nids[ast[1]] + last = true + return (last and " " or "") .. "goto " .. tbl.nids[ast[1]] end, ["Function"] = function(ast,tbl) + local funcstr + if last then funcstr = " function(" else funcstr = "function(" end + last = false local funcargs = ast[1].tag ~= nil and stringfor(ast[1],tbl) or "" + last = false local code = stringfor(ast[2],tbl) - return table.concat({" function(",funcargs,")",code," end "}) + local endstr + if last then endstr = " end" else endstr = "end" end + last = true + return table.concat({funcstr,funcargs,")",code,endstr}) end, ["Localrec"] = function(ast,tbl) local ident @@ -434,23 +599,46 @@ syntax = { tbl.ids[ast[1][1][1]] = newvar ident = newvar end + local locfuncstr + if last then locfuncstr = " local function " else locfuncstr = "local function " end + last = false local argstr = ast[2][1][1].tag ~= nil and stringfor(ast[2][1][1],tbl) or "" + last = false local expr = stringfor(ast[2][1][2],tbl) + local endstr + if last then endstr = " end" else endstr = "end" end + last = true tbl.numlocals = tbl.numlocals + 1 - print("Found 1 local as Localrec") - return table.concat({" local function ",ident,"(",argstr,")",expr," end "}) + print(string.format("At localrec, locfuncstr:%q ident:%q argstr:%q expr:%q endstr:%q last:%q",locfuncstr,ident,argstr,expr,endstr,tostring(last))) + --print("Found 1 local as Localrec") + return table.concat({locfuncstr,ident,"(",argstr,")",expr,endstr}) end, ["Continue"] = function(ast,tbl) - return " continue " + local ret + if last then ret = " continue" else ret = "continue" end + last = true + return ret end, ["While"] = function(ast,tbl) + local whilestr + if last then whilestr = " while" else whilestr = "while" end + last = true local expr = stringfor(ast[1],tbl) + local dostr + if last then dostr = " do" else dostr = "do" end + last = true local block = stringfor(ast[2],tbl) - local output = table.concat({" while " , expr , " do " , block , " end "}) + local endstr + if last then endstr = " end" else endstr = "end" end + last = true + local output = table.concat({whilestr, expr , dostr, block , endstr}) return output end, ["Break"] = function(ast,tbl) - return " break " + local ret + if last then ret = " break" else ret = "break" end + last = true + return ret end, ["Block"] = function(ast,oldtbl) local tbl = deepcopy(oldtbl) @@ -468,6 +656,7 @@ syntax = { --Removes extra spaces and duplicated ; from a string +--[[ local function removespaces(str) local removables = { {"%s*%)%s*","%)"}, --Spaces before or after ) @@ -485,6 +674,7 @@ local function removespaces(str) end return str end +]] --Compress the string, and adds a little decompression code at the top. --local function compress(str) @@ -498,6 +688,8 @@ glum.minify = function(str, name) error(error_msg) return nil end + print("in glum.minify, ast is ") + printtable(ast) print("Finding string reps") local strreps = getstringreps(ast) printtable(strreps) @@ -512,13 +704,12 @@ glum.minify = function(str, name) --printtable(ast) local lvt = deepcopy(olocalvar) - local ret = stringfor(ast,olocalvar) local numstrreplaced = 0 local maxlocals = lvt.numlocals while (numstrreplaced + maxlocals < 200) and --We have some locals left (numstrreplaced < #strreps) and --We have more strings to replace - (strreps[numstrreplaced+1][2] > 5) do --Replaceing this string will at least cover the cost of "local " + ((strreps[numstrreplaced + 1][2] * #strreps[numstrreplaced + 1][1]) > (strreps[numstrreplaced + 1][2] * 4 + 6)) do --Replaceing this string will at least cover the cost of "local " and [] numstrreplaced = numstrreplaced + 1 local nvar = getnextvarname(olocalvar.lname) olocalvar.strings[strreps[numstrreplaced][1]] = nvar @@ -531,82 +722,18 @@ glum.minify = function(str, name) rhss[#rhss + 1] = string.format("%q",k) end local inits = "" - print("lhss is") - printtable(lhss) + --print("lhss is") + --printtable(lhss) local lhs = " local " .. table.concat(lhss,",") local rhs = table.concat(rhss,",") if string.len(rhs) > 0 then inits = table.concat({lhs, "=", rhs, ";"}) end - print("Before doing stringfor for the second time, olocalvar is") - printtable(olocalvar) + --print("Before doing stringfor for the second time, olocalvar is") + --printtable(olocalvar) return inits .. stringfor(ast,olocalvar) end -glum.uglify = function(str) - assert(str ~= nil, "Cannot uglify a nil string") - 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_min.lua b/src/parser_min.lua new file mode 100644 index 0000000..a627cc4 --- /dev/null +++ b/src/parser_min.lua @@ -0,0 +1,554 @@ +local a, b, d, e, c = "String", "errorinfo", "function", "cannot use '...' outside a vararg function", "SuffixedExp" + local f = {} + local g = require("lpeg") + local h + + if include ~= nil then + h = include("./scope.lua") + else + h = dofile("../src/scope.lua") + end + + g.locale(g) + local i, j, k = g.P, g.S, g.V + local l, m, n, o = g.C, g.Carg, g.Cb, g.Cc + local p, q, r, s, t = g.Cf, g.Cg, g.Cmt, g.Cp, g.Ct + local u, v, w = g.alpha, g.digit, g.alnum + local x = g.xdigit + local y = g.space + local z = h.lineno + local A, B = h.new_scope, h.end_scope + local C, D = h.new_function, h.end_function + local E, F = h.begin_loop, h.end_loop + local G = h.insideloop + local H = g.P("/*") + local I = g.P("*/") + local J = (1 - H) ^ 0 + local K = (1 - I) ^ 0 + local L = H * K * I + + local function M(N, O, P) + local Q, R = z(N.subject, O) + local S = "%s:%d:%d: syntax error, %s" + + return string.format(S, N.filename, Q, R, P) + end + + local function Q(R, S, T) + return T.ffp or S, T + end + + local function U() + return r(m(1), Q) * (l(k("OneWord")) + o("EOF")) / function(T, V) + T.unexpected = V + + return T + end + end + + local function V() + return U() / function(T) + local W = T.ffp or 1 + local P = "unexpected '%s', expecting %s" + P = string.format(P, T.unexpected, T.expected) + + return nil, M(T, W, P) + end + end + + local function W() + return V() + end + + local function X(R, S, T, Y) + if not T.ffp or T.ffp < S then + T.ffp = S + T.list = {} + T.list[Y] = Y + T.expected = "'" .. Y .. "'" + elseif S == T.ffp then + end + + return false + end + + local function Z(aa) + return r(m(1) * o(aa), X) + end + + local function ba(ca) + ca = string.gsub(ca, "\\a", "\a") + ca = string.gsub(ca, "\\b", "\b") + ca = string.gsub(ca, "\\f", "\f") + ca = string.gsub(ca, "\\n", "\n") + ca = string.gsub(ca, "\\r", "\r") + ca = string.gsub(ca, "\\t", "\t") + ca = string.gsub(ca, "\\v", "\v") + ca = string.gsub(ca, "\\\n", "\n") + ca = string.gsub(ca, "\\\r", "\n") + ca = string.gsub(ca, "\\'", "'") + ca = string.gsub(ca, [[\\"]], [["]]) + ca = string.gsub(ca, "\\\\", "\\") + + return ca + end + + local function da(ea, aa) + return ea * k("Skip") + Z(aa) * i(false) + end + + local function fa(ca) + return da(i(ca), ca) + end + + local function ga(ca) + return da(i(ca) * -k("idRest"), ca) + end + + local function ha(ia, ea) + return t(q(s(), "pos") * q(o(ia), "tag") * ea) + end + + local function ja(ka, la) + return { + ["tag"] = "Op", + ["pos"] = la.pos, + [1] = ka, + [2] = la + } + end + + local function ma(na, ka, oa) + if not ka then + return na + elseif ka == "add" then + orka"sub" + orka"mul" + orka"div" + orka"idiv" + orka"mod" + orka"pow" + orka"concat" + orka"band" + orka"bor" + orka"bxor" + orka"shl" + orka"shr" + orka"eq" + orka"lt" + orka"le" + orka"and" + orka"or" + elseif ka == "ne" then + elseif ka == "gt" then + elseif ka == "ge" then + end + end + + local function pa(ea, qa, ra) + return p(ea * q(qa * ea) ^ 0, ma) + ra + end + + local function sa(ea, qa) + return p(ea * q(qa * ea) ^ 0, ma) + end + + local function ta(ea, qa, ia) + return ha(ia, (ea * (qa * ea) ^ 0) ^ (-1)) + end + + local function ua(ea, qa, ia) + return ha(ia, ea * (qa * ea) ^ 0) + end + + local va = { + k("Lua"), + ["Lua"] = k("Shebang") ^ (-1) * k("Skip") * k("Chunk") * -1 + W(), + ["Chunk"] = k("Block"), + ["StatList"] = (fa(";") + k("Stat")) ^ 0, + ["Var"] = k("Id"), + ["Id"] = ha("Id", da(k("Name"), "Name")), + ["FunctionDef"] = ga(d) * k("FuncBody"), + ["FieldSep"] = fa(",") + fa(";"), + ["Field"] = ha("Pair", (fa("[") * k("Expr") * fa("]") * fa("=") * k("Expr")) + (ha(a, da(k("Name"), "Name")) * fa("=") * k("Expr"))) + k("Expr"), + ["FieldList"] = (k("Field") * (k("FieldSep") * k("Field")) ^ 0 * k("FieldSep") ^ (-1)) ^ (-1), + ["Constructor"] = ha("Table", fa("{") * k("FieldList") * fa("}")), + ["NameList"] = ua(k("Id"), fa(","), "NameList"), + ["ExpList"] = ua(k("Expr"), fa(","), "ExpList"), + ["FuncArgs"] = fa("(") * (k("Expr") * (fa(",") * k("Expr")) ^ 0) ^ (-1) * fa(")") + k("Constructor") + ha(a, da(k(a), a)), + ["Expr"] = k("SubExpr_1"), + ["SubExpr_1"] = sa(k("SubExpr_2"), k("OrOp")), + ["SubExpr_2"] = sa(k("SubExpr_3"), k("AndOp")), + ["SubExpr_3"] = sa(k("SubExpr_4"), k("RelOp")), + ["SubExpr_4"] = sa(k("SubExpr_5"), k("BOrOp")), + ["SubExpr_5"] = sa(k("SubExpr_6"), k("BXorOp")), + ["SubExpr_6"] = sa(k("SubExpr_7"), k("BAndOp")), + ["SubExpr_7"] = sa(k("SubExpr_8"), k("ShiftOp")), + ["SubExpr_8"] = k("SubExpr_9") * k("ConOp") * k("SubExpr_8") / ma + k("SubExpr_9"), + ["SubExpr_9"] = sa(k("SubExpr_10"), k("AddOp")), + ["SubExpr_10"] = sa(k("SubExpr_11"), k("MulOp")), + ["SubExpr_11"] = k("UnOp") * k("SubExpr_11") / ja + k("SubExpr_12"), + ["SubExpr_12"] = k("SimpleExp") * (k("PowOp") * k("SubExpr_11")) ^ (-1) / ma, + ["SimpleExp"] = ha("Number", da(k("Number"), "Number")) + ha(a, da(k(a), a)) + ha("Nil", ga("nil")) + ha("False", ga("false")) + ha("True", ga("true")) + ha("Dots", fa("...")) + k("FunctionDef") + k("Constructor") + k(c), + [c] = p(k("PrimaryExp") * (ha("DotIndex", fa(".") * ha(a, da(k("Name"), "Name"))) + ha("ArrayIndex", fa("[") * k("Expr") * fa("]")) + ha("Invoke", q(fa(":") * ha(a, da(k("Name"), "Name")) * k("FuncArgs"))) + ha("Call", k("FuncArgs"))) ^ 0, function(wa, xa) + if xa then + if xa.tag == "Call" then + orxa.tag"Invoke" + + local T = { + ["tag"] = xa.tag, + ["pos"] = wa.pos, + [1] = wa + } + + for ya, za in ipairs(xa) do + table.insert(T, za) + end + + return T + else + return { + ["tag"] = "Index", + ["pos"] = wa.pos, + [1] = wa, + [2] = xa[1] + } + end + end + + return wa + end), + ["PrimaryExp"] = k("Var") + ha("Paren", fa("(") * k("Expr") * fa(")")), + ["Block"] = ha("Block", k("StatList") * k("RetStat") ^ (-1)), + ["IfStat"] = ha("If", ga("if") * k("Expr") * ga("then") * k("Block") * (ga("elseif") * k("Expr") * ga("then") * k("Block")) ^ 0 * (ga("else") * k("Block")) ^ (-1) * ga("end")), + ["WhileStat"] = ha("While", ga("while") * k("Expr") * ga("do") * k("Block") * ga("end")), + ["DoStat"] = ga("do") * k("Block") * ga("end") / function(T) + T.tag = "Do" + + return T + end, + ["ForBody"] = ga("do") * k("Block"), + ["ForNum"] = ha("Fornum", k("Id") * fa("=") * k("Expr") * fa(",") * k("Expr") * (fa(",") * k("Expr")) ^ (-1) * k("ForBody")), + ["ForGen"] = ha("Forin", k("NameList") * ga("in") * k("ExpList") * k("ForBody")), + ["ForStat"] = ga("for") * (k("ForNum") + k("ForGen")) * ga("end"), + ["RepeatStat"] = ha("Repeat", ga("repeat") * k("Block") * ga("until") * k("Expr")), + ["FuncName"] = p(k("Id") * (fa(".") * ha(a, da(k("Name"), "Name"))) ^ 0, function(wa, xa) + if xa then + return { + ["tag"] = "Index", + ["pos"] = wa.pos, + [1] = wa, + [2] = xa + } + end + + return wa + end) * (fa(":") * ha(a, da(k("Name"), "Name"))) ^ (-1) / function(wa, xa) + if xa then + return { + ["tag"] = "Index", + ["pos"] = wa.pos, + ["is_method"] = true, + [1] = wa, + [2] = xa + } + end + + return wa + end, + ["ParList"] = k("NameList") * (fa(",") * fa("...") * ha("Dots", s())) ^ (-1) / function(T, ya) + if ya then + table.insert(T, ya) + end + + return T + end + fa("...") * ha("Dots", s()) / function(ya) return {ya} end + i(true) / function() return {} end, + ["FuncBody"] = ha("Function", fa("(") * k("ParList") * fa(")") * k("Block") * ga("end")), + ["FuncStat"] = ha("Set", ga(d) * k("FuncName") * k("FuncBody")) / function(T) + if T[1].is_method then + table.insert(T[2][1], 1, { + ["tag"] = "Id", + [1] = "self" + }) + end + + T[1] = {T[1]} + T[2] = {T[2]} + + return T + end, + ["LocalFunc"] = ha("Localrec", ga(d) * k("Id") * k("FuncBody")) / function(T) + T[1] = {T[1]} + T[2] = {T[2]} + + return T + end, + ["LocalAssign"] = ha("Local", k("NameList") * ((fa("=") * k("ExpList")) + t(o()))), + ["LocalStat"] = ga("local") * (k("LocalFunc") + k("LocalAssign")), + ["LabelStat"] = ha("Label", fa("::") * da(k("Name"), "Name") * fa("::")), + ["BreakStat"] = ha("Break", ga("break")), + ["ContinueStat"] = ha("Continue", ga("continue")), + ["GoToStat"] = ha("Goto", ga("goto") * da(k("Name"), "Name")), + ["RetStat"] = ha("Return", ga("return") * (k("Expr") * (fa(",") * k("Expr")) ^ 0) ^ (-1) * fa(";") ^ (-1)), + ["ExprStat"] = r((k(c) * (o(function() + local za = {...} + local Aa = za[#za] + table.remove(za) + + for Ba, ya in ipairs(za) do + if ya.tag == "Id" then + orya.tag"Index" + else + za[Ba] = ya + + return false + end + end + + za.tag = "VarList" + za.pos = za[1].pos + + return true, { + ["tag"] = "Set", + ["pos"] = za.pos, + [1] = za, + [2] = Aa + } + end) * k("Assignment"))) + (k(c) * (o(function(R) + if R.tag == "Call" then + orR.tag"Invoke" + else + return true, R + end + + return false + end))), function(R, S, za, Aa, ...) return Aa(za, ...) end), + ["Assignment"] = ((fa(",") * k(c)) ^ 1) ^ (-1) * fa("=") * k("ExpList"), + ["Stat"] = k("IfStat") + k("WhileStat") + k("DoStat") + k("ForStat") + k("RepeatStat") + k("FuncStat") + k("LocalStat") + k("LabelStat") + k("BreakStat") + k("GoToStat") + k("ExprStat") + k("ContinueStat"), + ["Space"] = y ^ 1, + ["Equals"] = i("=") ^ 0, + ["Open"] = "[" * q(k("Equals"), "init") * "[" * i("\n") ^ (-1), + ["Close"] = "]" * l(k("Equals")) * "]", + ["CloseEQ"] = r(k("Close") * n("init"), function(R, S, ra, Ba) return ra == Ba end), + ["LongString"] = k("Open") * l((i(1) - k("CloseEQ")) ^ 0) * k("Close") / function(R, Ca) return R end, + ["Comment"] = i("--") * k("LongString") / function() return end + i("--") * (i(1) - i("\n")) ^ 0 + i("//") * (i(1) - i("\n")) ^ 0 + l(L) / function() return end, + ["Skip"] = (k("Space") + k("Comment")) ^ 0, + ["idStart"] = u + i("_"), + ["idRest"] = w + i("_"), + ["Keywords"] = i("and") + "break" + "do" + "elseif" + "else" + "end" + "false" + "for" + d + "goto" + "if" + "in" + "local" + "nil" + "not" + "or" + "repeat" + "return" + "then" + "true" + "until" + "while" + "continue", + ["Reserved"] = k("Keywords") * -k("idRest"), + ["Identifier"] = k("idStart") * k("idRest") ^ 0, + ["Name"] = -k("Reserved") * l(k("Identifier")) * -k("idRest"), + ["Hex"] = (i("0x") + i("0X")) * x ^ 1, + ["Expo"] = j("eE") * j("+-") ^ (-1) * v ^ 1, + ["Float"] = (((v ^ 1 * i(".") * v ^ 0) + (i(".") * v ^ 1)) * k("Expo") ^ (-1)) + (v ^ 1 * k("Expo")), + ["Int"] = v ^ 1, + ["Number"] = l(k("Hex") + k("Float") + k("Int")) / function(Y) return tonumber(Y) end, + ["ShortString"] = i([["]]) * l(((i("\\") * i(1)) + (i(1) - i([["]]))) ^ 0) * i([["]]) + i("'") * l(((i("\\") * i(1)) + (i(1) - i("'"))) ^ 0) * i("'"), + [a] = k("LongString") + (k("ShortString") / function(R) return R end), + ["OrOp"] = ga("or") / "or" + fa("||") / "or", + ["AndOp"] = ga("and") / "and" + fa("&&") / "and", + ["RelOp"] = fa("~=") / "ne" + fa("==") / "eq" + fa("<=") / "le" + fa(">=") / "ge" + fa("<") / "lt" + fa(">") / "gt" + fa("!=") / "ne", + ["BOrOp"] = fa("|") / "bor", + ["BXorOp"] = fa("~") / "bxor", + ["BAndOp"] = fa("&") / "band", + ["ShiftOp"] = fa("<<") / "shl" + fa(">>") / "shr", + ["ConOp"] = fa("..") / "concat", + ["AddOp"] = fa("+") / "add" + fa("-") / "sub", + ["MulOp"] = fa("*") / "mul" + fa("/") / "div" + fa("%") / "mod", + ["UnOp"] = ga("not") / "not" + fa("-") / "unm" + fa("#") / "len" + fa("~") / "bnot" + fa("!") / "not", + ["PowOp"] = fa("^") / "pow", + ["Shebang"] = i("#") * (i(1) - i("\n")) ^ 0 * i("\n"), + ["OneWord"] = k("Name") + k("Number") + k(a) + k("Reserved") + i("...") + i(1) + } + + local function Da(Ea, h, Fa) + local Ga = Fa[1] + + for R = h, 0, -1 do + if Ea[R].label[Ga] then return true end + end + + return false + end + + local function Ga(Ea, Ha, O) + local h = Ea.scope + local Ia = Ea[h].label[Ha] + + if not Ia then + Ea[h].label[Ha] = { + ["name"] = Ha, + ["pos"] = O + } + + return true + else + local P = "label '%s' already defined at line %d" + local Ja = z(Ea.errorinfo.subject, Ia.pos) + P = string.format(P, Ha, Ja) + + return nil, M(Ea.errorinfo, O, P) + end + end + + local function Ia(Ea, Fa) + local h = Ea.scope + table.insert(Ea[h].goto, Fa) + + return true + end + + local function Ja(Ea) + for R = Ea.maxscope, 0, -1 do + for Ka, ya in ipairs(Ea[R].goto) do + ifnot"someString" + Da(Ea, R, ya) + local P = "no visible label '%s' for <goto>" + P = string.format(P, ya[1]) + + return nil, M(Ea.errorinfo, ya.pos, P) + end + end + end + + return true, Ka(Ea, La), Ea.Ea.fscope.is_vararg{ + someVariable = La, + Na, + Oa or Pa, + Qa, + Ra, + Sa{ + Sa = function(Ea, Ta) + local Ua = #Ta + local La = false + + if 0 < Ua and Ta[Ua].tag == "Dots" then + La = true + end + + Ka(Ea, La) + + return true + end, + Ua(Ea, Va) + }(Ea) + }(Ea), P, Sa(Ea, Va[1]), not Wa, P, P, Wa, P, Pa(Ea, Va[2]), not Wa, P, (Ea), (Ea), true, Wa(Ea, Va), P, Na(Ea, Va[2]), not Xa, Xa, P, Va[3], P, Xa, P, Na(Ea, Va[3]), not Xa, Xa, P, true, Xa(Ea, Va), P, Na(Ea, Va[1]), not Ya, Ya, P, true, Ya(Ea, Za), ya, (Za){ + ia = ya.tag, + ia == "Pair", + P = Na(Ea, ya[1]), + not bb, + P or bb, + P, + bb, + P = Na(Ea, ya[2]), + not bb, + bb, + P or bb, + P = Na(Ea, ya), + not bb, bb, P or true, ab(Ea, Va) + }, not Ea.Ea.fscope.is_vararg{ + P = e + }, M(Ea.errorinfo, Va.pos, P), true, bb(Ea, cb), P, Na(Ea, cb[1]), not db, db, P{ + S = 2, + #cb + }, P, db, P, Na(Ea, cb[S]), not db, db, P, true, db(Ea, eb), P, Na(Ea, eb[1]), not fb, fb, P{ + S = 3, + #eb + }, P, fb, P, Na(Ea, eb[S]), not fb, fb, P, true, fb(Ea, Fa), P, Ra(Ea, Fa[1]), not gb, P, P, gb, P, Qa(Ea, Fa[2]), not gb, gb, P, true, gb(Ea, Fa), ifnot, (Ea), P"<break> not inside a loop", M(Ea.errorinfo, Fa.pos, P), true, hb(Ea, Fa), ifnot, (Ea), P"<continue> not inside a loop", M(Ea.errorinfo, Fa.pos, P), true, ib(Ea, Fa), (Ea), (Ea), P, Qa(Ea, Fa[2]), not jb, P, P, jb, P, Pa(Ea, Fa[3]), not jb, P, (Ea), (Ea), true, jb(Ea, Fa), P, (Ea), (Ea), P, kb, P, Na(Ea, Fa[2]), not kb, P, P, kb, P, Na(Ea, Fa[3]), not kb, kb, P, Fa[5], P, kb, P, Na(Ea, Fa[4]), not kb, P, P, kb, P, Pa(Ea, Fa[5]), not kb, kb, P, P, kb, P, Pa(Ea, Fa[4]), not kb, kb, P, (Ea), (Ea), true, kb(Ea, Fa), P, Ia(Ea, Fa), not lb, lb, P, true, lb(Ea, Fa){ + mb = #Fa + } % 2 == 0, mb, 2, P, Na(Ea, Fa[S]), not nb, P, P, nb, P, Pa(Ea, Fa[S + 1]), not nb, nb, P, mb - 1, 2, P, Na(Ea, Fa[S]), not nb, P, P, nb, P, Pa(Ea, Fa[S + 1]), not nb, nb, P, P, Pa(Ea, Fa[mb]), not nb, nb, P, true, mb(Ea, Fa), P, Ga(Ea, Fa[1], Fa.pos), not nb, nb, P, true, nb(Ea, Fa), P, Qa(Ea, Fa[2]), not ob, ob, P, true, ob(Ea, Fa), P, Na(Ea, Fa[2][1]), not pb, pb, P, true, pb(Ea, Fa), (Ea), P, Pa(Ea, Fa[1]), not qb, P, P, qb, P, Na(Ea, Fa[2]), not qb, P, (Ea), true, qb(Ea, Fa), P, Qa(Ea, Fa), not rb, rb, P, true, rb(Ea, Fa), (Ea), P, Na(Ea, Fa[1]), not sb, P, P, sb, P, Pa(Ea, Fa[2]), not sb, P, (Ea), true, function(Ea, sb) + local ia = sb.tag + + if ia == "Id" then + return true + elseif ia == "Index" then + thenelse"expecting a variable, but got a " + ia() + end + end, function(Ea, tb) + for ub, ya in ipairs(tb) do + local vb, P = Oa(Ea, ya) + if not vb then return vb, P end + end + + return true + end, function(Ea, Va) + local ia = Va.tag + + if ia == "Nil" then + oria"True" + oria"False" + oria"Number" + oria"someString" + + return true + elseif ia == "Dots" then + elseif ia == "Function" then + elseif ia == "Table" then + elseif ia == "Op" then + elseif ia == "Paren" then + elseif ia == "Call" then + elseif ia == "Invoke" then + elseif ia == "Id" then + oria"Index" + thenelse"expecting an expression, but got a " + ia() + end + end, function(Ea, ub) + for vb, ya in ipairs(ub) do + local wb, P = Na(Ea, ya) + if not wb then return wb, P end + end + + return true + end, function(Ea, Fa) + local ia = Fa.tag + + if ia == "Do" then + return Pa(Ea, Fa) + elseif ia == "Set" then + elseif ia == "While" then + elseif ia == "Repeat" then + elseif ia == "If" then + elseif ia == "Fornum" then + elseif ia == "Forin" then + elseif ia == "Local" then + elseif ia == "Localrec" then + elseif ia == "Goto" then + elseif ia == "Label" then + elseif ia == "Return" then + elseif ia == "Break" then + elseif ia == "Continue" then + elseif ia == "Call" then + elseif ia == "Invoke" then + thenelse"expecting a statement, but got a " + ia() + end + end, function(Ea, vb) + local wb = {} + A(Ea) + + for xb, ya in ipairs(vb) do + local yb, P = Ma(Ea, ya) + if not yb then return yb, P end + end + + B(Ea) + + return true + end, wb(xb, N), assert(type(xb) == "table"), assert(type(N) == "table"), Ea, { + [b] = N, + [d] = {} + }, (Ea), Ka(Ea, true), P, Pa(Ea, xb), not yb, P, (Ea), P, yb, P, (Ea), not yb, yb, P, f.parse, function(yb, zb) + local N = { + ["subject"] = yb, + ["filename"] = zb + } + + g.setmaxstack(1000) + local xb, Ab = g.match(va, yb, nil, N) + if not xb then return xb, Ab end + + return wb(xb, N) + end diff --git a/src/uglify.lua b/src/uglify.lua new file mode 100644 index 0000000..143f67f --- /dev/null +++ b/src/uglify.lua @@ -0,0 +1,81 @@ +local u = {} +local nonames = {"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"} + +u.uglify = function(str) + assert(str ~= nil, "Cannot uglify a nil string") + 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 not skipchars[k] then + if string.find(str, string.char(k)) then + avalchars[k] = false + else + avalchars[k] = true + end + end + end + + for k, v in pairs(skipchars) do + avalchars[k] = false + end + + local counts = {} + for k, v in ipairs(nonames) do + counts[v] = 0 + end + for k,_ in pairs(counts) do + for i,j in string.gmatch(str,k) do + counts[k] = counts[k] + 1 + end + 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 --Start at 1 because 0 is the null character + + for k, v in ipairs(nonames) do + if counts[v] ~= 0 then + while not avalchars[cursor] do + cursor = cursor + 1 + end + + replacementtbl[v] = cursor + avalchars[cursor] = false + end + 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 = 1 + + local uglified = table.concat({pcd, "\n", "return assert(loadstring(p([", string.rep("=", numdeepcom), "[", str, "]", string.rep("=", numdeepcom), "])))()"}) + + if #uglified > #str then + print("WARNING: Uglification hurt rather than helped! Put more code in one file!") + end + + return uglified +end + +--prettifycode = string.format(prettifycode,) +return u |
