forked from lua/lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua.stx
935 lines (758 loc) · 22.4 KB
/
lua.stx
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
%{
/*
** $Id: lua.stx,v 1.22 1997/12/09 16:01:08 roberto Exp roberto $
** Syntax analizer and code generator
** See Copyright Notice in lua.h
*/
#include <stdlib.h>
#include <string.h>
#include "lauxlib.h"
#include "ldo.h"
#include "lfunc.h"
#include "llex.h"
#include "lmem.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "lua.h"
#include "luadebug.h"
#include "lzio.h"
int luaY_parse (void);
#define MES_LIM(x) "(limit=" x ")"
/* size of a "normal" jump instruction: OpCode + 1 byte */
#define JMPSIZE 2
/* maximum number of local variables */
#define MAXLOCALS 32
#define SMAXLOCALS "32"
#define MINGLOBAL (MAXLOCALS+1)
/* maximum number of variables in a multiple assignment */
#define MAXVAR 32
#define SMAXVAR "32"
/* maximum number of nested functions */
#define MAXSTATES 6
#define SMAXSTATES "6"
/* maximum number of upvalues */
#define MAXUPVALUES 16
#define SMAXUPVALUES "16"
/*
** Variable descriptor:
** if 0<n<MINGLOBAL, represents local variable indexed by (n-1);
** if MINGLOBAL<=n, represents global variable at position (n-MINGLOBAL);
** if n<0, indexed variable with index (-n)-1 (table on top of stack);
** if n==0, an indexed variable (table and index on top of stack)
** Must be long to store negative Word values.
*/
typedef long vardesc;
#define isglobal(v) (MINGLOBAL<=(v))
#define globalindex(v) ((v)-MINGLOBAL)
#define islocal(v) (0<(v) && (v)<MINGLOBAL)
#define localindex(v) ((v)-1)
#define isdot(v) (v<0)
#define dotindex(v) ((-(v))-1)
/* state needed to generate code for a given function */
typedef struct FuncState {
TProtoFunc *f; /* current function header */
int pc; /* next position to code */
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
int stacksize; /* number of values on activation register */
int maxstacksize; /* maximum number of values on activation register */
int nlocalvar; /* number of active local variables */
int nupvalues; /* number of upvalues */
int nvars; /* number of entries in f->locvars */
int maxcode; /* size of f->code */
int maxvars; /* size of f->locvars (-1 if no debug information) */
int maxconsts; /* size of f->consts */
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
vardesc upvalues[MAXUPVALUES]; /* upvalues */
} FuncState;
#define YYPURE 1
void luaY_syntaxerror (char *s, char *token)
{
if (token[0] == 0)
token = "<eof>";
luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s",
s, token, L->lexstate->linenumber, L->mainState->f->fileName->str);
}
void luaY_error (char *s)
{
luaY_syntaxerror(s, luaX_lasttoken());
}
static void check_pc (int n)
{
if (L->currState->pc+n > L->currState->maxcode)
L->currState->maxcode = luaM_growvector(&L->currState->f->code,
L->currState->maxcode, Byte, codeEM, MAX_INT);
}
static void movecode_up (int d, int s, int n)
{
while (n--)
L->currState->f->code[d+n] = L->currState->f->code[s+n];
}
static void movecode_down (int d, int s, int n)
{
int i;
for (i=0; i<n; i++)
L->currState->f->code[d+i] = L->currState->f->code[s+i];
}
static void code_byte (Byte c)
{
check_pc(1);
L->currState->f->code[L->currState->pc++] = c;
}
static void deltastack (int delta)
{
L->currState->stacksize += delta;
if (L->currState->stacksize > L->currState->maxstacksize) {
if (L->currState->stacksize > 255)
luaY_error("function/expression too complex");
L->currState->maxstacksize = L->currState->stacksize;
}
}
static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
{
deltastack(delta);
if (arg < builtin) {
L->currState->f->code[pc] = op+1+arg;
return 1;
}
else if (arg <= 255) {
L->currState->f->code[pc] = op;
L->currState->f->code[pc+1] = arg;
return 2;
}
else if (arg <= MAX_WORD) {
L->currState->f->code[pc] = op+1+builtin;
L->currState->f->code[pc+1] = arg&0xFF;
L->currState->f->code[pc+2] = arg>>8;
return 3;
}
else luaY_error("code too long " MES_LIM("64K"));
return 0; /* to avoid warnings */
}
static int fix_opcode (int pc, OpCode op, int builtin, int arg)
{
if (arg < builtin) { /* close space */
movecode_down(pc+1, pc+2, L->currState->pc-(pc+2));
L->currState->pc--;
}
else if (arg > 255) { /* open space */
check_pc(1);
movecode_up(pc+1, pc, L->currState->pc-pc);
L->currState->pc++;
}
return code_oparg_at(pc, op, builtin, arg, 0) - 2;
}
static void code_oparg (OpCode op, int builtin, int arg, int delta)
{
check_pc(3); /* maximum code size */
L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
}
static void code_opcode (OpCode op, int delta)
{
deltastack(delta);
code_byte(op);
}
static void code_pop (OpCode op)
{
code_opcode(op, -1);
}
/* binary operations get 2 arguments and leave one, so they pop one */
#define code_binop(op) code_pop(op)
static void code_neutralop (OpCode op)
{
code_opcode(op, 0);
}
/* unary operations get 1 argument and leave one, so they are neutral */
#define code_unop(op) code_neutralop(op)
static void code_constant (int c)
{
code_oparg(PUSHCONSTANT, 8, c, 1);
}
static int next_constant (FuncState *cs)
{
TProtoFunc *f = cs->f;
if (f->nconsts >= cs->maxconsts) {
cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject,
constantEM, MAX_WORD);
}
return f->nconsts++;
}
static int string_constant (TaggedString *s, FuncState *cs)
{
TProtoFunc *f = cs->f;
int c = s->constindex;
if (!(c < f->nconsts &&
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
c = next_constant(cs);
ttype(&f->consts[c]) = LUA_T_STRING;
tsvalue(&f->consts[c]) = s;
s->constindex = c; /* hint for next time */
}
return c;
}
static void code_string (TaggedString *s)
{
code_constant(string_constant(s, L->currState));
}
#define LIM 20
static int real_constant (real r)
{
/* check whether 'r' has appeared within the last LIM entries */
TObject *cnt = L->currState->f->consts;
int c = L->currState->f->nconsts;
int lim = c < LIM ? 0 : c-LIM;
while (--c >= lim) {
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
return c;
}
/* not found; create a luaM_new entry */
c = next_constant(L->currState);
cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
ttype(&cnt[c]) = LUA_T_NUMBER;
nvalue(&cnt[c]) = r;
return c;
}
static void code_number (real f)
{
Word i;
if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f)
code_oparg(PUSHNUMBER, 3, i, 1); /* f has an (short) integer value */
else
code_constant(real_constant(f));
}
static void flush_record (int n)
{
if (n > 0)
code_oparg(SETMAP, 1, n-1, -2*n);
}
static void flush_list (int m, int n)
{
if (n == 0) return;
code_oparg(SETLIST, 1, m, -n);
code_byte(n);
}
static void luaI_registerlocalvar (TaggedString *varname, int line)
{
if (L->currState->maxvars != -1) { /* debug information? */
if (L->currState->nvars >= L->currState->maxvars)
L->currState->maxvars = luaM_growvector(&L->currState->f->locvars,
L->currState->maxvars, LocVar, "", MAX_WORD);
L->currState->f->locvars[L->currState->nvars].varname = varname;
L->currState->f->locvars[L->currState->nvars].line = line;
L->currState->nvars++;
}
}
static void luaI_unregisterlocalvar (int line)
{
luaI_registerlocalvar(NULL, line);
}
static void store_localvar (TaggedString *name, int n)
{
if (L->currState->nlocalvar+n < MAXLOCALS)
L->currState->localvar[L->currState->nlocalvar+n] = name;
else
luaY_error("too many local variables " MES_LIM(SMAXLOCALS));
luaI_registerlocalvar(name, L->lexstate->linenumber);
}
static void add_localvar (TaggedString *name)
{
store_localvar(name, 0);
L->currState->nlocalvar++;
}
/*
** dotted variables <a.x> must be stored like regular indexed vars <a["x"]>
*/
static vardesc var2store (vardesc var)
{
if (isdot(var)) {
code_constant(dotindex(var));
var = 0;
}
return var;
}
static void add_varbuffer (vardesc var, int n)
{
if (n >= MAXVAR)
luaY_error("variable buffer overflow " MES_LIM(SMAXVAR));
L->currState->varbuffer[n] = var2store(var);
}
static int aux_localname (TaggedString *n, FuncState *st)
{
int i;
for (i=st->nlocalvar-1; i >= 0; i--)
if (n == st->localvar[i]) return i; /* local var index */
return -1; /* not found */
}
static vardesc singlevar (TaggedString *n, FuncState *st)
{
int i = aux_localname(n, st);
if (i == -1) { /* check shadowing */
int l;
for (l=1; l<=(st-L->mainState); l++)
if (aux_localname(n, st-l) >= 0)
luaY_syntaxerror("cannot access a variable in outer scope", n->str);
return string_constant(n, st)+MINGLOBAL; /* global value */
}
else return i+1; /* local value */
}
static int indexupvalue (TaggedString *n)
{
vardesc v = singlevar(n, L->currState-1);
int i;
for (i=0; i<L->currState->nupvalues; i++) {
if (L->currState->upvalues[i] == v)
return i;
}
/* new one */
if (++(L->currState->nupvalues) > MAXUPVALUES)
luaY_error("too many upvalues in a single function " MES_LIM(SMAXUPVALUES));
L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
return i;
}
static void pushupvalue (TaggedString *n)
{
int i;
if (L->currState == L->mainState)
luaY_error("cannot access upvalue in main");
if (aux_localname(n, L->currState) >= 0)
luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
i = indexupvalue(n);
code_oparg(PUSHUPVALUE, 2, i, 1);
}
void luaY_codedebugline (int line)
{
if (lua_debug && line != L->lexstate->lastline) {
code_oparg(SETLINE, 0, line, 0);
L->lexstate->lastline = line;
}
}
static void adjuststack (int n)
{
if (n > 0)
code_oparg(POP, 2, n-1, -n);
else if (n < 0)
code_oparg(PUSHNIL, 1, (-n)-1, -n);
}
static long adjust_functioncall (long exp, int nresults)
{
if (exp <= 0)
return -exp; /* exp is -list length */
else {
int temp = L->currState->f->code[exp];
int nparams = L->currState->f->code[exp-1];
exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
L->currState->f->code[exp] = nparams;
if (nresults != MULT_RET)
deltastack(nresults);
deltastack(-(nparams+1));
return temp+nresults;
}
}
static void adjust_mult_assign (int vars, long exps)
{
if (exps > 0) { /* must correct function call */
int diff = L->currState->f->code[exps] - vars;
if (diff < 0)
adjust_functioncall(exps, -diff);
else {
adjust_functioncall(exps, 0);
adjuststack(diff);
}
}
else adjuststack((-exps)-vars);
}
static void code_args (int nparams, int dots)
{
L->currState->nlocalvar += nparams;
if (!dots)
code_oparg(ARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar);
else {
code_oparg(VARARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar+1);
add_localvar(luaS_new("arg"));
}
}
static void lua_pushvar (vardesc var)
{
if (isglobal(var))
code_oparg(GETGLOBAL, 8, globalindex(var), 1);
else if (islocal(var))
code_oparg(PUSHLOCAL, 8, localindex(var), 1);
else if (isdot(var))
code_oparg(GETDOTTED, 8, dotindex(var), 0);
else
code_pop(GETTABLE);
}
static void storevar (vardesc var)
{
if (var == 0) /* indexed var */
code_opcode(SETTABLE0, -3);
else if (isglobal(var))
code_oparg(SETGLOBAL, 8, globalindex(var), -1);
else /* local var */
code_oparg(SETLOCAL, 8, localindex(var), -1);
}
/* returns how many elements are left as 'garbage' on the stack */
static int lua_codestore (int i, int left)
{
if (L->currState->varbuffer[i] != 0 || /* global or local var or */
left+i == 0) { /* indexed var without values in between */
storevar(L->currState->varbuffer[i]);
return left;
}
else { /* indexed var with values in between*/
code_oparg(SETTABLE, 0, left+i, -1);
return left+2; /* table/index are not popped, since they are not on top */
}
}
static int fix_jump (int pc, OpCode op, int n)
{
/* jump is relative to position following jump instruction */
return fix_opcode(pc, op, 0, n-(pc+JMPSIZE));
}
static void fix_upjmp (OpCode op, int pos)
{
int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
if (delta > 255) delta++;
code_oparg(op, 0, delta, 0);
}
static void codeIf (int thenAdd, int elseAdd)
{
int elseinit = elseAdd+JMPSIZE;
if (L->currState->pc == elseinit) { /* no else part */
L->currState->pc -= JMPSIZE;
elseinit = L->currState->pc;
}
else
elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
fix_jump(thenAdd, IFFJMP, elseinit);
}
static void code_shortcircuit (int pc, OpCode op)
{
fix_jump(pc, op, L->currState->pc);
}
static void codereturn (void)
{
code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
L->currState->stacksize = L->currState->nlocalvar;
}
static void func_onstack (TProtoFunc *f)
{
int i;
int nupvalues = (L->currState+1)->nupvalues;
int c = next_constant(L->currState);
ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
L->currState->f->consts[c].value.tf = (L->currState+1)->f;
if (nupvalues == 0)
code_constant(c);
else {
for (i=0; i<nupvalues; i++)
lua_pushvar((L->currState+1)->upvalues[i]);
code_constant(c);
code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
}
}
static void init_state (TaggedString *filename)
{
TProtoFunc *f = luaF_newproto();
L->currState->stacksize = 0;
L->currState->maxstacksize = 0;
L->currState->nlocalvar = 0;
L->currState->nupvalues = 0;
L->currState->f = f;
f->fileName = filename;
L->currState->pc = 0;
L->currState->maxcode = 0;
f->code = NULL;
L->currState->maxconsts = 0;
if (lua_debug) {
L->currState->nvars = 0;
L->currState->maxvars = 0;
}
else
L->currState->maxvars = -1; /* flag no debug information */
code_byte(0); /* to be filled with stacksize */
L->lexstate->lastline = 0; /* invalidate it */
}
static void init_func (void)
{
if (L->currState-L->mainState >= MAXSTATES-1)
luaY_error("too many nested functions " MES_LIM(SMAXSTATES));
L->currState++;
init_state(L->mainState->f->fileName);
luaY_codedebugline(L->lexstate->linenumber);
L->currState->f->lineDefined = L->lexstate->linenumber;
}
static TProtoFunc *close_func (void)
{
TProtoFunc *f = L->currState->f;
code_neutralop(ENDCODE);
f->code[0] = L->currState->maxstacksize;
f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
if (L->currState->maxvars != -1) { /* debug information? */
luaI_registerlocalvar(NULL, -1); /* flag end of vector */
f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
}
L->currState--;
return f;
}
/*
** Parse Lua code.
*/
TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
{
struct LexState lexstate;
FuncState state[MAXSTATES];
L->currState = L->mainState = &state[0];
L->lexstate = &lexstate;
luaX_setinput(z);
init_state(luaS_new(chunkname));
if (luaY_parse()) lua_error("parse error");
return close_func();
}
%}
%union
{
int vInt;
real vReal;
char *pChar;
long vLong;
TaggedString *pTStr;
TProtoFunc *pFunc;
}
%start chunk
%token WRONGTOKEN
%token NIL
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
%token RETURN
%token LOCAL
%token FUNCTION
%token DOTS
%token <vReal> NUMBER
%token <pTStr> NAME STRING
%type <vInt> SaveWord, cond, GetPC, SaveWordPop, SaveWordPush
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
counter (which has list length); if <= 0, -list lenght */
%type <vLong> functioncall, expr /* if != 0, points to function return
counter */
%type <vInt> varlist1, funcParams, funcvalue
%type <vInt> fieldlist, localnamelist, decinit
%type <vInt> ffieldlist, ffieldlist1, semicolonpart
%type <vInt> lfieldlist, lfieldlist1
%type <vLong> var, funcname /* vardesc */
%type <pFunc> body
%left AND OR
%left EQ NE '>' '<' LE GE
%left CONC
%left '+' '-'
%left '*' '/'
%left UNARY NOT
%right '^'
%% /* beginning of rules section */
chunk : statlist ret
;
statlist : /* empty */
| statlist stat sc
;
sc : /* empty */ | ';' ;
stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
| DO block END
| WHILE GetPC cond DO block END
{{
int expsize = $3-$2;
int newpos = $2+JMPSIZE;
check_pc(expsize);
memcpy(&L->currState->f->code[L->currState->pc],
&L->currState->f->code[$2], expsize);
movecode_down($2, $3, L->currState->pc-$2);
newpos += fix_jump($2, JMP, L->currState->pc-expsize);
fix_upjmp(IFTUPJMP, newpos);
}}
| REPEAT GetPC block UNTIL expr1
{
fix_upjmp(IFFUPJMP, $2);
deltastack(-1); /* pops condition */
}
| varlist1 '=' exprlist1
{{
int i;
int left = 0;
adjust_mult_assign($1, $3);
for (i=$1-1; i>=0; i--)
left = lua_codestore(i, left);
adjuststack(left); /* remove eventual 'garbage' left on stack */
}}
| functioncall { adjust_functioncall($1, 0); }
| LOCAL localnamelist decinit
{
L->currState->nlocalvar += $2;
adjust_mult_assign($2, $3);
}
| FUNCTION funcname body { func_onstack($3); storevar($2); }
;
block : {$<vInt>$ = L->currState->nlocalvar;} chunk
{
adjuststack(L->currState->nlocalvar - $<vInt>1);
for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
luaI_unregisterlocalvar(L->lexstate->linenumber);
}
;
funcname : var { $$ = var2store($1); init_func(); }
| varexp ':' NAME
{
code_string($3);
$$ = 0; /* flag indexed variable */
init_func();
add_localvar(luaS_new("self"));
}
;
body : '(' parlist ')' chunk END { $$ = close_func(); }
;
elsepart : /* empty */
| ELSE block
| ELSEIF cond THEN block SaveWord elsepart { codeIf($2, $5); }
;
ret : /* empty */
| RETURN exprlist sc
{
adjust_functioncall($2, MULT_RET);
codereturn();
}
;
GetPC : /* empty */ { $$ = L->currState->pc; }
;
SaveWord : /* empty */
{ $$ = L->currState->pc;
check_pc(JMPSIZE);
L->currState->pc += JMPSIZE; /* open space */
}
;
SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ }
;
SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ }
;
cond : expr1 SaveWordPop { $$ = $2; }
;
expr1 : expr { adjust_functioncall($1, 1); }
;
expr : '(' expr ')' { $$ = $2; }
| expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
| expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
| expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
| expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
| expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
| expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
| expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
| expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
| expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
| expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
| expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
| expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
| '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
| NOT expr1 { code_unop(NOTOP); $$ = 0;}
| table { $$ = 0; }
| varexp { $$ = 0;}
| NUMBER { code_number($1); $$ = 0; }
| STRING { code_string($1); $$ = 0; }
| NIL { adjuststack(-1); $$ = 0; }
| functioncall { $$ = $1; }
| FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
| expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
| expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
;
table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); }
;
functioncall : funcvalue funcParams
{
code_byte(0); /* save space for opcode */
code_byte($1+$2); /* number of parameters */
$$ = L->currState->pc;
code_byte(0); /* must be adjusted by other rules */
}
;
funcvalue : varexp { $$ = 0; }
| varexp ':' NAME
{
code_oparg(PUSHSELF, 0, string_constant($3, L->currState), 1);
$$ = 1;
}
;
funcParams : '(' exprlist ')' { $$ = adjust_functioncall($2, 1); }
| table { $$ = 1; }
;
exprlist : /* empty */ { $$ = 0; }
| exprlist1 { $$ = $1; }
;
exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
| exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
{
if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
else {
L->currState->f->code[$4] = $<vLong>3; /* store list length */
$$ = $4;
}
}
;
parlist : /* empty */ { code_args(0, 0); }
| DOTS { code_args(0, 1); }
| localnamelist { code_args($1, 0); }
| localnamelist ',' DOTS { code_args($1, 1); }
;
fieldlist : lfieldlist
{ flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH); }
semicolonpart
{ $$ = $1+$3; }
| ffieldlist1 lastcomma
{ $$ = $1; flush_record($1%RFIELDS_PER_FLUSH); }
;
semicolonpart : /* empty */ { $$ = 0; }
| ';' ffieldlist { $$ = $2; flush_record($2%RFIELDS_PER_FLUSH); }
;
lastcomma : /* empty */
| ','
;
ffieldlist : /* empty */ { $$ = 0; }
| ffieldlist1 lastcomma { $$ = $1; }
;
ffieldlist1 : ffield {$$=1;}
| ffieldlist1 ',' ffield
{
$$=$1+1;
if ($$%RFIELDS_PER_FLUSH == 0)
flush_record(RFIELDS_PER_FLUSH);
}
;
ffield : ffieldkey '=' expr1
;
ffieldkey : '[' expr1 ']'
| NAME { code_string($1); }
;
lfieldlist : /* empty */ { $$ = 0; }
| lfieldlist1 lastcomma { $$ = $1; }
;
lfieldlist1 : expr1 {$$=1;}
| lfieldlist1 ',' expr1
{
$$=$1+1;
if ($$%LFIELDS_PER_FLUSH == 0)
flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
}
;
varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
| varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
;
var : NAME { $$ = singlevar($1, L->currState); }
| varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */
| varexp '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
;
varexp : var { lua_pushvar($1); }
| '%' NAME { pushupvalue($2); }
;
localnamelist : NAME {store_localvar($1, 0); $$ = 1;}
| localnamelist ',' NAME { store_localvar($3, $1); $$ = $1+1; }
;
decinit : /* empty */ { $$ = 0; }
| '=' exprlist1 { $$ = $2; }
;
%%