00001
00002
00003
00004
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
00038
00039 typedef struct BlockCnt {
00040
00041 struct BlockCnt *previous;
00042 int breaklist;
00043 int nactvar;
00044 int upval;
00045 int isbreakable;
00046 } BlockCnt;
00047
00048
00049
00050
00051
00052
00053 static void chunk (LexState *ls)
00054 ;
00055 static void expr (LexState *ls, expdesc *v)
00056 ;
00057
00058
00059
00060 static void next (LexState *ls)
00061
00062 {
00063 ls->lastline = ls->linenumber;
00064 if (ls->lookahead.token != TK_EOS) {
00065 ls->t = ls->lookahead;
00066 ls->lookahead.token = TK_EOS;
00067 }
00068 else
00069 ls->t.token = luaX_lex(ls, &ls->t.seminfo);
00070 }
00071
00072
00073 static void lookahead (LexState *ls)
00074
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
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
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
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
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
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
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
00149 {
00150 init_exp(e, VK, luaK_stringK(ls->fs, s));
00151 }
00152
00153
00154 static void checkname(LexState *ls, expdesc *e)
00155
00156 {
00157 codestring(ls, e, str_checkname(ls));
00158 }
00159
00160
00161 static int luaI_registerlocalvar (LexState *ls, TString *varname)
00162
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
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
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
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
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
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
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
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;
00247 }
00248
00249
00250 static void markupval (FuncState *fs, int level)
00251
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
00261 {
00262 if (fs == NULL)
00263 init_exp(var, VGLOBAL, NO_REG);
00264 else {
00265 int v = searchvar(fs, n);
00266 if (v >= 0) {
00267 init_exp(var, VLOCAL, v);
00268 if (!base)
00269 markupval(fs, v);
00270 }
00271 else {
00272 singlevaraux(fs->prev, n, var, 0);
00273 if (var->k == VGLOBAL) {
00274 if (base)
00275 var->info = luaK_stringK(fs, n);
00276 }
00277 else {
00278 var->info = indexupvalue(fs, n, var);
00279 var->k = VUPVAL;
00280 }
00281 }
00282 }
00283 }
00284
00285
00286 static TString *singlevar (LexState *ls, expdesc *var, int base)
00287
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
00297 {
00298 FuncState *fs = ls->fs;
00299 int extra = nvars - nexps;
00300 if (e->k == VCALL) {
00301 extra++;
00302 if (extra <= 0) extra = 0;
00303 else luaK_reserveregs(fs, extra-1);
00304 luaK_setcallreturns(fs, e, extra);
00305 }
00306 else {
00307 if (e->k != VVOID) luaK_exp2nextreg(fs, e);
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
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);
00328 }
00329
00330
00331 static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable)
00332
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
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;
00354 luaK_patchtohere(fs, bl->breaklist);
00355 }
00356
00357
00358 static void pushclosure (LexState *ls, FuncState *func, expdesc *v)
00359
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
00367 f->p[fs->np++] = func->f;
00368
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
00379 {
00380 Proto *f = luaF_newproto(ls->L);
00381 fs->f = f;
00382 fs->prev = ls->fs;
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;
00398 }
00399
00400
00401 static void close_func (LexState *ls)
00402
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);
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);
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
00448
00449
00450
00451 static void luaY_field (LexState *ls, expdesc *v)
00452
00453 {
00454
00455 FuncState *fs = ls->fs;
00456 expdesc key;
00457 luaK_exp2anyreg(fs, v);
00458 next(ls);
00459 checkname(ls, &key);
00460 luaK_indexed(fs, v, &key);
00461 }
00462
00463
00464 static void luaY_index (LexState *ls, expdesc *v)
00465
00466 {
00467
00468 next(ls);
00469 expr(ls, v);
00470 luaK_exp2val(ls->fs, v);
00471 check(ls, ']');
00472 }
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 struct ConsControl {
00483 expdesc v;
00484 expdesc *t;
00485 int nh;
00486 int na;
00487 int tostore;
00488 };
00489
00490
00491 static void recfield (LexState *ls, struct ConsControl *cc)
00492
00493 {
00494
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
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;
00511 }
00512
00513
00514 static void closelistfield (FuncState *fs, struct ConsControl *cc)
00515
00516 {
00517 if (cc->v.k == VVOID) return;
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);
00522 cc->tostore = 0;
00523 fs->freereg = cc->t->info + 1;
00524 }
00525 }
00526
00527
00528 static void lastlistfield (FuncState *fs, struct ConsControl *cc)
00529
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;
00542 }
00543
00544
00545 static void listfield (LexState *ls, struct ConsControl *cc)
00546
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
00557 {
00558
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);
00567 luaK_exp2nextreg(ls->fs, t);
00568 check(ls, '{');
00569 do {
00570 lua_assert(cc.v.k == VVOID || cc.tostore > 0);
00571 testnext(ls, ';');
00572 if (ls->t.token == '}') break;
00573 closelistfield(fs, &cc);
00574 switch(ls->t.token) {
00575 case TK_NAME: {
00576 lookahead(ls);
00577 if (ls->lookahead.token != '=')
00578 listfield(ls, &cc);
00579 else
00580 recfield(ls, &cc);
00581 break;
00582 }
00583 case '[': {
00584 recfield(ls, &cc);
00585 break;
00586 }
00587 default: {
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));
00596 SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1);
00597 }
00598
00599
00600
00601
00602
00603 static void parlist (LexState *ls)
00604
00605 {
00606
00607 int nparams = 0;
00608 int dots = 0;
00609 if (ls->t.token != ')') {
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
00624 {
00625
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
00643 {
00644
00645 int n = 1;
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
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 '(': {
00665 if (line != ls->lastline)
00666 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
00667 next(ls);
00668 if (ls->t.token == ')')
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 '{': {
00678 constructor(ls, &args);
00679 break;
00680 }
00681 case TK_STRING: {
00682 codestring(ls, &args, ls->t.seminfo.ts);
00683 next(ls);
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;
00693 if (args.k == VCALL)
00694 nparams = LUA_MULTRET;
00695 else {
00696 if (args.k != VVOID)
00697 luaK_exp2nextreg(fs, &args);
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;
00703
00704 }
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716 static void prefixexp (LexState *ls, expdesc *v)
00717
00718 {
00719
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 '%': {
00735 TString *varname;
00736 int line = ls->linenumber;
00737 next(ls);
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
00755 {
00756
00757
00758 FuncState *fs = ls->fs;
00759 prefixexp(ls, v);
00760 for (;;) {
00761 switch (ls->t.token) {
00762 case '.': {
00763 luaY_field(ls, v);
00764 break;
00765 }
00766 case '[': {
00767 expdesc key;
00768 luaK_exp2anyreg(fs, v);
00769 luaY_index(ls, &key);
00770 luaK_indexed(fs, v, &key);
00771 break;
00772 }
00773 case ':': {
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 '{': {
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
00794 {
00795
00796
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);
00801 break;
00802 }
00803 case TK_STRING: {
00804 codestring(ls, v, ls->t.seminfo.ts);
00805 next(ls);
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 '{': {
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
00875 static const struct {
00876 lu_byte left;
00877 lu_byte right;
00878 } priority[] = {
00879 {6, 6}, {6, 6}, {7, 7}, {7, 7},
00880 {10, 9}, {5, 4},
00881 {3, 3}, {3, 3},
00882 {3, 3}, {3, 3}, {3, 3}, {3, 3},
00883 {2, 2}, {1, 1}
00884 };
00885
00886 #define UNARY_PRIORITY 8
00887
00888
00889
00890
00891
00892
00893 static BinOpr subexpr (LexState *ls, expdesc *v, int limit)
00894
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
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
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;
00920 }
00921
00922
00923 static void expr (LexState *ls, expdesc *v)
00924
00925 {
00926 subexpr(ls, v, -1);
00927 }
00928
00929
00930
00931
00932
00933
00934
00935
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
00954 {
00955
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
00967
00968
00969 struct LHS_assign {
00970 struct LHS_assign *prev;
00971 expdesc v;
00972 };
00973
00974
00975
00976
00977
00978
00979
00980
00981 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v)
00982
00983 {
00984 FuncState *fs = ls->fs;
00985 int extra = fs->freereg;
00986 int conflict = 0;
00987 for (; lh; lh = lh->prev) {
00988 if (lh->v.k == VINDEXED) {
00989 if (lh->v.info == v->info) {
00990 conflict = 1;
00991 lh->v.info = extra;
00992 }
00993 if (lh->v.aux == v->info) {
00994 conflict = 1;
00995 lh->v.aux = extra;
00996 }
00997 }
00998 }
00999 if (conflict) {
01000 luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0);
01001 luaK_reserveregs(fs, 1);
01002 }
01003 }
01004
01005
01006 static void assignment (LexState *ls, struct LHS_assign *lh, int nvars)
01007
01008 {
01009 expdesc e;
01010 check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
01011 "syntax error");
01012 if (testnext(ls, ',')) {
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 {
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;
01028 }
01029 else {
01030 luaK_setcallreturns(ls->fs, &e, 1);
01031 luaK_storevar(ls->fs, &lh->v, &e);
01032 return;
01033 }
01034 }
01035 init_exp(&e, VNONRELOC, ls->fs->freereg-1);
01036 luaK_storevar(ls->fs, &lh->v, &e);
01037 }
01038
01039
01040 static void cond (LexState *ls, expdesc *v)
01041
01042 {
01043
01044 expr(ls, v);
01045 if (v->k == VNIL) v->k = VFALSE;
01046 luaK_goiftrue(ls->fs, v);
01047 luaK_patchtohere(ls->fs, v->t);
01048 }
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059 #ifndef MAXEXPWHILE
01060 #define MAXEXPWHILE 100
01061 #endif
01062
01063
01064
01065
01066
01067 #define EXTRAEXP 5
01068
01069 static void whilestat (LexState *ls, int line)
01070
01071 {
01072
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);
01082 whileinit = luaK_jump(fs);
01083 expinit = luaK_getlabel(fs);
01084 expr(ls, &v);
01085 if (v.k == VK) v.k = VTRUE;
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;
01091 if (sizeexp > MAXEXPWHILE)
01092 luaX_syntaxerror(ls, "`while' condition too complex");
01093 for (i = 0; i < sizeexp; i++)
01094 codeexp[i] = fs->f->code[expinit + i];
01095 fs->pc = expinit;
01096 enterblock(fs, &bl, 1);
01097 check(ls, TK_DO);
01098 blockinit = luaK_getlabel(fs);
01099 block(ls);
01100 luaK_patchtohere(fs, whileinit);
01101
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);
01109 luaK_patchtohere(fs, v.f);
01110 }
01111
01112
01113 static void repeatstat (LexState *ls, int line)
01114
01115 {
01116
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
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
01145 {
01146 BlockCnt bl;
01147 FuncState *fs = ls->fs;
01148 int prep, endfor;
01149 adjustlocalvars(ls, nvars);
01150 check(ls, TK_DO);
01151 enterblock(fs, &bl, 1);
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);
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
01165 {
01166
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);
01174 check(ls, ',');
01175 exp1(ls);
01176 if (testnext(ls, ','))
01177 exp1(ls);
01178 else {
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
01190 {
01191
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);
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
01213 {
01214
01215 FuncState *fs = ls->fs;
01216 TString *varname;
01217 BlockCnt bl;
01218 enterblock(fs, &bl, 0);
01219 next(ls);
01220 varname = str_checkname(ls);
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
01233 {
01234
01235 next(ls);
01236 cond(ls, v);
01237 check(ls, TK_THEN);
01238 block(ls);
01239 }
01240
01241
01242 static void ifstat (LexState *ls, int line)
01243
01244 {
01245
01246 FuncState *fs = ls->fs;
01247 expdesc v;
01248 int escapelist = NO_JUMP;
01249 test_then_block(ls, &v);
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);
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);
01259 block(ls);
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
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
01280 getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
01281 }
01282
01283
01284 static void localstat (LexState *ls)
01285
01286 {
01287
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
01307 {
01308
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
01323 {
01324
01325 int needself;
01326 expdesc v, b;
01327 next(ls);
01328 needself = funcname(ls, &v);
01329 body(ls, &b, needself, line);
01330 luaK_storevar(ls->fs, &v, &b);
01331 luaK_fixline(ls->fs, line);
01332 }
01333
01334
01335 static void exprstat (LexState *ls)
01336
01337 {
01338
01339 FuncState *fs = ls->fs;
01340 struct LHS_assign v;
01341 primaryexp(ls, &v.v);
01342 if (v.v.k == VCALL) {
01343 luaK_setcallreturns(fs, &v.v, 0);
01344 }
01345 else {
01346 v.prev = NULL;
01347 assignment(ls, &v, 1);
01348 }
01349 }
01350
01351
01352 static void retstat (LexState *ls)
01353
01354 {
01355
01356 FuncState *fs = ls->fs;
01357 expdesc e;
01358 int first, nret;
01359 next(ls);
01360 if (block_follow(ls->t.token) || ls->t.token == ';')
01361 first = nret = 0;
01362 else {
01363 nret = explist1(ls, &e);
01364 if (e.k == VCALL) {
01365 luaK_setcallreturns(fs, &e, LUA_MULTRET);
01366 if (nret == 1) {
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;
01372 }
01373 else {
01374 if (nret == 1)
01375 first = luaK_exp2anyreg(fs, &e);
01376 else {
01377 luaK_exp2nextreg(fs, &e);
01378 first = fs->nactvar;
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
01389 {
01390
01391 FuncState *fs = ls->fs;
01392 BlockCnt *bl = fs->bl;
01393 int upval = 0;
01394 next(ls);
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
01409 {
01410 int line = ls->linenumber;
01411 switch (ls->t.token) {
01412 case TK_IF: {
01413 ifstat(ls, line);
01414 return 0;
01415 }
01416 case TK_WHILE: {
01417 whilestat(ls, line);
01418 return 0;
01419 }
01420 case TK_DO: {
01421 next(ls);
01422 block(ls);
01423 check_match(ls, TK_END, TK_DO, line);
01424 return 0;
01425 }
01426 case TK_FOR: {
01427 forstat(ls, line);
01428 return 0;
01429 }
01430 case TK_REPEAT: {
01431 repeatstat(ls, line);
01432 return 0;
01433 }
01434 case TK_FUNCTION: {
01435 funcstat(ls, line);
01436 return 0;
01437 }
01438 case TK_LOCAL: {
01439 next(ls);
01440 if (testnext(ls, TK_FUNCTION))
01441 localfunc(ls);
01442 else
01443 localstat(ls);
01444 return 0;
01445 }
01446 case TK_RETURN: {
01447 retstat(ls);
01448 return 1;
01449 }
01450 case TK_BREAK: {
01451 breakstat(ls);
01452 return 1;
01453 }
01454 default: {
01455 exprstat(ls);
01456 return 0;
01457 }
01458 }
01459 }
01460
01461
01462 static void chunk (LexState *ls)
01463
01464 {
01465
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;
01473 }
01474 leavelevel(ls);
01475 }
01476
01477