aboutsummaryrefslogtreecommitdiff
path: root/src/glum.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/glum.lua')
-rw-r--r--src/glum.lua113
1 files changed, 80 insertions, 33 deletions
diff --git a/src/glum.lua b/src/glum.lua
index ff05953..a50382a 100644
--- a/src/glum.lua
+++ b/src/glum.lua
@@ -12,27 +12,39 @@ This moudle allows you to minify gLua code
Dependencies:
lua-parser
]]
+
+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)
local glum = {}
---- Creates a deep copy of a table.
--- Creates a deep copy, will even copy metamethods.
--- @tab orig the original table to copy
--- @return a copy of the table
-local function deepcopy(orig) end --Creates a deep copy of a table
-
-local function getnextvarname(latname) end --generates the next valid variable name from the last valid variable name.
-
-local function printtable(tbl) end --A debugging function, a replacement for glua PrintTable
-
-local function stringfor(ast,tbl) end --Returns the string for the given abstract syntax tree, within the scope of tbl
-
-local function removespaces(string) end --Removes extra spaces and semicolons in string
-
-
--Creates a deep copy of a table
local function deepcopy(orig)
local orig_type = type(orig)
@@ -91,10 +103,19 @@ local function printtable(tbl, tabset)
end
end
+local syntax = {}
+
+local function stringfor(ast,tbl)
+ if syntax[ast.tag] ~= nil then
+ return syntax[ast.tag](ast,tbl)
+ else
+ 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.
-local syntax = {
+syntax = {
["Call"] = function(ast,tbl)
local exprname = stringfor(ast[1],tbl)
local argnames = {}
@@ -129,10 +150,33 @@ local syntax = {
end,
["String"] = function(ast,tbl)
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
+ tbl.strings[ast[1] ] = nextvar
return nextvar
+ ]]
end
return tbl.strings[ast[1]]
end,
@@ -267,17 +311,17 @@ local syntax = {
["mod"] = "%", ["pow"] = "^",
}
local uniop = {
- ["len"] = "#", ["not"] = "not",
+ ["len"] = "#", ["not"] = " not ",
["unm"] = "-", ["bnot"] = "~",
}
local opname = ast[1]
if uniop[opname] ~= nil then
local rhs = stringfor(ast[2],tbl)
- return optbl[opname] .. rhs
+ return uniop[opname] .. rhs
end
local lhs = stringfor(ast[2],tbl)
local rhs = stringfor(ast[3],tbl)
- local output = table.concat(lhs,binop[opname],rhs)
+ local output = table.concat({lhs,binop[opname],rhs})
return output
end,
["Pair"] = function(ast,tbl)
@@ -317,12 +361,14 @@ local syntax = {
end,
["Set"] = function(ast,tbl)
local lhs = {}
+ local a1 = ast[1].tag ~= nil and ast[1] or ast[1][1]
for k = 1,#ast[1] do
- lhs[#lhs + 1] = stringfor(ast[1],tbl)
+ lhs[#lhs + 1] = stringfor(a1,tbl)
end
local rhs = {}
+ local a2 = ast[2].tag ~= nil and ast[2] or ast[2][1]
for k = 1,#ast[2] do
- rhs[#rhs + 1] = stringfor(ast[2],tbl)
+ rhs[#rhs + 1] = stringfor(a2,tbl)
end
local ostring = table.concat(lhs,",")
ostring = ostring .. "=" .. table.concat(rhs,",")
@@ -345,7 +391,7 @@ local syntax = {
return " goto " .. tbl.nids[ast[1]]
end,
["Function"] = function(ast,tbl)
- local funcargs = stringfor(ast[1],tbl)
+ local funcargs = ast[1].tag ~= nil and stringfor(ast[1],tbl) or ""
local code = stringfor(ast[2],tbl)
return table.concat({" function(",funcargs,")",code," end "})
end,
@@ -359,7 +405,7 @@ local syntax = {
tbl.ids[ast[1][1][1]] = newvar
ident = newvar
end
- local argstr = stringfor(ast[2][1][1],tbl)
+ local argstr = ast[2][1][1].tag ~= nil and stringfor(ast[2][1][1],tbl) or ""
local expr = stringfor(ast[2][1][2],tbl)
return table.concat({" local function ",ident,"(",argstr,")",expr," end "})
end,
@@ -369,7 +415,7 @@ local syntax = {
["While"] = function(ast,tbl)
local expr = stringfor(ast[1],tbl)
local block = stringfor(ast[2],tbl)
- local output = table.concat(" while " , expr , " do " , block , " end ")
+ local output = table.concat({" while " , expr , " do " , block , " end "})
return output
end,
["Break"] = function(ast,tbl)
@@ -400,13 +446,7 @@ local syntax = {
end,
}
-local function stringfor(ast,tbl)
- if syntax[ast.tag] ~= nil then
- return syntax[ast.tag](ast,tbl)
- else
- error("Attempted to use unknown tag type:" .. ast.tag)
- end
-end
+
--Removes extra spaces and duplicated ; from a string
local function removespaces(str)
@@ -427,7 +467,13 @@ local function removespaces(str)
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)
if not ast then
error(error_msg)
@@ -438,7 +484,8 @@ glum.minify = function(str, name)
["lname"] = "",
["nids"] = {},
}
- return removespaces(stringfor(ast,localvar))
+ --printtable(ast)
+ return --[[removespaces(]]stringfor(ast,localvar)--)
end
return glum