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