Lua 5.1.4: lparser.c


L0001    /*
L0002    ** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $
L0003    ** Lua Parser
L0004    ** See Copyright Notice in lua.h
L0005    */
L0006    
L0007    
L0008    #include <string.h>
L0009    
L0010    #define lparser_c
L0011    #define LUA_CORE
L0012    
L0013    #include "lua.h"
L0014    
L0015    #include "lcode.h"
L0016    #include "ldebug.h"
L0017    #include "ldo.h"
L0018    #include "lfunc.h"
L0019    #include "llex.h"
L0020    #include "lmem.h"
L0021    #include "lobject.h"
L0022    #include "lopcodes.h"
L0023    #include "lparser.h"
L0024    #include "lstate.h"
L0025    #include "lstring.h"
L0026    #include "ltable.h"
L0027    
L0028    
L0029    
L0030    #define hasmultret(k)		((k) == VCALL || (k) == VVARARG)
Whether expkind k has multiple return values.
L0031 L0032
#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) L0033 L0034 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) L0035 L0036 L0037 /* L0038 ** nodes for block list (list of active blocks) L0039 */ L0040 typedef struct BlockCnt { L0041 struct BlockCnt *previous; /* chain */ L0042 int breaklist; /* list of jumps out of this loop */ L0043 lu_byte nactvar; /* # active locals outside the breakable structure */ L0044 lu_byte upval; /* true if some variable in the block is an upvalue */ L0045 lu_byte isbreakable; /* true if `block' is a loop */ L0046 } BlockCnt; L0047 L0048 L0049 L0050 /* L0051 ** prototypes for recursive non-terminal functions L0052 */ L0053 static void chunk (LexState *ls); L0054 static void expr (LexState *ls, expdesc *v); L0055 L0056 L0057 static void anchor_token (LexState *ls) { L0058 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { L0059 TString *ts = ls->t.seminfo.ts; L0060 luaX_newstring(ls, getstr(ts), ts->tsv.len); L0061 } L0062 } L0063 L0064 L0065 static void error_expected (LexState *ls, int token) { L0066 luaX_syntaxerror(ls, L0067 luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); L0068 } L0069 L0070 L0071 static void errorlimit (FuncState *fs, int limit, const char *what) { L0072 const char *msg = (fs->f->linedefined == 0) ? L0073 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : L0074 luaO_pushfstring(fs->L, "function at line %d has more than %d %s", L0075 fs->f->linedefined, limit, what); L0076 luaX_lexerror(fs->ls, msg, 0); L0077 } L0078 L0079 L0080 static int testnext (LexState *ls, int c) { L0081 if (ls->t.token == c) { L0082 luaX_next(ls); L0083 return 1; L0084 } L0085 else return 0; L0086 } L0087 L0088 L0089 static void check (LexState *ls, int c) { L0090 if (ls->t.token != c) L0091 error_expected(ls, c); L0092 } L0093 L0094 static void checknext (LexState *ls, int c) { L0095 check(ls, c); L0096 luaX_next(ls); L0097 } L0098 L0099 L0100 #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } L0101 L0102 L0103 L0104 static void check_match (LexState *ls, int what, int who, int where) { L0105 if (!testnext(ls, what)) { L0106 if (where == ls->linenumber) L0107 error_expected(ls, what); L0108 else { L0109 luaX_syntaxerror(ls, luaO_pushfstring(ls->L, L0110 LUA_QS " expected (to close " LUA_QS " at line %d)", L0111 luaX_token2str(ls, what), luaX_token2str(ls, who), where)); L0112 } L0113 } L0114 } L0115 L0116 L0117 static TString *str_checkname (LexState *ls) { L0118 TString *ts; L0119 check(ls, TK_NAME); L0120 ts = ls->t.seminfo.ts; L0121 luaX_next(ls); L0122 return ts; L0123 } L0124 L0125 L0126 static void init_exp (expdesc *e, expkind k, int i) { L0127 e->f = e->t = NO_JUMP; L0128 e->k = k; L0129 e->u.s.info = i; L0130 } L0131 L0132 L0133 static void codestring (LexState *ls, expdesc *e, TString *s) { L0134 init_exp(e, VK, luaK_stringK(ls->fs, s)); L0135 } L0136 L0137 L0138 static void checkname(LexState *ls, expdesc *e) { L0139 codestring(ls, e, str_checkname(ls)); L0140 } L0141 L0142 L0143 static int registerlocalvar (LexState *ls, TString *varname) { L0144 FuncState *fs = ls->fs; L0145 Proto *f = fs->f; L0146 int oldsize = f->sizelocvars; L0147 luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, L0148 LocVar, SHRT_MAX, "too many local variables"); L0149 while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; L0150 f->locvars[fs->nlocvars].varname = varname; L0151 luaC_objbarrier(ls->L, f, varname); L0152 return fs->nlocvars++; L0153 } L0154 L0155 L0156 #define new_localvarliteral(ls,v,n) \ L0157 new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) L0158 L0159 L0160 static void new_localvar (LexState *ls, TString *name, int n) { L0161 FuncState *fs = ls->fs; L0162 luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); L0163 fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); L0164 } L0165 L0166 L0167 static void adjustlocalvars (LexState *ls, int nvars) { L0168 FuncState *fs = ls->fs; L0169 fs->nactvar = cast_byte(fs->nactvar + nvars); L0170 for (; nvars; nvars--) { L0171 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; L0172 } L0173 } L0174 L0175 L0176 static void removevars (LexState *ls, int tolevel) { L0177 FuncState *fs = ls->fs; L0178 while (fs->nactvar > tolevel) L0179 getlocvar(fs, --fs->nactvar).endpc = fs->pc; L0180 } L0181 L0182 L0183 static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { L0184 int i; L0185 Proto *f = fs->f; L0186 int oldsize = f->sizeupvalues; L0187 for (i=0; i<f->nups; i++) { L0188 if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { L0189 lua_assert(f->upvalues[i] == name); L0190 return i; L0191 } L0192 } L0193 /* new one */ L0194 luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); L0195 luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, L0196 TString *, MAX_INT, ""); L0197 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; L0198 f->upvalues[f->nups] = name; L0199 luaC_objbarrier(fs->L, f, name); L0200 lua_assert(v->k == VLOCAL || v->k == VUPVAL); L0201 fs->upvalues[f->nups].k = cast_byte(v->k); L0202 fs->upvalues[f->nups].info = cast_byte(v->u.s.info); L0203 return f->nups++; L0204 } L0205 L0206 L0207 static int searchvar (FuncState *fs, TString *n) { L0208 int i; L0209 for (i=fs->nactvar-1; i >= 0; i--) { L0210 if (n == getlocvar(fs, i).varname) L0211 return i; L0212 } L0213 return -1; /* not found */ L0214 } L0215 L0216 L0217 static void markupval (FuncState *fs, int level) { L0218 BlockCnt *bl = fs->bl; L0219 while (bl && bl->nactvar > level) bl = bl->previous; L0220 if (bl) bl->upval = 1; L0221 } L0222 L0223 L0224 static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { L0225 if (fs == NULL) { /* no more levels? */ L0226 init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ L0227 return VGLOBAL; L0228 } L0229 else { L0230 int v = searchvar(fs, n); /* look up at current level */ L0231 if (v >= 0) { L0232 init_exp(var, VLOCAL, v); L0233 if (!base) L0234 markupval(fs, v); /* local will be used as an upval */ L0235 return VLOCAL; L0236 } L0237 else { /* not found at current level; try upper one */ L0238 if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) L0239 return VGLOBAL; L0240 var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ L0241 var->k = VUPVAL; /* upvalue in this level */ L0242 return VUPVAL; L0243 } L0244 } L0245 } L0246 L0247 L0248 static void singlevar (LexState *ls, expdesc *var) { L0249 TString *varname = str_checkname(ls); L0250 FuncState *fs = ls->fs; L0251 if (singlevaraux(fs, varname, var, 1) == VGLOBAL) L0252 var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ L0253 } L0254 L0255 L0256 static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { L0257 FuncState *fs = ls->fs; L0258 int extra = nvars - nexps; L0259 if (hasmultret(e->k)) { L0260 extra++; /* includes call itself */ L0261 if (extra < 0) extra = 0; L0262 luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ L0263 if (extra > 1) luaK_reserveregs(fs, extra-1); L0264 } L0265 else { L0266 if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ L0267 if (extra > 0) { L0268 int reg = fs->freereg; L0269 luaK_reserveregs(fs, extra); L0270 luaK_nil(fs, reg, extra); L0271 } L0272 } L0273 } L0274 L0275 L0276 static void enterlevel (LexState *ls) { L0277 if (++ls->L->nCcalls > LUAI_MAXCCALLS) L0278 luaX_lexerror(ls, "chunk has too many syntax levels", 0); L0279 } L0280 L0281 L0282 #define leavelevel(ls) ((ls)->L->nCcalls--) L0283 L0284 L0285 static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { L0286 bl->breaklist = NO_JUMP; L0287 bl->isbreakable = isbreakable; L0288 bl->nactvar = fs->nactvar; L0289 bl->upval = 0; L0290 bl->previous = fs->bl; L0291 fs->bl = bl; L0292 lua_assert(fs->freereg == fs->nactvar); L0293 } L0294 L0295 L0296 static void leaveblock (FuncState *fs) { L0297 BlockCnt *bl = fs->bl; L0298 fs->bl = bl->previous; L0299 removevars(fs->ls, bl->nactvar); L0300 if (bl->upval) L0301 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); L0302 /* a block either controls scope or breaks (never both) */ L0303 lua_assert(!bl->isbreakable || !bl->upval); L0304 lua_assert(bl->nactvar == fs->nactvar); L0305 fs->freereg = fs->nactvar; /* free registers */ L0306 luaK_patchtohere(fs, bl->breaklist); L0307 } L0308 L0309 L0310 static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { L0311 FuncState *fs = ls->fs; L0312 Proto *f = fs->f; L0313 int oldsize = f->sizep; L0314 int i; L0315 luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, L0316 MAXARG_Bx, "constant table overflow"); L0317 while (oldsize < f->sizep) f->p[oldsize++] = NULL; L0318 f->p[fs->np++] = func->f; L0319 luaC_objbarrier(ls->L, f, func->f); L0320 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); L0321 for (i=0; i<func->f->nups; i++) { L0322 OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; L0323 luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); L0324 } L0325 } L0326 L0327 L0328 static void open_func (LexState *ls, FuncState *fs) { L0329 lua_State *L = ls->L; L0330 Proto *f = luaF_newproto(L); L0331 fs->f = f; L0332 fs->prev = ls->fs; /* linked list of funcstates */ L0333 fs->ls = ls; L0334 fs->L = L; L0335 ls->fs = fs; L0336 fs->pc = 0; L0337 fs->lasttarget = -1; L0338 fs->jpc = NO_JUMP; L0339 fs->freereg = 0; L0340 fs->nk = 0; L0341 fs->np = 0; L0342 fs->nlocvars = 0; L0343 fs->nactvar = 0; L0344 fs->bl = NULL; L0345 f->source = ls->source; L0346 f->maxstacksize = 2; /* registers 0/1 are always valid */ L0347 fs->h = luaH_new(L, 0, 0); L0348 /* anchor table of constants and prototype (to avoid being collected) */ L0349 sethvalue2s(L, L->top, fs->h); L0350 incr_top(L); L0351 setptvalue2s(L, L->top, f); L0352 incr_top(L); L0353 } L0354 L0355 L0356 static void close_func (LexState *ls) { L0357 lua_State *L = ls->L; L0358 FuncState *fs = ls->fs; L0359 Proto *f = fs->f; L0360 removevars(ls, 0); L0361 luaK_ret(fs, 0, 0); /* final return */ L0362 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); L0363 f->sizecode = fs->pc; L0364 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); L0365 f->sizelineinfo = fs->pc; L0366 luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); L0367 f->sizek = fs->nk; L0368 luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); L0369 f->sizep = fs->np; L0370 luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); L0371 f->sizelocvars = fs->nlocvars; L0372 luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); L0373 f->sizeupvalues = f->nups; L0374 lua_assert(luaG_checkcode(f)); L0375 lua_assert(fs->bl == NULL); L0376 ls->fs = fs->prev; L0377 L->top -= 2; /* remove table and prototype from the stack */ L0378 /* last token read was anchored in defunct function; must reanchor it */ L0379 if (fs) anchor_token(ls); L0380 } L0381 L0382 L0383 Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { L0384 struct LexState lexstate; L0385 struct FuncState funcstate; L0386 lexstate.buff = buff; L0387 luaX_setinput(L, &lexstate, z, luaS_new(L, name)); L0388 open_func(&lexstate, &funcstate); L0389 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ L0390 luaX_next(&lexstate); /* read first token */ L0391 chunk(&lexstate); L0392 check(&lexstate, TK_EOS); L0393 close_func(&lexstate); L0394 lua_assert(funcstate.prev == NULL); L0395 lua_assert(funcstate.f->nups == 0); L0396 lua_assert(lexstate.fs == NULL); L0397 return funcstate.f; L0398 } L0399 L0400 L0401 L0402 /*============================================================*/ L0403 /* GRAMMAR RULES */ L0404 /*============================================================*/ L0405 L0406 L0407 static void field (LexState *ls, expdesc *v) { L0408 /* field -> ['.' | ':'] NAME */ L0409 FuncState *fs = ls->fs; L0410 expdesc key; L0411 luaK_exp2anyreg(fs, v); L0412 luaX_next(ls); /* skip the dot or colon */ L0413 checkname(ls, &key); L0414 luaK_indexed(fs, v, &key); L0415 } L0416 L0417 L0418 static void yindex (LexState *ls, expdesc *v) { L0419 /* index -> '[' expr ']' */ L0420 luaX_next(ls); /* skip the '[' */ L0421 expr(ls, v); L0422 luaK_exp2val(ls->fs, v); L0423 checknext(ls, ']'); L0424 } L0425 L0426 L0427 /* L0428 ** {====================================================================== L0429 ** Rules for Constructors L0430 ** ======================================================================= L0431 */ L0432 L0433 L0434 struct ConsControl { L0435 expdesc v; /* last list item read */ L0436 expdesc *t; /* table descriptor */ L0437 int nh; /* total number of `record' elements */ L0438 int na; /* total number of array elements */ L0439 int tostore; /* number of array elements pending to be stored */ L0440 }; L0441 L0442 L0443 static void recfield (LexState *ls, struct ConsControl *cc) { L0444 /* recfield -> (NAME | `['exp1`]') = exp1 */ L0445 FuncState *fs = ls->fs; L0446 int reg = ls->fs->freereg; L0447 expdesc key, val; L0448 int rkkey; L0449 if (ls->t.token == TK_NAME) { L0450 luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); L0451 checkname(ls, &key); L0452 } L0453 else /* ls->t.token == '[' */ L0454 yindex(ls, &key); L0455 cc->nh++; L0456 checknext(ls, '='); L0457 rkkey = luaK_exp2RK(fs, &key); L0458 expr(ls, &val); L0459 luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); L0460 fs->freereg = reg; /* free registers */ L0461 } L0462 L0463 L0464 static void closelistfield (FuncState *fs, struct ConsControl *cc) { L0465 if (cc->v.k == VVOID) return; /* there is no list item */ L0466 luaK_exp2nextreg(fs, &cc->v); L0467 cc->v.k = VVOID; L0468 if (cc->tostore == LFIELDS_PER_FLUSH) { L0469 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ L0470 cc->tostore = 0; /* no more items pending */ L0471 } L0472 } L0473 L0474 L0475 static void lastlistfield (FuncState *fs, struct ConsControl *cc) { L0476 if (cc->tostore == 0) return; L0477 if (hasmultret(cc->v.k)) { L0478 luaK_setmultret(fs, &cc->v); L0479 luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); L0480 cc->na--; /* do not count last expression (unknown number of elements) */ L0481 } L0482 else { L0483 if (cc->v.k != VVOID) L0484 luaK_exp2nextreg(fs, &cc->v); L0485 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); L0486 } L0487 } L0488 L0489 L0490 static void listfield (LexState *ls, struct ConsControl *cc) { L0491 expr(ls, &cc->v); L0492 luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); L0493 cc->na++; L0494 cc->tostore++; L0495 } L0496 L0497 L0498 static void constructor (LexState *ls, expdesc *t) { L0499 /* constructor -> ?? */ L0500 FuncState *fs = ls->fs; L0501 int line = ls->linenumber; L0502 int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); L0503 struct ConsControl cc; L0504 cc.na = cc.nh = cc.tostore = 0; L0505 cc.t = t; L0506 init_exp(t, VRELOCABLE, pc); L0507 init_exp(&cc.v, VVOID, 0); /* no value (yet) */ L0508 luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ L0509 checknext(ls, '{'); L0510 do { L0511 lua_assert(cc.v.k == VVOID || cc.tostore > 0); L0512 if (ls->t.token == '}') break; L0513 closelistfield(fs, &cc); L0514 switch(ls->t.token) { L0515 case TK_NAME: { /* may be listfields or recfields */ L0516 luaX_lookahead(ls); L0517 if (ls->lookahead.token != '=') /* expression? */ L0518 listfield(ls, &cc); L0519 else L0520 recfield(ls, &cc); L0521 break; L0522 } L0523 case '[': { /* constructor_item -> recfield */ L0524 recfield(ls, &cc); L0525 break; L0526 } L0527 default: { /* constructor_part -> listfield */ L0528 listfield(ls, &cc); L0529 break; L0530 } L0531 } L0532 } while (testnext(ls, ',') || testnext(ls, ';')); L0533 check_match(ls, '}', '{', line); L0534 lastlistfield(fs, &cc); L0535 SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ L0536 SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ L0537 } L0538 L0539 /* }====================================================================== */ L0540 L0541 L0542 L0543 static void parlist (LexState *ls) { L0544 /* parlist -> [ param { `,' param } ] */ L0545 FuncState *fs = ls->fs; L0546 Proto *f = fs->f; L0547 int nparams = 0; L0548 f->is_vararg = 0; L0549 if (ls->t.token != ')') { /* is `parlist' not empty? */ L0550 do { L0551 switch (ls->t.token) { L0552 case TK_NAME: { /* param -> NAME */ L0553 new_localvar(ls, str_checkname(ls), nparams++); L0554 break; L0555 } L0556 case TK_DOTS: { /* param -> `...' */ L0557 luaX_next(ls); L0558 #if defined(LUA_COMPAT_VARARG) L0559 /* use `arg' as default name */ L0560 new_localvarliteral(ls, "arg", nparams++); L0561 f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; L0562 #endif L0563 f->is_vararg |= VARARG_ISVARARG; L0564 break; L0565 } L0566 default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected"); L0567 } L0568 } while (!f->is_vararg && testnext(ls, ',')); L0569 } L0570 adjustlocalvars(ls, nparams); L0571 f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); L0572 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ L0573 } L0574 L0575 L0576 static void body (LexState *ls, expdesc *e, int needself, int line) { L0577 /* body -> `(' parlist `)' chunk END */ L0578 FuncState new_fs; L0579 open_func(ls, &new_fs); L0580 new_fs.f->linedefined = line; L0581 checknext(ls, '('); L0582 if (needself) { L0583 new_localvarliteral(ls, "self", 0); L0584 adjustlocalvars(ls, 1); L0585 } L0586 parlist(ls); L0587 checknext(ls, ')'); L0588 chunk(ls); L0589 new_fs.f->lastlinedefined = ls->linenumber; L0590 check_match(ls, TK_END, TK_FUNCTION, line); L0591 close_func(ls); L0592 pushclosure(ls, &new_fs, e); L0593 } L0594 L0595 L0596 static int explist1 (LexState *ls, expdesc *v) { L0597 /* explist1 -> expr { `,' expr } */ L0598 int n = 1; /* at least one expression */ L0599 expr(ls, v); L0600 while (testnext(ls, ',')) { L0601 luaK_exp2nextreg(ls->fs, v); L0602 expr(ls, v); L0603 n++; L0604 } L0605 return n; L0606 } L0607 L0608 L0609 static void funcargs (LexState *ls, expdesc *f) { L0610 FuncState *fs = ls->fs; L0611 expdesc args; L0612 int base, nparams; L0613 int line = ls->linenumber; L0614 switch (ls->t.token) { L0615 case '(': { /* funcargs -> `(' [ explist1 ] `)' */ L0616 if (line != ls->lastline) L0617 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); L0618 luaX_next(ls); L0619 if (ls->t.token == ')') /* arg list is empty? */ L0620 args.k = VVOID; L0621 else { L0622 explist1(ls, &args); L0623 luaK_setmultret(fs, &args); L0624 } L0625 check_match(ls, ')', '(', line); L0626 break; L0627 } L0628 case '{': { /* funcargs -> constructor */ L0629 constructor(ls, &args); L0630 break; L0631 } L0632 case TK_STRING: { /* funcargs -> STRING */ L0633 codestring(ls, &args, ls->t.seminfo.ts); L0634 luaX_next(ls); /* must use `seminfo' before `next' */ L0635 break; L0636 } L0637 default: { L0638 luaX_syntaxerror(ls, "function arguments expected"); L0639 return; L0640 } L0641 } L0642 lua_assert(f->k == VNONRELOC); L0643 base = f->u.s.info; /* base register for call */ L0644 if (hasmultret(args.k)) L0645 nparams = LUA_MULTRET; /* open call */ L0646 else { L0647 if (args.k != VVOID) L0648 luaK_exp2nextreg(fs, &args); /* close last argument */ L0649 nparams = fs->freereg - (base+1); L0650 } L0651 init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); L0652 luaK_fixline(fs, line); L0653 fs->freereg = base+1; /* call remove function and arguments and leaves L0654 (unless changed) one result */ L0655 } L0656 L0657 L0658 L0659 L0660 /* L0661 ** {====================================================================== L0662 ** Expression parsing L0663 ** ======================================================================= L0664 */ L0665 L0666 L0667 static void prefixexp (LexState *ls, expdesc *v) { L0668 /* prefixexp -> NAME | '(' expr ')' */ L0669 switch (ls->t.token) { L0670 case '(': { L0671 int line = ls->linenumber; L0672 luaX_next(ls); L0673 expr(ls, v); L0674 check_match(ls, ')', '(', line); L0675 luaK_dischargevars(ls->fs, v); L0676 return; L0677 } L0678 case TK_NAME: { L0679 singlevar(ls, v); L0680 return; L0681 } L0682 default: { L0683 luaX_syntaxerror(ls, "unexpected symbol"); L0684 return; L0685 } L0686 } L0687 } L0688 L0689 L0690 static void primaryexp (LexState *ls, expdesc *v) { L0691 /* primaryexp -> L0692 prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ L0693 FuncState *fs = ls->fs; L0694 prefixexp(ls, v); L0695 for (;;) { L0696 switch (ls->t.token) { L0697 case '.': { /* field */ L0698 field(ls, v); L0699 break; L0700 } L0701 case '[': { /* `[' exp1 `]' */ L0702 expdesc key; L0703 luaK_exp2anyreg(fs, v); L0704 yindex(ls, &key); L0705 luaK_indexed(fs, v, &key); L0706 break; L0707 } L0708 case ':': { /* `:' NAME funcargs */ L0709 expdesc key; L0710 luaX_next(ls); L0711 checkname(ls, &key); L0712 luaK_self(fs, v, &key); L0713 funcargs(ls, v); L0714 break; L0715 } L0716 case '(': case TK_STRING: case '{': { /* funcargs */ L0717 luaK_exp2nextreg(fs, v); L0718 funcargs(ls, v); L0719 break; L0720 } L0721 default: return; L0722 } L0723 } L0724 } L0725 L0726 L0727 static void simpleexp (LexState *ls, expdesc *v) { L0728 /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | L0729 constructor | FUNCTION body | primaryexp */ L0730 switch (ls->t.token) { L0731 case TK_NUMBER: { L0732 init_exp(v, VKNUM, 0); L0733 v->u.nval = ls->t.seminfo.r; L0734 break; L0735 } L0736 case TK_STRING: { L0737 codestring(ls, v, ls->t.seminfo.ts); L0738 break; L0739 } L0740 case TK_NIL: { L0741 init_exp(v, VNIL, 0); L0742 break; L0743 } L0744 case TK_TRUE: { L0745 init_exp(v, VTRUE, 0); L0746 break; L0747 } L0748 case TK_FALSE: { L0749 init_exp(v, VFALSE, 0); L0750 break; L0751 } L0752 case TK_DOTS: { /* vararg */ L0753 FuncState *fs = ls->fs; L0754 check_condition(ls, fs->f->is_vararg, L0755 "cannot use " LUA_QL("...") " outside a vararg function"); L0756 fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ L0757 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); L0758 break; L0759 } L0760 case '{': { /* constructor */ L0761 constructor(ls, v); L0762 return; L0763 } L0764 case TK_FUNCTION: { L0765 luaX_next(ls); L0766 body(ls, v, 0, ls->linenumber); L0767 return; L0768 } L0769 default: { L0770 primaryexp(ls, v); L0771 return; L0772 } L0773 } L0774 luaX_next(ls); L0775 } L0776 L0777 L0778 static UnOpr getunopr (int op) { L0779 switch (op) { L0780 case TK_NOT: return OPR_NOT; L0781 case '-': return OPR_MINUS; L0782 case '#': return OPR_LEN; L0783 default: return OPR_NOUNOPR; L0784 } L0785 } L0786 L0787 L0788 static BinOpr getbinopr (int op) { L0789 switch (op) { L0790 case '+': return OPR_ADD; L0791 case '-': return OPR_SUB; L0792 case '*': return OPR_MUL; L0793 case '/': return OPR_DIV; L0794 case '%': return OPR_MOD; L0795 case '^': return OPR_POW; L0796 case TK_CONCAT: return OPR_CONCAT; L0797 case TK_NE: return OPR_NE; L0798 case TK_EQ: return OPR_EQ; L0799 case '<': return OPR_LT; L0800 case TK_LE: return OPR_LE; L0801 case '>': return OPR_GT; L0802 case TK_GE: return OPR_GE; L0803 case TK_AND: return OPR_AND; L0804 case TK_OR: return OPR_OR; L0805 default: return OPR_NOBINOPR; L0806 } L0807 } L0808 L0809 L0810 static const struct { L0811 lu_byte left; /* left priority for each binary operator */ L0812 lu_byte right; /* right priority */ L0813 } priority[] = { /* ORDER OPR */ L0814 {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ L0815 {10, 9}, {5, 4}, /* power and concat (right associative) */ L0816 {3, 3}, {3, 3}, /* equality and inequality */ L0817 {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ L0818 {2, 2}, {1, 1} /* logical (and/or) */ L0819 }; L0820 L0821 #define UNARY_PRIORITY 8 /* priority for unary operators */ L0822 L0823 L0824 /* L0825 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } L0826 ** where `binop' is any binary operator with a priority higher than `limit' L0827 */ L0828 static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { L0829 BinOpr op; L0830 UnOpr uop; L0831 enterlevel(ls); L0832 uop = getunopr(ls->t.token); L0833 if (uop != OPR_NOUNOPR) { L0834 luaX_next(ls); L0835 subexpr(ls, v, UNARY_PRIORITY); L0836 luaK_prefix(ls->fs, uop, v); L0837 } L0838 else simpleexp(ls, v); L0839 /* expand while operators have priorities higher than `limit' */ L0840 op = getbinopr(ls->t.token); L0841 while (op != OPR_NOBINOPR && priority[op].left > limit) { L0842 expdesc v2; L0843 BinOpr nextop; L0844 luaX_next(ls); L0845 luaK_infix(ls->fs, op, v); L0846 /* read sub-expression with higher priority */ L0847 nextop = subexpr(ls, &v2, priority[op].right); L0848 luaK_posfix(ls->fs, op, v, &v2); L0849 op = nextop; L0850 } L0851 leavelevel(ls); L0852 return op; /* return first untreated operator */ L0853 } L0854 L0855 L0856 static void expr (LexState *ls, expdesc *v) { L0857 subexpr(ls, v, 0); L0858 } L0859 L0860 /* }==================================================================== */ L0861 L0862 L0863 L0864 /* L0865 ** {====================================================================== L0866 ** Rules for Statements L0867 ** ======================================================================= L0868 */ L0869 L0870 L0871 static int block_follow (int token) { L0872 switch (token) { L0873 case TK_ELSE: case TK_ELSEIF: case TK_END: L0874 case TK_UNTIL: case TK_EOS: L0875 return 1; L0876 default: return 0; L0877 } L0878 } L0879 L0880 L0881 static void block (LexState *ls) { L0882 /* block -> chunk */ L0883 FuncState *fs = ls->fs; L0884 BlockCnt bl; L0885 enterblock(fs, &bl, 0); L0886 chunk(ls); L0887 lua_assert(bl.breaklist == NO_JUMP); L0888 leaveblock(fs); L0889 } L0890 L0891 L0892 /* L0893 ** structure to chain all variables in the left-hand side of an L0894 ** assignment L0895 */ L0896 struct LHS_assign { L0897 struct LHS_assign *prev; L0898 expdesc v; /* variable (global, local, upvalue, or indexed) */ L0899 }; L0900 L0901 L0902 /* L0903 ** check whether, in an assignment to a local variable, the local variable L0904 ** is needed in a previous assignment (to a table). If so, save original L0905 ** local value in a safe place and use this safe copy in the previous L0906 ** assignment. L0907 */ L0908 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { L0909 FuncState *fs = ls->fs; L0910 int extra = fs->freereg; /* eventual position to save local variable */ L0911 int conflict = 0; L0912 for (; lh; lh = lh->prev) { L0913 if (lh->v.k == VINDEXED) { L0914 if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ L0915 conflict = 1; L0916 lh->v.u.s.info = extra; /* previous assignment will use safe copy */ L0917 } L0918 if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ L0919 conflict = 1; L0920 lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ L0921 } L0922 } L0923 } L0924 if (conflict) { L0925 luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ L0926 luaK_reserveregs(fs, 1); L0927 } L0928 } L0929 L0930 L0931 static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { L0932 expdesc e; L0933 check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, L0934 "syntax error"); L0935 if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ L0936 struct LHS_assign nv; L0937 nv.prev = lh; L0938 primaryexp(ls, &nv.v); L0939 if (nv.v.k == VLOCAL) L0940 check_conflict(ls, lh, &nv.v); L0941 luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, L0942 "variables in assignment"); L0943 assignment(ls, &nv, nvars+1); L0944 } L0945 else { /* assignment -> `=' explist1 */ L0946 int nexps; L0947 checknext(ls, '='); L0948 nexps = explist1(ls, &e); L0949 if (nexps != nvars) { L0950 adjust_assign(ls, nvars, nexps, &e); L0951 if (nexps > nvars) L0952 ls->fs->freereg -= nexps - nvars; /* remove extra values */ L0953 } L0954 else { L0955 luaK_setoneret(ls->fs, &e); /* close last expression */ L0956 luaK_storevar(ls->fs, &lh->v, &e); L0957 return; /* avoid default */ L0958 } L0959 } L0960 init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ L0961 luaK_storevar(ls->fs, &lh->v, &e); L0962 } L0963 L0964 L0965 static int cond (LexState *ls) { L0966 /* cond -> exp */ L0967 expdesc v; L0968 expr(ls, &v); /* read condition */ L0969 if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ L0970 luaK_goiftrue(ls->fs, &v); L0971 return v.f; L0972 } L0973 L0974 L0975 static void breakstat (LexState *ls) { L0976 FuncState *fs = ls->fs; L0977 BlockCnt *bl = fs->bl; L0978 int upval = 0; L0979 while (bl && !bl->isbreakable) { L0980 upval |= bl->upval; L0981 bl = bl->previous; L0982 } L0983 if (!bl) L0984 luaX_syntaxerror(ls, "no loop to break"); L0985 if (upval) L0986 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); L0987 luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); L0988 } L0989 L0990 L0991 static void whilestat (LexState *ls, int line) { L0992 /* whilestat -> WHILE cond DO block END */ L0993 FuncState *fs = ls->fs; L0994 int whileinit; L0995 int condexit; L0996 BlockCnt bl; L0997 luaX_next(ls); /* skip WHILE */ L0998 whileinit = luaK_getlabel(fs); L0999 condexit = cond(ls); L1000 enterblock(fs, &bl, 1); L1001 checknext(ls, TK_DO); L1002 block(ls); L1003 luaK_patchlist(fs, luaK_jump(fs), whileinit); L1004 check_match(ls, TK_END, TK_WHILE, line); L1005 leaveblock(fs); L1006 luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ L1007 } L1008 L1009 L1010 static void repeatstat (LexState *ls, int line) { L1011 /* repeatstat -> REPEAT block UNTIL cond */ L1012 int condexit; L1013 FuncState *fs = ls->fs; L1014 int repeat_init = luaK_getlabel(fs); L1015 BlockCnt bl1, bl2; L1016 enterblock(fs, &bl1, 1); /* loop block */ L1017 enterblock(fs, &bl2, 0); /* scope block */ L1018 luaX_next(ls); /* skip REPEAT */ L1019 chunk(ls); L1020 check_match(ls, TK_UNTIL, TK_REPEAT, line); L1021 condexit = cond(ls); /* read condition (inside scope block) */ L1022 if (!bl2.upval) { /* no upvalues? */ L1023 leaveblock(fs); /* finish scope */ L1024 luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ L1025 } L1026 else { /* complete semantics when there are upvalues */ L1027 breakstat(ls); /* if condition then break */ L1028 luaK_patchtohere(ls->fs, condexit); /* else... */ L1029 leaveblock(fs); /* finish scope... */ L1030 luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ L1031 } L1032 leaveblock(fs); /* finish loop */ L1033 } L1034 L1035 L1036 static int exp1 (LexState *ls) { L1037 expdesc e; L1038 int k; L1039 expr(ls, &e); L1040 k = e.k; L1041 luaK_exp2nextreg(ls->fs, &e); L1042 return k; L1043 } L1044 L1045 L1046 static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { L1047 /* forbody -> DO block */ L1048 BlockCnt bl; L1049 FuncState *fs = ls->fs; L1050 int prep, endfor; L1051 adjustlocalvars(ls, 3); /* control variables */ L1052 checknext(ls, TK_DO); L1053 prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); L1054 enterblock(fs, &bl, 0); /* scope for declared variables */ L1055 adjustlocalvars(ls, nvars); L1056 luaK_reserveregs(fs, nvars); L1057 block(ls); L1058 leaveblock(fs); /* end of scope for declared variables */ L1059 luaK_patchtohere(fs, prep); L1060 endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : L1061 luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); L1062 luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ L1063 luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); L1064 } L1065 L1066 L1067 static void fornum (LexState *ls, TString *varname, int line) { L1068 /* fornum -> NAME = exp1,exp1[,exp1] forbody */ L1069 FuncState *fs = ls->fs; L1070 int base = fs->freereg; L1071 new_localvarliteral(ls, "(for index)", 0); L1072 new_localvarliteral(ls, "(for limit)", 1); L1073 new_localvarliteral(ls, "(for step)", 2); L1074 new_localvar(ls, varname, 3); L1075 checknext(ls, '='); L1076 exp1(ls); /* initial value */ L1077 checknext(ls, ','); L1078 exp1(ls); /* limit */ L1079 if (testnext(ls, ',')) L1080 exp1(ls); /* optional step */ L1081 else { /* default step = 1 */ L1082 luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); L1083 luaK_reserveregs(fs, 1); L1084 } L1085 forbody(ls, base, line, 1, 1); L1086 } L1087 L1088 L1089 static void forlist (LexState *ls, TString *indexname) { L1090 /* forlist -> NAME {,NAME} IN explist1 forbody */ L1091 FuncState *fs = ls->fs; L1092 expdesc e; L1093 int nvars = 0; L1094 int line; L1095 int base = fs->freereg; L1096 /* create control variables */ L1097 new_localvarliteral(ls, "(for generator)", nvars++); L1098 new_localvarliteral(ls, "(for state)", nvars++); L1099 new_localvarliteral(ls, "(for control)", nvars++); L1100 /* create declared variables */ L1101 new_localvar(ls, indexname, nvars++); L1102 while (testnext(ls, ',')) L1103 new_localvar(ls, str_checkname(ls), nvars++); L1104 checknext(ls, TK_IN); L1105 line = ls->linenumber; L1106 adjust_assign(ls, 3, explist1(ls, &e), &e); L1107 luaK_checkstack(fs, 3); /* extra space to call generator */ L1108 forbody(ls, base, line, nvars - 3, 0); L1109 } L1110 L1111 L1112 static void forstat (LexState *ls, int line) { L1113 /* forstat -> FOR (fornum | forlist) END */ L1114 FuncState *fs = ls->fs; L1115 TString *varname; L1116 BlockCnt bl; L1117 enterblock(fs, &bl, 1); /* scope for loop and control variables */ L1118 luaX_next(ls); /* skip `for' */ L1119 varname = str_checkname(ls); /* first variable name */ L1120 switch (ls->t.token) { L1121 case '=': fornum(ls, varname, line); break; L1122 case ',': case TK_IN: forlist(ls, varname); break; L1123 default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); L1124 } L1125 check_match(ls, TK_END, TK_FOR, line); L1126 leaveblock(fs); /* loop scope (`break' jumps to this point) */ L1127 } L1128 L1129 L1130 static int test_then_block (LexState *ls) { L1131 /* test_then_block -> [IF | ELSEIF] cond THEN block */ L1132 int condexit; L1133 luaX_next(ls); /* skip IF or ELSEIF */ L1134 condexit = cond(ls); L1135 checknext(ls, TK_THEN); L1136 block(ls); /* `then' part */ L1137 return condexit; L1138 } L1139 L1140 L1141 static void ifstat (LexState *ls, int line) { L1142 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ L1143 FuncState *fs = ls->fs; L1144 int flist; L1145 int escapelist = NO_JUMP; L1146 flist = test_then_block(ls); /* IF cond THEN block */ L1147 while (ls->t.token == TK_ELSEIF) { L1148 luaK_concat(fs, &escapelist, luaK_jump(fs)); L1149 luaK_patchtohere(fs, flist); L1150 flist = test_then_block(ls); /* ELSEIF cond THEN block */ L1151 } L1152 if (ls->t.token == TK_ELSE) { L1153 luaK_concat(fs, &escapelist, luaK_jump(fs)); L1154 luaK_patchtohere(fs, flist); L1155 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ L1156 block(ls); /* `else' part */ L1157 } L1158 else L1159 luaK_concat(fs, &escapelist, flist); L1160 luaK_patchtohere(fs, escapelist); L1161 check_match(ls, TK_END, TK_IF, line); L1162 } L1163 L1164 L1165 static void localfunc (LexState *ls) { L1166 expdesc v, b; L1167 FuncState *fs = ls->fs; L1168 new_localvar(ls, str_checkname(ls), 0); L1169 init_exp(&v, VLOCAL, fs->freereg); L1170 luaK_reserveregs(fs, 1); L1171 adjustlocalvars(ls, 1); L1172 body(ls, &b, 0, ls->linenumber); L1173 luaK_storevar(fs, &v, &b); L1174 /* debug information will only see the variable after this point! */ L1175 getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; L1176 } L1177 L1178 L1179 static void localstat (LexState *ls) { L1180 /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ L1181 int nvars = 0; L1182 int nexps; L1183 expdesc e; L1184 do { L1185 new_localvar(ls, str_checkname(ls), nvars++); L1186 } while (testnext(ls, ',')); L1187 if (testnext(ls, '=')) L1188 nexps = explist1(ls, &e); L1189 else { L1190 e.k = VVOID; L1191 nexps = 0; L1192 } L1193 adjust_assign(ls, nvars, nexps, &e); L1194 adjustlocalvars(ls, nvars); L1195 } L1196 L1197 L1198 static int funcname (LexState *ls, expdesc *v) { L1199 /* funcname -> NAME {field} [`:' NAME] */ L1200 int needself = 0; L1201 singlevar(ls, v); L1202 while (ls->t.token == '.') L1203 field(ls, v); L1204 if (ls->t.token == ':') { L1205 needself = 1; L1206 field(ls, v); L1207 } L1208 return needself; L1209 } L1210 L1211 L1212 static void funcstat (LexState *ls, int line) { L1213 /* funcstat -> FUNCTION funcname body */ L1214 int needself; L1215 expdesc v, b; L1216 luaX_next(ls); /* skip FUNCTION */ L1217 needself = funcname(ls, &v); L1218 body(ls, &b, needself, line); L1219 luaK_storevar(ls->fs, &v, &b); L1220 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ L1221 } L1222 L1223 L1224 static void exprstat (LexState *ls) { L1225 /* stat -> func | assignment */ L1226 FuncState *fs = ls->fs; L1227 struct LHS_assign v; L1228 primaryexp(ls, &v.v); L1229 if (v.v.k == VCALL) /* stat -> func */ L1230 SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ L1231 else { /* stat -> assignment */ L1232 v.prev = NULL; L1233 assignment(ls, &v, 1); L1234 } L1235 } L1236 L1237 L1238 static void retstat (LexState *ls) { L1239 /* stat -> RETURN explist */ L1240 FuncState *fs = ls->fs; L1241 expdesc e; L1242 int first, nret; /* registers with returned values */ L1243 luaX_next(ls); /* skip RETURN */ L1244 if (block_follow(ls->t.token) || ls->t.token == ';') L1245 first = nret = 0; /* return no values */ L1246 else { L1247 nret = explist1(ls, &e); /* optional return values */ L1248 if (hasmultret(e.k)) { L1249 luaK_setmultret(fs, &e); L1250 if (e.k == VCALL && nret == 1) { /* tail call? */ L1251 SET_OPCODE(getcode(fs,&e), OP_TAILCALL); L1252 lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); L1253 } L1254 first = fs->nactvar; L1255 nret = LUA_MULTRET; /* return all values */ L1256 } L1257 else { L1258 if (nret == 1) /* only one single value? */ L1259 first = luaK_exp2anyreg(fs, &e); L1260 else { L1261 luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ L1262 first = fs->nactvar; /* return all `active' values */ L1263 lua_assert(nret == fs->freereg - first); L1264 } L1265 } L1266 } L1267 luaK_ret(fs, first, nret); L1268 } L1269 L1270 L1271 static int statement (LexState *ls) { L1272 int line = ls->linenumber; /* may be needed for error messages */ L1273 switch (ls->t.token) { L1274 case TK_IF: { /* stat -> ifstat */ L1275 ifstat(ls, line); L1276 return 0; L1277 } L1278 case TK_WHILE: { /* stat -> whilestat */ L1279 whilestat(ls, line); L1280 return 0; L1281 } L1282 case TK_DO: { /* stat -> DO block END */ L1283 luaX_next(ls); /* skip DO */ L1284 block(ls); L1285 check_match(ls, TK_END, TK_DO, line); L1286 return 0; L1287 } L1288 case TK_FOR: { /* stat -> forstat */ L1289 forstat(ls, line); L1290 return 0; L1291 } L1292 case TK_REPEAT: { /* stat -> repeatstat */ L1293 repeatstat(ls, line); L1294 return 0; L1295 } L1296 case TK_FUNCTION: { L1297 funcstat(ls, line); /* stat -> funcstat */ L1298 return 0; L1299 } L1300 case TK_LOCAL: { /* stat -> localstat */ L1301 luaX_next(ls); /* skip LOCAL */ L1302 if (testnext(ls, TK_FUNCTION)) /* local function? */ L1303 localfunc(ls); L1304 else L1305 localstat(ls); L1306 return 0; L1307 } L1308 case TK_RETURN: { /* stat -> retstat */ L1309 retstat(ls); L1310 return 1; /* must be last statement */ L1311 } L1312 case TK_BREAK: { /* stat -> breakstat */ L1313 luaX_next(ls); /* skip BREAK */ L1314 breakstat(ls); L1315 return 1; /* must be last statement */ L1316 } L1317 default: { L1318 exprstat(ls); L1319 return 0; /* to avoid warnings */ L1320 } L1321 } L1322 } L1323 L1324 L1325 static void chunk (LexState *ls) { L1326 /* chunk -> { stat [`;'] } */ L1327 int islast = 0; L1328 enterlevel(ls); L1329 while (!islast && !block_follow(ls->t.token)) { L1330 islast = statement(ls); L1331 testnext(ls, ';'); L1332 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && L1333 ls->fs->freereg >= ls->fs->nactvar); L1334 ls->fs->freereg = ls->fs->nactvar; /* free registers */ L1335 } L1336 leavelevel(ls); L1337 } L1338 L1339 /* }====================================================================== */

Generated by pretty.lua