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 @@
11 /*
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 $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -41,8 +41,8 @@ · 35 unmodified lines
4141 */
4242 typedef struct BlockCnt {
4343 struct BlockCnt *previous; /* chain */
44− int firstlabel; /* index of first label in this block */
45− int firstgoto; /* index of first pending goto in this block */
44+ short firstlabel; /* index of first label in this block */
45+ short firstgoto; /* index of first pending goto in this block */
4646 lu_byte nactvar; /* # active locals outside the block */
4747 lu_byte upval; /* true if some variable in the block is an upvalue */
4848 lu_byte isloop; /* true if `block' is a loop */
@@ -81,13 +81,14 @@ static void error_expected (LexState *ls, int token) { · 32 unmodified lines
8181
8282
8383 static void errorlimit (FuncState *fs, int limit, const char *what) {
84+ lua_State *L = fs->ls->L;
8485 const char *msg;
8586 int line = fs->f->linedefined;
8687 const char *where = (line == 0)
8788 ? "main function"
88− : luaO_pushfstring(fs->L, "function at line %d", line);
89− msg = luaO_pushfstring(fs->L, "too many %s (limit is %d) in %s",
90− what, limit, where);
89+ : luaO_pushfstring(L, "function at line %d", line);
90+ msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
91+ what, limit, where);
9192 luaX_syntaxerror(fs->ls, msg);
9293 }
9394
@@ -182,7 +183,7 @@ static void new_localvar (LexState *ls, TString *name) { · 88 unmodified lines
182183 MAXVARS, "local variables");
183184 luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
184185 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);
186187 }
187188
188189
@@ -231,13 +232,13 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { · 42 unmodified lines
231232 Proto *f = fs->f;
232233 int oldsize = f->sizeupvalues;
233234 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,
235236 Upvaldesc, MAXUPVAL, "upvalues");
236237 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;
237238 f->upvalues[fs->nups].instack = (v->k == VLOCAL);
238239 f->upvalues[fs->nups].idx = cast_byte(v->u.info);
239240 f->upvalues[fs->nups].name = name;
240− luaC_objbarrier(fs->L, f, name);
241+ luaC_objbarrier(fs->ls->L, f, name);
241242 return fs->nups++;
242243 }
243244
@@ -327,13 +328,13 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { · 83 unmodified lines
327328
328329
329330 static void enterlevel (LexState *ls) {
330− global_State *g = G(ls->L);
331− ++g->nCcalls;
332− checklimit(ls->fs, g->nCcalls, LUAI_MAXCCALLS, "syntax levels");
331+ lua_State *L = ls->L;
332+ ++L->nCcalls;
333+ checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "syntax levels");
333334 }
334335
335336
336−#define leavelevel(ls) (G((ls)->L)->nCcalls--)
337+#define leavelevel(ls) ((ls)->L->nCcalls--)
337338
338339
339340 static void closegoto (LexState *ls, int g, Labeldesc *label) {
@@ -343,9 +344,10 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { · 3 unmodified lines
343344 Labeldesc *gt = &gl->arr[g];
344345 lua_assert(eqstr(gt->name, label->name));
345346 if (gt->nactvar < label->nactvar) {
347+ TString *vname = getlocvar(fs, gt->nactvar)->varname;
346348 const char *msg = luaO_pushfstring(ls->L,
347349 "<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));
349351 semerror(ls, msg);
350352 }
351353 luaK_patchlist(fs, gt->pc, label->pc);
@@ -382,7 +384,8 @@ static int findlabel (LexState *ls, int g) { · 30 unmodified lines
382384 static int newlabelentry (LexState *ls, Labellist *l, TString *name,
383385 int line, int pc) {
384386 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");
386389 l->arr[n].name = name;
387390 l->arr[n].line = line;
388391 l->arr[n].nactvar = ls->fs->nactvar;
@@ -418,7 +421,7 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) { · 29 unmodified lines
418421 int i = bl->firstgoto;
419422 Labellist *gl = &fs->ls->dyd->gt;
420423 /* correct pending gotos to current block and try to close it
421− with visible labels */
424+ with visible labels */
422425 while (i < gl->n) {
423426 Labeldesc *gt = &gl->arr[i];
424427 if (gt->nactvar > bl->nactvar) {
@@ -514,7 +517,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { · 89 unmodified lines
514517 Proto *f;
515518 fs->prev = ls->fs; /* linked list of funcstates */
516519 fs->ls = ls;
517− fs->L = L;
518520 ls->fs = fs;
519521 fs->pc = 0;
520522 fs->lasttarget = 0;
@@ -1039,7 +1041,7 @@ static const struct { · 518 unmodified lines
10391041 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
10401042 ** where `binop' is any binary operator with a priority higher than `limit'
10411043 */
1042−static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
1044+static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
10431045 BinOpr op;
10441046 UnOpr uop;
10451047 enterlevel(ls);
@@ -1105,31 +1107,34 @@ struct LHS_assign { · 59 unmodified lines
11051107
11061108
11071109 /*
1108−** check whether, in an assignment to a local variable, the local variable
1109−** is needed in a previous assignment (to a table). If so, save original
1110−** local value in a safe place and use this safe copy in the previous
1111−** assignment.
1110+** check whether, in an assignment to an upvalue/local variable, the
1111+** upvalue/local variable is begin used in a previous assignment to a
1112+** table. If so, save original upvalue/local value in a safe place and
1113+** use this safe copy in the previous assignment.
11121114 */
11131115 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
11141116 FuncState *fs = ls->fs;
11151117 int extra = fs->freereg; /* eventual position to save local variable */
11161118 int conflict = 0;
1117− for (; lh; lh = lh->prev) {
1118− /* conflict in table 't'? */
1119− if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
1120− conflict = 1;
1121− lh->v.u.ind.vt = VLOCAL;
1122− lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
1123− }
1124− /* conflict in index 'idx'? */
1125− if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
1126− conflict = 1;
1127− lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
1119+ for (; lh; lh = lh->prev) { /* check all previous assignments */
1120+ if (lh->v.k == VINDEXED) { /* assigning to a table? */
1121+ /* table is the upvalue/local being assigned now? */
1122+ if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
1123+ conflict = 1;
1124+ lh->v.u.ind.vt = VLOCAL;
1125+ lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
1126+ }
1127+ /* index is the local being assigned? (index cannot be upvalue) */
1128+ if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
1129+ conflict = 1;
1130+ lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
1131+ }
11281132 }
11291133 }
11301134 if (conflict) {
1135+ /* copy upvalue/local value to a temporary (in position 'extra') */
11311136 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);
11331138 luaK_reserveregs(fs, 1);
11341139 }
11351140 }
@@ -1144,7 +1149,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { · 8 unmodified lines
11441149 primaryexp(ls, &nv.v);
11451150 if (nv.v.k != VINDEXED)
11461151 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,
11481153 "variable names");
11491154 assignment(ls, &nv, nvars+1);
11501155 }
@@ -1178,10 +1183,18 @@ static int cond (LexState *ls) { · 27 unmodified lines
11781183 }
11791184
11801185
1181−static void gotostat (LexState *ls, TString *label, int line) {
1182− /* create new entry for this goto */
1183− int g = newlabelentry(ls, &ls->dyd->gt, label, line, luaK_jump(ls->fs));
1184− findlabel(ls, g);
1186+static void gotostat (LexState *ls, int pc) {
1187+ int line = ls->linenumber;
1188+ TString *label;
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 */
11851198 }
11861199
11871200
@@ -1190,7 +1203,7 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { · 2 unmodified lines
11901203 int i;
11911204 for (i = fs->bl->firstlabel; i < ll->n; i++) {
11921205 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,
11941207 "label " LUA_QS " already defined on line %d",
11951208 getstr(label), ll->arr[i].line);
11961209 semerror(fs->ls, msg);
@@ -1360,38 +1373,51 @@ static void forstat (LexState *ls, int line) { · 163 unmodified lines
13601373 }
13611374
13621375
1363−static int test_then_block (LexState *ls) {
1376+static void test_then_block (LexState *ls, int *escapelist) {
13641377 /* 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) */
13661382 luaX_next(ls); /* skip IF or ELSEIF */
1367− condexit = cond(ls);
1383+ expr(ls, &v); /* read condition */
13681384 checknext(ls, TK_THEN);
1369− block(ls); /* `then' part */
1370− return condexit;
1385+ if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) {
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);
13711407 }
13721408
13731409
13741410 static void ifstat (LexState *ls, int line) {
13751411 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
13761412 FuncState *fs = ls->fs;
1377− int flist;
1378− int escapelist = NO_JUMP;
1379− flist = test_then_block(ls); /* IF cond THEN block */
1380− while (ls->t.token == TK_ELSEIF) {
1381− luaK_concat(fs, &escapelist, luaK_jump(fs));
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) */
1413+ int escapelist = NO_JUMP; /* exit list for finished parts */
1414+ test_then_block(ls, &escapelist); /* IF cond THEN block */
1415+ while (ls->t.token == TK_ELSEIF)
1416+ test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
1417+ if (testnext(ls, TK_ELSE))
13891418 block(ls); /* `else' part */
1390− }
1391− else
1392− luaK_concat(fs, &escapelist, flist);
1393− luaK_patchtohere(fs, escapelist);
13941419 check_match(ls, TK_END, TK_IF, line);
1420+ luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
13951421 }
13961422
13971423
@@ -1551,15 +1577,9 @@ static void statement (LexState *ls) { · 153 unmodified lines
15511577 retstat(ls);
15521578 break;
15531579 }
1554− 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− }
1580+ case TK_BREAK: /* stat -> breakstat */
15601581 case TK_GOTO: { /* stat -> 'goto' NAME */
1561− luaX_next(ls); /* skip GOTO */
1562− gotostat(ls, str_checkname(ls), line);
1582+ gotostat(ls, luaK_jump(ls->fs));
15631583 break;
15641584 }
15651585 default: { /* stat -> func | assignment */
@@ -1595,8 +1615,8 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, · 29 unmodified lines
15951615 close_func(&lexstate);
15961616 L->top--; /* pop name */
15971617 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1598− return funcstate.f;
15991618 /* all scopes should be correctly finished */
16001619 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
1620+ return funcstate.f;
16011621 }
16021622

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