/
Compiler.lua
556 lines (456 loc) · 12 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
--[[
Callisto
#class Callisto.Compiler
#description {
The pure compiler API for Callisto.
}
]]
local Callisto = (...)
local Support = Callisto:GetGraphene().Support
local loadsource
if (Support.lua51) then
function loadsource(source, from, environment)
environment = environment or getfenv()
local chunk, err = loadstring(source, from)
if (not chunk) then
return chunk, err
end
if (environment) then
setfenv(chunk, environment)
end
return chunk
end
elseif (Support.lua52) then
function loadsource(source, from, environment)
return load(source, from, nil, environment)
end
end
local Compiler = {}
local cnal_indices = {
x = 1, y = 2, z = 3, w = 4,
r = 1, g = 2, b = 3, a = 4,
}
local cnal_indices_legacy = {
x = 1, y = 2, z = 3, w = 4,
r = 1, g = 2, b = 3, a = 4,
u = 1, v = 2,
s = 1, t = 2, p = 3, q = 4
}
--[[
matchexpr
The powerhouse of the Callisto parser!
Used when we want to match a whole expression, but no more.
]]
local function matchexpr(source, start, backwards, last_nspace)
local forwards = not backwards
local direction = backwards and -1 or 1
local target = (backwards and 1 or #source)
local elevel = 0 -- Current depth of parens/brackets/braces
for i = start, target, direction do
local char = source:sub(i, i)
if (not char:match("%s")) then
last_nspace = char
end
local elevelc = false
if (char:match("[%[%(%{]")) then
elevelc = true
if (forwards) then
elevel = elevel + 1
else
elevel = elevel - 1
end
elseif (char:match("[%]%)%}]")) then
elevelc = true
if (forwards) then
elevel = elevel - 1
else
elevel = elevel + 1
end
elseif (char:match(";")) then
-- Explicit expression termination!
if (elevel == 0) then
return i - direction
else
return false, "Cannot have semicolon (';') when elevel ~= 0!"
end
elseif (elevel == 0 and char:match(",")) then
return i - direction
elseif (char:match("%s")) then
-- Can we have a space right now?
if (last_nspace) then
-- Crawl to see if the next few characters are legal
local illegal_set = "=;"
if (last_nspace:match("[%w_]")) then
illegal_set = illegal_set .. "%w_\26\27"
if (backwards) then
illegal_set = illegal_set .. "%)%}%]"
end
elseif (forwards) then
if (last_nspace:match("[%)%}%]\27]")) then
illegal_set = illegal_set .. "%w"
end
end
if (elevel == 0) then
if (forwards) then
illegal_set = illegal_set .. "%{"
else
illegal_set = illegal_set .. "%}"
end
end
local illegal
if (illegal_set:len() > 0) then
illegal = "[" .. illegal_set .. "]"
end
local prospective = i
for j = i, target, direction do
local jchar = source:sub(j, j)
if (jchar:match("%s")) then
prospective = j
else
if (illegal and jchar:match(illegal)) then
return i - direction
else
i = prospective
break
end
end
end
end
end
if (elevelc) then
if (elevel < 0) then
return i - direction
end
end
end
return target
end
local function operator_double(source, operator)
local double_operator = operator .. operator
local start, finish = 0, 0
while (true) do
start, finish = source:find(double_operator, finish + 1, true)
if (not start) then
break
end
local target_beginning = matchexpr(source, start - 1, true, operator)
local target = source:sub(target_beginning, start - 1)
source = ("%s%s = (%s) %s 1%s"):format(
source:sub(1, target_beginning - 1),
target, target,
operator,
source:sub(finish + 1)
)
end
return source
end
local function operator_mutating(source, operator)
local start, finish = 0, 0
while (true) do
start, finish = source:find("%s*%" .. operator .. "%=%s*", finish + 1)
if (not start) then
break
end
local target_beginning = matchexpr(source, start - 1, true, operator)
local target = source:sub(target_beginning, start - 1)
local value_ending = matchexpr(source, finish + 1)
local value = source:sub(finish + 1, value_ending)
source = ("%s%s = %s %s (%s)%s"):format(
source:sub(1, target_beginning - 1),
target, target,
operator,
value,
source:sub(value_ending + 1)
)
end
return source
end
local function operator_cnal(source, settings)
local clookups
if (settings.Legacy) then
clookups = cnal_indices_legacy
else
clookups = cnal_indices
end
local start, finish = 0, 0
while (true) do
local prec, keys
start, finish, keys = source:find("%->([%w_]+)", finish + 1)
if (not start) then
break
end
local target_beginning = matchexpr(source, start - 1, true, "-")
local target = source:sub(target_beginning, start - 1)
local mod_ending = matchexpr(source, finish + 1, false, "a")
local mod = source:sub(finish + 1, mod_ending)
local lookups = {}
for i = 1, #keys do
local key = keys:sub(i, i)
local index = clookups[key]
if (not index) then
return false, ("Invalid array lookup %q in %q!"):format(key, keys)
end
table.insert(lookups, ("%s[%d]%s"):format(target, index, mod))
end
source = ("%s%s%s"):format(
source:sub(1, target_beginning - 1),
table.concat(lookups, ", "),
source:sub(mod_ending + 1)
)
end
return source
end
local function operator_lambda(source)
local start, finish = 0, 0
while (true) do
local prec, keys
start, finish, name, args = source:find("([%w%.:]*)%s*(%b())%s*=>%s*", finish + 1)
if (not start) then
break
end
local body_ending = matchexpr(source, finish + 1, false, " ")
local body = source:sub(finish + 1, body_ending)
source = ("%sfunction %s%s return %s end%s"):format(
source:sub(1, start - 1),
name, args,
body,
source:sub(body_ending + 1)
)
end
return source
end
local function operator_defaultargs(source)
local start, finish = 0, 0
while (true) do
local prec, keys
start, finish, name, args = source:find("function ([%w%.:]*)%s*(%b())", finish + 1)
if (not start) then
break
end
local args_buffer = {}
local defs_buffer = {}
local state = 0
local aname_building = ""
local adef_building = ""
local depth = 0
for i = 2, args:len() do
local char = args:sub(i, i)
if (state == 0) then -- match name
if (char:match("[%w%._]")) then
aname_building = aname_building .. char
elseif (char == ",") then
table.insert(args_buffer, aname_building)
aname_building = ""
elseif (char == "=") then
state = 1
end
elseif (state == 1) then -- match default definition
if (char:match("[,%)]") and depth == 0) then
adef_building = adef_building:match("^%s*(.-)%s*$")
table.insert(args_buffer, aname_building)
table.insert(defs_buffer, {aname_building, adef_building})
aname_building = ""
adef_building = ""
state = 0
else
adef_building = adef_building .. char
end
if (char:match("[%[%(%{]")) then
depth = depth + 1
elseif (char:match("[%]%)%}]")) then
depth = depth - 1
end
end
end
if (state == 0 and aname_building ~= "") then
table.insert(args_buffer, aname_building)
end
local defo_buffer = {}
for key, def in ipairs(defs_buffer) do
local name, value = def[1], def[2]
table.insert(defo_buffer, ("%s = (%s ~= nil) and %s or %s"):format(
name, name, name, value
))
end
local body_ending = matchexpr(source, finish + 1, false, " ")
local body = source:sub(finish + 1, body_ending)
source = ("%sfunction %s(%s)%s%s"):format(
source:sub(1, start - 1),
name,
table.concat(args_buffer, ","),
table.concat(defo_buffer, "\n"),
source:sub(finish + 1)
)
end
return source
end
local function operator_bang(source, settings)
return (source:gsub("([%w_!]*![%w_!]*)", function(banged)
if (settings.Legacy) then
return (banged:gsub("!", "InPlace"))
else
return (banged:gsub("!", "BANG"))
end
end))
end
local function operator_que(source, settings)
return (source:gsub("([%w_%?]*%?[%w_%?]*)", function(banged)
return (banged:gsub("%?", "QUE"))
end))
end
local function strip_strings(source, str_tab)
str_tab = str_tab or {}
local function predicate(whole)
table.insert(str_tab, whole)
return "\26" .. #str_tab .. "\27"
end
source = source
:gsub("(%-%-%[(=*)%[.-%]%2%])", "")
:gsub("%-%-[^\r\n]+\r?\n?", "")
:gsub("%b\"\"", predicate)
:gsub("%b''", predicate)
:gsub("(%[(=*)%[.-%]%2%])", predicate)
return source, str_tab
end
local function replace_strings(source, str_tab)
return (source:gsub("\26(%d-)\27", function(number)
return str_tab[tonumber(number)]
end))
end
Compiler.Features = {
operator_lambda,
operator_defaultargs,
operator_bang,
operator_que,
operator_cnal,
function(source, settings)
if (settings.Legacy) then
return operator_double(source, "+")
else
return source
end
end,
function(source)
return operator_mutating(source, "+")
end,
function(source)
return operator_mutating(source, "-")
end,
function(source)
return operator_mutating(source, "*")
end,
function(source)
return operator_mutating(source, "/")
end,
function(source)
return operator_mutating(source, "^")
end,
function(source)
return operator_mutating(source, "..")
end
}
Compiler.Extensions = {
Annotations = function(source)
WarnOnce("Annotations have been enabled but are not implemented!")
return source
end
}
--[[#method {
class public @string Compiler.Transform(@string source, [@table settings])
required source: The source to transform.
optional settings: Settings (source directives) to pass to the transpiler.
Transforms the given source from Callisto to Lua.
}]]
function Compiler.Transform(source, settings)
settings = settings or {}
local legacy
if (Compiler.Legacy or settings.Legacy) then
legacy = true
else
legacy = not not (source:match("#CallistoLegacy") or source:match("#CARBIDE_LEGACY"))
end
settings.Legacy = legacy
local extensions
if (settings.CallistoExtensions) then
extensions = settings.CallistoExtensions
else
extensions = {}
if (legacy) then
for nameset in source:gmatch("#CARBIDE_EXTENSIONS ([^\r\n]+)") do
for name in nameset:gmatch("[^%s,]+") do
table.insert(extensions, name)
end
end
end
for nameset in source:gmatch("#CallistoExtensions ([^\r\n]+)") do
for name in nameset:gmatch("[^%s,]+") do
table.insert(extensions, name)
end
end
end
local report_out
if (legacy) then
if (settings.COMPILE_TO) then
report_out = settings.COMPILE_TO
else
local out = source:match("#COMPILE_TO ([^\r\n]+)")
if (out) then
report_out = out
end
end
end
if (settings.CompileTo) then
report_out = settings.CompileTo
else
local out = source:match("#CompileTo ([^\r\n]+)")
if (out) then
report_out = out
end
end
local str_tab
source, str_tab = strip_strings(source)
local err
for key, operator in ipairs(Compiler.Features) do
source, err = operator(source, settings)
if (not source) then
return nil, err
end
end
for key, name in ipairs(extensions) do
local extension = Compiler.Extensions[name]
if (extension) then
source, err = extension(source)
if (not source) then
return nil, err
end
else
WarnOnce(("Unknown extension '%s'!"):format(name))
end
end
source = replace_strings(source, str_tab)
if (report_out) then
if (report_out == "stdout") then
print(source)
else
local handle, err = io.open(report_out, "wb")
if (not handle) then
error(("Couldn't write compiled output to %q: %s"):format(tostring(report_out), err))
end
handle:write(source)
handle:close()
end
end
return source, {}
end
--[[#method {
class public @function Compiler.LoadString(@string source, [@table sourcemap, @string chunkname, @table environment])
required source: The source to load into a chunk.
optional sourcemap: A Callisto sourcemap mapping lines.
optional chunkname: A name to give the resulting chunk.
optional environment: The Lua environment to load the chunk into.
Loads a compiled chunk in a way compatible with any Lua 5.1+
}]]
function Compiler.LoadString(source, sourcemap, chunkname, environment)
return loadsource(source, chunkname, environment)
end
return Compiler