forked from teal-language/tl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tl.lua
13635 lines (10980 loc) · 380 KB
/
tl.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 _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local load = _tl_compat and _tl_compat.load or load; local math = _tl_compat and _tl_compat.math or math; local _tl_math_maxinteger = math.maxinteger or math.pow(2, 53); local os = _tl_compat and _tl_compat.os or os; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local utf8 = _tl_compat and _tl_compat.utf8 or utf8
local VERSION = "0.24.1+dev"
local stdlib = [=====[
do
global type any
global type thread
global type userdata
local enum FileStringMode
"a" "l" "L" "*a" "*l" "*L"
end
local enum FileNumberMode
"n" "*n"
end
local enum FileMode
"a" "l" "L" "*a" "*l" "*L" "n" "*n"
end
global record FILE
userdata
enum SeekWhence
"set" "cur" "end"
end
enum SetVBufMode
"no" "full" "line"
end
close: function(FILE): boolean, string, number
flush: function(FILE)
lines: function(FILE): (function(): (string))
lines: function(FILE, FileNumberMode...): (function(): (number...))
lines: function(FILE, (number | FileStringMode)...): (function(): (string...))
lines: function(FILE, (number | FileMode)...): (function(): ((string | number)...))
lines: function(FILE, (number | string)...): (function(): (string...))
read: function(FILE): string
read: function(FILE, FileNumberMode...): number...
read: function(FILE, (number | FileStringMode)...): string...
read: function(FILE, (number | FileMode)...): ((string | number)...)
read: function(FILE, (number | string)...): (string...)
seek: function(FILE, ? SeekWhence, ? integer): integer, string
setvbuf: function(FILE, SetVBufMode, ? integer)
write: function(FILE, (string | number)...): FILE, string
metamethod __close: function(FILE)
end
global record coroutine
type Function = function(any...): any...
close: function(thread): boolean, string
create: function(Function): thread
isyieldable: function(): boolean
resume: function(thread, any...): boolean, any...
running: function(): thread, boolean
status: function(thread): string
wrap: function<F>(F): F
yield: function(any...): any...
end
global record debug
record GetInfoTable
name: string
namewhat: string
source: string
short_src: string
linedefined: integer
lastlinedefined: integer
what: string
currentline: integer
istailcall: boolean
nups: integer
nparams: integer
isvararg: boolean
func: any
activelines: {integer:boolean}
end
enum HookEvent
"call" "tail call" "return" "line" "count"
end
type HookFunction = function(HookEvent, integer)
type AnyFunction = function(any...):any...
debug: function()
gethook: function(? thread): HookFunction, integer
getinfo: function(AnyFunction | integer): GetInfoTable
getinfo: function(AnyFunction | integer, string): GetInfoTable
getinfo: function(thread, AnyFunction | integer, string): GetInfoTable
getlocal: function(thread, AnyFunction, integer): string
getlocal: function(thread, integer, integer): string, any
getlocal: function(AnyFunction, integer): string
getlocal: function(integer, integer): string, any
getmetatable: function<T>(T): metatable<T>
getregistry: function(): {any:any}
getupvalue: function(AnyFunction, integer): any
getuservalue: function(userdata, integer): any
sethook: function(thread, HookFunction, string, ? integer)
sethook: function(HookFunction, string, ? integer)
setlocal: function(thread, integer, integer, any): string
setlocal: function(integer, integer, any): string
setmetatable: function<T>(T, metatable<T>): T
setupvalue: function(AnyFunction, integer, any): string
setuservalue: function<U>(U, any, integer): U --[[U is userdata]]
traceback: function(thread, ? string, ? integer): string
traceback: function(? string, ? integer): string
upvalueid: function(AnyFunction, integer): userdata
upvaluejoin: function(AnyFunction, integer, AnyFunction, integer)
end
global record io
enum OpenMode
"r" "w" "a" "r+" "w+" "a+"
"rb" "wb" "ab" "r+b" "w+b" "a+b"
"*r" "*w" "*a" "*r+" "*w+" "*a+"
"*rb" "*wb" "*ab" "*r+b" "*w+b" "*a+b"
end
close: function(? FILE)
input: function(? FILE): FILE
flush: function()
lines: function(? string): (function(): (string))
lines: function(? string, FileNumberMode...): (function(): (number...))
lines: function(? string, (number | FileStringMode)...): (function(): (string...))
lines: function(? string, (number | FileMode)...): (function(): ((string | number)...))
lines: function(? string, (number | string)...): (function(): (string...))
open: function(string, ? OpenMode): FILE, string, integer
output: function(? FILE): FILE
popen: function(string, ? OpenMode): FILE, string
read: function(): string
read: function(FileNumberMode...): number...
read: function((number | FileStringMode)...): string...
read: function((number | FileMode)...): ((string | number)...)
read: function((number | string)...): (string...)
stderr: FILE
stdin: FILE
stdout: FILE
tmpfile: function(): FILE
type: function(any): string
write: function((string | number)...): FILE, string
end
global record math
type Numeric = number | integer
abs: function<N is Numeric>(N): N
acos: function(number): number
asin: function(number): number
atan: function(number, ? number): number
atan2: function(number, number): number
ceil: function(number): integer
cos: function(number): number
cosh: function(number): number
deg: function(number): number
exp: function(number): number
floor: function(number): integer
fmod: function(integer, integer): integer
fmod: function(number, number): number
frexp: function(number): number, integer
huge: number
ldexp: function(number, integer): number
log: function(number, ? number): number
log10: function(number): number
max: function(integer...): integer
max: function((number | integer)...): number
max: function<T>(T...): T
max: function(any...): any
maxinteger: integer --[[needs_compat]]
min: function(integer...): integer
min: function((number | integer)...): number
min: function<T>(T...): T
min: function(any...): any
mininteger: integer --[[needs_compat]]
modf: function(number): integer, number
pi: number
pow: function(number, number): number
rad: function(number): number
random: function(integer, ? integer): integer
random: function(): number
randomseed: function(? integer, ? integer): integer, integer
sin: function(number): number
sinh: function(number): number
sqrt: function(number): number
tan: function(number): number
tanh: function(number): number
tointeger: function(any): integer
type: function(any): string
ult: function(number, number): boolean
end
global record metatable<T>
enum Mode
"k" "v" "kv"
end
__call: function(T, any...): any...
__mode: Mode
__name: string
__tostring: function(T): string
__pairs: function<K, V>(T): function(): (K, V)
__index: any --[[FIXME: function | table | anything with an __index metamethod]]
__newindex: any --[[FIXME: function | table | anything with an __index metamethod]]
__gc: function(T)
__close: function(T)
__add: function<A, B, C>(A, B): C
__sub: function<A, B, C>(A, B): C
__mul: function<A, B, C>(A, B): C
__div: function<A, B, C>(A, B): C
__idiv: function<A, B, C>(A, B): C
__mod: function<A, B, C>(A, B): C
__pow: function<A, B, C>(A, B): C
__band: function<A, B, C>(A, B): C
__bor: function<A, B, C>(A, B): C
__bxor: function<A, B, C>(A, B): C
__shl: function<A, B, C>(A, B): C
__shr: function<A, B, C>(A, B): C
__concat: function<A, B, C>(A, B): C
__len: function<A>(T): A
__unm: function<A>(T): A
__bnot: function<A>(T): A
__eq: function<A, B>(A, B): boolean
__lt: function<A, B>(A, B): boolean
__le: function<A, B>(A, B): boolean
end
global record os
record DateTable
year: integer
month: integer
day: integer
hour: integer
min: integer
sec: integer
wday: integer
yday: integer
isdst: boolean
end
enum DateMode
"!*t" "*t"
end
clock: function(): number
date: function(DateMode, ? number): DateTable
date: function(? string, ? number): string
difftime: function(integer, integer): number
execute: function(string): boolean, string, integer
exit: function(? (integer | boolean), ? boolean)
getenv: function(string): string
remove: function(string): boolean, string
rename: function(string, string): boolean, string
setlocale: function(string, ? string): string
time: function(? DateTable): integer
tmpname: function(): string
end
global record package
config: string
cpath: string
loaded: {string:any}
loadlib: function(string, string): (function)
loaders: { function(string): any, any }
path: string
preload: {any:any}
searchers: { function(string): any }
searchpath: function(string, string, ? string, ? string): string, string
end
global record string
byte: function(string, ? integer): integer
byte: function(string, integer, ? integer): integer...
char: function(integer...): string
dump: function(function(any...): (any), ? boolean): string
find: function(string, string, ? integer, ? boolean): integer, integer, string
format: function(string, any...): string
gmatch: function(string, string, ? integer): (function(): string...)
gsub: function(string, string, string, ? integer): string, integer
gsub: function(string, string, {string:string}, ? integer): string, integer
gsub: function(string, string, function(string...): ((string | integer | number)...), ? integer): string, integer
len: function(string): integer
lower: function(string): string
match: function(string, string, ? integer): string...
pack: function(string, any...): string
packsize: function(string): integer
rep: function(string, integer, ? string): string
reverse: function(string): string
sub: function(string, integer, ? integer): string
unpack: function(string, string, ? integer): any...
upper: function(string): string
end
global record table
type SortFunction = function<A>(A, A): boolean
record PackTable<A>
is {A}
n: integer
end
concat: function({(string | number)}, ? string, ? integer, ? integer): string
insert: function<A>({A}, integer, A)
insert: function<A>({A}, A)
move: function<A>({A}, integer, integer, integer, ? {A}): {A}
pack: function<T>(T...): PackTable<T> --[[needs_compat]]
pack: function(any...): {any:any} --[[needs_compat]]
remove: function<A>({A}, ? integer): A
sort: function<A>({A}, ? SortFunction<A>)
unpack: function<A>({A}, ? number, ? number): A... --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
end
global record utf8
char: function(number...): string
charpattern: string
codepoint: function(string, ? number, ? number, ? boolean): number...
codes: function(string, ? boolean): (function(string, ? number): (number, number))
len: function(string, ? number, ? number, ? boolean): number
offset: function(string, number, ? number): number
end
local record StandardLibrary
enum CollectGarbageCommand
"collect"
"count"
"stop"
"restart"
end
enum CollectGarbageSetValue
"step"
"setpause"
"setstepmul"
end
enum CollectGarbageIsRunning
"isrunning"
end
type LoadFunction = function(): string
enum LoadMode
"b" "t" "bt"
end
type XpcallMsghFunction = function(...: any): ()
arg: {string}
assert: function<A, B>(A, ? B, ...: any): A
collectgarbage: function(? CollectGarbageCommand): number
collectgarbage: function(CollectGarbageSetValue, integer): number
collectgarbage: function(CollectGarbageIsRunning): boolean
collectgarbage: function(string, ? number): (boolean | number)
dofile: function(? string): any...
error: function(? any, ? integer)
getmetatable: function<T>(T): metatable<T>
ipairs: function<A>({A}): (function():(integer, A))
load: function((string | LoadFunction), ? string, ? LoadMode, ? table): (function, string)
load: function((string | LoadFunction), ? string, ? string, ? table): (function, string)
loadfile: function(? string, ? string, ? {any:any}): (function, string)
next: function<K, V>({K:V}, ? K): (K, V)
next: function<A>({A}, ? integer): (integer, A)
pairs: function<K, V>({K:V}): (function():(K, V))
pcall: function(function(any...):(any...), any...): boolean, any...
print: function(any...)
rawequal: function(any, any): boolean
rawget: function<K, V>({K:V}, K): V
rawget: function({any:any}, any): any
rawget: function(any, any): any
rawlen: function<A>({A}): integer
rawset: function<K, V>({K:V}, K, V): {K:V}
rawset: function({any:any}, any, any): {any:any}
rawset: function(any, any, any): any
require: function(string): any
select: function<T>(integer, T...): T...
select: function(integer, any...): any...
select: function(string, any...): integer
setmetatable: function<T>(T, metatable<T>): T
tonumber: function(any): number
tonumber: function(any, integer): integer
tostring: function(any): string
type: function(any): string
warn: function(string, string...)
xpcall: function(function(any...):(any...), XpcallMsghFunction, any...): boolean, any...
_VERSION: string
end
global arg <const> = StandardLibrary.arg
global assert <const> = StandardLibrary.assert
global collectgarbage <const> = StandardLibrary.collectgarbage
global dofile <const> = StandardLibrary.dofile
global error <const> = StandardLibrary.error
global getmetatable <const> = StandardLibrary.getmetatable
global load <const> = StandardLibrary.load
global loadfile <const> = StandardLibrary.loadfile
global next <const> = StandardLibrary.next
global pairs <const> = StandardLibrary.pairs
global pcall <const> = StandardLibrary.pcall
global print <const> = StandardLibrary.print
global rawequal <const> = StandardLibrary.rawequal
global rawget <const> = StandardLibrary.rawget
global rawlen <const> = StandardLibrary.rawlen
global rawset <const> = StandardLibrary.rawset
global require <const> = StandardLibrary.require
global select <const> = StandardLibrary.select
global setmetatable <const> = StandardLibrary.setmetatable
global tostring <const> = StandardLibrary.tostring
global tonumber <const> = StandardLibrary.tonumber
global ipairs <const> = StandardLibrary.ipairs
global type <const> = StandardLibrary.type
global xpcall <const> = StandardLibrary.xpcall
global _VERSION <const> = StandardLibrary._VERSION
end
]=====]
local Errors = {}
local tl = {GenerateOptions = {}, CheckOptions = {}, Env = {}, Result = {}, Error = {}, TypeInfo = {}, TypeReport = {}, EnvOptions = {}, Token = {}, TypeCheckOptions = {}, }
local TypeReporter = {}
local wk = {
["unknown"] = true,
["unused"] = true,
["redeclaration"] = true,
["branch"] = true,
["hint"] = true,
["debug"] = true,
}
tl.warning_kinds = wk
tl.typecodes = {
NIL = 0x00000001,
NUMBER = 0x00000002,
BOOLEAN = 0x00000004,
STRING = 0x00000008,
TABLE = 0x00000010,
FUNCTION = 0x00000020,
USERDATA = 0x00000040,
THREAD = 0x00000080,
INTEGER = 0x00010002,
ENUM = 0x00010004,
EMPTY_TABLE = 0x00000008,
ARRAY = 0x00010008,
RECORD = 0x00020008,
MAP = 0x00040008,
TUPLE = 0x00080008,
INTERFACE = 0x00100008,
SELF = 0x00200008,
POLY = 0x20000020,
UNION = 0x40000000,
NOMINAL = 0x10000000,
TYPE_VARIABLE = 0x08000000,
ANY = 0xffffffff,
UNKNOWN = 0x80008000,
INVALID = 0x80000000,
}
local DEFAULT_GEN_COMPAT = "optional"
local DEFAULT_GEN_TARGET = "5.3"
local TL_DEBUG = os.getenv("TL_DEBUG")
local TL_DEBUG_FACTS = os.getenv("TL_DEBUG_FACTS")
local TL_DEBUG_MAXLINE = _tl_math_maxinteger
if TL_DEBUG_FACTS and not TL_DEBUG then
TL_DEBUG = "1"
end
if TL_DEBUG then
local max = assert(tonumber(TL_DEBUG), "TL_DEBUG was defined, but not a number")
if max < 0 then
TL_DEBUG_MAXLINE = math.tointeger(-max)
elseif max > 1 then
local count = 0
local skip = nil
debug.sethook(function(event)
if event == "call" or event == "tail call" or event == "return" then
local info = debug.getinfo(2)
if skip then
if info.name == skip and event == "return" then
skip = nil
end
return
elseif (info.name or "?"):match("^tl_debug_") and event == "call" then
skip = info.name
return
end
local name = info.name or "<anon>", info.currentline > 0 and "@" .. info.currentline or ""
io.stderr:write(name, " :: ", event, "\n")
io.stderr:flush()
else
count = count + 100
if count > max then
error("Too many instructions")
end
end
end, "cr", 100)
end
end
do
local last_token_kind = {
["start"] = nil,
["any"] = nil,
["identifier"] = "identifier",
["got -"] = "op",
["got --"] = nil,
["got ."] = ".",
["got .."] = "op",
["got ="] = "op",
["got ~"] = "op",
["got ["] = "[",
["got 0"] = "number",
["got <"] = "op",
["got >"] = "op",
["got /"] = "op",
["got :"] = "op",
["got --["] = nil,
["string single"] = "$ERR$",
["string single got \\"] = "$ERR$",
["string double"] = "$ERR$",
["string double got \\"] = "$ERR$",
["string long"] = "$ERR$",
["string long got ]"] = "$ERR$",
["comment short"] = nil,
["comment long"] = "$ERR$",
["comment long got ]"] = "$ERR$",
["number dec"] = "integer",
["number decfloat"] = "number",
["number hex"] = "integer",
["number hexfloat"] = "number",
["number power"] = "number",
["number powersign"] = "$ERR$",
["pragma"] = nil,
["pragma any"] = nil,
["pragma word"] = "pragma_identifier",
}
local keywords = {
["and"] = true,
["break"] = true,
["do"] = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["false"] = true,
["for"] = true,
["function"] = true,
["goto"] = true,
["if"] = true,
["in"] = true,
["local"] = true,
["nil"] = true,
["not"] = true,
["or"] = true,
["repeat"] = true,
["return"] = true,
["then"] = true,
["true"] = true,
["until"] = true,
["while"] = true,
}
local lex_any_char_states = {
["\""] = "string double",
["'"] = "string single",
["-"] = "got -",
["."] = "got .",
["0"] = "got 0",
["<"] = "got <",
[">"] = "got >",
["/"] = "got /",
[":"] = "got :",
["="] = "got =",
["~"] = "got ~",
["["] = "got [",
}
for c = string.byte("a"), string.byte("z") do
lex_any_char_states[string.char(c)] = "identifier"
end
for c = string.byte("A"), string.byte("Z") do
lex_any_char_states[string.char(c)] = "identifier"
end
lex_any_char_states["_"] = "identifier"
for c = string.byte("1"), string.byte("9") do
lex_any_char_states[string.char(c)] = "number dec"
end
local lex_word = {}
for c = string.byte("a"), string.byte("z") do
lex_word[string.char(c)] = true
end
for c = string.byte("A"), string.byte("Z") do
lex_word[string.char(c)] = true
end
for c = string.byte("0"), string.byte("9") do
lex_word[string.char(c)] = true
end
lex_word["_"] = true
local lex_decimals = {}
for c = string.byte("0"), string.byte("9") do
lex_decimals[string.char(c)] = true
end
local lex_hexadecimals = {}
for c = string.byte("0"), string.byte("9") do
lex_hexadecimals[string.char(c)] = true