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