forked from viegg69/LuaJGG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.lua
More file actions
1264 lines (1263 loc) · 30.8 KB
/
parser.lua
File metadata and controls
1264 lines (1263 loc) · 30.8 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 luac = require("luac");
local lex = require("lex");
local opcodes = require("opcodes");
local code = require("code");
local vm = require("vm");
local parser = {};
parser.vm = vm;
local LuaState = {};
parser.LUA_QS = lex.LUA_QS or "\'%s\'";
parser.SHRT_MAX = 32767;
parser.LUAI_MAXVARS = 200;
parser.LUAI_MAXUPVALUES = 60;
parser.MAX_INT = lex.MAX_INT or 2147483645;
parser.LUAI_MAXCCALLS = 200;
parser.VARARG_HASARG = 1;
parser.HASARG_MASK = 2;
parser.VARARG_ISVARARG = 2;
parser.VARARG_NEEDSARG = 4;
parser.LUA_MULTRET = -1;
function parser.LUA_QL(self, x)
return "\'" .. (x .. "\'");
end;
function parser.growvector(self, L, v, nelems, size, t, limit, e)
if nelems >= limit then
error(e);
end;
end;
function parser.newproto(self, L)
local f = {};
f.k = {};
f.sizek = 0;
f.p = {};
f.sizep = 0;
f.code = {};
f.sizecode = 0;
f.sizelineinfo = 0;
f.sizeupvalues = 0;
f.nups = 0;
f.upvalues = {};
f.numparams = 0;
f.is_vararg = 0;
f.maxstacksize = 0;
f.lineinfo = {};
f.sizelocvars = 0;
f.locvars = {};
f.lineDefined = 0;
f.lastlinedefined = 0;
f.source = nil;
return f;
end;
function parser.int2fb(self, x)
local e = 0;
while x >= 16 do
x = math.floor((x + 1) / 2);
e = e + 1;
end;
if x < 8 then
return x;
else
return (e + 1) * 8 + (x - 8);
end;
end;
function parser.hasmultret(self, k)
return k == "VCALL" or k == "VVARARG";
end;
function parser.getlocvar(self, fs, i)
return fs.f.locvars[fs.actvar[i]];
end;
function parser.checklimit(self, fs, v, l, m)
if v > l then
self:errorlimit(fs, l, m);
end;
end;
function parser.anchor_token(self, ls)
if ls.t.token == "TK_NAME" or ls.t.token == "TK_STRING" then
end;
end;
function parser.error_expected(self, ls, token)
lex:syntaxerror(ls, string.format(self.LUA_QS .. " expected", lex:token2str(ls, token)));
end;
function parser.errorlimit(self, fs, limit, what)
local msg = fs.f.linedefined == 0 and string.format("main function has more than %d %s", limit, what) or string.format("function at line %d has more than %d %s", fs.f.linedefined, limit, what);
lex:lexerror(fs.ls, msg, 0);
end;
function parser.testnext(self, ls, c)
if ls.t.token == c then
lex:next(ls);
return true;
else
return false;
end;
end;
function parser.check(self, ls, c)
if ls.t.token ~= c then
self:error_expected(ls, c);
end;
end;
function parser.checknext(self, ls, c)
self:check(ls, c);
lex:next(ls);
end;
function parser.check_condition(self, ls, c, msg)
if not c then
lex:syntaxerror(ls, msg);
end;
end;
function parser.check_match(self, ls, what, who, where)
if not self:testnext(ls, what) then
if where == ls.linenumber then
self:error_expected(ls, what);
else
lex:syntaxerror(ls, string.format(self.LUA_QS .. (" expected (to close " .. (self.LUA_QS .. " at line %d)")), lex:token2str(ls, what), lex:token2str(ls, who), where));
end;
end;
end;
function parser.str_checkname(self, ls)
self:check(ls, "TK_NAME");
local ts = ls.t.seminfo;
lex:next(ls);
return ts;
end;
function parser.init_exp(self, e, k, i)
e.f, e.t = code.NO_JUMP, code.NO_JUMP;
e.k = k;
e.info = i;
end;
function parser.codestring(self, ls, e, s)
self:init_exp(e, "VK", code:stringK(ls.fs, s));
end;
function parser.checkname(self, ls, e)
self:codestring(ls, e, self:str_checkname(ls));
end;
function parser.registerlocalvar(self, ls, varname)
local fs = ls.fs;
local f = fs.f;
self:growvector(ls.L, f.locvars, fs.nlocvars, f.sizelocvars, nil, self.SHRT_MAX, "too many local variables");
f.locvars[fs.nlocvars] = {};
f.locvars[fs.nlocvars].varname = varname;
local nlocvars = fs.nlocvars;
fs.nlocvars = fs.nlocvars + 1;
return nlocvars;
end;
function parser.new_localvarliteral(self, ls, v, n)
self:new_localvar(ls, v, n);
end;
function parser.new_localvar(self, ls, name, n)
local fs = ls.fs;
self:checklimit(fs, (fs.nactvar + n) + 1, self.LUAI_MAXVARS, "local variables");
fs.actvar[fs.nactvar + n] = self:registerlocalvar(ls, name);
end;
function parser.adjustlocalvars(self, ls, nvars)
local fs = ls.fs;
fs.nactvar = fs.nactvar + nvars;
for i = nvars, 1, -1 do
(self:getlocvar(fs, fs.nactvar - i)).startpc = fs.pc;
end;
end;
function parser.removevars(self, ls, tolevel)
local fs = ls.fs;
while fs.nactvar > tolevel do
fs.nactvar = fs.nactvar - 1;
(self:getlocvar(fs, fs.nactvar)).endpc = fs.pc;
end;
end;
function parser.searchupvalue(self, fs, name)
for i = 0, fs.f.nups - 1, 1 do
if fs.f.upvalues[i] == name then
return i;
end;
end;
return -1;
end;
function parser.indexupvalue(self, fs, name, v)
local f = fs.f;
for i = 0, f.nups - 1, 1 do
if fs.upvalues[i].k == v.k and fs.upvalues[i].info == v.info then
assert(f.upvalues[i] == name);
return i;
end;
end;
self:checklimit(fs, f.nups + 1, self.LUAI_MAXUPVALUES, "upvalues");
self:growvector(fs.L, f.upvalues, f.nups, f.sizeupvalues, nil, self.MAX_INT, "");
f.upvalues[f.nups] = name;
assert(v.k == "VLOCAL" or v.k == "VUPVAL");
fs.upvalues[f.nups] = { k = v.k, info = v.info };
local nups = f.nups;
f.nups = f.nups + 1;
return nups;
end;
function parser.searchvar(self, fs, n)
for i = fs.nactvar - 1, 0, -1 do
if n == (self:getlocvar(fs, i)).varname then
return i;
end;
end;
return -1;
end;
function parser.markupval(self, fs, level)
local bl = fs.bl;
while bl and bl.nactvar > level do
bl = bl.previous;
end;
if bl then
bl.upval = true;
end;
end;
function parser.singlevaraux(self, fs, n, var, base)
if fs == nil then
return "VVOID";
else
local v = self:searchvar(fs, n);
if v >= 0 then
self:init_exp(var, "VLOCAL", v);
if base == 0 then
self:markupval(fs, v);
end;
return "VLOCAL";
else
local idx = self:searchupvalue(fs, n);
if idx < 0 then
if self:singlevaraux(fs.prev, n, var, 0) == "VVOID" then
return "VVOID";
end;
idx = self:indexupvalue(fs, n, var);
end;
self:init_exp(var, "VUPVAL", idx);
return "VUPVAL";
end;
end;
end;
function parser.singlevar(self, ls, var)
local varname = self:str_checkname(ls);
local fs = ls.fs;
if self:singlevaraux(fs, varname, var, 1) == "VVOID" then
self:singlevaraux(fs, "_ENV", var, 1);
assert(var.k == "VLOCAL" or var.k == "VUPVAL");
local key = {};
self:codestring(ls, key, varname);
code:indexed(fs, var, key);
end;
end;
function parser.adjust_assign(self, ls, nvars, nexps, e)
local fs = ls.fs;
local extra = nvars - nexps;
if self:hasmultret(e.k) then
extra = extra + 1;
if extra <= 0 then
extra = 0;
end;
code:setreturns(fs, e, extra);
if extra > 1 then
code:reserveregs(fs, extra - 1);
end;
else
if e.k ~= "VVOID" then
code:exp2nextreg(fs, e);
end;
if extra > 0 then
local reg = fs.freereg;
code:reserveregs(fs, extra);
code:_nil(fs, reg, extra);
end;
end;
end;
function parser.enterlevel(self, ls)
ls.L.nCcalls = ls.L.nCcalls + 1;
if ls.L.nCcalls > self.LUAI_MAXCCALLS then
lex:lexerror(ls, "chunk has too many syntax levels", 0);
end;
end;
function parser.leavelevel(self, ls)
ls.L.nCcalls = ls.L.nCcalls - 1;
end;
function parser.enterblock(self, fs, bl, isbreakable)
bl.breaklist = code.NO_JUMP;
bl.isbreakable = isbreakable;
bl.nactvar = fs.nactvar;
bl.upval = false;
bl.previous = fs.bl;
fs.bl = bl;
assert(fs.freereg == fs.nactvar);
end;
function parser.leaveblock(self, fs)
local bl = fs.bl;
fs.bl = bl.previous;
self:removevars(fs.ls, bl.nactvar);
if bl.upval then
local j = code:jump(fs);
code:patchclose(fs, j, bl.nactvar);
code:patchtohere(fs, j);
end;
assert(not bl.isbreakable or not bl.upval);
assert(bl.nactvar == fs.nactvar);
fs.freereg = fs.nactvar;
code:patchtohere(fs, bl.breaklist);
end;
function parser.pushclosure(self, ls, func, v)
local fs = ls.fs;
local f = fs.f;
self:growvector(ls.L, f.p, fs.np, f.sizep, nil, opcodes.MAXARG_Bx, "constant table overflow");
f.p[fs.np] = func.f;
fs.np = fs.np + 1;
self:init_exp(v, "VRELOCABLE", code:codeABx(fs, "OP_CLOSURE", 0, fs.np - 1));
for i = 0, func.f.nups - 1, 1 do
local o = func.upvalues[i].k == "VLOCAL" and "OP_MOVE" or "OP_GETUPVAL";
code:codeABC(fs, o, 0, func.upvalues[i].info, 0);
end;
end;
function parser.open_func(self, ls, fs)
local L = ls.L;
local f = self:newproto(ls.L);
fs.f = f;
fs.prev = ls.fs;
fs.ls = ls;
fs.L = L;
ls.fs = fs;
fs.pc = 0;
fs.lasttarget = -1;
fs.jpc = code.NO_JUMP;
fs.freereg = 0;
fs.nk = 0;
fs.np = 0;
fs.nlocvars = 0;
fs.nactvar = 0;
fs.bl = nil;
f.source = ls.source;
f.maxstacksize = 2;
fs.h = {};
end;
function parser.base_close_func(self, ls)
local L = ls.L;
local fs = ls.fs;
local f = fs.f;
self:removevars(ls, 0);
code:ret(fs, 0, 0);
f.sizecode = fs.pc;
f.sizelineinfo = fs.pc;
f.sizek = fs.nk;
f.sizep = fs.np;
f.sizelocvars = fs.nlocvars;
f.sizeupvalues = f.nups;
assert(fs.bl == nil);
ls.fs = fs.prev;
if fs then
self:anchor_token(ls);
end;
end;
local labels = {};
local gotos = {};
function parser.close_func(self, ls)
self:base_close_func(ls);
for label, goto_list in pairs(gotos) do
for _, goto_pc in ipairs(goto_list) do
lex:syntaxerror(ls, "no visible label \'" .. (label .. "\' for goto"));
end;
end;
end;
function parser.parser(self, L, z, buff, name)
local lexstate = {};
lexstate.t = {};
lexstate.lookahead = {};
local funcstate = {};
funcstate.upvalues = {};
funcstate.actvar = {};
L.nCcalls = 0;
lexstate.buff = buff;
lex:setinput(L, lexstate, z, name);
self:open_func(lexstate, funcstate);
funcstate.f.is_vararg = 1;
local v = {};
self:init_exp(v, "VLOCAL", 0);
self:indexupvalue(funcstate, "_ENV", v);
lex:next(lexstate);
self:chunk(lexstate);
self:check(lexstate, "TK_EOS");
self:close_func(lexstate);
assert(funcstate.prev == nil);
assert(lexstate.fs == nil);
return funcstate.f;
end;
function parser.field(self, ls, v)
local fs = ls.fs;
local key = {};
code:exp2anyreg(fs, v);
lex:next(ls);
self:checkname(ls, key);
code:indexed(fs, v, key);
end;
function parser.yindex(self, ls, v)
lex:next(ls);
self:expr(ls, v);
code:exp2val(ls.fs, v);
self:checknext(ls, "]");
end;
function parser.recfield(self, ls, cc)
local fs = ls.fs;
local reg = ls.fs.freereg;
local key, val = {}, {};
if ls.t.token == "TK_NAME" then
self:checklimit(fs, cc.nh, self.MAX_INT, "items in a constructor");
self:checkname(ls, key);
else
self:yindex(ls, key);
end;
cc.nh = cc.nh + 1;
self:checknext(ls, "=");
local rkkey = code:exp2RK(fs, key);
self:expr(ls, val);
code:codeABC(fs, "OP_SETTABLE", cc.t.info, rkkey, code:exp2RK(fs, val));
fs.freereg = reg;
end;
function parser.closelistfield(self, fs, cc)
if cc.v.k == "VVOID" then
return;
end;
code:exp2nextreg(fs, cc.v);
cc.v.k = "VVOID";
if cc.tostore == opcodes.LFIELDS_PER_FLUSH then
code:setlist(fs, cc.t.info, cc.na, cc.tostore);
cc.tostore = 0;
end;
end;
function parser.lastlistfield(self, fs, cc)
if cc.tostore == 0 then
return;
end;
if self:hasmultret(cc.v.k) then
code:setmultret(fs, cc.v);
code:setlist(fs, cc.t.info, cc.na, self.LUA_MULTRET);
cc.na = cc.na - 1;
else
if cc.v.k ~= "VVOID" then
code:exp2nextreg(fs, cc.v);
end;
code:setlist(fs, cc.t.info, cc.na, cc.tostore);
end;
end;
function parser.listfield(self, ls, cc)
self:expr(ls, cc.v);
self:checklimit(ls.fs, cc.na, self.MAX_INT, "items in a constructor");
cc.na = cc.na + 1;
cc.tostore = cc.tostore + 1;
end;
function parser.constructor(self, ls, t)
local fs = ls.fs;
local line = ls.linenumber;
local pc = code:codeABC(fs, "OP_NEWTABLE", 0, 0, 0);
local cc = {};
cc.v = {};
cc.na, cc.nh, cc.tostore = 0, 0, 0;
cc.t = t;
self:init_exp(t, "VRELOCABLE", pc);
self:init_exp(cc.v, "VVOID", 0);
code:exp2nextreg(ls.fs, t);
self:checknext(ls, "{");
repeat
assert(cc.v.k == "VVOID" or cc.tostore > 0);
if ls.t.token == "}" then
break;
end;
self:closelistfield(fs, cc);
local c = ls.t.token;
if c == "TK_NAME" then
lex:lookahead(ls);
if ls.lookahead.token ~= "=" then
self:listfield(ls, cc);
else
self:recfield(ls, cc);
end;
elseif c == "[" then
self:recfield(ls, cc);
else
self:listfield(ls, cc);
end;
until not self:testnext(ls, ",") and not self:testnext(ls, ";");
self:check_match(ls, "}", "{", line);
self:lastlistfield(fs, cc);
opcodes:SETARG_B(fs.f.code[pc], self:int2fb(cc.na));
opcodes:SETARG_C(fs.f.code[pc], self:int2fb(cc.nh));
end;
function parser.parlist(self, ls)
local fs = ls.fs;
local f = fs.f;
local nparams = 0;
f.is_vararg = 0;
if ls.t.token ~= ")" then
repeat
local c = ls.t.token;
if c == "TK_NAME" then
self:new_localvar(ls, self:str_checkname(ls), nparams);
nparams = nparams + 1;
elseif c == "TK_DOTS" then
lex:next(ls);
self:new_localvarliteral(ls, "arg", nparams);
nparams = nparams + 1;
f.is_vararg = self.VARARG_HASARG + self.VARARG_NEEDSARG;
f.is_vararg = f.is_vararg + self.VARARG_ISVARARG;
else
lex:syntaxerror(ls, "<name> or " .. (self:LUA_QL("...") .. " expected"));
end;
until f.is_vararg ~= 0 or not self:testnext(ls, ",");
end;
self:adjustlocalvars(ls, nparams);
f.numparams = fs.nactvar - f.is_vararg % self.HASARG_MASK;
code:reserveregs(fs, fs.nactvar);
end;
function parser.body(self, ls, e, needself, line)
local new_fs = {};
new_fs.upvalues = {};
new_fs.actvar = {};
self:open_func(ls, new_fs);
new_fs.f.lineDefined = line;
self:checknext(ls, "(");
if needself then
self:new_localvarliteral(ls, "self", 0);
self:adjustlocalvars(ls, 1);
end;
self:parlist(ls);
self:checknext(ls, ")");
self:chunk(ls);
new_fs.f.lastlinedefined = ls.linenumber;
self:check_match(ls, "TK_END", "TK_FUNCTION", line);
self:close_func(ls);
self:pushclosure(ls, new_fs, e);
end;
function parser.explist1(self, ls, v)
local n = 1;
self:expr(ls, v);
while self:testnext(ls, ",") do
code:exp2nextreg(ls.fs, v);
self:expr(ls, v);
n = n + 1;
end;
return n;
end;
function parser.funcargs(self, ls, f)
local fs = ls.fs;
local args = {};
local nparams;
local line = ls.linenumber;
local c = ls.t.token;
if c == "(" then
if line ~= ls.lastline then
lex:syntaxerror(ls, "ambiguous syntax (function call x new statement)");
end;
lex:next(ls);
if ls.t.token == ")" then
args.k = "VVOID";
else
self:explist1(ls, args);
code:setmultret(fs, args);
end;
self:check_match(ls, ")", "(", line);
elseif c == "{" then
self:constructor(ls, args);
elseif c == "TK_STRING" then
self:codestring(ls, args, ls.t.seminfo);
lex:next(ls);
else
lex:syntaxerror(ls, "function arguments expected");
return;
end;
assert(f.k == "VNONRELOC");
local base = f.info;
if self:hasmultret(args.k) then
nparams = self.LUA_MULTRET;
else
if args.k ~= "VVOID" then
code:exp2nextreg(fs, args);
end;
nparams = fs.freereg - (base + 1);
end;
self:init_exp(f, "VCALL", code:codeABC(fs, "OP_CALL", base, nparams + 1, 2));
code:fixline(fs, line);
fs.freereg = base + 1;
end;
function parser.prefixexp(self, ls, v)
local c = ls.t.token;
if c == "(" then
local line = ls.linenumber;
lex:next(ls);
self:expr(ls, v);
self:check_match(ls, ")", "(", line);
code:dischargevars(ls.fs, v);
elseif c == "TK_NAME" then
self:singlevar(ls, v);
if v.k == "VUPVAL" then
local key = {};
end;
else
lex:syntaxerror(ls, "unexpected symbol");
end;
return;
end;
function parser.primaryexp(self, ls, v)
local fs = ls.fs;
self:prefixexp(ls, v);
while true do
local c = ls.t.token;
if c == "." then
self:field(ls, v);
elseif c == "[" then
local key = {};
code:exp2anyregup(fs, v);
self:yindex(ls, key);
code:indexed(fs, v, key);
elseif c == ":" then
local key = {};
lex:next(ls);
self:checkname(ls, key);
code:_self(fs, v, key);
self:funcargs(ls, v);
elseif c == "(" or c == "TK_STRING" or c == "{" then
code:exp2nextreg(fs, v);
self:funcargs(ls, v);
else
return;
end;
end;
end;
function parser.simpleexp(self, ls, v)
local c = ls.t.token;
if c == "TK_NUMBER" then
self:init_exp(v, "VKNUM", 0);
v.nval = ls.t.seminfo;
elseif c == "TK_STRING" then
self:codestring(ls, v, ls.t.seminfo);
elseif c == "TK_NIL" then
self:init_exp(v, "VNIL", 0);
elseif c == "TK_TRUE" then
self:init_exp(v, "VTRUE", 0);
elseif c == "TK_FALSE" then
self:init_exp(v, "VFALSE", 0);
elseif c == "TK_DOTS" then
local fs = ls.fs;
self:check_condition(ls, fs.f.is_vararg ~= 0, "cannot use " .. (self:LUA_QL("...") .. " outside a vararg function"));
local is_vararg = fs.f.is_vararg;
if is_vararg >= self.VARARG_NEEDSARG then
fs.f.is_vararg = is_vararg - self.VARARG_NEEDSARG;
end;
self:init_exp(v, "VVARARG", code:codeABC(fs, "OP_VARARG", 0, 1, 0));
elseif c == "{" then
self:constructor(ls, v);
return;
elseif c == "TK_FUNCTION" then
lex:next(ls);
self:body(ls, v, false, ls.linenumber);
return;
else
self:primaryexp(ls, v);
return;
end;
lex:next(ls);
end;
function parser.getunopr(self, op)
if op == "TK_NOT" then
return "OPR_NOT";
elseif op == "-" then
return "OPR_MINUS";
elseif op == "#" then
return "OPR_LEN";
elseif op == "~" then
return "OPR_BNOT";
else
return "OPR_NOUNOPR";
end;
end;
parser.getbinopr_table = {
["+"] = "OPR_ADD",
["-"] = "OPR_SUB",
["*"] = "OPR_MUL",
["/"] = "OPR_DIV",
["%"] = "OPR_MOD",
["^"] = "OPR_POW",
TK_IDIV = "OPR_IDIV",
["&"] = "OPR_BAND",
["|"] = "OPR_BOR",
["~"] = "OPR_BXOR",
TK_SHR = "OPR_SHR",
TK_SHL = "OPR_SHL",
TK_CONCAT = "OPR_CONCAT",
TK_NE = "OPR_NE",
TK_EQ = "OPR_EQ",
["<"] = "OPR_LT",
TK_LE = "OPR_LE",
[">"] = "OPR_GT",
TK_GE = "OPR_GE",
TK_AND = "OPR_AND",
TK_OR = "OPR_OR",
};
function parser.getbinopr(self, op)
local opr = self.getbinopr_table[op];
if opr then
return opr;
else
return "OPR_NOBINOPR";
end;
end;
parser.priority = {
{ 6, 6 },
{ 6, 6 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 7, 7 },
{ 10, 9 },
{ 5, 4 },
{ 3, 3 },
{ 3, 3 },
{ 3, 3 },
{ 3, 3 },
{ 3, 3 },
{ 3, 3 },
{ 2, 2 },
{ 1, 1 },
};
parser.UNARY_PRIORITY = 8;
function parser.subexpr(self, ls, v, limit)
self:enterlevel(ls);
local uop = self:getunopr(ls.t.token);
if uop ~= "OPR_NOUNOPR" then
lex:next(ls);
self:subexpr(ls, v, self.UNARY_PRIORITY);
code:prefix(ls.fs, uop, v);
else
self:simpleexp(ls, v);
end;
local op = self:getbinopr(ls.t.token);
while op ~= "OPR_NOBINOPR" and self.priority[code.BinOpr[op] + 1][1] > limit do
local v2 = {};
lex:next(ls);
code:infix(ls.fs, op, v);
local nextop = self:subexpr(ls, v2, self.priority[code.BinOpr[op] + 1][2]);
code:posfix(ls.fs, op, v, v2);
op = nextop;
end;
self:leavelevel(ls);
return op;
end;
function parser.expr(self, ls, v)
self:subexpr(ls, v, 0);
end;
function parser.block_follow(self, token)
if token == "TK_ELSE" or token == "TK_ELSEIF" or token == "TK_END" or token == "TK_UNTIL" or token == "TK_EOS" then
return true;
else
return false;
end;
end;
function parser.block(self, ls)
local fs = ls.fs;
local bl = {};
self:enterblock(fs, bl, false);
self:chunk(ls);
assert(bl.breaklist == code.NO_JUMP);
self:leaveblock(fs);
end;
function parser.check_conflict(self, ls, lh, v)
local fs = ls.fs;
local extra = fs.freereg;
local conflict = false;
while lh do
if lh.v.k == "VINDEXED" then
if lh.v.info == v.info then
conflict = true;
lh.v.ind_vt = "VLOCAL";
lh.v.info = extra;
end;
if lh.v.aux == v.info then
conflict = true;
lh.v.aux = extra;
end;
end;
lh = lh.prev;
end;
if conflict then
local op = v.k == "VLOCAL" and "OP_MOVE" or "OP_GETUPVAL";
print("parser:check_conflict:", op);
code:codeABC(fs, "OP_MOVE", fs.freereg, v.info, 0);
code:reserveregs(fs, 1);
end;
end;
function parser.assignment(self, ls, lh, nvars)
local e = {};
local c = lh.v.k;
self:check_condition(ls, c == "VLOCAL" or c == "VUPVAL" or c == "VVOID" or c == "VINDEXED", "syntax error");
if self:testnext(ls, ",") then
local nv = {};
nv.v = {};
nv.prev = lh;
self:primaryexp(ls, nv.v);
if nv.v.k ~= "VINDEXED" then
self:check_conflict(ls, lh, nv.v);
end;
self:checklimit(ls.fs, nvars, self.LUAI_MAXCCALLS - ls.L.nCcalls, "variables in assignment");
self:assignment(ls, nv, nvars + 1);
else
self:checknext(ls, "=");
local nexps = self:explist1(ls, e);
if nexps ~= nvars then
self:adjust_assign(ls, nvars, nexps, e);
if nexps > nvars then
ls.fs.freereg = ls.fs.freereg - (nexps - nvars);
end;
else
code:setoneret(ls.fs, e);
code:storevar(ls.fs, lh.v, e);
return;
end;
end;
self:init_exp(e, "VNONRELOC", ls.fs.freereg - 1);
code:storevar(ls.fs, lh.v, e);
end;
function parser.cond(self, ls)
local v = {};
self:expr(ls, v);
if v.k == "VNIL" then
v.k = "VFALSE";
end;
code:goiftrue(ls.fs, v);
return v.f;
end;
function parser.labelstat(self, ls, line)
local label = self:str_checkname(ls);
local fs = ls.fs;
if labels[label] then
lex:syntaxerror(ls, "label \'" .. (label .. "\' already defined"));
end;
labels[label] = code:getlabel(fs);
if gotos[label] then
for _, goto_pc in ipairs(gotos[label]) do
code:patchlist(fs, goto_pc, labels[label]);
end;
gotos[label] = nil;
end;
self:checknext(ls, "TK_DBCOLON");
end;
function parser.gotostat(self, ls)
local label = self:str_checkname(ls);
local fs = ls.fs;
local goto_pc = code:jump(fs);
if labels[label] then
code:patchlist(fs, goto_pc, labels[label]);
else
gotos[label] = gotos[label] or {};
table.insert(gotos[label], goto_pc);
end;
end;
function parser.breakstat(self, ls)
local fs = ls.fs;
local bl = fs.bl;
local upval = false;
while bl and not bl.isbreakable do
if bl.upval then
upval = true;
end;
bl = bl.previous;
end;
if not bl then
lex:syntaxerror(ls, "no loop to break");
end;
if upval then
code:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0);
end;
bl.breaklist = code:concat(fs, bl.breaklist, code:jump(fs));
end;
function parser.whilestat(self, ls, line)
local fs = ls.fs;
local bl = {};
lex:next(ls);
local whileinit = code:getlabel(fs);
local condexit = self:cond(ls);
self:enterblock(fs, bl, true);
self:checknext(ls, "TK_DO");
self:block(ls);
code:patchlist(fs, code:jump(fs), whileinit);
self:check_match(ls, "TK_END", "TK_WHILE", line);
self:leaveblock(fs);
code:patchtohere(fs, condexit);
end;
function parser.repeatstat(self, ls, line)
local fs = ls.fs;
local repeat_init = code:getlabel(fs);
local bl1, bl2 = {}, {};
self:enterblock(fs, bl1, true);
self:enterblock(fs, bl2, false);
lex:next(ls);
self:chunk(ls);
self:check_match(ls, "TK_UNTIL", "TK_REPEAT", line);
local condexit = self:cond(ls);
if not bl2.upval then
self:leaveblock(fs);
code:patchlist(ls.fs, condexit, repeat_init);
else
self:breakstat(ls);
code:patchtohere(ls.fs, condexit);
self:leaveblock(fs);
code:patchlist(ls.fs, code:jump(fs), repeat_init);
end;
self:leaveblock(fs);
end;
function parser.exp1(self, ls)
local e = {};
self:expr(ls, e);
local k = e.k;
code:exp2nextreg(ls.fs, e);
return k;
end;
function parser.forbody(self, ls, base, line, nvars, isnum)
local bl = {};
local fs = ls.fs;
self:adjustlocalvars(ls, 3);
self:checknext(ls, "TK_DO");
local prep = isnum and code:codeAsBx(fs, "OP_FORPREP", base, code.NO_JUMP) or code:jump(fs);
self:enterblock(fs, bl, false);
self:adjustlocalvars(ls, nvars);
code:reserveregs(fs, nvars);
self:block(ls);
self:leaveblock(fs);
code:patchtohere(fs, prep);
if isnum then
endfor = code:codeAsBx(fs, "OP_FORLOOP", base, code.NO_JUMP);
else
code:codeABC(fs, "OP_TFORCALL", base, 0, nvars);
code:fixline(fs, line);
endfor = code:codeAsBx(fs, "OP_TFORLOOP", base + 2, code.NO_JUMP);
end;
code:fixline(fs, line);
code:patchlist(fs, endfor, prep + 1);
end;
function parser.fornum(self, ls, varname, line)
local fs = ls.fs;
local base = fs.freereg;
self:new_localvarliteral(ls, "(for index)", 0);
self:new_localvarliteral(ls, "(for limit)", 1);
self:new_localvarliteral(ls, "(for step)", 2);
self:new_localvar(ls, varname, 3);
self:checknext(ls, "=");
self:exp1(ls);
self:checknext(ls, ",");
self:exp1(ls);
if self:testnext(ls, ",") then
self:exp1(ls);
else
code:codeABx(fs, "OP_LOADK", fs.freereg, code:numberK(fs, 1));
code:reserveregs(fs, 1);
end;
self:forbody(ls, base, line, 1, true);
end;
function parser.forlist(self, ls, indexname)
local fs = ls.fs;
local e = {};
local nvars = 0;
local base = fs.freereg;
self:new_localvarliteral(ls, "(for generator)", nvars);
nvars = nvars + 1;
self:new_localvarliteral(ls, "(for state)", nvars);
nvars = nvars + 1;
self:new_localvarliteral(ls, "(for control)", nvars);
nvars = nvars + 1;
self:new_localvar(ls, indexname, nvars);
nvars = nvars + 1;
while self:testnext(ls, ",") do
self:new_localvar(ls, self:str_checkname(ls), nvars);
nvars = nvars + 1;
end;
self:checknext(ls, "TK_IN");
local line = ls.linenumber;
self:adjust_assign(ls, 3, self:explist1(ls, e), e);
code:checkstack(fs, 3);
self:forbody(ls, base, line, nvars - 3, false);
end;
function parser.forstat(self, ls, line)
local fs = ls.fs;
local bl = {};
self:enterblock(fs, bl, true);
lex:next(ls);
local varname = self:str_checkname(ls);
local c = ls.t.token;
if c == "=" then
self:fornum(ls, varname, line);
elseif c == "," or c == "TK_IN" then
self:forlist(ls, varname);
else
lex:syntaxerror(ls, self:LUA_QL("=") .. (" or " .. (self:LUA_QL("in") .. " expected")));
end;
self:check_match(ls, "TK_END", "TK_FOR", line);
self:leaveblock(fs);
end;
function parser.test_then_block(self, ls)
lex:next(ls);
local condexit = self:cond(ls);
self:checknext(ls, "TK_THEN");
self:block(ls);