xref: /netbsd/external/mit/lua/dist/src/lparser.c (revision f13f21ab)
1 /*	$NetBSD: lparser.c,v 1.15 2023/06/08 21:12:08 nikita Exp $	*/
2 
3 /*
4 ** Id: lparser.c
5 ** Lua Parser
6 ** See Copyright Notice in lua.h
7 */
8 
9 #define lparser_c
10 #define LUA_CORE
11 
12 #include "lprefix.h"
13 
14 
15 #ifndef _KERNEL
16 #include <limits.h>
17 #include <string.h>
18 #endif /* _KERNEL */
19 
20 #include "lua.h"
21 
22 #include "lcode.h"
23 #include "ldebug.h"
24 #include "ldo.h"
25 #include "lfunc.h"
26 #include "llex.h"
27 #include "lmem.h"
28 #include "lobject.h"
29 #include "lopcodes.h"
30 #include "lparser.h"
31 #include "lstate.h"
32 #include "lstring.h"
33 #include "ltable.h"
34 
35 
36 
37 /* maximum number of local variables per function (must be smaller
38    than 250, due to the bytecode format) */
39 #define MAXVARS		200
40 
41 
42 #define hasmultret(k)		((k) == VCALL || (k) == VVARARG)
43 
44 
45 /* because all strings are unified by the scanner, the parser
46    can use pointer equality for string equality */
47 #define eqstr(a,b)	((a) == (b))
48 
49 
50 /*
51 ** nodes for block list (list of active blocks)
52 */
53 typedef struct BlockCnt {
54   struct BlockCnt *previous;  /* chain */
55   int firstlabel;  /* index of first label in this block */
56   int firstgoto;  /* index of first pending goto in this block */
57   lu_byte nactvar;  /* # active locals outside the block */
58   lu_byte upval;  /* true if some variable in the block is an upvalue */
59   lu_byte isloop;  /* true if 'block' is a loop */
60   lu_byte insidetbc;  /* true if inside the scope of a to-be-closed var. */
61 } BlockCnt;
62 
63 
64 
65 /*
66 ** prototypes for recursive non-terminal functions
67 */
68 static void statement (LexState *ls);
69 static void expr (LexState *ls, expdesc *v);
70 
71 
error_expected(LexState * ls,int token)72 static l_noret error_expected (LexState *ls, int token) {
73   luaX_syntaxerror(ls,
74       luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
75 }
76 
77 
errorlimit(FuncState * fs,int limit,const char * what)78 static l_noret errorlimit (FuncState *fs, int limit, const char *what) {
79   lua_State *L = fs->ls->L;
80   const char *msg;
81   int line = fs->f->linedefined;
82   const char *where = (line == 0)
83                       ? "main function"
84                       : luaO_pushfstring(L, "function at line %d", line);
85   msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
86                              what, limit, where);
87   luaX_syntaxerror(fs->ls, msg);
88 }
89 
90 
checklimit(FuncState * fs,int v,int l,const char * what)91 static void checklimit (FuncState *fs, int v, int l, const char *what) {
92   if (v > l) errorlimit(fs, l, what);
93 }
94 
95 
96 /*
97 ** Test whether next token is 'c'; if so, skip it.
98 */
testnext(LexState * ls,int c)99 static int testnext (LexState *ls, int c) {
100   if (ls->t.token == c) {
101     luaX_next(ls);
102     return 1;
103   }
104   else return 0;
105 }
106 
107 
108 /*
109 ** Check that next token is 'c'.
110 */
check(LexState * ls,int c)111 static void check (LexState *ls, int c) {
112   if (ls->t.token != c)
113     error_expected(ls, c);
114 }
115 
116 
117 /*
118 ** Check that next token is 'c' and skip it.
119 */
checknext(LexState * ls,int c)120 static void checknext (LexState *ls, int c) {
121   check(ls, c);
122   luaX_next(ls);
123 }
124 
125 
126 #define check_condition(ls,c,msg)	{ if (!(c)) luaX_syntaxerror(ls, msg); }
127 
128 
129 /*
130 ** Check that next token is 'what' and skip it. In case of error,
131 ** raise an error that the expected 'what' should match a 'who'
132 ** in line 'where' (if that is not the current line).
133 */
check_match(LexState * ls,int what,int who,int where)134 static void check_match (LexState *ls, int what, int who, int where) {
135   if (l_unlikely(!testnext(ls, what))) {
136     if (where == ls->linenumber)  /* all in the same line? */
137       error_expected(ls, what);  /* do not need a complex message */
138     else {
139       luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
140              "%s expected (to close %s at line %d)",
141               luaX_token2str(ls, what), luaX_token2str(ls, who), where));
142     }
143   }
144 }
145 
146 
str_checkname(LexState * ls)147 static TString *str_checkname (LexState *ls) {
148   TString *ts;
149   check(ls, TK_NAME);
150   ts = ls->t.seminfo.ts;
151   luaX_next(ls);
152   return ts;
153 }
154 
155 
init_exp(expdesc * e,expkind k,int i)156 static void init_exp (expdesc *e, expkind k, int i) {
157   e->f = e->t = NO_JUMP;
158   e->k = k;
159   e->u.info = i;
160 }
161 
162 
codestring(expdesc * e,TString * s)163 static void codestring (expdesc *e, TString *s) {
164   e->f = e->t = NO_JUMP;
165   e->k = VKSTR;
166   e->u.strval = s;
167 }
168 
169 
codename(LexState * ls,expdesc * e)170 static void codename (LexState *ls, expdesc *e) {
171   codestring(e, str_checkname(ls));
172 }
173 
174 
175 /*
176 ** Register a new local variable in the active 'Proto' (for debug
177 ** information).
178 */
registerlocalvar(LexState * ls,FuncState * fs,TString * varname)179 static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) {
180   Proto *f = fs->f;
181   int oldsize = f->sizelocvars;
182   luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars,
183                   LocVar, SHRT_MAX, "local variables");
184   while (oldsize < f->sizelocvars)
185     f->locvars[oldsize++].varname = NULL;
186   f->locvars[fs->ndebugvars].varname = varname;
187   f->locvars[fs->ndebugvars].startpc = fs->pc;
188   luaC_objbarrier(ls->L, f, varname);
189   return fs->ndebugvars++;
190 }
191 
192 
193 /*
194 ** Create a new local variable with the given 'name'. Return its index
195 ** in the function.
196 */
new_localvar(LexState * ls,TString * name)197 static int new_localvar (LexState *ls, TString *name) {
198   lua_State *L = ls->L;
199   FuncState *fs = ls->fs;
200   Dyndata *dyd = ls->dyd;
201   Vardesc *var;
202   checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
203                  MAXVARS, "local variables");
204   luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1,
205                   dyd->actvar.size, Vardesc, USHRT_MAX, "local variables");
206   var = &dyd->actvar.arr[dyd->actvar.n++];
207   var->vd.kind = VDKREG;  /* default */
208   var->vd.name = name;
209   return dyd->actvar.n - 1 - fs->firstlocal;
210 }
211 
212 #define new_localvarliteral(ls,v) \
213     new_localvar(ls,  \
214       luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1));
215 
216 
217 
218 /*
219 ** Return the "variable description" (Vardesc) of a given variable.
220 ** (Unless noted otherwise, all variables are referred to by their
221 ** compiler indices.)
222 */
getlocalvardesc(FuncState * fs,int vidx)223 static Vardesc *getlocalvardesc (FuncState *fs, int vidx) {
224   return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx];
225 }
226 
227 
228 /*
229 ** Convert 'nvar', a compiler index level, to its corresponding
230 ** register. For that, search for the highest variable below that level
231 ** that is in a register and uses its register index ('ridx') plus one.
232 */
reglevel(FuncState * fs,int nvar)233 static int reglevel (FuncState *fs, int nvar) {
234   while (nvar-- > 0) {
235     Vardesc *vd = getlocalvardesc(fs, nvar);  /* get previous variable */
236     if (vd->vd.kind != RDKCTC)  /* is in a register? */
237       return vd->vd.ridx + 1;
238   }
239   return 0;  /* no variables in registers */
240 }
241 
242 
243 /*
244 ** Return the number of variables in the register stack for the given
245 ** function.
246 */
luaY_nvarstack(FuncState * fs)247 int luaY_nvarstack (FuncState *fs) {
248   return reglevel(fs, fs->nactvar);
249 }
250 
251 
252 /*
253 ** Get the debug-information entry for current variable 'vidx'.
254 */
localdebuginfo(FuncState * fs,int vidx)255 static LocVar *localdebuginfo (FuncState *fs, int vidx) {
256   Vardesc *vd = getlocalvardesc(fs,  vidx);
257   if (vd->vd.kind == RDKCTC)
258     return NULL;  /* no debug info. for constants */
259   else {
260     int idx = vd->vd.pidx;
261     lua_assert(idx < fs->ndebugvars);
262     return &fs->f->locvars[idx];
263   }
264 }
265 
266 
267 /*
268 ** Create an expression representing variable 'vidx'
269 */
init_var(FuncState * fs,expdesc * e,int vidx)270 static void init_var (FuncState *fs, expdesc *e, int vidx) {
271   e->f = e->t = NO_JUMP;
272   e->k = VLOCAL;
273   e->u.var.vidx = vidx;
274   e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx;
275 }
276 
277 
278 /*
279 ** Raises an error if variable described by 'e' is read only
280 */
check_readonly(LexState * ls,expdesc * e)281 static void check_readonly (LexState *ls, expdesc *e) {
282   FuncState *fs = ls->fs;
283   TString *varname = NULL;  /* to be set if variable is const */
284   switch (e->k) {
285     case VCONST: {
286       varname = ls->dyd->actvar.arr[e->u.info].vd.name;
287       break;
288     }
289     case VLOCAL: {
290       Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx);
291       if (vardesc->vd.kind != VDKREG)  /* not a regular variable? */
292         varname = vardesc->vd.name;
293       break;
294     }
295     case VUPVAL: {
296       Upvaldesc *up = &fs->f->upvalues[e->u.info];
297       if (up->kind != VDKREG)
298         varname = up->name;
299       break;
300     }
301     default:
302       return;  /* other cases cannot be read-only */
303   }
304   if (varname) {
305     const char *msg = luaO_pushfstring(ls->L,
306        "attempt to assign to const variable '%s'", getstr(varname));
307     luaK_semerror(ls, msg);  /* error */
308   }
309 }
310 
311 
312 /*
313 ** Start the scope for the last 'nvars' created variables.
314 */
adjustlocalvars(LexState * ls,int nvars)315 static void adjustlocalvars (LexState *ls, int nvars) {
316   FuncState *fs = ls->fs;
317   int reglevel = luaY_nvarstack(fs);
318   int i;
319   for (i = 0; i < nvars; i++) {
320     int vidx = fs->nactvar++;
321     Vardesc *var = getlocalvardesc(fs, vidx);
322     var->vd.ridx = reglevel++;
323     var->vd.pidx = registerlocalvar(ls, fs, var->vd.name);
324   }
325 }
326 
327 
328 /*
329 ** Close the scope for all variables up to level 'tolevel'.
330 ** (debug info.)
331 */
removevars(FuncState * fs,int tolevel)332 static void removevars (FuncState *fs, int tolevel) {
333   fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
334   while (fs->nactvar > tolevel) {
335     LocVar *var = localdebuginfo(fs, --fs->nactvar);
336     if (var)  /* does it have debug information? */
337       var->endpc = fs->pc;
338   }
339 }
340 
341 
342 /*
343 ** Search the upvalues of the function 'fs' for one
344 ** with the given 'name'.
345 */
searchupvalue(FuncState * fs,TString * name)346 static int searchupvalue (FuncState *fs, TString *name) {
347   int i;
348   Upvaldesc *up = fs->f->upvalues;
349   for (i = 0; i < fs->nups; i++) {
350     if (eqstr(up[i].name, name)) return i;
351   }
352   return -1;  /* not found */
353 }
354 
355 
allocupvalue(FuncState * fs)356 static Upvaldesc *allocupvalue (FuncState *fs) {
357   Proto *f = fs->f;
358   int oldsize = f->sizeupvalues;
359   checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
360   luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
361                   Upvaldesc, MAXUPVAL, "upvalues");
362   while (oldsize < f->sizeupvalues)
363     f->upvalues[oldsize++].name = NULL;
364   return &f->upvalues[fs->nups++];
365 }
366 
367 
newupvalue(FuncState * fs,TString * name,expdesc * v)368 static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
369   Upvaldesc *up = allocupvalue(fs);
370   FuncState *prev = fs->prev;
371   if (v->k == VLOCAL) {
372     up->instack = 1;
373     up->idx = v->u.var.ridx;
374     up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind;
375     lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name));
376   }
377   else {
378     up->instack = 0;
379     up->idx = cast_byte(v->u.info);
380     up->kind = prev->f->upvalues[v->u.info].kind;
381     lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name));
382   }
383   up->name = name;
384   luaC_objbarrier(fs->ls->L, fs->f, name);
385   return fs->nups - 1;
386 }
387 
388 
389 /*
390 ** Look for an active local variable with the name 'n' in the
391 ** function 'fs'. If found, initialize 'var' with it and return
392 ** its expression kind; otherwise return -1.
393 */
searchvar(FuncState * fs,TString * n,expdesc * var)394 static int searchvar (FuncState *fs, TString *n, expdesc *var) {
395   int i;
396   for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
397     Vardesc *vd = getlocalvardesc(fs, i);
398     if (eqstr(n, vd->vd.name)) {  /* found? */
399       if (vd->vd.kind == RDKCTC)  /* compile-time constant? */
400         init_exp(var, VCONST, fs->firstlocal + i);
401       else  /* real variable */
402         init_var(fs, var, i);
403       return var->k;
404     }
405   }
406   return -1;  /* not found */
407 }
408 
409 
410 /*
411 ** Mark block where variable at given level was defined
412 ** (to emit close instructions later).
413 */
markupval(FuncState * fs,int level)414 static void markupval (FuncState *fs, int level) {
415   BlockCnt *bl = fs->bl;
416   while (bl->nactvar > level)
417     bl = bl->previous;
418   bl->upval = 1;
419   fs->needclose = 1;
420 }
421 
422 
423 /*
424 ** Mark that current block has a to-be-closed variable.
425 */
marktobeclosed(FuncState * fs)426 static void marktobeclosed (FuncState *fs) {
427   BlockCnt *bl = fs->bl;
428   bl->upval = 1;
429   bl->insidetbc = 1;
430   fs->needclose = 1;
431 }
432 
433 
434 /*
435 ** Find a variable with the given name 'n'. If it is an upvalue, add
436 ** this upvalue into all intermediate functions. If it is a global, set
437 ** 'var' as 'void' as a flag.
438 */
singlevaraux(FuncState * fs,TString * n,expdesc * var,int base)439 static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
440   if (fs == NULL)  /* no more levels? */
441     init_exp(var, VVOID, 0);  /* default is global */
442   else {
443     int v = searchvar(fs, n, var);  /* look up locals at current level */
444     if (v >= 0) {  /* found? */
445       if (v == VLOCAL && !base)
446         markupval(fs, var->u.var.vidx);  /* local will be used as an upval */
447     }
448     else {  /* not found as local at current level; try upvalues */
449       int idx = searchupvalue(fs, n);  /* try existing upvalues */
450       if (idx < 0) {  /* not found? */
451         singlevaraux(fs->prev, n, var, 0);  /* try upper levels */
452         if (var->k == VLOCAL || var->k == VUPVAL)  /* local or upvalue? */
453           idx  = newupvalue(fs, n, var);  /* will be a new upvalue */
454         else  /* it is a global or a constant */
455           return;  /* don't need to do anything at this level */
456       }
457       init_exp(var, VUPVAL, idx);  /* new or old upvalue */
458     }
459   }
460 }
461 
462 
463 /*
464 ** Find a variable with the given name 'n', handling global variables
465 ** too.
466 */
singlevar(LexState * ls,expdesc * var)467 static void singlevar (LexState *ls, expdesc *var) {
468   TString *varname = str_checkname(ls);
469   FuncState *fs = ls->fs;
470   singlevaraux(fs, varname, var, 1);
471   if (var->k == VVOID) {  /* global name? */
472     expdesc key;
473     singlevaraux(fs, ls->envn, var, 1);  /* get environment variable */
474     lua_assert(var->k != VVOID);  /* this one must exist */
475     luaK_exp2anyregup(fs, var);  /* but could be a constant */
476     codestring(&key, varname);  /* key is variable name */
477     luaK_indexed(fs, var, &key);  /* env[varname] */
478   }
479 }
480 
481 
482 /*
483 ** Adjust the number of results from an expression list 'e' with 'nexps'
484 ** expressions to 'nvars' values.
485 */
adjust_assign(LexState * ls,int nvars,int nexps,expdesc * e)486 static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
487   FuncState *fs = ls->fs;
488   int needed = nvars - nexps;  /* extra values needed */
489   if (hasmultret(e->k)) {  /* last expression has multiple returns? */
490     int extra = needed + 1;  /* discount last expression itself */
491     if (extra < 0)
492       extra = 0;
493     luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */
494   }
495   else {
496     if (e->k != VVOID)  /* at least one expression? */
497       luaK_exp2nextreg(fs, e);  /* close last expression */
498     if (needed > 0)  /* missing values? */
499       luaK_nil(fs, fs->freereg, needed);  /* complete with nils */
500   }
501   if (needed > 0)
502     luaK_reserveregs(fs, needed);  /* registers for extra values */
503   else  /* adding 'needed' is actually a subtraction */
504     fs->freereg += needed;  /* remove extra values */
505 }
506 
507 
508 #define enterlevel(ls)	luaE_incCstack(ls->L)
509 
510 
511 #define leavelevel(ls) ((ls)->L->nCcalls--)
512 
513 
514 /*
515 ** Generates an error that a goto jumps into the scope of some
516 ** local variable.
517 */
jumpscopeerror(LexState * ls,Labeldesc * gt)518 static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) {
519   const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name);
520   const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'";
521   msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname);
522   luaK_semerror(ls, msg);  /* raise the error */
523 }
524 
525 
526 /*
527 ** Solves the goto at index 'g' to given 'label' and removes it
528 ** from the list of pending gotos.
529 ** If it jumps into the scope of some variable, raises an error.
530 */
solvegoto(LexState * ls,int g,Labeldesc * label)531 static void solvegoto (LexState *ls, int g, Labeldesc *label) {
532   int i;
533   Labellist *gl = &ls->dyd->gt;  /* list of gotos */
534   Labeldesc *gt = &gl->arr[g];  /* goto to be resolved */
535   lua_assert(eqstr(gt->name, label->name));
536   if (l_unlikely(gt->nactvar < label->nactvar))  /* enter some scope? */
537     jumpscopeerror(ls, gt);
538   luaK_patchlist(ls->fs, gt->pc, label->pc);
539   for (i = g; i < gl->n - 1; i++)  /* remove goto from pending list */
540     gl->arr[i] = gl->arr[i + 1];
541   gl->n--;
542 }
543 
544 
545 /*
546 ** Search for an active label with the given name.
547 */
findlabel(LexState * ls,TString * name)548 static Labeldesc *findlabel (LexState *ls, TString *name) {
549   int i;
550   Dyndata *dyd = ls->dyd;
551   /* check labels in current function for a match */
552   for (i = ls->fs->firstlabel; i < dyd->label.n; i++) {
553     Labeldesc *lb = &dyd->label.arr[i];
554     if (eqstr(lb->name, name))  /* correct label? */
555       return lb;
556   }
557   return NULL;  /* label not found */
558 }
559 
560 
561 /*
562 ** Adds a new label/goto in the corresponding list.
563 */
newlabelentry(LexState * ls,Labellist * l,TString * name,int line,int pc)564 static int newlabelentry (LexState *ls, Labellist *l, TString *name,
565                           int line, int pc) {
566   int n = l->n;
567   luaM_growvector(ls->L, l->arr, n, l->size,
568                   Labeldesc, SHRT_MAX, "labels/gotos");
569   l->arr[n].name = name;
570   l->arr[n].line = line;
571   l->arr[n].nactvar = ls->fs->nactvar;
572   l->arr[n].close = 0;
573   l->arr[n].pc = pc;
574   l->n = n + 1;
575   return n;
576 }
577 
578 
newgotoentry(LexState * ls,TString * name,int line,int pc)579 static int newgotoentry (LexState *ls, TString *name, int line, int pc) {
580   return newlabelentry(ls, &ls->dyd->gt, name, line, pc);
581 }
582 
583 
584 /*
585 ** Solves forward jumps. Check whether new label 'lb' matches any
586 ** pending gotos in current block and solves them. Return true
587 ** if any of the gotos need to close upvalues.
588 */
solvegotos(LexState * ls,Labeldesc * lb)589 static int solvegotos (LexState *ls, Labeldesc *lb) {
590   Labellist *gl = &ls->dyd->gt;
591   int i = ls->fs->bl->firstgoto;
592   int needsclose = 0;
593   while (i < gl->n) {
594     if (eqstr(gl->arr[i].name, lb->name)) {
595       needsclose |= gl->arr[i].close;
596       solvegoto(ls, i, lb);  /* will remove 'i' from the list */
597     }
598     else
599       i++;
600   }
601   return needsclose;
602 }
603 
604 
605 /*
606 ** Create a new label with the given 'name' at the given 'line'.
607 ** 'last' tells whether label is the last non-op statement in its
608 ** block. Solves all pending gotos to this new label and adds
609 ** a close instruction if necessary.
610 ** Returns true iff it added a close instruction.
611 */
createlabel(LexState * ls,TString * name,int line,int last)612 static int createlabel (LexState *ls, TString *name, int line,
613                         int last) {
614   FuncState *fs = ls->fs;
615   Labellist *ll = &ls->dyd->label;
616   int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs));
617   if (last) {  /* label is last no-op statement in the block? */
618     /* assume that locals are already out of scope */
619     ll->arr[l].nactvar = fs->bl->nactvar;
620   }
621   if (solvegotos(ls, &ll->arr[l])) {  /* need close? */
622     luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0);
623     return 1;
624   }
625   return 0;
626 }
627 
628 
629 /*
630 ** Adjust pending gotos to outer level of a block.
631 */
movegotosout(FuncState * fs,BlockCnt * bl)632 static void movegotosout (FuncState *fs, BlockCnt *bl) {
633   int i;
634   Labellist *gl = &fs->ls->dyd->gt;
635   /* correct pending gotos to current block */
636   for (i = bl->firstgoto; i < gl->n; i++) {  /* for each pending goto */
637     Labeldesc *gt = &gl->arr[i];
638     /* leaving a variable scope? */
639     if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar))
640       gt->close |= bl->upval;  /* jump may need a close */
641     gt->nactvar = bl->nactvar;  /* update goto level */
642   }
643 }
644 
645 
enterblock(FuncState * fs,BlockCnt * bl,lu_byte isloop)646 static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
647   bl->isloop = isloop;
648   bl->nactvar = fs->nactvar;
649   bl->firstlabel = fs->ls->dyd->label.n;
650   bl->firstgoto = fs->ls->dyd->gt.n;
651   bl->upval = 0;
652   bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc);
653   bl->previous = fs->bl;
654   fs->bl = bl;
655   lua_assert(fs->freereg == luaY_nvarstack(fs));
656 }
657 
658 
659 /*
660 ** generates an error for an undefined 'goto'.
661 */
undefgoto(LexState * ls,Labeldesc * gt)662 static l_noret undefgoto (LexState *ls, Labeldesc *gt) {
663   const char *msg;
664   if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) {
665     msg = "break outside loop at line %d";
666     msg = luaO_pushfstring(ls->L, msg, gt->line);
667   }
668   else {
669     msg = "no visible label '%s' for <goto> at line %d";
670     msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
671   }
672   luaK_semerror(ls, msg);
673 }
674 
675 
leaveblock(FuncState * fs)676 static void leaveblock (FuncState *fs) {
677   BlockCnt *bl = fs->bl;
678   LexState *ls = fs->ls;
679   int hasclose = 0;
680   int stklevel = reglevel(fs, bl->nactvar);  /* level outside the block */
681   removevars(fs, bl->nactvar);  /* remove block locals */
682   lua_assert(bl->nactvar == fs->nactvar);  /* back to level on entry */
683   if (bl->isloop)  /* has to fix pending breaks? */
684     hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0);
685   if (!hasclose && bl->previous && bl->upval)  /* still need a 'close'? */
686     luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0);
687   fs->freereg = stklevel;  /* free registers */
688   ls->dyd->label.n = bl->firstlabel;  /* remove local labels */
689   fs->bl = bl->previous;  /* current block now is previous one */
690   if (bl->previous)  /* was it a nested block? */
691     movegotosout(fs, bl);  /* update pending gotos to enclosing block */
692   else {
693     if (bl->firstgoto < ls->dyd->gt.n)  /* still pending gotos? */
694       undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]);  /* error */
695   }
696 }
697 
698 
699 /*
700 ** adds a new prototype into list of prototypes
701 */
addprototype(LexState * ls)702 static Proto *addprototype (LexState *ls) {
703   Proto *clp;
704   lua_State *L = ls->L;
705   FuncState *fs = ls->fs;
706   Proto *f = fs->f;  /* prototype of current function */
707   if (fs->np >= f->sizep) {
708     int oldsize = f->sizep;
709     luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
710     while (oldsize < f->sizep)
711       f->p[oldsize++] = NULL;
712   }
713   f->p[fs->np++] = clp = luaF_newproto(L);
714   luaC_objbarrier(L, f, clp);
715   return clp;
716 }
717 
718 
719 /*
720 ** codes instruction to create new closure in parent function.
721 ** The OP_CLOSURE instruction uses the last available register,
722 ** so that, if it invokes the GC, the GC knows which registers
723 ** are in use at that time.
724 
725 */
codeclosure(LexState * ls,expdesc * v)726 static void codeclosure (LexState *ls, expdesc *v) {
727   FuncState *fs = ls->fs->prev;
728   init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
729   luaK_exp2nextreg(fs, v);  /* fix it at the last register */
730 }
731 
732 
open_func(LexState * ls,FuncState * fs,BlockCnt * bl)733 static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
734   Proto *f = fs->f;
735   fs->prev = ls->fs;  /* linked list of funcstates */
736   fs->ls = ls;
737   ls->fs = fs;
738   fs->pc = 0;
739   fs->previousline = f->linedefined;
740   fs->iwthabs = 0;
741   fs->lasttarget = 0;
742   fs->freereg = 0;
743   fs->nk = 0;
744   fs->nabslineinfo = 0;
745   fs->np = 0;
746   fs->nups = 0;
747   fs->ndebugvars = 0;
748   fs->nactvar = 0;
749   fs->needclose = 0;
750   fs->firstlocal = ls->dyd->actvar.n;
751   fs->firstlabel = ls->dyd->label.n;
752   fs->bl = NULL;
753   f->source = ls->source;
754   luaC_objbarrier(ls->L, f, f->source);
755   f->maxstacksize = 2;  /* registers 0/1 are always valid */
756   enterblock(fs, bl, 0);
757 }
758 
759 
close_func(LexState * ls)760 static void close_func (LexState *ls) {
761   lua_State *L = ls->L;
762   FuncState *fs = ls->fs;
763   Proto *f = fs->f;
764   luaK_ret(fs, luaY_nvarstack(fs), 0);  /* final return */
765   leaveblock(fs);
766   lua_assert(fs->bl == NULL);
767   luaK_finish(fs);
768   luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction);
769   luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte);
770   luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo,
771                        fs->nabslineinfo, AbsLineInfo);
772   luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue);
773   luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *);
774   luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar);
775   luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
776   ls->fs = fs->prev;
777   luaC_checkGC(L);
778 }
779 
780 
781 
782 /*============================================================*/
783 /* GRAMMAR RULES */
784 /*============================================================*/
785 
786 
787 /*
788 ** check whether current token is in the follow set of a block.
789 ** 'until' closes syntactical blocks, but do not close scope,
790 ** so it is handled in separate.
791 */
block_follow(LexState * ls,int withuntil)792 static int block_follow (LexState *ls, int withuntil) {
793   switch (ls->t.token) {
794     case TK_ELSE: case TK_ELSEIF:
795     case TK_END: case TK_EOS:
796       return 1;
797     case TK_UNTIL: return withuntil;
798     default: return 0;
799   }
800 }
801 
802 
statlist(LexState * ls)803 static void statlist (LexState *ls) {
804   /* statlist -> { stat [';'] } */
805   while (!block_follow(ls, 1)) {
806     if (ls->t.token == TK_RETURN) {
807       statement(ls);
808       return;  /* 'return' must be last statement */
809     }
810     statement(ls);
811   }
812 }
813 
814 
fieldsel(LexState * ls,expdesc * v)815 static void fieldsel (LexState *ls, expdesc *v) {
816   /* fieldsel -> ['.' | ':'] NAME */
817   FuncState *fs = ls->fs;
818   expdesc key;
819   luaK_exp2anyregup(fs, v);
820   luaX_next(ls);  /* skip the dot or colon */
821   codename(ls, &key);
822   luaK_indexed(fs, v, &key);
823 }
824 
825 
yindex(LexState * ls,expdesc * v)826 static void yindex (LexState *ls, expdesc *v) {
827   /* index -> '[' expr ']' */
828   luaX_next(ls);  /* skip the '[' */
829   expr(ls, v);
830   luaK_exp2val(ls->fs, v);
831   checknext(ls, ']');
832 }
833 
834 
835 /*
836 ** {======================================================================
837 ** Rules for Constructors
838 ** =======================================================================
839 */
840 
841 
842 typedef struct ConsControl {
843   expdesc v;  /* last list item read */
844   expdesc *t;  /* table descriptor */
845   int nh;  /* total number of 'record' elements */
846   int na;  /* number of array elements already stored */
847   int tostore;  /* number of array elements pending to be stored */
848 } ConsControl;
849 
850 
recfield(LexState * ls,ConsControl * cc)851 static void recfield (LexState *ls, ConsControl *cc) {
852   /* recfield -> (NAME | '['exp']') = exp */
853   FuncState *fs = ls->fs;
854   int reg = ls->fs->freereg;
855   expdesc tab, key, val;
856   if (ls->t.token == TK_NAME) {
857     checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
858     codename(ls, &key);
859   }
860   else  /* ls->t.token == '[' */
861     yindex(ls, &key);
862   cc->nh++;
863   checknext(ls, '=');
864   tab = *cc->t;
865   luaK_indexed(fs, &tab, &key);
866   expr(ls, &val);
867   luaK_storevar(fs, &tab, &val);
868   fs->freereg = reg;  /* free registers */
869 }
870 
871 
closelistfield(FuncState * fs,ConsControl * cc)872 static void closelistfield (FuncState *fs, ConsControl *cc) {
873   if (cc->v.k == VVOID) return;  /* there is no list item */
874   luaK_exp2nextreg(fs, &cc->v);
875   cc->v.k = VVOID;
876   if (cc->tostore == LFIELDS_PER_FLUSH) {
877     luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);  /* flush */
878     cc->na += cc->tostore;
879     cc->tostore = 0;  /* no more items pending */
880   }
881 }
882 
883 
lastlistfield(FuncState * fs,ConsControl * cc)884 static void lastlistfield (FuncState *fs, ConsControl *cc) {
885   if (cc->tostore == 0) return;
886   if (hasmultret(cc->v.k)) {
887     luaK_setmultret(fs, &cc->v);
888     luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET);
889     cc->na--;  /* do not count last expression (unknown number of elements) */
890   }
891   else {
892     if (cc->v.k != VVOID)
893       luaK_exp2nextreg(fs, &cc->v);
894     luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);
895   }
896   cc->na += cc->tostore;
897 }
898 
899 
listfield(LexState * ls,ConsControl * cc)900 static void listfield (LexState *ls, ConsControl *cc) {
901   /* listfield -> exp */
902   expr(ls, &cc->v);
903   cc->tostore++;
904 }
905 
906 
field(LexState * ls,ConsControl * cc)907 static void field (LexState *ls, ConsControl *cc) {
908   /* field -> listfield | recfield */
909   switch(ls->t.token) {
910     case TK_NAME: {  /* may be 'listfield' or 'recfield' */
911       if (luaX_lookahead(ls) != '=')  /* expression? */
912         listfield(ls, cc);
913       else
914         recfield(ls, cc);
915       break;
916     }
917     case '[': {
918       recfield(ls, cc);
919       break;
920     }
921     default: {
922       listfield(ls, cc);
923       break;
924     }
925   }
926 }
927 
928 
constructor(LexState * ls,expdesc * t)929 static void constructor (LexState *ls, expdesc *t) {
930   /* constructor -> '{' [ field { sep field } [sep] ] '}'
931      sep -> ',' | ';' */
932   FuncState *fs = ls->fs;
933   int line = ls->linenumber;
934   int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
935   ConsControl cc;
936   luaK_code(fs, 0);  /* space for extra arg. */
937   cc.na = cc.nh = cc.tostore = 0;
938   cc.t = t;
939   init_exp(t, VNONRELOC, fs->freereg);  /* table will be at stack top */
940   luaK_reserveregs(fs, 1);
941   init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
942   checknext(ls, '{');
943   do {
944     lua_assert(cc.v.k == VVOID || cc.tostore > 0);
945     if (ls->t.token == '}') break;
946     closelistfield(fs, &cc);
947     field(ls, &cc);
948   } while (testnext(ls, ',') || testnext(ls, ';'));
949   check_match(ls, '}', '{', line);
950   lastlistfield(fs, &cc);
951   luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh);
952 }
953 
954 /* }====================================================================== */
955 
956 
setvararg(FuncState * fs,int nparams)957 static void setvararg (FuncState *fs, int nparams) {
958   fs->f->is_vararg = 1;
959   luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0);
960 }
961 
962 
parlist(LexState * ls)963 static void parlist (LexState *ls) {
964   /* parlist -> [ {NAME ','} (NAME | '...') ] */
965   FuncState *fs = ls->fs;
966   Proto *f = fs->f;
967   int nparams = 0;
968   int isvararg = 0;
969   if (ls->t.token != ')') {  /* is 'parlist' not empty? */
970     do {
971       switch (ls->t.token) {
972         case TK_NAME: {
973           new_localvar(ls, str_checkname(ls));
974           nparams++;
975           break;
976         }
977         case TK_DOTS: {
978           luaX_next(ls);
979           isvararg = 1;
980           break;
981         }
982         default: luaX_syntaxerror(ls, "<name> or '...' expected");
983       }
984     } while (!isvararg && testnext(ls, ','));
985   }
986   adjustlocalvars(ls, nparams);
987   f->numparams = cast_byte(fs->nactvar);
988   if (isvararg)
989     setvararg(fs, f->numparams);  /* declared vararg */
990   luaK_reserveregs(fs, fs->nactvar);  /* reserve registers for parameters */
991 }
992 
993 
body(LexState * ls,expdesc * e,int ismethod,int line)994 static void body (LexState *ls, expdesc *e, int ismethod, int line) {
995   /* body ->  '(' parlist ')' block END */
996   FuncState new_fs;
997   BlockCnt bl;
998   new_fs.f = addprototype(ls);
999   new_fs.f->linedefined = line;
1000   open_func(ls, &new_fs, &bl);
1001   checknext(ls, '(');
1002   if (ismethod) {
1003     new_localvarliteral(ls, "self");  /* create 'self' parameter */
1004     adjustlocalvars(ls, 1);
1005   }
1006   parlist(ls);
1007   checknext(ls, ')');
1008   statlist(ls);
1009   new_fs.f->lastlinedefined = ls->linenumber;
1010   check_match(ls, TK_END, TK_FUNCTION, line);
1011   codeclosure(ls, e);
1012   close_func(ls);
1013 }
1014 
1015 
explist(LexState * ls,expdesc * v)1016 static int explist (LexState *ls, expdesc *v) {
1017   /* explist -> expr { ',' expr } */
1018   int n = 1;  /* at least one expression */
1019   expr(ls, v);
1020   while (testnext(ls, ',')) {
1021     luaK_exp2nextreg(ls->fs, v);
1022     expr(ls, v);
1023     n++;
1024   }
1025   return n;
1026 }
1027 
1028 
funcargs(LexState * ls,expdesc * f,int line)1029 static void funcargs (LexState *ls, expdesc *f, int line) {
1030   FuncState *fs = ls->fs;
1031   expdesc args;
1032   int base, nparams;
1033   switch (ls->t.token) {
1034     case '(': {  /* funcargs -> '(' [ explist ] ')' */
1035       luaX_next(ls);
1036       if (ls->t.token == ')')  /* arg list is empty? */
1037         args.k = VVOID;
1038       else {
1039         explist(ls, &args);
1040         if (hasmultret(args.k))
1041           luaK_setmultret(fs, &args);
1042       }
1043       check_match(ls, ')', '(', line);
1044       break;
1045     }
1046     case '{': {  /* funcargs -> constructor */
1047       constructor(ls, &args);
1048       break;
1049     }
1050     case TK_STRING: {  /* funcargs -> STRING */
1051       codestring(&args, ls->t.seminfo.ts);
1052       luaX_next(ls);  /* must use 'seminfo' before 'next' */
1053       break;
1054     }
1055     default: {
1056       luaX_syntaxerror(ls, "function arguments expected");
1057     }
1058   }
1059   lua_assert(f->k == VNONRELOC);
1060   base = f->u.info;  /* base register for call */
1061   if (hasmultret(args.k))
1062     nparams = LUA_MULTRET;  /* open call */
1063   else {
1064     if (args.k != VVOID)
1065       luaK_exp2nextreg(fs, &args);  /* close last argument */
1066     nparams = fs->freereg - (base+1);
1067   }
1068   init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
1069   luaK_fixline(fs, line);
1070   fs->freereg = base+1;  /* call remove function and arguments and leaves
1071                             (unless changed) one result */
1072 }
1073 
1074 
1075 
1076 
1077 /*
1078 ** {======================================================================
1079 ** Expression parsing
1080 ** =======================================================================
1081 */
1082 
1083 
primaryexp(LexState * ls,expdesc * v)1084 static void primaryexp (LexState *ls, expdesc *v) {
1085   /* primaryexp -> NAME | '(' expr ')' */
1086   switch (ls->t.token) {
1087     case '(': {
1088       int line = ls->linenumber;
1089       luaX_next(ls);
1090       expr(ls, v);
1091       check_match(ls, ')', '(', line);
1092       luaK_dischargevars(ls->fs, v);
1093       return;
1094     }
1095     case TK_NAME: {
1096       singlevar(ls, v);
1097       return;
1098     }
1099     default: {
1100       luaX_syntaxerror(ls, "unexpected symbol");
1101     }
1102   }
1103 }
1104 
1105 
suffixedexp(LexState * ls,expdesc * v)1106 static void suffixedexp (LexState *ls, expdesc *v) {
1107   /* suffixedexp ->
1108        primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
1109   FuncState *fs = ls->fs;
1110   int line = ls->linenumber;
1111   primaryexp(ls, v);
1112   for (;;) {
1113     switch (ls->t.token) {
1114       case '.': {  /* fieldsel */
1115         fieldsel(ls, v);
1116         break;
1117       }
1118       case '[': {  /* '[' exp ']' */
1119         expdesc key;
1120         luaK_exp2anyregup(fs, v);
1121         yindex(ls, &key);
1122         luaK_indexed(fs, v, &key);
1123         break;
1124       }
1125       case ':': {  /* ':' NAME funcargs */
1126         expdesc key;
1127         luaX_next(ls);
1128         codename(ls, &key);
1129         luaK_self(fs, v, &key);
1130         funcargs(ls, v, line);
1131         break;
1132       }
1133       case '(': case TK_STRING: case '{': {  /* funcargs */
1134         luaK_exp2nextreg(fs, v);
1135         funcargs(ls, v, line);
1136         break;
1137       }
1138       default: return;
1139     }
1140   }
1141 }
1142 
1143 
simpleexp(LexState * ls,expdesc * v)1144 static void simpleexp (LexState *ls, expdesc *v) {
1145   /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... |
1146                   constructor | FUNCTION body | suffixedexp */
1147   switch (ls->t.token) {
1148 #ifndef _KERNEL
1149     case TK_FLT: {
1150       init_exp(v, VKFLT, 0);
1151       v->u.nval = ls->t.seminfo.r;
1152       break;
1153     }
1154 #endif /* _KERNEL */
1155     case TK_INT: {
1156       init_exp(v, VKINT, 0);
1157       v->u.ival = ls->t.seminfo.i;
1158       break;
1159     }
1160     case TK_STRING: {
1161       codestring(v, ls->t.seminfo.ts);
1162       break;
1163     }
1164     case TK_NIL: {
1165       init_exp(v, VNIL, 0);
1166       break;
1167     }
1168     case TK_TRUE: {
1169       init_exp(v, VTRUE, 0);
1170       break;
1171     }
1172     case TK_FALSE: {
1173       init_exp(v, VFALSE, 0);
1174       break;
1175     }
1176     case TK_DOTS: {  /* vararg */
1177       FuncState *fs = ls->fs;
1178       check_condition(ls, fs->f->is_vararg,
1179                       "cannot use '...' outside a vararg function");
1180       init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1));
1181       break;
1182     }
1183     case '{': {  /* constructor */
1184       constructor(ls, v);
1185       return;
1186     }
1187     case TK_FUNCTION: {
1188       luaX_next(ls);
1189       body(ls, v, 0, ls->linenumber);
1190       return;
1191     }
1192     default: {
1193       suffixedexp(ls, v);
1194       return;
1195     }
1196   }
1197   luaX_next(ls);
1198 }
1199 
1200 
getunopr(int op)1201 static UnOpr getunopr (int op) {
1202   switch (op) {
1203     case TK_NOT: return OPR_NOT;
1204     case '-': return OPR_MINUS;
1205     case '~': return OPR_BNOT;
1206     case '#': return OPR_LEN;
1207     default: return OPR_NOUNOPR;
1208   }
1209 }
1210 
1211 
getbinopr(int op)1212 static BinOpr getbinopr (int op) {
1213   switch (op) {
1214     case '+': return OPR_ADD;
1215     case '-': return OPR_SUB;
1216     case '*': return OPR_MUL;
1217     case '%': return OPR_MOD;
1218 #ifndef _KERNEL
1219     case '^': return OPR_POW;
1220     case '/': return OPR_DIV;
1221 #else /* _KERNEL */
1222     case '/': return OPR_IDIV;
1223 #endif /* _KERNEL */
1224     case TK_IDIV: return OPR_IDIV;
1225     case '&': return OPR_BAND;
1226     case '|': return OPR_BOR;
1227     case '~': return OPR_BXOR;
1228     case TK_SHL: return OPR_SHL;
1229     case TK_SHR: return OPR_SHR;
1230     case TK_CONCAT: return OPR_CONCAT;
1231     case TK_NE: return OPR_NE;
1232     case TK_EQ: return OPR_EQ;
1233     case '<': return OPR_LT;
1234     case TK_LE: return OPR_LE;
1235     case '>': return OPR_GT;
1236     case TK_GE: return OPR_GE;
1237     case TK_AND: return OPR_AND;
1238     case TK_OR: return OPR_OR;
1239     default: return OPR_NOBINOPR;
1240   }
1241 }
1242 
1243 
1244 /*
1245 ** Priority table for binary operators.
1246 */
1247 static const struct {
1248   lu_byte left;  /* left priority for each binary operator */
1249   lu_byte right; /* right priority */
1250 } priority[] = {  /* ORDER OPR */
1251    {10, 10}, {10, 10},           /* '+' '-' */
1252    {11, 11}, {11, 11},           /* '*' '%' */
1253 #ifndef _KERNEL
1254    {14, 13},                  /* '^' (right associative) */
1255    {11, 11}, {11, 11},           /* '/' '//' */
1256 #else /* _KERNEL */
1257    {11, 11},                  /* '//' */
1258 #endif /* _KERNEL */
1259    {6, 6}, {4, 4}, {5, 5},   /* '&' '|' '~' */
1260    {7, 7}, {7, 7},           /* '<<' '>>' */
1261    {9, 8},                   /* '..' (right associative) */
1262    {3, 3}, {3, 3}, {3, 3},   /* ==, <, <= */
1263    {3, 3}, {3, 3}, {3, 3},   /* ~=, >, >= */
1264    {2, 2}, {1, 1}            /* and, or */
1265 };
1266 
1267 #define UNARY_PRIORITY	12  /* priority for unary operators */
1268 
1269 
1270 /*
1271 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
1272 ** where 'binop' is any binary operator with a priority higher than 'limit'
1273 */
subexpr(LexState * ls,expdesc * v,int limit)1274 static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
1275   BinOpr op;
1276   UnOpr uop;
1277   enterlevel(ls);
1278   uop = getunopr(ls->t.token);
1279   if (uop != OPR_NOUNOPR) {  /* prefix (unary) operator? */
1280     int line = ls->linenumber;
1281     luaX_next(ls);  /* skip operator */
1282     subexpr(ls, v, UNARY_PRIORITY);
1283     luaK_prefix(ls->fs, uop, v, line);
1284   }
1285   else simpleexp(ls, v);
1286   /* expand while operators have priorities higher than 'limit' */
1287   op = getbinopr(ls->t.token);
1288   while (op != OPR_NOBINOPR && priority[op].left > limit) {
1289     expdesc v2;
1290     BinOpr nextop;
1291     int line = ls->linenumber;
1292     luaX_next(ls);  /* skip operator */
1293     luaK_infix(ls->fs, op, v);
1294     /* read sub-expression with higher priority */
1295     nextop = subexpr(ls, &v2, priority[op].right);
1296     luaK_posfix(ls->fs, op, v, &v2, line);
1297     op = nextop;
1298   }
1299   leavelevel(ls);
1300   return op;  /* return first untreated operator */
1301 }
1302 
1303 
expr(LexState * ls,expdesc * v)1304 static void expr (LexState *ls, expdesc *v) {
1305   subexpr(ls, v, 0);
1306 }
1307 
1308 /* }==================================================================== */
1309 
1310 
1311 
1312 /*
1313 ** {======================================================================
1314 ** Rules for Statements
1315 ** =======================================================================
1316 */
1317 
1318 
block(LexState * ls)1319 static void block (LexState *ls) {
1320   /* block -> statlist */
1321   FuncState *fs = ls->fs;
1322   BlockCnt bl;
1323   enterblock(fs, &bl, 0);
1324   statlist(ls);
1325   leaveblock(fs);
1326 }
1327 
1328 
1329 /*
1330 ** structure to chain all variables in the left-hand side of an
1331 ** assignment
1332 */
1333 struct LHS_assign {
1334   struct LHS_assign *prev;
1335   expdesc v;  /* variable (global, local, upvalue, or indexed) */
1336 };
1337 
1338 
1339 /*
1340 ** check whether, in an assignment to an upvalue/local variable, the
1341 ** upvalue/local variable is begin used in a previous assignment to a
1342 ** table. If so, save original upvalue/local value in a safe place and
1343 ** use this safe copy in the previous assignment.
1344 */
check_conflict(LexState * ls,struct LHS_assign * lh,expdesc * v)1345 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
1346   FuncState *fs = ls->fs;
1347   int extra = fs->freereg;  /* eventual position to save local variable */
1348   int conflict = 0;
1349   for (; lh; lh = lh->prev) {  /* check all previous assignments */
1350     if (vkisindexed(lh->v.k)) {  /* assignment to table field? */
1351       if (lh->v.k == VINDEXUP) {  /* is table an upvalue? */
1352         if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) {
1353           conflict = 1;  /* table is the upvalue being assigned now */
1354           lh->v.k = VINDEXSTR;
1355           lh->v.u.ind.t = extra;  /* assignment will use safe copy */
1356         }
1357       }
1358       else {  /* table is a register */
1359         if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) {
1360           conflict = 1;  /* table is the local being assigned now */
1361           lh->v.u.ind.t = extra;  /* assignment will use safe copy */
1362         }
1363         /* is index the local being assigned? */
1364         if (lh->v.k == VINDEXED && v->k == VLOCAL &&
1365             lh->v.u.ind.idx == v->u.var.ridx) {
1366           conflict = 1;
1367           lh->v.u.ind.idx = extra;  /* previous assignment will use safe copy */
1368         }
1369       }
1370     }
1371   }
1372   if (conflict) {
1373     /* copy upvalue/local value to a temporary (in position 'extra') */
1374     if (v->k == VLOCAL)
1375       luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0);
1376     else
1377       luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0);
1378     luaK_reserveregs(fs, 1);
1379   }
1380 }
1381 
1382 /*
1383 ** Parse and compile a multiple assignment. The first "variable"
1384 ** (a 'suffixedexp') was already read by the caller.
1385 **
1386 ** assignment -> suffixedexp restassign
1387 ** restassign -> ',' suffixedexp restassign | '=' explist
1388 */
restassign(LexState * ls,struct LHS_assign * lh,int nvars)1389 static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) {
1390   expdesc e;
1391   check_condition(ls, vkisvar(lh->v.k), "syntax error");
1392   check_readonly(ls, &lh->v);
1393   if (testnext(ls, ',')) {  /* restassign -> ',' suffixedexp restassign */
1394     struct LHS_assign nv;
1395     nv.prev = lh;
1396     suffixedexp(ls, &nv.v);
1397     if (!vkisindexed(nv.v.k))
1398       check_conflict(ls, lh, &nv.v);
1399     enterlevel(ls);  /* control recursion depth */
1400     restassign(ls, &nv, nvars+1);
1401     leavelevel(ls);
1402   }
1403   else {  /* restassign -> '=' explist */
1404     int nexps;
1405     checknext(ls, '=');
1406     nexps = explist(ls, &e);
1407     if (nexps != nvars)
1408       adjust_assign(ls, nvars, nexps, &e);
1409     else {
1410       luaK_setoneret(ls->fs, &e);  /* close last expression */
1411       luaK_storevar(ls->fs, &lh->v, &e);
1412       return;  /* avoid default */
1413     }
1414   }
1415   init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
1416   luaK_storevar(ls->fs, &lh->v, &e);
1417 }
1418 
1419 
cond(LexState * ls)1420 static int cond (LexState *ls) {
1421   /* cond -> exp */
1422   expdesc v;
1423   expr(ls, &v);  /* read condition */
1424   if (v.k == VNIL) v.k = VFALSE;  /* 'falses' are all equal here */
1425   luaK_goiftrue(ls->fs, &v);
1426   return v.f;
1427 }
1428 
1429 
gotostat(LexState * ls)1430 static void gotostat (LexState *ls) {
1431   FuncState *fs = ls->fs;
1432   int line = ls->linenumber;
1433   TString *name = str_checkname(ls);  /* label's name */
1434   Labeldesc *lb = findlabel(ls, name);
1435   if (lb == NULL)  /* no label? */
1436     /* forward jump; will be resolved when the label is declared */
1437     newgotoentry(ls, name, line, luaK_jump(fs));
1438   else {  /* found a label */
1439     /* backward jump; will be resolved here */
1440     int lblevel = reglevel(fs, lb->nactvar);  /* label level */
1441     if (luaY_nvarstack(fs) > lblevel)  /* leaving the scope of a variable? */
1442       luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0);
1443     /* create jump and link it to the label */
1444     luaK_patchlist(fs, luaK_jump(fs), lb->pc);
1445   }
1446 }
1447 
1448 
1449 /*
1450 ** Break statement. Semantically equivalent to "goto break".
1451 */
breakstat(LexState * ls)1452 static void breakstat (LexState *ls) {
1453   int line = ls->linenumber;
1454   luaX_next(ls);  /* skip break */
1455   newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs));
1456 }
1457 
1458 
1459 /*
1460 ** Check whether there is already a label with the given 'name'.
1461 */
checkrepeated(LexState * ls,TString * name)1462 static void checkrepeated (LexState *ls, TString *name) {
1463   Labeldesc *lb = findlabel(ls, name);
1464   if (l_unlikely(lb != NULL)) {  /* already defined? */
1465     const char *msg = "label '%s' already defined on line %d";
1466     msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line);
1467     luaK_semerror(ls, msg);  /* error */
1468   }
1469 }
1470 
1471 
labelstat(LexState * ls,TString * name,int line)1472 static void labelstat (LexState *ls, TString *name, int line) {
1473   /* label -> '::' NAME '::' */
1474   checknext(ls, TK_DBCOLON);  /* skip double colon */
1475   while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
1476     statement(ls);  /* skip other no-op statements */
1477   checkrepeated(ls, name);  /* check for repeated labels */
1478   createlabel(ls, name, line, block_follow(ls, 0));
1479 }
1480 
1481 
whilestat(LexState * ls,int line)1482 static void whilestat (LexState *ls, int line) {
1483   /* whilestat -> WHILE cond DO block END */
1484   FuncState *fs = ls->fs;
1485   int whileinit;
1486   int condexit;
1487   BlockCnt bl;
1488   luaX_next(ls);  /* skip WHILE */
1489   whileinit = luaK_getlabel(fs);
1490   condexit = cond(ls);
1491   enterblock(fs, &bl, 1);
1492   checknext(ls, TK_DO);
1493   block(ls);
1494   luaK_jumpto(fs, whileinit);
1495   check_match(ls, TK_END, TK_WHILE, line);
1496   leaveblock(fs);
1497   luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */
1498 }
1499 
1500 
repeatstat(LexState * ls,int line)1501 static void repeatstat (LexState *ls, int line) {
1502   /* repeatstat -> REPEAT block UNTIL cond */
1503   int condexit;
1504   FuncState *fs = ls->fs;
1505   int repeat_init = luaK_getlabel(fs);
1506   BlockCnt bl1, bl2;
1507   enterblock(fs, &bl1, 1);  /* loop block */
1508   enterblock(fs, &bl2, 0);  /* scope block */
1509   luaX_next(ls);  /* skip REPEAT */
1510   statlist(ls);
1511   check_match(ls, TK_UNTIL, TK_REPEAT, line);
1512   condexit = cond(ls);  /* read condition (inside scope block) */
1513   leaveblock(fs);  /* finish scope */
1514   if (bl2.upval) {  /* upvalues? */
1515     int exit = luaK_jump(fs);  /* normal exit must jump over fix */
1516     luaK_patchtohere(fs, condexit);  /* repetition must close upvalues */
1517     luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0);
1518     condexit = luaK_jump(fs);  /* repeat after closing upvalues */
1519     luaK_patchtohere(fs, exit);  /* normal exit comes to here */
1520   }
1521   luaK_patchlist(fs, condexit, repeat_init);  /* close the loop */
1522   leaveblock(fs);  /* finish loop */
1523 }
1524 
1525 
1526 /*
1527 ** Read an expression and generate code to put its results in next
1528 ** stack slot.
1529 **
1530 */
exp1(LexState * ls)1531 static void exp1 (LexState *ls) {
1532   expdesc e;
1533   expr(ls, &e);
1534   luaK_exp2nextreg(ls->fs, &e);
1535   lua_assert(e.k == VNONRELOC);
1536 }
1537 
1538 
1539 /*
1540 ** Fix for instruction at position 'pc' to jump to 'dest'.
1541 ** (Jump addresses are relative in Lua). 'back' true means
1542 ** a back jump.
1543 */
fixforjump(FuncState * fs,int pc,int dest,int back)1544 static void fixforjump (FuncState *fs, int pc, int dest, int back) {
1545   Instruction *jmp = &fs->f->code[pc];
1546   int offset = dest - (pc + 1);
1547   if (back)
1548     offset = -offset;
1549   if (l_unlikely(offset > MAXARG_Bx))
1550     luaX_syntaxerror(fs->ls, "control structure too long");
1551   SETARG_Bx(*jmp, offset);
1552 }
1553 
1554 
1555 /*
1556 ** Generate code for a 'for' loop.
1557 */
forbody(LexState * ls,int base,int line,int nvars,int isgen)1558 static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
1559   /* forbody -> DO block */
1560   static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP};
1561   static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP};
1562   BlockCnt bl;
1563   FuncState *fs = ls->fs;
1564   int prep, endfor;
1565   checknext(ls, TK_DO);
1566   prep = luaK_codeABx(fs, forprep[isgen], base, 0);
1567   enterblock(fs, &bl, 0);  /* scope for declared variables */
1568   adjustlocalvars(ls, nvars);
1569   luaK_reserveregs(fs, nvars);
1570   block(ls);
1571   leaveblock(fs);  /* end of scope for declared variables */
1572   fixforjump(fs, prep, luaK_getlabel(fs), 0);
1573   if (isgen) {  /* generic for? */
1574     luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
1575     luaK_fixline(fs, line);
1576   }
1577   endfor = luaK_codeABx(fs, forloop[isgen], base, 0);
1578   fixforjump(fs, endfor, prep + 1, 1);
1579   luaK_fixline(fs, line);
1580 }
1581 
1582 
fornum(LexState * ls,TString * varname,int line)1583 static void fornum (LexState *ls, TString *varname, int line) {
1584   /* fornum -> NAME = exp,exp[,exp] forbody */
1585   FuncState *fs = ls->fs;
1586   int base = fs->freereg;
1587   new_localvarliteral(ls, "(for state)");
1588   new_localvarliteral(ls, "(for state)");
1589   new_localvarliteral(ls, "(for state)");
1590   new_localvar(ls, varname);
1591   checknext(ls, '=');
1592   exp1(ls);  /* initial value */
1593   checknext(ls, ',');
1594   exp1(ls);  /* limit */
1595   if (testnext(ls, ','))
1596     exp1(ls);  /* optional step */
1597   else {  /* default step = 1 */
1598     luaK_int(fs, fs->freereg, 1);
1599     luaK_reserveregs(fs, 1);
1600   }
1601   adjustlocalvars(ls, 3);  /* control variables */
1602   forbody(ls, base, line, 1, 0);
1603 }
1604 
1605 
forlist(LexState * ls,TString * indexname)1606 static void forlist (LexState *ls, TString *indexname) {
1607   /* forlist -> NAME {,NAME} IN explist forbody */
1608   FuncState *fs = ls->fs;
1609   expdesc e;
1610   int nvars = 5;  /* gen, state, control, toclose, 'indexname' */
1611   int line;
1612   int base = fs->freereg;
1613   /* create control variables */
1614   new_localvarliteral(ls, "(for state)");
1615   new_localvarliteral(ls, "(for state)");
1616   new_localvarliteral(ls, "(for state)");
1617   new_localvarliteral(ls, "(for state)");
1618   /* create declared variables */
1619   new_localvar(ls, indexname);
1620   while (testnext(ls, ',')) {
1621     new_localvar(ls, str_checkname(ls));
1622     nvars++;
1623   }
1624   checknext(ls, TK_IN);
1625   line = ls->linenumber;
1626   adjust_assign(ls, 4, explist(ls, &e), &e);
1627   adjustlocalvars(ls, 4);  /* control variables */
1628   marktobeclosed(fs);  /* last control var. must be closed */
1629   luaK_checkstack(fs, 3);  /* extra space to call generator */
1630   forbody(ls, base, line, nvars - 4, 1);
1631 }
1632 
1633 
forstat(LexState * ls,int line)1634 static void forstat (LexState *ls, int line) {
1635   /* forstat -> FOR (fornum | forlist) END */
1636   FuncState *fs = ls->fs;
1637   TString *varname;
1638   BlockCnt bl;
1639   enterblock(fs, &bl, 1);  /* scope for loop and control variables */
1640   luaX_next(ls);  /* skip 'for' */
1641   varname = str_checkname(ls);  /* first variable name */
1642   switch (ls->t.token) {
1643     case '=': fornum(ls, varname, line); break;
1644     case ',': case TK_IN: forlist(ls, varname); break;
1645     default: luaX_syntaxerror(ls, "'=' or 'in' expected");
1646   }
1647   check_match(ls, TK_END, TK_FOR, line);
1648   leaveblock(fs);  /* loop scope ('break' jumps to this point) */
1649 }
1650 
1651 
test_then_block(LexState * ls,int * escapelist)1652 static void test_then_block (LexState *ls, int *escapelist) {
1653   /* test_then_block -> [IF | ELSEIF] cond THEN block */
1654   BlockCnt bl;
1655   FuncState *fs = ls->fs;
1656   expdesc v;
1657   int jf;  /* instruction to skip 'then' code (if condition is false) */
1658   luaX_next(ls);  /* skip IF or ELSEIF */
1659   expr(ls, &v);  /* read condition */
1660   checknext(ls, TK_THEN);
1661   if (ls->t.token == TK_BREAK) {  /* 'if x then break' ? */
1662     int line = ls->linenumber;
1663     luaK_goiffalse(ls->fs, &v);  /* will jump if condition is true */
1664     luaX_next(ls);  /* skip 'break' */
1665     enterblock(fs, &bl, 0);  /* must enter block before 'goto' */
1666     newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t);
1667     while (testnext(ls, ';')) {}  /* skip semicolons */
1668     if (block_follow(ls, 0)) {  /* jump is the entire block? */
1669       leaveblock(fs);
1670       return;  /* and that is it */
1671     }
1672     else  /* must skip over 'then' part if condition is false */
1673       jf = luaK_jump(fs);
1674   }
1675   else {  /* regular case (not a break) */
1676     luaK_goiftrue(ls->fs, &v);  /* skip over block if condition is false */
1677     enterblock(fs, &bl, 0);
1678     jf = v.f;
1679   }
1680   statlist(ls);  /* 'then' part */
1681   leaveblock(fs);
1682   if (ls->t.token == TK_ELSE ||
1683       ls->t.token == TK_ELSEIF)  /* followed by 'else'/'elseif'? */
1684     luaK_concat(fs, escapelist, luaK_jump(fs));  /* must jump over it */
1685   luaK_patchtohere(fs, jf);
1686 }
1687 
1688 
ifstat(LexState * ls,int line)1689 static void ifstat (LexState *ls, int line) {
1690   /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
1691   FuncState *fs = ls->fs;
1692   int escapelist = NO_JUMP;  /* exit list for finished parts */
1693   test_then_block(ls, &escapelist);  /* IF cond THEN block */
1694   while (ls->t.token == TK_ELSEIF)
1695     test_then_block(ls, &escapelist);  /* ELSEIF cond THEN block */
1696   if (testnext(ls, TK_ELSE))
1697     block(ls);  /* 'else' part */
1698   check_match(ls, TK_END, TK_IF, line);
1699   luaK_patchtohere(fs, escapelist);  /* patch escape list to 'if' end */
1700 }
1701 
1702 
localfunc(LexState * ls)1703 static void localfunc (LexState *ls) {
1704   expdesc b;
1705   FuncState *fs = ls->fs;
1706   int fvar = fs->nactvar;  /* function's variable index */
1707   new_localvar(ls, str_checkname(ls));  /* new local variable */
1708   adjustlocalvars(ls, 1);  /* enter its scope */
1709   body(ls, &b, 0, ls->linenumber);  /* function created in next register */
1710   /* debug information will only see the variable after this point! */
1711   localdebuginfo(fs, fvar)->startpc = fs->pc;
1712 }
1713 
1714 
getlocalattribute(LexState * ls)1715 static int getlocalattribute (LexState *ls) {
1716   /* ATTRIB -> ['<' Name '>'] */
1717   if (testnext(ls, '<')) {
1718     const char *attr = getstr(str_checkname(ls));
1719     checknext(ls, '>');
1720     if (strcmp(attr, "const") == 0)
1721       return RDKCONST;  /* read-only variable */
1722     else if (strcmp(attr, "close") == 0)
1723       return RDKTOCLOSE;  /* to-be-closed variable */
1724     else
1725       luaK_semerror(ls,
1726         luaO_pushfstring(ls->L, "unknown attribute '%s'", attr));
1727   }
1728   return VDKREG;  /* regular variable */
1729 }
1730 
1731 
checktoclose(FuncState * fs,int level)1732 static void checktoclose (FuncState *fs, int level) {
1733   if (level != -1) {  /* is there a to-be-closed variable? */
1734     marktobeclosed(fs);
1735     luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0);
1736   }
1737 }
1738 
1739 
localstat(LexState * ls)1740 static void localstat (LexState *ls) {
1741   /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */
1742   FuncState *fs = ls->fs;
1743   int toclose = -1;  /* index of to-be-closed variable (if any) */
1744   Vardesc *var;  /* last variable */
1745   int vidx, kind;  /* index and kind of last variable */
1746   int nvars = 0;
1747   int nexps;
1748   expdesc e;
1749   do {
1750     vidx = new_localvar(ls, str_checkname(ls));
1751     kind = getlocalattribute(ls);
1752     getlocalvardesc(fs, vidx)->vd.kind = kind;
1753     if (kind == RDKTOCLOSE) {  /* to-be-closed? */
1754       if (toclose != -1)  /* one already present? */
1755         luaK_semerror(ls, "multiple to-be-closed variables in local list");
1756       toclose = fs->nactvar + nvars;
1757     }
1758     nvars++;
1759   } while (testnext(ls, ','));
1760   if (testnext(ls, '='))
1761     nexps = explist(ls, &e);
1762   else {
1763     e.k = VVOID;
1764     nexps = 0;
1765   }
1766   var = getlocalvardesc(fs, vidx);  /* get last variable */
1767   if (nvars == nexps &&  /* no adjustments? */
1768       var->vd.kind == RDKCONST &&  /* last variable is const? */
1769       luaK_exp2const(fs, &e, &var->k)) {  /* compile-time constant? */
1770     var->vd.kind = RDKCTC;  /* variable is a compile-time constant */
1771     adjustlocalvars(ls, nvars - 1);  /* exclude last variable */
1772     fs->nactvar++;  /* but count it */
1773   }
1774   else {
1775     adjust_assign(ls, nvars, nexps, &e);
1776     adjustlocalvars(ls, nvars);
1777   }
1778   checktoclose(fs, toclose);
1779 }
1780 
1781 
funcname(LexState * ls,expdesc * v)1782 static int funcname (LexState *ls, expdesc *v) {
1783   /* funcname -> NAME {fieldsel} [':' NAME] */
1784   int ismethod = 0;
1785   singlevar(ls, v);
1786   while (ls->t.token == '.')
1787     fieldsel(ls, v);
1788   if (ls->t.token == ':') {
1789     ismethod = 1;
1790     fieldsel(ls, v);
1791   }
1792   return ismethod;
1793 }
1794 
1795 
funcstat(LexState * ls,int line)1796 static void funcstat (LexState *ls, int line) {
1797   /* funcstat -> FUNCTION funcname body */
1798   int ismethod;
1799   expdesc v, b;
1800   luaX_next(ls);  /* skip FUNCTION */
1801   ismethod = funcname(ls, &v);
1802   body(ls, &b, ismethod, line);
1803   check_readonly(ls, &v);
1804   luaK_storevar(ls->fs, &v, &b);
1805   luaK_fixline(ls->fs, line);  /* definition "happens" in the first line */
1806 }
1807 
1808 
exprstat(LexState * ls)1809 static void exprstat (LexState *ls) {
1810   /* stat -> func | assignment */
1811   FuncState *fs = ls->fs;
1812   struct LHS_assign v;
1813   suffixedexp(ls, &v.v);
1814   if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */
1815     v.prev = NULL;
1816     restassign(ls, &v, 1);
1817   }
1818   else {  /* stat -> func */
1819     Instruction *inst;
1820     check_condition(ls, v.v.k == VCALL, "syntax error");
1821     inst = &getinstruction(fs, &v.v);
1822     SETARG_C(*inst, 1);  /* call statement uses no results */
1823   }
1824 }
1825 
1826 
retstat(LexState * ls)1827 static void retstat (LexState *ls) {
1828   /* stat -> RETURN [explist] [';'] */
1829   FuncState *fs = ls->fs;
1830   expdesc e;
1831   int nret;  /* number of values being returned */
1832   int first = luaY_nvarstack(fs);  /* first slot to be returned */
1833   if (block_follow(ls, 1) || ls->t.token == ';')
1834     nret = 0;  /* return no values */
1835   else {
1836     nret = explist(ls, &e);  /* optional return values */
1837     if (hasmultret(e.k)) {
1838       luaK_setmultret(fs, &e);
1839       if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) {  /* tail call? */
1840         SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
1841         lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs));
1842       }
1843       nret = LUA_MULTRET;  /* return all values */
1844     }
1845     else {
1846       if (nret == 1)  /* only one single value? */
1847         first = luaK_exp2anyreg(fs, &e);  /* can use original slot */
1848       else {  /* values must go to the top of the stack */
1849         luaK_exp2nextreg(fs, &e);
1850         lua_assert(nret == fs->freereg - first);
1851       }
1852     }
1853   }
1854   luaK_ret(fs, first, nret);
1855   testnext(ls, ';');  /* skip optional semicolon */
1856 }
1857 
1858 
statement(LexState * ls)1859 static void statement (LexState *ls) {
1860   int line = ls->linenumber;  /* may be needed for error messages */
1861   enterlevel(ls);
1862   switch (ls->t.token) {
1863     case ';': {  /* stat -> ';' (empty statement) */
1864       luaX_next(ls);  /* skip ';' */
1865       break;
1866     }
1867     case TK_IF: {  /* stat -> ifstat */
1868       ifstat(ls, line);
1869       break;
1870     }
1871     case TK_WHILE: {  /* stat -> whilestat */
1872       whilestat(ls, line);
1873       break;
1874     }
1875     case TK_DO: {  /* stat -> DO block END */
1876       luaX_next(ls);  /* skip DO */
1877       block(ls);
1878       check_match(ls, TK_END, TK_DO, line);
1879       break;
1880     }
1881     case TK_FOR: {  /* stat -> forstat */
1882       forstat(ls, line);
1883       break;
1884     }
1885     case TK_REPEAT: {  /* stat -> repeatstat */
1886       repeatstat(ls, line);
1887       break;
1888     }
1889     case TK_FUNCTION: {  /* stat -> funcstat */
1890       funcstat(ls, line);
1891       break;
1892     }
1893     case TK_LOCAL: {  /* stat -> localstat */
1894       luaX_next(ls);  /* skip LOCAL */
1895       if (testnext(ls, TK_FUNCTION))  /* local function? */
1896         localfunc(ls);
1897       else
1898         localstat(ls);
1899       break;
1900     }
1901     case TK_DBCOLON: {  /* stat -> label */
1902       luaX_next(ls);  /* skip double colon */
1903       labelstat(ls, str_checkname(ls), line);
1904       break;
1905     }
1906     case TK_RETURN: {  /* stat -> retstat */
1907       luaX_next(ls);  /* skip RETURN */
1908       retstat(ls);
1909       break;
1910     }
1911     case TK_BREAK: {  /* stat -> breakstat */
1912       breakstat(ls);
1913       break;
1914     }
1915     case TK_GOTO: {  /* stat -> 'goto' NAME */
1916       luaX_next(ls);  /* skip 'goto' */
1917       gotostat(ls);
1918       break;
1919     }
1920     default: {  /* stat -> func | assignment */
1921       exprstat(ls);
1922       break;
1923     }
1924   }
1925   lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1926              ls->fs->freereg >= luaY_nvarstack(ls->fs));
1927   ls->fs->freereg = luaY_nvarstack(ls->fs);  /* free registers */
1928   leavelevel(ls);
1929 }
1930 
1931 /* }====================================================================== */
1932 
1933 
1934 /*
1935 ** compiles the main function, which is a regular vararg function with an
1936 ** upvalue named LUA_ENV
1937 */
mainfunc(LexState * ls,FuncState * fs)1938 static void mainfunc (LexState *ls, FuncState *fs) {
1939   BlockCnt bl;
1940   Upvaldesc *env;
1941   open_func(ls, fs, &bl);
1942   setvararg(fs, 0);  /* main function is always declared vararg */
1943   env = allocupvalue(fs);  /* ...set environment upvalue */
1944   env->instack = 1;
1945   env->idx = 0;
1946   env->kind = VDKREG;
1947   env->name = ls->envn;
1948   luaC_objbarrier(ls->L, fs->f, env->name);
1949   luaX_next(ls);  /* read first token */
1950   statlist(ls);  /* parse main body */
1951   check(ls, TK_EOS);
1952   close_func(ls);
1953 }
1954 
1955 
luaY_parser(lua_State * L,ZIO * z,Mbuffer * buff,Dyndata * dyd,const char * name,int firstchar)1956 LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1957                        Dyndata *dyd, const char *name, int firstchar) {
1958   LexState lexstate;
1959   FuncState funcstate;
1960   LClosure *cl = luaF_newLclosure(L, 1);  /* create main closure */
1961   setclLvalue2s(L, L->top.p, cl);  /* anchor it (to avoid being collected) */
1962   luaD_inctop(L);
1963   lexstate.h = luaH_new(L);  /* create table for scanner */
1964   sethvalue2s(L, L->top.p, lexstate.h);  /* anchor it */
1965   luaD_inctop(L);
1966   funcstate.f = cl->p = luaF_newproto(L);
1967   luaC_objbarrier(L, cl, cl->p);
1968   funcstate.f->source = luaS_new(L, name);  /* create and anchor TString */
1969   luaC_objbarrier(L, funcstate.f, funcstate.f->source);
1970   lexstate.buff = buff;
1971   lexstate.dyd = dyd;
1972   dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
1973   luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
1974   mainfunc(&lexstate, &funcstate);
1975   lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1976   /* all scopes should be correctly finished */
1977   lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
1978   L->top.p--;  /* remove scanner's table */
1979   return cl;  /* closure is on the stack, too */
1980 }
1981 
1982