-
Notifications
You must be signed in to change notification settings - Fork 17
/
preprocess.lua
295 lines (242 loc) · 7.78 KB
/
preprocess.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
local gine = ... or _G.gine
local char_class = "[" .. string.buildclass("%p", "%s", function(char)
if char == "_" then return false end
end) .. "]"
local function insert(chars, i, what)
local left_space = chars[i - 1] and chars[i - 1]:find(char_class)
local right_space = chars[i + 1] and chars[i + 1]:find(char_class)
if left_space and right_space then
chars[i] = what
elseif left_space and not right_space then
chars[i] = what .. " "
elseif not left_space and right_space then
chars[i] = " " .. what
elseif not left_space and not right_space then
chars[i] = " " .. what .. " "
end
end
function gine.PreprocessLua(code, add_newlines)
if not code:find("DEFINE_BASECLASS", nil, true) and loadstring(code) then
return code
end
if not code:find("\n", nil, true) and code:find("\r", nil, true) then
code = code:gsub("\r", "\n")
end
local in_string
local in_comment
local multiline_comment_invalid_char_count = 0
local multiline_comment_start_pos
local multiline_open = false
local in_multiline
local chars = (" " .. code .. " "):to_list()
for i = 1, #chars do
if chars[i] == "\\" then
chars[i] = "\\" .. chars[i + 1]
chars[i + 1] = ""
end
if not in_string and not in_comment and not in_multiline then
if (chars[i] == "/" and chars[i + 1] == "/") or (chars[i] == "-" and chars[i + 1] == "-") then
chars[i] = "-"
chars[i + 1] = "-"
in_comment = "line"
elseif chars[i] == "/" and chars[i + 1] == "*" then
chars[i] = ""
chars[i + 1] = "--[EQUAL["
in_comment = "c_multiline"
multiline_comment_start_pos = i + 1
multiline_comment_invalid_char_count = 0
end
end
if not in_string and not in_comment and not in_multiline then
if chars[i] == "'" or chars[i] == "\"" then in_string = chars[i] end
elseif in_string then
-- \\\"
-- \\"
-- TODO: my head hurts
--if chars[i - 1] ~= "\\" or chars[i - 2] == "\\" or chars[i - 3] ~= "\\" then
if (chars[i] == "'" or chars[i] == "\"") and chars[i] == in_string then
in_string = nil
end
--end
end
if in_comment then
if in_comment == "line" and not in_multiline then
if chars[i] == "\n" then in_comment = nil end
elseif in_comment == "c_multiline" then
if chars[i] == "]" then
multiline_comment_invalid_char_count = multiline_comment_invalid_char_count + 1
end
if chars[i] == "*" and chars[i + 1] == "/" then
in_comment = nil
chars[i] = ""
chars[i + 1] = "]EQUAL]"
local eq = ("="):rep(multiline_comment_invalid_char_count)
chars[i + 1] = chars[i + 1]:gsub("EQUAL", eq)
chars[multiline_comment_start_pos] = chars[multiline_comment_start_pos]:gsub("EQUAL", eq)
end
end
end
if in_multiline then
if multiline_open then
if chars[i] == "=" then
in_multiline = in_multiline + 1
elseif chars[i] == "[" then
multiline_open = false
end
elseif chars[i] == "]" then
local ok = true
for offset = 1, in_multiline do
if chars[i + offset] ~= "=" then ok = false end
end
if ok and (in_multiline ~= 0 or chars[i + 1] == "]") then
in_multiline = nil
in_comment = nil
end
end
end
if not in_string and not in_comment and not in_multiline or in_comment == "line" then
if chars[i] == "[" and (chars[i + 1] == "=" or chars[i + 1] == "[") then
multiline_open = true
in_multiline = 0
if in_comment == "line" and chars[i - 1] == "-" and chars[i - 2] == "-" then
in_comment = nil
-- ---[[ comment comment
if chars[i - 3] == "-" then
multiline_open = false
in_multiline = nil
end
end
end
end
if not in_string and not in_comment and not in_multiline then
if chars[i] == "!" and chars[i + 1] == "=" then
list.remove(chars, i)
chars[i] = "~="
elseif chars[i] == "&" and chars[i + 1] == "&" then
list.remove(chars, i)
insert(chars, i, "and")
elseif chars[i] == "|" and chars[i + 1] == "|" then
list.remove(chars, i)
insert(chars, i, "or")
elseif chars[i] == "!" then
insert(chars, i, "not")
end
end
end
local code = list.concat(chars):sub(4, -4)
if code:whole_word("continue") and not loadstring(code) then
local tokens = {}
local CONTINUE_LABEL = "CONTINUE"
while code:find("::" .. CONTINUE_LABEL .. "::", nil, true) do
CONTINUE_LABEL = CONTINUE_LABEL .. "_" .. tostring(math.random(1, 1000000))
end
local ls = require("lang.lexer")(require("lang.reader").string(code), code)
for i = 1, math.huge do
ls:next()
tokens[i] = {token = ls.token, tokenval = ls.tokenval, linenumber = ls.linenumber}
if ls.token == "TK_eof" then break end
end
local found_continue = false
local lines = code:split("\n")
for i, token in ipairs(tokens) do
if token.token == "TK_name" and token.tokenval == "continue" then
found_continue = true
local start_token
local stop_token
local return_token
local balance = 0
for i = i, 1, -1 do
local val = tokens[i]
if val.token == "TK_end" or val.token == "TK_until" then
balance = balance - 1
end
if balance < 0 then
if
val.token == "TK_do" or
val.token == "TK_if" or
val.token == "TK_for" or
val.token == "TK_function" or
val.token == "TK_repeat"
then
balance = balance + 1
end
else
if balance == 0 and val.token == "TK_do" or val.token == "TK_repeat" then
start_token = val
start_token.stack_pos = i
break
end
end
end
if not start_token then error("unable to find start of loop") end
local balance = 0
local in_function = false
local in_loop = false
for i = start_token.stack_pos, #tokens do
local token = tokens[i]
if
token.token == "TK_do" or
token.token == "TK_if" or
token.token == "TK_function" or
token.token == "TK_repeat"
then
balance = balance + 1
if token.token == "TK_function" then in_function = balance end
if token.token == "TK_do" or token.token == "TK_repeat" then
if i ~= start_token.stack_pos then in_loop = balance end
end
elseif token.token == "TK_end" or token.token == "TK_until" then
if token.token == "TK_end" and in_function == balance then
in_function = false
end
if (token.token == "TK_end" or token.token == "TK_until") and in_loop == balance then
in_loop = false
end
balance = balance - 1
end
if token.token == "TK_return" or token.token == "TK_break" then
if not in_function and not in_loop then return_token = token end
end
if balance == 0 then
stop_token = token
break
end
end
if not stop_token then error("unable to find stop of loop") end
lines[token.linenumber] = lines[token.linenumber]:gsub("continue", "goto " .. CONTINUE_LABEL)
if return_token and not return_token.fixed then
local space = " "
for i = return_token.linenumber - 1, 1, -1 do
if lines[i]:trim() ~= "" then
space = lines[i]
break
end
end
space = space:match("^(%s*)") or " "
lines[return_token.linenumber] = space .. "do " .. lines[return_token.linenumber]:trim() .. " end"
return_token.fixed = true
end
if stop_token and not stop_token.fixed then
local space = " "
for i = stop_token.linenumber - 1, 1, -1 do
if lines[i]:trim() ~= "" then
space = lines[i]
break
end
end
space = space:match("^(%s*)") or " "
lines[stop_token.linenumber] = space .. "::" .. CONTINUE_LABEL .. "::" .. (
add_newlines and
"\n" or
""
) .. lines[stop_token.linenumber]
stop_token.fixed = true
end
end
end
if not found_continue then error("unable to find continue keyword") end
code = list.concat(lines, "\n")
end
code = code:gsub("DEFINE_BASECLASS", "local BaseClass = baseclass.Get")
return code
end