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