diff src/lua/lparser.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/lparser.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/lparser.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,1339 @@
     1.4 +/*
     1.5 +** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $
     1.6 +** Lua Parser
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <string.h>
    1.12 +
    1.13 +#define lparser_c
    1.14 +#define LUA_CORE
    1.15 +
    1.16 +#include "lua.h"
    1.17 +
    1.18 +#include "lcode.h"
    1.19 +#include "ldebug.h"
    1.20 +#include "ldo.h"
    1.21 +#include "lfunc.h"
    1.22 +#include "llex.h"
    1.23 +#include "lmem.h"
    1.24 +#include "lobject.h"
    1.25 +#include "lopcodes.h"
    1.26 +#include "lparser.h"
    1.27 +#include "lstate.h"
    1.28 +#include "lstring.h"
    1.29 +#include "ltable.h"
    1.30 +
    1.31 +
    1.32 +
    1.33 +#define hasmultret(k)		((k) == VCALL || (k) == VVARARG)
    1.34 +
    1.35 +#define getlocvar(fs, i)	((fs)->f->locvars[(fs)->actvar[i]])
    1.36 +
    1.37 +#define luaY_checklimit(fs,v,l,m)	if ((v)>(l)) errorlimit(fs,l,m)
    1.38 +
    1.39 +
    1.40 +/*
    1.41 +** nodes for block list (list of active blocks)
    1.42 +*/
    1.43 +typedef struct BlockCnt {
    1.44 +  struct BlockCnt *previous;  /* chain */
    1.45 +  int breaklist;  /* list of jumps out of this loop */
    1.46 +  lu_byte nactvar;  /* # active locals outside the breakable structure */
    1.47 +  lu_byte upval;  /* true if some variable in the block is an upvalue */
    1.48 +  lu_byte isbreakable;  /* true if `block' is a loop */
    1.49 +} BlockCnt;
    1.50 +
    1.51 +
    1.52 +
    1.53 +/*
    1.54 +** prototypes for recursive non-terminal functions
    1.55 +*/
    1.56 +static void chunk (LexState *ls);
    1.57 +static void expr (LexState *ls, expdesc *v);
    1.58 +
    1.59 +
    1.60 +static void anchor_token (LexState *ls) {
    1.61 +  if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
    1.62 +    TString *ts = ls->t.seminfo.ts;
    1.63 +    luaX_newstring(ls, getstr(ts), ts->tsv.len);
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +
    1.68 +static void error_expected (LexState *ls, int token) {
    1.69 +  luaX_syntaxerror(ls,
    1.70 +      luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token)));
    1.71 +}
    1.72 +
    1.73 +
    1.74 +static void errorlimit (FuncState *fs, int limit, const char *what) {
    1.75 +  const char *msg = (fs->f->linedefined == 0) ?
    1.76 +    luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
    1.77 +    luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
    1.78 +                            fs->f->linedefined, limit, what);
    1.79 +  luaX_lexerror(fs->ls, msg, 0);
    1.80 +}
    1.81 +
    1.82 +
    1.83 +static int testnext (LexState *ls, int c) {
    1.84 +  if (ls->t.token == c) {
    1.85 +    luaX_next(ls);
    1.86 +    return 1;
    1.87 +  }
    1.88 +  else return 0;
    1.89 +}
    1.90 +
    1.91 +
    1.92 +static void check (LexState *ls, int c) {
    1.93 +  if (ls->t.token != c)
    1.94 +    error_expected(ls, c);
    1.95 +}
    1.96 +
    1.97 +static void checknext (LexState *ls, int c) {
    1.98 +  check(ls, c);
    1.99 +  luaX_next(ls);
   1.100 +}
   1.101 +
   1.102 +
   1.103 +#define check_condition(ls,c,msg)	{ if (!(c)) luaX_syntaxerror(ls, msg); }
   1.104 +
   1.105 +
   1.106 +
   1.107 +static void check_match (LexState *ls, int what, int who, int where) {
   1.108 +  if (!testnext(ls, what)) {
   1.109 +    if (where == ls->linenumber)
   1.110 +      error_expected(ls, what);
   1.111 +    else {
   1.112 +      luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
   1.113 +             LUA_QS " expected (to close " LUA_QS " at line %d)",
   1.114 +              luaX_token2str(ls, what), luaX_token2str(ls, who), where));
   1.115 +    }
   1.116 +  }
   1.117 +}
   1.118 +
   1.119 +
   1.120 +static TString *str_checkname (LexState *ls) {
   1.121 +  TString *ts;
   1.122 +  check(ls, TK_NAME);
   1.123 +  ts = ls->t.seminfo.ts;
   1.124 +  luaX_next(ls);
   1.125 +  return ts;
   1.126 +}
   1.127 +
   1.128 +
   1.129 +static void init_exp (expdesc *e, expkind k, int i) {
   1.130 +  e->f = e->t = NO_JUMP;
   1.131 +  e->k = k;
   1.132 +  e->u.s.info = i;
   1.133 +}
   1.134 +
   1.135 +
   1.136 +static void codestring (LexState *ls, expdesc *e, TString *s) {
   1.137 +  init_exp(e, VK, luaK_stringK(ls->fs, s));
   1.138 +}
   1.139 +
   1.140 +
   1.141 +static void checkname(LexState *ls, expdesc *e) {
   1.142 +  codestring(ls, e, str_checkname(ls));
   1.143 +}
   1.144 +
   1.145 +
   1.146 +static int registerlocalvar (LexState *ls, TString *varname) {
   1.147 +  FuncState *fs = ls->fs;
   1.148 +  Proto *f = fs->f;
   1.149 +  int oldsize = f->sizelocvars;
   1.150 +  luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
   1.151 +                  LocVar, SHRT_MAX, "too many local variables");
   1.152 +  while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
   1.153 +  f->locvars[fs->nlocvars].varname = varname;
   1.154 +  luaC_objbarrier(ls->L, f, varname);
   1.155 +  return fs->nlocvars++;
   1.156 +}
   1.157 +
   1.158 +
   1.159 +#define new_localvarliteral(ls,v,n) \
   1.160 +  new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
   1.161 +
   1.162 +
   1.163 +static void new_localvar (LexState *ls, TString *name, int n) {
   1.164 +  FuncState *fs = ls->fs;
   1.165 +  luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
   1.166 +  fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));
   1.167 +}
   1.168 +
   1.169 +
   1.170 +static void adjustlocalvars (LexState *ls, int nvars) {
   1.171 +  FuncState *fs = ls->fs;
   1.172 +  fs->nactvar = cast_byte(fs->nactvar + nvars);
   1.173 +  for (; nvars; nvars--) {
   1.174 +    getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
   1.175 +  }
   1.176 +}
   1.177 +
   1.178 +
   1.179 +static void removevars (LexState *ls, int tolevel) {
   1.180 +  FuncState *fs = ls->fs;
   1.181 +  while (fs->nactvar > tolevel)
   1.182 +    getlocvar(fs, --fs->nactvar).endpc = fs->pc;
   1.183 +}
   1.184 +
   1.185 +
   1.186 +static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
   1.187 +  int i;
   1.188 +  Proto *f = fs->f;
   1.189 +  int oldsize = f->sizeupvalues;
   1.190 +  for (i=0; i<f->nups; i++) {
   1.191 +    if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {
   1.192 +      lua_assert(f->upvalues[i] == name);
   1.193 +      return i;
   1.194 +    }
   1.195 +  }
   1.196 +  /* new one */
   1.197 +  luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues");
   1.198 +  luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,
   1.199 +                  TString *, MAX_INT, "");
   1.200 +  while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;
   1.201 +  f->upvalues[f->nups] = name;
   1.202 +  luaC_objbarrier(fs->L, f, name);
   1.203 +  lua_assert(v->k == VLOCAL || v->k == VUPVAL);
   1.204 +  fs->upvalues[f->nups].k = cast_byte(v->k);
   1.205 +  fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
   1.206 +  return f->nups++;
   1.207 +}
   1.208 +
   1.209 +
   1.210 +static int searchvar (FuncState *fs, TString *n) {
   1.211 +  int i;
   1.212 +  for (i=fs->nactvar-1; i >= 0; i--) {
   1.213 +    if (n == getlocvar(fs, i).varname)
   1.214 +      return i;
   1.215 +  }
   1.216 +  return -1;  /* not found */
   1.217 +}
   1.218 +
   1.219 +
   1.220 +static void markupval (FuncState *fs, int level) {
   1.221 +  BlockCnt *bl = fs->bl;
   1.222 +  while (bl && bl->nactvar > level) bl = bl->previous;
   1.223 +  if (bl) bl->upval = 1;
   1.224 +}
   1.225 +
   1.226 +
   1.227 +static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
   1.228 +  if (fs == NULL) {  /* no more levels? */
   1.229 +    init_exp(var, VGLOBAL, NO_REG);  /* default is global variable */
   1.230 +    return VGLOBAL;
   1.231 +  }
   1.232 +  else {
   1.233 +    int v = searchvar(fs, n);  /* look up at current level */
   1.234 +    if (v >= 0) {
   1.235 +      init_exp(var, VLOCAL, v);
   1.236 +      if (!base)
   1.237 +        markupval(fs, v);  /* local will be used as an upval */
   1.238 +      return VLOCAL;
   1.239 +    }
   1.240 +    else {  /* not found at current level; try upper one */
   1.241 +      if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
   1.242 +        return VGLOBAL;
   1.243 +      var->u.s.info = indexupvalue(fs, n, var);  /* else was LOCAL or UPVAL */
   1.244 +      var->k = VUPVAL;  /* upvalue in this level */
   1.245 +      return VUPVAL;
   1.246 +    }
   1.247 +  }
   1.248 +}
   1.249 +
   1.250 +
   1.251 +static void singlevar (LexState *ls, expdesc *var) {
   1.252 +  TString *varname = str_checkname(ls);
   1.253 +  FuncState *fs = ls->fs;
   1.254 +  if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
   1.255 +    var->u.s.info = luaK_stringK(fs, varname);  /* info points to global name */
   1.256 +}
   1.257 +
   1.258 +
   1.259 +static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
   1.260 +  FuncState *fs = ls->fs;
   1.261 +  int extra = nvars - nexps;
   1.262 +  if (hasmultret(e->k)) {
   1.263 +    extra++;  /* includes call itself */
   1.264 +    if (extra < 0) extra = 0;
   1.265 +    luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */
   1.266 +    if (extra > 1) luaK_reserveregs(fs, extra-1);
   1.267 +  }
   1.268 +  else {
   1.269 +    if (e->k != VVOID) luaK_exp2nextreg(fs, e);  /* close last expression */
   1.270 +    if (extra > 0) {
   1.271 +      int reg = fs->freereg;
   1.272 +      luaK_reserveregs(fs, extra);
   1.273 +      luaK_nil(fs, reg, extra);
   1.274 +    }
   1.275 +  }
   1.276 +}
   1.277 +
   1.278 +
   1.279 +static void enterlevel (LexState *ls) {
   1.280 +  if (++ls->L->nCcalls > LUAI_MAXCCALLS)
   1.281 +	luaX_lexerror(ls, "chunk has too many syntax levels", 0);
   1.282 +}
   1.283 +
   1.284 +
   1.285 +#define leavelevel(ls)	((ls)->L->nCcalls--)
   1.286 +
   1.287 +
   1.288 +static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
   1.289 +  bl->breaklist = NO_JUMP;
   1.290 +  bl->isbreakable = isbreakable;
   1.291 +  bl->nactvar = fs->nactvar;
   1.292 +  bl->upval = 0;
   1.293 +  bl->previous = fs->bl;
   1.294 +  fs->bl = bl;
   1.295 +  lua_assert(fs->freereg == fs->nactvar);
   1.296 +}
   1.297 +
   1.298 +
   1.299 +static void leaveblock (FuncState *fs) {
   1.300 +  BlockCnt *bl = fs->bl;
   1.301 +  fs->bl = bl->previous;
   1.302 +  removevars(fs->ls, bl->nactvar);
   1.303 +  if (bl->upval)
   1.304 +    luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
   1.305 +  /* a block either controls scope or breaks (never both) */
   1.306 +  lua_assert(!bl->isbreakable || !bl->upval);
   1.307 +  lua_assert(bl->nactvar == fs->nactvar);
   1.308 +  fs->freereg = fs->nactvar;  /* free registers */
   1.309 +  luaK_patchtohere(fs, bl->breaklist);
   1.310 +}
   1.311 +
   1.312 +
   1.313 +static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
   1.314 +  FuncState *fs = ls->fs;
   1.315 +  Proto *f = fs->f;
   1.316 +  int oldsize = f->sizep;
   1.317 +  int i;
   1.318 +  luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
   1.319 +                  MAXARG_Bx, "constant table overflow");
   1.320 +  while (oldsize < f->sizep) f->p[oldsize++] = NULL;
   1.321 +  f->p[fs->np++] = func->f;
   1.322 +  luaC_objbarrier(ls->L, f, func->f);
   1.323 +  init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
   1.324 +  for (i=0; i<func->f->nups; i++) {
   1.325 +    OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
   1.326 +    luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
   1.327 +  }
   1.328 +}
   1.329 +
   1.330 +
   1.331 +static void open_func (LexState *ls, FuncState *fs) {
   1.332 +  lua_State *L = ls->L;
   1.333 +  Proto *f = luaF_newproto(L);
   1.334 +  fs->f = f;
   1.335 +  fs->prev = ls->fs;  /* linked list of funcstates */
   1.336 +  fs->ls = ls;
   1.337 +  fs->L = L;
   1.338 +  ls->fs = fs;
   1.339 +  fs->pc = 0;
   1.340 +  fs->lasttarget = -1;
   1.341 +  fs->jpc = NO_JUMP;
   1.342 +  fs->freereg = 0;
   1.343 +  fs->nk = 0;
   1.344 +  fs->np = 0;
   1.345 +  fs->nlocvars = 0;
   1.346 +  fs->nactvar = 0;
   1.347 +  fs->bl = NULL;
   1.348 +  f->source = ls->source;
   1.349 +  f->maxstacksize = 2;  /* registers 0/1 are always valid */
   1.350 +  fs->h = luaH_new(L, 0, 0);
   1.351 +  /* anchor table of constants and prototype (to avoid being collected) */
   1.352 +  sethvalue2s(L, L->top, fs->h);
   1.353 +  incr_top(L);
   1.354 +  setptvalue2s(L, L->top, f);
   1.355 +  incr_top(L);
   1.356 +}
   1.357 +
   1.358 +
   1.359 +static void close_func (LexState *ls) {
   1.360 +  lua_State *L = ls->L;
   1.361 +  FuncState *fs = ls->fs;
   1.362 +  Proto *f = fs->f;
   1.363 +  removevars(ls, 0);
   1.364 +  luaK_ret(fs, 0, 0);  /* final return */
   1.365 +  luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
   1.366 +  f->sizecode = fs->pc;
   1.367 +  luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
   1.368 +  f->sizelineinfo = fs->pc;
   1.369 +  luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
   1.370 +  f->sizek = fs->nk;
   1.371 +  luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
   1.372 +  f->sizep = fs->np;
   1.373 +  luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
   1.374 +  f->sizelocvars = fs->nlocvars;
   1.375 +  luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
   1.376 +  f->sizeupvalues = f->nups;
   1.377 +  lua_assert(luaG_checkcode(f));
   1.378 +  lua_assert(fs->bl == NULL);
   1.379 +  ls->fs = fs->prev;
   1.380 +  L->top -= 2;  /* remove table and prototype from the stack */
   1.381 +  /* last token read was anchored in defunct function; must reanchor it */
   1.382 +  if (fs) anchor_token(ls);
   1.383 +}
   1.384 +
   1.385 +
   1.386 +Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
   1.387 +  struct LexState lexstate;
   1.388 +  struct FuncState funcstate;
   1.389 +  lexstate.buff = buff;
   1.390 +  luaX_setinput(L, &lexstate, z, luaS_new(L, name));
   1.391 +  open_func(&lexstate, &funcstate);
   1.392 +  funcstate.f->is_vararg = VARARG_ISVARARG;  /* main func. is always vararg */
   1.393 +  luaX_next(&lexstate);  /* read first token */
   1.394 +  chunk(&lexstate);
   1.395 +  check(&lexstate, TK_EOS);
   1.396 +  close_func(&lexstate);
   1.397 +  lua_assert(funcstate.prev == NULL);
   1.398 +  lua_assert(funcstate.f->nups == 0);
   1.399 +  lua_assert(lexstate.fs == NULL);
   1.400 +  return funcstate.f;
   1.401 +}
   1.402 +
   1.403 +
   1.404 +
   1.405 +/*============================================================*/
   1.406 +/* GRAMMAR RULES */
   1.407 +/*============================================================*/
   1.408 +
   1.409 +
   1.410 +static void field (LexState *ls, expdesc *v) {
   1.411 +  /* field -> ['.' | ':'] NAME */
   1.412 +  FuncState *fs = ls->fs;
   1.413 +  expdesc key;
   1.414 +  luaK_exp2anyreg(fs, v);
   1.415 +  luaX_next(ls);  /* skip the dot or colon */
   1.416 +  checkname(ls, &key);
   1.417 +  luaK_indexed(fs, v, &key);
   1.418 +}
   1.419 +
   1.420 +
   1.421 +static void yindex (LexState *ls, expdesc *v) {
   1.422 +  /* index -> '[' expr ']' */
   1.423 +  luaX_next(ls);  /* skip the '[' */
   1.424 +  expr(ls, v);
   1.425 +  luaK_exp2val(ls->fs, v);
   1.426 +  checknext(ls, ']');
   1.427 +}
   1.428 +
   1.429 +
   1.430 +/*
   1.431 +** {======================================================================
   1.432 +** Rules for Constructors
   1.433 +** =======================================================================
   1.434 +*/
   1.435 +
   1.436 +
   1.437 +struct ConsControl {
   1.438 +  expdesc v;  /* last list item read */
   1.439 +  expdesc *t;  /* table descriptor */
   1.440 +  int nh;  /* total number of `record' elements */
   1.441 +  int na;  /* total number of array elements */
   1.442 +  int tostore;  /* number of array elements pending to be stored */
   1.443 +};
   1.444 +
   1.445 +
   1.446 +static void recfield (LexState *ls, struct ConsControl *cc) {
   1.447 +  /* recfield -> (NAME | `['exp1`]') = exp1 */
   1.448 +  FuncState *fs = ls->fs;
   1.449 +  int reg = ls->fs->freereg;
   1.450 +  expdesc key, val;
   1.451 +  int rkkey;
   1.452 +  if (ls->t.token == TK_NAME) {
   1.453 +    luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
   1.454 +    checkname(ls, &key);
   1.455 +  }
   1.456 +  else  /* ls->t.token == '[' */
   1.457 +    yindex(ls, &key);
   1.458 +  cc->nh++;
   1.459 +  checknext(ls, '=');
   1.460 +  rkkey = luaK_exp2RK(fs, &key);
   1.461 +  expr(ls, &val);
   1.462 +  luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
   1.463 +  fs->freereg = reg;  /* free registers */
   1.464 +}
   1.465 +
   1.466 +
   1.467 +static void closelistfield (FuncState *fs, struct ConsControl *cc) {
   1.468 +  if (cc->v.k == VVOID) return;  /* there is no list item */
   1.469 +  luaK_exp2nextreg(fs, &cc->v);
   1.470 +  cc->v.k = VVOID;
   1.471 +  if (cc->tostore == LFIELDS_PER_FLUSH) {
   1.472 +    luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);  /* flush */
   1.473 +    cc->tostore = 0;  /* no more items pending */
   1.474 +  }
   1.475 +}
   1.476 +
   1.477 +
   1.478 +static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
   1.479 +  if (cc->tostore == 0) return;
   1.480 +  if (hasmultret(cc->v.k)) {
   1.481 +    luaK_setmultret(fs, &cc->v);
   1.482 +    luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);
   1.483 +    cc->na--;  /* do not count last expression (unknown number of elements) */
   1.484 +  }
   1.485 +  else {
   1.486 +    if (cc->v.k != VVOID)
   1.487 +      luaK_exp2nextreg(fs, &cc->v);
   1.488 +    luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);
   1.489 +  }
   1.490 +}
   1.491 +
   1.492 +
   1.493 +static void listfield (LexState *ls, struct ConsControl *cc) {
   1.494 +  expr(ls, &cc->v);
   1.495 +  luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
   1.496 +  cc->na++;
   1.497 +  cc->tostore++;
   1.498 +}
   1.499 +
   1.500 +
   1.501 +static void constructor (LexState *ls, expdesc *t) {
   1.502 +  /* constructor -> ?? */
   1.503 +  FuncState *fs = ls->fs;
   1.504 +  int line = ls->linenumber;
   1.505 +  int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
   1.506 +  struct ConsControl cc;
   1.507 +  cc.na = cc.nh = cc.tostore = 0;
   1.508 +  cc.t = t;
   1.509 +  init_exp(t, VRELOCABLE, pc);
   1.510 +  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
   1.511 +  luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */
   1.512 +  checknext(ls, '{');
   1.513 +  do {
   1.514 +    lua_assert(cc.v.k == VVOID || cc.tostore > 0);
   1.515 +    if (ls->t.token == '}') break;
   1.516 +    closelistfield(fs, &cc);
   1.517 +    switch(ls->t.token) {
   1.518 +      case TK_NAME: {  /* may be listfields or recfields */
   1.519 +        luaX_lookahead(ls);
   1.520 +        if (ls->lookahead.token != '=')  /* expression? */
   1.521 +          listfield(ls, &cc);
   1.522 +        else
   1.523 +          recfield(ls, &cc);
   1.524 +        break;
   1.525 +      }
   1.526 +      case '[': {  /* constructor_item -> recfield */
   1.527 +        recfield(ls, &cc);
   1.528 +        break;
   1.529 +      }
   1.530 +      default: {  /* constructor_part -> listfield */
   1.531 +        listfield(ls, &cc);
   1.532 +        break;
   1.533 +      }
   1.534 +    }
   1.535 +  } while (testnext(ls, ',') || testnext(ls, ';'));
   1.536 +  check_match(ls, '}', '{', line);
   1.537 +  lastlistfield(fs, &cc);
   1.538 +  SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
   1.539 +  SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */
   1.540 +}
   1.541 +
   1.542 +/* }====================================================================== */
   1.543 +
   1.544 +
   1.545 +
   1.546 +static void parlist (LexState *ls) {
   1.547 +  /* parlist -> [ param { `,' param } ] */
   1.548 +  FuncState *fs = ls->fs;
   1.549 +  Proto *f = fs->f;
   1.550 +  int nparams = 0;
   1.551 +  f->is_vararg = 0;
   1.552 +  if (ls->t.token != ')') {  /* is `parlist' not empty? */
   1.553 +    do {
   1.554 +      switch (ls->t.token) {
   1.555 +        case TK_NAME: {  /* param -> NAME */
   1.556 +          new_localvar(ls, str_checkname(ls), nparams++);
   1.557 +          break;
   1.558 +        }
   1.559 +        case TK_DOTS: {  /* param -> `...' */
   1.560 +          luaX_next(ls);
   1.561 +#if defined(LUA_COMPAT_VARARG)
   1.562 +          /* use `arg' as default name */
   1.563 +          new_localvarliteral(ls, "arg", nparams++);
   1.564 +          f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
   1.565 +#endif
   1.566 +          f->is_vararg |= VARARG_ISVARARG;
   1.567 +          break;
   1.568 +        }
   1.569 +        default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
   1.570 +      }
   1.571 +    } while (!f->is_vararg && testnext(ls, ','));
   1.572 +  }
   1.573 +  adjustlocalvars(ls, nparams);
   1.574 +  f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
   1.575 +  luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
   1.576 +}
   1.577 +
   1.578 +
   1.579 +static void body (LexState *ls, expdesc *e, int needself, int line) {
   1.580 +  /* body ->  `(' parlist `)' chunk END */
   1.581 +  FuncState new_fs;
   1.582 +  open_func(ls, &new_fs);
   1.583 +  new_fs.f->linedefined = line;
   1.584 +  checknext(ls, '(');
   1.585 +  if (needself) {
   1.586 +    new_localvarliteral(ls, "self", 0);
   1.587 +    adjustlocalvars(ls, 1);
   1.588 +  }
   1.589 +  parlist(ls);
   1.590 +  checknext(ls, ')');
   1.591 +  chunk(ls);
   1.592 +  new_fs.f->lastlinedefined = ls->linenumber;
   1.593 +  check_match(ls, TK_END, TK_FUNCTION, line);
   1.594 +  close_func(ls);
   1.595 +  pushclosure(ls, &new_fs, e);
   1.596 +}
   1.597 +
   1.598 +
   1.599 +static int explist1 (LexState *ls, expdesc *v) {
   1.600 +  /* explist1 -> expr { `,' expr } */
   1.601 +  int n = 1;  /* at least one expression */
   1.602 +  expr(ls, v);
   1.603 +  while (testnext(ls, ',')) {
   1.604 +    luaK_exp2nextreg(ls->fs, v);
   1.605 +    expr(ls, v);
   1.606 +    n++;
   1.607 +  }
   1.608 +  return n;
   1.609 +}
   1.610 +
   1.611 +
   1.612 +static void funcargs (LexState *ls, expdesc *f) {
   1.613 +  FuncState *fs = ls->fs;
   1.614 +  expdesc args;
   1.615 +  int base, nparams;
   1.616 +  int line = ls->linenumber;
   1.617 +  switch (ls->t.token) {
   1.618 +    case '(': {  /* funcargs -> `(' [ explist1 ] `)' */
   1.619 +      if (line != ls->lastline)
   1.620 +        luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
   1.621 +      luaX_next(ls);
   1.622 +      if (ls->t.token == ')')  /* arg list is empty? */
   1.623 +        args.k = VVOID;
   1.624 +      else {
   1.625 +        explist1(ls, &args);
   1.626 +        luaK_setmultret(fs, &args);
   1.627 +      }
   1.628 +      check_match(ls, ')', '(', line);
   1.629 +      break;
   1.630 +    }
   1.631 +    case '{': {  /* funcargs -> constructor */
   1.632 +      constructor(ls, &args);
   1.633 +      break;
   1.634 +    }
   1.635 +    case TK_STRING: {  /* funcargs -> STRING */
   1.636 +      codestring(ls, &args, ls->t.seminfo.ts);
   1.637 +      luaX_next(ls);  /* must use `seminfo' before `next' */
   1.638 +      break;
   1.639 +    }
   1.640 +    default: {
   1.641 +      luaX_syntaxerror(ls, "function arguments expected");
   1.642 +      return;
   1.643 +    }
   1.644 +  }
   1.645 +  lua_assert(f->k == VNONRELOC);
   1.646 +  base = f->u.s.info;  /* base register for call */
   1.647 +  if (hasmultret(args.k))
   1.648 +    nparams = LUA_MULTRET;  /* open call */
   1.649 +  else {
   1.650 +    if (args.k != VVOID)
   1.651 +      luaK_exp2nextreg(fs, &args);  /* close last argument */
   1.652 +    nparams = fs->freereg - (base+1);
   1.653 +  }
   1.654 +  init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
   1.655 +  luaK_fixline(fs, line);
   1.656 +  fs->freereg = base+1;  /* call remove function and arguments and leaves
   1.657 +                            (unless changed) one result */
   1.658 +}
   1.659 +
   1.660 +
   1.661 +
   1.662 +
   1.663 +/*
   1.664 +** {======================================================================
   1.665 +** Expression parsing
   1.666 +** =======================================================================
   1.667 +*/
   1.668 +
   1.669 +
   1.670 +static void prefixexp (LexState *ls, expdesc *v) {
   1.671 +  /* prefixexp -> NAME | '(' expr ')' */
   1.672 +  switch (ls->t.token) {
   1.673 +    case '(': {
   1.674 +      int line = ls->linenumber;
   1.675 +      luaX_next(ls);
   1.676 +      expr(ls, v);
   1.677 +      check_match(ls, ')', '(', line);
   1.678 +      luaK_dischargevars(ls->fs, v);
   1.679 +      return;
   1.680 +    }
   1.681 +    case TK_NAME: {
   1.682 +      singlevar(ls, v);
   1.683 +      return;
   1.684 +    }
   1.685 +    default: {
   1.686 +      luaX_syntaxerror(ls, "unexpected symbol");
   1.687 +      return;
   1.688 +    }
   1.689 +  }
   1.690 +}
   1.691 +
   1.692 +
   1.693 +static void primaryexp (LexState *ls, expdesc *v) {
   1.694 +  /* primaryexp ->
   1.695 +        prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
   1.696 +  FuncState *fs = ls->fs;
   1.697 +  prefixexp(ls, v);
   1.698 +  for (;;) {
   1.699 +    switch (ls->t.token) {
   1.700 +      case '.': {  /* field */
   1.701 +        field(ls, v);
   1.702 +        break;
   1.703 +      }
   1.704 +      case '[': {  /* `[' exp1 `]' */
   1.705 +        expdesc key;
   1.706 +        luaK_exp2anyreg(fs, v);
   1.707 +        yindex(ls, &key);
   1.708 +        luaK_indexed(fs, v, &key);
   1.709 +        break;
   1.710 +      }
   1.711 +      case ':': {  /* `:' NAME funcargs */
   1.712 +        expdesc key;
   1.713 +        luaX_next(ls);
   1.714 +        checkname(ls, &key);
   1.715 +        luaK_self(fs, v, &key);
   1.716 +        funcargs(ls, v);
   1.717 +        break;
   1.718 +      }
   1.719 +      case '(': case TK_STRING: case '{': {  /* funcargs */
   1.720 +        luaK_exp2nextreg(fs, v);
   1.721 +        funcargs(ls, v);
   1.722 +        break;
   1.723 +      }
   1.724 +      default: return;
   1.725 +    }
   1.726 +  }
   1.727 +}
   1.728 +
   1.729 +
   1.730 +static void simpleexp (LexState *ls, expdesc *v) {
   1.731 +  /* simpleexp -> NUMBER | STRING | NIL | true | false | ... |
   1.732 +                  constructor | FUNCTION body | primaryexp */
   1.733 +  switch (ls->t.token) {
   1.734 +    case TK_NUMBER: {
   1.735 +      init_exp(v, VKNUM, 0);
   1.736 +      v->u.nval = ls->t.seminfo.r;
   1.737 +      break;
   1.738 +    }
   1.739 +    case TK_STRING: {
   1.740 +      codestring(ls, v, ls->t.seminfo.ts);
   1.741 +      break;
   1.742 +    }
   1.743 +    case TK_NIL: {
   1.744 +      init_exp(v, VNIL, 0);
   1.745 +      break;
   1.746 +    }
   1.747 +    case TK_TRUE: {
   1.748 +      init_exp(v, VTRUE, 0);
   1.749 +      break;
   1.750 +    }
   1.751 +    case TK_FALSE: {
   1.752 +      init_exp(v, VFALSE, 0);
   1.753 +      break;
   1.754 +    }
   1.755 +    case TK_DOTS: {  /* vararg */
   1.756 +      FuncState *fs = ls->fs;
   1.757 +      check_condition(ls, fs->f->is_vararg,
   1.758 +                      "cannot use " LUA_QL("...") " outside a vararg function");
   1.759 +      fs->f->is_vararg &= ~VARARG_NEEDSARG;  /* don't need 'arg' */
   1.760 +      init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
   1.761 +      break;
   1.762 +    }
   1.763 +    case '{': {  /* constructor */
   1.764 +      constructor(ls, v);
   1.765 +      return;
   1.766 +    }
   1.767 +    case TK_FUNCTION: {
   1.768 +      luaX_next(ls);
   1.769 +      body(ls, v, 0, ls->linenumber);
   1.770 +      return;
   1.771 +    }
   1.772 +    default: {
   1.773 +      primaryexp(ls, v);
   1.774 +      return;
   1.775 +    }
   1.776 +  }
   1.777 +  luaX_next(ls);
   1.778 +}
   1.779 +
   1.780 +
   1.781 +static UnOpr getunopr (int op) {
   1.782 +  switch (op) {
   1.783 +    case TK_NOT: return OPR_NOT;
   1.784 +    case '-': return OPR_MINUS;
   1.785 +    case '#': return OPR_LEN;
   1.786 +    default: return OPR_NOUNOPR;
   1.787 +  }
   1.788 +}
   1.789 +
   1.790 +
   1.791 +static BinOpr getbinopr (int op) {
   1.792 +  switch (op) {
   1.793 +    case '+': return OPR_ADD;
   1.794 +    case '-': return OPR_SUB;
   1.795 +    case '*': return OPR_MUL;
   1.796 +    case '/': return OPR_DIV;
   1.797 +    case '%': return OPR_MOD;
   1.798 +    case '^': return OPR_POW;
   1.799 +    case TK_CONCAT: return OPR_CONCAT;
   1.800 +    case TK_NE: return OPR_NE;
   1.801 +    case TK_EQ: return OPR_EQ;
   1.802 +    case '<': return OPR_LT;
   1.803 +    case TK_LE: return OPR_LE;
   1.804 +    case '>': return OPR_GT;
   1.805 +    case TK_GE: return OPR_GE;
   1.806 +    case TK_AND: return OPR_AND;
   1.807 +    case TK_OR: return OPR_OR;
   1.808 +    default: return OPR_NOBINOPR;
   1.809 +  }
   1.810 +}
   1.811 +
   1.812 +
   1.813 +static const struct {
   1.814 +  lu_byte left;  /* left priority for each binary operator */
   1.815 +  lu_byte right; /* right priority */
   1.816 +} priority[] = {  /* ORDER OPR */
   1.817 +   {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7},  /* `+' `-' `/' `%' */
   1.818 +   {10, 9}, {5, 4},                 /* power and concat (right associative) */
   1.819 +   {3, 3}, {3, 3},                  /* equality and inequality */
   1.820 +   {3, 3}, {3, 3}, {3, 3}, {3, 3},  /* order */
   1.821 +   {2, 2}, {1, 1}                   /* logical (and/or) */
   1.822 +};
   1.823 +
   1.824 +#define UNARY_PRIORITY	8  /* priority for unary operators */
   1.825 +
   1.826 +
   1.827 +/*
   1.828 +** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
   1.829 +** where `binop' is any binary operator with a priority higher than `limit'
   1.830 +*/
   1.831 +static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
   1.832 +  BinOpr op;
   1.833 +  UnOpr uop;
   1.834 +  enterlevel(ls);
   1.835 +  uop = getunopr(ls->t.token);
   1.836 +  if (uop != OPR_NOUNOPR) {
   1.837 +    luaX_next(ls);
   1.838 +    subexpr(ls, v, UNARY_PRIORITY);
   1.839 +    luaK_prefix(ls->fs, uop, v);
   1.840 +  }
   1.841 +  else simpleexp(ls, v);
   1.842 +  /* expand while operators have priorities higher than `limit' */
   1.843 +  op = getbinopr(ls->t.token);
   1.844 +  while (op != OPR_NOBINOPR && priority[op].left > limit) {
   1.845 +    expdesc v2;
   1.846 +    BinOpr nextop;
   1.847 +    luaX_next(ls);
   1.848 +    luaK_infix(ls->fs, op, v);
   1.849 +    /* read sub-expression with higher priority */
   1.850 +    nextop = subexpr(ls, &v2, priority[op].right);
   1.851 +    luaK_posfix(ls->fs, op, v, &v2);
   1.852 +    op = nextop;
   1.853 +  }
   1.854 +  leavelevel(ls);
   1.855 +  return op;  /* return first untreated operator */
   1.856 +}
   1.857 +
   1.858 +
   1.859 +static void expr (LexState *ls, expdesc *v) {
   1.860 +  subexpr(ls, v, 0);
   1.861 +}
   1.862 +
   1.863 +/* }==================================================================== */
   1.864 +
   1.865 +
   1.866 +
   1.867 +/*
   1.868 +** {======================================================================
   1.869 +** Rules for Statements
   1.870 +** =======================================================================
   1.871 +*/
   1.872 +
   1.873 +
   1.874 +static int block_follow (int token) {
   1.875 +  switch (token) {
   1.876 +    case TK_ELSE: case TK_ELSEIF: case TK_END:
   1.877 +    case TK_UNTIL: case TK_EOS:
   1.878 +      return 1;
   1.879 +    default: return 0;
   1.880 +  }
   1.881 +}
   1.882 +
   1.883 +
   1.884 +static void block (LexState *ls) {
   1.885 +  /* block -> chunk */
   1.886 +  FuncState *fs = ls->fs;
   1.887 +  BlockCnt bl;
   1.888 +  enterblock(fs, &bl, 0);
   1.889 +  chunk(ls);
   1.890 +  lua_assert(bl.breaklist == NO_JUMP);
   1.891 +  leaveblock(fs);
   1.892 +}
   1.893 +
   1.894 +
   1.895 +/*
   1.896 +** structure to chain all variables in the left-hand side of an
   1.897 +** assignment
   1.898 +*/
   1.899 +struct LHS_assign {
   1.900 +  struct LHS_assign *prev;
   1.901 +  expdesc v;  /* variable (global, local, upvalue, or indexed) */
   1.902 +};
   1.903 +
   1.904 +
   1.905 +/*
   1.906 +** check whether, in an assignment to a local variable, the local variable
   1.907 +** is needed in a previous assignment (to a table). If so, save original
   1.908 +** local value in a safe place and use this safe copy in the previous
   1.909 +** assignment.
   1.910 +*/
   1.911 +static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
   1.912 +  FuncState *fs = ls->fs;
   1.913 +  int extra = fs->freereg;  /* eventual position to save local variable */
   1.914 +  int conflict = 0;
   1.915 +  for (; lh; lh = lh->prev) {
   1.916 +    if (lh->v.k == VINDEXED) {
   1.917 +      if (lh->v.u.s.info == v->u.s.info) {  /* conflict? */
   1.918 +        conflict = 1;
   1.919 +        lh->v.u.s.info = extra;  /* previous assignment will use safe copy */
   1.920 +      }
   1.921 +      if (lh->v.u.s.aux == v->u.s.info) {  /* conflict? */
   1.922 +        conflict = 1;
   1.923 +        lh->v.u.s.aux = extra;  /* previous assignment will use safe copy */
   1.924 +      }
   1.925 +    }
   1.926 +  }
   1.927 +  if (conflict) {
   1.928 +    luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0);  /* make copy */
   1.929 +    luaK_reserveregs(fs, 1);
   1.930 +  }
   1.931 +}
   1.932 +
   1.933 +
   1.934 +static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
   1.935 +  expdesc e;
   1.936 +  check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
   1.937 +                      "syntax error");
   1.938 +  if (testnext(ls, ',')) {  /* assignment -> `,' primaryexp assignment */
   1.939 +    struct LHS_assign nv;
   1.940 +    nv.prev = lh;
   1.941 +    primaryexp(ls, &nv.v);
   1.942 +    if (nv.v.k == VLOCAL)
   1.943 +      check_conflict(ls, lh, &nv.v);
   1.944 +    luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
   1.945 +                    "variables in assignment");
   1.946 +    assignment(ls, &nv, nvars+1);
   1.947 +  }
   1.948 +  else {  /* assignment -> `=' explist1 */
   1.949 +    int nexps;
   1.950 +    checknext(ls, '=');
   1.951 +    nexps = explist1(ls, &e);
   1.952 +    if (nexps != nvars) {
   1.953 +      adjust_assign(ls, nvars, nexps, &e);
   1.954 +      if (nexps > nvars)
   1.955 +        ls->fs->freereg -= nexps - nvars;  /* remove extra values */
   1.956 +    }
   1.957 +    else {
   1.958 +      luaK_setoneret(ls->fs, &e);  /* close last expression */
   1.959 +      luaK_storevar(ls->fs, &lh->v, &e);
   1.960 +      return;  /* avoid default */
   1.961 +    }
   1.962 +  }
   1.963 +  init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
   1.964 +  luaK_storevar(ls->fs, &lh->v, &e);
   1.965 +}
   1.966 +
   1.967 +
   1.968 +static int cond (LexState *ls) {
   1.969 +  /* cond -> exp */
   1.970 +  expdesc v;
   1.971 +  expr(ls, &v);  /* read condition */
   1.972 +  if (v.k == VNIL) v.k = VFALSE;  /* `falses' are all equal here */
   1.973 +  luaK_goiftrue(ls->fs, &v);
   1.974 +  return v.f;
   1.975 +}
   1.976 +
   1.977 +
   1.978 +static void breakstat (LexState *ls) {
   1.979 +  FuncState *fs = ls->fs;
   1.980 +  BlockCnt *bl = fs->bl;
   1.981 +  int upval = 0;
   1.982 +  while (bl && !bl->isbreakable) {
   1.983 +    upval |= bl->upval;
   1.984 +    bl = bl->previous;
   1.985 +  }
   1.986 +  if (!bl)
   1.987 +    luaX_syntaxerror(ls, "no loop to break");
   1.988 +  if (upval)
   1.989 +    luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
   1.990 +  luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
   1.991 +}
   1.992 +
   1.993 +
   1.994 +static void whilestat (LexState *ls, int line) {
   1.995 +  /* whilestat -> WHILE cond DO block END */
   1.996 +  FuncState *fs = ls->fs;
   1.997 +  int whileinit;
   1.998 +  int condexit;
   1.999 +  BlockCnt bl;
  1.1000 +  luaX_next(ls);  /* skip WHILE */
  1.1001 +  whileinit = luaK_getlabel(fs);
  1.1002 +  condexit = cond(ls);
  1.1003 +  enterblock(fs, &bl, 1);
  1.1004 +  checknext(ls, TK_DO);
  1.1005 +  block(ls);
  1.1006 +  luaK_patchlist(fs, luaK_jump(fs), whileinit);
  1.1007 +  check_match(ls, TK_END, TK_WHILE, line);
  1.1008 +  leaveblock(fs);
  1.1009 +  luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */
  1.1010 +}
  1.1011 +
  1.1012 +
  1.1013 +static void repeatstat (LexState *ls, int line) {
  1.1014 +  /* repeatstat -> REPEAT block UNTIL cond */
  1.1015 +  int condexit;
  1.1016 +  FuncState *fs = ls->fs;
  1.1017 +  int repeat_init = luaK_getlabel(fs);
  1.1018 +  BlockCnt bl1, bl2;
  1.1019 +  enterblock(fs, &bl1, 1);  /* loop block */
  1.1020 +  enterblock(fs, &bl2, 0);  /* scope block */
  1.1021 +  luaX_next(ls);  /* skip REPEAT */
  1.1022 +  chunk(ls);
  1.1023 +  check_match(ls, TK_UNTIL, TK_REPEAT, line);
  1.1024 +  condexit = cond(ls);  /* read condition (inside scope block) */
  1.1025 +  if (!bl2.upval) {  /* no upvalues? */
  1.1026 +    leaveblock(fs);  /* finish scope */
  1.1027 +    luaK_patchlist(ls->fs, condexit, repeat_init);  /* close the loop */
  1.1028 +  }
  1.1029 +  else {  /* complete semantics when there are upvalues */
  1.1030 +    breakstat(ls);  /* if condition then break */
  1.1031 +    luaK_patchtohere(ls->fs, condexit);  /* else... */
  1.1032 +    leaveblock(fs);  /* finish scope... */
  1.1033 +    luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init);  /* and repeat */
  1.1034 +  }
  1.1035 +  leaveblock(fs);  /* finish loop */
  1.1036 +}
  1.1037 +
  1.1038 +
  1.1039 +static int exp1 (LexState *ls) {
  1.1040 +  expdesc e;
  1.1041 +  int k;
  1.1042 +  expr(ls, &e);
  1.1043 +  k = e.k;
  1.1044 +  luaK_exp2nextreg(ls->fs, &e);
  1.1045 +  return k;
  1.1046 +}
  1.1047 +
  1.1048 +
  1.1049 +static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  1.1050 +  /* forbody -> DO block */
  1.1051 +  BlockCnt bl;
  1.1052 +  FuncState *fs = ls->fs;
  1.1053 +  int prep, endfor;
  1.1054 +  adjustlocalvars(ls, 3);  /* control variables */
  1.1055 +  checknext(ls, TK_DO);
  1.1056 +  prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
  1.1057 +  enterblock(fs, &bl, 0);  /* scope for declared variables */
  1.1058 +  adjustlocalvars(ls, nvars);
  1.1059 +  luaK_reserveregs(fs, nvars);
  1.1060 +  block(ls);
  1.1061 +  leaveblock(fs);  /* end of scope for declared variables */
  1.1062 +  luaK_patchtohere(fs, prep);
  1.1063 +  endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  1.1064 +                     luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  1.1065 +  luaK_fixline(fs, line);  /* pretend that `OP_FOR' starts the loop */
  1.1066 +  luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  1.1067 +}
  1.1068 +
  1.1069 +
  1.1070 +static void fornum (LexState *ls, TString *varname, int line) {
  1.1071 +  /* fornum -> NAME = exp1,exp1[,exp1] forbody */
  1.1072 +  FuncState *fs = ls->fs;
  1.1073 +  int base = fs->freereg;
  1.1074 +  new_localvarliteral(ls, "(for index)", 0);
  1.1075 +  new_localvarliteral(ls, "(for limit)", 1);
  1.1076 +  new_localvarliteral(ls, "(for step)", 2);
  1.1077 +  new_localvar(ls, varname, 3);
  1.1078 +  checknext(ls, '=');
  1.1079 +  exp1(ls);  /* initial value */
  1.1080 +  checknext(ls, ',');
  1.1081 +  exp1(ls);  /* limit */
  1.1082 +  if (testnext(ls, ','))
  1.1083 +    exp1(ls);  /* optional step */
  1.1084 +  else {  /* default step = 1 */
  1.1085 +    luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  1.1086 +    luaK_reserveregs(fs, 1);
  1.1087 +  }
  1.1088 +  forbody(ls, base, line, 1, 1);
  1.1089 +}
  1.1090 +
  1.1091 +
  1.1092 +static void forlist (LexState *ls, TString *indexname) {
  1.1093 +  /* forlist -> NAME {,NAME} IN explist1 forbody */
  1.1094 +  FuncState *fs = ls->fs;
  1.1095 +  expdesc e;
  1.1096 +  int nvars = 0;
  1.1097 +  int line;
  1.1098 +  int base = fs->freereg;
  1.1099 +  /* create control variables */
  1.1100 +  new_localvarliteral(ls, "(for generator)", nvars++);
  1.1101 +  new_localvarliteral(ls, "(for state)", nvars++);
  1.1102 +  new_localvarliteral(ls, "(for control)", nvars++);
  1.1103 +  /* create declared variables */
  1.1104 +  new_localvar(ls, indexname, nvars++);
  1.1105 +  while (testnext(ls, ','))
  1.1106 +    new_localvar(ls, str_checkname(ls), nvars++);
  1.1107 +  checknext(ls, TK_IN);
  1.1108 +  line = ls->linenumber;
  1.1109 +  adjust_assign(ls, 3, explist1(ls, &e), &e);
  1.1110 +  luaK_checkstack(fs, 3);  /* extra space to call generator */
  1.1111 +  forbody(ls, base, line, nvars - 3, 0);
  1.1112 +}
  1.1113 +
  1.1114 +
  1.1115 +static void forstat (LexState *ls, int line) {
  1.1116 +  /* forstat -> FOR (fornum | forlist) END */
  1.1117 +  FuncState *fs = ls->fs;
  1.1118 +  TString *varname;
  1.1119 +  BlockCnt bl;
  1.1120 +  enterblock(fs, &bl, 1);  /* scope for loop and control variables */
  1.1121 +  luaX_next(ls);  /* skip `for' */
  1.1122 +  varname = str_checkname(ls);  /* first variable name */
  1.1123 +  switch (ls->t.token) {
  1.1124 +    case '=': fornum(ls, varname, line); break;
  1.1125 +    case ',': case TK_IN: forlist(ls, varname); break;
  1.1126 +    default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
  1.1127 +  }
  1.1128 +  check_match(ls, TK_END, TK_FOR, line);
  1.1129 +  leaveblock(fs);  /* loop scope (`break' jumps to this point) */
  1.1130 +}
  1.1131 +
  1.1132 +
  1.1133 +static int test_then_block (LexState *ls) {
  1.1134 +  /* test_then_block -> [IF | ELSEIF] cond THEN block */
  1.1135 +  int condexit;
  1.1136 +  luaX_next(ls);  /* skip IF or ELSEIF */
  1.1137 +  condexit = cond(ls);
  1.1138 +  checknext(ls, TK_THEN);
  1.1139 +  block(ls);  /* `then' part */
  1.1140 +  return condexit;
  1.1141 +}
  1.1142 +
  1.1143 +
  1.1144 +static void ifstat (LexState *ls, int line) {
  1.1145 +  /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  1.1146 +  FuncState *fs = ls->fs;
  1.1147 +  int flist;
  1.1148 +  int escapelist = NO_JUMP;
  1.1149 +  flist = test_then_block(ls);  /* IF cond THEN block */
  1.1150 +  while (ls->t.token == TK_ELSEIF) {
  1.1151 +    luaK_concat(fs, &escapelist, luaK_jump(fs));
  1.1152 +    luaK_patchtohere(fs, flist);
  1.1153 +    flist = test_then_block(ls);  /* ELSEIF cond THEN block */
  1.1154 +  }
  1.1155 +  if (ls->t.token == TK_ELSE) {
  1.1156 +    luaK_concat(fs, &escapelist, luaK_jump(fs));
  1.1157 +    luaK_patchtohere(fs, flist);
  1.1158 +    luaX_next(ls);  /* skip ELSE (after patch, for correct line info) */
  1.1159 +    block(ls);  /* `else' part */
  1.1160 +  }
  1.1161 +  else
  1.1162 +    luaK_concat(fs, &escapelist, flist);
  1.1163 +  luaK_patchtohere(fs, escapelist);
  1.1164 +  check_match(ls, TK_END, TK_IF, line);
  1.1165 +}
  1.1166 +
  1.1167 +
  1.1168 +static void localfunc (LexState *ls) {
  1.1169 +  expdesc v, b;
  1.1170 +  FuncState *fs = ls->fs;
  1.1171 +  new_localvar(ls, str_checkname(ls), 0);
  1.1172 +  init_exp(&v, VLOCAL, fs->freereg);
  1.1173 +  luaK_reserveregs(fs, 1);
  1.1174 +  adjustlocalvars(ls, 1);
  1.1175 +  body(ls, &b, 0, ls->linenumber);
  1.1176 +  luaK_storevar(fs, &v, &b);
  1.1177 +  /* debug information will only see the variable after this point! */
  1.1178 +  getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
  1.1179 +}
  1.1180 +
  1.1181 +
  1.1182 +static void localstat (LexState *ls) {
  1.1183 +  /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  1.1184 +  int nvars = 0;
  1.1185 +  int nexps;
  1.1186 +  expdesc e;
  1.1187 +  do {
  1.1188 +    new_localvar(ls, str_checkname(ls), nvars++);
  1.1189 +  } while (testnext(ls, ','));
  1.1190 +  if (testnext(ls, '='))
  1.1191 +    nexps = explist1(ls, &e);
  1.1192 +  else {
  1.1193 +    e.k = VVOID;
  1.1194 +    nexps = 0;
  1.1195 +  }
  1.1196 +  adjust_assign(ls, nvars, nexps, &e);
  1.1197 +  adjustlocalvars(ls, nvars);
  1.1198 +}
  1.1199 +
  1.1200 +
  1.1201 +static int funcname (LexState *ls, expdesc *v) {
  1.1202 +  /* funcname -> NAME {field} [`:' NAME] */
  1.1203 +  int needself = 0;
  1.1204 +  singlevar(ls, v);
  1.1205 +  while (ls->t.token == '.')
  1.1206 +    field(ls, v);
  1.1207 +  if (ls->t.token == ':') {
  1.1208 +    needself = 1;
  1.1209 +    field(ls, v);
  1.1210 +  }
  1.1211 +  return needself;
  1.1212 +}
  1.1213 +
  1.1214 +
  1.1215 +static void funcstat (LexState *ls, int line) {
  1.1216 +  /* funcstat -> FUNCTION funcname body */
  1.1217 +  int needself;
  1.1218 +  expdesc v, b;
  1.1219 +  luaX_next(ls);  /* skip FUNCTION */
  1.1220 +  needself = funcname(ls, &v);
  1.1221 +  body(ls, &b, needself, line);
  1.1222 +  luaK_storevar(ls->fs, &v, &b);
  1.1223 +  luaK_fixline(ls->fs, line);  /* definition `happens' in the first line */
  1.1224 +}
  1.1225 +
  1.1226 +
  1.1227 +static void exprstat (LexState *ls) {
  1.1228 +  /* stat -> func | assignment */
  1.1229 +  FuncState *fs = ls->fs;
  1.1230 +  struct LHS_assign v;
  1.1231 +  primaryexp(ls, &v.v);
  1.1232 +  if (v.v.k == VCALL)  /* stat -> func */
  1.1233 +    SETARG_C(getcode(fs, &v.v), 1);  /* call statement uses no results */
  1.1234 +  else {  /* stat -> assignment */
  1.1235 +    v.prev = NULL;
  1.1236 +    assignment(ls, &v, 1);
  1.1237 +  }
  1.1238 +}
  1.1239 +
  1.1240 +
  1.1241 +static void retstat (LexState *ls) {
  1.1242 +  /* stat -> RETURN explist */
  1.1243 +  FuncState *fs = ls->fs;
  1.1244 +  expdesc e;
  1.1245 +  int first, nret;  /* registers with returned values */
  1.1246 +  luaX_next(ls);  /* skip RETURN */
  1.1247 +  if (block_follow(ls->t.token) || ls->t.token == ';')
  1.1248 +    first = nret = 0;  /* return no values */
  1.1249 +  else {
  1.1250 +    nret = explist1(ls, &e);  /* optional return values */
  1.1251 +    if (hasmultret(e.k)) {
  1.1252 +      luaK_setmultret(fs, &e);
  1.1253 +      if (e.k == VCALL && nret == 1) {  /* tail call? */
  1.1254 +        SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1.1255 +        lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1.1256 +      }
  1.1257 +      first = fs->nactvar;
  1.1258 +      nret = LUA_MULTRET;  /* return all values */
  1.1259 +    }
  1.1260 +    else {
  1.1261 +      if (nret == 1)  /* only one single value? */
  1.1262 +        first = luaK_exp2anyreg(fs, &e);
  1.1263 +      else {
  1.1264 +        luaK_exp2nextreg(fs, &e);  /* values must go to the `stack' */
  1.1265 +        first = fs->nactvar;  /* return all `active' values */
  1.1266 +        lua_assert(nret == fs->freereg - first);
  1.1267 +      }
  1.1268 +    }
  1.1269 +  }
  1.1270 +  luaK_ret(fs, first, nret);
  1.1271 +}
  1.1272 +
  1.1273 +
  1.1274 +static int statement (LexState *ls) {
  1.1275 +  int line = ls->linenumber;  /* may be needed for error messages */
  1.1276 +  switch (ls->t.token) {
  1.1277 +    case TK_IF: {  /* stat -> ifstat */
  1.1278 +      ifstat(ls, line);
  1.1279 +      return 0;
  1.1280 +    }
  1.1281 +    case TK_WHILE: {  /* stat -> whilestat */
  1.1282 +      whilestat(ls, line);
  1.1283 +      return 0;
  1.1284 +    }
  1.1285 +    case TK_DO: {  /* stat -> DO block END */
  1.1286 +      luaX_next(ls);  /* skip DO */
  1.1287 +      block(ls);
  1.1288 +      check_match(ls, TK_END, TK_DO, line);
  1.1289 +      return 0;
  1.1290 +    }
  1.1291 +    case TK_FOR: {  /* stat -> forstat */
  1.1292 +      forstat(ls, line);
  1.1293 +      return 0;
  1.1294 +    }
  1.1295 +    case TK_REPEAT: {  /* stat -> repeatstat */
  1.1296 +      repeatstat(ls, line);
  1.1297 +      return 0;
  1.1298 +    }
  1.1299 +    case TK_FUNCTION: {
  1.1300 +      funcstat(ls, line);  /* stat -> funcstat */
  1.1301 +      return 0;
  1.1302 +    }
  1.1303 +    case TK_LOCAL: {  /* stat -> localstat */
  1.1304 +      luaX_next(ls);  /* skip LOCAL */
  1.1305 +      if (testnext(ls, TK_FUNCTION))  /* local function? */
  1.1306 +        localfunc(ls);
  1.1307 +      else
  1.1308 +        localstat(ls);
  1.1309 +      return 0;
  1.1310 +    }
  1.1311 +    case TK_RETURN: {  /* stat -> retstat */
  1.1312 +      retstat(ls);
  1.1313 +      return 1;  /* must be last statement */
  1.1314 +    }
  1.1315 +    case TK_BREAK: {  /* stat -> breakstat */
  1.1316 +      luaX_next(ls);  /* skip BREAK */
  1.1317 +      breakstat(ls);
  1.1318 +      return 1;  /* must be last statement */
  1.1319 +    }
  1.1320 +    default: {
  1.1321 +      exprstat(ls);
  1.1322 +      return 0;  /* to avoid warnings */
  1.1323 +    }
  1.1324 +  }
  1.1325 +}
  1.1326 +
  1.1327 +
  1.1328 +static void chunk (LexState *ls) {
  1.1329 +  /* chunk -> { stat [`;'] } */
  1.1330 +  int islast = 0;
  1.1331 +  enterlevel(ls);
  1.1332 +  while (!islast && !block_follow(ls->t.token)) {
  1.1333 +    islast = statement(ls);
  1.1334 +    testnext(ls, ';');
  1.1335 +    lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
  1.1336 +               ls->fs->freereg >= ls->fs->nactvar);
  1.1337 +    ls->fs->freereg = ls->fs->nactvar;  /* free registers */
  1.1338 +  }
  1.1339 +  leavelevel(ls);
  1.1340 +}
  1.1341 +
  1.1342 +/* }====================================================================== */