aboutsummaryrefslogtreecommitdiff
path: root/dist/glum_min.lua
blob: 32610b847d37fd643dca26e7355993905571a241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
local a = dofile("../src/parser.lua")
local b = require("lpeg")
b.locale(b)
local c = {}

local function d(e)
    local f = type(e)
    local g

    if f == "table" then
        g = {}

        for h, i in next, e, nil do
            g[d(h)] = d(i)
        end

        setmetatable(g, d(getmetatable(e)))
    else
        g = e
    end

    return g
end

local f = {"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 g = {}

for h, i in ipairs(f) do
    g[i] = true
end

local h = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"

local function i(j)
    local k = string.find(j, "[" .. h .. "]")
    local l = string.len(j)

    if k == nil then
        return string.rep("a", l + 1)
    else
        local m = string.byte(j, k)
        local n = string.char(m + (m < 122 and 1 or -57))
        local o = string.sub(j, k + 1, l)
        local p = string.rep("a", k - 1)
        local q = p .. n .. o

        while g[q] or _G[q] do
            q = i(q)
        end

        return q
    end
end

local function k(l, m)
    m = m or 0

    for n, o in pairs(l) do
        for p = 0, m do
            io.write("\t")
        end

        io.write(n .. ":")

        if type(o) == "table" then
            io.write("\n")
            k(o, m + 1)
        else
            io.write(tostring(o) .. "\n")
        end
    end
end

local n = {}

local function o(p, l)
    if not n[p.tag] == nil then
        return n[p.tag](p, l)
    else
        print("Valid tags are:")

        for q, r in pairs(n) do
            print(q)
        end

        error("Attempted to use unknown tag type:" .. p.tag)
    end
end

n = {
    ["Call"] = function(p, l)
        local q = o(p[1], l)
        local r = {}
        local s = 2

        while not p[s] == nil do
            r[s - 1] = o(p[s], l)
            s = s + 1
        end

        local t = table.concat(r, ",")
        local u = table.concat({q, "(", t, ")"})

        return u
    end,
    ["Invoke"] = function(p, l)
        local q = o(p[1], l)
        local r = {}

        for s = 3, #p do
            r[#r + 1] = o(p[s], l)
        end

        local t = q
        local u

        if p[2].tag == "String" and #p[2][1] < (#q + 2) then
            u = p[2][1]
            t = table.concat({t, ":", u, "("})
        else
            u = o(p[2], l)
            t = table.concat({t, "[", u, "](", q, ","})
        end

        t = t .. table.concat(r, ",") .. ")"

        return t
    end,
    ["String"] = function(p, l)
        if l.strings[p[1]] == nil then return table.concat({"\"", p[1], "\""}) end
        print("Returning non-catated string")

        return l.strings[p[1]]
    end,
    ["Id"] = function(p, l)
        if l.ids[p[1]] == nil then return p[1] end

        return l.ids[p[1]]
    end,
    ["Index"] = function(p, l)
        local q = o(p[1], l)
        if p[2].tag == "String" then return table.concat({q, ".", p[2][1]}) end

        return table.concat({q, "[", o(p[2], l), "]"})
    end,
    ["Paren"] = function(p, l) return table.concat({"(", o(p[1], l), ")"}) end,
    ["Dots"] = function(p, l) return "..." end,
    ["Forin"] = function(p, l)
        local q = d(l)
        local r = o(p[1], q)
        local s = o(p[2], q)
        local t = o(p[3], q)
        local u = table.concat({" for ", r, " in ", s, " do ", t, " end "})

        return u
    end,
    ["NameList"] = function(p, l)
        local q = {}

        for r = 1, #p do
            if not p[r].tag == "Id" then
                q[#q + 1] = o(p[r])
            else
                if not l.ids[p[r][1]] == nil then
                    q[#q + 1] = l.ids[p[r][1]]
                else
                    local s = i(l.lname)
                    l.lname = s
                    l.ids[p[r][1]] = s
                    q[#q + 1] = s
                end
            end
        end

        local s = table.concat(q, ",")

        return s
    end,
    ["ExpList"] = function(p, l)
        local q = {}

        for r = 1, #p do
            q[r] = o(p[r], l)
        end

        return table.concat(q, ",")
    end,
    ["Nil"] = function(p, l) return " nil " end,
    ["True"] = function(p, l) return " true " end,
    ["False"] = function(p, l) return " false " end,
    ["Return"] = function(p, l)
        local q = {}

        for r, s in ipairs(p) do
            q[r] = o(s, l)
        end

        return " return " .. table.concat(q, ",")
    end,
    ["If"] = function(p, l)
        local q = o(p[1], l)
        local r = o(p[2], l)
        local s = {}
        s[#s + 1] = table.concat({" if ", q, " then ", r})

        for t = 3, #p - 1, 2 do
            local u = o(p[t], l)
            local v = o(p[t + 1], l)
            s[#s + 1] = table.concat({" elseif ", u, " then ", v})
        end

        if #p % 2 == 1 then
            local u = o(p[#p], l)
            s[#s + 1] = " else " .. u
        end

        s[#s + 1] = " end "

        return table.concat(s)
    end,
    ["Fornum"] = function(p, l)
        local q

        if p[1].tag == "Id" then
            if not l.ids[p[1][1]] == nil then
                q = l.ids[p[1][1]]
            else
                local r = i(l.lname)
                l.lname = r
                l.ids[p[1][1]] = r
                q = r
            end
        else
            q = o(p[1], l)
        end

        local r = o(p[2], l)
        local s = o(p[3], l)
        local t = 1
        local u = ""

        if not p[4].tag == "Block" then
            t = o(p[4], l)
            u = o(p[5], l)
        else
            u = o(p[4], l)
        end

        local v = not t == 1 and ("," .. t) or ""
        l[q] = nil

        return table.concat({" for ", q, "=", r, ",", s, v, " do ", u, " end "})
    end,
    ["Op"] = function(p, l)
        local q = {
            ["or"] = " or ",
            ["and"] = " and ",
            ["ne"] = "~=",
            ["eq"] = "==",
            ["le"] = "<=",
            ["ge"] = ">=",
            ["lt"] = "<",
            ["gt"] = ">",
            ["bor"] = "|",
            ["bxor"] = "~",
            ["band"] = "&",
            ["shl"] = "<<",
            ["shr"] = ">>",
            ["concat"] = "..",
            ["add"] = "+",
            ["sub"] = "-",
            ["mul"] = "*",
            ["div"] = "/",
            ["mod"] = "%",
            ["pow"] = "^"
        }

        local r = {
            ["len"] = "#",
            ["not"] = " not ",
            ["unm"] = "-",
            ["bnot"] = "~"
        }

        local s = p[1]

        if not r[s] == nil then
            local t = o(p[2], l)

            return r[s] .. t
        end

        local t = o(p[2], l)
        local u = o(p[3], l)
        local v = table.concat({t, q[s], u})

        return v
    end,
    ["Pair"] = function(p, l)
        local q = o(p[1], l)
        local r = o(p[2], l)

        return table.concat({"[", q, "]=", r})
    end,
    ["Table"] = function(p, l)
        local q = {}

        for r = 1, #p do
            q[#q + 1] = o(p[r], l)
        end

        local s = table.concat(q, ",")

        return table.concat({"{", s, "}"})
    end,
    ["Number"] = function(p, l) return p[1] end,
    ["Local"] = function(p, l)
        local q = l
        local r, s = o(p[1], q), nil

        if not p[2].tag == nil then
            s = o(p[2], q)
        end

        local t = "local " .. r

        if not p[2].tag == nil then
            t = t .. "=" .. s .. ";"
        end

        return t
    end,
    ["VarList"] = function(p, l)
        local q = {}

        for r = 1, #p do
            q[#q + 1] = o(p[r], l)
        end

        return table.concat(q, ",")
    end,
    ["Set"] = function(p, l)
        local q = {}
        local r = not p[1].tag == nil and p[1] or p[1][1]

        for s = 1, #p[1] do
            q[#q + 1] = o(r, l)
        end

        local t = {}
        local u = not p[2].tag == nil and p[2] or p[2][1]

        for s = 1, #p[2] do
            t[#t + 1] = o(u, l)
        end

        local v = table.concat(q, ",")
        v = v .. "=" .. table.concat(t, ",")

        return v .. ";"
    end,
    ["Label"] = function(p, l)
        if l.nids[p[1]] == nil then
            local q = i(l.lname)
            l.lname = q
            l.nids[p[1]] = q
        end

        return "::" .. l.nids[p[1]] .. "::"
    end,
    ["Goto"] = function(p, l)
        if l.nids[p[1]] == nil then
            local q = i(l.lname)
            l.lname = q
            l.nids[p[1]] = q
        end

        return " goto " .. l.nids[p[1]]
    end,
    ["Function"] = function(p, l)
        local q = not p[1].tag == nil and o(p[1], l) or ""
        local r = o(p[2], l)

        return table.concat({" function(", q, ")", r, " end "})
    end,
    ["Localrec"] = function(p, l)
        local q

        if not l.ids[p[1][1]] == nil then
            q = l.ids[p[1][1]]
        else
            local r = i(l.lname)
            l.lname = r
            l.ids[p[1][1][1]] = r
            q = r
        end

        local r = not p[2][1][1].tag == nil and o(p[2][1][1], l) or ""
        local s = o(p[2][1][2], l)

        return table.concat({" local function ", q, "(", r, ")", s, " end "})
    end,
    ["Continue"] = function(p, l) return " continue " end,
    ["While"] = function(p, l)
        local q = o(p[1], l)
        local r = o(p[2], l)
        local s = table.concat({" while ", q, " do ", r, " end "})

        return s
    end,
    ["Break"] = function(p, l) return " break " end,
    ["Block"] = function(p, q)
        local l = d(q)
        q.block = true
        local r = {}

        for s = 1, #p do
            r[#r + 1] = o(p[s], l)
        end

        local t = table.concat(r)
        local u, v = {}, {}

        for s, w in pairs(l.strings) do
            if not q.strings[s] == l.strings[s] then
                u[#u + 1] = w
                v[#v + 1] = string.format("%q", s)
            end
        end

        local w = ""
        local x = " local " .. table.concat(u, ",")
        local y = table.concat(v, ",")

        if 0 < string.len(y) then
            w = table.concat({x, "=", y, ";"})
        end

        return w .. t
    end
}

local function r(s)
    local t = {{"%s*%)%s*", "%)"}, {"%s*%(%s*", "%("}, {"%s*;%s*", ";"}, {"%s*,%s*", ","}, {";+", ";"}, {"^%s*", ""}, {"%s*$", ""}, {"%s+", " "}}

    for u, v in ipairs(t) do
        s = string.gsub(s, v[1], v[2])
    end

    return s
end

c.minify = function(s, t)
    t = t or "anonymous"
    local p, u = a.parse(s, t)

    if not p then
        error(u)
    end

    local v = {
        ["numlocals"] = 0,
        ["strings"] = {},
        ["ids"] = {},
        ["lname"] = "",
        ["nids"] = {}
    }

    return o(p, v)
end

return c