forked from teal-language/tl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tl.lua
11078 lines (9335 loc) · 329 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 _tl_table_unpack = unpack or table.unpack; local utf8 = _tl_compat and _tl_compat.utf8 or utf8
local VERSION = "0.15.3+dev"
local tl = {PrettyPrintOptions = {}, TypeCheckOptions = {}, Env = {}, Symbol = {}, Result = {}, Error = {}, TypeInfo = {}, TypeReport = {}, TypeReportEnv = {}, }
tl.version = function()
return VERSION
end
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,
IS_TABLE = 0x00000008,
IS_NUMBER = 0x00000002,
IS_STRING = 0x00000004,
LUA_MASK = 0x00000fff,
INTEGER = 0x00010002,
ARRAY = 0x00010008,
RECORD = 0x00020008,
ARRAYRECORD = 0x00030008,
MAP = 0x00040008,
TUPLE = 0x00080008,
EMPTY_TABLE = 0x00000008,
ENUM = 0x00010004,
IS_ARRAY = 0x00010008,
IS_RECORD = 0x00020008,
NOMINAL = 0x10000000,
TYPE_VARIABLE = 0x08000000,
IS_UNION = 0x40000000,
IS_POLY = 0x20000020,
ANY = 0xffffffff,
UNKNOWN = 0x80008000,
INVALID = 0x80000000,
IS_SPECIAL = 0x80000000,
IS_VALID = 0x00000fff,
}
local TL_DEBUG = os.getenv("TL_DEBUG")
local TL_DEBUG_MAXLINE = _tl_math_maxinteger
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
io.stderr:write(info.name or "<anon>", info.currentline > 0 and "@" .. info.currentline or "", " :: ", 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 invalid_string$",
["string single got \\"] = "$ERR invalid_string$",
["string double"] = "$ERR invalid_string$",
["string double got \\"] = "$ERR invalid_string$",
["string long"] = "$ERR invalid_string$",
["string long got ]"] = "$ERR invalid_string$",
["comment short"] = nil,
["comment long"] = "$ERR unfinished_comment$",
["comment long got ]"] = "$ERR unfinished_comment$",
["number dec"] = "integer",
["number decfloat"] = "number",
["number hex"] = "integer",
["number hexfloat"] = "number",
["number power"] = "number",
["number powersign"] = "$ERR invalid_number$",
}
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
end
for c = string.byte("a"), string.byte("f") do
lex_hexadecimals[string.char(c)] = true
end
for c = string.byte("A"), string.byte("F") do
lex_hexadecimals[string.char(c)] = true
end
local lex_any_char_kinds = {}
local single_char_kinds = { "[", "]", "(", ")", "{", "}", ",", "#", ";" }
for _, c in ipairs(single_char_kinds) do
lex_any_char_kinds[c] = c
end
for _, c in ipairs({ "+", "*", "|", "&", "%", "^" }) do
lex_any_char_kinds[c] = "op"
end
local lex_space = {}
for _, c in ipairs({ " ", "\t", "\v", "\n", "\r" }) do
lex_space[c] = true
end
local escapable_characters = {
a = true,
b = true,
f = true,
n = true,
r = true,
t = true,
v = true,
z = true,
["\\"] = true,
["\'"] = true,
["\""] = true,
["\r"] = true,
["\n"] = true,
}
local function lex_string_escape(input, i, c)
if escapable_characters[c] then
return 0, true
elseif c == "x" then
return 2, (
lex_hexadecimals[input:sub(i + 1, i + 1)] and
lex_hexadecimals[input:sub(i + 2, i + 2)])
elseif c == "u" then
if input:sub(i + 1, i + 1) == "{" then
local p = i + 2
if not lex_hexadecimals[input:sub(p, p)] then
return 2, false
end
while true do
p = p + 1
c = input:sub(p, p)
if not lex_hexadecimals[c] then
return p - i, c == "}"
end
end
end
elseif lex_decimals[c] then
local len = lex_decimals[input:sub(i + 1, i + 1)] and
(lex_decimals[input:sub(i + 2, i + 2)] and 2 or 1) or
0
return len, tonumber(input:sub(i, i + len)) < 256
else
return 0, false
end
end
function tl.lex(input, filename)
local tokens = {}
local state = "any"
local fwd = true
local y = 1
local x = 0
local i = 0
local lc_open_lvl = 0
local lc_close_lvl = 0
local ls_open_lvl = 0
local ls_close_lvl = 0
local errs = {}
local nt = 0
local tx
local ty
local ti
local in_token = false
local function begin_token()
tx = x
ty = y
ti = i
in_token = true
end
local function end_token(kind, tk)
nt = nt + 1
tokens[nt] = {
x = tx,
y = ty,
tk = tk,
kind = kind,
}
in_token = false
end
local function end_token_identifier()
local tk = input:sub(ti, i - 1)
nt = nt + 1
tokens[nt] = {
x = tx,
y = ty,
tk = tk,
kind = keywords[tk] and "keyword" or "identifier",
}
in_token = false
end
local function end_token_prev(kind)
local tk = input:sub(ti, i - 1)
nt = nt + 1
tokens[nt] = {
x = tx,
y = ty,
tk = tk,
kind = kind,
}
in_token = false
end
local function end_token_here(kind)
local tk = input:sub(ti, i)
nt = nt + 1
tokens[nt] = {
x = tx,
y = ty,
tk = tk,
kind = kind,
}
in_token = false
end
local function drop_token()
in_token = false
end
local function add_syntax_error()
local t = tokens[nt]
local msg
if t.kind == "$ERR invalid_string$" then
msg = "malformed string"
elseif t.kind == "$ERR invalid_number$" then
msg = "malformed number"
elseif t.kind == "$ERR unfinished_comment$" then
msg = "unfinished long comment"
else
msg = "invalid token '" .. t.tk .. "'"
end
table.insert(errs, {
filename = filename,
y = t.y,
x = t.x,
msg = msg,
})
end
local len = #input
if input:sub(1, 2) == "#!" then
begin_token()
i = input:find("\n")
if not i then
i = len + 1
end
end_token_prev("hashbang")
y = 2
x = 0
end
state = "any"
while i <= len do
if fwd then
i = i + 1
if i > len then
break
end
end
local c = input:sub(i, i)
if fwd then
if c == "\n" then
y = y + 1
x = 0
else
x = x + 1
end
else
fwd = true
end
if state == "any" then
local st = lex_any_char_states[c]
if st then
state = st
begin_token()
else
local k = lex_any_char_kinds[c]
if k then
begin_token()
end_token(k, c)
elseif not lex_space[c] then
begin_token()
end_token_here("$ERR invalid$")
add_syntax_error()
end
end
elseif state == "identifier" then
if not lex_word[c] then
end_token_identifier()
fwd = false
state = "any"
end
elseif state == "string double" then
if c == "\\" then
state = "string double got \\"
elseif c == "\"" then
end_token_here("string")
state = "any"
end
elseif state == "comment short" then
if c == "\n" then
state = "any"
end
elseif state == "got =" then
local t
if c == "=" then
t = "=="
else
t = "="
fwd = false
end
end_token("op", t)
state = "any"
elseif state == "got ." then
if c == "." then
state = "got .."
elseif lex_decimals[c] then
state = "number decfloat"
else
end_token(".", ".")
fwd = false
state = "any"
end
elseif state == "got :" then
local t
if c == ":" then
t = "::"
else
t = ":"
fwd = false
end
end_token(t, t)
state = "any"
elseif state == "got [" then
if c == "[" then
state = "string long"
elseif c == "=" then
ls_open_lvl = ls_open_lvl + 1
else
end_token("[", "[")
fwd = false
state = "any"
ls_open_lvl = 0
end
elseif state == "number dec" then
if lex_decimals[c] then
elseif c == "." then
state = "number decfloat"
elseif c == "e" or c == "E" then
state = "number powersign"
else
end_token_prev("integer")
fwd = false
state = "any"
end
elseif state == "got -" then
if c == "-" then
state = "got --"
else
end_token("op", "-")
fwd = false
state = "any"
end
elseif state == "got .." then
if c == "." then
end_token("...", "...")
else
end_token("op", "..")
fwd = false
end
state = "any"
elseif state == "number hex" then
if lex_hexadecimals[c] then
elseif c == "." then
state = "number hexfloat"
elseif c == "p" or c == "P" then
state = "number powersign"
else
end_token_prev("integer")
fwd = false
state = "any"
end
elseif state == "got --" then
if c == "[" then
state = "got --["
else
fwd = false
state = "comment short"
drop_token()
end
elseif state == "got 0" then
if c == "x" or c == "X" then
state = "number hex"
elseif c == "e" or c == "E" then
state = "number powersign"
elseif lex_decimals[c] then
state = "number dec"
elseif c == "." then
state = "number decfloat"
else
end_token_prev("integer")
fwd = false
state = "any"
end
elseif state == "got --[" then
if c == "[" then
state = "comment long"
elseif c == "=" then
lc_open_lvl = lc_open_lvl + 1
else
fwd = false
state = "comment short"
drop_token()
lc_open_lvl = 0
end
elseif state == "comment long" then
if c == "]" then
state = "comment long got ]"
end
elseif state == "comment long got ]" then
if c == "]" and lc_close_lvl == lc_open_lvl then
drop_token()
state = "any"
lc_open_lvl = 0
lc_close_lvl = 0
elseif c == "=" then
lc_close_lvl = lc_close_lvl + 1
else
state = "comment long"
lc_close_lvl = 0
end
elseif state == "string double got \\" then
local skip, valid = lex_string_escape(input, i, c)
i = i + skip
if not valid then
end_token_here("$ERR invalid_string$")
add_syntax_error()
end
x = x + skip
state = "string double"
elseif state == "string single" then
if c == "\\" then
state = "string single got \\"
elseif c == "'" then
end_token_here("string")
state = "any"
end
elseif state == "string single got \\" then
local skip, valid = lex_string_escape(input, i, c)
i = i + skip
if not valid then
end_token_here("$ERR invalid_string$")
add_syntax_error()
end
x = x + skip
state = "string single"
elseif state == "got ~" then
local t
if c == "=" then
t = "~="
else
t = "~"
fwd = false
end
end_token("op", t)
state = "any"
elseif state == "got <" then
local t
if c == "=" then
t = "<="
elseif c == "<" then
t = "<<"
else
t = "<"
fwd = false
end
end_token("op", t)
state = "any"
elseif state == "got >" then
local t
if c == "=" then
t = ">="
elseif c == ">" then
t = ">>"
else
t = ">"
fwd = false
end
end_token("op", t)
state = "any"
elseif state == "got /" then
local t
if c == "/" then
t = "//"
else
t = "/"
fwd = false
end
end_token("op", t)
state = "any"
elseif state == "string long" then
if c == "]" then
state = "string long got ]"
end
elseif state == "string long got ]" then
if c == "]" then
if ls_close_lvl == ls_open_lvl then
end_token_here("string")
state = "any"
ls_open_lvl = 0
ls_close_lvl = 0
end
elseif c == "=" then
ls_close_lvl = ls_close_lvl + 1
else
state = "string long"
ls_close_lvl = 0
end
elseif state == "number hexfloat" then
if c == "p" or c == "P" then
state = "number powersign"
elseif not lex_hexadecimals[c] then
end_token_prev("number")
fwd = false
state = "any"
end
elseif state == "number decfloat" then
if c == "e" or c == "E" then
state = "number powersign"
elseif not lex_decimals[c] then
end_token_prev("number")
fwd = false
state = "any"
end
elseif state == "number powersign" then
if c == "-" or c == "+" then
state = "number power"
elseif lex_decimals[c] then
state = "number power"
else
end_token_here("$ERR invalid_number$")
add_syntax_error()
state = "any"
end
elseif state == "number power" then
if not lex_decimals[c] then
end_token_prev("number")
fwd = false
state = "any"
end
end
end
if in_token then
if last_token_kind[state] then
end_token_prev(last_token_kind[state])
if last_token_kind[state]:sub(1, 4) == "$ERR" then
add_syntax_error()
elseif keywords[tokens[nt].tk] then
tokens[nt].kind = "keyword"
end
else
drop_token()
end
end
table.insert(tokens, { x = x + 1, y = y, i = i, tk = "$EOF$", kind = "$EOF$" })
return tokens, errs
end
end
local function binary_search(list, item, cmp)
local len = #list
local mid
local s, e = 1, len
while s <= e do
mid = math.floor((s + e) / 2)
local val = list[mid]
local res = cmp(val, item)
if res then
if mid == len then
return mid, val
else
if not cmp(list[mid + 1], item) then
return mid, val
end
end
s = mid + 1
else
e = mid - 1
end
end
end
function tl.get_token_at(tks, y, x)
local _, found = binary_search(
tks, nil,
function(tk)
return tk.y < y or
(tk.y == y and tk.x <= x)
end)
if found and
found.y == y and
found.x <= x and x < found.x + #found.tk then
return found.tk
end
end
local last_typeid = 0
local function new_typeid()
last_typeid = last_typeid + 1
return last_typeid
end