LCOV - code coverage report
Current view: top level - AP_Scripting/lua/src - lparser.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 900 959 93.8 %
Date: 2026-09-09 11:02:08 Functions: 82 86 95.3 %

          Line data    Source code
       1             : /*
       2             : ** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $
       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 <string.h>
      14             : 
      15             : #include "lua.h"
      16             : 
      17             : #include "lcode.h"
      18             : #include "ldebug.h"
      19             : #include "ldo.h"
      20             : #include "lfunc.h"
      21             : #include "llex.h"
      22             : #include "lmem.h"
      23             : #include "lobject.h"
      24             : #include "lopcodes.h"
      25             : #include "lparser.h"
      26             : #include "lstate.h"
      27             : #include "lstring.h"
      28             : #include "ltable.h"
      29             : 
      30             : 
      31             : 
      32             : /* maximum number of local variables per function (must be smaller
      33             :    than 250, due to the bytecode format) */
      34             : #define MAXVARS         100
      35             : 
      36             : 
      37             : #define hasmultret(k)           ((k) == VCALL || (k) == VVARARG)
      38             : 
      39             : 
      40             : /* because all strings are unified by the scanner, the parser
      41             :    can use pointer equality for string equality */
      42             : #define eqstr(a,b)      ((a) == (b))
      43             : 
      44             : 
      45             : /*
      46             : ** nodes for block list (list of active blocks)
      47             : */
      48             : typedef struct BlockCnt {
      49             :   struct BlockCnt *previous;  /* chain */
      50             :   int firstlabel;  /* index of first label in this block */
      51             :   int firstgoto;  /* index of first pending goto in this block */
      52             :   lu_byte nactvar;  /* # active locals outside the block */
      53             :   lu_byte upval;  /* true if some variable in the block is an upvalue */
      54             :   lu_byte isloop;  /* true if 'block' is a loop */
      55             : } BlockCnt;
      56             : 
      57             : 
      58             : 
      59             : /*
      60             : ** prototypes for recursive non-terminal functions
      61             : */
      62             : static void statement (LexState *ls);
      63             : static void expr (LexState *ls, expdesc *v);
      64             : 
      65             : 
      66             : /* semantic error */
      67           0 : static l_noret semerror (LexState *ls, const char *msg) {
      68           0 :   ls->t.token = 0;  /* remove "near <token>" from final message */
      69           0 :   luaX_syntaxerror(ls, msg);
      70             : }
      71             : 
      72             : 
      73           0 : static l_noret error_expected (LexState *ls, int token) {
      74           0 :   luaX_syntaxerror(ls,
      75           0 :       luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
      76             : }
      77             : 
      78             : 
      79           0 : static l_noret errorlimit (FuncState *fs, int limit, const char *what) {
      80           0 :   lua_State *L = fs->ls->L;
      81             :   const char *msg;
      82           0 :   int line = fs->f->linedefined;
      83           0 :   const char *where = (line == 0)
      84             :                       ? "main function"
      85           0 :                       : luaO_pushfstring(L, "function at line %d", line);
      86           0 :   msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
      87             :                              what, limit, where);
      88           0 :   luaX_syntaxerror(fs->ls, msg);
      89             : }
      90             : 
      91             : 
      92       94506 : static void checklimit (FuncState *fs, int v, int l, const char *what) {
      93       94506 :   if (v > l) errorlimit(fs, l, what);
      94       94506 : }
      95             : 
      96             : 
      97       85828 : static int testnext (LexState *ls, int c) {
      98       85828 :   if (ls->t.token == c) {
      99       42416 :     luaX_next(ls);
     100       42416 :     return 1;
     101             :   }
     102       43412 :   else return 0;
     103             : }
     104             : 
     105             : 
     106       80328 : static void check (LexState *ls, int c) {
     107       80328 :   if (ls->t.token != c)
     108           0 :     error_expected(ls, c);
     109       80328 : }
     110             : 
     111             : 
     112       18517 : static void checknext (LexState *ls, int c) {
     113       18517 :   check(ls, c);
     114       18517 :   luaX_next(ls);
     115       18517 : }
     116             : 
     117             : 
     118             : #define check_condition(ls,c,msg)       { if (!(c)) luaX_syntaxerror(ls, msg); }
     119             : 
     120             : 
     121             : 
     122       22393 : static void check_match (LexState *ls, int what, int who, int where) {
     123       22393 :   if (!testnext(ls, what)) {
     124           0 :     if (where == ls->linenumber)
     125           0 :       error_expected(ls, what);
     126             :     else {
     127           0 :       luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
     128             :              "%s expected (to close %s at line %d)",
     129             :               luaX_token2str(ls, what), luaX_token2str(ls, who), where));
     130             :     }
     131             :   }
     132       22393 : }
     133             : 
     134             : 
     135       61691 : static TString *str_checkname (LexState *ls) {
     136             :   TString *ts;
     137       61691 :   check(ls, TK_NAME);
     138       61691 :   ts = ls->t.seminfo.ts;
     139       61691 :   luaX_next(ls);
     140       61691 :   return ts;
     141             : }
     142             : 
     143             : 
     144      127915 : static void init_exp (expdesc *e, expkind k, int i) {
     145      127915 :   e->f = e->t = NO_JUMP;
     146      127915 :   e->k = k;
     147      127915 :   e->u.info = i;
     148      127915 : }
     149             : 
     150             : 
     151       35347 : static void codestring (LexState *ls, expdesc *e, TString *s) {
     152       35347 :   init_exp(e, VK, luaK_stringK(ls->fs, s));
     153       35347 : }
     154             : 
     155             : 
     156       14898 : static void checkname (LexState *ls, expdesc *e) {
     157       14898 :   codestring(ls, e, str_checkname(ls));
     158       14898 : }
     159             : 
     160             : 
     161        9315 : static int registerlocalvar (LexState *ls, TString *varname) {
     162        9315 :   FuncState *fs = ls->fs;
     163        9315 :   Proto *f = fs->f;
     164        9315 :   int oldsize = f->sizelocvars;
     165        9315 :   luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
     166             :                   LocVar, SHRT_MAX, "local variables");
     167       23611 :   while (oldsize < f->sizelocvars)
     168       14296 :     f->locvars[oldsize++].varname = NULL;
     169        9315 :   f->locvars[fs->nlocvars].varname = varname;
     170        9315 :   luaC_objbarrier(ls->L, f, varname);
     171        9315 :   return fs->nlocvars++;
     172             : }
     173             : 
     174             : 
     175        9315 : static void new_localvar (LexState *ls, TString *name) {
     176        9315 :   FuncState *fs = ls->fs;
     177        9315 :   Dyndata *dyd = ls->dyd;
     178        9315 :   int reg = registerlocalvar(ls, name);
     179        9315 :   checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
     180             :                   MAXVARS, "local variables");
     181        9315 :   luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
     182             :                   dyd->actvar.size, Vardesc, MAX_INT, "local variables");
     183        9315 :   dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg);
     184        9315 : }
     185             : 
     186             : 
     187        1315 : static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) {
     188        1315 :   new_localvar(ls, luaX_newstring(ls, name, sz));
     189        1315 : }
     190             : 
     191             : #define new_localvarliteral(ls,v) \
     192             :         new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1)
     193             : 
     194             : 
     195      805009 : static LocVar *getlocvar (FuncState *fs, int i) {
     196      805009 :   int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx;
     197             :   lua_assert(idx < fs->nlocvars);
     198      805009 :   return &fs->f->locvars[idx];
     199             : }
     200             : 
     201             : 
     202        7803 : static void adjustlocalvars (LexState *ls, int nvars) {
     203        7803 :   FuncState *fs = ls->fs;
     204        7803 :   fs->nactvar = cast_byte(fs->nactvar + nvars);
     205       17118 :   for (; nvars; nvars--) {
     206        9315 :     getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc;
     207             :   }
     208        7803 : }
     209             : 
     210             : 
     211        6867 : static void removevars (FuncState *fs, int tolevel) {
     212        6867 :   fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
     213       16182 :   while (fs->nactvar > tolevel)
     214        9315 :     getlocvar(fs, --fs->nactvar)->endpc = fs->pc;
     215        6867 : }
     216             : 
     217             : 
     218       47042 : static int searchupvalue (FuncState *fs, TString *name) {
     219             :   int i;
     220       47042 :   Upvaldesc *up = fs->f->upvalues;
     221      141735 :   for (i = 0; i < fs->nups; i++) {
     222      112820 :     if (eqstr(up[i].name, name)) return i;
     223             :   }
     224       28915 :   return -1;  /* not found */
     225             : }
     226             : 
     227             : 
     228        6078 : static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
     229        6078 :   Proto *f = fs->f;
     230        6078 :   int oldsize = f->sizeupvalues;
     231        6078 :   checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
     232        6078 :   luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
     233             :                   Upvaldesc, MAXUPVAL, "upvalues");
     234       16362 :   while (oldsize < f->sizeupvalues)
     235       10284 :     f->upvalues[oldsize++].name = NULL;
     236        6078 :   f->upvalues[fs->nups].instack = (v->k == VLOCAL);
     237        6078 :   f->upvalues[fs->nups].idx = cast_byte(v->u.info);
     238        6078 :   f->upvalues[fs->nups].name = name;
     239        6078 :   luaC_objbarrier(fs->ls->L, f, name);
     240        6078 :   return fs->nups++;
     241             : }
     242             : 
     243             : 
     244       67492 : static int searchvar (FuncState *fs, TString *n) {
     245             :   int i;
     246      832764 :   for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
     247      785722 :     if (eqstr(n, getlocvar(fs, i)->varname))
     248       20450 :       return i;
     249             :   }
     250       47042 :   return -1;  /* not found */
     251             : }
     252             : 
     253             : 
     254             : /*
     255             :   Mark block where variable at given level was defined
     256             :   (to emit close instructions later).
     257             : */
     258        3927 : static void markupval (FuncState *fs, int level) {
     259        3927 :   BlockCnt *bl = fs->bl;
     260        3979 :   while (bl->nactvar > level)
     261          52 :     bl = bl->previous;
     262        3927 :   bl->upval = 1;
     263        3927 : }
     264             : 
     265             : 
     266             : /*
     267             :   Find variable with given name 'n'. If it is an upvalue, add this
     268             :   upvalue into all intermediate functions.
     269             : */
     270       81750 : static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
     271       81750 :   if (fs == NULL)  /* no more levels? */
     272       14258 :     init_exp(var, VVOID, 0);  /* default is global */
     273             :   else {
     274       67492 :     int v = searchvar(fs, n);  /* look up locals at current level */
     275       67492 :     if (v >= 0) {  /* found? */
     276       20450 :       init_exp(var, VLOCAL, v);  /* variable is local */
     277       20450 :       if (!base)
     278        3927 :         markupval(fs, v);  /* local will be used as an upval */
     279             :     }
     280             :     else {  /* not found as local at current level; try upvalues */
     281       47042 :       int idx = searchupvalue(fs, n);  /* try existing upvalues */
     282       47042 :       if (idx < 0) {  /* not found? */
     283       28915 :         singlevaraux(fs->prev, n, var, 0);  /* try upper levels */
     284       28915 :         if (var->k == VVOID)  /* not found? */
     285       22957 :           return;  /* it is a global */
     286             :         /* else was LOCAL or UPVAL */
     287        5958 :         idx  = newupvalue(fs, n, var);  /* will be a new upvalue */
     288             :       }
     289       24085 :       init_exp(var, VUPVAL, idx);  /* new or old upvalue */
     290             :     }
     291             :   }
     292             : }
     293             : 
     294             : 
     295       38577 : static void singlevar (LexState *ls, expdesc *var) {
     296       38577 :   TString *varname = str_checkname(ls);
     297       38577 :   FuncState *fs = ls->fs;
     298       38577 :   singlevaraux(fs, varname, var, 1);
     299       38577 :   if (var->k == VVOID) {  /* global name? */
     300             :     expdesc key;
     301       14258 :     singlevaraux(fs, ls->envn, var, 1);  /* get environment variable */
     302             :     lua_assert(var->k != VVOID);  /* this one must exist */
     303       14258 :     codestring(ls, &key, varname);  /* key is variable name */
     304       14258 :     luaK_indexed(fs, var, &key);  /* env[varname] */
     305             :   }
     306       38577 : }
     307             : 
     308             : 
     309        4900 : static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
     310        4900 :   FuncState *fs = ls->fs;
     311        4900 :   int extra = nvars - nexps;
     312        4900 :   if (hasmultret(e->k)) {
     313        2493 :     extra++;  /* includes call itself */
     314        2493 :     if (extra < 0) extra = 0;
     315        2493 :     luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */
     316        2493 :     if (extra > 1) luaK_reserveregs(fs, extra-1);
     317             :   }
     318             :   else {
     319        2407 :     if (e->k != VVOID) luaK_exp2nextreg(fs, e);  /* close last expression */
     320        2407 :     if (extra > 0) {
     321         196 :       int reg = fs->freereg;
     322         196 :       luaK_reserveregs(fs, extra);
     323         196 :       luaK_nil(fs, reg, extra);
     324             :     }
     325             :   }
     326        4900 :   if (nexps > nvars)
     327           0 :     ls->fs->freereg -= nexps - nvars;  /* remove extra values */
     328        4900 : }
     329             : 
     330             : 
     331       75815 : static void enterlevel (LexState *ls) {
     332       75815 :   lua_State *L = ls->L;
     333       75815 :   ++L->nCcalls;
     334       75815 :   checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels");
     335       75815 : }
     336             : 
     337             : 
     338             : #define leavelevel(ls)  ((ls)->L->nCcalls--)
     339             : 
     340             : 
     341         184 : static void closegoto (LexState *ls, int g, Labeldesc *label) {
     342             :   int i;
     343         184 :   FuncState *fs = ls->fs;
     344         184 :   Labellist *gl = &ls->dyd->gt;
     345         184 :   Labeldesc *gt = &gl->arr[g];
     346             :   lua_assert(eqstr(gt->name, label->name));
     347         184 :   if (gt->nactvar < label->nactvar) {
     348           0 :     TString *vname = getlocvar(fs, gt->nactvar)->varname;
     349           0 :     const char *msg = luaO_pushfstring(ls->L,
     350             :       "<goto %s> at line %d jumps into the scope of local '%s'",
     351           0 :       getstr(gt->name), gt->line, getstr(vname));
     352           0 :     semerror(ls, msg);
     353             :   }
     354         184 :   luaK_patchlist(fs, gt->pc, label->pc);
     355             :   /* remove goto from pending list */
     356         330 :   for (i = g; i < gl->n - 1; i++)
     357         146 :     gl->arr[i] = gl->arr[i + 1];
     358         184 :   gl->n--;
     359         184 : }
     360             : 
     361             : 
     362             : /*
     363             : ** try to close a goto with existing labels; this solves backward jumps
     364             : */
     365         412 : static int findlabel (LexState *ls, int g) {
     366             :   int i;
     367         412 :   BlockCnt *bl = ls->fs->bl;
     368         412 :   Dyndata *dyd = ls->dyd;
     369         412 :   Labeldesc *gt = &dyd->gt.arr[g];
     370             :   /* check labels in current block for a match */
     371         412 :   for (i = bl->firstlabel; i < dyd->label.n; i++) {
     372           0 :     Labeldesc *lb = &dyd->label.arr[i];
     373           0 :     if (eqstr(lb->name, gt->name)) {  /* correct label? */
     374           0 :       if (gt->nactvar > lb->nactvar &&
     375           0 :           (bl->upval || dyd->label.n > bl->firstlabel))
     376           0 :         luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
     377           0 :       closegoto(ls, g, lb);  /* close it */
     378           0 :       return 1;
     379             :     }
     380             :   }
     381         412 :   return 0;  /* label not found; cannot close goto */
     382             : }
     383             : 
     384             : 
     385         708 : static int newlabelentry (LexState *ls, Labellist *l, TString *name,
     386             :                           int line, int pc) {
     387         708 :   int n = l->n;
     388         708 :   luaM_growvector(ls->L, l->arr, n, l->size,
     389             :                   Labeldesc, SHRT_MAX, "labels/gotos");
     390         708 :   l->arr[n].name = name;
     391         708 :   l->arr[n].line = line;
     392         708 :   l->arr[n].nactvar = ls->fs->nactvar;
     393         708 :   l->arr[n].pc = pc;
     394         708 :   l->n = n + 1;
     395         708 :   return n;
     396             : }
     397             : 
     398             : 
     399             : /*
     400             : ** check whether new label 'lb' matches any pending gotos in current
     401             : ** block; solves forward jumps
     402             : */
     403         524 : static void findgotos (LexState *ls, Labeldesc *lb) {
     404         524 :   Labellist *gl = &ls->dyd->gt;
     405         524 :   int i = ls->fs->bl->firstgoto;
     406         714 :   while (i < gl->n) {
     407         190 :     if (eqstr(gl->arr[i].name, lb->name))
     408         184 :       closegoto(ls, i, lb);
     409             :     else
     410           6 :       i++;
     411             :   }
     412         524 : }
     413             : 
     414             : 
     415             : /*
     416             : ** export pending gotos to outer level, to check them against
     417             : ** outer labels; if the block being exited has upvalues, and
     418             : ** the goto exits the scope of any variable (which can be the
     419             : ** upvalue), close those variables being exited.
     420             : */
     421        5038 : static void movegotosout (FuncState *fs, BlockCnt *bl) {
     422        5038 :   int i = bl->firstgoto;
     423        5038 :   Labellist *gl = &fs->ls->dyd->gt;
     424             :   /* correct pending gotos to current block and try to close it
     425             :      with visible labels */
     426        5266 :   while (i < gl->n) {
     427         228 :     Labeldesc *gt = &gl->arr[i];
     428         228 :     if (gt->nactvar > bl->nactvar) {
     429          44 :       if (bl->upval)
     430           0 :         luaK_patchclose(fs, gt->pc, bl->nactvar);
     431          44 :       gt->nactvar = bl->nactvar;
     432             :     }
     433         228 :     if (!findlabel(fs->ls, i))
     434         228 :       i++;  /* move to next one */
     435             :   }
     436        5038 : }
     437             : 
     438             : 
     439        6867 : static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
     440        6867 :   bl->isloop = isloop;
     441        6867 :   bl->nactvar = fs->nactvar;
     442        6867 :   bl->firstlabel = fs->ls->dyd->label.n;
     443        6867 :   bl->firstgoto = fs->ls->dyd->gt.n;
     444        6867 :   bl->upval = 0;
     445        6867 :   bl->previous = fs->bl;
     446        6867 :   fs->bl = bl;
     447             :   lua_assert(fs->freereg == fs->nactvar);
     448        6867 : }
     449             : 
     450             : 
     451             : /*
     452             : ** create a label named 'break' to resolve break statements
     453             : */
     454         458 : static void breaklabel (LexState *ls) {
     455         458 :   TString *n = luaS_new(ls->L, "break");
     456         458 :   int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc);
     457         458 :   findgotos(ls, &ls->dyd->label.arr[l]);
     458         458 : }
     459             : 
     460             : /*
     461             : ** generates an error for an undefined 'goto'; choose appropriate
     462             : ** message when label name is a reserved word (which can only be 'break')
     463             : */
     464           0 : static l_noret undefgoto (LexState *ls, Labeldesc *gt) {
     465           0 :   const char *msg = isreserved(gt->name)
     466             :                     ? "<%s> at line %d not inside a loop"
     467           0 :                     : "no visible label '%s' for <goto> at line %d";
     468           0 :   msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
     469           0 :   semerror(ls, msg);
     470             : }
     471             : 
     472             : 
     473        6867 : static void leaveblock (FuncState *fs) {
     474        6867 :   BlockCnt *bl = fs->bl;
     475        6867 :   LexState *ls = fs->ls;
     476        6867 :   if (bl->previous && bl->upval) {
     477             :     /* create a 'jump to here' to close upvalues */
     478          12 :     int j = luaK_jump(fs);
     479          12 :     luaK_patchclose(fs, j, bl->nactvar);
     480          12 :     luaK_patchtohere(fs, j);
     481             :   }
     482        6867 :   if (bl->isloop)
     483         458 :     breaklabel(ls);  /* close pending breaks */
     484        6867 :   fs->bl = bl->previous;
     485        6867 :   removevars(fs, bl->nactvar);
     486             :   lua_assert(bl->nactvar == fs->nactvar);
     487        6867 :   fs->freereg = fs->nactvar;  /* free registers */
     488        6867 :   ls->dyd->label.n = bl->firstlabel;  /* remove local labels */
     489        6867 :   if (bl->previous)  /* inner block? */
     490        5038 :     movegotosout(fs, bl);  /* update pending gotos to outer block */
     491        1829 :   else if (bl->firstgoto < ls->dyd->gt.n)  /* pending gotos in outer block? */
     492           0 :     undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]);  /* error */
     493        6867 : }
     494             : 
     495             : 
     496             : /*
     497             : ** adds a new prototype into list of prototypes
     498             : */
     499        1709 : static Proto *addprototype (LexState *ls) {
     500             :   Proto *clp;
     501        1709 :   lua_State *L = ls->L;
     502        1709 :   FuncState *fs = ls->fs;
     503        1709 :   Proto *f = fs->f;  /* prototype of current function */
     504        1709 :   if (fs->np >= f->sizep) {
     505         389 :     int oldsize = f->sizep;
     506         389 :     luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
     507        3181 :     while (oldsize < f->sizep)
     508        2792 :       f->p[oldsize++] = NULL;
     509             :   }
     510        1709 :   f->p[fs->np++] = clp = luaF_newproto(L);
     511        1709 :   luaC_objbarrier(L, f, clp);
     512        1709 :   return clp;
     513             : }
     514             : 
     515             : 
     516             : /*
     517             : ** codes instruction to create new closure in parent function.
     518             : ** The OP_CLOSURE instruction must use the last available register,
     519             : ** so that, if it invokes the GC, the GC knows which registers
     520             : ** are in use at that time.
     521             : */
     522        1709 : static void codeclosure (LexState *ls, expdesc *v) {
     523        1709 :   FuncState *fs = ls->fs->prev;
     524        1709 :   init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
     525        1709 :   luaK_exp2nextreg(fs, v);  /* fix it at the last register */
     526        1709 : }
     527             : 
     528             : 
     529        1829 : static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
     530             :   Proto *f;
     531        1829 :   fs->prev = ls->fs;  /* linked list of funcstates */
     532        1829 :   fs->ls = ls;
     533        1829 :   ls->fs = fs;
     534        1829 :   fs->pc = 0;
     535        1829 :   fs->lasttarget = 0;
     536        1829 :   fs->jpc = NO_JUMP;
     537        1829 :   fs->freereg = 0;
     538        1829 :   fs->nk = 0;
     539        1829 :   fs->np = 0;
     540        1829 :   fs->nups = 0;
     541        1829 :   fs->nlocvars = 0;
     542        1829 :   fs->nactvar = 0;
     543        1829 :   fs->firstlocal = ls->dyd->actvar.n;
     544        1829 :   fs->bl = NULL;
     545        1829 :   f = fs->f;
     546        1829 :   f->source = ls->source;
     547        1829 :   luaC_objbarrier(ls->L, f, f->source);
     548        1829 :   f->maxstacksize = 2;  /* registers 0/1 are always valid */
     549        1829 :   enterblock(fs, bl, 0);
     550        1829 : }
     551             : 
     552             : 
     553        1829 : static void close_func (LexState *ls) {
     554        1829 :   lua_State *L = ls->L;
     555        1829 :   FuncState *fs = ls->fs;
     556        1829 :   Proto *f = fs->f;
     557        1829 :   luaK_ret(fs, 0, 0);  /* final return */
     558        1829 :   leaveblock(fs);
     559        1829 :   luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
     560        1829 :   f->sizecode = fs->pc;
     561        1829 :   luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
     562        1829 :   f->sizelineinfo = fs->pc;
     563        1829 :   luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
     564        1829 :   f->sizek = fs->nk;
     565        1829 :   luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
     566        1829 :   f->sizep = fs->np;
     567        1829 :   luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
     568        1829 :   f->sizelocvars = fs->nlocvars;
     569        1829 :   luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
     570        1829 :   f->sizeupvalues = fs->nups;
     571             :   lua_assert(fs->bl == NULL);
     572        1829 :   ls->fs = fs->prev;
     573        1829 :   luaC_checkGC(L);
     574        1829 : }
     575             : 
     576             : 
     577             : 
     578             : /*============================================================*/
     579             : /* GRAMMAR RULES */
     580             : /*============================================================*/
     581             : 
     582             : 
     583             : /*
     584             : ** check whether current token is in the follow set of a block.
     585             : ** 'until' closes syntactical blocks, but do not close scope,
     586             : ** so it is handled in separate.
     587             : */
     588       27378 : static int block_follow (LexState *ls, int withuntil) {
     589       27378 :   switch (ls->t.token) {
     590        3825 :     case TK_ELSE: case TK_ELSEIF:
     591             :     case TK_END: case TK_EOS:
     592        3825 :       return 1;
     593           7 :     case TK_UNTIL: return withuntil;
     594       23546 :     default: return 0;
     595             :   }
     596             : }
     597             : 
     598             : 
     599        5915 : static void statlist (LexState *ls) {
     600             :   /* statlist -> { stat [';'] } */
     601       24561 :   while (!block_follow(ls, 1)) {
     602       21303 :     if (ls->t.token == TK_RETURN) {
     603        2657 :       statement(ls);
     604        2657 :       return;  /* 'return' must be last statement */
     605             :     }
     606       18646 :     statement(ls);
     607             :   }
     608             : }
     609             : 
     610             : 
     611        8202 : static void fieldsel (LexState *ls, expdesc *v) {
     612             :   /* fieldsel -> ['.' | ':'] NAME */
     613        8202 :   FuncState *fs = ls->fs;
     614             :   expdesc key;
     615        8202 :   luaK_exp2anyregup(fs, v);
     616        8202 :   luaX_next(ls);  /* skip the dot or colon */
     617        8202 :   checkname(ls, &key);
     618        8202 :   luaK_indexed(fs, v, &key);
     619        8202 : }
     620             : 
     621             : 
     622        2349 : static void yindex (LexState *ls, expdesc *v) {
     623             :   /* index -> '[' expr ']' */
     624        2349 :   luaX_next(ls);  /* skip the '[' */
     625        2349 :   expr(ls, v);
     626        2349 :   luaK_exp2val(ls->fs, v);
     627        2349 :   checknext(ls, ']');
     628        2349 : }
     629             : 
     630             : 
     631             : /*
     632             : ** {======================================================================
     633             : ** Rules for Constructors
     634             : ** =======================================================================
     635             : */
     636             : 
     637             : 
     638             : struct ConsControl {
     639             :   expdesc v;  /* last list item read */
     640             :   expdesc *t;  /* table descriptor */
     641             :   int nh;  /* total number of 'record' elements */
     642             :   int na;  /* total number of array elements */
     643             :   int tostore;  /* number of array elements pending to be stored */
     644             : };
     645             : 
     646             : 
     647        2922 : static void recfield (LexState *ls, struct ConsControl *cc) {
     648             :   /* recfield -> (NAME | '['exp1']') = exp1 */
     649        2922 :   FuncState *fs = ls->fs;
     650        2922 :   int reg = ls->fs->freereg;
     651             :   expdesc key, val;
     652             :   int rkkey;
     653        2922 :   if (ls->t.token == TK_NAME) {
     654        1761 :     checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
     655        1761 :     checkname(ls, &key);
     656             :   }
     657             :   else  /* ls->t.token == '[' */
     658        1161 :     yindex(ls, &key);
     659        2922 :   cc->nh++;
     660        2922 :   checknext(ls, '=');
     661        2922 :   rkkey = luaK_exp2RK(fs, &key);
     662        2922 :   expr(ls, &val);
     663        2922 :   luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val));
     664        2922 :   fs->freereg = reg;  /* free registers */
     665        2922 : }
     666             : 
     667             : 
     668        4364 : static void closelistfield (FuncState *fs, struct ConsControl *cc) {
     669        4364 :   if (cc->v.k == VVOID) return;  /* there is no list item */
     670         946 :   luaK_exp2nextreg(fs, &cc->v);
     671         946 :   cc->v.k = VVOID;
     672         946 :   if (cc->tostore == LFIELDS_PER_FLUSH) {
     673           0 :     luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);  /* flush */
     674           0 :     cc->tostore = 0;  /* no more items pending */
     675             :   }
     676             : }
     677             : 
     678             : 
     679        1381 : static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
     680        1381 :   if (cc->tostore == 0) return;
     681         523 :   if (hasmultret(cc->v.k)) {
     682         218 :     luaK_setmultret(fs, &cc->v);
     683         218 :     luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET);
     684         218 :     cc->na--;  /* do not count last expression (unknown number of elements) */
     685             :   }
     686             :   else {
     687         305 :     if (cc->v.k != VVOID)
     688         278 :       luaK_exp2nextreg(fs, &cc->v);
     689         305 :     luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);
     690             :   }
     691             : }
     692             : 
     693             : 
     694        1442 : static void listfield (LexState *ls, struct ConsControl *cc) {
     695             :   /* listfield -> exp */
     696        1442 :   expr(ls, &cc->v);
     697        1442 :   checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
     698        1442 :   cc->na++;
     699        1442 :   cc->tostore++;
     700        1442 : }
     701             : 
     702             : 
     703        4364 : static void field (LexState *ls, struct ConsControl *cc) {
     704             :   /* field -> listfield | recfield */
     705        4364 :   switch(ls->t.token) {
     706        2386 :     case TK_NAME: {  /* may be 'listfield' or 'recfield' */
     707        2386 :       if (luaX_lookahead(ls) != '=')  /* expression? */
     708         625 :         listfield(ls, cc);
     709             :       else
     710        1761 :         recfield(ls, cc);
     711        2386 :       break;
     712             :     }
     713        1161 :     case '[': {
     714        1161 :       recfield(ls, cc);
     715        1161 :       break;
     716             :     }
     717         817 :     default: {
     718         817 :       listfield(ls, cc);
     719         817 :       break;
     720             :     }
     721             :   }
     722        4364 : }
     723             : 
     724             : 
     725        1381 : static void constructor (LexState *ls, expdesc *t) {
     726             :   /* constructor -> '{' [ field { sep field } [sep] ] '}'
     727             :      sep -> ',' | ';' */
     728        1381 :   FuncState *fs = ls->fs;
     729        1381 :   int line = ls->linenumber;
     730        1381 :   int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
     731             :   struct ConsControl cc;
     732        1381 :   cc.na = cc.nh = cc.tostore = 0;
     733        1381 :   cc.t = t;
     734        1381 :   init_exp(t, VRELOCABLE, pc);
     735        1381 :   init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
     736        1381 :   luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top */
     737        1381 :   checknext(ls, '{');
     738             :   do {
     739             :     lua_assert(cc.v.k == VVOID || cc.tostore > 0);
     740        4972 :     if (ls->t.token == '}') break;
     741        4364 :     closelistfield(fs, &cc);
     742        4364 :     field(ls, &cc);
     743        4364 :   } while (testnext(ls, ',') || testnext(ls, ';'));
     744        1381 :   check_match(ls, '}', '{', line);
     745        1381 :   lastlistfield(fs, &cc);
     746        1381 :   SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
     747        1381 :   SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */
     748        1381 : }
     749             : 
     750             : /* }====================================================================== */
     751             : 
     752             : 
     753             : 
     754        1709 : static void parlist (LexState *ls) {
     755             :   /* parlist -> [ param { ',' param } ] */
     756        1709 :   FuncState *fs = ls->fs;
     757        1709 :   Proto *f = fs->f;
     758        1709 :   int nparams = 0;
     759        1709 :   f->is_vararg = 0;
     760        1709 :   if (ls->t.token != ')') {  /* is 'parlist' not empty? */
     761             :     do {
     762        1851 :       switch (ls->t.token) {
     763        1847 :         case TK_NAME: {  /* param -> NAME */
     764        1847 :           new_localvar(ls, str_checkname(ls));
     765        1847 :           nparams++;
     766        1847 :           break;
     767             :         }
     768           4 :         case TK_DOTS: {  /* param -> '...' */
     769           4 :           luaX_next(ls);
     770           4 :           f->is_vararg = 1;  /* declared vararg */
     771           4 :           break;
     772             :         }
     773           0 :         default: luaX_syntaxerror(ls, "<name> or '...' expected");
     774             :       }
     775        1851 :     } while (!f->is_vararg && testnext(ls, ','));
     776             :   }
     777        1709 :   adjustlocalvars(ls, nparams);
     778        1709 :   f->numparams = cast_byte(fs->nactvar);
     779        1709 :   luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
     780        1709 : }
     781             : 
     782             : 
     783        1709 : static void body (LexState *ls, expdesc *e, int ismethod, int line) {
     784             :   /* body ->  '(' parlist ')' block END */
     785             :   FuncState new_fs;
     786             :   BlockCnt bl;
     787        1709 :   new_fs.f = addprototype(ls);
     788        1709 :   new_fs.f->linedefined = line;
     789        1709 :   open_func(ls, &new_fs, &bl);
     790        1709 :   checknext(ls, '(');
     791        1709 :   if (ismethod) {
     792         115 :     new_localvarliteral(ls, "self");  /* create 'self' parameter */
     793         115 :     adjustlocalvars(ls, 1);
     794             :   }
     795        1709 :   parlist(ls);
     796        1709 :   checknext(ls, ')');
     797        1709 :   statlist(ls);
     798        1709 :   new_fs.f->lastlinedefined = ls->linenumber;
     799        1709 :   check_match(ls, TK_END, TK_FUNCTION, line);
     800        1709 :   codeclosure(ls, e);
     801        1709 :   close_func(ls);
     802        1709 : }
     803             : 
     804             : 
     805       21585 : static int explist (LexState *ls, expdesc *v) {
     806             :   /* explist -> expr { ',' expr } */
     807       21585 :   int n = 1;  /* at least one expression */
     808       21585 :   expr(ls, v);
     809       30939 :   while (testnext(ls, ',')) {
     810        9354 :     luaK_exp2nextreg(ls->fs, v);
     811        9354 :     expr(ls, v);
     812        9354 :     n++;
     813             :   }
     814       21585 :   return n;
     815             : }
     816             : 
     817             : 
     818       14339 : static void funcargs (LexState *ls, expdesc *f, int line) {
     819       14339 :   FuncState *fs = ls->fs;
     820             :   expdesc args;
     821             :   int base, nparams;
     822       14339 :   switch (ls->t.token) {
     823       14289 :     case '(': {  /* funcargs -> '(' [ explist ] ')' */
     824       14289 :       luaX_next(ls);
     825       14289 :       if (ls->t.token == ')')  /* arg list is empty? */
     826        3655 :         args.k = VVOID;
     827             :       else {
     828       10634 :         explist(ls, &args);
     829       10634 :         luaK_setmultret(fs, &args);
     830             :       }
     831       14289 :       check_match(ls, ')', '(', line);
     832       14289 :       break;
     833             :     }
     834          18 :     case '{': {  /* funcargs -> constructor */
     835          18 :       constructor(ls, &args);
     836          18 :       break;
     837             :     }
     838          32 :     case TK_STRING: {  /* funcargs -> STRING */
     839          32 :       codestring(ls, &args, ls->t.seminfo.ts);
     840          32 :       luaX_next(ls);  /* must use 'seminfo' before 'next' */
     841          32 :       break;
     842             :     }
     843           0 :     default: {
     844           0 :       luaX_syntaxerror(ls, "function arguments expected");
     845             :     }
     846             :   }
     847             :   lua_assert(f->k == VNONRELOC);
     848       14339 :   base = f->u.info;  /* base register for call */
     849       14339 :   if (hasmultret(args.k))
     850        1708 :     nparams = LUA_MULTRET;  /* open call */
     851             :   else {
     852       12631 :     if (args.k != VVOID)
     853        8976 :       luaK_exp2nextreg(fs, &args);  /* close last argument */
     854       12631 :     nparams = fs->freereg - (base+1);
     855             :   }
     856       14339 :   init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
     857       14339 :   luaK_fixline(fs, line);
     858       14339 :   fs->freereg = base+1;  /* call remove function and arguments and leaves
     859             :                             (unless changed) one result */
     860       14339 : }
     861             : 
     862             : 
     863             : 
     864             : 
     865             : /*
     866             : ** {======================================================================
     867             : ** Expression parsing
     868             : ** =======================================================================
     869             : */
     870             : 
     871             : 
     872       38936 : static void primaryexp (LexState *ls, expdesc *v) {
     873             :   /* primaryexp -> NAME | '(' expr ')' */
     874       38936 :   switch (ls->t.token) {
     875        1297 :     case '(': {
     876        1297 :       int line = ls->linenumber;
     877        1297 :       luaX_next(ls);
     878        1297 :       expr(ls, v);
     879        1297 :       check_match(ls, ')', '(', line);
     880        1297 :       luaK_dischargevars(ls->fs, v);
     881        1297 :       return;
     882             :     }
     883       37639 :     case TK_NAME: {
     884       37639 :       singlevar(ls, v);
     885       37639 :       return;
     886             :     }
     887           0 :     default: {
     888           0 :       luaX_syntaxerror(ls, "unexpected symbol");
     889             :     }
     890             :   }
     891             : }
     892             : 
     893             : 
     894       38936 : static void suffixedexp (LexState *ls, expdesc *v) {
     895             :   /* suffixedexp ->
     896             :        primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
     897       38936 :   FuncState *fs = ls->fs;
     898       38936 :   int line = ls->linenumber;
     899       38936 :   primaryexp(ls, v);
     900             :   for (;;) {
     901       62269 :     switch (ls->t.token) {
     902        7806 :       case '.': {  /* fieldsel */
     903        7806 :         fieldsel(ls, v);
     904        7806 :         break;
     905             :       }
     906        1188 :       case '[': {  /* '[' exp1 ']' */
     907             :         expdesc key;
     908        1188 :         luaK_exp2anyregup(fs, v);
     909        1188 :         yindex(ls, &key);
     910        1188 :         luaK_indexed(fs, v, &key);
     911        1188 :         break;
     912             :       }
     913        4935 :       case ':': {  /* ':' NAME funcargs */
     914             :         expdesc key;
     915        4935 :         luaX_next(ls);
     916        4935 :         checkname(ls, &key);
     917        4935 :         luaK_self(fs, v, &key);
     918        4935 :         funcargs(ls, v, line);
     919        4935 :         break;
     920             :       }
     921        9404 :       case '(': case TK_STRING: case '{': {  /* funcargs */
     922        9404 :         luaK_exp2nextreg(fs, v);
     923        9404 :         funcargs(ls, v, line);
     924        9404 :         break;
     925             :       }
     926       38936 :       default: return;
     927             :     }
     928             :   }
     929             : }
     930             : 
     931             : 
     932       52545 : static void simpleexp (LexState *ls, expdesc *v) {
     933             :   /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... |
     934             :                   constructor | FUNCTION body | suffixedexp */
     935       52545 :   switch (ls->t.token) {
     936        1835 :     case TK_FLT: {
     937        1835 :       init_exp(v, VKFLT, 0);
     938        1835 :       v->u.nval = ls->t.seminfo.r;
     939        1835 :       break;
     940             :     }
     941        9704 :     case TK_INT: {
     942        9704 :       init_exp(v, VKINT, 0);
     943        9704 :       v->u.ival = ls->t.seminfo.i;
     944        9704 :       break;
     945             :     }
     946        6159 :     case TK_STRING: {
     947        6159 :       codestring(ls, v, ls->t.seminfo.ts);
     948        6159 :       break;
     949             :     }
     950         941 :     case TK_NIL: {
     951         941 :       init_exp(v, VNIL, 0);
     952         941 :       break;
     953             :     }
     954        1149 :     case TK_TRUE: {
     955        1149 :       init_exp(v, VTRUE, 0);
     956        1149 :       break;
     957             :     }
     958        1051 :     case TK_FALSE: {
     959        1051 :       init_exp(v, VFALSE, 0);
     960        1051 :       break;
     961             :     }
     962           4 :     case TK_DOTS: {  /* vararg */
     963           4 :       FuncState *fs = ls->fs;
     964           4 :       check_condition(ls, fs->f->is_vararg,
     965             :                       "cannot use '...' outside a vararg function");
     966           4 :       init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
     967           4 :       break;
     968             :     }
     969        1363 :     case '{': {  /* constructor */
     970        1363 :       constructor(ls, v);
     971        1363 :       return;
     972             :     }
     973         114 :     case TK_FUNCTION: {
     974         114 :       luaX_next(ls);
     975         114 :       body(ls, v, 0, ls->linenumber);
     976         114 :       return;
     977             :     }
     978       30225 :     default: {
     979       30225 :       suffixedexp(ls, v);
     980       30225 :       return;
     981             :     }
     982             :   }
     983       20843 :   luaX_next(ls);
     984             : }
     985             : 
     986             : 
     987       54512 : static UnOpr getunopr (int op) {
     988       54512 :   switch (op) {
     989         853 :     case TK_NOT: return OPR_NOT;
     990         997 :     case '-': return OPR_MINUS;
     991           3 :     case '~': return OPR_BNOT;
     992         114 :     case '#': return OPR_LEN;
     993       52545 :     default: return OPR_NOUNOPR;
     994             :   }
     995             : }
     996             : 
     997             : 
     998       54512 : static BinOpr getbinopr (int op) {
     999       54512 :   switch (op) {
    1000         832 :     case '+': return OPR_ADD;
    1001         716 :     case '-': return OPR_SUB;
    1002        1041 :     case '*': return OPR_MUL;
    1003          44 :     case '%': return OPR_MOD;
    1004         119 :     case '^': return OPR_POW;
    1005         609 :     case '/': return OPR_DIV;
    1006          64 :     case TK_IDIV: return OPR_IDIV;
    1007          62 :     case '&': return OPR_BAND;
    1008           7 :     case '|': return OPR_BOR;
    1009          15 :     case '~': return OPR_BXOR;
    1010          90 :     case TK_SHL: return OPR_SHL;
    1011           7 :     case TK_SHR: return OPR_SHR;
    1012         828 :     case TK_CONCAT: return OPR_CONCAT;
    1013         672 :     case TK_NE: return OPR_NE;
    1014        2085 :     case TK_EQ: return OPR_EQ;
    1015         500 :     case '<': return OPR_LT;
    1016         242 :     case TK_LE: return OPR_LE;
    1017         665 :     case '>': return OPR_GT;
    1018         115 :     case TK_GE: return OPR_GE;
    1019         899 :     case TK_AND: return OPR_AND;
    1020         781 :     case TK_OR: return OPR_OR;
    1021       44119 :     default: return OPR_NOBINOPR;
    1022             :   }
    1023             : }
    1024             : 
    1025             : 
    1026             : static const struct {
    1027             :   lu_byte left;  /* left priority for each binary operator */
    1028             :   lu_byte right; /* right priority */
    1029             : } priority[] = {  /* ORDER OPR */
    1030             :    {10, 10}, {10, 10},           /* '+' '-' */
    1031             :    {11, 11}, {11, 11},           /* '*' '%' */
    1032             :    {14, 13},                  /* '^' (right associative) */
    1033             :    {11, 11}, {11, 11},           /* '/' '//' */
    1034             :    {6, 6}, {4, 4}, {5, 5},   /* '&' '|' '~' */
    1035             :    {7, 7}, {7, 7},           /* '<<' '>>' */
    1036             :    {9, 8},                   /* '..' (right associative) */
    1037             :    {3, 3}, {3, 3}, {3, 3},   /* ==, <, <= */
    1038             :    {3, 3}, {3, 3}, {3, 3},   /* ~=, >, >= */
    1039             :    {2, 2}, {1, 1}            /* and, or */
    1040             : };
    1041             : 
    1042             : #define UNARY_PRIORITY  12  /* priority for unary operators */
    1043             : 
    1044             : 
    1045             : /*
    1046             : ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
    1047             : ** where 'binop' is any binary operator with a priority higher than 'limit'
    1048             : */
    1049       54512 : static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
    1050             :   BinOpr op;
    1051             :   UnOpr uop;
    1052       54512 :   enterlevel(ls);
    1053       54512 :   uop = getunopr(ls->t.token);
    1054       54512 :   if (uop != OPR_NOUNOPR) {
    1055        1967 :     int line = ls->linenumber;
    1056        1967 :     luaX_next(ls);
    1057        1967 :     subexpr(ls, v, UNARY_PRIORITY);
    1058        1967 :     luaK_prefix(ls->fs, uop, v, line);
    1059             :   }
    1060       52545 :   else simpleexp(ls, v);
    1061             :   /* expand while operators have priorities higher than 'limit' */
    1062       54512 :   op = getbinopr(ls->t.token);
    1063       64500 :   while (op != OPR_NOBINOPR && priority[op].left > limit) {
    1064             :     expdesc v2;
    1065             :     BinOpr nextop;
    1066        9988 :     int line = ls->linenumber;
    1067        9988 :     luaX_next(ls);
    1068        9988 :     luaK_infix(ls->fs, op, v);
    1069             :     /* read sub-expression with higher priority */
    1070        9988 :     nextop = subexpr(ls, &v2, priority[op].right);
    1071        9988 :     luaK_posfix(ls->fs, op, v, &v2, line);
    1072        9988 :     op = nextop;
    1073             :   }
    1074       54512 :   leavelevel(ls);
    1075       54512 :   return op;  /* return first untreated operator */
    1076             : }
    1077             : 
    1078             : 
    1079       42557 : static void expr (LexState *ls, expdesc *v) {
    1080       42557 :   subexpr(ls, v, 0);
    1081       42557 : }
    1082             : 
    1083             : /* }==================================================================== */
    1084             : 
    1085             : 
    1086             : 
    1087             : /*
    1088             : ** {======================================================================
    1089             : ** Rules for Statements
    1090             : ** =======================================================================
    1091             : */
    1092             : 
    1093             : 
    1094         801 : static void block (LexState *ls) {
    1095             :   /* block -> statlist */
    1096         801 :   FuncState *fs = ls->fs;
    1097             :   BlockCnt bl;
    1098         801 :   enterblock(fs, &bl, 0);
    1099         801 :   statlist(ls);
    1100         801 :   leaveblock(fs);
    1101         801 : }
    1102             : 
    1103             : 
    1104             : /*
    1105             : ** structure to chain all variables in the left-hand side of an
    1106             : ** assignment
    1107             : */
    1108             : struct LHS_assign {
    1109             :   struct LHS_assign *prev;
    1110             :   expdesc v;  /* variable (global, local, upvalue, or indexed) */
    1111             : };
    1112             : 
    1113             : 
    1114             : /*
    1115             : ** check whether, in an assignment to an upvalue/local variable, the
    1116             : ** upvalue/local variable is begin used in a previous assignment to a
    1117             : ** table. If so, save original upvalue/local value in a safe place and
    1118             : ** use this safe copy in the previous assignment.
    1119             : */
    1120          61 : static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
    1121          61 :   FuncState *fs = ls->fs;
    1122          61 :   int extra = fs->freereg;  /* eventual position to save local variable */
    1123          61 :   int conflict = 0;
    1124         130 :   for (; lh; lh = lh->prev) {  /* check all previous assignments */
    1125          69 :     if (lh->v.k == VINDEXED) {  /* assigning to a table? */
    1126             :       /* table is the upvalue/local being assigned now? */
    1127          10 :       if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
    1128           0 :         conflict = 1;
    1129           0 :         lh->v.u.ind.vt = VLOCAL;
    1130           0 :         lh->v.u.ind.t = extra;  /* previous assignment will use safe copy */
    1131             :       }
    1132             :       /* index is the local being assigned? (index cannot be upvalue) */
    1133          10 :       if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
    1134           0 :         conflict = 1;
    1135           0 :         lh->v.u.ind.idx = extra;  /* previous assignment will use safe copy */
    1136             :       }
    1137             :     }
    1138             :   }
    1139          61 :   if (conflict) {
    1140             :     /* copy upvalue/local value to a temporary (in position 'extra') */
    1141           0 :     OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
    1142           0 :     luaK_codeABC(fs, op, extra, v->u.info, 0);
    1143           0 :     luaK_reserveregs(fs, 1);
    1144             :   }
    1145          61 : }
    1146             : 
    1147             : 
    1148        4165 : static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
    1149             :   expdesc e;
    1150        4165 :   check_condition(ls, vkisvar(lh->v.k), "syntax error");
    1151        4165 :   if (testnext(ls, ',')) {  /* assignment -> ',' suffixedexp assignment */
    1152             :     struct LHS_assign nv;
    1153          95 :     nv.prev = lh;
    1154          95 :     suffixedexp(ls, &nv.v);
    1155          95 :     if (nv.v.k != VINDEXED)
    1156          61 :       check_conflict(ls, lh, &nv.v);
    1157          95 :     checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS,
    1158             :                     "C levels");
    1159          95 :     assignment(ls, &nv, nvars+1);
    1160             :   }
    1161             :   else {  /* assignment -> '=' explist */
    1162             :     int nexps;
    1163        4070 :     checknext(ls, '=');
    1164        4070 :     nexps = explist(ls, &e);
    1165        4070 :     if (nexps != nvars)
    1166          66 :       adjust_assign(ls, nvars, nexps, &e);
    1167             :     else {
    1168        4004 :       luaK_setoneret(ls->fs, &e);  /* close last expression */
    1169        4004 :       luaK_storevar(ls->fs, &lh->v, &e);
    1170        4004 :       return;  /* avoid default */
    1171             :     }
    1172             :   }
    1173         161 :   init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
    1174         161 :   luaK_storevar(ls->fs, &lh->v, &e);
    1175             : }
    1176             : 
    1177             : 
    1178          58 : static int cond (LexState *ls) {
    1179             :   /* cond -> exp */
    1180             :   expdesc v;
    1181          58 :   expr(ls, &v);  /* read condition */
    1182          58 :   if (v.k == VNIL) v.k = VFALSE;  /* 'falses' are all equal here */
    1183          58 :   luaK_goiftrue(ls->fs, &v);
    1184          58 :   return v.f;
    1185             : }
    1186             : 
    1187             : 
    1188         184 : static void gotostat (LexState *ls, int pc) {
    1189         184 :   int line = ls->linenumber;
    1190             :   TString *label;
    1191             :   int g;
    1192         184 :   if (testnext(ls, TK_GOTO))
    1193         150 :     label = str_checkname(ls);
    1194             :   else {
    1195          34 :     luaX_next(ls);  /* skip break */
    1196          34 :     label = luaS_new(ls->L, "break");
    1197             :   }
    1198         184 :   g = newlabelentry(ls, &ls->dyd->gt, label, line, pc);
    1199         184 :   findlabel(ls, g);  /* close it if label already defined */
    1200         184 : }
    1201             : 
    1202             : 
    1203             : /* check for repeated labels on the same block */
    1204          66 : static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
    1205             :   int i;
    1206          66 :   for (i = fs->bl->firstlabel; i < ll->n; i++) {
    1207           0 :     if (eqstr(label, ll->arr[i].name)) {
    1208           0 :       const char *msg = luaO_pushfstring(fs->ls->L,
    1209             :                           "label '%s' already defined on line %d",
    1210           0 :                           getstr(label), ll->arr[i].line);
    1211           0 :       semerror(fs->ls, msg);
    1212             :     }
    1213             :   }
    1214          66 : }
    1215             : 
    1216             : 
    1217             : /* skip no-op statements */
    1218          66 : static void skipnoopstat (LexState *ls) {
    1219          66 :   while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
    1220           0 :     statement(ls);
    1221          66 : }
    1222             : 
    1223             : 
    1224          66 : static void labelstat (LexState *ls, TString *label, int line) {
    1225             :   /* label -> '::' NAME '::' */
    1226          66 :   FuncState *fs = ls->fs;
    1227          66 :   Labellist *ll = &ls->dyd->label;
    1228             :   int l;  /* index of new label being created */
    1229          66 :   checkrepeated(fs, ll, label);  /* check for repeated labels */
    1230          66 :   checknext(ls, TK_DBCOLON);  /* skip double colon */
    1231             :   /* create new entry for this label */
    1232          66 :   l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs));
    1233          66 :   skipnoopstat(ls);  /* skip other no-op statements */
    1234          66 :   if (block_follow(ls, 0)) {  /* label is last no-op statement in the block? */
    1235             :     /* assume that locals are already out of scope */
    1236          66 :     ll->arr[l].nactvar = fs->bl->nactvar;
    1237             :   }
    1238          66 :   findgotos(ls, &ll->arr[l]);
    1239          66 : }
    1240             : 
    1241             : 
    1242          51 : static void whilestat (LexState *ls, int line) {
    1243             :   /* whilestat -> WHILE cond DO block END */
    1244          51 :   FuncState *fs = ls->fs;
    1245             :   int whileinit;
    1246             :   int condexit;
    1247             :   BlockCnt bl;
    1248          51 :   luaX_next(ls);  /* skip WHILE */
    1249          51 :   whileinit = luaK_getlabel(fs);
    1250          51 :   condexit = cond(ls);
    1251          51 :   enterblock(fs, &bl, 1);
    1252          51 :   checknext(ls, TK_DO);
    1253          51 :   block(ls);
    1254          51 :   luaK_jumpto(fs, whileinit);
    1255          51 :   check_match(ls, TK_END, TK_WHILE, line);
    1256          51 :   leaveblock(fs);
    1257          51 :   luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */
    1258          51 : }
    1259             : 
    1260             : 
    1261           7 : static void repeatstat (LexState *ls, int line) {
    1262             :   /* repeatstat -> REPEAT block UNTIL cond */
    1263             :   int condexit;
    1264           7 :   FuncState *fs = ls->fs;
    1265           7 :   int repeat_init = luaK_getlabel(fs);
    1266             :   BlockCnt bl1, bl2;
    1267           7 :   enterblock(fs, &bl1, 1);  /* loop block */
    1268           7 :   enterblock(fs, &bl2, 0);  /* scope block */
    1269           7 :   luaX_next(ls);  /* skip REPEAT */
    1270           7 :   statlist(ls);
    1271           7 :   check_match(ls, TK_UNTIL, TK_REPEAT, line);
    1272           7 :   condexit = cond(ls);  /* read condition (inside scope block) */
    1273           7 :   if (bl2.upval)  /* upvalues? */
    1274           0 :     luaK_patchclose(fs, condexit, bl2.nactvar);
    1275           7 :   leaveblock(fs);  /* finish scope */
    1276           7 :   luaK_patchlist(fs, condexit, repeat_init);  /* close the loop */
    1277           7 :   leaveblock(fs);  /* finish loop */
    1278           7 : }
    1279             : 
    1280             : 
    1281         178 : static int exp1 (LexState *ls) {
    1282             :   expdesc e;
    1283             :   int reg;
    1284         178 :   expr(ls, &e);
    1285         178 :   luaK_exp2nextreg(ls->fs, &e);
    1286             :   lua_assert(e.k == VNONRELOC);
    1287         178 :   reg = e.u.info;
    1288         178 :   return reg;
    1289             : }
    1290             : 
    1291             : 
    1292         400 : static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
    1293             :   /* forbody -> DO block */
    1294             :   BlockCnt bl;
    1295         400 :   FuncState *fs = ls->fs;
    1296             :   int prep, endfor;
    1297         400 :   adjustlocalvars(ls, 3);  /* control variables */
    1298         400 :   checknext(ls, TK_DO);
    1299         400 :   prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
    1300         400 :   enterblock(fs, &bl, 0);  /* scope for declared variables */
    1301         400 :   adjustlocalvars(ls, nvars);
    1302         400 :   luaK_reserveregs(fs, nvars);
    1303         400 :   block(ls);
    1304         400 :   leaveblock(fs);  /* end of scope for declared variables */
    1305         400 :   luaK_patchtohere(fs, prep);
    1306         400 :   if (isnum)  /* numeric for? */
    1307          88 :     endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
    1308             :   else {  /* generic for */
    1309         312 :     luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
    1310         312 :     luaK_fixline(fs, line);
    1311         312 :     endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP);
    1312             :   }
    1313         400 :   luaK_patchlist(fs, endfor, prep + 1);
    1314         400 :   luaK_fixline(fs, line);
    1315         400 : }
    1316             : 
    1317             : 
    1318          88 : static void fornum (LexState *ls, TString *varname, int line) {
    1319             :   /* fornum -> NAME = exp1,exp1[,exp1] forbody */
    1320          88 :   FuncState *fs = ls->fs;
    1321          88 :   int base = fs->freereg;
    1322          88 :   new_localvarliteral(ls, "(for index)");
    1323          88 :   new_localvarliteral(ls, "(for limit)");
    1324          88 :   new_localvarliteral(ls, "(for step)");
    1325          88 :   new_localvar(ls, varname);
    1326          88 :   checknext(ls, '=');
    1327          88 :   exp1(ls);  /* initial value */
    1328          88 :   checknext(ls, ',');
    1329          88 :   exp1(ls);  /* limit */
    1330          88 :   if (testnext(ls, ','))
    1331           2 :     exp1(ls);  /* optional step */
    1332             :   else {  /* default step = 1 */
    1333          86 :     luaK_codek(fs, fs->freereg, luaK_intK(fs, 1));
    1334          86 :     luaK_reserveregs(fs, 1);
    1335             :   }
    1336          88 :   forbody(ls, base, line, 1, 1);
    1337          88 : }
    1338             : 
    1339             : 
    1340         312 : static void forlist (LexState *ls, TString *indexname) {
    1341             :   /* forlist -> NAME {,NAME} IN explist forbody */
    1342         312 :   FuncState *fs = ls->fs;
    1343             :   expdesc e;
    1344         312 :   int nvars = 4;  /* gen, state, control, plus at least one declared var */
    1345             :   int line;
    1346         312 :   int base = fs->freereg;
    1347             :   /* create control variables */
    1348         312 :   new_localvarliteral(ls, "(for generator)");
    1349         312 :   new_localvarliteral(ls, "(for state)");
    1350         312 :   new_localvarliteral(ls, "(for control)");
    1351             :   /* create declared variables */
    1352         312 :   new_localvar(ls, indexname);
    1353         616 :   while (testnext(ls, ',')) {
    1354         304 :     new_localvar(ls, str_checkname(ls));
    1355         304 :     nvars++;
    1356             :   }
    1357         312 :   checknext(ls, TK_IN);
    1358         312 :   line = ls->linenumber;
    1359         312 :   adjust_assign(ls, 3, explist(ls, &e), &e);
    1360         312 :   luaK_checkstack(fs, 3);  /* extra space to call generator */
    1361         312 :   forbody(ls, base, line, nvars - 3, 0);
    1362         312 : }
    1363             : 
    1364             : 
    1365         400 : static void forstat (LexState *ls, int line) {
    1366             :   /* forstat -> FOR (fornum | forlist) END */
    1367         400 :   FuncState *fs = ls->fs;
    1368             :   TString *varname;
    1369             :   BlockCnt bl;
    1370         400 :   enterblock(fs, &bl, 1);  /* scope for loop and control variables */
    1371         400 :   luaX_next(ls);  /* skip 'for' */
    1372         400 :   varname = str_checkname(ls);  /* first variable name */
    1373         400 :   switch (ls->t.token) {
    1374          88 :     case '=': fornum(ls, varname, line); break;
    1375         312 :     case ',': case TK_IN: forlist(ls, varname); break;
    1376           0 :     default: luaX_syntaxerror(ls, "'=' or 'in' expected");
    1377             :   }
    1378         400 :   check_match(ls, TK_END, TK_FOR, line);
    1379         400 :   leaveblock(fs);  /* loop scope ('break' jumps to this point) */
    1380         400 : }
    1381             : 
    1382             : 
    1383        3372 : static void test_then_block (LexState *ls, int *escapelist) {
    1384             :   /* test_then_block -> [IF | ELSEIF] cond THEN block */
    1385             :   BlockCnt bl;
    1386        3372 :   FuncState *fs = ls->fs;
    1387             :   expdesc v;
    1388             :   int jf;  /* instruction to skip 'then' code (if condition is false) */
    1389        3372 :   luaX_next(ls);  /* skip IF or ELSEIF */
    1390        3372 :   expr(ls, &v);  /* read condition */
    1391        3372 :   checknext(ls, TK_THEN);
    1392        3372 :   if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) {
    1393          94 :     luaK_goiffalse(ls->fs, &v);  /* will jump to label if condition is true */
    1394          94 :     enterblock(fs, &bl, 0);  /* must enter block before 'goto' */
    1395          94 :     gotostat(ls, v.t);  /* handle goto/break */
    1396          94 :     while (testnext(ls, ';')) {}  /* skip colons */
    1397          94 :     if (block_follow(ls, 0)) {  /* 'goto' is the entire block? */
    1398          94 :       leaveblock(fs);
    1399          94 :       return;  /* and that is it */
    1400             :     }
    1401             :     else  /* must skip over 'then' part if condition is false */
    1402           0 :       jf = luaK_jump(fs);
    1403             :   }
    1404             :   else {  /* regular case (not goto/break) */
    1405        3278 :     luaK_goiftrue(ls->fs, &v);  /* skip over block if condition is false */
    1406        3278 :     enterblock(fs, &bl, 0);
    1407        3278 :     jf = v.f;
    1408             :   }
    1409        3278 :   statlist(ls);  /* 'then' part */
    1410        3278 :   leaveblock(fs);
    1411        3278 :   if (ls->t.token == TK_ELSE ||
    1412        2972 :       ls->t.token == TK_ELSEIF)  /* followed by 'else'/'elseif'? */
    1413         461 :     luaK_concat(fs, escapelist, luaK_jump(fs));  /* must jump over it */
    1414        3278 :   luaK_patchtohere(fs, jf);
    1415             : }
    1416             : 
    1417             : 
    1418        3215 : static void ifstat (LexState *ls, int line) {
    1419             :   /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
    1420        3215 :   FuncState *fs = ls->fs;
    1421        3215 :   int escapelist = NO_JUMP;  /* exit list for finished parts */
    1422        3215 :   test_then_block(ls, &escapelist);  /* IF cond THEN block */
    1423        3372 :   while (ls->t.token == TK_ELSEIF)
    1424         157 :     test_then_block(ls, &escapelist);  /* ELSEIF cond THEN block */
    1425        3215 :   if (testnext(ls, TK_ELSE))
    1426         306 :     block(ls);  /* 'else' part */
    1427        3215 :   check_match(ls, TK_END, TK_IF, line);
    1428        3215 :   luaK_patchtohere(fs, escapelist);  /* patch escape list to 'if' end */
    1429        3215 : }
    1430             : 
    1431             : 
    1432         657 : static void localfunc (LexState *ls) {
    1433             :   expdesc b;
    1434         657 :   FuncState *fs = ls->fs;
    1435         657 :   new_localvar(ls, str_checkname(ls));  /* new local variable */
    1436         657 :   adjustlocalvars(ls, 1);  /* enter its scope */
    1437         657 :   body(ls, &b, 0, ls->linenumber);  /* function created in next register */
    1438             :   /* debug information will only see the variable after this point! */
    1439         657 :   getlocvar(fs, b.u.info)->startpc = fs->pc;
    1440         657 : }
    1441             : 
    1442             : 
    1443        4522 : static void localstat (LexState *ls) {
    1444             :   /* stat -> LOCAL NAME {',' NAME} ['=' explist] */
    1445        4522 :   int nvars = 0;
    1446             :   int nexps;
    1447             :   expdesc e;
    1448             :   do {
    1449        4792 :     new_localvar(ls, str_checkname(ls));
    1450        4792 :     nvars++;
    1451        4792 :   } while (testnext(ls, ','));
    1452        4522 :   if (testnext(ls, '='))
    1453        4326 :     nexps = explist(ls, &e);
    1454             :   else {
    1455         196 :     e.k = VVOID;
    1456         196 :     nexps = 0;
    1457             :   }
    1458        4522 :   adjust_assign(ls, nvars, nexps, &e);
    1459        4522 :   adjustlocalvars(ls, nvars);
    1460        4522 : }
    1461             : 
    1462             : 
    1463         938 : static int funcname (LexState *ls, expdesc *v) {
    1464             :   /* funcname -> NAME {fieldsel} [':' NAME] */
    1465         938 :   int ismethod = 0;
    1466         938 :   singlevar(ls, v);
    1467        1219 :   while (ls->t.token == '.')
    1468         281 :     fieldsel(ls, v);
    1469         938 :   if (ls->t.token == ':') {
    1470         115 :     ismethod = 1;
    1471         115 :     fieldsel(ls, v);
    1472             :   }
    1473         938 :   return ismethod;
    1474             : }
    1475             : 
    1476             : 
    1477         938 : static void funcstat (LexState *ls, int line) {
    1478             :   /* funcstat -> FUNCTION funcname body */
    1479             :   int ismethod;
    1480             :   expdesc v, b;
    1481         938 :   luaX_next(ls);  /* skip FUNCTION */
    1482         938 :   ismethod = funcname(ls, &v);
    1483         938 :   body(ls, &b, ismethod, line);
    1484         938 :   luaK_storevar(ls->fs, &v, &b);
    1485         938 :   luaK_fixline(ls->fs, line);  /* definition "happens" in the first line */
    1486         938 : }
    1487             : 
    1488             : 
    1489        8616 : static void exprstat (LexState *ls) {
    1490             :   /* stat -> func | assignment */
    1491        8616 :   FuncState *fs = ls->fs;
    1492             :   struct LHS_assign v;
    1493        8616 :   suffixedexp(ls, &v.v);
    1494        8616 :   if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */
    1495        4070 :     v.prev = NULL;
    1496        4070 :     assignment(ls, &v, 1);
    1497             :   }
    1498             :   else {  /* stat -> func */
    1499        4546 :     check_condition(ls, v.v.k == VCALL, "syntax error");
    1500        4546 :     SETARG_C(getinstruction(fs, &v.v), 1);  /* call statement uses no results */
    1501             :   }
    1502        8616 : }
    1503             : 
    1504             : 
    1505        2657 : static void retstat (LexState *ls) {
    1506             :   /* stat -> RETURN [explist] [';'] */
    1507        2657 :   FuncState *fs = ls->fs;
    1508             :   expdesc e;
    1509             :   int first, nret;  /* registers with returned values */
    1510        2657 :   if (block_follow(ls, 1) || ls->t.token == ';')
    1511         414 :     first = nret = 0;  /* return no values */
    1512             :   else {
    1513        2243 :     nret = explist(ls, &e);  /* optional return values */
    1514        2243 :     if (hasmultret(e.k)) {
    1515         394 :       luaK_setmultret(fs, &e);
    1516         394 :       if (e.k == VCALL && nret == 1) {  /* tail call? */
    1517         391 :         SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
    1518             :         lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar);
    1519             :       }
    1520         394 :       first = fs->nactvar;
    1521         394 :       nret = LUA_MULTRET;  /* return all values */
    1522             :     }
    1523             :     else {
    1524        1849 :       if (nret == 1)  /* only one single value? */
    1525        1586 :         first = luaK_exp2anyreg(fs, &e);
    1526             :       else {
    1527         263 :         luaK_exp2nextreg(fs, &e);  /* values must go to the stack */
    1528         263 :         first = fs->nactvar;  /* return all active values */
    1529             :         lua_assert(nret == fs->freereg - first);
    1530             :       }
    1531             :     }
    1532             :   }
    1533        2657 :   luaK_ret(fs, first, nret);
    1534        2657 :   testnext(ls, ';');  /* skip optional semicolon */
    1535        2657 : }
    1536             : 
    1537             : 
    1538       21303 : static void statement (LexState *ls) {
    1539       21303 :   int line = ls->linenumber;  /* may be needed for error messages */
    1540       21303 :   enterlevel(ls);
    1541       21303 :   switch (ls->t.token) {
    1542          40 :     case ';': {  /* stat -> ';' (empty statement) */
    1543          40 :       luaX_next(ls);  /* skip ';' */
    1544          40 :       break;
    1545             :     }
    1546        3215 :     case TK_IF: {  /* stat -> ifstat */
    1547        3215 :       ifstat(ls, line);
    1548        3215 :       break;
    1549             :     }
    1550          51 :     case TK_WHILE: {  /* stat -> whilestat */
    1551          51 :       whilestat(ls, line);
    1552          51 :       break;
    1553             :     }
    1554          44 :     case TK_DO: {  /* stat -> DO block END */
    1555          44 :       luaX_next(ls);  /* skip DO */
    1556          44 :       block(ls);
    1557          44 :       check_match(ls, TK_END, TK_DO, line);
    1558          44 :       break;
    1559             :     }
    1560         400 :     case TK_FOR: {  /* stat -> forstat */
    1561         400 :       forstat(ls, line);
    1562         400 :       break;
    1563             :     }
    1564           7 :     case TK_REPEAT: {  /* stat -> repeatstat */
    1565           7 :       repeatstat(ls, line);
    1566           7 :       break;
    1567             :     }
    1568         938 :     case TK_FUNCTION: {  /* stat -> funcstat */
    1569         938 :       funcstat(ls, line);
    1570         938 :       break;
    1571             :     }
    1572        5179 :     case TK_LOCAL: {  /* stat -> localstat */
    1573        5179 :       luaX_next(ls);  /* skip LOCAL */
    1574        5179 :       if (testnext(ls, TK_FUNCTION))  /* local function? */
    1575         657 :         localfunc(ls);
    1576             :       else
    1577        4522 :         localstat(ls);
    1578        5179 :       break;
    1579             :     }
    1580          66 :     case TK_DBCOLON: {  /* stat -> label */
    1581          66 :       luaX_next(ls);  /* skip double colon */
    1582          66 :       labelstat(ls, str_checkname(ls), line);
    1583          66 :       break;
    1584             :     }
    1585        2657 :     case TK_RETURN: {  /* stat -> retstat */
    1586        2657 :       luaX_next(ls);  /* skip RETURN */
    1587        2657 :       retstat(ls);
    1588        2657 :       break;
    1589             :     }
    1590          90 :     case TK_BREAK:   /* stat -> breakstat */
    1591             :     case TK_GOTO: {  /* stat -> 'goto' NAME */
    1592          90 :       gotostat(ls, luaK_jump(ls->fs));
    1593          90 :       break;
    1594             :     }
    1595        8616 :     default: {  /* stat -> func | assignment */
    1596        8616 :       exprstat(ls);
    1597        8616 :       break;
    1598             :     }
    1599             :   }
    1600             :   lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
    1601             :              ls->fs->freereg >= ls->fs->nactvar);
    1602       21303 :   ls->fs->freereg = ls->fs->nactvar;  /* free registers */
    1603       21303 :   leavelevel(ls);
    1604       21303 : }
    1605             : 
    1606             : /* }====================================================================== */
    1607             : 
    1608             : 
    1609             : /*
    1610             : ** compiles the main function, which is a regular vararg function with an
    1611             : ** upvalue named LUA_ENV
    1612             : */
    1613         120 : static void mainfunc (LexState *ls, FuncState *fs) {
    1614             :   BlockCnt bl;
    1615             :   expdesc v;
    1616         120 :   open_func(ls, fs, &bl);
    1617         120 :   fs->f->is_vararg = 1;  /* main function is always declared vararg */
    1618         120 :   init_exp(&v, VLOCAL, 0);  /* create and... */
    1619         120 :   newupvalue(fs, ls->envn, &v);  /* ...set environment upvalue */
    1620         120 :   luaC_objbarrier(ls->L, fs->f, ls->envn);
    1621         120 :   luaX_next(ls);  /* read first token */
    1622         120 :   statlist(ls);  /* parse main body */
    1623         120 :   check(ls, TK_EOS);
    1624         120 :   close_func(ls);
    1625         120 : }
    1626             : 
    1627             : 
    1628         120 : LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
    1629             :                        Dyndata *dyd, const char *name, int firstchar) {
    1630             :   LexState lexstate;
    1631             :   FuncState funcstate;
    1632         120 :   LClosure *cl = luaF_newLclosure(L, 1);  /* create main closure */
    1633         120 :   setclLvalue(L, L->top, cl);  /* anchor it (to avoid being collected) */
    1634         120 :   luaD_inctop(L);
    1635         120 :   lexstate.h = luaH_new(L);  /* create table for scanner */
    1636         120 :   sethvalue(L, L->top, lexstate.h);  /* anchor it */
    1637         120 :   luaD_inctop(L);
    1638         120 :   funcstate.f = cl->p = luaF_newproto(L);
    1639         120 :   luaC_objbarrier(L, cl, cl->p);
    1640         120 :   funcstate.f->source = luaS_new(L, name);  /* create and anchor TString */
    1641             :   lua_assert(iswhite(funcstate.f));  /* do not need barrier here */
    1642         120 :   lexstate.buff = buff;
    1643         120 :   lexstate.dyd = dyd;
    1644         120 :   dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
    1645         120 :   luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
    1646         120 :   mainfunc(&lexstate, &funcstate);
    1647             :   lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
    1648             :   /* all scopes should be correctly finished */
    1649             :   lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
    1650         120 :   L->top--;  /* remove scanner's table */
    1651         120 :   return cl;  /* closure is on the stack, too */
    1652             : }
    1653             : 

Generated by: LCOV version 1.14