00001
00002
00003
00004
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
00039
00040 typedef struct BlockCnt {
00041 struct BlockCnt *previous;
00042 int breaklist;
00043 lu_byte nactvar;
00044 lu_byte upval;
00045 lu_byte isbreakable;
00046 } BlockCnt;
00047
00048
00049
00050
00051
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
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;
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) {
00226 init_exp(var, VGLOBAL, NO_REG);
00227 return VGLOBAL;
00228 }
00229 else {
00230 int v = searchvar(fs, n);
00231 if (v >= 0) {
00232 init_exp(var, VLOCAL, v);
00233 if (!base)
00234 markupval(fs, v);
00235 return VLOCAL;
00236 }
00237 else {
00238 if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
00239 return VGLOBAL;
00240 var->u.s.info = indexupvalue(fs, n, var);
00241 var->k = VUPVAL;
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);
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++;
00261 if (extra < 0) extra = 0;
00262 luaK_setreturns(fs, e, extra);
00263 if (extra > 1) luaK_reserveregs(fs, extra-1);
00264 }
00265 else {
00266 if (e->k != VVOID) luaK_exp2nextreg(fs, e);
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
00303 lua_assert(!bl->isbreakable || !bl->upval);
00304 lua_assert(bl->nactvar == fs->nactvar);
00305 fs->freereg = fs->nactvar;
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;
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;
00347 fs->h = luaH_new(L, 0, 0);
00348
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);
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;
00378
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;
00390 luaX_next(&lexstate);
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
00404
00405
00406
00407 static void field (LexState *ls, expdesc *v) {
00408
00409 FuncState *fs = ls->fs;
00410 expdesc key;
00411 luaK_exp2anyreg(fs, v);
00412 luaX_next(ls);
00413 checkname(ls, &key);
00414 luaK_indexed(fs, v, &key);
00415 }
00416
00417
00418 static void yindex (LexState *ls, expdesc *v) {
00419
00420 luaX_next(ls);
00421 expr(ls, v);
00422 luaK_exp2val(ls->fs, v);
00423 checknext(ls, ']');
00424 }
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 struct ConsControl {
00435 expdesc v;
00436 expdesc *t;
00437 int nh;
00438 int na;
00439 int tostore;
00440 };
00441
00442
00443 static void recfield (LexState *ls, struct ConsControl *cc) {
00444
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
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;
00461 }
00462
00463
00464 static void closelistfield (FuncState *fs, struct ConsControl *cc) {
00465 if (cc->v.k == VVOID) return;
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);
00470 cc->tostore = 0;
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--;
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
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);
00508 luaK_exp2nextreg(ls->fs, t);
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: {
00516 luaX_lookahead(ls);
00517 if (ls->lookahead.token != '=')
00518 listfield(ls, &cc);
00519 else
00520 recfield(ls, &cc);
00521 break;
00522 }
00523 case '[': {
00524 recfield(ls, &cc);
00525 break;
00526 }
00527 default: {
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));
00536 SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));
00537 }
00538
00539
00540
00541
00542
00543 static void parlist (LexState *ls) {
00544
00545 FuncState *fs = ls->fs;
00546 Proto *f = fs->f;
00547 int nparams = 0;
00548 f->is_vararg = 0;
00549 if (ls->t.token != ')') {
00550 do {
00551 switch (ls->t.token) {
00552 case TK_NAME: {
00553 new_localvar(ls, str_checkname(ls), nparams++);
00554 break;
00555 }
00556 case TK_DOTS: {
00557 luaX_next(ls);
00558 #if defined(LUA_COMPAT_VARARG)
00559
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);
00573 }
00574
00575
00576 static void body (LexState *ls, expdesc *e, int needself, int line) {
00577
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
00598 int n = 1;
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 '(': {
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 == ')')
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 '{': {
00629 constructor(ls, &args);
00630 break;
00631 }
00632 case TK_STRING: {
00633 codestring(ls, &args, ls->t.seminfo.ts);
00634 luaX_next(ls);
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;
00644 if (hasmultret(args.k))
00645 nparams = LUA_MULTRET;
00646 else {
00647 if (args.k != VVOID)
00648 luaK_exp2nextreg(fs, &args);
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;
00654
00655 }
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 static void prefixexp (LexState *ls, expdesc *v) {
00668
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
00692
00693 FuncState *fs = ls->fs;
00694 prefixexp(ls, v);
00695 for (;;) {
00696 switch (ls->t.token) {
00697 case '.': {
00698 field(ls, v);
00699 break;
00700 }
00701 case '[': {
00702 expdesc key;
00703 luaK_exp2anyreg(fs, v);
00704 yindex(ls, &key);
00705 luaK_indexed(fs, v, &key);
00706 break;
00707 }
00708 case ':': {
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 '{': {
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
00729
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: {
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;
00757 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
00758 break;
00759 }
00760 case '{': {
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;
00812 lu_byte right;
00813 } priority[] = {
00814 {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7},
00815 {10, 9}, {5, 4},
00816 {3, 3}, {3, 3},
00817 {3, 3}, {3, 3}, {3, 3}, {3, 3},
00818 {2, 2}, {1, 1}
00819 };
00820
00821 #define UNARY_PRIORITY 8
00822
00823
00824
00825
00826
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
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
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;
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
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
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
00894
00895
00896 struct LHS_assign {
00897 struct LHS_assign *prev;
00898 expdesc v;
00899 };
00900
00901
00902
00903
00904
00905
00906
00907
00908 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
00909 FuncState *fs = ls->fs;
00910 int extra = fs->freereg;
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) {
00915 conflict = 1;
00916 lh->v.u.s.info = extra;
00917 }
00918 if (lh->v.u.s.aux == v->u.s.info) {
00919 conflict = 1;
00920 lh->v.u.s.aux = extra;
00921 }
00922 }
00923 }
00924 if (conflict) {
00925 luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0);
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, ',')) {
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 assignment(ls, &nv, nvars+1);
00942 }
00943 else {
00944 int nexps;
00945 checknext(ls, '=');
00946 nexps = explist1(ls, &e);
00947 if (nexps != nvars) {
00948 adjust_assign(ls, nvars, nexps, &e);
00949 if (nexps > nvars)
00950 ls->fs->freereg -= nexps - nvars;
00951 }
00952 else {
00953 luaK_setoneret(ls->fs, &e);
00954 luaK_storevar(ls->fs, &lh->v, &e);
00955 return;
00956 }
00957 }
00958 init_exp(&e, VNONRELOC, ls->fs->freereg-1);
00959 luaK_storevar(ls->fs, &lh->v, &e);
00960 }
00961
00962
00963 static int cond (LexState *ls) {
00964
00965 expdesc v;
00966 expr(ls, &v);
00967 if (v.k == VNIL) v.k = VFALSE;
00968 luaK_goiftrue(ls->fs, &v);
00969 return v.f;
00970 }
00971
00972
00973 static void breakstat (LexState *ls) {
00974 FuncState *fs = ls->fs;
00975 BlockCnt *bl = fs->bl;
00976 int upval = 0;
00977 while (bl && !bl->isbreakable) {
00978 upval |= bl->upval;
00979 bl = bl->previous;
00980 }
00981 if (!bl)
00982 luaX_syntaxerror(ls, "no loop to break");
00983 if (upval)
00984 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
00985 luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
00986 }
00987
00988
00989 static void whilestat (LexState *ls, int line) {
00990
00991 FuncState *fs = ls->fs;
00992 int whileinit;
00993 int condexit;
00994 BlockCnt bl;
00995 luaX_next(ls);
00996 whileinit = luaK_getlabel(fs);
00997 condexit = cond(ls);
00998 enterblock(fs, &bl, 1);
00999 checknext(ls, TK_DO);
01000 block(ls);
01001 luaK_patchlist(fs, luaK_jump(fs), whileinit);
01002 check_match(ls, TK_END, TK_WHILE, line);
01003 leaveblock(fs);
01004 luaK_patchtohere(fs, condexit);
01005 }
01006
01007
01008 static void repeatstat (LexState *ls, int line) {
01009
01010 int condexit;
01011 FuncState *fs = ls->fs;
01012 int repeat_init = luaK_getlabel(fs);
01013 BlockCnt bl1, bl2;
01014 enterblock(fs, &bl1, 1);
01015 enterblock(fs, &bl2, 0);
01016 luaX_next(ls);
01017 chunk(ls);
01018 check_match(ls, TK_UNTIL, TK_REPEAT, line);
01019 condexit = cond(ls);
01020 if (!bl2.upval) {
01021 leaveblock(fs);
01022 luaK_patchlist(ls->fs, condexit, repeat_init);
01023 }
01024 else {
01025 breakstat(ls);
01026 luaK_patchtohere(ls->fs, condexit);
01027 leaveblock(fs);
01028 luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init);
01029 }
01030 leaveblock(fs);
01031 }
01032
01033
01034 static int exp1 (LexState *ls) {
01035 expdesc e;
01036 int k;
01037 expr(ls, &e);
01038 k = e.k;
01039 luaK_exp2nextreg(ls->fs, &e);
01040 return k;
01041 }
01042
01043
01044 static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
01045
01046 BlockCnt bl;
01047 FuncState *fs = ls->fs;
01048 int prep, endfor;
01049 adjustlocalvars(ls, 3);
01050 checknext(ls, TK_DO);
01051 prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
01052 enterblock(fs, &bl, 0);
01053 adjustlocalvars(ls, nvars);
01054 luaK_reserveregs(fs, nvars);
01055 block(ls);
01056 leaveblock(fs);
01057 luaK_patchtohere(fs, prep);
01058 endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
01059 luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
01060 luaK_fixline(fs, line);
01061 luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
01062 }
01063
01064
01065 static void fornum (LexState *ls, TString *varname, int line) {
01066
01067 FuncState *fs = ls->fs;
01068 int base = fs->freereg;
01069 new_localvarliteral(ls, "(for index)", 0);
01070 new_localvarliteral(ls, "(for limit)", 1);
01071 new_localvarliteral(ls, "(for step)", 2);
01072 new_localvar(ls, varname, 3);
01073 checknext(ls, '=');
01074 exp1(ls);
01075 checknext(ls, ',');
01076 exp1(ls);
01077 if (testnext(ls, ','))
01078 exp1(ls);
01079 else {
01080 luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
01081 luaK_reserveregs(fs, 1);
01082 }
01083 forbody(ls, base, line, 1, 1);
01084 }
01085
01086
01087 static void forlist (LexState *ls, TString *indexname) {
01088
01089 FuncState *fs = ls->fs;
01090 expdesc e;
01091 int nvars = 0;
01092 int line;
01093 int base = fs->freereg;
01094
01095 new_localvarliteral(ls, "(for generator)", nvars++);
01096 new_localvarliteral(ls, "(for state)", nvars++);
01097 new_localvarliteral(ls, "(for control)", nvars++);
01098
01099 new_localvar(ls, indexname, nvars++);
01100 while (testnext(ls, ','))
01101 new_localvar(ls, str_checkname(ls), nvars++);
01102 checknext(ls, TK_IN);
01103 line = ls->linenumber;
01104 adjust_assign(ls, 3, explist1(ls, &e), &e);
01105 luaK_checkstack(fs, 3);
01106 forbody(ls, base, line, nvars - 3, 0);
01107 }
01108
01109
01110 static void forstat (LexState *ls, int line) {
01111
01112 FuncState *fs = ls->fs;
01113 TString *varname;
01114 BlockCnt bl;
01115 enterblock(fs, &bl, 1);
01116 luaX_next(ls);
01117 varname = str_checkname(ls);
01118 switch (ls->t.token) {
01119 case '=': fornum(ls, varname, line); break;
01120 case ',': case TK_IN: forlist(ls, varname); break;
01121 default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
01122 }
01123 check_match(ls, TK_END, TK_FOR, line);
01124 leaveblock(fs);
01125 }
01126
01127
01128 static int test_then_block (LexState *ls) {
01129
01130 int condexit;
01131 luaX_next(ls);
01132 condexit = cond(ls);
01133 checknext(ls, TK_THEN);
01134 block(ls);
01135 return condexit;
01136 }
01137
01138
01139 static void ifstat (LexState *ls, int line) {
01140
01141 FuncState *fs = ls->fs;
01142 int flist;
01143 int escapelist = NO_JUMP;
01144 flist = test_then_block(ls);
01145 while (ls->t.token == TK_ELSEIF) {
01146 luaK_concat(fs, &escapelist, luaK_jump(fs));
01147 luaK_patchtohere(fs, flist);
01148 flist = test_then_block(ls);
01149 }
01150 if (ls->t.token == TK_ELSE) {
01151 luaK_concat(fs, &escapelist, luaK_jump(fs));
01152 luaK_patchtohere(fs, flist);
01153 luaX_next(ls);
01154 block(ls);
01155 }
01156 else
01157 luaK_concat(fs, &escapelist, flist);
01158 luaK_patchtohere(fs, escapelist);
01159 check_match(ls, TK_END, TK_IF, line);
01160 }
01161
01162
01163 static void localfunc (LexState *ls) {
01164 expdesc v, b;
01165 FuncState *fs = ls->fs;
01166 new_localvar(ls, str_checkname(ls), 0);
01167 init_exp(&v, VLOCAL, fs->freereg);
01168 luaK_reserveregs(fs, 1);
01169 adjustlocalvars(ls, 1);
01170 body(ls, &b, 0, ls->linenumber);
01171 luaK_storevar(fs, &v, &b);
01172
01173 getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
01174 }
01175
01176
01177 static void localstat (LexState *ls) {
01178
01179 int nvars = 0;
01180 int nexps;
01181 expdesc e;
01182 do {
01183 new_localvar(ls, str_checkname(ls), nvars++);
01184 } while (testnext(ls, ','));
01185 if (testnext(ls, '='))
01186 nexps = explist1(ls, &e);
01187 else {
01188 e.k = VVOID;
01189 nexps = 0;
01190 }
01191 adjust_assign(ls, nvars, nexps, &e);
01192 adjustlocalvars(ls, nvars);
01193 }
01194
01195
01196 static int funcname (LexState *ls, expdesc *v) {
01197
01198 int needself = 0;
01199 singlevar(ls, v);
01200 while (ls->t.token == '.')
01201 field(ls, v);
01202 if (ls->t.token == ':') {
01203 needself = 1;
01204 field(ls, v);
01205 }
01206 return needself;
01207 }
01208
01209
01210 static void funcstat (LexState *ls, int line) {
01211
01212 int needself;
01213 expdesc v, b;
01214 luaX_next(ls);
01215 needself = funcname(ls, &v);
01216 body(ls, &b, needself, line);
01217 luaK_storevar(ls->fs, &v, &b);
01218 luaK_fixline(ls->fs, line);
01219 }
01220
01221
01222 static void exprstat (LexState *ls) {
01223
01224 FuncState *fs = ls->fs;
01225 struct LHS_assign v;
01226 primaryexp(ls, &v.v);
01227 if (v.v.k == VCALL)
01228 SETARG_C(getcode(fs, &v.v), 1);
01229 else {
01230 v.prev = NULL;
01231 assignment(ls, &v, 1);
01232 }
01233 }
01234
01235
01236 static void retstat (LexState *ls) {
01237
01238 FuncState *fs = ls->fs;
01239 expdesc e;
01240 int first, nret;
01241 luaX_next(ls);
01242 if (block_follow(ls->t.token) || ls->t.token == ';')
01243 first = nret = 0;
01244 else {
01245 nret = explist1(ls, &e);
01246 if (hasmultret(e.k)) {
01247 luaK_setmultret(fs, &e);
01248 if (e.k == VCALL && nret == 1) {
01249 SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
01250 lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
01251 }
01252 first = fs->nactvar;
01253 nret = LUA_MULTRET;
01254 }
01255 else {
01256 if (nret == 1)
01257 first = luaK_exp2anyreg(fs, &e);
01258 else {
01259 luaK_exp2nextreg(fs, &e);
01260 first = fs->nactvar;
01261 lua_assert(nret == fs->freereg - first);
01262 }
01263 }
01264 }
01265 luaK_ret(fs, first, nret);
01266 }
01267
01268
01269 static int statement (LexState *ls) {
01270 int line = ls->linenumber;
01271 switch (ls->t.token) {
01272 case TK_IF: {
01273 ifstat(ls, line);
01274 return 0;
01275 }
01276 case TK_WHILE: {
01277 whilestat(ls, line);
01278 return 0;
01279 }
01280 case TK_DO: {
01281 luaX_next(ls);
01282 block(ls);
01283 check_match(ls, TK_END, TK_DO, line);
01284 return 0;
01285 }
01286 case TK_FOR: {
01287 forstat(ls, line);
01288 return 0;
01289 }
01290 case TK_REPEAT: {
01291 repeatstat(ls, line);
01292 return 0;
01293 }
01294 case TK_FUNCTION: {
01295 funcstat(ls, line);
01296 return 0;
01297 }
01298 case TK_LOCAL: {
01299 luaX_next(ls);
01300 if (testnext(ls, TK_FUNCTION))
01301 localfunc(ls);
01302 else
01303 localstat(ls);
01304 return 0;
01305 }
01306 case TK_RETURN: {
01307 retstat(ls);
01308 return 1;
01309 }
01310 case TK_BREAK: {
01311 luaX_next(ls);
01312 breakstat(ls);
01313 return 1;
01314 }
01315 default: {
01316 exprstat(ls);
01317 return 0;
01318 }
01319 }
01320 }
01321
01322
01323 static void chunk (LexState *ls) {
01324
01325 int islast = 0;
01326 enterlevel(ls);
01327 while (!islast && !block_follow(ls->t.token)) {
01328 islast = statement(ls);
01329 testnext(ls, ';');
01330 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
01331 ls->fs->freereg >= ls->fs->nactvar);
01332 ls->fs->freereg = ls->fs->nactvar;
01333 }
01334 leavelevel(ls);
01335 }
01336
01337