00001 /* 00002 ** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $ 00003 ** Lua Parser 00004 ** See Copyright Notice in lua.h 00005 */ 00006 00007 00008 #include <string.h> 00009 00010 #define lparser_c 00011 #define LUA_CORE 00012 00013 #include "lua.h" 00014 00015 #include "lcode.h" 00016 #include "ldebug.h" 00017 #include "ldo.h" 00018 #include "lfunc.h" 00019 #include "llex.h" 00020 #include "lmem.h" 00021 #include "lobject.h" 00022 #include "lopcodes.h" 00023 #include "lparser.h" 00024 #include "lstate.h" 00025 #include "lstring.h" 00026 #include "ltable.h" 00027 00028 00029 00030 #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) 00031 00032 #define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) 00033 00034 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) 00035 00036 00037 /* 00038 ** nodes for block list (list of active blocks) 00039 */ 00040 typedef struct BlockCnt { 00041 struct BlockCnt *previous; /* chain */ 00042 int breaklist; /* list of jumps out of this loop */ 00043 lu_byte nactvar; /* # active locals outside the breakable structure */ 00044 lu_byte upval; /* true if some variable in the block is an upvalue */ 00045 lu_byte isbreakable; /* true if `block' is a loop */ 00046 } BlockCnt; 00047 00048 00049 00050 /* 00051 ** prototypes for recursive non-terminal functions 00052 */ 00053 static void chunk (LexState *ls); 00054 static void expr (LexState *ls, expdesc *v); 00055 00056 00057 static void anchor_token (LexState *ls) { 00058 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { 00059 TString *ts = ls->t.seminfo.ts; 00060 luaX_newstring(ls, getstr(ts), ts->tsv.len); 00061 } 00062 } 00063 00064 00065 static void error_expected (LexState *ls, int token) { 00066 luaX_syntaxerror(ls, 00067 luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); 00068 } 00069 00070 00071 static void errorlimit (FuncState *fs, int limit, const char *what) { 00072 const char *msg = (fs->f->linedefined == 0) ? 00073 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : 00074 luaO_pushfstring(fs->L, "function at line %d has more than %d %s", 00075 fs->f->linedefined, limit, what); 00076 luaX_lexerror(fs->ls, msg, 0); 00077 } 00078 00079 00080 static int testnext (LexState *ls, int c) { 00081 if (ls->t.token == c) { 00082 luaX_next(ls); 00083 return 1; 00084 } 00085 else return 0; 00086 } 00087 00088 00089 static void check (LexState *ls, int c) { 00090 if (ls->t.token != c) 00091 error_expected(ls, c); 00092 } 00093 00094 static void checknext (LexState *ls, int c) { 00095 check(ls, c); 00096 luaX_next(ls); 00097 } 00098 00099 00100 #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } 00101 00102 00103 00104 static void check_match (LexState *ls, int what, int who, int where) { 00105 if (!testnext(ls, what)) { 00106 if (where == ls->linenumber) 00107 error_expected(ls, what); 00108 else { 00109 luaX_syntaxerror(ls, luaO_pushfstring(ls->L, 00110 LUA_QS " expected (to close " LUA_QS " at line %d)", 00111 luaX_token2str(ls, what), luaX_token2str(ls, who), where)); 00112 } 00113 } 00114 } 00115 00116 00117 static TString *str_checkname (LexState *ls) { 00118 TString *ts; 00119 check(ls, TK_NAME); 00120 ts = ls->t.seminfo.ts; 00121 luaX_next(ls); 00122 return ts; 00123 } 00124 00125 00126 static void init_exp (expdesc *e, expkind k, int i) { 00127 e->f = e->t = NO_JUMP; 00128 e->k = k; 00129 e->u.s.info = i; 00130 } 00131 00132 00133 static void codestring (LexState *ls, expdesc *e, TString *s) { 00134 init_exp(e, VK, luaK_stringK(ls->fs, s)); 00135 } 00136 00137 00138 static void checkname(LexState *ls, expdesc *e) { 00139 codestring(ls, e, str_checkname(ls)); 00140 } 00141 00142 00143 static int registerlocalvar (LexState *ls, TString *varname) { 00144 FuncState *fs = ls->fs; 00145 Proto *f = fs->f; 00146 int oldsize = f->sizelocvars; 00147 luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, 00148 LocVar, SHRT_MAX, "too many local variables"); 00149 while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; 00150 f->locvars[fs->nlocvars].varname = varname; 00151 luaC_objbarrier(ls->L, f, varname); 00152 return fs->nlocvars++; 00153 } 00154 00155 00156 #define new_localvarliteral(ls,v,n) \ 00157 new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) 00158 00159 00160 static void new_localvar (LexState *ls, TString *name, int n) { 00161 FuncState *fs = ls->fs; 00162 luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); 00163 fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); 00164 } 00165 00166 00167 static void adjustlocalvars (LexState *ls, int nvars) { 00168 FuncState *fs = ls->fs; 00169 fs->nactvar = cast_byte(fs->nactvar + nvars); 00170 for (; nvars; nvars--) { 00171 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; 00172 } 00173 } 00174 00175 00176 static void removevars (LexState *ls, int tolevel) { 00177 FuncState *fs = ls->fs; 00178 while (fs->nactvar > tolevel) 00179 getlocvar(fs, --fs->nactvar).endpc = fs->pc; 00180 } 00181 00182 00183 static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { 00184 int i; 00185 Proto *f = fs->f; 00186 int oldsize = f->sizeupvalues; 00187 for (i=0; i<f->nups; i++) { 00188 if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { 00189 lua_assert(f->upvalues[i] == name); 00190 return i; 00191 } 00192 } 00193 /* new one */ 00194 luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); 00195 luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, 00196 TString *, MAX_INT, ""); 00197 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; 00198 f->upvalues[f->nups] = name; 00199 luaC_objbarrier(fs->L, f, name); 00200 lua_assert(v->k == VLOCAL || v->k == VUPVAL); 00201 fs->upvalues[f->nups].k = cast_byte(v->k); 00202 fs->upvalues[f->nups].info = cast_byte(v->u.s.info); 00203 return f->nups++; 00204 } 00205 00206 00207 static int searchvar (FuncState *fs, TString *n) { 00208 int i; 00209 for (i=fs->nactvar-1; i >= 0; i--) { 00210 if (n == getlocvar(fs, i).varname) 00211 return i; 00212 } 00213 return -1; /* not found */ 00214 } 00215 00216 00217 static void markupval (FuncState *fs, int level) { 00218 BlockCnt *bl = fs->bl; 00219 while (bl && bl->nactvar > level) bl = bl->previous; 00220 if (bl) bl->upval = 1; 00221 } 00222 00223 00224 static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { 00225 if (fs == NULL) { /* no more levels? */ 00226 init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ 00227 return VGLOBAL; 00228 } 00229 else { 00230 int v = searchvar(fs, n); /* look up at current level */ 00231 if (v >= 0) { 00232 init_exp(var, VLOCAL, v); 00233 if (!base) 00234 markupval(fs, v); /* local will be used as an upval */ 00235 return VLOCAL; 00236 } 00237 else { /* not found at current level; try upper one */ 00238 if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) 00239 return VGLOBAL; 00240 var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ 00241 var->k = VUPVAL; /* upvalue in this level */ 00242 return VUPVAL; 00243 } 00244 } 00245 } 00246 00247 00248 static void singlevar (LexState *ls, expdesc *var) { 00249 TString *varname = str_checkname(ls); 00250 FuncState *fs = ls->fs; 00251 if (singlevaraux(fs, varname, var, 1) == VGLOBAL) 00252 var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ 00253 } 00254 00255 00256 static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { 00257 FuncState *fs = ls->fs; 00258 int extra = nvars - nexps; 00259 if (hasmultret(e->k)) { 00260 extra++; /* includes call itself */ 00261 if (extra < 0) extra = 0; 00262 luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ 00263 if (extra > 1) luaK_reserveregs(fs, extra-1); 00264 } 00265 else { 00266 if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ 00267 if (extra > 0) { 00268 int reg = fs->freereg; 00269 luaK_reserveregs(fs, extra); 00270 luaK_nil(fs, reg, extra); 00271 } 00272 } 00273 } 00274 00275 00276 static void enterlevel (LexState *ls) { 00277 if (++ls->L->nCcalls > LUAI_MAXCCALLS) 00278 luaX_lexerror(ls, "chunk has too many syntax levels", 0); 00279 } 00280 00281 00282 #define leavelevel(ls) ((ls)->L->nCcalls--) 00283 00284 00285 static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { 00286 bl->breaklist = NO_JUMP; 00287 bl->isbreakable = isbreakable; 00288 bl->nactvar = fs->nactvar; 00289 bl->upval = 0; 00290 bl->previous = fs->bl; 00291 fs->bl = bl; 00292 lua_assert(fs->freereg == fs->nactvar); 00293 } 00294 00295 00296 static void leaveblock (FuncState *fs) { 00297 BlockCnt *bl = fs->bl; 00298 fs->bl = bl->previous; 00299 removevars(fs->ls, bl->nactvar); 00300 if (bl->upval) 00301 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); 00302 /* a block either controls scope or breaks (never both) */ 00303 lua_assert(!bl->isbreakable || !bl->upval); 00304 lua_assert(bl->nactvar == fs->nactvar); 00305 fs->freereg = fs->nactvar; /* free registers */ 00306 luaK_patchtohere(fs, bl->breaklist); 00307 } 00308 00309 00310 static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { 00311 FuncState *fs = ls->fs; 00312 Proto *f = fs->f; 00313 int oldsize = f->sizep; 00314 int i; 00315 luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, 00316 MAXARG_Bx, "constant table overflow"); 00317 while (oldsize < f->sizep) f->p[oldsize++] = NULL; 00318 f->p[fs->np++] = func->f; 00319 luaC_objbarrier(ls->L, f, func->f); 00320 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); 00321 for (i=0; i<func->f->nups; i++) { 00322 OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; 00323 luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); 00324 } 00325 } 00326 00327 00328 static void open_func (LexState *ls, FuncState *fs) { 00329 lua_State *L = ls->L; 00330 Proto *f = luaF_newproto(L); 00331 fs->f = f; 00332 fs->prev = ls->fs; /* linked list of funcstates */ 00333 fs->ls = ls; 00334 fs->L = L; 00335 ls->fs = fs; 00336 fs->pc = 0; 00337 fs->lasttarget = -1; 00338 fs->jpc = NO_JUMP; 00339 fs->freereg = 0; 00340 fs->nk = 0; 00341 fs->np = 0; 00342 fs->nlocvars = 0; 00343 fs->nactvar = 0; 00344 fs->bl = NULL; 00345 f->source = ls->source; 00346 f->maxstacksize = 2; /* registers 0/1 are always valid */ 00347 fs->h = luaH_new(L, 0, 0); 00348 /* anchor table of constants and prototype (to avoid being collected) */ 00349 sethvalue2s(L, L->top, fs->h); 00350 incr_top(L); 00351 setptvalue2s(L, L->top, f); 00352 incr_top(L); 00353 } 00354 00355 00356 static void close_func (LexState *ls) { 00357 lua_State *L = ls->L; 00358 FuncState *fs = ls->fs; 00359 Proto *f = fs->f; 00360 removevars(ls, 0); 00361 luaK_ret(fs, 0, 0); /* final return */ 00362 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); 00363 f->sizecode = fs->pc; 00364 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); 00365 f->sizelineinfo = fs->pc; 00366 luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); 00367 f->sizek = fs->nk; 00368 luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); 00369 f->sizep = fs->np; 00370 luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); 00371 f->sizelocvars = fs->nlocvars; 00372 luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); 00373 f->sizeupvalues = f->nups; 00374 lua_assert(luaG_checkcode(f)); 00375 lua_assert(fs->bl == NULL); 00376 ls->fs = fs->prev; 00377 L->top -= 2; /* remove table and prototype from the stack */ 00378 /* last token read was anchored in defunct function; must reanchor it */ 00379 if (fs) anchor_token(ls); 00380 } 00381 00382 00383 Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { 00384 struct LexState lexstate; 00385 struct FuncState funcstate; 00386 lexstate.buff = buff; 00387 luaX_setinput(L, &lexstate, z, luaS_new(L, name)); 00388 open_func(&lexstate, &funcstate); 00389 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ 00390 luaX_next(&lexstate); /* read first token */ 00391 chunk(&lexstate); 00392 check(&lexstate, TK_EOS); 00393 close_func(&lexstate); 00394 lua_assert(funcstate.prev == NULL); 00395 lua_assert(funcstate.f->nups == 0); 00396 lua_assert(lexstate.fs == NULL); 00397 return funcstate.f; 00398 } 00399 00400 00401 00402 /*============================================================*/ 00403 /* GRAMMAR RULES */ 00404 /*============================================================*/ 00405 00406 00407 static void field (LexState *ls, expdesc *v) { 00408 /* field -> ['.' | ':'] NAME */ 00409 FuncState *fs = ls->fs; 00410 expdesc key; 00411 luaK_exp2anyreg(fs, v); 00412 luaX_next(ls); /* skip the dot or colon */ 00413 checkname(ls, &key); 00414 luaK_indexed(fs, v, &key); 00415 } 00416 00417 00418 static void yindex (LexState *ls, expdesc *v) { 00419 /* index -> '[' expr ']' */ 00420 luaX_next(ls); /* skip the '[' */ 00421 expr(ls, v); 00422 luaK_exp2val(ls->fs, v); 00423 checknext(ls, ']'); 00424 } 00425 00426 00427 /* 00428 ** {====================================================================== 00429 ** Rules for Constructors 00430 ** ======================================================================= 00431 */ 00432 00433 00434 struct ConsControl { 00435 expdesc v; /* last list item read */ 00436 expdesc *t; /* table descriptor */ 00437 int nh; /* total number of `record' elements */ 00438 int na; /* total number of array elements */ 00439 int tostore; /* number of array elements pending to be stored */ 00440 }; 00441 00442 00443 static void recfield (LexState *ls, struct ConsControl *cc) { 00444 /* recfield -> (NAME | `['exp1`]') = exp1 */ 00445 FuncState *fs = ls->fs; 00446 int reg = ls->fs->freereg; 00447 expdesc key, val; 00448 int rkkey; 00449 if (ls->t.token == TK_NAME) { 00450 luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); 00451 checkname(ls, &key); 00452 } 00453 else /* ls->t.token == '[' */ 00454 yindex(ls, &key); 00455 cc->nh++; 00456 checknext(ls, '='); 00457 rkkey = luaK_exp2RK(fs, &key); 00458 expr(ls, &val); 00459 luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); 00460 fs->freereg = reg; /* free registers */ 00461 } 00462 00463 00464 static void closelistfield (FuncState *fs, struct ConsControl *cc) { 00465 if (cc->v.k == VVOID) return; /* there is no list item */ 00466 luaK_exp2nextreg(fs, &cc->v); 00467 cc->v.k = VVOID; 00468 if (cc->tostore == LFIELDS_PER_FLUSH) { 00469 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ 00470 cc->tostore = 0; /* no more items pending */ 00471 } 00472 } 00473 00474 00475 static void lastlistfield (FuncState *fs, struct ConsControl *cc) { 00476 if (cc->tostore == 0) return; 00477 if (hasmultret(cc->v.k)) { 00478 luaK_setmultret(fs, &cc->v); 00479 luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); 00480 cc->na--; /* do not count last expression (unknown number of elements) */ 00481 } 00482 else { 00483 if (cc->v.k != VVOID) 00484 luaK_exp2nextreg(fs, &cc->v); 00485 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); 00486 } 00487 } 00488 00489 00490 static void listfield (LexState *ls, struct ConsControl *cc) { 00491 expr(ls, &cc->v); 00492 luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); 00493 cc->na++; 00494 cc->tostore++; 00495 } 00496 00497 00498 static void constructor (LexState *ls, expdesc *t) { 00499 /* constructor -> ?? */ 00500 FuncState *fs = ls->fs; 00501 int line = ls->linenumber; 00502 int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); 00503 struct ConsControl cc; 00504 cc.na = cc.nh = cc.tostore = 0; 00505 cc.t = t; 00506 init_exp(t, VRELOCABLE, pc); 00507 init_exp(&cc.v, VVOID, 0); /* no value (yet) */ 00508 luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ 00509 checknext(ls, '{'); 00510 do { 00511 lua_assert(cc.v.k == VVOID || cc.tostore > 0); 00512 if (ls->t.token == '}') break; 00513 closelistfield(fs, &cc); 00514 switch(ls->t.token) { 00515 case TK_NAME: { /* may be listfields or recfields */ 00516 luaX_lookahead(ls); 00517 if (ls->lookahead.token != '=') /* expression? */ 00518 listfield(ls, &cc); 00519 else 00520 recfield(ls, &cc); 00521 break; 00522 } 00523 case '[': { /* constructor_item -> recfield */ 00524 recfield(ls, &cc); 00525 break; 00526 } 00527 default: { /* constructor_part -> listfield */ 00528 listfield(ls, &cc); 00529 break; 00530 } 00531 } 00532 } while (testnext(ls, ',') || testnext(ls, ';')); 00533 check_match(ls, '}', '{', line); 00534 lastlistfield(fs, &cc); 00535 SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ 00536 SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ 00537 } 00538 00539 /* }====================================================================== */ 00540 00541 00542 00543 static void parlist (LexState *ls) { 00544 /* parlist -> [ param { `,' param } ] */ 00545 FuncState *fs = ls->fs; 00546 Proto *f = fs->f; 00547 int nparams = 0; 00548 f->is_vararg = 0; 00549 if (ls->t.token != ')') { /* is `parlist' not empty? */ 00550 do { 00551 switch (ls->t.token) { 00552 case TK_NAME: { /* param -> NAME */ 00553 new_localvar(ls, str_checkname(ls), nparams++); 00554 break; 00555 } 00556 case TK_DOTS: { /* param -> `...' */ 00557 luaX_next(ls); 00558 #if defined(LUA_COMPAT_VARARG) 00559 /* use `arg' as default name */ 00560 new_localvarliteral(ls, "arg", nparams++); 00561 f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; 00562 #endif 00563 f->is_vararg |= VARARG_ISVARARG; 00564 break; 00565 } 00566 default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected"); 00567 } 00568 } while (!f->is_vararg && testnext(ls, ',')); 00569 } 00570 adjustlocalvars(ls, nparams); 00571 f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); 00572 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ 00573 } 00574 00575 00576 static void body (LexState *ls, expdesc *e, int needself, int line) { 00577 /* body -> `(' parlist `)' chunk END */ 00578 FuncState new_fs; 00579 open_func(ls, &new_fs); 00580 new_fs.f->linedefined = line; 00581 checknext(ls, '('); 00582 if (needself) { 00583 new_localvarliteral(ls, "self", 0); 00584 adjustlocalvars(ls, 1); 00585 } 00586 parlist(ls); 00587 checknext(ls, ')'); 00588 chunk(ls); 00589 new_fs.f->lastlinedefined = ls->linenumber; 00590 check_match(ls, TK_END, TK_FUNCTION, line); 00591 close_func(ls); 00592 pushclosure(ls, &new_fs, e); 00593 } 00594 00595 00596 static int explist1 (LexState *ls, expdesc *v) { 00597 /* explist1 -> expr { `,' expr } */ 00598 int n = 1; /* at least one expression */ 00599 expr(ls, v); 00600 while (testnext(ls, ',')) { 00601 luaK_exp2nextreg(ls->fs, v); 00602 expr(ls, v); 00603 n++; 00604 } 00605 return n; 00606 } 00607 00608 00609 static void funcargs (LexState *ls, expdesc *f) { 00610 FuncState *fs = ls->fs; 00611 expdesc args; 00612 int base, nparams; 00613 int line = ls->linenumber; 00614 switch (ls->t.token) { 00615 case '(': { /* funcargs -> `(' [ explist1 ] `)' */ 00616 if (line != ls->lastline) 00617 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); 00618 luaX_next(ls); 00619 if (ls->t.token == ')') /* arg list is empty? */ 00620 args.k = VVOID; 00621 else { 00622 explist1(ls, &args); 00623 luaK_setmultret(fs, &args); 00624 } 00625 check_match(ls, ')', '(', line); 00626 break; 00627 } 00628 case '{': { /* funcargs -> constructor */ 00629 constructor(ls, &args); 00630 break; 00631 } 00632 case TK_STRING: { /* funcargs -> STRING */ 00633 codestring(ls, &args, ls->t.seminfo.ts); 00634 luaX_next(ls); /* must use `seminfo' before `next' */ 00635 break; 00636 } 00637 default: { 00638 luaX_syntaxerror(ls, "function arguments expected"); 00639 return; 00640 } 00641 } 00642 lua_assert(f->k == VNONRELOC); 00643 base = f->u.s.info; /* base register for call */ 00644 if (hasmultret(args.k)) 00645 nparams = LUA_MULTRET; /* open call */ 00646 else { 00647 if (args.k != VVOID) 00648 luaK_exp2nextreg(fs, &args); /* close last argument */ 00649 nparams = fs->freereg - (base+1); 00650 } 00651 init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); 00652 luaK_fixline(fs, line); 00653 fs->freereg = base+1; /* call remove function and arguments and leaves 00654 (unless changed) one result */ 00655 } 00656 00657 00658 00659 00660 /* 00661 ** {====================================================================== 00662 ** Expression parsing 00663 ** ======================================================================= 00664 */ 00665 00666 00667 static void prefixexp (LexState *ls, expdesc *v) { 00668 /* prefixexp -> NAME | '(' expr ')' */ 00669 switch (ls->t.token) { 00670 case '(': { 00671 int line = ls->linenumber; 00672 luaX_next(ls); 00673 expr(ls, v); 00674 check_match(ls, ')', '(', line); 00675 luaK_dischargevars(ls->fs, v); 00676 return; 00677 } 00678 case TK_NAME: { 00679 singlevar(ls, v); 00680 return; 00681 } 00682 default: { 00683 luaX_syntaxerror(ls, "unexpected symbol"); 00684 return; 00685 } 00686 } 00687 } 00688 00689 00690 static void primaryexp (LexState *ls, expdesc *v) { 00691 /* primaryexp -> 00692 prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ 00693 FuncState *fs = ls->fs; 00694 prefixexp(ls, v); 00695 for (;;) { 00696 switch (ls->t.token) { 00697 case '.': { /* field */ 00698 field(ls, v); 00699 break; 00700 } 00701 case '[': { /* `[' exp1 `]' */ 00702 expdesc key; 00703 luaK_exp2anyreg(fs, v); 00704 yindex(ls, &key); 00705 luaK_indexed(fs, v, &key); 00706 break; 00707 } 00708 case ':': { /* `:' NAME funcargs */ 00709 expdesc key; 00710 luaX_next(ls); 00711 checkname(ls, &key); 00712 luaK_self(fs, v, &key); 00713 funcargs(ls, v); 00714 break; 00715 } 00716 case '(': case TK_STRING: case '{': { /* funcargs */ 00717 luaK_exp2nextreg(fs, v); 00718 funcargs(ls, v); 00719 break; 00720 } 00721 default: return; 00722 } 00723 } 00724 } 00725 00726 00727 static void simpleexp (LexState *ls, expdesc *v) { 00728 /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | 00729 constructor | FUNCTION body | primaryexp */ 00730 switch (ls->t.token) { 00731 case TK_NUMBER: { 00732 init_exp(v, VKNUM, 0); 00733 v->u.nval = ls->t.seminfo.r; 00734 break; 00735 } 00736 case TK_STRING: { 00737 codestring(ls, v, ls->t.seminfo.ts); 00738 break; 00739 } 00740 case TK_NIL: { 00741 init_exp(v, VNIL, 0); 00742 break; 00743 } 00744 case TK_TRUE: { 00745 init_exp(v, VTRUE, 0); 00746 break; 00747 } 00748 case TK_FALSE: { 00749 init_exp(v, VFALSE, 0); 00750 break; 00751 } 00752 case TK_DOTS: { /* vararg */ 00753 FuncState *fs = ls->fs; 00754 check_condition(ls, fs->f->is_vararg, 00755 "cannot use " LUA_QL("...") " outside a vararg function"); 00756 fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ 00757 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); 00758 break; 00759 } 00760 case '{': { /* constructor */ 00761 constructor(ls, v); 00762 return; 00763 } 00764 case TK_FUNCTION: { 00765 luaX_next(ls); 00766 body(ls, v, 0, ls->linenumber); 00767 return; 00768 } 00769 default: { 00770 primaryexp(ls, v); 00771 return; 00772 } 00773 } 00774 luaX_next(ls); 00775 } 00776 00777 00778 static UnOpr getunopr (int op) { 00779 switch (op) { 00780 case TK_NOT: return OPR_NOT; 00781 case '-': return OPR_MINUS; 00782 case '#': return OPR_LEN; 00783 default: return OPR_NOUNOPR; 00784 } 00785 } 00786 00787 00788 static BinOpr getbinopr (int op) { 00789 switch (op) { 00790 case '+': return OPR_ADD; 00791 case '-': return OPR_SUB; 00792 case '*': return OPR_MUL; 00793 case '/': return OPR_DIV; 00794 case '%': return OPR_MOD; 00795 case '^': return OPR_POW; 00796 case TK_CONCAT: return OPR_CONCAT; 00797 case TK_NE: return OPR_NE; 00798 case TK_EQ: return OPR_EQ; 00799 case '<': return OPR_LT; 00800 case TK_LE: return OPR_LE; 00801 case '>': return OPR_GT; 00802 case TK_GE: return OPR_GE; 00803 case TK_AND: return OPR_AND; 00804 case TK_OR: return OPR_OR; 00805 default: return OPR_NOBINOPR; 00806 } 00807 } 00808 00809 00810 static const struct { 00811 lu_byte left; /* left priority for each binary operator */ 00812 lu_byte right; /* right priority */ 00813 } priority[] = { /* ORDER OPR */ 00814 {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ 00815 {10, 9}, {5, 4}, /* power and concat (right associative) */ 00816 {3, 3}, {3, 3}, /* equality and inequality */ 00817 {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ 00818 {2, 2}, {1, 1} /* logical (and/or) */ 00819 }; 00820 00821 #define UNARY_PRIORITY 8 /* priority for unary operators */ 00822 00823 00824 /* 00825 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } 00826 ** where `binop' is any binary operator with a priority higher than `limit' 00827 */ 00828 static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { 00829 BinOpr op; 00830 UnOpr uop; 00831 enterlevel(ls); 00832 uop = getunopr(ls->t.token); 00833 if (uop != OPR_NOUNOPR) { 00834 luaX_next(ls); 00835 subexpr(ls, v, UNARY_PRIORITY); 00836 luaK_prefix(ls->fs, uop, v); 00837 } 00838 else simpleexp(ls, v); 00839 /* expand while operators have priorities higher than `limit' */ 00840 op = getbinopr(ls->t.token); 00841 while (op != OPR_NOBINOPR && priority[op].left > limit) { 00842 expdesc v2; 00843 BinOpr nextop; 00844 luaX_next(ls); 00845 luaK_infix(ls->fs, op, v); 00846 /* read sub-expression with higher priority */ 00847 nextop = subexpr(ls, &v2, priority[op].right); 00848 luaK_posfix(ls->fs, op, v, &v2); 00849 op = nextop; 00850 } 00851 leavelevel(ls); 00852 return op; /* return first untreated operator */ 00853 } 00854 00855 00856 static void expr (LexState *ls, expdesc *v) { 00857 subexpr(ls, v, 0); 00858 } 00859 00860 /* }==================================================================== */ 00861 00862 00863 00864 /* 00865 ** {====================================================================== 00866 ** Rules for Statements 00867 ** ======================================================================= 00868 */ 00869 00870 00871 static int block_follow (int token) { 00872 switch (token) { 00873 case TK_ELSE: case TK_ELSEIF: case TK_END: 00874 case TK_UNTIL: case TK_EOS: 00875 return 1; 00876 default: return 0; 00877 } 00878 } 00879 00880 00881 static void block (LexState *ls) { 00882 /* block -> chunk */ 00883 FuncState *fs = ls->fs; 00884 BlockCnt bl; 00885 enterblock(fs, &bl, 0); 00886 chunk(ls); 00887 lua_assert(bl.breaklist == NO_JUMP); 00888 leaveblock(fs); 00889 } 00890 00891 00892 /* 00893 ** structure to chain all variables in the left-hand side of an 00894 ** assignment 00895 */ 00896 struct LHS_assign { 00897 struct LHS_assign *prev; 00898 expdesc v; /* variable (global, local, upvalue, or indexed) */ 00899 }; 00900 00901 00902 /* 00903 ** check whether, in an assignment to a local variable, the local variable 00904 ** is needed in a previous assignment (to a table). If so, save original 00905 ** local value in a safe place and use this safe copy in the previous 00906 ** assignment. 00907 */ 00908 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { 00909 FuncState *fs = ls->fs; 00910 int extra = fs->freereg; /* eventual position to save local variable */ 00911 int conflict = 0; 00912 for (; lh; lh = lh->prev) { 00913 if (lh->v.k == VINDEXED) { 00914 if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ 00915 conflict = 1; 00916 lh->v.u.s.info = extra; /* previous assignment will use safe copy */ 00917 } 00918 if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ 00919 conflict = 1; 00920 lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ 00921 } 00922 } 00923 } 00924 if (conflict) { 00925 luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ 00926 luaK_reserveregs(fs, 1); 00927 } 00928 } 00929 00930 00931 static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { 00932 expdesc e; 00933 check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, 00934 "syntax error"); 00935 if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ 00936 struct LHS_assign nv; 00937 nv.prev = lh; 00938 primaryexp(ls, &nv.v); 00939 if (nv.v.k == VLOCAL) 00940 check_conflict(ls, lh, &nv.v); 00941 luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, 00942 "variables in assignment"); 00943 assignment(ls, &nv, nvars+1); 00944 } 00945 else { /* assignment -> `=' explist1 */ 00946 int nexps; 00947 checknext(ls, '='); 00948 nexps = explist1(ls, &e); 00949 if (nexps != nvars) { 00950 adjust_assign(ls, nvars, nexps, &e); 00951 if (nexps > nvars) 00952 ls->fs->freereg -= nexps - nvars; /* remove extra values */ 00953 } 00954 else { 00955 luaK_setoneret(ls->fs, &e); /* close last expression */ 00956 luaK_storevar(ls->fs, &lh->v, &e); 00957 return; /* avoid default */ 00958 } 00959 } 00960 init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ 00961 luaK_storevar(ls->fs, &lh->v, &e); 00962 } 00963 00964 00965 static int cond (LexState *ls) { 00966 /* cond -> exp */ 00967 expdesc v; 00968 expr(ls, &v); /* read condition */ 00969 if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ 00970 luaK_goiftrue(ls->fs, &v); 00971 return v.f; 00972 } 00973 00974 00975 static void breakstat (LexState *ls) { 00976 FuncState *fs = ls->fs; 00977 BlockCnt *bl = fs->bl; 00978 int upval = 0; 00979 while (bl && !bl->isbreakable) { 00980 upval |= bl->upval; 00981 bl = bl->previous; 00982 } 00983 if (!bl) 00984 luaX_syntaxerror(ls, "no loop to break"); 00985 if (upval) 00986 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); 00987 luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); 00988 } 00989 00990 00991 static void whilestat (LexState *ls, int line) { 00992 /* whilestat -> WHILE cond DO block END */ 00993 FuncState *fs = ls->fs; 00994 int whileinit; 00995 int condexit; 00996 BlockCnt bl; 00997 luaX_next(ls); /* skip WHILE */ 00998 whileinit = luaK_getlabel(fs); 00999 condexit = cond(ls); 01000 enterblock(fs, &bl, 1); 01001 checknext(ls, TK_DO); 01002 block(ls); 01003 luaK_patchlist(fs, luaK_jump(fs), whileinit); 01004 check_match(ls, TK_END, TK_WHILE, line); 01005 leaveblock(fs); 01006 luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ 01007 } 01008 01009 01010 static void repeatstat (LexState *ls, int line) { 01011 /* repeatstat -> REPEAT block UNTIL cond */ 01012 int condexit; 01013 FuncState *fs = ls->fs; 01014 int repeat_init = luaK_getlabel(fs); 01015 BlockCnt bl1, bl2; 01016 enterblock(fs, &bl1, 1); /* loop block */ 01017 enterblock(fs, &bl2, 0); /* scope block */ 01018 luaX_next(ls); /* skip REPEAT */ 01019 chunk(ls); 01020 check_match(ls, TK_UNTIL, TK_REPEAT, line); 01021 condexit = cond(ls); /* read condition (inside scope block) */ 01022 if (!bl2.upval) { /* no upvalues? */ 01023 leaveblock(fs); /* finish scope */ 01024 luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ 01025 } 01026 else { /* complete semantics when there are upvalues */ 01027 breakstat(ls); /* if condition then break */ 01028 luaK_patchtohere(ls->fs, condexit); /* else... */ 01029 leaveblock(fs); /* finish scope... */ 01030 luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ 01031 } 01032 leaveblock(fs); /* finish loop */ 01033 } 01034 01035 01036 static int exp1 (LexState *ls) { 01037 expdesc e; 01038 int k; 01039 expr(ls, &e); 01040 k = e.k; 01041 luaK_exp2nextreg(ls->fs, &e); 01042 return k; 01043 } 01044 01045 01046 static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { 01047 /* forbody -> DO block */ 01048 BlockCnt bl; 01049 FuncState *fs = ls->fs; 01050 int prep, endfor; 01051 adjustlocalvars(ls, 3); /* control variables */ 01052 checknext(ls, TK_DO); 01053 prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); 01054 enterblock(fs, &bl, 0); /* scope for declared variables */ 01055 adjustlocalvars(ls, nvars); 01056 luaK_reserveregs(fs, nvars); 01057 block(ls); 01058 leaveblock(fs); /* end of scope for declared variables */ 01059 luaK_patchtohere(fs, prep); 01060 endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : 01061 luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); 01062 luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ 01063 luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); 01064 } 01065 01066 01067 static void fornum (LexState *ls, TString *varname, int line) { 01068 /* fornum -> NAME = exp1,exp1[,exp1] forbody */ 01069 FuncState *fs = ls->fs; 01070 int base = fs->freereg; 01071 new_localvarliteral(ls, "(for index)", 0); 01072 new_localvarliteral(ls, "(for limit)", 1); 01073 new_localvarliteral(ls, "(for step)", 2); 01074 new_localvar(ls, varname, 3); 01075 checknext(ls, '='); 01076 exp1(ls); /* initial value */ 01077 checknext(ls, ','); 01078 exp1(ls); /* limit */ 01079 if (testnext(ls, ',')) 01080 exp1(ls); /* optional step */ 01081 else { /* default step = 1 */ 01082 luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); 01083 luaK_reserveregs(fs, 1); 01084 } 01085 forbody(ls, base, line, 1, 1); 01086 } 01087 01088 01089 static void forlist (LexState *ls, TString *indexname) { 01090 /* forlist -> NAME {,NAME} IN explist1 forbody */ 01091 FuncState *fs = ls->fs; 01092 expdesc e; 01093 int nvars = 0; 01094 int line; 01095 int base = fs->freereg; 01096 /* create control variables */ 01097 new_localvarliteral(ls, "(for generator)", nvars++); 01098 new_localvarliteral(ls, "(for state)", nvars++); 01099 new_localvarliteral(ls, "(for control)", nvars++); 01100 /* create declared variables */ 01101 new_localvar(ls, indexname, nvars++); 01102 while (testnext(ls, ',')) 01103 new_localvar(ls, str_checkname(ls), nvars++); 01104 checknext(ls, TK_IN); 01105 line = ls->linenumber; 01106 adjust_assign(ls, 3, explist1(ls, &e), &e); 01107 luaK_checkstack(fs, 3); /* extra space to call generator */ 01108 forbody(ls, base, line, nvars - 3, 0); 01109 } 01110 01111 01112 static void forstat (LexState *ls, int line) { 01113 /* forstat -> FOR (fornum | forlist) END */ 01114 FuncState *fs = ls->fs; 01115 TString *varname; 01116 BlockCnt bl; 01117 enterblock(fs, &bl, 1); /* scope for loop and control variables */ 01118 luaX_next(ls); /* skip `for' */ 01119 varname = str_checkname(ls); /* first variable name */ 01120 switch (ls->t.token) { 01121 case '=': fornum(ls, varname, line); break; 01122 case ',': case TK_IN: forlist(ls, varname); break; 01123 default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); 01124 } 01125 check_match(ls, TK_END, TK_FOR, line); 01126 leaveblock(fs); /* loop scope (`break' jumps to this point) */ 01127 } 01128 01129 01130 static int test_then_block (LexState *ls) { 01131 /* test_then_block -> [IF | ELSEIF] cond THEN block */ 01132 int condexit; 01133 luaX_next(ls); /* skip IF or ELSEIF */ 01134 condexit = cond(ls); 01135 checknext(ls, TK_THEN); 01136 block(ls); /* `then' part */ 01137 return condexit; 01138 } 01139 01140 01141 static void ifstat (LexState *ls, int line) { 01142 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ 01143 FuncState *fs = ls->fs; 01144 int flist; 01145 int escapelist = NO_JUMP; 01146 flist = test_then_block(ls); /* IF cond THEN block */ 01147 while (ls->t.token == TK_ELSEIF) { 01148 luaK_concat(fs, &escapelist, luaK_jump(fs)); 01149 luaK_patchtohere(fs, flist); 01150 flist = test_then_block(ls); /* ELSEIF cond THEN block */ 01151 } 01152 if (ls->t.token == TK_ELSE) { 01153 luaK_concat(fs, &escapelist, luaK_jump(fs)); 01154 luaK_patchtohere(fs, flist); 01155 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ 01156 block(ls); /* `else' part */ 01157 } 01158 else 01159 luaK_concat(fs, &escapelist, flist); 01160 luaK_patchtohere(fs, escapelist); 01161 check_match(ls, TK_END, TK_IF, line); 01162 } 01163 01164 01165 static void localfunc (LexState *ls) { 01166 expdesc v, b; 01167 FuncState *fs = ls->fs; 01168 new_localvar(ls, str_checkname(ls), 0); 01169 init_exp(&v, VLOCAL, fs->freereg); 01170 luaK_reserveregs(fs, 1); 01171 adjustlocalvars(ls, 1); 01172 body(ls, &b, 0, ls->linenumber); 01173 luaK_storevar(fs, &v, &b); 01174 /* debug information will only see the variable after this point! */ 01175 getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; 01176 } 01177 01178 01179 static void localstat (LexState *ls) { 01180 /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ 01181 int nvars = 0; 01182 int nexps; 01183 expdesc e; 01184 do { 01185 new_localvar(ls, str_checkname(ls), nvars++); 01186 } while (testnext(ls, ',')); 01187 if (testnext(ls, '=')) 01188 nexps = explist1(ls, &e); 01189 else { 01190 e.k = VVOID; 01191 nexps = 0; 01192 } 01193 adjust_assign(ls, nvars, nexps, &e); 01194 adjustlocalvars(ls, nvars); 01195 } 01196 01197 01198 static int funcname (LexState *ls, expdesc *v) { 01199 /* funcname -> NAME {field} [`:' NAME] */ 01200 int needself = 0; 01201 singlevar(ls, v); 01202 while (ls->t.token == '.') 01203 field(ls, v); 01204 if (ls->t.token == ':') { 01205 needself = 1; 01206 field(ls, v); 01207 } 01208 return needself; 01209 } 01210 01211 01212 static void funcstat (LexState *ls, int line) { 01213 /* funcstat -> FUNCTION funcname body */ 01214 int needself; 01215 expdesc v, b; 01216 luaX_next(ls); /* skip FUNCTION */ 01217 needself = funcname(ls, &v); 01218 body(ls, &b, needself, line); 01219 luaK_storevar(ls->fs, &v, &b); 01220 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ 01221 } 01222 01223 01224 static void exprstat (LexState *ls) { 01225 /* stat -> func | assignment */ 01226 FuncState *fs = ls->fs; 01227 struct LHS_assign v; 01228 primaryexp(ls, &v.v); 01229 if (v.v.k == VCALL) /* stat -> func */ 01230 SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ 01231 else { /* stat -> assignment */ 01232 v.prev = NULL; 01233 assignment(ls, &v, 1); 01234 } 01235 } 01236 01237 01238 static void retstat (LexState *ls) { 01239 /* stat -> RETURN explist */ 01240 FuncState *fs = ls->fs; 01241 expdesc e; 01242 int first, nret; /* registers with returned values */ 01243 luaX_next(ls); /* skip RETURN */ 01244 if (block_follow(ls->t.token) || ls->t.token == ';') 01245 first = nret = 0; /* return no values */ 01246 else { 01247 nret = explist1(ls, &e); /* optional return values */ 01248 if (hasmultret(e.k)) { 01249 luaK_setmultret(fs, &e); 01250 if (e.k == VCALL && nret == 1) { /* tail call? */ 01251 SET_OPCODE(getcode(fs,&e), OP_TAILCALL); 01252 lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); 01253 } 01254 first = fs->nactvar; 01255 nret = LUA_MULTRET; /* return all values */ 01256 } 01257 else { 01258 if (nret == 1) /* only one single value? */ 01259 first = luaK_exp2anyreg(fs, &e); 01260 else { 01261 luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ 01262 first = fs->nactvar; /* return all `active' values */ 01263 lua_assert(nret == fs->freereg - first); 01264 } 01265 } 01266 } 01267 luaK_ret(fs, first, nret); 01268 } 01269 01270 01271 static int statement (LexState *ls) { 01272 int line = ls->linenumber; /* may be needed for error messages */ 01273 switch (ls->t.token) { 01274 case TK_IF: { /* stat -> ifstat */ 01275 ifstat(ls, line); 01276 return 0; 01277 } 01278 case TK_WHILE: { /* stat -> whilestat */ 01279 whilestat(ls, line); 01280 return 0; 01281 } 01282 case TK_DO: { /* stat -> DO block END */ 01283 luaX_next(ls); /* skip DO */ 01284 block(ls); 01285 check_match(ls, TK_END, TK_DO, line); 01286 return 0; 01287 } 01288 case TK_FOR: { /* stat -> forstat */ 01289 forstat(ls, line); 01290 return 0; 01291 } 01292 case TK_REPEAT: { /* stat -> repeatstat */ 01293 repeatstat(ls, line); 01294 return 0; 01295 } 01296 case TK_FUNCTION: { 01297 funcstat(ls, line); /* stat -> funcstat */ 01298 return 0; 01299 } 01300 case TK_LOCAL: { /* stat -> localstat */ 01301 luaX_next(ls); /* skip LOCAL */ 01302 if (testnext(ls, TK_FUNCTION)) /* local function? */ 01303 localfunc(ls); 01304 else 01305 localstat(ls); 01306 return 0; 01307 } 01308 case TK_RETURN: { /* stat -> retstat */ 01309 retstat(ls); 01310 return 1; /* must be last statement */ 01311 } 01312 case TK_BREAK: { /* stat -> breakstat */ 01313 luaX_next(ls); /* skip BREAK */ 01314 breakstat(ls); 01315 return 1; /* must be last statement */ 01316 } 01317 default: { 01318 exprstat(ls); 01319 return 0; /* to avoid warnings */ 01320 } 01321 } 01322 } 01323 01324 01325 static void chunk (LexState *ls) { 01326 /* chunk -> { stat [`;'] } */ 01327 int islast = 0; 01328 enterlevel(ls); 01329 while (!islast && !block_follow(ls->t.token)) { 01330 islast = statement(ls); 01331 testnext(ls, ';'); 01332 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && 01333 ls->fs->freereg >= ls->fs->nactvar); 01334 ls->fs->freereg = ls->fs->nactvar; /* free registers */ 01335 } 01336 leavelevel(ls); 01337 } 01338 01339 /* }====================================================================== */
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1