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 @@
1/*1/*
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 $
3** Lua Parser3** Lua Parser
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -35,6 +35,10 @@ · 29 unmodified lines
35#define hasmultret(k) ((k) == VCALL || (k) == VVARARG)35#define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
36 36
37 37
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
38 42
39/*43/*
40** nodes for block list (list of active blocks)44** nodes for block list (list of active blocks)
@@ -57,16 +61,6 @@ static void statement (LexState *ls); · 16 unmodified lines
57static void expr (LexState *ls, expdesc *v);61static void expr (LexState *ls, expdesc *v);
58 62
59 63
60static 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
70/* semantic error */64/* semantic error */
71static l_noret semerror (LexState *ls, const char *msg) {65static l_noret semerror (LexState *ls, const char *msg) {
72 ls->t.token = 0; /* remove 'near to' from final message */66 ls->t.token = 0; /* remove 'near to' from final message */
@@ -222,7 +216,7 @@ static int searchupvalue (FuncState *fs, TString *name) { · 149 unmodified lines
222 int i;216 int i;
223 Upvaldesc *up = fs->f->upvalues;217 Upvaldesc *up = fs->f->upvalues;
224 for (i = 0; i < fs->nups; i++) {218 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;
226 }220 }
227 return -1; /* not found */221 return -1; /* not found */
228}222}
@@ -246,7 +240,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { · 17 unmodified lines
246static int searchvar (FuncState *fs, TString *n) {240static int searchvar (FuncState *fs, TString *n) {
247 int i;241 int i;
248 for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {242 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))
250 return i;244 return i;
251 }245 }
252 return -1; /* not found */246 return -1; /* not found */
@@ -342,7 +336,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { · 89 unmodified lines
342 FuncState *fs = ls->fs;336 FuncState *fs = ls->fs;
343 Labellist *gl = &ls->dyd->gt;337 Labellist *gl = &ls->dyd->gt;
344 Labeldesc *gt = &gl->arr[g];338 Labeldesc *gt = &gl->arr[g];
345 lua_assert(luaS_eqstr(gt->name, label->name));339 lua_assert(eqstr(gt->name, label->name));
346 if (gt->nactvar < label->nactvar) {340 if (gt->nactvar < label->nactvar) {
347 TString *vname = getlocvar(fs, gt->nactvar)->varname;341 TString *vname = getlocvar(fs, gt->nactvar)->varname;
348 const char *msg = luaO_pushfstring(ls->L,342 const char *msg = luaO_pushfstring(ls->L,
@@ -369,7 +363,7 @@ static int findlabel (LexState *ls, int g) { · 20 unmodified lines
369 /* check labels in current block for a match */363 /* check labels in current block for a match */
370 for (i = bl->firstlabel; i < dyd->label.n; i++) {364 for (i = bl->firstlabel; i < dyd->label.n; i++) {
371 Labeldesc *lb = &dyd->label.arr[i];365 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? */
373 if (gt->nactvar > lb->nactvar &&367 if (gt->nactvar > lb->nactvar &&
374 (bl->upval || dyd->label.n > bl->firstlabel))368 (bl->upval || dyd->label.n > bl->firstlabel))
375 luaK_patchclose(ls->fs, gt->pc, lb->nactvar);369 luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
@@ -403,7 +397,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) { · 27 unmodified lines
403 Labellist *gl = &ls->dyd->gt;397 Labellist *gl = &ls->dyd->gt;
404 int i = ls->fs->bl->firstgoto;398 int i = ls->fs->bl->firstgoto;
405 while (i < gl->n) {399 while (i < gl->n) {
406 if (luaS_eqstr(gl->arr[i].name, lb->name))400 if (eqstr(gl->arr[i].name, lb->name))
407 closegoto(ls, i, lb);401 closegoto(ls, i, lb);
408 else402 else
409 i++;403 i++;
@@ -525,7 +519,6 @@ static void codeclosure (LexState *ls, expdesc *v) { · 115 unmodified lines
525 519
526 520
527static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {521static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
528 lua_State *L = ls->L;
529 Proto *f;522 Proto *f;
530 fs->prev = ls->fs; /* linked list of funcstates */523 fs->prev = ls->fs; /* linked list of funcstates */
531 fs->ls = ls;524 fs->ls = ls;
@@ -544,10 +537,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { · 12 unmodified lines
544 f = fs->f;537 f = fs->f;
545 f->source = ls->source;538 f->source = ls->source;
546 f->maxstacksize = 2; /* registers 0/1 are always valid */539 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);
551 enterblock(fs, bl, 0);540 enterblock(fs, bl, 0);
552}541}
553 542
@@ -572,9 +561,6 @@ static void close_func (LexState *ls) { · 18 unmodified lines
572 f->sizeupvalues = fs->nups;561 f->sizeupvalues = fs->nups;
573 lua_assert(fs->bl == NULL);562 lua_assert(fs->bl == NULL);
574 ls->fs = fs->prev;563 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 */
578 luaC_checkGC(L);564 luaC_checkGC(L);
579}565}
580 566
@@ -993,6 +979,7 @@ static UnOpr getunopr (int op) { · 412 unmodified lines
993 switch (op) {979 switch (op) {
994 case TK_NOT: return OPR_NOT;980 case TK_NOT: return OPR_NOT;
995 case '-': return OPR_MINUS;981 case '-': return OPR_MINUS;
982 case '~': return OPR_BNOT;
996 case '#': return OPR_LEN;983 case '#': return OPR_LEN;
997 default: return OPR_NOUNOPR;984 default: return OPR_NOUNOPR;
998 }985 }
@@ -1004,10 +991,15 @@ static BinOpr getbinopr (int op) { · 5 unmodified lines
1004 case '+': return OPR_ADD;991 case '+': return OPR_ADD;
1005 case '-': return OPR_SUB;992 case '-': return OPR_SUB;
1006 case '*': return OPR_MUL;993 case '*': return OPR_MUL;
1007 case '/': return OPR_DIV;
1008 case TK_IDIV: return OPR_IDIV;
1009 case '%': return OPR_MOD;994 case '%': return OPR_MOD;
1010 case '^': return OPR_POW;995 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;
1011 case TK_CONCAT: return OPR_CONCAT;1003 case TK_CONCAT: return OPR_CONCAT;
1012 case TK_NE: return OPR_NE;1004 case TK_NE: return OPR_NE;
1013 case TK_EQ: return OPR_EQ;1005 case TK_EQ: return OPR_EQ;
@@ -1026,15 +1018,19 @@ static const struct { · 12 unmodified lines
1026 lu_byte left; /* left priority for each binary operator */1018 lu_byte left; /* left priority for each binary operator */
1027 lu_byte right; /* right priority */1019 lu_byte right; /* right priority */
1028} priority[] = { /* ORDER OPR */1020} priority[] = { /* ORDER OPR */
1029 {6, 6}, {6, 6}, /* '+' '-' */1021 {10, 10}, {10, 10}, /* '+' '-' */
1030 {7, 7}, {7, 7}, {7, 7}, {7, 7}, /* '*' '/' '//' '%' */1022 {11, 11}, {11, 11}, /* '*' '%' */
1031 {10, 9}, {5, 4}, /* ^, .. (right associative) */1023 {14, 13}, /* '^' (right associative) */
1032 {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */1024 {11, 11}, {11, 11}, /* '/' '//' */
1033 {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */1025 {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */
1034 {2, 2}, {1, 1} /* and, or */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 */
1035};1031};
1036 1032
1037#define UNARY_PRIORITY 8 /* priority for unary operators */1033#define UNARY_PRIORITY 12 /* priority for unary operators */
1038 1034
1039 1035
1040/*1036/*
@@ -1202,7 +1198,7 @@ static void gotostat (LexState *ls, int pc) { · 161 unmodified lines
1202static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {1198static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
1203 int i;1199 int i;
1204 for (i = fs->bl->firstlabel; i < ll->n; i++) {1200 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)) {
1206 const char *msg = luaO_pushfstring(fs->ls->L,1202 const char *msg = luaO_pushfstring(fs->ls->L,
1207 "label " LUA_QS " already defined on line %d",1203 "label " LUA_QS " already defined on line %d",
1208 getstr(label), ll->arr[i].line);1204 getstr(label), ll->arr[i].line);
@@ -1627,11 +1623,14 @@ Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, · 418 unmodified lines
1627 LexState lexstate;1623 LexState lexstate;
1628 FuncState funcstate;1624 FuncState funcstate;
1629 Closure *cl = luaF_newLclosure(L, 1); /* create main closure */1625 Closure *cl = luaF_newLclosure(L, 1); /* create main closure */
1630 /* anchor closure (to avoid being collected) */1626 setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
1631 setclLvalue(L, L->top, cl);1627 incr_top(L);
1628 lexstate.h = luaH_new(L); /* create table for scanner */
1629 sethvalue(L, L->top, lexstate.h); /* anchor it */
1632 incr_top(L);1630 incr_top(L);
1633 funcstate.f = cl->l.p = luaF_newproto(L);1631 funcstate.f = cl->l.p = luaF_newproto(L);
1634 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */1632 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
1633 luaC_objbarrier(L, funcstate.f, funcstate.f->source);
1635 lexstate.buff = buff;1634 lexstate.buff = buff;
1636 lexstate.dyd = dyd;1635 lexstate.dyd = dyd;
1637 dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;1636 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
1640 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);1639 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1641 /* all scopes should be correctly finished */1640 /* all scopes should be correctly finished */
1642 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);1641 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 */
1644}1644}
1645 1645