-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmylang.lua
More file actions
1124 lines (1042 loc) · 23.6 KB
/
Copy pathmylang.lua
File metadata and controls
1124 lines (1042 loc) · 23.6 KB
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 new_lang = require 'lua_codegen.new_lang'
local tonumber = tonumber
local strmatch = string.match
local strchar = string.char
local tconcat = table.concat
local tinsert = table.insert
local one_char_tokens = {
['(']=true, [')']=true, ['[']=true, [']']=true,
['{']=true, ['}']=true, ['~']=true, ['@']=true,
[',']=true, ['\\']=true, ['?']=true,
}
local optional_eq_tokens = {
['+']=true, ['*']=true, ['/']=true,
['%']=true, ['^']=true, ['>']=true, ['#']=true,
['!']=true, ['=']=true,
}
local keywords = {
['if'] = true,
['else'] = true,
['elseif'] = true,
['while'] = true,
['repeat'] = true,
['until'] = true,
['do'] = true,
['for'] = true,
['in'] = true,
['fn'] = true,
['end'] = true,
['local'] = true,
['goto'] = true,
['return'] = true,
['and'] = true,
['or'] = true,
['not'] = true,
['function'] = true,
['then'] = true,
}
local string_backslash_escapes = {
n = '\n',
r = '\r',
t = '\t',
v = '\v',
a = '\a',
['$'] = '$',
}
local function nexttok(ls)
local c = ls.c
if not c then
ls.tok = nil
return nil
end
local b = c:byte()
if ls.in_dq_str then
local chars = {}
if c == '$' then
c = ls.getch()
if c == '(' then
ls.tok = 'dqstr_expr'
ls.getch()
return
else
chars[1] = c
repeat
c = ls.getch()
if not c then
ls.err('unfinished double-quoted string', ls.line, true)
end
until not c:match('[_%w]')
ls.tok = 'dqstr_var'
ls.tokval = tconcat(chars)
return
end
elseif c == '"' then
ls.getch()
ls.tok = 'dqstr_end'
return
elseif not c then
ls.err('unfinished double-quoted string', ls.line, true)
else
while true do
if not c or c == '"' or c == '$' then
break
elseif c == '\\' then
c = ls.getch()
local cc = string_backslash_escapes[c]
if cc then
chars[#chars+1] = cc
c = ls.getch()
elseif c == '"' then
chars[#chars+1] = '"'
c = ls.getch()
elseif c == 'x' then
c = ls.getch()
if not strmatch(c, '%x') then
ls.err('expected hexadecimal digit after \\x in double-quoted string')
end
local cc = tonumber(c, 16)
c = ls.getch()
if strmatch(c, '%x') then
local cc = cc*16 + tonumber(c, 16)
else
ls.err('expected 2 hexadecimal digits after \\x in double-quoted string')
end
chars[#chars+1] = strchar(cc)
c = ls.getch()
else
if not strmatch(c, '%d') then
ls.err('invalid double-quoted string escape sequence')
end
local cc = tonumber(c)
c = ls.getch()
if strmatch(c, '%d') then
cc = cc*10 + tonumber(c)
c = ls.getch()
if strmatch(c, '%d') then
cc = cc*10 + tonumber(c)
end
end
if cc > 255 then
ls.err('charater value must be between 0 and 255 (inclusive)')
end
chars[#chars+1] = strchar(cc)
end
else
chars[#chars+1] = c
c = ls.getch()
end
end
ls.tok = 'dqstr_text'
ls.tokval = tconcat(chars)
return
end
end
while c==' ' or c=='\t' do
c = ls.getch()
end
if c=='#' then
repeat
c = ls.getch()
until c=='\n' or not c
return nexttok(ls)
end
if not c then
ls.tok = nil
return nil
end
if one_char_tokens[c] then
ls.getch()
ls.tok = c
elseif optional_eq_tokens[c] then
local cc = ls.getch()
if cc=='=' then
ls.getch()
ls.tok = c..'='
else
ls.tok = c
end
elseif c=='.' then
local cc = ls.getch()
if cc=='.' then
local ccc = ls.getch()
if ccc=='=' then
ls.getch()
ls.tok = '..='
else
ls.tok = '..'
end
else
ls.tok = '.'
end
elseif c==':' then
c = ls.getch()
if c==':' then
ls.getch()
ls.tok = '::'
else
ls.tok = ':'
end
elseif c=='-' then
c = ls.getch()
if c=='>' then
ls.getch()
ls.tok = '->'
else
ls.tok = '-'
end
elseif c=='<' then
c = ls.getch()
if c=='=' then
c = ls.getch()
if c=='>' then
ls.getch()
ls.tok = '<=>'
elseif c=='-' then
c = ls.getch()
if c == '>' then
ls.getch()
ls.tok = '<=->'
else
ls.err('invalid token')
end
else
ls.tok = '<='
end
elseif c=='-' then
c = ls.getch()
if c == '=' then
c = ls.getch()
if c == '>' then
ls.getch()
ls.tok = '<-=>'
else
ls.err('invalid token')
end
else
ls.tok = '<-'
end
elseif c=='>' then
ls.getch()
ls.tok = '<>'
else
ls.tok = '<'
end
elseif c=='\n' or c==';' then
ls.getch()
ls.tok = ';'
elseif strmatch(c, '%d') then
local str = c
c = ls.getch()
if str == '0' and c == 'x' then
c = ls.getch()
while strmatch(c, '%x') do
str = str..c
c = ls.getch()
end
str = tostring(tonumber(str, 16))
elseif c and strmatch(c, '%d') then
repeat
str = str..c
c = ls.getch()
until not strmatch(c, '%d')
if c == '.' then
repeat
str = str..c
c = ls.getch()
until not strmatch(c, '%d')
end
end
ls.tok = 'number'
ls.tokval = str
elseif strmatch(c, '[_%a]') then
local str = ''
repeat
str = str..c
c = ls.getch()
until not (c and strmatch(c, '[_%w]'))
if keywords[str] then
ls.tok = str
else
ls.tok = 'name'
ls.tokval = str
end
elseif c == "'" then
local str = ''
while true do
ls.getch()
if ls.c == "'" then
ls.getch()
if ls.c == "'" then
str = str.."'"
else
break
end
elseif not ls.c then
ls.err('unfinished single-quoted string', ls.line, true)
else
str = str..ls.c
end
end
ls.tok = "'"
ls.tokval = str
elseif c == '"' then
ls.getch()
ls.tok = 'dqstr_start'
else
ls.err('invalid token')
end
end
-- if this is the next token, there's another expression following
-- this is used for deciding if an expr is a function call: once an expr has
-- been parsed, it checks if the next token is one of these, and if so, it's
-- a function call and the next expr is the first arg
local expr_start_tokens = {
['name'] = true,
['number'] = true,
['true'] = true,
['false'] = true,
['nil'] = true,
['fn'] = true,
['if'] = true,
['while'] = true,
['for'] = true,
['local'] = true,
['not'] = true,
['do'] = true,
['goto'] = true,
['return'] = true,
['repeat'] = true,
['::'] = true,
["'"] = true,
['dqstr_start'] = true,
['('] = true,
['['] = true,
['{'] = true,
['~'] = true,
['@'] = true,
}
local function expect(ls, tok)
if ls.tok ~= tok then
ls.err("expected token: "..tok, ls.line, ls.tok==nil)
end
nexttok(ls)
end
local parse_expr
local function parse_expr_list(ls)
if expr_start_tokens[ls.tok] then
local e = {'explist', parse_expr(ls)}
while ls.tok == ',' do
nexttok(ls)
while ls.tok == ';' do
nexttok(ls)
end
e[#e+1] = parse_expr(ls)
end
return e
end
return {'explist'}
end
local function parse_opt_expr_list(ls)
if expr_start_tokens[ls.tok] then
local e = parse_expr(ls)
if ls.tok == ',' then
e = {'explist', e}
while ls.tok == ',' do
nexttok(ls)
while ls.tok == ';' do
nexttok(ls)
end
e[#e+1] = parse_expr(ls)
end
end
return e
end
return {'explist'}
end
local function parse_stat_list(ls)
local e = {'seq'}
while ls.tok == ';' do nexttok(ls) end
while expr_start_tokens[ls.tok] do
e[#e+1] = parse_opt_expr_list(ls)
local count = 0
while ls.tok == ';' do
nexttok(ls)
count = count + 1
end
if count==0 then break end
end
return e
end
local function parse_opt_stat_list(ls)
while ls.tok == ';' do nexttok(ls) end
local i, e = 0, nil
while expr_start_tokens[ls.tok] do
i=i+1
if i == 1 then
e = parse_opt_expr_list(ls)
elseif i == 2 then
e = {'seq', e, parse_opt_expr_list(ls)}
else
e[#e+1] = parse_expr_list(ls)
end
local count = 0
while ls.tok == ';' do
nexttok(ls)
count = count + 1
end
if count==0 then break end
end
return e or {'seq'}
end
local parse_start_expr
local function parse_var_list(ls)
local l = ls.line
local e = {'explist'}
local has_tbl = false
while true do
if ls.tok == 'name' then
e[#e+1] = {'name', ls.tokval}
nexttok(ls)
elseif ls.tok == '{' then
e[#e+1] = parse_start_expr(ls)
has_tbl = true
else
ls.err('expected name or table deconstructor')
end
if ls.tok == ',' then
nexttok(ls)
while ls.tok == ';' do
nexttok(ls)
end
else
break
end
end
return e, has_tbl
end
local function parse_fnargs(ls)
if ls.tok == 'name' then
local e = {ls.tokval}
nexttok(ls)
while ls.tok == ',' do
nexttok(ls)
while ls.tok == ';' do
nexttok(ls)
end
e[#e+1] = ls.tokval
expect(ls, 'name')
end
return e
end
return {}
end
local function parse_block(ls)
if ls.tok == ':' then
nexttok(ls)
return parse_expr(ls)
elseif ls.tok == ';' then
local l = ls.line
nexttok(ls)
local e = parse_opt_stat_list(ls)
if ls.tok ~= 'end' then
ls.err('expected "end" to match block on line '..l, ls.line, ls.tok==nil)
end
nexttok(ls)
return e
end
ls.err('expected ":", ";", or newline')
end
local function tbl_node_iter(node)
local i, nextarrkey = 2, 1
return function ()
local k, v = node[i], node[i+1]
if not v then return end
i = i + 2
if not k then
k = {'number', tostring(nextarrkey)}
nextarrkey = nextarrkey + 1
end
return k, v
end
end
local function convert_table_deconstruction_2(ls, tblnode, tbltmp, islocal)
local new_node = {'seq'}
local tmp_assign_node = nil
for k, v in tbl_node_iter(tblnode) do
if v[1] == 'table' then
if not tmp_assign_node then
tmp_assign_node = {'local', {'explist'}, {'explist'}}
new_node[2] = tmp_assign_node
end
local tbltmp2 = {'name', ls.new_tmp_var()}
tinsert(tmp_assign_node[2], tbltmp2)
tinsert(tmp_assign_node[3], {'gettable', tbltmp, k})
new_node[#new_node+1] = convert_table_deconstruction_2(ls, v, tbltmp2, islocal)
end
end
local var_assign_node = nil
for k, v in tbl_node_iter(tblnode) do
if v[1] ~= 'table' then
if not islocal and v[1]~='name' and v[1]~='gettable' then
ls.err('table deconstructor values must be assignable')
end
if not var_assign_node then
var_assign_node = {islocal and 'local' or 'assign', {'explist'}, {'explist'}}
new_node[#new_node+1] = var_assign_node
end
tinsert(var_assign_node[2], v)
tinsert(var_assign_node[3], {'gettable', tbltmp, k})
end
end
return new_node
end
local function convert_table_deconstruction(ls, node)
local new_node = {'seq'}
local islocal = node[1] == 'local'
if node[2][1] ~= 'explist' then node[2] = {'explist', node[2]} end
local tmp_assign_node = {'local', {'explist'}, node[3]}
new_node[2] = tmp_assign_node
for i = 2, #node[2] do
if node[2][i][1]=='table' then
local tbltmp = {'name', ls.new_tmp_var()}
tinsert(tmp_assign_node[2], tbltmp)
tinsert(new_node, convert_table_deconstruction_2(ls, node[2][i], tbltmp, islocal))
elseif not islocal then
if node[2][i][1]~='name' and node[2][i][1]~='gettable' then
ls.err('table deconstructor values must be assignable')
end
local tmp = {'name', ls.new_tmp_var()}
tinsert(tmp_assign_node[2], tbltmp)
tinsert(new_node, {'assign', node[2][i], tmp})
else
tinsert(tmp_assign_node[2], node[2][i])
end
end
return new_node
end
parse_start_expr = function(ls)
local t = ls.tok
if t == "nil" then
local l = ls.line
nexttok(ls)
return {line=l, "nil"}
elseif t == "name" then
local l, n = ls.line, ls.tokval
nexttok(ls)
return {line=l, "name", n}
elseif t == "(" then
nexttok(ls)
local e = parse_opt_stat_list(ls)
if ls.tok ~= ')' then
ls.err('expected ")" to match "(" in line '..l, ls.line, ls.tok==nil)
end
nexttok(ls)
if e[1]=='seq' and not e[2] then
return {'nil'}
end
return e
elseif t == "true" then
local l = ls.line
nexttok(ls)
return {line=l, "true"}
elseif t == "false" then
local l = ls.line
nexttok(ls)
return {line=l, "false"}
elseif t == "number" then
local l = ls.line
nexttok(ls)
return {line=l, "number", ls.tokval}
elseif t == "break" then
local l = ls.line
nexttok(ls)
return {line=l, "break"}
elseif t == "local" then
local ln = ls.line
nexttok(ls)
local l, has_tbl = parse_var_list(ls)
if ls.tok == '=' then
nexttok(ls)
local e = {line=ln, 'local', l, parse_opt_expr_list(ls)}
if has_tbl then
e = convert_table_deconstruction(ls, e)
end
return e
else
if has_tbl then
ls.err('local variable declaration with a table deconstructor cannot omit the assignment')
end
return {line=ln, 'local', l}
end
elseif t == "::" then
nexttok(ls)
local l = ls.line
local name = ls.tokval
expect(ls, 'name')
expect(ls, '::')
return {line=l, "label", name}
elseif t == "goto" then
local l = ls.line
nexttok(ls)
local name = ls.tokval
expect(ls, "name")
return {line=l, "goto", name}
elseif t == "do" then
local l = ls.line
nexttok(ls)
local block = parse_opt_stat_list(ls)
expect(ls, "end")
return {line=l, "do", block}
elseif t == "while" then
local l = ls.line
nexttok(ls)
local cond = parse_expr(ls)
local body = parse_block(ls)
return {line=l, "while", cond, body}
elseif t == "repeat" then
local l = ls.line
nexttok(ls)
local block = parse_opt_stat_list(ls)
if ls.tok ~= 'until' then
ls.err('expected "until" to match "repeat" on line '..l, ls.line, ls.tok==nil)
end
nexttok(ls)
local cond = parse_exp(ls)
return {line=l, "repeat", block, cond}
elseif t == "if" then
local l = ls.line
nexttok(ls)
local cond = parse_expr(ls)
if ls.tok == ':' then
nexttok(ls)
local true_branch = parse_expr(ls)
local node = {line=l, "if", cond, true_branch}
if ls.tok == "else" then
nexttok(ls)
node[4] = parse_expr(ls)
end
return node
else
expect(ls, ';')
local true_branch = parse_opt_stat_list(ls)
local node = {line=l, "if", cond, true_branch}
while ls.tok == "elseif" do
nexttok(ls)
node[#node+1] = parse_expr(ls)
expect(ls, ';')
node[#node+1] = parse_opt_stat_list(ls)
end
if ls.tok == "else" then
nexttok(ls)
node[#node+1] = parse_opt_stat_list(ls)
end
if ls.tok ~= 'end' then
ls.err('expected "end" to match "if" on line '..l, ls.line, ls.tok==nil)
end
nexttok(ls)
return node
end
elseif t == "for" then
local l = ls.line
nexttok(ls)
if ls.tok == ":" or ls.tok == ';' then
local body = parse_block(ls)
return {line=l, 'repeat', body, {'false'}}
else
local itervars = parse_var_list(ls)
if ls.tok == "in" then
nexttok(ls)
local iterexp = parse_opt_expr_list(ls)
local block = parse_block(ls)
return {line=l, "for_iter", itervars, iterexp, block}
else
if itervars[3] or itervars[2][1] ~= 'name' then
ls.err("numeric for loop can only have one variable")
end
expect(ls, "=")
local istart = parse_expr(ls);
expect(ls, ",")
local iend = parse_expr(ls);
local istep = false
if ls.tok == "," then
nexttok(ls)
istep = parse_expr(ls);
end
local block = parse_block(ls)
return {line=l, "for_num", itervars[2][2], istart, iend, istep, block}
end
end
elseif t == "fn" then
local l = ls.line
nexttok(ls)
local args = parse_fnargs(ls)
local ret = ls.tok == ':'
local body = parse_block(ls)
if ret then
return {line=l, "function", args, {'return', body}}
end
return {line=l, "function", args, body}
elseif t == "@" then
local l = ls.line
nexttok(ls)
local name = ls.tokval
expect(ls, "name")
if ls.tok == '!' then
nexttok(ls)
return {line=l, 'method_call', {"name", "self"}, name, {'explist'}}
elseif expr_start_tokens[ls.tok] then
return {line=l, 'method_call', {"name", "self"}, name, parse_expr_list(ls)}
else
return {line=l, "gettable", {"name", "self"}, {"string", name}}
end
elseif t == "{" then
local l = ls.line
nexttok(ls)
while ls.tok==';' or ls.tok==',' do
nexttok(ls)
end
local node = {line=l, "table"}
local first = true
while true do
if first then
first = false
else
local count = 0
while ls.tok==';' or ls.tok==',' do
count = count + 1
nexttok(ls)
end
if count == 0 and ls.tok ~= '}' then
ls.err('expected "}"')
end
end
if ls.tok == "}" then
nexttok(ls)
break
--[[
elseif ls.tok == "[" then
nexttok(ls)
local key = parse_expr(ls)
expect(ls, "]")
expect(ls, ":")
local val = parse_expr(ls)
node.hash[key] = val
]]
else
local key = parse_expr(ls)
if key[1] == "name" and ls.tok == ':' then
nexttok(ls)
local val = parse_expr(ls)
key[1] = "string"
node[#node+1] = key
node[#node+1] = val
elseif ls.tok == '->' then
nexttok(ls)
local val = parse_expr(ls)
node[#node+1] = key
node[#node+1] = val
else
node[#node+1] = false
node[#node+1] = key
end
end
local count = 0
end
return node
elseif t == "'" then
local l = ls.line
local str = ls.tokval
nexttok(ls)
return {line=l, "string", str}
elseif t == 'dqstr_start' then
local l = ls.line
ls.in_dq_str = true
nexttok(ls)
local node = nil
while true do
local newnode
if ls.tok == "dqstr_text" then
newnode = {line=l, "string", ls.tokval}
nexttok(ls)
elseif ls.tok == "dqstr_expr" then
ls.in_dq_str = false
nexttok(ls)
if ls.tok == ')' then
newnode = {line=l, 'nil'}
else
newnode = parse_expr(ls)
if ls.tok ~= ')' then
ls.err('expected matching ) for expression in double-quoted string')
end
end
ls.in_dq_str = true
nexttok(ls)
elseif ls.tok == "dqstr_var" then
newnode = {line=l, 'name', ls.tokval}
nexttok(ls)
elseif ls.tok == 'dqstr_end' then
ls.in_dq_str = false
nexttok(ls)
break
else
ls.err("unexpected token in double-quoted string")
end
if node then
node = {"binop", "..", node, newnode}
else
node = newnode
end
end
return node or {line=l, "string", ""}
elseif t == "[" then
local l = ls.line
nexttok(ls)
local quotetype
if ls.tok == ">" then
quotetype = "quote"
elseif ls.tok == "<" then
quotetype = "dequote"
else
ls.err("expected < or > after [")
end
nexttok(ls)
local node = {line=l, quotetype, parse_opt_stat_list(ls)}
if quotetype == 'dequote' then
node[3] = {'return', node[3]}
end
if ls.tok ~= ']' then
ls.err('expected "]" to match "[" in line '..l, ls.line, ls.tok==nil)
end
nexttok(ls)
return node
elseif t == "return" then
nexttok(ls)
return {'return', parse_opt_expr_list(ls)}
end
ls.err("expected expression", ls.line, ls.tok==nil)
end
local cmp_op_tokens = {
['=='] = true, ['!='] = true,
['>'] = true, ['<'] = true,
['>='] = true, ['<='] = true,
}
local function parse_atom_expr(ls)
if ls.tok == '~' then
nexttok(ls)
return {'unop', '-', parse_atom_expr(ls)}
end
local e = parse_start_expr(ls)
while true do
if ls.tok == "!" then
nexttok(ls)
e = {line=e.line, "call", e, {'explist'}}
elseif ls.tok == "." then
nexttok(ls)
if ls.tok == '?' then
nexttok(ls)
local name = ls.tokval
expect(ls, "name")
local tmpname = {'name', ls.new_tmp_var()}
e = {line=e.line, "if", {'binop', '~=', {'local', tmpname, e}, {'nil'}},
{"gettable", tmpname, {'string', name}}}
else
local name = ls.tokval
expect(ls, "name")
e = {line=e.line, "gettable", e, {'string', name}}
end
elseif ls.tok == "\\" then
nexttok(ls)
local name = ls.tokval
expect(ls, "name")
if ls.tok == '!' then
nexttok(ls)
e = {line=e.line, "method_call", e, name, {'explist'}}
elseif expr_start_tokens[ls.tok] then
e = {line=e.line, "method_call", e, name, parse_expr_list(ls)}
else
local tmpname = {'name', ls.new_tmp_var()}
e = {line=e.line, 'do', {'seq',
{'local', tmpname, e},
{'function', {'...'}, {'return',
{'method_call', tmpname, name, {'vararg'}},
}},
}}
end
elseif ls.tok == "[" then
nexttok(ls)
if ls.tok == '?' then
nexttok(ls)
local key = parse_expr(ls)
expect(ls, "]")
local tmpname = {'name', ls.new_tmp_var()}
e = {line=e.line, "if", {'binop', '~=', {'local', tmpname, e}, {'nil'}},
{"gettable", tmpname, key}}
else
local key = parse_expr(ls)
expect(ls, "]")
e = {line=e.line, "gettable", e, key}
end
elseif ls.tok == "in" then
local l = ls.line
nexttok(ls)
if not ls.tok == '(' then
ls.err('expected "(" after "in"')
end
nexttok(ls)
local cmp
local tmpname = {'name', ls.new_tmp_var()}
while true do
local choice = parse_expr(ls)
if ls.tok == '<>' then
nexttok(ls)
choice = {'binop', 'and',
{'binop', '>', tmpname, choice},
{'binop', '<', tmpname, parse_expr(ls)},
}
elseif ls.tok == '<=>' then
nexttok(ls)
choice = {'binop', 'and',
{'binop', '>=', tmpname, choice},
{'binop', '<=', tmpname, parse_expr(ls)},
}
elseif ls.tok == '<=->' then
nexttok(ls)
choice = {'binop', 'and',
{'binop', '>=', tmpname, choice},
{'binop', '<', tmpname, parse_expr(ls)},
}
elseif ls.tok == '<-=>' then
nexttok(ls)
choice = {'binop', 'and',
{'binop', '>', tmpname, choice},
{'binop', '<=', tmpname, parse_expr(ls)},
}
else
choice = {'binop', '==', tmpname, choice}
end
cmp = cmp and {'binop', 'or', cmp, choice} or choice
if ls.tok == ',' then
nexttok(ls)
while ls.tok == ';' do nexttok(ls) end
else
break
end
end
expect(ls, ')')
e = {line=l, 'do', {'seq', {'local', tmpname, e}, cmp}}
elseif expr_start_tokens[ls.tok] then
e = {line=e.line, "call", e, parse_expr_list(ls)}