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


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09