aboutsummaryrefslogtreecommitdiff
path: root/src/glum.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-03-13 15:38:23 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-03-13 15:38:23 -0400
commitf687e914e07094b1eef11d84dab2e069a9298df5 (patch)
treed74600b3afebd028689baa1c306432fac5030f9f /src/glum.lua
parentf96040eb138efdc8f13521bade5bf8bd299bb9fb (diff)
downloadglum-f687e914e07094b1eef11d84dab2e069a9298df5.tar.gz
glum-f687e914e07094b1eef11d84dab2e069a9298df5.tar.bz2
glum-f687e914e07094b1eef11d84dab2e069a9298df5.zip
Started updateing unit tests
Diffstat (limited to 'src/glum.lua')
-rw-r--r--src/glum.lua136
1 files changed, 91 insertions, 45 deletions
diff --git a/src/glum.lua b/src/glum.lua
index f4385c0..bcdd5e6 100644
--- a/src/glum.lua
+++ b/src/glum.lua
@@ -21,18 +21,37 @@ This moudle allows you to minify gLua code
local parser
local msg
+local optimi
if include ~= nil then
parser = include("./parser.lua")
msg = Msg
+ optimi = include("./ast_opts.lua")
else
parser = dofile("../src/parser.lua")
msg = io.write
+ optimi = dofile("../src/ast_opts.lua")
+ print("Just did optimi, it is",type(optimi))
end
local lpeg = require("lpeg")
lpeg.locale(lpeg)
local glum = {}
+--Checks if two tables are the same
+local function deepcompare(tbl1, tbl2)
+ for k,v in pairs(tbl1) do
+ if type(v) == "table" then
+ if not deepcompare(v,tbl2[k]) then
+ return false
+ end
+ else
+ if v ~= tbl2[k] then
+ return false
+ end
+ end
+ end
+end
+
--Creates a deep copy of a table
local function deepcopy(orig)
local orig_type = type(orig)
@@ -125,6 +144,22 @@ local getstringreps = function(ast)
return sstbl
end
+local function astwalker(ast)
+ for i,j in pairs(optimi) do
+ local new = j(ast)
+ changed = changed or new
+ end
+ for k,v in pairs(ast) do
+ if type(v) == "table" then
+ astwalker(v)
+ end
+ end
+ for i,j in pairs(optimi) do
+ local new = j(ast)
+ changed = changed or new
+ end
+end
+
local syntax = {}
local function stringfor(ast,tbl)
@@ -142,10 +177,7 @@ end
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
@@ -213,7 +245,6 @@ syntax = {
["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
ret = ret .. ast[1]
last = true
@@ -225,7 +256,7 @@ syntax = {
end,
["Index"] = function(ast,tbl)
local globalvar = stringfor(ast[1],tbl)
- if ast[2].tag == "String" then
+ if ast[2].tag == "String" and tbl.strings[ast[2][1]] == nil then
last = true
return table.concat({globalvar, ".", ast[2][1]})
end
@@ -243,8 +274,6 @@ syntax = {
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
@@ -275,17 +304,20 @@ syntax = {
outputtbl[#outputtbl + 1] = stringfor(ast[k])
else
if tbl.ids[ast[k][1]] ~= nil then
+ print("Found id in id table")
outputtbl[#outputtbl + 1] = tbl.ids[ast[k][1]]
else
local newvar = getnextvarname(tbl.lname)
tbl.lname = newvar
tbl.ids[ast[k][1]] = newvar
outputtbl[#outputtbl + 1] = newvar
+ print("Not found, output is ", newvar)
end
last = false
end
end
local output = bef .. table.concat(outputtbl, ",")
+ print("Output from namelist is ", output)
last = true
return output
end,
@@ -307,14 +339,12 @@ syntax = {
return ret
end,
["True"] = function(ast,tbl)
- local ret
- if last then ret = " true" else ret = "true" end
+ local ret = "!!1"
last = true
return ret
end,
["False"] = function(ast,tbl)
- local ret
- if last then ret = " false" else ret = "false" end
+ local ret = "!1"
last = true
return ret
end,
@@ -393,8 +423,8 @@ syntax = {
end,
["Fornum"] = function(ast,tbl)
local spargs = {}
- if last then spargs[1] = " for " else spargs[1] = "for " end
- last = false
+ if last then spargs[1] = " for" else spargs[1] = "for" end
+ last = true
local var
assert(ast[1].tag == "Id","Oh no, I was expecting an ID!")
if ast[1].tag == "Id" then
@@ -424,9 +454,12 @@ syntax = {
local code = ""
spargs[7] = ""
if ast[4].tag ~= "Block" then -- incrementer
+ last = false
incrementer = stringfor(ast[4],tbl)
if incrementer ~= 1 then
spargs[7] = "," .. incrementer
+ else
+ last = true
end
if last then spargs[8] = " do" else spargs[8] = "do" end
last = true
@@ -470,14 +503,14 @@ syntax = {
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.
+ local bef
if opname == "not" and ast[2]["tag"] == "Op" and ast[2][1] == "eq" then
ast[2][1] = "ne"
local ret = stringfor(ast[2],tbl)
return ret
end
- local rhs = stringfor(ast[2],tbl)
- local bef
if last then bef = " " else bef = "" end
+ local rhs = stringfor(ast[2],tbl)
return bef .. uniop[opname] .. rhs
end
local sargs = {}
@@ -578,6 +611,12 @@ syntax = {
return (last and " " or "") .. "goto " .. tbl.nids[ast[1]]
end,
["Function"] = function(ast,tbl)
+ --Sometimes the parser fucks up, correct it here
+ if ast[1][1] ~= nil and ast[1][1].tag == nil then
+ ast[1] = ast[1][1]
+ error("Detected parser fuckup")
+ end
+ --end of parser-fuckup-fix code
local funcstr
if last then funcstr = " function(" else funcstr = "function(" end
last = false
@@ -590,7 +629,39 @@ syntax = {
return table.concat({funcstr,funcargs,")",code,endstr})
end,
["Localrec"] = function(ast,tbl)
- local ident
+ local ident = ast[1][1]
+ local args = ast[2][1][1]
+ local func = ast[2][1][2]
+ local bf = {}
+ if last then bf[1] = " local" else bf[1] = "local" end
+ bf[2] = stringfor(ident,tbl) --ident
+ bf[3] = " function"
+ bf[4] = "("
+ last = false
+ bf[5] = stringfor(args,tbl) --args
+ bf[6] = ")"
+ last = false
+ bf[7] = stringfor(func,tbl) -- function
+ if last then bf[8] = " end" else bf[8] = "end" end
+ last = true
+ return table.concat(bf)
+
+ --[==[
+ --Sometimes the parser fucks up, correct it here
+ print("in localrec, ast is")
+ printtable(ast)
+ if ast[1][1] ~= nil and ast[1].tag == nil then
+ ast[1] = ast[1][1]
+ --error("Detected parser fuckup")
+ print("after fixing fuckup, ast was")
+ printtable(ast)
+ else
+ print("ast[1][1] is",ast[1][1])
+ printtable(ast[1][1])
+ end
+ --end of parser-fuckup-fix code
+ local ident = stringfor(ast[1],tbl)
+ --[=[
if tbl.ids[ast[1][1]] ~= nil then
ident = tbl.ids[ast[1][1]]
else
@@ -599,6 +670,7 @@ 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
@@ -612,6 +684,7 @@ syntax = {
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)
local ret
@@ -653,34 +726,6 @@ syntax = {
end,
}
-
-
---Removes extra spaces and duplicated ; from a string
---[[
-local function removespaces(str)
- local removables = {
- {"%s*%)%s*","%)"}, --Spaces before or after )
- {"%s*%(%s*","%("}, --Spaces before or after (
- {"%s*;%s*",";"}, --Spaces before or after ;
- {"%s*,%s*",","}, --Spaces before or after ,
- {";+",";"}, --Multiple ; in a row
- {"^%s*",""}, --Spaces at the beginning of the file
- {"%s*$",""}, --Spaces at the end of the file
- {"%s+"," "}, --Multiple spaces in a row
- }
- --Order is important
- for k,v in ipairs(removables) do
- str = string.gsub(str,v[1],v[2])
- end
- return str
-end
-]]
-
---Compress the string, and adds a little decompression code at the top.
---local function compress(str)
---
---end
-
glum.minify = function(str, name)
name = name or "anonymous"
local ast, error_msg = parser.parse(str, name)
@@ -688,7 +733,8 @@ glum.minify = function(str, name)
error(error_msg)
return nil
end
- print("in glum.minify, ast is ")
+ astwalker(ast)
+ print("After astwalker, ast is")
printtable(ast)
print("Finding string reps")
local strreps = getstringreps(ast)