LCOV - code coverage report
Current view: top level - src - lparser.c Coverage Total Hit
Test: Lua 5.1.5 Lines: 96.9 % 797 772
Test Date: 2024-04-28 10:23:09
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : ** $Id: lparser.c,v 2.42.1.4 2011/10/21 19:31:42 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              : #define hasmultret(k)           ((k) == VCALL || (k) == VVARARG)
      31              : 
      32              : #define getlocvar(fs, i)        ((fs)->f->locvars[(fs)->actvar[i]])
      33              : 
      34              : #define luaY_checklimit(fs,v,l,m)       if ((v)>(l)) errorlimit(fs,l,m)
      35              : 
      36              : 
      37              : /*
      38              : ** nodes for block list (list of active blocks)
      39              : */
      40              : typedef struct BlockCnt {
      41              :   struct BlockCnt *previous;  /* chain */
      42              :   int breaklist;  /* list of jumps out of this loop */
      43              :   lu_byte nactvar;  /* # active locals outside the breakable structure */
      44              :   lu_byte upval;  /* true if some variable in the block is an upvalue */
      45              :   lu_byte isbreakable;  /* true if `block' is a loop */
      46              : } BlockCnt;
      47              : 
      48              : 
      49              : 
      50              : /*
      51              : ** prototypes for recursive non-terminal functions
      52              : */
      53              : static void chunk (LexState *ls);
      54              : static void expr (LexState *ls, expdesc *v);
      55              : 
      56              : 
      57         2135 : static void anchor_token (LexState *ls) {
      58         2135 :   if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
      59          146 :     TString *ts = ls->t.seminfo.ts;
      60          146 :     luaX_newstring(ls, getstr(ts), ts->tsv.len);
      61              :   }
      62         2135 : }
      63              : 
      64              : 
      65           12 : static void error_expected (LexState *ls, int token) {
      66           24 :   luaX_syntaxerror(ls,
      67           12 :       luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token)));
      68            0 : }
      69              : 
      70              : 
      71            0 : static void errorlimit (FuncState *fs, int limit, const char *what) {
      72            0 :   const char *msg = (fs->f->linedefined == 0) ?
      73            0 :     luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
      74            0 :     luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
      75            0 :                             fs->f->linedefined, limit, what);
      76            0 :   luaX_lexerror(fs->ls, msg, 0);
      77            0 : }
      78              : 
      79              : 
      80        58478 : static int testnext (LexState *ls, int c) {
      81        58478 :   if (ls->t.token == c) {
      82        23337 :     luaX_next(ls);
      83        23337 :     return 1;
      84              :   }
      85        35141 :   else return 0;
      86              : }
      87              : 
      88              : 
      89        40692 : static void check (LexState *ls, int c) {
      90        40692 :   if (ls->t.token != c)
      91            3 :     error_expected(ls, c);
      92        40689 : }
      93              : 
      94         9542 : static void checknext (LexState *ls, int c) {
      95         9542 :   check(ls, c);
      96         9539 :   luaX_next(ls);
      97         9531 : }
      98              : 
      99              : 
     100              : #define check_condition(ls,c,msg)       { if (!(c)) luaX_syntaxerror(ls, msg); }
     101              : 
     102              : 
     103              : 
     104        13292 : static void check_match (LexState *ls, int what, int who, int where) {
     105        13292 :   if (!testnext(ls, what)) {
     106           11 :     if (where == ls->linenumber)
     107            9 :       error_expected(ls, what);
     108              :     else {
     109            2 :       luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
     110              :              LUA_QS " expected (to close " LUA_QS " at line %d)",
     111              :               luaX_token2str(ls, what), luaX_token2str(ls, who), where));
     112              :     }
     113              :   }
     114        13281 : }
     115              : 
     116              : 
     117        30707 : static TString *str_checkname (LexState *ls) {
     118              :   TString *ts;
     119        30707 :   check(ls, TK_NAME);
     120        30707 :   ts = ls->t.seminfo.ts;
     121        30707 :   luaX_next(ls);
     122        30707 :   return ts;
     123              : }
     124              : 
     125              : 
     126        50357 : static void init_exp (expdesc *e, expkind k, int i) {
     127        50357 :   e->f = e->t = NO_JUMP;
     128        50357 :   e->k = k;
     129        50357 :   e->u.s.info = i;
     130        50357 : }
     131              : 
     132              : 
     133        10354 : static void codestring (LexState *ls, expdesc *e, TString *s) {
     134        10354 :   init_exp(e, VK, luaK_stringK(ls->fs, s));
     135        10354 : }
     136              : 
     137              : 
     138         3067 : static void checkname(LexState *ls, expdesc *e) {
     139         3067 :   codestring(ls, e, str_checkname(ls));
     140         3067 : }
     141              : 
     142              : 
     143         5953 : static int registerlocalvar (LexState *ls, TString *varname) {
     144         5953 :   FuncState *fs = ls->fs;
     145         5953 :   Proto *f = fs->f;
     146         5953 :   int oldsize = f->sizelocvars;
     147         5953 :   luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
     148              :                   LocVar, SHRT_MAX, "too many local variables");
     149        15761 :   while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
     150         5953 :   f->locvars[fs->nlocvars].varname = varname;
     151         5953 :   luaC_objbarrier(ls->L, f, varname);
     152         5953 :   return fs->nlocvars++;
     153              : }
     154              : 
     155              : 
     156              : #define new_localvarliteral(ls,v,n) \
     157              :   new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
     158              : 
     159              : 
     160         5953 : static void new_localvar (LexState *ls, TString *name, int n) {
     161         5953 :   FuncState *fs = ls->fs;
     162         5953 :   luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
     163         5953 :   fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));
     164         5953 : }
     165              : 
     166              : 
     167         4569 : static void adjustlocalvars (LexState *ls, int nvars) {
     168         4569 :   FuncState *fs = ls->fs;
     169         4569 :   fs->nactvar = cast_byte(fs->nactvar + nvars);
     170        10520 :   for (; nvars; nvars--) {
     171         5951 :     getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
     172              :   }
     173         4569 : }
     174              : 
     175              : 
     176         5385 : static void removevars (LexState *ls, int tolevel) {
     177         5385 :   FuncState *fs = ls->fs;
     178        11330 :   while (fs->nactvar > tolevel)
     179         5945 :     getlocvar(fs, --fs->nactvar).endpc = fs->pc;
     180         5385 : }
     181              : 
     182              : 
     183         2810 : static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
     184              :   int i;
     185         2810 :   Proto *f = fs->f;
     186         2810 :   int oldsize = f->sizeupvalues;
     187         4304 :   for (i=0; i<f->nups; i++) {
     188         2350 :     if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {
     189              :       lua_assert(f->upvalues[i] == name);
     190          856 :       return i;
     191              :     }
     192              :   }
     193              :   /* new one */
     194         1954 :   luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues");
     195         1954 :   luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,
     196              :                   TString *, MAX_INT, "");
     197         6466 :   while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;
     198         1954 :   f->upvalues[f->nups] = name;
     199         1954 :   luaC_objbarrier(fs->L, f, name);
     200              :   lua_assert(v->k == VLOCAL || v->k == VUPVAL);
     201         1954 :   fs->upvalues[f->nups].k = cast_byte(v->k);
     202         1954 :   fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
     203         1954 :   return f->nups++;
     204              : }
     205              : 
     206              : 
     207        28200 : static int searchvar (FuncState *fs, TString *n) {
     208              :   int i;
     209       150904 :   for (i=fs->nactvar-1; i >= 0; i--) {
     210       135954 :     if (n == getlocvar(fs, i).varname)
     211        13250 :       return i;
     212              :   }
     213        14950 :   return -1;  /* not found */
     214              : }
     215              : 
     216              : 
     217         2588 : static void markupval (FuncState *fs, int level) {
     218         2588 :   BlockCnt *bl = fs->bl;
     219         2603 :   while (bl && bl->nactvar > level) bl = bl->previous;
     220         2588 :   if (bl) bl->upval = 1;
     221         2588 : }
     222              : 
     223              : 
     224        37625 : static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
     225        37625 :   if (fs == NULL) {  /* no more levels? */
     226         9425 :     init_exp(var, VGLOBAL, NO_REG);  /* default is global variable */
     227         9425 :     return VGLOBAL;
     228              :   }
     229              :   else {
     230        28200 :     int v = searchvar(fs, n);  /* look up at current level */
     231        28200 :     if (v >= 0) {
     232        13250 :       init_exp(var, VLOCAL, v);
     233        13250 :       if (!base)
     234         2588 :         markupval(fs, v);  /* local will be used as an upval */
     235        13250 :       return VLOCAL;
     236              :     }
     237              :     else {  /* not found at current level; try upper one */
     238        14950 :       if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
     239        12140 :         return VGLOBAL;
     240         2810 :       var->u.s.info = indexupvalue(fs, n, var);  /* else was LOCAL or UPVAL */
     241         2810 :       var->k = VUPVAL;  /* upvalue in this level */
     242         2810 :       return VUPVAL;
     243              :     }
     244              :   }
     245              : }
     246              : 
     247              : 
     248        22675 : static void singlevar (LexState *ls, expdesc *var) {
     249        22675 :   TString *varname = str_checkname(ls);
     250        22675 :   FuncState *fs = ls->fs;
     251        22675 :   if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
     252         9425 :     var->u.s.info = luaK_stringK(fs, varname);  /* info points to global name */
     253        22675 : }
     254              : 
     255              : 
     256         2150 : static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
     257         2150 :   FuncState *fs = ls->fs;
     258         2150 :   int extra = nvars - nexps;
     259         2150 :   if (hasmultret(e->k)) {
     260          582 :     extra++;  /* includes call itself */
     261          582 :     if (extra < 0) extra = 0;
     262          582 :     luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */
     263          582 :     if (extra > 1) luaK_reserveregs(fs, extra-1);
     264              :   }
     265              :   else {
     266         1568 :     if (e->k != VVOID) luaK_exp2nextreg(fs, e);  /* close last expression */
     267         1568 :     if (extra > 0) {
     268           63 :       int reg = fs->freereg;
     269           63 :       luaK_reserveregs(fs, extra);
     270           63 :       luaK_nil(fs, reg, extra);
     271              :     }
     272              :   }
     273         2150 : }
     274              : 
     275              : 
     276        33067 : static void enterlevel (LexState *ls) {
     277        33067 :   if (++ls->L->nCcalls > LUAI_MAXCCALLS)
     278            0 :         luaX_lexerror(ls, "chunk has too many syntax levels", 0);
     279        33067 : }
     280              : 
     281              : 
     282              : #define leavelevel(ls)  ((ls)->L->nCcalls--)
     283              : 
     284              : 
     285         3261 : static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
     286         3261 :   bl->breaklist = NO_JUMP;
     287         3261 :   bl->isbreakable = isbreakable;
     288         3261 :   bl->nactvar = fs->nactvar;
     289         3261 :   bl->upval = 0;
     290         3261 :   bl->previous = fs->bl;
     291         3261 :   fs->bl = bl;
     292              :   lua_assert(fs->freereg == fs->nactvar);
     293         3261 : }
     294              : 
     295              : 
     296         3250 : static void leaveblock (FuncState *fs) {
     297         3250 :   BlockCnt *bl = fs->bl;
     298         3250 :   fs->bl = bl->previous;
     299         3250 :   removevars(fs->ls, bl->nactvar);
     300         3250 :   if (bl->upval)
     301           72 :     luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
     302              :   /* a block either controls scope or breaks (never both) */
     303              :   lua_assert(!bl->isbreakable || !bl->upval);
     304              :   lua_assert(bl->nactvar == fs->nactvar);
     305         3250 :   fs->freereg = fs->nactvar;  /* free registers */
     306         3250 :   luaK_patchtohere(fs, bl->breaklist);
     307         3250 : }
     308              : 
     309              : 
     310         1692 : static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
     311         1692 :   FuncState *fs = ls->fs;
     312         1692 :   Proto *f = fs->f;
     313         1692 :   int oldsize = f->sizep;
     314              :   int i;
     315         1692 :   luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
     316              :                   MAXARG_Bx, "constant table overflow");
     317         4428 :   while (oldsize < f->sizep) f->p[oldsize++] = NULL;
     318         1692 :   f->p[fs->np++] = func->f;
     319         1692 :   luaC_objbarrier(ls->L, f, func->f);
     320         1692 :   init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
     321         3646 :   for (i=0; i<func->f->nups; i++) {
     322         1954 :     OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
     323         1954 :     luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
     324              :   }
     325         1692 : }
     326              : 
     327              : 
     328         2177 : static void open_func (LexState *ls, FuncState *fs) {
     329         2177 :   lua_State *L = ls->L;
     330         2177 :   Proto *f = luaF_newproto(L);
     331         2177 :   fs->f = f;
     332         2177 :   fs->prev = ls->fs;  /* linked list of funcstates */
     333         2177 :   fs->ls = ls;
     334         2177 :   fs->L = L;
     335         2177 :   ls->fs = fs;
     336         2177 :   fs->pc = 0;
     337         2177 :   fs->lasttarget = -1;
     338         2177 :   fs->jpc = NO_JUMP;
     339         2177 :   fs->freereg = 0;
     340         2177 :   fs->nk = 0;
     341         2177 :   fs->np = 0;
     342         2177 :   fs->nlocvars = 0;
     343         2177 :   fs->nactvar = 0;
     344         2177 :   fs->bl = NULL;
     345         2177 :   f->source = ls->source;
     346         2177 :   f->maxstacksize = 2;  /* registers 0/1 are always valid */
     347         2177 :   fs->h = luaH_new(L, 0, 0);
     348              :   /* anchor table of constants and prototype (to avoid being collected) */
     349         2177 :   sethvalue2s(L, L->top, fs->h);
     350         2177 :   incr_top(L);
     351         2177 :   setptvalue2s(L, L->top, f);
     352         2177 :   incr_top(L);
     353         2177 : }
     354              : 
     355              : 
     356         2135 : static void close_func (LexState *ls) {
     357         2135 :   lua_State *L = ls->L;
     358         2135 :   FuncState *fs = ls->fs;
     359         2135 :   Proto *f = fs->f;
     360         2135 :   removevars(ls, 0);
     361         2135 :   luaK_ret(fs, 0, 0);  /* final return */
     362         2135 :   luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
     363         2135 :   f->sizecode = fs->pc;
     364         2135 :   luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
     365         2135 :   f->sizelineinfo = fs->pc;
     366         2135 :   luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
     367         2135 :   f->sizek = fs->nk;
     368         2135 :   luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
     369         2135 :   f->sizep = fs->np;
     370         2135 :   luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
     371         2135 :   f->sizelocvars = fs->nlocvars;
     372         2135 :   luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
     373         2135 :   f->sizeupvalues = f->nups;
     374              :   lua_assert(luaG_checkcode(f));
     375              :   lua_assert(fs->bl == NULL);
     376         2135 :   ls->fs = fs->prev;
     377              :   /* last token read was anchored in defunct function; must reanchor it */
     378         2135 :   if (fs) anchor_token(ls);
     379         2135 :   L->top -= 2;  /* remove table and prototype from the stack */
     380         2135 : }
     381              : 
     382              : 
     383          479 : Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
     384              :   struct LexState lexstate;
     385              :   struct FuncState funcstate;
     386          479 :   lexstate.buff = buff;
     387          479 :   luaX_setinput(L, &lexstate, z, luaS_new(L, name));
     388          479 :   open_func(&lexstate, &funcstate);
     389          479 :   funcstate.f->is_vararg = VARARG_ISVARARG;  /* main func. is always vararg */
     390          479 :   luaX_next(&lexstate);  /* read first token */
     391          478 :   chunk(&lexstate);
     392          443 :   check(&lexstate, TK_EOS);
     393          443 :   close_func(&lexstate);
     394              :   lua_assert(funcstate.prev == NULL);
     395              :   lua_assert(funcstate.f->nups == 0);
     396              :   lua_assert(lexstate.fs == NULL);
     397          443 :   return funcstate.f;
     398              : }
     399              : 
     400              : 
     401              : 
     402              : /*============================================================*/
     403              : /* GRAMMAR RULES */
     404              : /*============================================================*/
     405              : 
     406              : 
     407         2217 : static void field (LexState *ls, expdesc *v) {
     408              :   /* field -> ['.' | ':'] NAME */
     409         2217 :   FuncState *fs = ls->fs;
     410              :   expdesc key;
     411         2217 :   luaK_exp2anyreg(fs, v);
     412         2217 :   luaX_next(ls);  /* skip the dot or colon */
     413         2217 :   checkname(ls, &key);
     414         2217 :   luaK_indexed(fs, v, &key);
     415         2217 : }
     416              : 
     417              : 
     418          744 : static void yindex (LexState *ls, expdesc *v) {
     419              :   /* index -> '[' expr ']' */
     420          744 :   luaX_next(ls);  /* skip the '[' */
     421          744 :   expr(ls, v);
     422          744 :   luaK_exp2val(ls->fs, v);
     423          744 :   checknext(ls, ']');
     424          744 : }
     425              : 
     426              : 
     427              : /*
     428              : ** {======================================================================
     429              : ** Rules for Constructors
     430              : ** =======================================================================
     431              : */
     432              : 
     433              : 
     434              : struct ConsControl {
     435              :   expdesc v;  /* last list item read */
     436              :   expdesc *t;  /* table descriptor */
     437              :   int nh;  /* total number of `record' elements */
     438              :   int na;  /* total number of array elements */
     439              :   int tostore;  /* number of array elements pending to be stored */
     440              : };
     441              : 
     442              : 
     443          350 : static void recfield (LexState *ls, struct ConsControl *cc) {
     444              :   /* recfield -> (NAME | `['exp1`]') = exp1 */
     445          350 :   FuncState *fs = ls->fs;
     446          350 :   int reg = ls->fs->freereg;
     447              :   expdesc key, val;
     448              :   int rkkey;
     449          350 :   if (ls->t.token == TK_NAME) {
     450          330 :     luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
     451          330 :     checkname(ls, &key);
     452              :   }
     453              :   else  /* ls->t.token == '[' */
     454           20 :     yindex(ls, &key);
     455          350 :   cc->nh++;
     456          350 :   checknext(ls, '=');
     457          350 :   rkkey = luaK_exp2RK(fs, &key);
     458          350 :   expr(ls, &val);
     459          350 :   luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
     460          350 :   fs->freereg = reg;  /* free registers */
     461          350 : }
     462              : 
     463              : 
     464         1455 : static void closelistfield (FuncState *fs, struct ConsControl *cc) {
     465         1455 :   if (cc->v.k == VVOID) return;  /* there is no list item */
     466          697 :   luaK_exp2nextreg(fs, &cc->v);
     467          697 :   cc->v.k = VVOID;
     468          697 :   if (cc->tostore == LFIELDS_PER_FLUSH) {
     469            4 :     luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);  /* flush */
     470            4 :     cc->tostore = 0;  /* no more items pending */
     471              :   }
     472              : }
     473              : 
     474              : 
     475          714 : static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
     476          714 :   if (cc->tostore == 0) return;
     477          412 :   if (hasmultret(cc->v.k)) {
     478          233 :     luaK_setmultret(fs, &cc->v);
     479          233 :     luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);
     480          233 :     cc->na--;  /* do not count last expression (unknown number of elements) */
     481              :   }
     482              :   else {
     483          179 :     if (cc->v.k != VVOID)
     484          174 :       luaK_exp2nextreg(fs, &cc->v);
     485          179 :     luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);
     486              :   }
     487              : }
     488              : 
     489              : 
     490         1105 : static void listfield (LexState *ls, struct ConsControl *cc) {
     491         1105 :   expr(ls, &cc->v);
     492         1105 :   luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
     493         1105 :   cc->na++;
     494         1105 :   cc->tostore++;
     495         1105 : }
     496              : 
     497              : 
     498          715 : static void constructor (LexState *ls, expdesc *t) {
     499              :   /* constructor -> ?? */
     500          715 :   FuncState *fs = ls->fs;
     501          715 :   int line = ls->linenumber;
     502          715 :   int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
     503              :   struct ConsControl cc;
     504          715 :   cc.na = cc.nh = cc.tostore = 0;
     505          715 :   cc.t = t;
     506          715 :   init_exp(t, VRELOCABLE, pc);
     507          715 :   init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
     508          715 :   luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */
     509          715 :   checknext(ls, '{');
     510              :   do {
     511              :     lua_assert(cc.v.k == VVOID || cc.tostore > 0);
     512         1693 :     if (ls->t.token == '}') break;
     513         1455 :     closelistfield(fs, &cc);
     514         1455 :     switch(ls->t.token) {
     515          564 :       case TK_NAME: {  /* may be listfields or recfields */
     516          564 :         luaX_lookahead(ls);
     517          564 :         if (ls->lookahead.token != '=')  /* expression? */
     518          234 :           listfield(ls, &cc);
     519              :         else
     520          330 :           recfield(ls, &cc);
     521          564 :         break;
     522              :       }
     523           20 :       case '[': {  /* constructor_item -> recfield */
     524           20 :         recfield(ls, &cc);
     525           20 :         break;
     526              :       }
     527          871 :       default: {  /* constructor_part -> listfield */
     528          871 :         listfield(ls, &cc);
     529          871 :         break;
     530              :       }
     531              :     }
     532         1455 :   } while (testnext(ls, ',') || testnext(ls, ';'));
     533          715 :   check_match(ls, '}', '{', line);
     534          714 :   lastlistfield(fs, &cc);
     535          714 :   SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
     536          714 :   SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */
     537          714 : }
     538              : 
     539              : /* }====================================================================== */
     540              : 
     541              : 
     542              : 
     543         1698 : static void parlist (LexState *ls) {
     544              :   /* parlist -> [ param { `,' param } ] */
     545         1698 :   FuncState *fs = ls->fs;
     546         1698 :   Proto *f = fs->f;
     547         1698 :   int nparams = 0;
     548         1698 :   f->is_vararg = 0;
     549         1698 :   if (ls->t.token != ')') {  /* is `parlist' not empty? */
     550              :     do {
     551         2169 :       switch (ls->t.token) {
     552         2150 :         case TK_NAME: {  /* param -> NAME */
     553         2150 :           new_localvar(ls, str_checkname(ls), nparams++);
     554         2150 :           break;
     555              :         }
     556           18 :         case TK_DOTS: {  /* param -> `...' */
     557           18 :           luaX_next(ls);
     558              : #if defined(LUA_COMPAT_VARARG)
     559              :           /* use `arg' as default name */
     560           18 :           new_localvarliteral(ls, "arg", nparams++);
     561           18 :           f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
     562              : #endif
     563           18 :           f->is_vararg |= VARARG_ISVARARG;
     564           18 :           break;
     565              :         }
     566            1 :         default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
     567              :       }
     568         2168 :     } while (!f->is_vararg && testnext(ls, ','));
     569              :   }
     570         1697 :   adjustlocalvars(ls, nparams);
     571         1697 :   f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
     572         1697 :   luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
     573         1697 : }
     574              : 
     575              : 
     576         1698 : static void body (LexState *ls, expdesc *e, int needself, int line) {
     577              :   /* body ->  `(' parlist `)' chunk END */
     578              :   FuncState new_fs;
     579         1698 :   open_func(ls, &new_fs);
     580         1698 :   new_fs.f->linedefined = line;
     581         1698 :   checknext(ls, '(');
     582         1698 :   if (needself) {
     583           20 :     new_localvarliteral(ls, "self", 0);
     584           20 :     adjustlocalvars(ls, 1);
     585              :   }
     586         1698 :   parlist(ls);
     587         1697 :   checknext(ls, ')');
     588         1697 :   chunk(ls);
     589         1693 :   new_fs.f->lastlinedefined = ls->linenumber;
     590         1693 :   check_match(ls, TK_END, TK_FUNCTION, line);
     591         1692 :   close_func(ls);
     592         1692 :   pushclosure(ls, &new_fs, e);
     593         1692 : }
     594              : 
     595              : 
     596        13296 : static int explist1 (LexState *ls, expdesc *v) {
     597              :   /* explist1 -> expr { `,' expr } */
     598        13296 :   int n = 1;  /* at least one expression */
     599        13296 :   expr(ls, v);
     600        18693 :   while (testnext(ls, ',')) {
     601         5402 :     luaK_exp2nextreg(ls->fs, v);
     602         5402 :     expr(ls, v);
     603         5402 :     n++;
     604              :   }
     605        13291 :   return n;
     606              : }
     607              : 
     608              : 
     609         9206 : static void funcargs (LexState *ls, expdesc *f) {
     610         9206 :   FuncState *fs = ls->fs;
     611              :   expdesc args;
     612              :   int base, nparams;
     613         9206 :   int line = ls->linenumber;
     614         9206 :   switch (ls->t.token) {
     615         8749 :     case '(': {  /* funcargs -> `(' [ explist1 ] `)' */
     616         8749 :       if (line != ls->lastline)
     617            0 :         luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
     618         8749 :       luaX_next(ls);
     619         8749 :       if (ls->t.token == ')')  /* arg list is empty? */
     620          384 :         args.k = VVOID;
     621              :       else {
     622         8365 :         explist1(ls, &args);
     623         8364 :         luaK_setmultret(fs, &args);
     624              :       }
     625         8748 :       check_match(ls, ')', '(', line);
     626         8747 :       break;
     627              :     }
     628           14 :     case '{': {  /* funcargs -> constructor */
     629           14 :       constructor(ls, &args);
     630           14 :       break;
     631              :     }
     632          442 :     case TK_STRING: {  /* funcargs -> STRING */
     633          442 :       codestring(ls, &args, ls->t.seminfo.ts);
     634          442 :       luaX_next(ls);  /* must use `seminfo' before `next' */
     635          442 :       break;
     636              :     }
     637            1 :     default: {
     638            1 :       luaX_syntaxerror(ls, "function arguments expected");
     639            0 :       return;
     640              :     }
     641              :   }
     642              :   lua_assert(f->k == VNONRELOC);
     643         9203 :   base = f->u.s.info;  /* base register for call */
     644         9203 :   if (hasmultret(args.k))
     645          142 :     nparams = LUA_MULTRET;  /* open call */
     646              :   else {
     647         9061 :     if (args.k != VVOID)
     648         8677 :       luaK_exp2nextreg(fs, &args);  /* close last argument */
     649         9061 :     nparams = fs->freereg - (base+1);
     650              :   }
     651         9203 :   init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
     652         9203 :   luaK_fixline(fs, line);
     653         9203 :   fs->freereg = base+1;  /* call remove function and arguments and leaves
     654              :                             (unless changed) one result */
     655              : }
     656              : 
     657              : 
     658              : 
     659              : 
     660              : /*
     661              : ** {======================================================================
     662              : ** Expression parsing
     663              : ** =======================================================================
     664              : */
     665              : 
     666              : 
     667        21848 : static void prefixexp (LexState *ls, expdesc *v) {
     668              :   /* prefixexp -> NAME | '(' expr ')' */
     669        21848 :   switch (ls->t.token) {
     670          136 :     case '(': {
     671          136 :       int line = ls->linenumber;
     672          136 :       luaX_next(ls);
     673          136 :       expr(ls, v);
     674          136 :       check_match(ls, ')', '(', line);
     675          135 :       luaK_dischargevars(ls->fs, v);
     676          135 :       return;
     677              :     }
     678        21704 :     case TK_NAME: {
     679        21704 :       singlevar(ls, v);
     680        21704 :       return;
     681              :     }
     682            8 :     default: {
     683            8 :       luaX_syntaxerror(ls, "unexpected symbol");
     684            0 :       return;
     685              :     }
     686              :   }
     687              : }
     688              : 
     689              : 
     690        21848 : static void primaryexp (LexState *ls, expdesc *v) {
     691              :   /* primaryexp ->
     692              :         prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
     693        21848 :   FuncState *fs = ls->fs;
     694        21848 :   prefixexp(ls, v);
     695              :   for (;;) {
     696        33926 :     switch (ls->t.token) {
     697         2160 :       case '.': {  /* field */
     698         2160 :         field(ls, v);
     699         2160 :         break;
     700              :       }
     701          724 :       case '[': {  /* `[' exp1 `]' */
     702              :         expdesc key;
     703          724 :         luaK_exp2anyreg(fs, v);
     704          724 :         yindex(ls, &key);
     705          724 :         luaK_indexed(fs, v, &key);
     706          724 :         break;
     707              :       }
     708          520 :       case ':': {  /* `:' NAME funcargs */
     709              :         expdesc key;
     710          520 :         luaX_next(ls);
     711          520 :         checkname(ls, &key);
     712          520 :         luaK_self(fs, v, &key);
     713          520 :         funcargs(ls, v);
     714          519 :         break;
     715              :       }
     716         8686 :       case '(': case TK_STRING: case '{': {  /* funcargs */
     717         8686 :         luaK_exp2nextreg(fs, v);
     718         8686 :         funcargs(ls, v);
     719         8684 :         break;
     720              :       }
     721        21836 :       default: return;
     722              :     }
     723              :   }
     724              : }
     725              : 
     726              : 
     727        27239 : static void simpleexp (LexState *ls, expdesc *v) {
     728              :   /* simpleexp -> NUMBER | STRING | NIL | true | false | ... |
     729              :                   constructor | FUNCTION body | primaryexp */
     730        27239 :   switch (ls->t.token) {
     731         3360 :     case TK_NUMBER: {
     732         3360 :       init_exp(v, VKNUM, 0);
     733         3360 :       v->u.nval = ls->t.seminfo.r;
     734         3360 :       break;
     735              :     }
     736         6845 :     case TK_STRING: {
     737         6845 :       codestring(ls, v, ls->t.seminfo.ts);
     738         6845 :       break;
     739              :     }
     740          264 :     case TK_NIL: {
     741          264 :       init_exp(v, VNIL, 0);
     742          264 :       break;
     743              :     }
     744          492 :     case TK_TRUE: {
     745          492 :       init_exp(v, VTRUE, 0);
     746          492 :       break;
     747              :     }
     748          511 :     case TK_FALSE: {
     749          511 :       init_exp(v, VFALSE, 0);
     750          511 :       break;
     751              :     }
     752           21 :     case TK_DOTS: {  /* vararg */
     753           21 :       FuncState *fs = ls->fs;
     754           21 :       check_condition(ls, fs->f->is_vararg,
     755              :                       "cannot use " LUA_QL("...") " outside a vararg function");
     756           20 :       fs->f->is_vararg &= ~VARARG_NEEDSARG;  /* don't need 'arg' */
     757           20 :       init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
     758           20 :       break;
     759              :     }
     760          701 :     case '{': {  /* constructor */
     761          701 :       constructor(ls, v);
     762          700 :       return;
     763              :     }
     764          543 :     case TK_FUNCTION: {
     765          543 :       luaX_next(ls);
     766          543 :       body(ls, v, 0, ls->linenumber);
     767          543 :       return;
     768              :     }
     769        14502 :     default: {
     770        14502 :       primaryexp(ls, v);
     771        14499 :       return;
     772              :     }
     773              :   }
     774        11492 :   luaX_next(ls);
     775              : }
     776              : 
     777              : 
     778        28332 : static UnOpr getunopr (int op) {
     779        28332 :   switch (op) {
     780          447 :     case TK_NOT: return OPR_NOT;
     781          127 :     case '-': return OPR_MINUS;
     782          519 :     case '#': return OPR_LEN;
     783        27239 :     default: return OPR_NOUNOPR;
     784              :   }
     785              : }
     786              : 
     787              : 
     788        28327 : static BinOpr getbinopr (int op) {
     789        28327 :   switch (op) {
     790          351 :     case '+': return OPR_ADD;
     791          279 :     case '-': return OPR_SUB;
     792           77 :     case '*': return OPR_MUL;
     793           45 :     case '/': return OPR_DIV;
     794           26 :     case '%': return OPR_MOD;
     795           29 :     case '^': return OPR_POW;
     796         2099 :     case TK_CONCAT: return OPR_CONCAT;
     797          233 :     case TK_NE: return OPR_NE;
     798          588 :     case TK_EQ: return OPR_EQ;
     799           45 :     case '<': return OPR_LT;
     800           82 :     case TK_LE: return OPR_LE;
     801           36 :     case '>': return OPR_GT;
     802          191 :     case TK_GE: return OPR_GE;
     803          146 :     case TK_AND: return OPR_AND;
     804          232 :     case TK_OR: return OPR_OR;
     805        23868 :     default: return OPR_NOBINOPR;
     806              :   }
     807              : }
     808              : 
     809              : 
     810              : static const struct {
     811              :   lu_byte left;  /* left priority for each binary operator */
     812              :   lu_byte right; /* right priority */
     813              : } priority[] = {  /* ORDER OPR */
     814              :    {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7},  /* `+' `-' `/' `%' */
     815              :    {10, 9}, {5, 4},                 /* power and concat (right associative) */
     816              :    {3, 3}, {3, 3},                  /* equality and inequality */
     817              :    {3, 3}, {3, 3}, {3, 3}, {3, 3},  /* order */
     818              :    {2, 2}, {1, 1}                   /* logical (and/or) */
     819              : };
     820              : 
     821              : #define UNARY_PRIORITY  8  /* priority for unary operators */
     822              : 
     823              : 
     824              : /*
     825              : ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
     826              : ** where `binop' is any binary operator with a priority higher than `limit'
     827              : */
     828        28332 : static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
     829              :   BinOpr op;
     830              :   UnOpr uop;
     831        28332 :   enterlevel(ls);
     832        28332 :   uop = getunopr(ls->t.token);
     833        28332 :   if (uop != OPR_NOUNOPR) {
     834         1093 :     luaX_next(ls);
     835         1093 :     subexpr(ls, v, UNARY_PRIORITY);
     836         1093 :     luaK_prefix(ls->fs, uop, v);
     837              :   }
     838        27239 :   else simpleexp(ls, v);
     839              :   /* expand while operators have priorities higher than `limit' */
     840        28327 :   op = getbinopr(ls->t.token);
     841        32535 :   while (op != OPR_NOBINOPR && priority[op].left > limit) {
     842              :     expdesc v2;
     843              :     BinOpr nextop;
     844         4208 :     luaX_next(ls);
     845         4208 :     luaK_infix(ls->fs, op, v);
     846              :     /* read sub-expression with higher priority */
     847         4208 :     nextop = subexpr(ls, &v2, priority[op].right);
     848         4208 :     luaK_posfix(ls->fs, op, v, &v2);
     849         4208 :     op = nextop;
     850              :   }
     851        28327 :   leavelevel(ls);
     852        28327 :   return op;  /* return first untreated operator */
     853              : }
     854              : 
     855              : 
     856        23031 : static void expr (LexState *ls, expdesc *v) {
     857        23031 :   subexpr(ls, v, 0);
     858        23026 : }
     859              : 
     860              : /* }==================================================================== */
     861              : 
     862              : 
     863              : 
     864              : /*
     865              : ** {======================================================================
     866              : ** Rules for Statements
     867              : ** =======================================================================
     868              : */
     869              : 
     870              : 
     871        18152 : static int block_follow (int token) {
     872        18152 :   switch (token) {
     873         3758 :     case TK_ELSE: case TK_ELSEIF: case TK_END:
     874              :     case TK_UNTIL: case TK_EOS:
     875         3758 :       return 1;
     876        14394 :     default: return 0;
     877              :   }
     878              : }
     879              : 
     880              : 
     881         2556 : static void block (LexState *ls) {
     882              :   /* block -> chunk */
     883         2556 :   FuncState *fs = ls->fs;
     884              :   BlockCnt bl;
     885         2556 :   enterblock(fs, &bl, 0);
     886         2556 :   chunk(ls);
     887              :   lua_assert(bl.breaklist == NO_JUMP);
     888         2554 :   leaveblock(fs);
     889         2554 : }
     890              : 
     891              : 
     892              : /*
     893              : ** structure to chain all variables in the left-hand side of an
     894              : ** assignment
     895              : */
     896              : struct LHS_assign {
     897              :   struct LHS_assign *prev;
     898              :   expdesc v;  /* variable (global, local, upvalue, or indexed) */
     899              : };
     900              : 
     901              : 
     902              : /*
     903              : ** check whether, in an assignment to a local variable, the local variable
     904              : ** is needed in a previous assignment (to a table). If so, save original
     905              : ** local value in a safe place and use this safe copy in the previous
     906              : ** assignment.
     907              : */
     908           90 : static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
     909           90 :   FuncState *fs = ls->fs;
     910           90 :   int extra = fs->freereg;  /* eventual position to save local variable */
     911           90 :   int conflict = 0;
     912          193 :   for (; lh; lh = lh->prev) {
     913          103 :     if (lh->v.k == VINDEXED) {
     914            0 :       if (lh->v.u.s.info == v->u.s.info) {  /* conflict? */
     915            0 :         conflict = 1;
     916            0 :         lh->v.u.s.info = extra;  /* previous assignment will use safe copy */
     917              :       }
     918            0 :       if (lh->v.u.s.aux == v->u.s.info) {  /* conflict? */
     919            0 :         conflict = 1;
     920            0 :         lh->v.u.s.aux = extra;  /* previous assignment will use safe copy */
     921              :       }
     922              :     }
     923              :   }
     924           90 :   if (conflict) {
     925            0 :     luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0);  /* make copy */
     926            0 :     luaK_reserveregs(fs, 1);
     927              :   }
     928           90 : }
     929              : 
     930              : 
     931         2097 : static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
     932              :   expdesc e;
     933         2097 :   check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
     934              :                       "syntax error");
     935         2097 :   if (testnext(ls, ',')) {  /* assignment -> `,' primaryexp assignment */
     936              :     struct LHS_assign nv;
     937           97 :     nv.prev = lh;
     938           97 :     primaryexp(ls, &nv.v);
     939           97 :     if (nv.v.k == VLOCAL)
     940           90 :       check_conflict(ls, lh, &nv.v);
     941           97 :     luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
     942              :                     "variables in assignment");
     943           97 :     assignment(ls, &nv, nvars+1);
     944              :   }
     945              :   else {  /* assignment -> `=' explist1 */
     946              :     int nexps;
     947         2000 :     checknext(ls, '=');
     948         1992 :     nexps = explist1(ls, &e);
     949         1988 :     if (nexps != nvars) {
     950           75 :       adjust_assign(ls, nvars, nexps, &e);
     951           75 :       if (nexps > nvars)
     952            1 :         ls->fs->freereg -= nexps - nvars;  /* remove extra values */
     953              :     }
     954              :     else {
     955         1913 :       luaK_setoneret(ls->fs, &e);  /* close last expression */
     956         1913 :       luaK_storevar(ls->fs, &lh->v, &e);
     957         1913 :       return;  /* avoid default */
     958              :     }
     959              :   }
     960          172 :   init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
     961          172 :   luaK_storevar(ls->fs, &lh->v, &e);
     962              : }
     963              : 
     964              : 
     965         1430 : static int cond (LexState *ls) {
     966              :   /* cond -> exp */
     967              :   expdesc v;
     968         1430 :   expr(ls, &v);  /* read condition */
     969         1430 :   if (v.k == VNIL) v.k = VFALSE;  /* `falses' are all equal here */
     970         1430 :   luaK_goiftrue(ls->fs, &v);
     971         1430 :   return v.f;
     972              : }
     973              : 
     974              : 
     975           14 : static void breakstat (LexState *ls) {
     976           14 :   FuncState *fs = ls->fs;
     977           14 :   BlockCnt *bl = fs->bl;
     978           14 :   int upval = 0;
     979           45 :   while (bl && !bl->isbreakable) {
     980           31 :     upval |= bl->upval;
     981           31 :     bl = bl->previous;
     982              :   }
     983           14 :   if (!bl)
     984            1 :     luaX_syntaxerror(ls, "no loop to break");
     985           13 :   if (upval)
     986            0 :     luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
     987           13 :   luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
     988           13 : }
     989              : 
     990              : 
     991           63 : static void whilestat (LexState *ls, int line) {
     992              :   /* whilestat -> WHILE cond DO block END */
     993           63 :   FuncState *fs = ls->fs;
     994              :   int whileinit;
     995              :   int condexit;
     996              :   BlockCnt bl;
     997           63 :   luaX_next(ls);  /* skip WHILE */
     998           63 :   whileinit = luaK_getlabel(fs);
     999           63 :   condexit = cond(ls);
    1000           63 :   enterblock(fs, &bl, 1);
    1001           63 :   checknext(ls, TK_DO);
    1002           62 :   block(ls);
    1003           61 :   luaK_patchlist(fs, luaK_jump(fs), whileinit);
    1004           61 :   check_match(ls, TK_END, TK_WHILE, line);
    1005           59 :   leaveblock(fs);
    1006           59 :   luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */
    1007           59 : }
    1008              : 
    1009              : 
    1010            4 : static void repeatstat (LexState *ls, int line) {
    1011              :   /* repeatstat -> REPEAT block UNTIL cond */
    1012              :   int condexit;
    1013            4 :   FuncState *fs = ls->fs;
    1014            4 :   int repeat_init = luaK_getlabel(fs);
    1015              :   BlockCnt bl1, bl2;
    1016            4 :   enterblock(fs, &bl1, 1);  /* loop block */
    1017            4 :   enterblock(fs, &bl2, 0);  /* scope block */
    1018            4 :   luaX_next(ls);  /* skip REPEAT */
    1019            4 :   chunk(ls);
    1020            4 :   check_match(ls, TK_UNTIL, TK_REPEAT, line);
    1021            3 :   condexit = cond(ls);  /* read condition (inside scope block) */
    1022            3 :   if (!bl2.upval) {  /* no upvalues? */
    1023            3 :     leaveblock(fs);  /* finish scope */
    1024            3 :     luaK_patchlist(ls->fs, condexit, repeat_init);  /* close the loop */
    1025              :   }
    1026              :   else {  /* complete semantics when there are upvalues */
    1027            0 :     breakstat(ls);  /* if condition then break */
    1028            0 :     luaK_patchtohere(ls->fs, condexit);  /* else... */
    1029            0 :     leaveblock(fs);  /* finish scope... */
    1030            0 :     luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init);  /* and repeat */
    1031              :   }
    1032            3 :   leaveblock(fs);  /* finish loop */
    1033            3 : }
    1034              : 
    1035              : 
    1036          568 : static int exp1 (LexState *ls) {
    1037              :   expdesc e;
    1038              :   int k;
    1039          568 :   expr(ls, &e);
    1040          568 :   k = e.k;
    1041          568 :   luaK_exp2nextreg(ls->fs, &e);
    1042          568 :   return k;
    1043              : }
    1044              : 
    1045              : 
    1046          317 : static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
    1047              :   /* forbody -> DO block */
    1048              :   BlockCnt bl;
    1049          317 :   FuncState *fs = ls->fs;
    1050              :   int prep, endfor;
    1051          317 :   adjustlocalvars(ls, 3);  /* control variables */
    1052          317 :   checknext(ls, TK_DO);
    1053          316 :   prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
    1054          316 :   enterblock(fs, &bl, 0);  /* scope for declared variables */
    1055          316 :   adjustlocalvars(ls, nvars);
    1056          316 :   luaK_reserveregs(fs, nvars);
    1057          316 :   block(ls);
    1058          316 :   leaveblock(fs);  /* end of scope for declared variables */
    1059          316 :   luaK_patchtohere(fs, prep);
    1060          316 :   endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
    1061           40 :                      luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
    1062          316 :   luaK_fixline(fs, line);  /* pretend that `OP_FOR' starts the loop */
    1063          316 :   luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
    1064          316 : }
    1065              : 
    1066              : 
    1067          277 : static void fornum (LexState *ls, TString *varname, int line) {
    1068              :   /* fornum -> NAME = exp1,exp1[,exp1] forbody */
    1069          277 :   FuncState *fs = ls->fs;
    1070          277 :   int base = fs->freereg;
    1071          277 :   new_localvarliteral(ls, "(for index)", 0);
    1072          277 :   new_localvarliteral(ls, "(for limit)", 1);
    1073          277 :   new_localvarliteral(ls, "(for step)", 2);
    1074          277 :   new_localvar(ls, varname, 3);
    1075          277 :   checknext(ls, '=');
    1076          277 :   exp1(ls);  /* initial value */
    1077          277 :   checknext(ls, ',');
    1078          277 :   exp1(ls);  /* limit */
    1079          277 :   if (testnext(ls, ','))
    1080           14 :     exp1(ls);  /* optional step */
    1081              :   else {  /* default step = 1 */
    1082          263 :     luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
    1083          263 :     luaK_reserveregs(fs, 1);
    1084              :   }
    1085          277 :   forbody(ls, base, line, 1, 1);
    1086          276 : }
    1087              : 
    1088              : 
    1089           40 : static void forlist (LexState *ls, TString *indexname) {
    1090              :   /* forlist -> NAME {,NAME} IN explist1 forbody */
    1091           40 :   FuncState *fs = ls->fs;
    1092              :   expdesc e;
    1093           40 :   int nvars = 0;
    1094              :   int line;
    1095           40 :   int base = fs->freereg;
    1096              :   /* create control variables */
    1097           40 :   new_localvarliteral(ls, "(for generator)", nvars++);
    1098           40 :   new_localvarliteral(ls, "(for state)", nvars++);
    1099           40 :   new_localvarliteral(ls, "(for control)", nvars++);
    1100              :   /* create declared variables */
    1101           40 :   new_localvar(ls, indexname, nvars++);
    1102           60 :   while (testnext(ls, ','))
    1103           20 :     new_localvar(ls, str_checkname(ls), nvars++);
    1104           40 :   checknext(ls, TK_IN);
    1105           40 :   line = ls->linenumber;
    1106           40 :   adjust_assign(ls, 3, explist1(ls, &e), &e);
    1107           40 :   luaK_checkstack(fs, 3);  /* extra space to call generator */
    1108           40 :   forbody(ls, base, line, nvars - 3, 0);
    1109           40 : }
    1110              : 
    1111              : 
    1112          318 : static void forstat (LexState *ls, int line) {
    1113              :   /* forstat -> FOR (fornum | forlist) END */
    1114          318 :   FuncState *fs = ls->fs;
    1115              :   TString *varname;
    1116              :   BlockCnt bl;
    1117          318 :   enterblock(fs, &bl, 1);  /* scope for loop and control variables */
    1118          318 :   luaX_next(ls);  /* skip `for' */
    1119          318 :   varname = str_checkname(ls);  /* first variable name */
    1120          318 :   switch (ls->t.token) {
    1121          277 :     case '=': fornum(ls, varname, line); break;
    1122           40 :     case ',': case TK_IN: forlist(ls, varname); break;
    1123            1 :     default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
    1124              :   }
    1125          316 :   check_match(ls, TK_END, TK_FOR, line);
    1126          315 :   leaveblock(fs);  /* loop scope (`break' jumps to this point) */
    1127          315 : }
    1128              : 
    1129              : 
    1130         1364 : static int test_then_block (LexState *ls) {
    1131              :   /* test_then_block -> [IF | ELSEIF] cond THEN block */
    1132              :   int condexit;
    1133         1364 :   luaX_next(ls);  /* skip IF or ELSEIF */
    1134         1364 :   condexit = cond(ls);
    1135         1364 :   checknext(ls, TK_THEN);
    1136         1363 :   block(ls);  /* `then' part */
    1137         1363 :   return condexit;
    1138              : }
    1139              : 
    1140              : 
    1141         1328 : static void ifstat (LexState *ls, int line) {
    1142              :   /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
    1143         1328 :   FuncState *fs = ls->fs;
    1144              :   int flist;
    1145         1328 :   int escapelist = NO_JUMP;
    1146         1328 :   flist = test_then_block(ls);  /* IF cond THEN block */
    1147         1363 :   while (ls->t.token == TK_ELSEIF) {
    1148           36 :     luaK_concat(fs, &escapelist, luaK_jump(fs));
    1149           36 :     luaK_patchtohere(fs, flist);
    1150           36 :     flist = test_then_block(ls);  /* ELSEIF cond THEN block */
    1151              :   }
    1152         1327 :   if (ls->t.token == TK_ELSE) {
    1153          522 :     luaK_concat(fs, &escapelist, luaK_jump(fs));
    1154          522 :     luaK_patchtohere(fs, flist);
    1155          522 :     luaX_next(ls);  /* skip ELSE (after patch, for correct line info) */
    1156          522 :     block(ls);  /* `else' part */
    1157              :   }
    1158              :   else
    1159          805 :     luaK_concat(fs, &escapelist, flist);
    1160         1327 :   luaK_patchtohere(fs, escapelist);
    1161         1327 :   check_match(ls, TK_END, TK_IF, line);
    1162         1325 : }
    1163              : 
    1164              : 
    1165          184 : static void localfunc (LexState *ls) {
    1166              :   expdesc v, b;
    1167          184 :   FuncState *fs = ls->fs;
    1168          184 :   new_localvar(ls, str_checkname(ls), 0);
    1169          184 :   init_exp(&v, VLOCAL, fs->freereg);
    1170          184 :   luaK_reserveregs(fs, 1);
    1171          184 :   adjustlocalvars(ls, 1);
    1172          184 :   body(ls, &b, 0, ls->linenumber);
    1173          184 :   luaK_storevar(fs, &v, &b);
    1174              :   /* debug information will only see the variable after this point! */
    1175          184 :   getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
    1176          184 : }
    1177              : 
    1178              : 
    1179         2035 : static void localstat (LexState *ls) {
    1180              :   /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
    1181         2035 :   int nvars = 0;
    1182              :   int nexps;
    1183              :   expdesc e;
    1184              :   do {
    1185         2293 :     new_localvar(ls, str_checkname(ls), nvars++);
    1186         2293 :   } while (testnext(ls, ','));
    1187         2035 :   if (testnext(ls, '='))
    1188         1976 :     nexps = explist1(ls, &e);
    1189              :   else {
    1190           59 :     e.k = VVOID;
    1191           59 :     nexps = 0;
    1192              :   }
    1193         2035 :   adjust_assign(ls, nvars, nexps, &e);
    1194         2035 :   adjustlocalvars(ls, nvars);
    1195         2035 : }
    1196              : 
    1197              : 
    1198          971 : static int funcname (LexState *ls, expdesc *v) {
    1199              :   /* funcname -> NAME {field} [`:' NAME] */
    1200          971 :   int needself = 0;
    1201          971 :   singlevar(ls, v);
    1202         1008 :   while (ls->t.token == '.')
    1203           37 :     field(ls, v);
    1204          971 :   if (ls->t.token == ':') {
    1205           20 :     needself = 1;
    1206           20 :     field(ls, v);
    1207              :   }
    1208          971 :   return needself;
    1209              : }
    1210              : 
    1211              : 
    1212          971 : static void funcstat (LexState *ls, int line) {
    1213              :   /* funcstat -> FUNCTION funcname body */
    1214              :   int needself;
    1215              :   expdesc v, b;
    1216          971 :   luaX_next(ls);  /* skip FUNCTION */
    1217          971 :   needself = funcname(ls, &v);
    1218          971 :   body(ls, &b, needself, line);
    1219          965 :   luaK_storevar(ls->fs, &v, &b);
    1220          965 :   luaK_fixline(ls->fs, line);  /* definition `happens' in the first line */
    1221          965 : }
    1222              : 
    1223              : 
    1224         7249 : static void exprstat (LexState *ls) {
    1225              :   /* stat -> func | assignment */
    1226         7249 :   FuncState *fs = ls->fs;
    1227              :   struct LHS_assign v;
    1228         7249 :   primaryexp(ls, &v.v);
    1229         7240 :   if (v.v.k == VCALL)  /* stat -> func */
    1230         5240 :     SETARG_C(getcode(fs, &v.v), 1);  /* call statement uses no results */
    1231              :   else {  /* stat -> assignment */
    1232         2000 :     v.prev = NULL;
    1233         2000 :     assignment(ls, &v, 1);
    1234              :   }
    1235         7228 : }
    1236              : 
    1237              : 
    1238         1012 : static void retstat (LexState *ls) {
    1239              :   /* stat -> RETURN explist */
    1240         1012 :   FuncState *fs = ls->fs;
    1241              :   expdesc e;
    1242              :   int first, nret;  /* registers with returned values */
    1243         1012 :   luaX_next(ls);  /* skip RETURN */
    1244         1012 :   if (block_follow(ls->t.token) || ls->t.token == ';')
    1245           89 :     first = nret = 0;  /* return no values */
    1246              :   else {
    1247          923 :     nret = explist1(ls, &e);  /* optional return values */
    1248          923 :     if (hasmultret(e.k)) {
    1249          193 :       luaK_setmultret(fs, &e);
    1250          193 :       if (e.k == VCALL && nret == 1) {  /* tail call? */
    1251          192 :         SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
    1252              :         lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
    1253              :       }
    1254          193 :       first = fs->nactvar;
    1255          193 :       nret = LUA_MULTRET;  /* return all values */
    1256              :     }
    1257              :     else {
    1258          730 :       if (nret == 1)  /* only one single value? */
    1259          712 :         first = luaK_exp2anyreg(fs, &e);
    1260              :       else {
    1261           18 :         luaK_exp2nextreg(fs, &e);  /* values must go to the `stack' */
    1262           18 :         first = fs->nactvar;  /* return all `active' values */
    1263              :         lua_assert(nret == fs->freereg - first);
    1264              :       }
    1265              :     }
    1266              :   }
    1267         1012 :   luaK_ret(fs, first, nret);
    1268         1012 : }
    1269              : 
    1270              : 
    1271        13471 : static int statement (LexState *ls) {
    1272        13471 :   int line = ls->linenumber;  /* may be needed for error messages */
    1273        13471 :   switch (ls->t.token) {
    1274         1328 :     case TK_IF: {  /* stat -> ifstat */
    1275         1328 :       ifstat(ls, line);
    1276         1325 :       return 0;
    1277              :     }
    1278           63 :     case TK_WHILE: {  /* stat -> whilestat */
    1279           63 :       whilestat(ls, line);
    1280           59 :       return 0;
    1281              :     }
    1282          293 :     case TK_DO: {  /* stat -> DO block END */
    1283          293 :       luaX_next(ls);  /* skip DO */
    1284          293 :       block(ls);
    1285          292 :       check_match(ls, TK_END, TK_DO, line);
    1286          291 :       return 0;
    1287              :     }
    1288          318 :     case TK_FOR: {  /* stat -> forstat */
    1289          318 :       forstat(ls, line);
    1290          315 :       return 0;
    1291              :     }
    1292            4 :     case TK_REPEAT: {  /* stat -> repeatstat */
    1293            4 :       repeatstat(ls, line);
    1294            3 :       return 0;
    1295              :     }
    1296          971 :     case TK_FUNCTION: {
    1297          971 :       funcstat(ls, line);  /* stat -> funcstat */
    1298          965 :       return 0;
    1299              :     }
    1300         2219 :     case TK_LOCAL: {  /* stat -> localstat */
    1301         2219 :       luaX_next(ls);  /* skip LOCAL */
    1302         2219 :       if (testnext(ls, TK_FUNCTION))  /* local function? */
    1303          184 :         localfunc(ls);
    1304              :       else
    1305         2035 :         localstat(ls);
    1306         2219 :       return 0;
    1307              :     }
    1308         1012 :     case TK_RETURN: {  /* stat -> retstat */
    1309         1012 :       retstat(ls);
    1310         1012 :       return 1;  /* must be last statement */
    1311              :     }
    1312           14 :     case TK_BREAK: {  /* stat -> breakstat */
    1313           14 :       luaX_next(ls);  /* skip BREAK */
    1314           14 :       breakstat(ls);
    1315           13 :       return 1;  /* must be last statement */
    1316              :     }
    1317         7249 :     default: {
    1318         7249 :       exprstat(ls);
    1319         7228 :       return 0;  /* to avoid warnings */
    1320              :     }
    1321              :   }
    1322              : }
    1323              : 
    1324              : 
    1325         4735 : static void chunk (LexState *ls) {
    1326              :   /* chunk -> { stat [`;'] } */
    1327         4735 :   int islast = 0;
    1328         4735 :   enterlevel(ls);
    1329        18165 :   while (!islast && !block_follow(ls->t.token)) {
    1330        13471 :     islast = statement(ls);
    1331        13430 :     testnext(ls, ';');
    1332              :     lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
    1333              :                ls->fs->freereg >= ls->fs->nactvar);
    1334        13430 :     ls->fs->freereg = ls->fs->nactvar;  /* free registers */
    1335              :   }
    1336         4694 :   leavelevel(ls);
    1337         4694 : }
    1338              : 
    1339              : /* }====================================================================== */
        

Generated by: LCOV version 2.0-1