History

Lua 5.3.0-work2

Lua Team committed 12 years ago

Commit ad386d91 parent b83a40a60 files · +3280 −2672Browse files

Showing the changes to src/lparser.c (1 of 60 files). Show all

Unified · Split

src/lparser.c+37−37
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.c,v 2.133 2013/04/26 13:07:53 roberto Exp $
2+** $Id: lparser.c,v 2.138 2013/12/30 20:47:58 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -35,6 +35,10 @@ · 29 unmodified lines
3535 #define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
3636
3737
38+/* because all strings are unified by the scanner, the parser
39+ can use pointer equality for string equality */
40+#define eqstr(a,b) ((a) == (b))
41+
3842
3943 /*
4044 ** nodes for block list (list of active blocks)
@@ -57,16 +61,6 @@ static void statement (LexState *ls); · 16 unmodified lines
5761 static void expr (LexState *ls, expdesc *v);
5862
5963
60−static void anchor_token (LexState *ls) {
61− /* last token from outer function must be EOS */
62− lua_assert(ls->fs != NULL || ls->t.token == TK_EOS);
63− if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
64− TString *ts = ls->t.seminfo.ts;
65− luaX_newstring(ls, getstr(ts), ts->tsv.len);
66− }
67−}
68−
69−
7064 /* semantic error */
7165 static l_noret semerror (LexState *ls, const char *msg) {
7266 ls->t.token = 0; /* remove 'near to' from final message */
@@ -222,7 +216,7 @@ static int searchupvalue (FuncState *fs, TString *name) { · 149 unmodified lines
222216 int i;
223217 Upvaldesc *up = fs->f->upvalues;
224218 for (i = 0; i < fs->nups; i++) {
225− if (luaS_eqstr(up[i].name, name)) return i;
219+ if (eqstr(up[i].name, name)) return i;
226220 }
227221 return -1; /* not found */
228222 }
@@ -246,7 +240,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { · 17 unmodified lines
246240 static int searchvar (FuncState *fs, TString *n) {
247241 int i;
248242 for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
249− if (luaS_eqstr(n, getlocvar(fs, i)->varname))
243+ if (eqstr(n, getlocvar(fs, i)->varname))
250244 return i;
251245 }
252246 return -1; /* not found */
@@ -342,7 +336,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { · 89 unmodified lines
342336 FuncState *fs = ls->fs;
343337 Labellist *gl = &ls->dyd->gt;
344338 Labeldesc *gt = &gl->arr[g];
345− lua_assert(luaS_eqstr(gt->name, label->name));
339+ lua_assert(eqstr(gt->name, label->name));
346340 if (gt->nactvar < label->nactvar) {
347341 TString *vname = getlocvar(fs, gt->nactvar)->varname;
348342 const char *msg = luaO_pushfstring(ls->L,
@@ -369,7 +363,7 @@ static int findlabel (LexState *ls, int g) { · 20 unmodified lines
369363 /* check labels in current block for a match */
370364 for (i = bl->firstlabel; i < dyd->label.n; i++) {
371365 Labeldesc *lb = &dyd->label.arr[i];
372− if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */
366+ if (eqstr(lb->name, gt->name)) { /* correct label? */
373367 if (gt->nactvar > lb->nactvar &&
374368 (bl->upval || dyd->label.n > bl->firstlabel))
375369 luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
@@ -403,7 +397,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) { · 27 unmodified lines
403397 Labellist *gl = &ls->dyd->gt;
404398 int i = ls->fs->bl->firstgoto;
405399 while (i < gl->n) {
406− if (luaS_eqstr(gl->arr[i].name, lb->name))
400+ if (eqstr(gl->arr[i].name, lb->name))
407401 closegoto(ls, i, lb);
408402 else
409403 i++;
@@ -525,7 +519,6 @@ static void codeclosure (LexState *ls, expdesc *v) { · 115 unmodified lines
525519
526520
527521 static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
528− lua_State *L = ls->L;
529522 Proto *f;
530523 fs->prev = ls->fs; /* linked list of funcstates */
531524 fs->ls = ls;
@@ -544,10 +537,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { · 12 unmodified lines
544537 f = fs->f;
545538 f->source = ls->source;
546539 f->maxstacksize = 2; /* registers 0/1 are always valid */
547− fs->h = luaH_new(L);
548− /* anchor table of constants (to avoid being collected) */
549− sethvalue2s(L, L->top, fs->h);
550− incr_top(L);
551540 enterblock(fs, bl, 0);
552541 }
553542
@@ -572,9 +561,6 @@ static void close_func (LexState *ls) { · 18 unmodified lines
572561 f->sizeupvalues = fs->nups;
573562 lua_assert(fs->bl == NULL);
574563 ls->fs = fs->prev;
575− /* last token read was anchored in defunct function; must re-anchor it */
576− anchor_token(ls);
577− L->top--; /* pop table of constants */
578564 luaC_checkGC(L);
579565 }
580566
@@ -993,6 +979,7 @@ static UnOpr getunopr (int op) { · 412 unmodified lines
993979 switch (op) {
994980 case TK_NOT: return OPR_NOT;
995981 case '-': return OPR_MINUS;
982+ case '~': return OPR_BNOT;
996983 case '#': return OPR_LEN;
997984 default: return OPR_NOUNOPR;
998985 }
@@ -1004,10 +991,15 @@ static BinOpr getbinopr (int op) { · 5 unmodified lines
1004991 case '+': return OPR_ADD;
1005992 case '-': return OPR_SUB;
1006993 case '*': return OPR_MUL;
1007− case '/': return OPR_DIV;
1008− case TK_IDIV: return OPR_IDIV;
1009994 case '%': return OPR_MOD;
1010995 case '^': return OPR_POW;
996+ case '/': return OPR_DIV;
997+ case TK_IDIV: return OPR_IDIV;
998+ case '&': return OPR_BAND;
999+ case '|': return OPR_BOR;
1000+ case '~': return OPR_BXOR;
1001+ case TK_SHL: return OPR_SHL;
1002+ case TK_SHR: return OPR_SHR;
10111003 case TK_CONCAT: return OPR_CONCAT;
10121004 case TK_NE: return OPR_NE;
10131005 case TK_EQ: return OPR_EQ;
@@ -1026,15 +1018,19 @@ static const struct { · 12 unmodified lines
10261018 lu_byte left; /* left priority for each binary operator */
10271019 lu_byte right; /* right priority */
10281020 } priority[] = { /* ORDER OPR */
1029− {6, 6}, {6, 6}, /* '+' '-' */
1030− {7, 7}, {7, 7}, {7, 7}, {7, 7}, /* '*' '/' '//' '%' */
1031− {10, 9}, {5, 4}, /* ^, .. (right associative) */
1032− {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
1033− {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
1034− {2, 2}, {1, 1} /* and, or */
1021+ {10, 10}, {10, 10}, /* '+' '-' */
1022+ {11, 11}, {11, 11}, /* '*' '%' */
1023+ {14, 13}, /* '^' (right associative) */
1024+ {11, 11}, {11, 11}, /* '/' '//' */
1025+ {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */
1026+ {7, 7}, {7, 7}, /* '<<' '>>' */
1027+ {9, 8}, /* '..' (right associative) */
1028+ {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
1029+ {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
1030+ {2, 2}, {1, 1} /* and, or */
10351031 };
10361032
1037−#define UNARY_PRIORITY 8 /* priority for unary operators */
1033+#define UNARY_PRIORITY 12 /* priority for unary operators */
10381034
10391035
10401036 /*
@@ -1202,7 +1198,7 @@ static void gotostat (LexState *ls, int pc) { · 161 unmodified lines
12021198 static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
12031199 int i;
12041200 for (i = fs->bl->firstlabel; i < ll->n; i++) {
1205− if (luaS_eqstr(label, ll->arr[i].name)) {
1201+ if (eqstr(label, ll->arr[i].name)) {
12061202 const char *msg = luaO_pushfstring(fs->ls->L,
12071203 "label " LUA_QS " already defined on line %d",
12081204 getstr(label), ll->arr[i].line);
@@ -1627,11 +1623,14 @@ Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, · 418 unmodified lines
16271623 LexState lexstate;
16281624 FuncState funcstate;
16291625 Closure *cl = luaF_newLclosure(L, 1); /* create main closure */
1630− /* anchor closure (to avoid being collected) */
1631− setclLvalue(L, L->top, cl);
1626+ setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
1627+ incr_top(L);
1628+ lexstate.h = luaH_new(L); /* create table for scanner */
1629+ sethvalue(L, L->top, lexstate.h); /* anchor it */
16321630 incr_top(L);
16331631 funcstate.f = cl->l.p = luaF_newproto(L);
16341632 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
1633+ luaC_objbarrier(L, funcstate.f, funcstate.f->source);
16351634 lexstate.buff = buff;
16361635 lexstate.dyd = dyd;
16371636 dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
@@ -1640,6 +1639,7 @@ Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, · 2 unmodified lines
16401639 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
16411640 /* all scopes should be correctly finished */
16421641 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
1643− return cl; /* it's on the stack too */
1642+ L->top--; /* remove scanner's table */
1643+ return cl; /* closure is on the stack, too */
16441644 }
16451645