-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompiler.lua
2311 lines (2213 loc) · 60.8 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local MAJOR_VERSION = "LibDogTag-3.0"
local MINOR_VERSION = tonumber(("@project-date-integer@"):match("%d+")) or 33333333333333
if MINOR_VERSION > _G.DogTag_MINOR_VERSION then
_G.DogTag_MINOR_VERSION = MINOR_VERSION
end
local type, error, rawget, next, pairs, ipairs, setmetatable, loadstring, pcall, table, tonumber, tostring, math, assert, unpack =
type, error, rawget, next, pairs, ipairs, setmetatable, loadstring, pcall, table, tonumber, tostring, math, assert, unpack
-- #AUTODOC_NAMESPACE DogTag
DogTag_funcs[#DogTag_funcs+1] = function(DogTag)
local L = DogTag.L
local Tags = DogTag.Tags
local newList, newDict, newSet, del, deepCopy, deepDel = DogTag.newList, DogTag.newDict, DogTag.newSet, DogTag.del, DogTag.deepCopy, DogTag.deepDel
local fixNamespaceList = DogTag.fixNamespaceList
local select2 = DogTag.select2
local joinSet = DogTag.joinSet
local unpackNamespaceList = DogTag.unpackNamespaceList
local getASTType = DogTag.getASTType
local kwargsToKwargTypes = DogTag.kwargsToKwargTypes
local kwargsToKwargTypesWithTableCache = DogTag.kwargsToKwargTypesWithTableCache
local memoizeTable = DogTag.memoizeTable
local unparse, parse, standardize, codeToEventList, clearCodes
DogTag_funcs[#DogTag_funcs+1] = function()
unparse = DogTag.unparse
parse = DogTag.parse
standardize = DogTag.standardize
codeToEventList = DogTag.codeToEventList
clearCodes = DogTag.clearCodes
end
local compilationSteps
do
local mt = {__index = function(self, ns)
self[ns] = newList()
return self[ns]
end}
if DogTag.oldLib and DogTag.oldLib.compilationSteps then
compilationSteps = DogTag.oldLib.compilationSteps
setmetatable(compilationSteps.pre, mt)
compilationSteps.pre.Base = {}
setmetatable(compilationSteps.start, mt)
compilationSteps.start.Base = {}
setmetatable(compilationSteps.tag, mt)
compilationSteps.tag.Base = {}
setmetatable(compilationSteps.tagevents, mt)
compilationSteps.tagevents.Base = {}
setmetatable(compilationSteps.finish, mt)
compilationSteps.finish.Base = {}
else
compilationSteps = {}
compilationSteps.pre = setmetatable({ ['Base'] = {} }, mt)
compilationSteps.start = setmetatable({ ['Base'] = {} }, mt)
compilationSteps.tag = setmetatable({ ['Base'] = {} }, mt)
compilationSteps.tagevents = setmetatable({ ['Base'] = {} }, mt)
compilationSteps.finish = setmetatable({ ['Base'] = {} }, mt)
end
end
DogTag.compilationSteps = compilationSteps
local correctTagCasing = setmetatable({}, {__index = function(self, tag)
for ns, data in pairs(Tags) do
if data[tag] then
self[tag] = tag
return tag
end
end
local tag_lower = tag:lower()
for ns, data in pairs(Tags) do
for t in pairs(data) do
if tag_lower == t:lower() then
self[tag] = t
return t
end
end
end
self[tag] = tag
return tag
end})
local function correctASTCasing(ast)
if type(ast) ~= "table" then
return
end
local astType = ast[1]
if astType == "tag" or astType == "mod" then
ast[2] = correctTagCasing[ast[2]]
if ast.kwarg then
for k,v in pairs(ast.kwarg) do
correctASTCasing(v)
end
end
end
for i = 1, #ast do
correctASTCasing(ast[i])
end
end
DogTag.correctASTCasing = correctASTCasing
local codeToFunction
do
local codeToFunction_mt_mt = {__index = function(self, code)
if not code then
return self[""]
end
local nsList = self[1]
local kwargTypes = self[2]
local s, functions = DogTag:CreateFunctionFromCode(code, nsList, kwargTypes, true)
local func, err = loadstring(s)
local val
if not func then
local _, minor = LibStub(MAJOR_VERSION)
geterrorhandler()(("%s.%d: Error (%s) loading code %q. Please inform ckknight."):format(MAJOR_VERSION, minor, err, code))
val = self[""]
else
DogTag.__functions = functions
local status, result = pcall(func)
DogTag.__functions = nil
if not status then
local _, minor = LibStub(MAJOR_VERSION)
geterrorhandler()(("%s.%d: Error (%s) loading code %q. Please inform ckknight."):format(MAJOR_VERSION, minor, result, code))
val = self[""]
else
val = result
end
end
if functions then
functions = del(functions)
end
self[code] = val
return val
end}
local codeToFunction_mt = {__index = function(self, kwargTypes)
local t = setmetatable(newList(self[1], kwargTypes), codeToFunction_mt_mt)
self[kwargTypes] = t
return t
end}
codeToFunction = setmetatable({}, {__index = function(self, nsList)
local t = setmetatable(newList(nsList), codeToFunction_mt)
self[nsList] = t
return t
end})
end
DogTag.codeToFunction = codeToFunction
local operators = {
["+"] = 'plus',
["-"] = 'minus',
["*"] = 'times',
["/"] = 'divide',
["%"] = 'modulus',
["^"] = 'raise',
["<"] = 'less',
[">"] = 'greater',
["<="] = 'lessequal',
[">="] = 'greaterequal',
["="] = 'equal',
["~="] = 'inequal',
["unm"] = 'unm',
}
local figureCachedTags
do
function figureCachedTags(ast)
local cachedTags = newList()
if type(ast) ~= "table" then
return cachedTags
end
local astType = ast[1]
if astType == 'tag' or operators[astType] then
local tagName = astType == 'tag' and ast[2] or astType
if not cachedTags[tagName] then
cachedTags[tagName] = 0
end
if not ast.kwarg then
cachedTags[tagName] = cachedTags[tagName] + 1
else
for key, value in pairs(ast.kwarg) do
local data = figureCachedTags(value)
for k,v in pairs(data) do
cachedTags[k] = (cachedTags[k] or 0) + v
end
data = del(data)
end
end
end
for i = 2, #ast do
local data = figureCachedTags(ast[i])
for k, v in pairs(data) do
cachedTags[k] = (cachedTags[k] or 0) + v
end
data = del(data)
end
return cachedTags
end
end
local function enumLines(text)
text = text:gsub("\r\n", "\n"):gsub("\t", " ")
local lines = newList(("\n"):split(text))
local t = newList()
local indent = 0
for i, v in ipairs(lines) do
if v:match("end;?$") or v:match("else$") or v:match("^ *elseif") then
indent = indent - 1
end
for j = 1, indent do
t[#t+1] = " "
end
t[#t+1] = v:gsub(";\s*$", "")
t[#t+1] = " -- "
t[#t+1] = i
t[#t+1] = "\n"
if v:match("then$") or v:match("do$") or v:match("else$") or v:match("function%(.-%)") then
indent = indent + 1
end
end
lines = del(lines)
local s = table.concat(t)
t = del(t)
return s
end
local newUniqueVar, clearUniqueVars, getNumUniqueVars
do
local num = 0
local pool = {}
function newUniqueVar()
num = num + 1
return 'arg' .. num
end
function clearUniqueVars()
num = 0
end
function getNumUniqueVars()
return num
end
end
local function getTagData(tag, nsList)
for _, ns in ipairs(unpackNamespaceList[nsList]) do
local Tags_ns = Tags[ns]
if Tags_ns then
local Tags_ns_tag = Tags_ns[tag]
if Tags_ns_tag then
return Tags_ns_tag, ns
end
end
end
end
DogTag.getTagData = getTagData
local function getKwargsForAST(ast, nsList, extraKwargs)
local tag
if ast[1] == "tag" then
tag = ast[2]
else
tag = ast[1]
end
local tagData = getTagData(tag, nsList)
local arg = tagData.arg
if not arg then
return newList() -- no issue, but no point
end
local kwargs = newList()
if extraKwargs then
-- extra kwargs specified on fontstring registration, e.g. { unit = "player" }
for k,v in pairs(extraKwargs) do
kwargs[k] = extraKwargs
end
end
if ast.kwarg then
for k,v in pairs(ast.kwarg) do
kwargs[k] = v
end
end
return kwargs
end
local function mytonumber(value)
local type_value = type(value)
if type_value == "number" then
return value
elseif type_value ~= "string" then
return nil
end
if value:match("^0x") then
return nil
elseif value:match("%.$") or value:match("%.%d*0$") then
return nil
elseif value:match("^0%d+") then
return nil
elseif value:match("^%+") then
return nil
end
return tonumber(value)
end
DogTag.__mytonumber = mytonumber
local allOperators = {
["concat"] = true,
["and"] = true,
["or"] = true,
["if"] = true,
["not"] = true,
}
for k in pairs(operators) do
allOperators[k] = true
end
local function numberToString(num)
if type(num) ~= "number" then
return tostring(num)
elseif num == math.huge then
return "NaN"
elseif num == -math.huge then
return "-NaN"
elseif math.floor(num) == num then
return tostring(num)
else
return ("%.22f"):format(num):gsub("0+$", "")
end
end
local function forceTypes(storeKey, types, staticValue, forceToTypes, t)
types = newSet((";"):split(types))
forceToTypes = newSet((";"):split(forceToTypes))
if forceToTypes["undef"] then
forceToTypes["undef"] = nil
forceToTypes["nil"] = true
end
if types["boolean"] and staticValue == nil then
assert(type(storeKey) == "string" and (storeKey:match("^arg%d+$") or storeKey == "result"))
if not types["string"] then
if not types["number"] then
t[#t+1] = storeKey
t[#t+1] = [=[ = not not ]=]
t[#t+1] = storeKey
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
else
t[#t+1] = [=[if type(]=]
t[#t+1] = storeKey
t[#t+1] = [=[) ~= "number" then]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = not not ]=]
t[#t+1] = storeKey
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
t[#t+1] = "end;\n"
end
else
t[#t+1] = [=[if ]=]
t[#t+1] = storeKey
t[#t+1] = [=[ == true then]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
t[#t+1] = ([=[%q]=]):format(L["True"])
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
t[#t+1] = "end;\n"
types["boolean"] = nil
types["nil"] = true
end
end
local unfulfilledTypes = newList()
local finalTypes = newList()
for k in pairs(types) do
if not forceToTypes[k] then
unfulfilledTypes[k] = true
else
finalTypes[k] = true
end
end
types = del(types)
if not next(unfulfilledTypes) then
unfulfilledTypes = del(unfulfilledTypes)
local types = joinSet(finalTypes, ';')
finalTypes = del(finalTypes)
forceToTypes = del(forceToTypes)
if type(storeKey) ~= "string" or (not storeKey:match("^arg%d+$") and storeKey ~= "result" and not storeKey:match("^%(.*%)$")) then
storeKey = "(" .. storeKey .. ")"
end
return storeKey, types, staticValue
end
if unfulfilledTypes['nil'] then
-- we have a possible unrequested nil
if forceToTypes['boolean'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
if unfulfilledTypes['number'] or unfulfilledTypes['string'] then
-- and a possible unrequested number or string
t[#t+1] = [=[not not ]=]
t[#t+1] = storeKey
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
staticValue = nil
else
t[#t+1] = [=[false;]=]
t[#t+1] = "\n"
staticValue = false
end
else
assert(storeKey == "nil" or storeKey == "(nil)")
storeKey = "false"
staticValue = false
end
finalTypes['boolean'] = true
elseif forceToTypes['string'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
if unfulfilledTypes['number'] then
-- and a possible unrequested number
t[#t+1] = [=[tostring(]=]
t[#t+1] = storeKey
t[#t+1] = [=[ or '');]=]
t[#t+1] = "\n"
staticValue = nil
elseif forceToTypes['number'] then
t[#t+1] = storeKey
t[#t+1] = [=[ or '';]=]
t[#t+1] = "\n"
finalTypes['number'] = true
staticValue = nil
else
t[#t+1] = [=['';]=]
t[#t+1] = "\n"
staticValue = ''
end
else
if storeKey == "nil" then
storeKey = "''"
staticValue = ''
else
storeKey = tostring(storeKey or "''")
staticValue = nil
end
end
finalTypes['string'] = true
elseif forceToTypes['number'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
if unfulfilledTypes["string"] then
t[#t+1] = storeKey
t[#t+1] = [=[ = tonumber(]=]
t[#t+1] = storeKey
t[#t+1] = [=[) or 0;]=]
t[#t+1] = "\n"
else
t[#t+1] = [=[if not ]=]
t[#t+1] = storeKey
t[#t+1] = [=[ then]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
t[#t+1] = [=[0;]=]
t[#t+1] = "\n"
t[#t+1] = "end;\n"
end
staticValue = nil
else
if storeKey == "nil" then
storeKey = "0"
staticValue = 0
else
staticValue = tonumber(storeKey) or 0
storeKey = numberToString(staticValue)
end
end
finalTypes['number'] = true
end
elseif unfulfilledTypes['number'] then
-- we have a possible unrequested number
if forceToTypes['boolean'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = true;]=]
t[#t+1] = "\n"
else
storeKey = "true"
end
finalTypes['boolean'] = true
staticValue = true
elseif forceToTypes['string'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
if forceToTypes['nil'] then
t[#t+1] = [=[if ]=]
t[#t+1] = storeKey
t[#t+1] = [=[ then]=]
t[#t+1] = "\n"
end
t[#t+1] = storeKey
t[#t+1] = [=[ = tostring(]=]
t[#t+1] = storeKey
t[#t+1] = [=[);]=]
t[#t+1] = "\n"
if forceToTypes['nil'] then
t[#t+1] = "end;\n"
end
staticValue = nil
else
if not forceToTypes['nil'] and storeKey ~= 'nil' and storeKey ~= '(nil)' then
if storeKey:match("^%(.*%)$") then
storeKey = storeKey:sub(2, -2)+0
else
storeKey = storeKey+0
end
staticValue = tostring(storeKey)
storeKey = ("%q"):format(tostring(storeKey))
end
end
finalTypes['string'] = true
elseif forceToTypes['nil'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = nil;]=]
t[#t+1] = "\n"
else
storeKey = "nil"
end
staticValue = "@nil"
finalTypes['nil'] = true
end
elseif unfulfilledTypes['string'] then
-- we have a possible unrequested string
if forceToTypes['boolean'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = true;]=]
t[#t+1] = "\n"
else
storeKey = "true"
end
staticValue = true
finalTypes['boolean'] = true
elseif forceToTypes['number'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = tonumber(]=]
t[#t+1] = storeKey
t[#t+1] = [=[)]=]
if not forceToTypes['nil'] then
t[#t+1] = [=[ or 0]=]
else
finalTypes['nil'] = true
end
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
staticValue = nil
else
staticValue = tonumber(staticValue)
if not forceToTypes['nil'] and not staticValue then
staticValue = 0
end
storeKey = numberToString(staticValue)
end
finalTypes['number'] = true
elseif forceToTypes['nil'] then
if type(storeKey) == "string" and storeKey:match("^arg%d+$") then
t[#t+1] = storeKey
t[#t+1] = [=[ = nil;]=]
t[#t+1] = "\n"
else
storeKey = "nil"
end
finalTypes['nil'] = true
staticValue = "@nil"
end
elseif unfulfilledTypes["boolean"] then
if forceToTypes["string"] then
if staticValue == nil then
t[#t+1] = [=[if ]=]
t[#t+1] = storeKey
t[#t+1] = [=[ then]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
t[#t+1] = ([=[%q]=]):format(L["True"])
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
t[#t+1] = [=[else]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
if forceToTypes["nil"] then
t[#t+1] = [=[nil]=]
finalTypes['nil'] = true
else
t[#t+1] = [=['']=]
end
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
t[#t+1] = "end;\n"
finalTypes['string'] = true
else
if staticValue then
staticValue = L["True"]
storeKey = ("%q"):format(staticValue)
finalTypes['string'] = true
else
if forceToTypes['nil'] then
staticValue = "@nil"
storeKey = 'nil'
finalTypes['nil'] = true
else
staticValue = ""
storeKey = "''"
finalTypes['string'] = true
end
end
end
elseif forceToTypes["number"] then
if staticValue == nil then
t[#t+1] = [=[if ]=]
t[#t+1] = storeKey
t[#t+1] = [=[ then]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = 1;]=]
t[#t+1] = "\n"
t[#t+1] = [=[else]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
if forceToTypes["nil"] then
t[#t+1] = [=[nil]=]
finalTypes['nil'] = true
else
t[#t+1] = [=[0]=]
end
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
t[#t+1] = "end;\n"
finalTypes['number'] = true
else
if staticValue then
staticValue = 1
storeKey = "1"
finalTypes['number'] = true
else
if forceToTypes['nil'] then
staticValue = "@nil"
storeKey = 'nil'
finalTypes['nil'] = true
else
staticValue = ""
storeKey = "0"
finalTypes['number'] = true
end
end
end
elseif forceToTypes["nil"] then
t[#t+1] = storeKey
t[#t+1] = [=[ = nil;]=]
t[#t+1] = "\n"
finalTypes['nil'] = true
staticValue = "@nil"
end
end
unfulfilledTypes = del(unfulfilledTypes)
forceToTypes = del(forceToTypes)
local types = joinSet(finalTypes, ';')
finalTypes = del(finalTypes)
if type(storeKey) ~= "string" or (not storeKey:match("^arg%d+$") and storeKey ~= "result" and not storeKey:match("^%(.*%)$")) then
storeKey = "(" .. storeKey .. ")"
end
return storeKey, types, staticValue
end
local function compile(ast, nsList, t, cachedTags, events, functions, extraKwargs, forceToTypes, storeKey, saveFirstArg)
if #t ~= 0 then
error(("Assertion failed: %s == %s"):format(#t, 0))
end
local astType = getASTType(ast)
if astType == 'nil' or ast == "@undef" then
if storeKey then
t[#t+1] = storeKey
t[#t+1] = [=[ = nil;]=]
t[#t+1] = "\n"
return forceTypes(storeKey, "nil", "@nil", forceToTypes, t)
else
return forceTypes("nil", "nil", "@nil", forceToTypes, t)
end
elseif astType == 'kwarg' then
local kwarg = extraKwargs[ast[2]]
local arg, types = kwarg[1], kwarg[2]
if storeKey then
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
t[#t+1] = arg
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
return forceTypes(storeKey, types, nil, forceToTypes, t)
else
return forceTypes(arg, types, nil, forceToTypes, t)
end
elseif astType == 'string' then
if ast == '' then
return compile(nil, nsList, t, cachedTags, events, functions, extraKwargs, forceToTypes, storeKey)
else
if storeKey then
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
t[#t+1] = ([=[%q]=]):format(ast)
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
return forceTypes(storeKey, "string", ast, forceToTypes, t)
else
return forceTypes(([=[%q]=]):format(ast), "string", ast, forceToTypes, t)
end
end
elseif astType == 'number' then
if storeKey then
t[#t+1] = storeKey
t[#t+1] = [=[ = ]=]
t[#t+1] = numberToString(ast)
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
return forceTypes(storeKey, "number", ast, forceToTypes, t)
else
return forceTypes(numberToString(ast), "number", ast, forceToTypes, t)
end
elseif astType == 'tag' or operators[astType] then
local tag = ast[astType == 'tag' and 2 or 1]
local tagData, tagNS = getTagData(tag, nsList)
if not storeKey then
storeKey = newUniqueVar()
end
local caching, cachingFirst
if astType == 'tag' and not ast.kwarg and cachedTags[tag] then
caching = true
cachingFirst = cachedTags[tag] == 1
cachedTags[tag] = 2
end
local static_t_num = #t
if caching and not cachingFirst then
t[#t+1] = [=[if cache_]=]
t[#t+1] = tag
t[#t+1] = [=[ ~= NIL then]=]
t[#t+1] = "\n"
t[#t+1] = storeKey
t[#t+1] = [=[ = cache_]=]
t[#t+1] = tag
t[#t+1] = [=[;]=]
t[#t+1] = "\n"
t[#t+1] = [=[else]=]
t[#t+1] = "\n"
else
t[#t+1] = [=[do]=]
t[#t+1] = "\n"
end
local kwargs = getKwargsForAST(ast, nsList, extraKwargs)
local arg = tagData.arg
local allArgsStatic = true
local compiledKwargs = newList()
local firstAndNonNil
local firstMaybeNumber = false
for k,v in pairs(kwargs) do
if v == extraKwargs then
compiledKwargs[k] = newList(unpack(extraKwargs[k]))
allArgsStatic = false
else
local argTypes = "nil;number;string;boolean"
local arg_num
local arg_default = false
if not k:match("^%.%.%.%d+$") then
for i = 1, #arg, 3 do
if arg[i] == k then
argTypes = arg[i+1]
arg_num = (i-1)/3 + 1
arg_default = arg[i+2]
break
end
end
else
for i = 1, #arg, 3 do
if arg[i] == "..." then
arg_num = (i-1)/3 + 1
if arg[i+1]:match("^tuple%-") then
argTypes = arg[i+1]:sub(7)
else
break
end
end
end
end
if arg_num == 1 and (arg_default == "@req" or arg_default == "@undef") then
local a = newSet((";"):split(argTypes))
firstAndNonNil = not a["nil"] and not a["boolean"] and k
if firstAndNonNil then
a["undef"] = nil
a["nil"] = true
argTypes = joinSet(a, ";")
end
a = del(a)
end
local u = newList()
local arg, types, static
if arg_num == 1 then
local rawTypes
arg, rawTypes, static = compile(v, nsList, u, cachedTags, events, functions, extraKwargs, "boolean;nil;number;string")
arg, types, static = forceTypes(arg, rawTypes, static, argTypes, u)
local a = newSet((";"):split(rawTypes))
firstMaybeNumber = a['number'] and rawTypes
a = del(a)
else
arg, types, static = compile(v, nsList, u, cachedTags, events, functions, extraKwargs, argTypes)
end
for i,v in ipairs(u) do
t[#t+1] = v
end
u = del(u)
if static == nil then
allArgsStatic = false
end
if firstAndNonNil == k then
local returns = newSet((";"):split(types))
if v == "@undef" then
firstAndNonNil = nil
-- firstAndNonNil_t_num = nil -- unused
elseif not returns["nil"] then
firstAndNonNil = nil
-- firstAndNonNil_t_num = nil -- unused
elseif returns["string"] or returns["number"] then
-- firstAndNonNil_t_num = nil -- unused
end
returns = del(returns)
end
compiledKwargs[k] = newList(arg, types, static)
end
end
if firstAndNonNil then
local compiledKwargs_firstAndNonNil = compiledKwargs[firstAndNonNil]
t[#t+1] = [=[if ]=]
t[#t+1] = compiledKwargs_firstAndNonNil[1]
t[#t+1] = [=[ then]=]
t[#t+1] = "\n"
local args = newSet((';'):split(compiledKwargs_firstAndNonNil[2]))
args['nil'] = nil
compiledKwargs_firstAndNonNil[2] = joinSet(args, ';')
args = del(args)
end
local step_t_num = #t
for step in pairs(compilationSteps.tag[tagNS]) do
step(ast, t, tag, tagData, kwargs, extraKwargs, compiledKwargs)
end
if step_t_num ~= #t then
allArgsStatic = false
end
local passData = newList() -- data that will be passed into functions like ret, code, etc.
for k, v in pairs(kwargs) do
local passData_k = newList()
passData[k] = passData_k
if type(v) ~= "table" or v[1] == "nil" then
local value = type(v) ~= "table" and v or nil
passData_k.isLiteral = true
passData_k.value = value
passData_k.types = type(value)
else
passData_k.isLiteral = false
passData_k.value = v
passData_k.types = compiledKwargs[k][2]
end
end
local code = tagData.code
local ret = tagData.ret
local evs = tagData.events
local static = tagData.static
if type(ret) == "function" then
ret = ret(passData)
end
local funcName
if tagData.dynamicCode then
code = code(passData)
for k, v in pairs(functions) do
if v == code then
funcName = k
end
end
if not funcName then
local pre = (operators[tag] or tag) .. "_"
local num = 1
while functions[pre .. num] do
num = num + 1
end
funcName = pre .. num
functions[funcName] = code
end
else
functions[operators[tag] or tag] = tag
end
if type(evs) == "function" then
evs = evs(passData)
end
if type(static) == "function" then
static = static(passData)
end
for k, v in pairs(passData) do
passData[k] = del(v)
end
passData = del(passData)
evs = evs and newSet((";"):split(evs)) or newSet()
ret = ret and newSet((";"):split(ret)) or newSet("nil")
local u = newList()
for step in pairs(compilationSteps.tagevents[tagNS]) do
u[#u+1] = newList()
step(ast, t, u[#u], tag, tagData, kwargs, extraKwargs, compiledKwargs, evs, ret)
if not next(u[#u]) then
u[#u] = del(u[#u])
end
end
local r = joinSet(ret, ";")
ret = del(ret)
ret = r
local afterAdditions = newList()
for i = #u, 1, -1 do
for _, v in ipairs(u[i]) do
afterAdditions[#afterAdditions+1] = v
end
u[i] = del(u[i])
end
u = del(u)
for k in pairs(evs) do
local ev_params = newList(("#"):split(k))
local ev = ev_params[1]
local events_ev = events[ev]
if events_ev ~= true then
if #ev_params >= 2 then
for i = 2, #ev_params do
local param = ev_params[i]
if param:match("^%$") then
local real_param = param:sub(2)
local compiledKwargs_real_param = compiledKwargs[real_param]
if not compiledKwargs_real_param then
error(("Unknown event parameter %q for tag %s. Please inform ckknight."):format(real_param, tag))
end
local compiledKwargs_real_param_1 = compiledKwargs_real_param[1]
if not compiledKwargs_real_param_1:match("^kwargs_[a-z]+$") then
local kwargs_real_param = kwargs[real_param]
if type(kwargs_real_param) == "table" then
if kwargs_real_param[1] == "kwarg" then
param = "$" .. kwargs_real_param[2]
else
param = unparse(kwargs_real_param)
end
else
param = kwargs_real_param or true
end
ev_params[i] = param
end
end
end
local paramResult
if #ev_params == 2 then
paramResult = ev_params[2]
else
paramResult = table.concat(ev_params, '#', 2, #ev_params)
end
if type(events_ev) == "table" then
if paramResult == true then
del(events_ev)
events[ev] = true
else
events_ev[paramResult] = true
end
elseif events_ev and events_ev ~= paramResult then
if paramResult == true then
events[ev] = true
else
events[ev] = newSet(events_ev, paramResult)
end
else
events[ev] = paramResult
end
else