History

Lua 5.2.0-rc1

Lua Team committed 14 years ago

Commit d74d0e51 parent 8203bab51 files · +1861 −1136Browse files

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

Unified · Split

src/lparser.c+90−70
@@ -1,5 +1,5 @@
1/*1/*
2** $Id: lparser.c,v 2.113 2011/07/02 15:58:14 roberto Exp $2** $Id: lparser.c,v 2.122 2011/10/31 17:46:04 roberto Exp $
3** Lua Parser3** Lua Parser
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -41,8 +41,8 @@ · 35 unmodified lines
41*/41*/
42typedef struct BlockCnt {42typedef struct BlockCnt {
43 struct BlockCnt *previous; /* chain */43 struct BlockCnt *previous; /* chain */
44 int firstlabel; /* index of first label in this block */44 short firstlabel; /* index of first label in this block */
45 int firstgoto; /* index of first pending goto in this block */45 short firstgoto; /* index of first pending goto in this block */
46 lu_byte nactvar; /* # active locals outside the block */46 lu_byte nactvar; /* # active locals outside the block */
47 lu_byte upval; /* true if some variable in the block is an upvalue */47 lu_byte upval; /* true if some variable in the block is an upvalue */
48 lu_byte isloop; /* true if `block' is a loop */48 lu_byte isloop; /* true if `block' is a loop */
@@ -81,13 +81,14 @@ static void error_expected (LexState *ls, int token) { · 32 unmodified lines
81 81
82 82
83static void errorlimit (FuncState *fs, int limit, const char *what) {83static void errorlimit (FuncState *fs, int limit, const char *what) {
84 lua_State *L = fs->ls->L;
84 const char *msg;85 const char *msg;
85 int line = fs->f->linedefined;86 int line = fs->f->linedefined;
86 const char *where = (line == 0)87 const char *where = (line == 0)
87 ? "main function"88 ? "main function"
88 : luaO_pushfstring(fs->L, "function at line %d", line);89 : luaO_pushfstring(L, "function at line %d", line);
89 msg = luaO_pushfstring(fs->L, "too many %s (limit is %d) in %s",90 msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
90 what, limit, where);91 what, limit, where);
91 luaX_syntaxerror(fs->ls, msg);92 luaX_syntaxerror(fs->ls, msg);
92}93}
93 94
@@ -182,7 +183,7 @@ static void new_localvar (LexState *ls, TString *name) { · 88 unmodified lines
182 MAXVARS, "local variables");183 MAXVARS, "local variables");
183 luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,184 luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
184 dyd->actvar.size, Vardesc, MAX_INT, "local variables");185 dyd->actvar.size, Vardesc, MAX_INT, "local variables");
185 dyd->actvar.arr[dyd->actvar.n++].idx = cast(unsigned short, reg);186 dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg);
186}187}
187 188
188 189
@@ -231,13 +232,13 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { · 42 unmodified lines
231 Proto *f = fs->f;232 Proto *f = fs->f;
232 int oldsize = f->sizeupvalues;233 int oldsize = f->sizeupvalues;
233 checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");234 checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
234 luaM_growvector(fs->L, f->upvalues, fs->nups, f->sizeupvalues,235 luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
235 Upvaldesc, MAXUPVAL, "upvalues");236 Upvaldesc, MAXUPVAL, "upvalues");
236 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;237 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;
237 f->upvalues[fs->nups].instack = (v->k == VLOCAL);238 f->upvalues[fs->nups].instack = (v->k == VLOCAL);
238 f->upvalues[fs->nups].idx = cast_byte(v->u.info);239 f->upvalues[fs->nups].idx = cast_byte(v->u.info);
239 f->upvalues[fs->nups].name = name;240 f->upvalues[fs->nups].name = name;
240 luaC_objbarrier(fs->L, f, name);241 luaC_objbarrier(fs->ls->L, f, name);
241 return fs->nups++;242 return fs->nups++;
242}243}
243 244
@@ -327,13 +328,13 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { · 83 unmodified lines
327 328
328 329
329static void enterlevel (LexState *ls) {330static void enterlevel (LexState *ls) {
330 global_State *g = G(ls->L);331 lua_State *L = ls->L;
331 ++g->nCcalls;332 ++L->nCcalls;
332 checklimit(ls->fs, g->nCcalls, LUAI_MAXCCALLS, "syntax levels");333 checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "syntax levels");
333}334}
334 335
335 336
336#define leavelevel(ls) (G((ls)->L)->nCcalls--)337#define leavelevel(ls) ((ls)->L->nCcalls--)
337 338
338 339
339static void closegoto (LexState *ls, int g, Labeldesc *label) {340static void closegoto (LexState *ls, int g, Labeldesc *label) {
@@ -343,9 +344,10 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { · 3 unmodified lines
343 Labeldesc *gt = &gl->arr[g];344 Labeldesc *gt = &gl->arr[g];
344 lua_assert(eqstr(gt->name, label->name));345 lua_assert(eqstr(gt->name, label->name));
345 if (gt->nactvar < label->nactvar) {346 if (gt->nactvar < label->nactvar) {
347 TString *vname = getlocvar(fs, gt->nactvar)->varname;
346 const char *msg = luaO_pushfstring(ls->L,348 const char *msg = luaO_pushfstring(ls->L,
347 "<goto %s> at line %d jumps into the scope of local " LUA_QS,349 "<goto %s> at line %d jumps into the scope of local " LUA_QS,
348 getstr(gt->name), gt->line, getstr(getlocvar(fs, gt->nactvar)->varname));350 getstr(gt->name), gt->line, getstr(vname));
349 semerror(ls, msg);351 semerror(ls, msg);
350 }352 }
351 luaK_patchlist(fs, gt->pc, label->pc);353 luaK_patchlist(fs, gt->pc, label->pc);
@@ -382,7 +384,8 @@ static int findlabel (LexState *ls, int g) { · 30 unmodified lines
382static int newlabelentry (LexState *ls, Labellist *l, TString *name,384static int newlabelentry (LexState *ls, Labellist *l, TString *name,
383 int line, int pc) {385 int line, int pc) {
384 int n = l->n;386 int n = l->n;
385 luaM_growvector(ls->L, l->arr, n, l->size, Labeldesc, MAX_INT, "labels");387 luaM_growvector(ls->L, l->arr, n, l->size,
388 Labeldesc, SHRT_MAX, "labels/gotos");
386 l->arr[n].name = name;389 l->arr[n].name = name;
387 l->arr[n].line = line;390 l->arr[n].line = line;
388 l->arr[n].nactvar = ls->fs->nactvar;391 l->arr[n].nactvar = ls->fs->nactvar;
@@ -418,7 +421,7 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) { · 29 unmodified lines
418 int i = bl->firstgoto;421 int i = bl->firstgoto;
419 Labellist *gl = &fs->ls->dyd->gt;422 Labellist *gl = &fs->ls->dyd->gt;
420 /* correct pending gotos to current block and try to close it423 /* correct pending gotos to current block and try to close it
421 with visible labels */ 424 with visible labels */
422 while (i < gl->n) {425 while (i < gl->n) {
423 Labeldesc *gt = &gl->arr[i];426 Labeldesc *gt = &gl->arr[i];
424 if (gt->nactvar > bl->nactvar) {427 if (gt->nactvar > bl->nactvar) {
@@ -514,7 +517,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { · 89 unmodified lines
514 Proto *f;517 Proto *f;
515 fs->prev = ls->fs; /* linked list of funcstates */518 fs->prev = ls->fs; /* linked list of funcstates */
516 fs->ls = ls;519 fs->ls = ls;
517 fs->L = L;
518 ls->fs = fs;520 ls->fs = fs;
519 fs->pc = 0;521 fs->pc = 0;
520 fs->lasttarget = 0;522 fs->lasttarget = 0;
@@ -1039,7 +1041,7 @@ static const struct { · 518 unmodified lines
1039** subexpr -> (simpleexp | unop subexpr) { binop subexpr }1041** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
1040** where `binop' is any binary operator with a priority higher than `limit'1042** where `binop' is any binary operator with a priority higher than `limit'
1041*/1043*/
1042static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {1044static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
1043 BinOpr op;1045 BinOpr op;
1044 UnOpr uop;1046 UnOpr uop;
1045 enterlevel(ls);1047 enterlevel(ls);
@@ -1105,31 +1107,34 @@ struct LHS_assign { · 59 unmodified lines
1105 1107
1106 1108
1107/*1109/*
1108** check whether, in an assignment to a local variable, the local variable1110** check whether, in an assignment to an upvalue/local variable, the
1109** is needed in a previous assignment (to a table). If so, save original1111** upvalue/local variable is begin used in a previous assignment to a
1110** local value in a safe place and use this safe copy in the previous1112** table. If so, save original upvalue/local value in a safe place and
1111** assignment.1113** use this safe copy in the previous assignment.
1112*/1114*/
1113static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {1115static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
1114 FuncState *fs = ls->fs;1116 FuncState *fs = ls->fs;
1115 int extra = fs->freereg; /* eventual position to save local variable */1117 int extra = fs->freereg; /* eventual position to save local variable */
1116 int conflict = 0;1118 int conflict = 0;
1117 for (; lh; lh = lh->prev) {1119 for (; lh; lh = lh->prev) { /* check all previous assignments */
1118 /* conflict in table 't'? */1120 if (lh->v.k == VINDEXED) { /* assigning to a table? */
1119 if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {1121 /* table is the upvalue/local being assigned now? */
1120 conflict = 1;1122 if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
1121 lh->v.u.ind.vt = VLOCAL;1123 conflict = 1;
1122 lh->v.u.ind.t = extra; /* previous assignment will use safe copy */1124 lh->v.u.ind.vt = VLOCAL;
1123 }1125 lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
1124 /* conflict in index 'idx'? */1126 }
1125 if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {1127 /* index is the local being assigned? (index cannot be upvalue) */
1126 conflict = 1;1128 if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
1127 lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */1129 conflict = 1;
1130 lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
1131 }
1128 }1132 }
1129 }1133 }
1130 if (conflict) {1134 if (conflict) {
1135 /* copy upvalue/local value to a temporary (in position 'extra') */
1131 OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;1136 OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
1132 luaK_codeABC(fs, op, fs->freereg, v->u.info, 0); /* make copy */1137 luaK_codeABC(fs, op, extra, v->u.info, 0);
1133 luaK_reserveregs(fs, 1);1138 luaK_reserveregs(fs, 1);
1134 }1139 }
1135}1140}
@@ -1144,7 +1149,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { · 8 unmodified lines
1144 primaryexp(ls, &nv.v);1149 primaryexp(ls, &nv.v);
1145 if (nv.v.k != VINDEXED)1150 if (nv.v.k != VINDEXED)
1146 check_conflict(ls, lh, &nv.v);1151 check_conflict(ls, lh, &nv.v);
1147 checklimit(ls->fs, nvars, LUAI_MAXCCALLS - G(ls->L)->nCcalls,1152 checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
1148 "variable names");1153 "variable names");
1149 assignment(ls, &nv, nvars+1);1154 assignment(ls, &nv, nvars+1);
1150 }1155 }
@@ -1178,10 +1183,18 @@ static int cond (LexState *ls) { · 27 unmodified lines
1178}1183}
1179 1184
1180 1185
1181static void gotostat (LexState *ls, TString *label, int line) {1186static void gotostat (LexState *ls, int pc) {
1182 /* create new entry for this goto */1187 int line = ls->linenumber;
1183 int g = newlabelentry(ls, &ls->dyd->gt, label, line, luaK_jump(ls->fs));1188 TString *label;
1184 findlabel(ls, g);1189 int g;
1190 if (testnext(ls, TK_GOTO))
1191 label = str_checkname(ls);
1192 else {
1193 luaX_next(ls); /* skip break */
1194 label = luaS_new(ls->L, "break");
1195 }
1196 g = newlabelentry(ls, &ls->dyd->gt, label, line, pc);
1197 findlabel(ls, g); /* close it if label already defined */
1185}1198}
1186 1199
1187 1200
@@ -1190,7 +1203,7 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { · 2 unmodified lines
1190 int i;1203 int i;
1191 for (i = fs->bl->firstlabel; i < ll->n; i++) {1204 for (i = fs->bl->firstlabel; i < ll->n; i++) {
1192 if (eqstr(label, ll->arr[i].name)) {1205 if (eqstr(label, ll->arr[i].name)) {
1193 const char *msg = luaO_pushfstring(fs->ls->L, 1206 const char *msg = luaO_pushfstring(fs->ls->L,
1194 "label " LUA_QS " already defined on line %d",1207 "label " LUA_QS " already defined on line %d",
1195 getstr(label), ll->arr[i].line);1208 getstr(label), ll->arr[i].line);
1196 semerror(fs->ls, msg);1209 semerror(fs->ls, msg);
@@ -1360,38 +1373,51 @@ static void forstat (LexState *ls, int line) { · 163 unmodified lines
1360}1373}
1361 1374
1362 1375
1363static int test_then_block (LexState *ls) {1376static void test_then_block (LexState *ls, int *escapelist) {
1364 /* test_then_block -> [IF | ELSEIF] cond THEN block */1377 /* test_then_block -> [IF | ELSEIF] cond THEN block */
1365 int condexit;1378 BlockCnt bl;
1379 FuncState *fs = ls->fs;
1380 expdesc v;
1381 int jf; /* instruction to skip 'then' code (if condition is false) */
1366 luaX_next(ls); /* skip IF or ELSEIF */1382 luaX_next(ls); /* skip IF or ELSEIF */
1367 condexit = cond(ls);1383 expr(ls, &v); /* read condition */
1368 checknext(ls, TK_THEN);1384 checknext(ls, TK_THEN);
1369 block(ls); /* `then' part */1385 if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) {
1370 return condexit;1386 luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */
1387 enterblock(fs, &bl, 0); /* must enter block before 'goto' */
1388 gotostat(ls, v.t); /* handle goto/break */
1389 if (block_follow(ls, 0)) { /* 'goto' is the entire block? */
1390 leaveblock(fs);
1391 return; /* and that is it */
1392 }
1393 else /* must skip over 'then' part if condition is false */
1394 jf = luaK_jump(fs);
1395 }
1396 else { /* regular case (not goto/break) */
1397 luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */
1398 enterblock(fs, &bl, 0);
1399 jf = v.f;
1400 }
1401 statlist(ls); /* `then' part */
1402 leaveblock(fs);
1403 if (ls->t.token == TK_ELSE ||
1404 ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
1405 luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */
1406 luaK_patchtohere(fs, jf);
1371}1407}
1372 1408
1373 1409
1374static void ifstat (LexState *ls, int line) {1410static void ifstat (LexState *ls, int line) {
1375 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */1411 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
1376 FuncState *fs = ls->fs;1412 FuncState *fs = ls->fs;
1377 int flist;1413 int escapelist = NO_JUMP; /* exit list for finished parts */
1378 int escapelist = NO_JUMP;1414 test_then_block(ls, &escapelist); /* IF cond THEN block */
1379 flist = test_then_block(ls); /* IF cond THEN block */1415 while (ls->t.token == TK_ELSEIF)
1380 while (ls->t.token == TK_ELSEIF) {1416 test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
1381 luaK_concat(fs, &escapelist, luaK_jump(fs));1417 if (testnext(ls, TK_ELSE))
1382 luaK_patchtohere(fs, flist);
1383 flist = test_then_block(ls); /* ELSEIF cond THEN block */
1384 }
1385 if (ls->t.token == TK_ELSE) {
1386 luaK_concat(fs, &escapelist, luaK_jump(fs));
1387 luaK_patchtohere(fs, flist);
1388 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
1389 block(ls); /* `else' part */1418 block(ls); /* `else' part */
1390 }
1391 else
1392 luaK_concat(fs, &escapelist, flist);
1393 luaK_patchtohere(fs, escapelist);
1394 check_match(ls, TK_END, TK_IF, line);1419 check_match(ls, TK_END, TK_IF, line);
1420 luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
1395}1421}
1396 1422
1397 1423
@@ -1551,15 +1577,9 @@ static void statement (LexState *ls) { · 153 unmodified lines
1551 retstat(ls);1577 retstat(ls);
1552 break;1578 break;
1553 }1579 }
1554 case TK_BREAK: { /* stat -> breakstat */1580 case TK_BREAK: /* stat -> breakstat */
1555 luaX_next(ls); /* skip BREAK */
1556 /* code it as "goto 'break'" */
1557 gotostat(ls, luaS_new(ls->L, "break"), line);
1558 break;
1559 }
1560 case TK_GOTO: { /* stat -> 'goto' NAME */1581 case TK_GOTO: { /* stat -> 'goto' NAME */
1561 luaX_next(ls); /* skip GOTO */1582 gotostat(ls, luaK_jump(ls->fs));
1562 gotostat(ls, str_checkname(ls), line);
1563 break;1583 break;
1564 }1584 }
1565 default: { /* stat -> func | assignment */1585 default: { /* stat -> func | assignment */
@@ -1595,8 +1615,8 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, · 29 unmodified lines
1595 close_func(&lexstate);1615 close_func(&lexstate);
1596 L->top--; /* pop name */1616 L->top--; /* pop name */
1597 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);1617 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1598 return funcstate.f;
1599 /* all scopes should be correctly finished */1618 /* all scopes should be correctly finished */
1600 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);1619 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
1620 return funcstate.f;
1601}1621}
1602 1622

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t