LCOV - code coverage report
Current view: top level - src - lparser.c Coverage Total Hit
Test: Lua 5.2.4 Lines: 97.5 % 948 924
Test Date: 2024-04-28 10:23:12
Legend: Lines: hit not hit

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

Generated by: LCOV version 2.0-1