-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.lua
287 lines (244 loc) · 6.22 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
-- Global variables
__OUTPUT__ = {}
__FILE__, __LINE__ = '', 1
local g_IncludeDirs = {''}
local g_IncludeOnce = {}
local g_Defines = {}
-- Preprocessor internal functions
local function preprocessFile(file)
local codeTbl = {}
local lineNr = 1
local definesStr = ''
for k, v in pairs(g_Defines) do
definesStr = definesStr..('%s=%q '):format(k, v)
end
table.insert(codeTbl, definesStr)
for line in file:lines() do
local codeLine
local hashPos = line:match('^%s*()#')
if(hashPos) then
codeLine = line:sub(hashPos + 1):gsub('%-%-[^\n]*', '')
local codeAfterEnd = codeLine:match('^%s*end%s*(.*)')
if(not codeAfterEnd) then
codeLine = '__LINE__='..lineNr..' '..codeLine
elseif(codeAfterEnd:match('%w')) then
print("["..__FILE__..":"..lineNr.."] Error! Unexpected code after \"end\": "..codeAfterEnd)
os.exit(-1)
end
else
--local lineQuoted = ('%q'):format(line)
--lineQuoted = lineQuoted:gsub('$(%b())', '"..%1.."')
local i = 1
local tbl = {}
while(true) do
local pos = line:find('$', i, true)
if(pos) then
if(pos > 1) then
local str = ('%q'):format(line:sub(i, pos - 1))
table.insert(tbl, str)
end
local arg, endPos = line:match('^$(%b())()', pos)
if(not arg) then
arg, endPos = line:match('^$([%w_]+%b())()', pos)
end
if(not arg) then
arg, endPos = line:match('^$([%w_]+)()', pos)
end
if(arg) then
table.insert(tbl, 'tostring('..arg..')')
else
table.insert(tbl, '\'$\'')
endPos = pos + 1
end
i = endPos
else
if(i <= line:len()) then
local str = ('%q'):format(line:sub(i))
table.insert(tbl, str)
end
break
end
end
if(#tbl > 0) then
local str = table.concat(tbl, '..')
codeLine = '__LINE__='..lineNr..' __OUTPUT__[__LINE__]=(__OUTPUT__[__LINE__] or "")..'..str
else
codeLine = '__LINE__='..lineNr
end
end
table.insert(codeTbl, codeLine)
lineNr = lineNr + 1
end
local linesCount = lineNr
local code = table.concat(codeTbl, '\n')
--print(code)
local func, err = loadstring(code, 'luapp')
if(not func) then
code = code:sub(1, 16000)
print('Error! Failed to parse '..__FILE__..':\n'..err..'\nCode:\n'..code)
os.exit(-1)
end
local oldOutput = __OUTPUT__
__OUTPUT__ = {}
local status, err = pcall(func)
if(not status) then
code = code:sub(1, 16000)
print('Error! Failed to preprocess '..__FILE__..':\n'..err..'\nCode:\n'..code)
os.exit(-1)
end
for i = 1, linesCount do
if(not __OUTPUT__[i]) then
__OUTPUT__[i] = ''
end
end
local result = table.concat(__OUTPUT__, '\n')
__OUTPUT__ = oldOutput
return result
end
local function preprocess(path)
local fullPath, file
for i, incDir in ipairs(g_IncludeDirs) do
file = io.open(incDir..path, 'r')
if(file) then
fullPath = incDir..path
break
end
end
if(not file) then
return false
end
local dir = fullPath:gsub('[^/\\]+$', '')
local oldFile, oldLine = __FILE__, __LINE__
table.insert(g_IncludeDirs, dir)
__FILE__ = fullPath
__LINE__ = 1
local buf = preprocessFile(file, true)
table.remove(g_IncludeDirs)
__FILE__ = oldFile
__LINE__ = oldLine
file:close()
return buf
end
local function handleArgs(arg)
local outputPath = 'output.lua'
local sourceList = {}
local i = 1
while(i <= #arg) do
local opt = arg[i]:sub(1, 2)
if(opt == '-o') then
if(arg[i]:len() > 2) then
outputPath = arg[i]:sub(3)
elseif(i + 1 <= #arg) then
outputPath = arg[i+1]
i = i + 1
end
elseif(opt == '-I') then
if(arg[i]:len() > 2) then
table.insert(g_IncludeDirs, arg[i]:sub(3)..'/')
elseif(i + 1 <= #arg) then
table.insert(g_IncludeDirs, arg[i+1]..'/')
i = i + 1
end
elseif(opt == '-D') then
local def
if(arg[i]:len() > 2) then
def = arg[i]:sub(3)
elseif(i + 1 <= #arg) then
def = arg[i+1]
i = i + 1
end
local name, val = def:match('(%w+)=(.*)')
if(name and val) then
g_Defines[name] = val
end
else
table.insert(sourceList, arg[i])
end
i = i + 1
end
return outputPath, sourceList
end
local function main(arg)
-- Handle args table
outputPath, sourceList = handleArgs(arg)
-- Check if arguments are valid
if(not outputPath or #sourceList < 1) then
print("Error! Invalid arguments count.")
os.exit(-1)
end
local result = {}
-- Process all files
for i, path in ipairs(sourceList) do
local dir = path:gsub('[^/\\]+$', '')
table.insert(g_IncludeDirs, dir)
__FILE__ = path
__LINE__ = 1
-- Open LUA file
local inputFile = io.open(path, 'r')
if(inputFile) then
-- Preprocess file
local data = preprocessFile(inputFile)
-- Save result
table.insert(result, data)
-- Close file
inputFile:close()
else
print("Error! Cannot open \""..path.."\".")
os.exit(-1)
end
table.remove(g_IncludeDirs)
end
-- Save preprocessed code in output file
local outputFile = io.open(outputPath, 'w')
if(not outputFile) then
print("Error! Cannot open \""..outputPath.."\".")
os.exit(-1)
end
if(#sourceList == 1) then
outputFile:write(result[1])
else
outputFile:write('do '..table.concat(result, ' end do ')..' end')
end
outputFile:close()
end
-- Utility functions
function include(path)
local buf = preprocess(path)
if(not buf) then
print("["..__FILE__..":"..__LINE__.."] Error! Cannot include \""..path.."\" (dirs "..table.concat(g_IncludeDirs, ', ')..").")
os.exit(-1)
end
buf = buf:gsub('%-%-%[%[.-%]%]', ''):gsub('%-%-[^\n]*', ''):gsub('%s+', ' ')
__OUTPUT__[__LINE__] = buf
return true
end
function includeGuard()
if(g_IncludeOnce[__FILE__]) then
return true
end
g_IncludeOnce[__FILE__] = true
return false
end
function load(path)
__LOAD__ = true
local buf = preprocess(path)
if(not buf) then
print("["..__FILE__..":"..__LINE__.."] Error! Cannot load \""..path.."\".")
os.exit(-1)
end
local func, err = loadstring(buf, path)
if(not func) then
print("["..__FILE__..":"..__LINE__.."] Error! Cannot load \""..path.."\": "..err.."\nCode:\n"..buf)
os.exit(-1)
end
local status, err = pcall(func)
if(not status) then
print("["..__FILE__..":"..__LINE__.."] Error! Cannot load \""..path.."\": "..err.."\nCode:\n"..buf)
os.exit(-1)
end
__LOAD__ = false
end
function isLoaded()
return __LOAD__
end
main(arg)