-
Notifications
You must be signed in to change notification settings - Fork 9
/
compiler.lua
313 lines (273 loc) · 6.25 KB
/
compiler.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
--
-- Compiler
-- By Oeed
--
local mainFile = "main.lua"
local file = [[
--
-- Firewolf
-- By GravityScore and 1lann
--
-- Credits:
-- * Files compiled together using Compilr by Oeed
-- * RC4 Implementation by AgentE382
-- * Base64 Implementation by KillaVanilla
--
]]
local files = {}
local function addFolder(path, tree)
for i, v in ipairs(fs.list(path)) do
local subPath = path .. "/" .. v
local isProtectedFile = subPath == "/rom" or subPath == "/build"
local isRunningFile = subPath == "/" .. shell.getRunningProgram()
if v == ".DS_Store" or v == ".git" or isProtectedFile or isRunningFile then
-- Ignore
elseif fs.isDir(subPath) then
tree[v] = {}
addFolder(subPath, tree[v])
else
local h = fs.open(subPath, "r")
if h then
tree[v] = h.readAll()
h.close()
end
end
end
end
addFolder("", files)
if not files[mainFile] then
error("You must have a file called " .. mainFile .. " to be executed at runtime.")
end
file = file .. "local files = " .. textutils.serialize(files) .. "\n"
file = file .. [[
local function run(args)
local fnFile, err = loadstring(files[ ]] .. mainFile .. [[ ], "]] .. mainFile .. [[")
if err then
error(err)
end
local function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t, cap)
end
last_end = e + 1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
local function resolveTreeForPath(path, single)
local _files = files
local parts = split(path, "/")
if parts then
for i, v in ipairs(parts) do
if #v > 0 then
if _files[v] then
_files = _files[v]
else
_files = nil
break
end
end
end
elseif #path > 0 and path ~= "/" then
_files = _files[path]
end
if not single or type(_files) == "string" then
return _files
end
end
local oldFs = fs
local env
env = {
fs = {
list = function(path)
local list = {}
if fs.exists(path) then
list = fs.list(path)
end
for k, v in pairs(resolveTreeForPath(path)) do
if not fs.exists(path .. "/" ..k) then
table.insert(list, k)
end
end
return list
end,
exists = function(path)
if fs.exists(path) then
return true
elseif resolveTreeForPath(path) then
return true
else
return false
end
end,
isDir = function(path)
if fs.isDir(path) then
return true
else
local tree = resolveTreeForPath(path)
if tree and type(tree) == "table" then
return true
else
return false
end
end
end,
isReadOnly = function(path)
if not fs.isReadOnly(path) then
return false
else
return true
end
end,
getName = fs.getName,
getSize = fs.getSize,
getFreespace = fs.getFreespace,
makeDir = fs.makeDir,
move = fs.move,
copy = fs.copy,
delete = fs.delete,
combine = fs.combine,
open = function(path, mode)
if fs.exists(path) then
return fs.open(path, mode)
elseif type(resolveTreeForPath(path)) == "string" then
local handle = {close = function()end}
if mode == "r" then
local content = resolveTreeForPath(path)
handle.readAll = function()
return content
end
local line = 1
local lines = split(content, "\n")
handle.readLine = function()
if line > #lines then
return nil
else
return lines[line]
end
line = line + 1
end
return handle
else
error("Cannot write to read-only file (compilr archived).")
end
else
return fs.open(path, mode)
end
end
},
io = {
input = io.input,
output = io.output,
type = io.type,
close = io.close,
write = io.write,
flush = io.flush,
lines = io.lines,
read = io.read,
open = function(path, mode)
if fs.exists(path) then
return io.open(path, mode)
elseif type(resolveTreeForPath(path)) == "string" then
local content = resolveTreeForPath(path)
local f = fs.open(path, "w")
f.write(content)
f.close()
if mode == "r" then
return io.open(path, mode)
else
error("Cannot write to read-only file (compilr archived).")
end
else
return io.open(path, mode)
end
end
},
loadfile = function(_sFile)
local file = env.fs.open(_sFile, "r")
if file then
local func, err = loadstring(file.readAll(), fs.getName(_sFile))
file.close()
return func, err
end
return nil, "File not found: ".._sFile
end,
dofile = function(_sFile)
local fnFile, e = env.loadfile(_sFile)
if fnFile then
setfenv(fnFile, getfenv(2))
return fnFile()
else
error(e, 2)
end
end
}
setmetatable(env, { __index = _G })
local tAPIsLoading = {}
env.os.loadAPI = function(_sPath)
local sName = fs.getName(_sPath)
if tAPIsLoading[sName] == true then
printError("API "..sName.." is already being loaded")
return false
end
tAPIsLoading[sName] = true
local tEnv = {}
setmetatable(tEnv, { __index = env })
local fnAPI, err = env.loadfile(_sPath)
if fnAPI then
setfenv(fnAPI, tEnv)
fnAPI()
else
printError(err)
tAPIsLoading[sName] = nil
return false
end
local tAPI = {}
for k,v in pairs(tEnv) do
tAPI[k] = v
end
env[sName] = tAPI
tAPIsLoading[sName] = nil
return true
end
env.shell = shell
setfenv(fnFile, env)
fnFile(unpack(args))
end
local function extract()
local function node(path, tree)
if type(tree) == "table" then
fs.makeDir(path)
for k, v in pairs(tree) do
node(path .. "/" .. k, v)
end
else
local f = fs.open(path, "w")
if f then
f.write(tree)
f.close()
end
end
end
node("", files)
end
local args = {...}
if #args == 1 and args[1] == "--extract" then
extract()
else
run(args)
end
]]
fs.delete("/build")
local f = fs.open("/build", "w")
f.write(file)
f.close()