-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea.lua
345 lines (269 loc) · 8.79 KB
/
tea.lua
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
local table_insert = table.insert
local next = next
local ipairs = ipairs
local pairs = pairs
local setmetatable = setmetatable
local preprocess = require "thirdparty.preprocess"
local tea = {
defines = {},
pragmas = {},
envs = {}
}
local var_ops = {
{
"%+=",
"+"
},
{
"%-=",
"-"
},
{
"%.=",
".."
},
{
"/=",
"/"
},
{
"%*=",
"*"
}
}
local inc_ops = {
{
"%-%-",
"-"
},
{
"%+%+",
"+"
}
}
local function find_function(lines, start)
for i = 1, 10 do -- Limit for the greater opimization. May be shitty :(
if lines[start + i]:match("function [0-9a-zA-Z, _:]+%([0-9a-zA-Z, _\"=']*%)") then
return start + i
end
end
return false
end
local line_ops = {
{
match = function(k, line, lines)
local vars = {}
for var in line:gmatch("%${(.+)}") do
table_insert(vars, var)
end
if next(vars) ~= nil then return true, vars end
return false
end,
replace = function(k, line, lines, vars)
for _, var in ipairs(vars) do
lines[k] = line:gsub("%${"..var.."}", '"..'..var..'.."')
end
end
},
{
match = function(k, line, lines)
local name, args = line:match("function ([0-9a-zA-Z, _:.]+)%(([0-9a-zA-Z, _\"=']*)%)")
local args_tbl = {}
if name then
tea.defines["__function__"] = name
end
if args then
for arg, default in args:gmatch("([0-9a-zA-Z_]+)=([0-9a-zA-Z _\"']+)") do
table_insert(args_tbl, {arg, default})
end
if next(args_tbl) ~= nil then
return true, name, args_tbl, args
end
end
return false
end,
replace = function(k, line, lines, name, args_tbl, args)
local args_str = ""
for n, arg in ipairs(args_tbl) do
-- Bottleneck?
args_str = args_str .. arg[1] .. (n == #args_tbl and "" or ",")
table_insert(lines, k+1, arg[1] .. " = " .. arg[1] .. " or " .. arg[2])
end
lines[k] = line:gsub("function " .. name .. "%(" .. args .. "%)", "function " .. name .. "(" .. args_str .. ")")
end
},
{
match = function(k, line, lines)
local key, value = line:match("^#pragma (.+)[ ]*(.*)$")
if key and value then
return true, key, value
end
end,
replace = function(k, line, lines, key, values)
lines[k] = "[ignore]"
tea.pragmas[key] = value or true
end
},
{
match = function(k, line, lines)
local def, replace = line:match("^#define (.+) (.+)$")
if def and replace then
return true, def, replace
end
end,
replace = function(k, line, lines, def, replace)
lines[k] = "[ignore]"
tea.defines[def] = replace
end
},
{
match = function(k, line, lines)
local define, alias = line:match("^#alias (.+) (.+)$")
if alias and define then
return true, define, alias
end
end,
replace = function(k, line, lines, define, alias)
lines[k] = "[ignore]"
local def = tea.defines[define]
if def then
tea.defines[alias] = def
end
end
},
{
match = function(k, line, lines)
local matcher, ret = line:match("^%[([a-zA-Z _:()\"='.]+)%]%[([a-zA-Z _:()\"=']*)%]$")
if matcher and ret then
local func = find_function(lines, k)
if func then return true, matcher, ret, func end
end
end,
replace = function(k, line, lines, matcher, ret, func_line)
lines[k] = "[ignore]"
table_insert(lines, func_line + 1, "if " .. matcher .. " then return " .. ret .. " end")
end
},
{
match = function(k, line, lines)
local deco = line:match("^[!-]%[([a-zA-Z _:()\"='.]+)%]$")
if deco then
local func = find_function(lines, k)
if func then return true, deco, func end
end
return false
end,
replace = function(k, line, lines, deco, func_line)
lines[k] = "[ignore]"
table_insert(lines, func_line + 1, "if not " .. deco .. " then return end")
end
},
{
match = function(k, line, lines)
local deco = line:match("^+%[([a-zA-Z _:()\"'.]+)%]$")
if deco then
local func = find_function(lines, k)
if func then return true, deco, func end
end
return false
end,
replace = function(k, line, lines, deco, func_line)
lines[k] = "[ignore]"
table_insert(lines, func_line + 1, "if " .. deco .. " then return end")
end
},
{
match = function(k, line, lines)
local casts = {}
for _type, junk, var in line:gmatch("[a-zA-Z0-9.\"'_(){} ]%(([a-zA-Z]+)%)([ ]*)([0-9a-zA-Z_.'\"]+)") do
table_insert(casts, {_type, junk, var})
end
if next(casts) ~= nil then
return true, casts
end
return false
end,
replace = function(k, line, lines, casts)
for k, v in ipairs(casts) do
local _type, junk, var = v[1], v[2], v[3]
lines[k] = line:gsub("%(".._type.."%)"..junk..var, "to".._type.."("..var..")")
end
end
},
{
match = function(k, line, lines)
local var, tbl = line:match("for ([a-zA-Z_0-9]+) in ([a-zA-Z_0-9._:]+) do")
if var and tbl then
return true, var, tbl
end
end,
replace = function(k, line, lines, var, tbl)
lines[k] = line:gsub("for " .. var .. " in " .. tbl .. " do", "for k, " .. var .. " in ipairs(" .. tbl .. ") do")
end
}
}
local function parse_lines(text)
local lines = {}
for v in text:gmatch("([^\n]*)\n?") do table_insert(lines, v) end
return lines
end
local function concat_lines(lines)
local result = ""
for k, v in ipairs(lines) do
if v ~= "[ignore]" then
result = result .. v .. "\n"
end
end
return result
end
local function parse_ops(lines)
for k, line in ipairs(lines) do
for _, v in ipairs(var_ops) do
local var, exp = line:match("([a-zA-Z_0-9]+) " .. v[1] .. " (.+)")
if var and exp then
lines[k] = line:gsub(var .. " " .. v[1] .. " " .. exp, var .. " = " .. var .. " " .. v[2] .. " " .. exp)
end
end
end
end
local function parse_increments(lines)
for k, line in ipairs(lines) do
for _, v in ipairs(inc_ops) do
local var = line:match("([a-zA-Z_0-9]+)" .. v[1])
if var then
lines[k] = line:gsub(var .. v[1], var .. " = " .. var .. " " .. v[2] .. " 1")
end
end
end
end
function tea.parse(text, file)
tea.defines["__file__"] = file or "unknown"
tea.defines["__date__"] = os.date("%b %d %Y", os.time())
tea.defines["__time__"] = os.date("%X", os.time())
local lines = parse_lines(text)
parse_ops(lines)
parse_increments(lines)
for lk, line in ipairs(lines) do
tea.defines["__line__"] = lk
for define, value in pairs(tea.defines) do
lines[lk] = lines[lk]:gsub(define, value)
end
for k, v in ipairs(line_ops) do
local result, arg1, arg2, arg3 = v.match(lk, line, lines)
if result then
v.replace(lk, line, lines, arg1, arg2, arg3)
end
end
end
text = concat_lines(lines)
if tea.pragmas["minimize"] then
local Parser = require'thirdparty.ParseLua'
local Format_Mini = require'thirdparty.FormatMini'
local ParseLua = Parser.ParseLua
local st, ast = ParseLua(text)
text = Format_Mini(ast)
end
setmetatable(tea.envs, {__index = _G})
return preprocess({input = text, lookup = tea.envs})
end
return tea