diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
commit | bfd0179042b0b02fb88748d54e56e7e208bb117f (patch) | |
tree | 42d5fd51574054caaf673420fca1ec962d62d2f2 /apps/plugins/lua/lparser.c | |
parent | 36378988ad4059982742f05f5eb50580b456840a (diff) | |
download | rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.tar.gz rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.zip |
Revert "Update lua plugin to 5.2.3"
FILE typedef to *void needs more work to not break sim and
application builds. I checked only a few random native builds
unfortunately. Sorry for inconvenience.
Diffstat (limited to 'apps/plugins/lua/lparser.c')
-rw-r--r-- | apps/plugins/lua/lparser.c | 1015 |
1 files changed, 358 insertions, 657 deletions
diff --git a/apps/plugins/lua/lparser.c b/apps/plugins/lua/lparser.c index 9e1a9ca2cf..800cdb1a3b 100644 --- a/apps/plugins/lua/lparser.c +++ b/apps/plugins/lua/lparser.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 2.130.1.1 2013/04/12 18:48:47 roberto Exp $ | 2 | ** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -27,13 +27,11 @@ | |||
27 | 27 | ||
28 | 28 | ||
29 | 29 | ||
30 | /* maximum number of local variables per function (must be smaller | ||
31 | than 250, due to the bytecode format) */ | ||
32 | #define MAXVARS 200 | ||
33 | |||
34 | |||
35 | #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) | 30 | #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) |
36 | 31 | ||
32 | #define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) | ||
33 | |||
34 | #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) | ||
37 | 35 | ||
38 | 36 | ||
39 | /* | 37 | /* |
@@ -41,11 +39,10 @@ | |||
41 | */ | 39 | */ |
42 | typedef struct BlockCnt { | 40 | typedef struct BlockCnt { |
43 | struct BlockCnt *previous; /* chain */ | 41 | struct BlockCnt *previous; /* chain */ |
44 | short firstlabel; /* index of first label in this block */ | 42 | int breaklist; /* list of jumps out of this loop */ |
45 | short firstgoto; /* index of first pending goto in this block */ | 43 | lu_byte nactvar; /* # active locals outside the breakable structure */ |
46 | lu_byte nactvar; /* # active locals outside the block */ | ||
47 | lu_byte upval; /* true if some variable in the block is an upvalue */ | 44 | lu_byte upval; /* true if some variable in the block is an upvalue */ |
48 | lu_byte isloop; /* true if `block' is a loop */ | 45 | lu_byte isbreakable; /* true if `block' is a loop */ |
49 | } BlockCnt; | 46 | } BlockCnt; |
50 | 47 | ||
51 | 48 | ||
@@ -53,13 +50,11 @@ typedef struct BlockCnt { | |||
53 | /* | 50 | /* |
54 | ** prototypes for recursive non-terminal functions | 51 | ** prototypes for recursive non-terminal functions |
55 | */ | 52 | */ |
56 | static void statement (LexState *ls); | 53 | static void chunk (LexState *ls); |
57 | static void expr (LexState *ls, expdesc *v); | 54 | static void expr (LexState *ls, expdesc *v); |
58 | 55 | ||
59 | 56 | ||
60 | static void anchor_token (LexState *ls) { | 57 | 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) { | 58 | if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { |
64 | TString *ts = ls->t.seminfo.ts; | 59 | TString *ts = ls->t.seminfo.ts; |
65 | luaX_newstring(ls, getstr(ts), ts->tsv.len); | 60 | luaX_newstring(ls, getstr(ts), ts->tsv.len); |
@@ -67,34 +62,18 @@ static void anchor_token (LexState *ls) { | |||
67 | } | 62 | } |
68 | 63 | ||
69 | 64 | ||
70 | /* semantic error */ | 65 | static void error_expected (LexState *ls, int token) { |
71 | static l_noret semerror (LexState *ls, const char *msg) { | ||
72 | ls->t.token = 0; /* remove 'near to' from final message */ | ||
73 | luaX_syntaxerror(ls, msg); | ||
74 | } | ||
75 | |||
76 | |||
77 | static l_noret error_expected (LexState *ls, int token) { | ||
78 | luaX_syntaxerror(ls, | 66 | luaX_syntaxerror(ls, |
79 | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); | 67 | luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); |
80 | } | ||
81 | |||
82 | |||
83 | static l_noret errorlimit (FuncState *fs, int limit, const char *what) { | ||
84 | lua_State *L = fs->ls->L; | ||
85 | const char *msg; | ||
86 | int line = fs->f->linedefined; | ||
87 | const char *where = (line == 0) | ||
88 | ? "main function" | ||
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); | ||
92 | luaX_syntaxerror(fs->ls, msg); | ||
93 | } | 68 | } |
94 | 69 | ||
95 | 70 | ||
96 | static void checklimit (FuncState *fs, int v, int l, const char *what) { | 71 | static void errorlimit (FuncState *fs, int limit, const char *what) { |
97 | if (v > l) errorlimit(fs, l, what); | 72 | const char *msg = (fs->f->linedefined == 0) ? |
73 | luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : | ||
74 | luaO_pushfstring(fs->L, "function at line %d has more than %d %s", | ||
75 | fs->f->linedefined, limit, what); | ||
76 | luaX_lexerror(fs->ls, msg, 0); | ||
98 | } | 77 | } |
99 | 78 | ||
100 | 79 | ||
@@ -112,7 +91,6 @@ static void check (LexState *ls, int c) { | |||
112 | error_expected(ls, c); | 91 | error_expected(ls, c); |
113 | } | 92 | } |
114 | 93 | ||
115 | |||
116 | static void checknext (LexState *ls, int c) { | 94 | static void checknext (LexState *ls, int c) { |
117 | check(ls, c); | 95 | check(ls, c); |
118 | luaX_next(ls); | 96 | luaX_next(ls); |
@@ -129,7 +107,7 @@ static void check_match (LexState *ls, int what, int who, int where) { | |||
129 | error_expected(ls, what); | 107 | error_expected(ls, what); |
130 | else { | 108 | else { |
131 | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, | 109 | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, |
132 | "%s expected (to close %s at line %d)", | 110 | LUA_QS " expected (to close " LUA_QS " at line %d)", |
133 | luaX_token2str(ls, what), luaX_token2str(ls, who), where)); | 111 | luaX_token2str(ls, what), luaX_token2str(ls, who), where)); |
134 | } | 112 | } |
135 | } | 113 | } |
@@ -148,7 +126,7 @@ static TString *str_checkname (LexState *ls) { | |||
148 | static void init_exp (expdesc *e, expkind k, int i) { | 126 | static void init_exp (expdesc *e, expkind k, int i) { |
149 | e->f = e->t = NO_JUMP; | 127 | e->f = e->t = NO_JUMP; |
150 | e->k = k; | 128 | e->k = k; |
151 | e->u.info = i; | 129 | e->u.s.info = i; |
152 | } | 130 | } |
153 | 131 | ||
154 | 132 | ||
@@ -157,7 +135,7 @@ static void codestring (LexState *ls, expdesc *e, TString *s) { | |||
157 | } | 135 | } |
158 | 136 | ||
159 | 137 | ||
160 | static void checkname (LexState *ls, expdesc *e) { | 138 | static void checkname(LexState *ls, expdesc *e) { |
161 | codestring(ls, e, str_checkname(ls)); | 139 | codestring(ls, e, str_checkname(ls)); |
162 | } | 140 | } |
163 | 141 | ||
@@ -167,7 +145,7 @@ static int registerlocalvar (LexState *ls, TString *varname) { | |||
167 | Proto *f = fs->f; | 145 | Proto *f = fs->f; |
168 | int oldsize = f->sizelocvars; | 146 | int oldsize = f->sizelocvars; |
169 | luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, | 147 | luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, |
170 | LocVar, SHRT_MAX, "local variables"); | 148 | LocVar, SHRT_MAX, "too many local variables"); |
171 | while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; | 149 | while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; |
172 | f->locvars[fs->nlocvars].varname = varname; | 150 | f->locvars[fs->nlocvars].varname = varname; |
173 | luaC_objbarrier(ls->L, f, varname); | 151 | luaC_objbarrier(ls->L, f, varname); |
@@ -175,30 +153,14 @@ static int registerlocalvar (LexState *ls, TString *varname) { | |||
175 | } | 153 | } |
176 | 154 | ||
177 | 155 | ||
178 | static void new_localvar (LexState *ls, TString *name) { | 156 | #define new_localvarliteral(ls,v,n) \ |
179 | FuncState *fs = ls->fs; | 157 | new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) |
180 | Dyndata *dyd = ls->dyd; | ||
181 | int reg = registerlocalvar(ls, name); | ||
182 | checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, | ||
183 | MAXVARS, "local variables"); | ||
184 | luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, | ||
185 | dyd->actvar.size, Vardesc, MAX_INT, "local variables"); | ||
186 | dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); | ||
187 | } | ||
188 | |||
189 | 158 | ||
190 | static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { | ||
191 | new_localvar(ls, luaX_newstring(ls, name, sz)); | ||
192 | } | ||
193 | 159 | ||
194 | #define new_localvarliteral(ls,v) \ | 160 | static void new_localvar (LexState *ls, TString *name, int n) { |
195 | new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1) | 161 | FuncState *fs = ls->fs; |
196 | 162 | luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); | |
197 | 163 | fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); | |
198 | static LocVar *getlocvar (FuncState *fs, int i) { | ||
199 | int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; | ||
200 | lua_assert(idx < fs->nlocvars); | ||
201 | return &fs->f->locvars[idx]; | ||
202 | } | 164 | } |
203 | 165 | ||
204 | 166 | ||
@@ -206,88 +168,77 @@ static void adjustlocalvars (LexState *ls, int nvars) { | |||
206 | FuncState *fs = ls->fs; | 168 | FuncState *fs = ls->fs; |
207 | fs->nactvar = cast_byte(fs->nactvar + nvars); | 169 | fs->nactvar = cast_byte(fs->nactvar + nvars); |
208 | for (; nvars; nvars--) { | 170 | for (; nvars; nvars--) { |
209 | getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; | 171 | getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; |
210 | } | 172 | } |
211 | } | 173 | } |
212 | 174 | ||
213 | 175 | ||
214 | static void removevars (FuncState *fs, int tolevel) { | 176 | static void removevars (LexState *ls, int tolevel) { |
215 | fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); | 177 | FuncState *fs = ls->fs; |
216 | while (fs->nactvar > tolevel) | 178 | while (fs->nactvar > tolevel) |
217 | getlocvar(fs, --fs->nactvar)->endpc = fs->pc; | 179 | getlocvar(fs, --fs->nactvar).endpc = fs->pc; |
218 | } | 180 | } |
219 | 181 | ||
220 | 182 | ||
221 | static int searchupvalue (FuncState *fs, TString *name) { | 183 | static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { |
222 | int i; | 184 | int i; |
223 | Upvaldesc *up = fs->f->upvalues; | ||
224 | for (i = 0; i < fs->nups; i++) { | ||
225 | if (luaS_eqstr(up[i].name, name)) return i; | ||
226 | } | ||
227 | return -1; /* not found */ | ||
228 | } | ||
229 | |||
230 | |||
231 | static int newupvalue (FuncState *fs, TString *name, expdesc *v) { | ||
232 | Proto *f = fs->f; | 185 | Proto *f = fs->f; |
233 | int oldsize = f->sizeupvalues; | 186 | int oldsize = f->sizeupvalues; |
234 | checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); | 187 | for (i=0; i<f->nups; i++) { |
235 | luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, | 188 | if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { |
236 | Upvaldesc, MAXUPVAL, "upvalues"); | 189 | lua_assert(f->upvalues[i] == name); |
237 | while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; | 190 | return i; |
238 | f->upvalues[fs->nups].instack = (v->k == VLOCAL); | 191 | } |
239 | f->upvalues[fs->nups].idx = cast_byte(v->u.info); | 192 | } |
240 | f->upvalues[fs->nups].name = name; | 193 | /* new one */ |
241 | luaC_objbarrier(fs->ls->L, f, name); | 194 | luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); |
242 | return fs->nups++; | 195 | luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, |
196 | TString *, MAX_INT, ""); | ||
197 | while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; | ||
198 | f->upvalues[f->nups] = name; | ||
199 | luaC_objbarrier(fs->L, f, name); | ||
200 | lua_assert(v->k == VLOCAL || v->k == VUPVAL); | ||
201 | fs->upvalues[f->nups].k = cast_byte(v->k); | ||
202 | fs->upvalues[f->nups].info = cast_byte(v->u.s.info); | ||
203 | return f->nups++; | ||
243 | } | 204 | } |
244 | 205 | ||
245 | 206 | ||
246 | static int searchvar (FuncState *fs, TString *n) { | 207 | static int searchvar (FuncState *fs, TString *n) { |
247 | int i; | 208 | int i; |
248 | for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { | 209 | for (i=fs->nactvar-1; i >= 0; i--) { |
249 | if (luaS_eqstr(n, getlocvar(fs, i)->varname)) | 210 | if (n == getlocvar(fs, i).varname) |
250 | return i; | 211 | return i; |
251 | } | 212 | } |
252 | return -1; /* not found */ | 213 | return -1; /* not found */ |
253 | } | 214 | } |
254 | 215 | ||
255 | 216 | ||
256 | /* | ||
257 | Mark block where variable at given level was defined | ||
258 | (to emit close instructions later). | ||
259 | */ | ||
260 | static void markupval (FuncState *fs, int level) { | 217 | static void markupval (FuncState *fs, int level) { |
261 | BlockCnt *bl = fs->bl; | 218 | BlockCnt *bl = fs->bl; |
262 | while (bl->nactvar > level) bl = bl->previous; | 219 | while (bl && bl->nactvar > level) bl = bl->previous; |
263 | bl->upval = 1; | 220 | if (bl) bl->upval = 1; |
264 | } | 221 | } |
265 | 222 | ||
266 | 223 | ||
267 | /* | ||
268 | Find variable with given name 'n'. If it is an upvalue, add this | ||
269 | upvalue into all intermediate functions. | ||
270 | */ | ||
271 | static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { | 224 | static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { |
272 | if (fs == NULL) /* no more levels? */ | 225 | if (fs == NULL) { /* no more levels? */ |
273 | return VVOID; /* default is global */ | 226 | init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ |
227 | return VGLOBAL; | ||
228 | } | ||
274 | else { | 229 | else { |
275 | int v = searchvar(fs, n); /* look up locals at current level */ | 230 | int v = searchvar(fs, n); /* look up at current level */ |
276 | if (v >= 0) { /* found? */ | 231 | if (v >= 0) { |
277 | init_exp(var, VLOCAL, v); /* variable is local */ | 232 | init_exp(var, VLOCAL, v); |
278 | if (!base) | 233 | if (!base) |
279 | markupval(fs, v); /* local will be used as an upval */ | 234 | markupval(fs, v); /* local will be used as an upval */ |
280 | return VLOCAL; | 235 | return VLOCAL; |
281 | } | 236 | } |
282 | else { /* not found as local at current level; try upvalues */ | 237 | else { /* not found at current level; try upper one */ |
283 | int idx = searchupvalue(fs, n); /* try existing upvalues */ | 238 | if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) |
284 | if (idx < 0) { /* not found? */ | 239 | return VGLOBAL; |
285 | if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */ | 240 | var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ |
286 | return VVOID; /* not found; is a global */ | 241 | var->k = VUPVAL; /* upvalue in this level */ |
287 | /* else was LOCAL or UPVAL */ | ||
288 | idx = newupvalue(fs, n, var); /* will be a new upvalue */ | ||
289 | } | ||
290 | init_exp(var, VUPVAL, idx); | ||
291 | return VUPVAL; | 242 | return VUPVAL; |
292 | } | 243 | } |
293 | } | 244 | } |
@@ -297,13 +248,8 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { | |||
297 | static void singlevar (LexState *ls, expdesc *var) { | 248 | static void singlevar (LexState *ls, expdesc *var) { |
298 | TString *varname = str_checkname(ls); | 249 | TString *varname = str_checkname(ls); |
299 | FuncState *fs = ls->fs; | 250 | FuncState *fs = ls->fs; |
300 | if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */ | 251 | if (singlevaraux(fs, varname, var, 1) == VGLOBAL) |
301 | expdesc key; | 252 | var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ |
302 | singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ | ||
303 | lua_assert(var->k == VLOCAL || var->k == VUPVAL); | ||
304 | codestring(ls, &key, varname); /* key is variable name */ | ||
305 | luaK_indexed(fs, var, &key); /* env[varname] */ | ||
306 | } | ||
307 | } | 253 | } |
308 | 254 | ||
309 | 255 | ||
@@ -328,118 +274,18 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { | |||
328 | 274 | ||
329 | 275 | ||
330 | static void enterlevel (LexState *ls) { | 276 | static void enterlevel (LexState *ls) { |
331 | lua_State *L = ls->L; | 277 | if (++ls->L->nCcalls > LUAI_MAXCCALLS) |
332 | ++L->nCcalls; | 278 | luaX_lexerror(ls, "chunk has too many syntax levels", 0); |
333 | checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels"); | ||
334 | } | 279 | } |
335 | 280 | ||
336 | 281 | ||
337 | #define leavelevel(ls) ((ls)->L->nCcalls--) | 282 | #define leavelevel(ls) ((ls)->L->nCcalls--) |
338 | 283 | ||
339 | 284 | ||
340 | static void closegoto (LexState *ls, int g, Labeldesc *label) { | 285 | static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { |
341 | int i; | 286 | bl->breaklist = NO_JUMP; |
342 | FuncState *fs = ls->fs; | 287 | bl->isbreakable = isbreakable; |
343 | Labellist *gl = &ls->dyd->gt; | ||
344 | Labeldesc *gt = &gl->arr[g]; | ||
345 | lua_assert(luaS_eqstr(gt->name, label->name)); | ||
346 | if (gt->nactvar < label->nactvar) { | ||
347 | TString *vname = getlocvar(fs, gt->nactvar)->varname; | ||
348 | const char *msg = luaO_pushfstring(ls->L, | ||
349 | "<goto %s> at line %d jumps into the scope of local " LUA_QS, | ||
350 | getstr(gt->name), gt->line, getstr(vname)); | ||
351 | semerror(ls, msg); | ||
352 | } | ||
353 | luaK_patchlist(fs, gt->pc, label->pc); | ||
354 | /* remove goto from pending list */ | ||
355 | for (i = g; i < gl->n - 1; i++) | ||
356 | gl->arr[i] = gl->arr[i + 1]; | ||
357 | gl->n--; | ||
358 | } | ||
359 | |||
360 | |||
361 | /* | ||
362 | ** try to close a goto with existing labels; this solves backward jumps | ||
363 | */ | ||
364 | static int findlabel (LexState *ls, int g) { | ||
365 | int i; | ||
366 | BlockCnt *bl = ls->fs->bl; | ||
367 | Dyndata *dyd = ls->dyd; | ||
368 | Labeldesc *gt = &dyd->gt.arr[g]; | ||
369 | /* check labels in current block for a match */ | ||
370 | for (i = bl->firstlabel; i < dyd->label.n; i++) { | ||
371 | Labeldesc *lb = &dyd->label.arr[i]; | ||
372 | if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */ | ||
373 | if (gt->nactvar > lb->nactvar && | ||
374 | (bl->upval || dyd->label.n > bl->firstlabel)) | ||
375 | luaK_patchclose(ls->fs, gt->pc, lb->nactvar); | ||
376 | closegoto(ls, g, lb); /* close it */ | ||
377 | return 1; | ||
378 | } | ||
379 | } | ||
380 | return 0; /* label not found; cannot close goto */ | ||
381 | } | ||
382 | |||
383 | |||
384 | static int newlabelentry (LexState *ls, Labellist *l, TString *name, | ||
385 | int line, int pc) { | ||
386 | int n = l->n; | ||
387 | luaM_growvector(ls->L, l->arr, n, l->size, | ||
388 | Labeldesc, SHRT_MAX, "labels/gotos"); | ||
389 | l->arr[n].name = name; | ||
390 | l->arr[n].line = line; | ||
391 | l->arr[n].nactvar = ls->fs->nactvar; | ||
392 | l->arr[n].pc = pc; | ||
393 | l->n++; | ||
394 | return n; | ||
395 | } | ||
396 | |||
397 | |||
398 | /* | ||
399 | ** check whether new label 'lb' matches any pending gotos in current | ||
400 | ** block; solves forward jumps | ||
401 | */ | ||
402 | static void findgotos (LexState *ls, Labeldesc *lb) { | ||
403 | Labellist *gl = &ls->dyd->gt; | ||
404 | int i = ls->fs->bl->firstgoto; | ||
405 | while (i < gl->n) { | ||
406 | if (luaS_eqstr(gl->arr[i].name, lb->name)) | ||
407 | closegoto(ls, i, lb); | ||
408 | else | ||
409 | i++; | ||
410 | } | ||
411 | } | ||
412 | |||
413 | |||
414 | /* | ||
415 | ** "export" pending gotos to outer level, to check them against | ||
416 | ** outer labels; if the block being exited has upvalues, and | ||
417 | ** the goto exits the scope of any variable (which can be the | ||
418 | ** upvalue), close those variables being exited. | ||
419 | */ | ||
420 | static void movegotosout (FuncState *fs, BlockCnt *bl) { | ||
421 | int i = bl->firstgoto; | ||
422 | Labellist *gl = &fs->ls->dyd->gt; | ||
423 | /* correct pending gotos to current block and try to close it | ||
424 | with visible labels */ | ||
425 | while (i < gl->n) { | ||
426 | Labeldesc *gt = &gl->arr[i]; | ||
427 | if (gt->nactvar > bl->nactvar) { | ||
428 | if (bl->upval) | ||
429 | luaK_patchclose(fs, gt->pc, bl->nactvar); | ||
430 | gt->nactvar = bl->nactvar; | ||
431 | } | ||
432 | if (!findlabel(fs->ls, i)) | ||
433 | i++; /* move to next one */ | ||
434 | } | ||
435 | } | ||
436 | |||
437 | |||
438 | static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { | ||
439 | bl->isloop = isloop; | ||
440 | bl->nactvar = fs->nactvar; | 288 | bl->nactvar = fs->nactvar; |
441 | bl->firstlabel = fs->ls->dyd->label.n; | ||
442 | bl->firstgoto = fs->ls->dyd->gt.n; | ||
443 | bl->upval = 0; | 289 | bl->upval = 0; |
444 | bl->previous = fs->bl; | 290 | bl->previous = fs->bl; |
445 | fs->bl = bl; | 291 | fs->bl = bl; |
@@ -447,108 +293,63 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { | |||
447 | } | 293 | } |
448 | 294 | ||
449 | 295 | ||
450 | /* | ||
451 | ** create a label named "break" to resolve break statements | ||
452 | */ | ||
453 | static void breaklabel (LexState *ls) { | ||
454 | TString *n = luaS_new(ls->L, "break"); | ||
455 | int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc); | ||
456 | findgotos(ls, &ls->dyd->label.arr[l]); | ||
457 | } | ||
458 | |||
459 | /* | ||
460 | ** generates an error for an undefined 'goto'; choose appropriate | ||
461 | ** message when label name is a reserved word (which can only be 'break') | ||
462 | */ | ||
463 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { | ||
464 | const char *msg = isreserved(gt->name) | ||
465 | ? "<%s> at line %d not inside a loop" | ||
466 | : "no visible label " LUA_QS " for <goto> at line %d"; | ||
467 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); | ||
468 | semerror(ls, msg); | ||
469 | } | ||
470 | |||
471 | |||
472 | static void leaveblock (FuncState *fs) { | 296 | static void leaveblock (FuncState *fs) { |
473 | BlockCnt *bl = fs->bl; | 297 | BlockCnt *bl = fs->bl; |
474 | LexState *ls = fs->ls; | ||
475 | if (bl->previous && bl->upval) { | ||
476 | /* create a 'jump to here' to close upvalues */ | ||
477 | int j = luaK_jump(fs); | ||
478 | luaK_patchclose(fs, j, bl->nactvar); | ||
479 | luaK_patchtohere(fs, j); | ||
480 | } | ||
481 | if (bl->isloop) | ||
482 | breaklabel(ls); /* close pending breaks */ | ||
483 | fs->bl = bl->previous; | 298 | fs->bl = bl->previous; |
484 | removevars(fs, bl->nactvar); | 299 | removevars(fs->ls, bl->nactvar); |
300 | if (bl->upval) | ||
301 | luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); | ||
302 | /* a block either controls scope or breaks (never both) */ | ||
303 | lua_assert(!bl->isbreakable || !bl->upval); | ||
485 | lua_assert(bl->nactvar == fs->nactvar); | 304 | lua_assert(bl->nactvar == fs->nactvar); |
486 | fs->freereg = fs->nactvar; /* free registers */ | 305 | fs->freereg = fs->nactvar; /* free registers */ |
487 | ls->dyd->label.n = bl->firstlabel; /* remove local labels */ | 306 | luaK_patchtohere(fs, bl->breaklist); |
488 | if (bl->previous) /* inner block? */ | ||
489 | movegotosout(fs, bl); /* update pending gotos to outer block */ | ||
490 | else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ | ||
491 | undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ | ||
492 | } | 307 | } |
493 | 308 | ||
494 | 309 | ||
495 | /* | 310 | static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { |
496 | ** adds a new prototype into list of prototypes | ||
497 | */ | ||
498 | static Proto *addprototype (LexState *ls) { | ||
499 | Proto *clp; | ||
500 | lua_State *L = ls->L; | ||
501 | FuncState *fs = ls->fs; | 311 | FuncState *fs = ls->fs; |
502 | Proto *f = fs->f; /* prototype of current function */ | 312 | Proto *f = fs->f; |
503 | if (fs->np >= f->sizep) { | 313 | int oldsize = f->sizep; |
504 | int oldsize = f->sizep; | 314 | int i; |
505 | luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); | 315 | luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, |
506 | while (oldsize < f->sizep) f->p[oldsize++] = NULL; | 316 | MAXARG_Bx, "constant table overflow"); |
317 | while (oldsize < f->sizep) f->p[oldsize++] = NULL; | ||
318 | f->p[fs->np++] = func->f; | ||
319 | luaC_objbarrier(ls->L, f, func->f); | ||
320 | init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); | ||
321 | for (i=0; i<func->f->nups; i++) { | ||
322 | OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; | ||
323 | luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); | ||
507 | } | 324 | } |
508 | f->p[fs->np++] = clp = luaF_newproto(L); | ||
509 | luaC_objbarrier(L, f, clp); | ||
510 | return clp; | ||
511 | } | 325 | } |
512 | 326 | ||
513 | 327 | ||
514 | /* | 328 | static void lparser_open_func (LexState *ls, FuncState *fs) { |
515 | ** codes instruction to create new closure in parent function. | ||
516 | ** The OP_CLOSURE instruction must use the last available register, | ||
517 | ** so that, if it invokes the GC, the GC knows which registers | ||
518 | ** are in use at that time. | ||
519 | */ | ||
520 | static void codeclosure (LexState *ls, expdesc *v) { | ||
521 | FuncState *fs = ls->fs->prev; | ||
522 | init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); | ||
523 | luaK_exp2nextreg(fs, v); /* fix it at the last register */ | ||
524 | } | ||
525 | |||
526 | |||
527 | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { | ||
528 | lua_State *L = ls->L; | 329 | lua_State *L = ls->L; |
529 | Proto *f; | 330 | Proto *f = luaF_newproto(L); |
331 | fs->f = f; | ||
530 | fs->prev = ls->fs; /* linked list of funcstates */ | 332 | fs->prev = ls->fs; /* linked list of funcstates */ |
531 | fs->ls = ls; | 333 | fs->ls = ls; |
334 | fs->L = L; | ||
532 | ls->fs = fs; | 335 | ls->fs = fs; |
533 | fs->pc = 0; | 336 | fs->pc = 0; |
534 | fs->lasttarget = 0; | 337 | fs->lasttarget = -1; |
535 | fs->jpc = NO_JUMP; | 338 | fs->jpc = NO_JUMP; |
536 | fs->freereg = 0; | 339 | fs->freereg = 0; |
537 | fs->nk = 0; | 340 | fs->nk = 0; |
538 | fs->np = 0; | 341 | fs->np = 0; |
539 | fs->nups = 0; | ||
540 | fs->nlocvars = 0; | 342 | fs->nlocvars = 0; |
541 | fs->nactvar = 0; | 343 | fs->nactvar = 0; |
542 | fs->firstlocal = ls->dyd->actvar.n; | ||
543 | fs->bl = NULL; | 344 | fs->bl = NULL; |
544 | f = fs->f; | ||
545 | f->source = ls->source; | 345 | f->source = ls->source; |
546 | f->maxstacksize = 2; /* registers 0/1 are always valid */ | 346 | f->maxstacksize = 2; /* registers 0/1 are always valid */ |
547 | fs->h = luaH_new(L); | 347 | fs->h = luaH_new(L, 0, 0); |
548 | /* anchor table of constants (to avoid being collected) */ | 348 | /* anchor table of constants and prototype (to avoid being collected) */ |
549 | sethvalue2s(L, L->top, fs->h); | 349 | sethvalue2s(L, L->top, fs->h); |
550 | incr_top(L); | 350 | incr_top(L); |
551 | enterblock(fs, bl, 0); | 351 | setptvalue2s(L, L->top, f); |
352 | incr_top(L); | ||
552 | } | 353 | } |
553 | 354 | ||
554 | 355 | ||
@@ -556,8 +357,8 @@ static void close_func (LexState *ls) { | |||
556 | lua_State *L = ls->L; | 357 | lua_State *L = ls->L; |
557 | FuncState *fs = ls->fs; | 358 | FuncState *fs = ls->fs; |
558 | Proto *f = fs->f; | 359 | Proto *f = fs->f; |
360 | removevars(ls, 0); | ||
559 | luaK_ret(fs, 0, 0); /* final return */ | 361 | luaK_ret(fs, 0, 0); /* final return */ |
560 | leaveblock(fs); | ||
561 | luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); | 362 | luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); |
562 | f->sizecode = fs->pc; | 363 | f->sizecode = fs->pc; |
563 | luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); | 364 | luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); |
@@ -568,14 +369,32 @@ static void close_func (LexState *ls) { | |||
568 | f->sizep = fs->np; | 369 | f->sizep = fs->np; |
569 | luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); | 370 | luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); |
570 | f->sizelocvars = fs->nlocvars; | 371 | f->sizelocvars = fs->nlocvars; |
571 | luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); | 372 | luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); |
572 | f->sizeupvalues = fs->nups; | 373 | f->sizeupvalues = f->nups; |
374 | lua_assert(luaG_checkcode(f)); | ||
573 | lua_assert(fs->bl == NULL); | 375 | lua_assert(fs->bl == NULL); |
574 | ls->fs = fs->prev; | 376 | ls->fs = fs->prev; |
575 | /* last token read was anchored in defunct function; must re-anchor it */ | 377 | L->top -= 2; /* remove table and prototype from the stack */ |
576 | anchor_token(ls); | 378 | /* last token read was anchored in defunct function; must reanchor it */ |
577 | L->top--; /* pop table of constants */ | 379 | if (fs) anchor_token(ls); |
578 | luaC_checkGC(L); | 380 | } |
381 | |||
382 | |||
383 | Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { | ||
384 | struct LexState lexstate; | ||
385 | struct FuncState funcstate; | ||
386 | lexstate.buff = buff; | ||
387 | luaX_setinput(L, &lexstate, z, luaS_new(L, name)); | ||
388 | lparser_open_func(&lexstate, &funcstate); | ||
389 | funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ | ||
390 | luaX_next(&lexstate); /* read first token */ | ||
391 | chunk(&lexstate); | ||
392 | check(&lexstate, TK_EOS); | ||
393 | close_func(&lexstate); | ||
394 | lua_assert(funcstate.prev == NULL); | ||
395 | lua_assert(funcstate.f->nups == 0); | ||
396 | lua_assert(lexstate.fs == NULL); | ||
397 | return funcstate.f; | ||
579 | } | 398 | } |
580 | 399 | ||
581 | 400 | ||
@@ -585,39 +404,11 @@ static void close_func (LexState *ls) { | |||
585 | /*============================================================*/ | 404 | /*============================================================*/ |
586 | 405 | ||
587 | 406 | ||
588 | /* | 407 | static void field (LexState *ls, expdesc *v) { |
589 | ** check whether current token is in the follow set of a block. | 408 | /* field -> ['.' | ':'] NAME */ |
590 | ** 'until' closes syntactical blocks, but do not close scope, | ||
591 | ** so it handled in separate. | ||
592 | */ | ||
593 | static int block_follow (LexState *ls, int withuntil) { | ||
594 | switch (ls->t.token) { | ||
595 | case TK_ELSE: case TK_ELSEIF: | ||
596 | case TK_END: case TK_EOS: | ||
597 | return 1; | ||
598 | case TK_UNTIL: return withuntil; | ||
599 | default: return 0; | ||
600 | } | ||
601 | } | ||
602 | |||
603 | |||
604 | static void statlist (LexState *ls) { | ||
605 | /* statlist -> { stat [`;'] } */ | ||
606 | while (!block_follow(ls, 1)) { | ||
607 | if (ls->t.token == TK_RETURN) { | ||
608 | statement(ls); | ||
609 | return; /* 'return' must be last statement */ | ||
610 | } | ||
611 | statement(ls); | ||
612 | } | ||
613 | } | ||
614 | |||
615 | |||
616 | static void fieldsel (LexState *ls, expdesc *v) { | ||
617 | /* fieldsel -> ['.' | ':'] NAME */ | ||
618 | FuncState *fs = ls->fs; | 409 | FuncState *fs = ls->fs; |
619 | expdesc key; | 410 | expdesc key; |
620 | luaK_exp2anyregup(fs, v); | 411 | luaK_exp2anyreg(fs, v); |
621 | luaX_next(ls); /* skip the dot or colon */ | 412 | luaX_next(ls); /* skip the dot or colon */ |
622 | checkname(ls, &key); | 413 | checkname(ls, &key); |
623 | luaK_indexed(fs, v, &key); | 414 | luaK_indexed(fs, v, &key); |
@@ -656,7 +447,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { | |||
656 | expdesc key, val; | 447 | expdesc key, val; |
657 | int rkkey; | 448 | int rkkey; |
658 | if (ls->t.token == TK_NAME) { | 449 | if (ls->t.token == TK_NAME) { |
659 | checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); | 450 | luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); |
660 | checkname(ls, &key); | 451 | checkname(ls, &key); |
661 | } | 452 | } |
662 | else /* ls->t.token == '[' */ | 453 | else /* ls->t.token == '[' */ |
@@ -665,7 +456,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { | |||
665 | checknext(ls, '='); | 456 | checknext(ls, '='); |
666 | rkkey = luaK_exp2RK(fs, &key); | 457 | rkkey = luaK_exp2RK(fs, &key); |
667 | expr(ls, &val); | 458 | expr(ls, &val); |
668 | luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val)); | 459 | luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); |
669 | fs->freereg = reg; /* free registers */ | 460 | fs->freereg = reg; /* free registers */ |
670 | } | 461 | } |
671 | 462 | ||
@@ -675,7 +466,7 @@ static void closelistfield (FuncState *fs, struct ConsControl *cc) { | |||
675 | luaK_exp2nextreg(fs, &cc->v); | 466 | luaK_exp2nextreg(fs, &cc->v); |
676 | cc->v.k = VVOID; | 467 | cc->v.k = VVOID; |
677 | if (cc->tostore == LFIELDS_PER_FLUSH) { | 468 | if (cc->tostore == LFIELDS_PER_FLUSH) { |
678 | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ | 469 | luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ |
679 | cc->tostore = 0; /* no more items pending */ | 470 | cc->tostore = 0; /* no more items pending */ |
680 | } | 471 | } |
681 | } | 472 | } |
@@ -685,51 +476,27 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) { | |||
685 | if (cc->tostore == 0) return; | 476 | if (cc->tostore == 0) return; |
686 | if (hasmultret(cc->v.k)) { | 477 | if (hasmultret(cc->v.k)) { |
687 | luaK_setmultret(fs, &cc->v); | 478 | luaK_setmultret(fs, &cc->v); |
688 | luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); | 479 | luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); |
689 | cc->na--; /* do not count last expression (unknown number of elements) */ | 480 | cc->na--; /* do not count last expression (unknown number of elements) */ |
690 | } | 481 | } |
691 | else { | 482 | else { |
692 | if (cc->v.k != VVOID) | 483 | if (cc->v.k != VVOID) |
693 | luaK_exp2nextreg(fs, &cc->v); | 484 | luaK_exp2nextreg(fs, &cc->v); |
694 | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); | 485 | luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); |
695 | } | 486 | } |
696 | } | 487 | } |
697 | 488 | ||
698 | 489 | ||
699 | static void listfield (LexState *ls, struct ConsControl *cc) { | 490 | static void listfield (LexState *ls, struct ConsControl *cc) { |
700 | /* listfield -> exp */ | ||
701 | expr(ls, &cc->v); | 491 | expr(ls, &cc->v); |
702 | checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); | 492 | luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); |
703 | cc->na++; | 493 | cc->na++; |
704 | cc->tostore++; | 494 | cc->tostore++; |
705 | } | 495 | } |
706 | 496 | ||
707 | 497 | ||
708 | static void field (LexState *ls, struct ConsControl *cc) { | ||
709 | /* field -> listfield | recfield */ | ||
710 | switch(ls->t.token) { | ||
711 | case TK_NAME: { /* may be 'listfield' or 'recfield' */ | ||
712 | if (luaX_lookahead(ls) != '=') /* expression? */ | ||
713 | listfield(ls, cc); | ||
714 | else | ||
715 | recfield(ls, cc); | ||
716 | break; | ||
717 | } | ||
718 | case '[': { | ||
719 | recfield(ls, cc); | ||
720 | break; | ||
721 | } | ||
722 | default: { | ||
723 | listfield(ls, cc); | ||
724 | break; | ||
725 | } | ||
726 | } | ||
727 | } | ||
728 | |||
729 | |||
730 | static void constructor (LexState *ls, expdesc *t) { | 498 | static void constructor (LexState *ls, expdesc *t) { |
731 | /* constructor -> '{' [ field { sep field } [sep] ] '}' | 499 | /* constructor -> ?? */ |
732 | sep -> ',' | ';' */ | ||
733 | FuncState *fs = ls->fs; | 500 | FuncState *fs = ls->fs; |
734 | int line = ls->linenumber; | 501 | int line = ls->linenumber; |
735 | int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); | 502 | int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); |
@@ -738,13 +505,30 @@ static void constructor (LexState *ls, expdesc *t) { | |||
738 | cc.t = t; | 505 | cc.t = t; |
739 | init_exp(t, VRELOCABLE, pc); | 506 | init_exp(t, VRELOCABLE, pc); |
740 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ | 507 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
741 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ | 508 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ |
742 | checknext(ls, '{'); | 509 | checknext(ls, '{'); |
743 | do { | 510 | do { |
744 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); | 511 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
745 | if (ls->t.token == '}') break; | 512 | if (ls->t.token == '}') break; |
746 | closelistfield(fs, &cc); | 513 | closelistfield(fs, &cc); |
747 | field(ls, &cc); | 514 | switch(ls->t.token) { |
515 | case TK_NAME: { /* may be listfields or recfields */ | ||
516 | luaX_lookahead(ls); | ||
517 | if (ls->lookahead.token != '=') /* expression? */ | ||
518 | listfield(ls, &cc); | ||
519 | else | ||
520 | recfield(ls, &cc); | ||
521 | break; | ||
522 | } | ||
523 | case '[': { /* constructor_item -> recfield */ | ||
524 | recfield(ls, &cc); | ||
525 | break; | ||
526 | } | ||
527 | default: { /* constructor_part -> listfield */ | ||
528 | listfield(ls, &cc); | ||
529 | break; | ||
530 | } | ||
531 | } | ||
748 | } while (testnext(ls, ',') || testnext(ls, ';')); | 532 | } while (testnext(ls, ',') || testnext(ls, ';')); |
749 | check_match(ls, '}', '{', line); | 533 | check_match(ls, '}', '{', line); |
750 | lastlistfield(fs, &cc); | 534 | lastlistfield(fs, &cc); |
@@ -766,13 +550,17 @@ static void parlist (LexState *ls) { | |||
766 | do { | 550 | do { |
767 | switch (ls->t.token) { | 551 | switch (ls->t.token) { |
768 | case TK_NAME: { /* param -> NAME */ | 552 | case TK_NAME: { /* param -> NAME */ |
769 | new_localvar(ls, str_checkname(ls)); | 553 | new_localvar(ls, str_checkname(ls), nparams++); |
770 | nparams++; | ||
771 | break; | 554 | break; |
772 | } | 555 | } |
773 | case TK_DOTS: { /* param -> `...' */ | 556 | case TK_DOTS: { /* param -> `...' */ |
774 | luaX_next(ls); | 557 | luaX_next(ls); |
775 | f->is_vararg = 1; | 558 | #if defined(LUA_COMPAT_VARARG) |
559 | /* use `arg' as default name */ | ||
560 | new_localvarliteral(ls, "arg", nparams++); | ||
561 | f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; | ||
562 | #endif | ||
563 | f->is_vararg |= VARARG_ISVARARG; | ||
776 | break; | 564 | break; |
777 | } | 565 | } |
778 | default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected"); | 566 | default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected"); |
@@ -780,35 +568,33 @@ static void parlist (LexState *ls) { | |||
780 | } while (!f->is_vararg && testnext(ls, ',')); | 568 | } while (!f->is_vararg && testnext(ls, ',')); |
781 | } | 569 | } |
782 | adjustlocalvars(ls, nparams); | 570 | adjustlocalvars(ls, nparams); |
783 | f->numparams = cast_byte(fs->nactvar); | 571 | f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); |
784 | luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ | 572 | luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ |
785 | } | 573 | } |
786 | 574 | ||
787 | 575 | ||
788 | static void body (LexState *ls, expdesc *e, int ismethod, int line) { | 576 | static void body (LexState *ls, expdesc *e, int needself, int line) { |
789 | /* body -> `(' parlist `)' block END */ | 577 | /* body -> `(' parlist `)' chunk END */ |
790 | FuncState new_fs; | 578 | FuncState new_fs; |
791 | BlockCnt bl; | 579 | lparser_open_func(ls, &new_fs); |
792 | new_fs.f = addprototype(ls); | ||
793 | new_fs.f->linedefined = line; | 580 | new_fs.f->linedefined = line; |
794 | open_func(ls, &new_fs, &bl); | ||
795 | checknext(ls, '('); | 581 | checknext(ls, '('); |
796 | if (ismethod) { | 582 | if (needself) { |
797 | new_localvarliteral(ls, "self"); /* create 'self' parameter */ | 583 | new_localvarliteral(ls, "self", 0); |
798 | adjustlocalvars(ls, 1); | 584 | adjustlocalvars(ls, 1); |
799 | } | 585 | } |
800 | parlist(ls); | 586 | parlist(ls); |
801 | checknext(ls, ')'); | 587 | checknext(ls, ')'); |
802 | statlist(ls); | 588 | chunk(ls); |
803 | new_fs.f->lastlinedefined = ls->linenumber; | 589 | new_fs.f->lastlinedefined = ls->linenumber; |
804 | check_match(ls, TK_END, TK_FUNCTION, line); | 590 | check_match(ls, TK_END, TK_FUNCTION, line); |
805 | codeclosure(ls, e); | ||
806 | close_func(ls); | 591 | close_func(ls); |
592 | pushclosure(ls, &new_fs, e); | ||
807 | } | 593 | } |
808 | 594 | ||
809 | 595 | ||
810 | static int explist (LexState *ls, expdesc *v) { | 596 | static int explist1 (LexState *ls, expdesc *v) { |
811 | /* explist -> expr { `,' expr } */ | 597 | /* explist1 -> expr { `,' expr } */ |
812 | int n = 1; /* at least one expression */ | 598 | int n = 1; /* at least one expression */ |
813 | expr(ls, v); | 599 | expr(ls, v); |
814 | while (testnext(ls, ',')) { | 600 | while (testnext(ls, ',')) { |
@@ -820,17 +606,20 @@ static int explist (LexState *ls, expdesc *v) { | |||
820 | } | 606 | } |
821 | 607 | ||
822 | 608 | ||
823 | static void funcargs (LexState *ls, expdesc *f, int line) { | 609 | static void funcargs (LexState *ls, expdesc *f) { |
824 | FuncState *fs = ls->fs; | 610 | FuncState *fs = ls->fs; |
825 | expdesc args; | 611 | expdesc args; |
826 | int base, nparams; | 612 | int base, nparams; |
613 | int line = ls->linenumber; | ||
827 | switch (ls->t.token) { | 614 | switch (ls->t.token) { |
828 | case '(': { /* funcargs -> `(' [ explist ] `)' */ | 615 | case '(': { /* funcargs -> `(' [ explist1 ] `)' */ |
616 | if (line != ls->lastline) | ||
617 | luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); | ||
829 | luaX_next(ls); | 618 | luaX_next(ls); |
830 | if (ls->t.token == ')') /* arg list is empty? */ | 619 | if (ls->t.token == ')') /* arg list is empty? */ |
831 | args.k = VVOID; | 620 | args.k = VVOID; |
832 | else { | 621 | else { |
833 | explist(ls, &args); | 622 | explist1(ls, &args); |
834 | luaK_setmultret(fs, &args); | 623 | luaK_setmultret(fs, &args); |
835 | } | 624 | } |
836 | check_match(ls, ')', '(', line); | 625 | check_match(ls, ')', '(', line); |
@@ -847,10 +636,11 @@ static void funcargs (LexState *ls, expdesc *f, int line) { | |||
847 | } | 636 | } |
848 | default: { | 637 | default: { |
849 | luaX_syntaxerror(ls, "function arguments expected"); | 638 | luaX_syntaxerror(ls, "function arguments expected"); |
639 | return; | ||
850 | } | 640 | } |
851 | } | 641 | } |
852 | lua_assert(f->k == VNONRELOC); | 642 | lua_assert(f->k == VNONRELOC); |
853 | base = f->u.info; /* base register for call */ | 643 | base = f->u.s.info; /* base register for call */ |
854 | if (hasmultret(args.k)) | 644 | if (hasmultret(args.k)) |
855 | nparams = LUA_MULTRET; /* open call */ | 645 | nparams = LUA_MULTRET; /* open call */ |
856 | else { | 646 | else { |
@@ -874,8 +664,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) { | |||
874 | */ | 664 | */ |
875 | 665 | ||
876 | 666 | ||
877 | static void primaryexp (LexState *ls, expdesc *v) { | 667 | static void prefixexp (LexState *ls, expdesc *v) { |
878 | /* primaryexp -> NAME | '(' expr ')' */ | 668 | /* prefixexp -> NAME | '(' expr ')' */ |
879 | switch (ls->t.token) { | 669 | switch (ls->t.token) { |
880 | case '(': { | 670 | case '(': { |
881 | int line = ls->linenumber; | 671 | int line = ls->linenumber; |
@@ -891,26 +681,26 @@ static void primaryexp (LexState *ls, expdesc *v) { | |||
891 | } | 681 | } |
892 | default: { | 682 | default: { |
893 | luaX_syntaxerror(ls, "unexpected symbol"); | 683 | luaX_syntaxerror(ls, "unexpected symbol"); |
684 | return; | ||
894 | } | 685 | } |
895 | } | 686 | } |
896 | } | 687 | } |
897 | 688 | ||
898 | 689 | ||
899 | static void suffixedexp (LexState *ls, expdesc *v) { | 690 | static void primaryexp (LexState *ls, expdesc *v) { |
900 | /* suffixedexp -> | 691 | /* primaryexp -> |
901 | primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ | 692 | prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ |
902 | FuncState *fs = ls->fs; | 693 | FuncState *fs = ls->fs; |
903 | int line = ls->linenumber; | 694 | prefixexp(ls, v); |
904 | primaryexp(ls, v); | ||
905 | for (;;) { | 695 | for (;;) { |
906 | switch (ls->t.token) { | 696 | switch (ls->t.token) { |
907 | case '.': { /* fieldsel */ | 697 | case '.': { /* field */ |
908 | fieldsel(ls, v); | 698 | field(ls, v); |
909 | break; | 699 | break; |
910 | } | 700 | } |
911 | case '[': { /* `[' exp1 `]' */ | 701 | case '[': { /* `[' exp1 `]' */ |
912 | expdesc key; | 702 | expdesc key; |
913 | luaK_exp2anyregup(fs, v); | 703 | luaK_exp2anyreg(fs, v); |
914 | yindex(ls, &key); | 704 | yindex(ls, &key); |
915 | luaK_indexed(fs, v, &key); | 705 | luaK_indexed(fs, v, &key); |
916 | break; | 706 | break; |
@@ -920,12 +710,12 @@ static void suffixedexp (LexState *ls, expdesc *v) { | |||
920 | luaX_next(ls); | 710 | luaX_next(ls); |
921 | checkname(ls, &key); | 711 | checkname(ls, &key); |
922 | luaK_self(fs, v, &key); | 712 | luaK_self(fs, v, &key); |
923 | funcargs(ls, v, line); | 713 | funcargs(ls, v); |
924 | break; | 714 | break; |
925 | } | 715 | } |
926 | case '(': case TK_STRING: case '{': { /* funcargs */ | 716 | case '(': case TK_STRING: case '{': { /* funcargs */ |
927 | luaK_exp2nextreg(fs, v); | 717 | luaK_exp2nextreg(fs, v); |
928 | funcargs(ls, v, line); | 718 | funcargs(ls, v); |
929 | break; | 719 | break; |
930 | } | 720 | } |
931 | default: return; | 721 | default: return; |
@@ -935,8 +725,8 @@ static void suffixedexp (LexState *ls, expdesc *v) { | |||
935 | 725 | ||
936 | 726 | ||
937 | static void simpleexp (LexState *ls, expdesc *v) { | 727 | static void simpleexp (LexState *ls, expdesc *v) { |
938 | /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | | 728 | /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | |
939 | constructor | FUNCTION body | suffixedexp */ | 729 | constructor | FUNCTION body | primaryexp */ |
940 | switch (ls->t.token) { | 730 | switch (ls->t.token) { |
941 | case TK_NUMBER: { | 731 | case TK_NUMBER: { |
942 | init_exp(v, VKNUM, 0); | 732 | init_exp(v, VKNUM, 0); |
@@ -963,6 +753,7 @@ static void simpleexp (LexState *ls, expdesc *v) { | |||
963 | FuncState *fs = ls->fs; | 753 | FuncState *fs = ls->fs; |
964 | check_condition(ls, fs->f->is_vararg, | 754 | check_condition(ls, fs->f->is_vararg, |
965 | "cannot use " LUA_QL("...") " outside a vararg function"); | 755 | "cannot use " LUA_QL("...") " outside a vararg function"); |
756 | fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ | ||
966 | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); | 757 | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); |
967 | break; | 758 | break; |
968 | } | 759 | } |
@@ -976,7 +767,7 @@ static void simpleexp (LexState *ls, expdesc *v) { | |||
976 | return; | 767 | return; |
977 | } | 768 | } |
978 | default: { | 769 | default: { |
979 | suffixedexp(ls, v); | 770 | primaryexp(ls, v); |
980 | return; | 771 | return; |
981 | } | 772 | } |
982 | } | 773 | } |
@@ -1020,11 +811,11 @@ static const struct { | |||
1020 | lu_byte left; /* left priority for each binary operator */ | 811 | lu_byte left; /* left priority for each binary operator */ |
1021 | lu_byte right; /* right priority */ | 812 | lu_byte right; /* right priority */ |
1022 | } priority[] = { /* ORDER OPR */ | 813 | } priority[] = { /* ORDER OPR */ |
1023 | {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `*' `/' `%' */ | 814 | {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ |
1024 | {10, 9}, {5, 4}, /* ^, .. (right associative) */ | 815 | {10, 9}, {5, 4}, /* power and concat (right associative) */ |
1025 | {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ | 816 | {3, 3}, {3, 3}, /* equality and inequality */ |
1026 | {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ | 817 | {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ |
1027 | {2, 2}, {1, 1} /* and, or */ | 818 | {2, 2}, {1, 1} /* logical (and/or) */ |
1028 | }; | 819 | }; |
1029 | 820 | ||
1030 | #define UNARY_PRIORITY 8 /* priority for unary operators */ | 821 | #define UNARY_PRIORITY 8 /* priority for unary operators */ |
@@ -1034,16 +825,15 @@ static const struct { | |||
1034 | ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } | 825 | ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } |
1035 | ** where `binop' is any binary operator with a priority higher than `limit' | 826 | ** where `binop' is any binary operator with a priority higher than `limit' |
1036 | */ | 827 | */ |
1037 | static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { | 828 | static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { |
1038 | BinOpr op; | 829 | BinOpr op; |
1039 | UnOpr uop; | 830 | UnOpr uop; |
1040 | enterlevel(ls); | 831 | enterlevel(ls); |
1041 | uop = getunopr(ls->t.token); | 832 | uop = getunopr(ls->t.token); |
1042 | if (uop != OPR_NOUNOPR) { | 833 | if (uop != OPR_NOUNOPR) { |
1043 | int line = ls->linenumber; | ||
1044 | luaX_next(ls); | 834 | luaX_next(ls); |
1045 | subexpr(ls, v, UNARY_PRIORITY); | 835 | subexpr(ls, v, UNARY_PRIORITY); |
1046 | luaK_prefix(ls->fs, uop, v, line); | 836 | luaK_prefix(ls->fs, uop, v); |
1047 | } | 837 | } |
1048 | else simpleexp(ls, v); | 838 | else simpleexp(ls, v); |
1049 | /* expand while operators have priorities higher than `limit' */ | 839 | /* expand while operators have priorities higher than `limit' */ |
@@ -1051,12 +841,11 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { | |||
1051 | while (op != OPR_NOBINOPR && priority[op].left > limit) { | 841 | while (op != OPR_NOBINOPR && priority[op].left > limit) { |
1052 | expdesc v2; | 842 | expdesc v2; |
1053 | BinOpr nextop; | 843 | BinOpr nextop; |
1054 | int line = ls->linenumber; | ||
1055 | luaX_next(ls); | 844 | luaX_next(ls); |
1056 | luaK_infix(ls->fs, op, v); | 845 | luaK_infix(ls->fs, op, v); |
1057 | /* read sub-expression with higher priority */ | 846 | /* read sub-expression with higher priority */ |
1058 | nextop = subexpr(ls, &v2, priority[op].right); | 847 | nextop = subexpr(ls, &v2, priority[op].right); |
1059 | luaK_posfix(ls->fs, op, v, &v2, line); | 848 | luaK_posfix(ls->fs, op, v, &v2); |
1060 | op = nextop; | 849 | op = nextop; |
1061 | } | 850 | } |
1062 | leavelevel(ls); | 851 | leavelevel(ls); |
@@ -1079,12 +868,23 @@ static void expr (LexState *ls, expdesc *v) { | |||
1079 | */ | 868 | */ |
1080 | 869 | ||
1081 | 870 | ||
871 | static int block_follow (int token) { | ||
872 | switch (token) { | ||
873 | case TK_ELSE: case TK_ELSEIF: case TK_END: | ||
874 | case TK_UNTIL: case TK_EOS: | ||
875 | return 1; | ||
876 | default: return 0; | ||
877 | } | ||
878 | } | ||
879 | |||
880 | |||
1082 | static void block (LexState *ls) { | 881 | static void block (LexState *ls) { |
1083 | /* block -> statlist */ | 882 | /* block -> chunk */ |
1084 | FuncState *fs = ls->fs; | 883 | FuncState *fs = ls->fs; |
1085 | BlockCnt bl; | 884 | BlockCnt bl; |
1086 | enterblock(fs, &bl, 0); | 885 | enterblock(fs, &bl, 0); |
1087 | statlist(ls); | 886 | chunk(ls); |
887 | lua_assert(bl.breaklist == NO_JUMP); | ||
1088 | leaveblock(fs); | 888 | leaveblock(fs); |
1089 | } | 889 | } |
1090 | 890 | ||
@@ -1100,34 +900,29 @@ struct LHS_assign { | |||
1100 | 900 | ||
1101 | 901 | ||
1102 | /* | 902 | /* |
1103 | ** check whether, in an assignment to an upvalue/local variable, the | 903 | ** check whether, in an assignment to a local variable, the local variable |
1104 | ** upvalue/local variable is begin used in a previous assignment to a | 904 | ** is needed in a previous assignment (to a table). If so, save original |
1105 | ** table. If so, save original upvalue/local value in a safe place and | 905 | ** local value in a safe place and use this safe copy in the previous |
1106 | ** use this safe copy in the previous assignment. | 906 | ** assignment. |
1107 | */ | 907 | */ |
1108 | static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { | 908 | static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { |
1109 | FuncState *fs = ls->fs; | 909 | FuncState *fs = ls->fs; |
1110 | int extra = fs->freereg; /* eventual position to save local variable */ | 910 | int extra = fs->freereg; /* eventual position to save local variable */ |
1111 | int conflict = 0; | 911 | int conflict = 0; |
1112 | for (; lh; lh = lh->prev) { /* check all previous assignments */ | 912 | for (; lh; lh = lh->prev) { |
1113 | if (lh->v.k == VINDEXED) { /* assigning to a table? */ | 913 | if (lh->v.k == VINDEXED) { |
1114 | /* table is the upvalue/local being assigned now? */ | 914 | if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ |
1115 | if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) { | ||
1116 | conflict = 1; | 915 | conflict = 1; |
1117 | lh->v.u.ind.vt = VLOCAL; | 916 | lh->v.u.s.info = extra; /* previous assignment will use safe copy */ |
1118 | lh->v.u.ind.t = extra; /* previous assignment will use safe copy */ | ||
1119 | } | 917 | } |
1120 | /* index is the local being assigned? (index cannot be upvalue) */ | 918 | if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ |
1121 | if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) { | ||
1122 | conflict = 1; | 919 | conflict = 1; |
1123 | lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ | 920 | lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ |
1124 | } | 921 | } |
1125 | } | 922 | } |
1126 | } | 923 | } |
1127 | if (conflict) { | 924 | if (conflict) { |
1128 | /* copy upvalue/local value to a temporary (in position 'extra') */ | 925 | luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ |
1129 | OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; | ||
1130 | luaK_codeABC(fs, op, extra, v->u.info, 0); | ||
1131 | luaK_reserveregs(fs, 1); | 926 | luaK_reserveregs(fs, 1); |
1132 | } | 927 | } |
1133 | } | 928 | } |
@@ -1135,21 +930,22 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { | |||
1135 | 930 | ||
1136 | static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { | 931 | static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { |
1137 | expdesc e; | 932 | expdesc e; |
1138 | check_condition(ls, vkisvar(lh->v.k), "syntax error"); | 933 | check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, |
1139 | if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */ | 934 | "syntax error"); |
935 | if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ | ||
1140 | struct LHS_assign nv; | 936 | struct LHS_assign nv; |
1141 | nv.prev = lh; | 937 | nv.prev = lh; |
1142 | suffixedexp(ls, &nv.v); | 938 | primaryexp(ls, &nv.v); |
1143 | if (nv.v.k != VINDEXED) | 939 | if (nv.v.k == VLOCAL) |
1144 | check_conflict(ls, lh, &nv.v); | 940 | check_conflict(ls, lh, &nv.v); |
1145 | checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, | 941 | luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, |
1146 | "C levels"); | 942 | "variables in assignment"); |
1147 | assignment(ls, &nv, nvars+1); | 943 | assignment(ls, &nv, nvars+1); |
1148 | } | 944 | } |
1149 | else { /* assignment -> `=' explist */ | 945 | else { /* assignment -> `=' explist1 */ |
1150 | int nexps; | 946 | int nexps; |
1151 | checknext(ls, '='); | 947 | checknext(ls, '='); |
1152 | nexps = explist(ls, &e); | 948 | nexps = explist1(ls, &e); |
1153 | if (nexps != nvars) { | 949 | if (nexps != nvars) { |
1154 | adjust_assign(ls, nvars, nexps, &e); | 950 | adjust_assign(ls, nvars, nexps, &e); |
1155 | if (nexps > nvars) | 951 | if (nexps > nvars) |
@@ -1176,57 +972,19 @@ static int cond (LexState *ls) { | |||
1176 | } | 972 | } |
1177 | 973 | ||
1178 | 974 | ||
1179 | static void gotostat (LexState *ls, int pc) { | 975 | static void breakstat (LexState *ls) { |
1180 | int line = ls->linenumber; | ||
1181 | TString *label; | ||
1182 | int g; | ||
1183 | if (testnext(ls, TK_GOTO)) | ||
1184 | label = str_checkname(ls); | ||
1185 | else { | ||
1186 | luaX_next(ls); /* skip break */ | ||
1187 | label = luaS_new(ls->L, "break"); | ||
1188 | } | ||
1189 | g = newlabelentry(ls, &ls->dyd->gt, label, line, pc); | ||
1190 | findlabel(ls, g); /* close it if label already defined */ | ||
1191 | } | ||
1192 | |||
1193 | |||
1194 | /* check for repeated labels on the same block */ | ||
1195 | static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { | ||
1196 | int i; | ||
1197 | for (i = fs->bl->firstlabel; i < ll->n; i++) { | ||
1198 | if (luaS_eqstr(label, ll->arr[i].name)) { | ||
1199 | const char *msg = luaO_pushfstring(fs->ls->L, | ||
1200 | "label " LUA_QS " already defined on line %d", | ||
1201 | getstr(label), ll->arr[i].line); | ||
1202 | semerror(fs->ls, msg); | ||
1203 | } | ||
1204 | } | ||
1205 | } | ||
1206 | |||
1207 | |||
1208 | /* skip no-op statements */ | ||
1209 | static void skipnoopstat (LexState *ls) { | ||
1210 | while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) | ||
1211 | statement(ls); | ||
1212 | } | ||
1213 | |||
1214 | |||
1215 | static void labelstat (LexState *ls, TString *label, int line) { | ||
1216 | /* label -> '::' NAME '::' */ | ||
1217 | FuncState *fs = ls->fs; | 976 | FuncState *fs = ls->fs; |
1218 | Labellist *ll = &ls->dyd->label; | 977 | BlockCnt *bl = fs->bl; |
1219 | int l; /* index of new label being created */ | 978 | int upval = 0; |
1220 | checkrepeated(fs, ll, label); /* check for repeated labels */ | 979 | while (bl && !bl->isbreakable) { |
1221 | checknext(ls, TK_DBCOLON); /* skip double colon */ | 980 | upval |= bl->upval; |
1222 | /* create new entry for this label */ | 981 | bl = bl->previous; |
1223 | l = newlabelentry(ls, ll, label, line, fs->pc); | ||
1224 | skipnoopstat(ls); /* skip other no-op statements */ | ||
1225 | if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ | ||
1226 | /* assume that locals are already out of scope */ | ||
1227 | ll->arr[l].nactvar = fs->bl->nactvar; | ||
1228 | } | 982 | } |
1229 | findgotos(ls, &ll->arr[l]); | 983 | if (!bl) |
984 | luaX_syntaxerror(ls, "no loop to break"); | ||
985 | if (upval) | ||
986 | luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); | ||
987 | luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); | ||
1230 | } | 988 | } |
1231 | 989 | ||
1232 | 990 | ||
@@ -1242,7 +1000,7 @@ static void whilestat (LexState *ls, int line) { | |||
1242 | enterblock(fs, &bl, 1); | 1000 | enterblock(fs, &bl, 1); |
1243 | checknext(ls, TK_DO); | 1001 | checknext(ls, TK_DO); |
1244 | block(ls); | 1002 | block(ls); |
1245 | luaK_jumpto(fs, whileinit); | 1003 | luaK_patchlist(fs, luaK_jump(fs), whileinit); |
1246 | check_match(ls, TK_END, TK_WHILE, line); | 1004 | check_match(ls, TK_END, TK_WHILE, line); |
1247 | leaveblock(fs); | 1005 | leaveblock(fs); |
1248 | luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ | 1006 | luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ |
@@ -1258,25 +1016,30 @@ static void repeatstat (LexState *ls, int line) { | |||
1258 | enterblock(fs, &bl1, 1); /* loop block */ | 1016 | enterblock(fs, &bl1, 1); /* loop block */ |
1259 | enterblock(fs, &bl2, 0); /* scope block */ | 1017 | enterblock(fs, &bl2, 0); /* scope block */ |
1260 | luaX_next(ls); /* skip REPEAT */ | 1018 | luaX_next(ls); /* skip REPEAT */ |
1261 | statlist(ls); | 1019 | chunk(ls); |
1262 | check_match(ls, TK_UNTIL, TK_REPEAT, line); | 1020 | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
1263 | condexit = cond(ls); /* read condition (inside scope block) */ | 1021 | condexit = cond(ls); /* read condition (inside scope block) */ |
1264 | if (bl2.upval) /* upvalues? */ | 1022 | if (!bl2.upval) { /* no upvalues? */ |
1265 | luaK_patchclose(fs, condexit, bl2.nactvar); | 1023 | leaveblock(fs); /* finish scope */ |
1266 | leaveblock(fs); /* finish scope */ | 1024 | luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ |
1267 | luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ | 1025 | } |
1026 | else { /* complete semantics when there are upvalues */ | ||
1027 | breakstat(ls); /* if condition then break */ | ||
1028 | luaK_patchtohere(ls->fs, condexit); /* else... */ | ||
1029 | leaveblock(fs); /* finish scope... */ | ||
1030 | luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ | ||
1031 | } | ||
1268 | leaveblock(fs); /* finish loop */ | 1032 | leaveblock(fs); /* finish loop */ |
1269 | } | 1033 | } |
1270 | 1034 | ||
1271 | 1035 | ||
1272 | static int exp1 (LexState *ls) { | 1036 | static int exp1 (LexState *ls) { |
1273 | expdesc e; | 1037 | expdesc e; |
1274 | int reg; | 1038 | int k; |
1275 | expr(ls, &e); | 1039 | expr(ls, &e); |
1040 | k = e.k; | ||
1276 | luaK_exp2nextreg(ls->fs, &e); | 1041 | luaK_exp2nextreg(ls->fs, &e); |
1277 | lua_assert(e.k == VNONRELOC); | 1042 | return k; |
1278 | reg = e.u.info; | ||
1279 | return reg; | ||
1280 | } | 1043 | } |
1281 | 1044 | ||
1282 | 1045 | ||
@@ -1294,15 +1057,10 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { | |||
1294 | block(ls); | 1057 | block(ls); |
1295 | leaveblock(fs); /* end of scope for declared variables */ | 1058 | leaveblock(fs); /* end of scope for declared variables */ |
1296 | luaK_patchtohere(fs, prep); | 1059 | luaK_patchtohere(fs, prep); |
1297 | if (isnum) /* numeric for? */ | 1060 | endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : |
1298 | endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); | 1061 | luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); |
1299 | else { /* generic for */ | 1062 | luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ |
1300 | luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); | 1063 | luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); |
1301 | luaK_fixline(fs, line); | ||
1302 | endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); | ||
1303 | } | ||
1304 | luaK_patchlist(fs, endfor, prep + 1); | ||
1305 | luaK_fixline(fs, line); | ||
1306 | } | 1064 | } |
1307 | 1065 | ||
1308 | 1066 | ||
@@ -1310,10 +1068,10 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
1310 | /* fornum -> NAME = exp1,exp1[,exp1] forbody */ | 1068 | /* fornum -> NAME = exp1,exp1[,exp1] forbody */ |
1311 | FuncState *fs = ls->fs; | 1069 | FuncState *fs = ls->fs; |
1312 | int base = fs->freereg; | 1070 | int base = fs->freereg; |
1313 | new_localvarliteral(ls, "(for index)"); | 1071 | new_localvarliteral(ls, "(for index)", 0); |
1314 | new_localvarliteral(ls, "(for limit)"); | 1072 | new_localvarliteral(ls, "(for limit)", 1); |
1315 | new_localvarliteral(ls, "(for step)"); | 1073 | new_localvarliteral(ls, "(for step)", 2); |
1316 | new_localvar(ls, varname); | 1074 | new_localvar(ls, varname, 3); |
1317 | checknext(ls, '='); | 1075 | checknext(ls, '='); |
1318 | exp1(ls); /* initial value */ | 1076 | exp1(ls); /* initial value */ |
1319 | checknext(ls, ','); | 1077 | checknext(ls, ','); |
@@ -1321,7 +1079,7 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
1321 | if (testnext(ls, ',')) | 1079 | if (testnext(ls, ',')) |
1322 | exp1(ls); /* optional step */ | 1080 | exp1(ls); /* optional step */ |
1323 | else { /* default step = 1 */ | 1081 | else { /* default step = 1 */ |
1324 | luaK_codek(fs, fs->freereg, luaK_numberK(fs, 1)); | 1082 | luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); |
1325 | luaK_reserveregs(fs, 1); | 1083 | luaK_reserveregs(fs, 1); |
1326 | } | 1084 | } |
1327 | forbody(ls, base, line, 1, 1); | 1085 | forbody(ls, base, line, 1, 1); |
@@ -1329,25 +1087,23 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
1329 | 1087 | ||
1330 | 1088 | ||
1331 | static void forlist (LexState *ls, TString *indexname) { | 1089 | static void forlist (LexState *ls, TString *indexname) { |
1332 | /* forlist -> NAME {,NAME} IN explist forbody */ | 1090 | /* forlist -> NAME {,NAME} IN explist1 forbody */ |
1333 | FuncState *fs = ls->fs; | 1091 | FuncState *fs = ls->fs; |
1334 | expdesc e; | 1092 | expdesc e; |
1335 | int nvars = 4; /* gen, state, control, plus at least one declared var */ | 1093 | int nvars = 0; |
1336 | int line; | 1094 | int line; |
1337 | int base = fs->freereg; | 1095 | int base = fs->freereg; |
1338 | /* create control variables */ | 1096 | /* create control variables */ |
1339 | new_localvarliteral(ls, "(for generator)"); | 1097 | new_localvarliteral(ls, "(for generator)", nvars++); |
1340 | new_localvarliteral(ls, "(for state)"); | 1098 | new_localvarliteral(ls, "(for state)", nvars++); |
1341 | new_localvarliteral(ls, "(for control)"); | 1099 | new_localvarliteral(ls, "(for control)", nvars++); |
1342 | /* create declared variables */ | 1100 | /* create declared variables */ |
1343 | new_localvar(ls, indexname); | 1101 | new_localvar(ls, indexname, nvars++); |
1344 | while (testnext(ls, ',')) { | 1102 | while (testnext(ls, ',')) |
1345 | new_localvar(ls, str_checkname(ls)); | 1103 | new_localvar(ls, str_checkname(ls), nvars++); |
1346 | nvars++; | ||
1347 | } | ||
1348 | checknext(ls, TK_IN); | 1104 | checknext(ls, TK_IN); |
1349 | line = ls->linenumber; | 1105 | line = ls->linenumber; |
1350 | adjust_assign(ls, 3, explist(ls, &e), &e); | 1106 | adjust_assign(ls, 3, explist1(ls, &e), &e); |
1351 | luaK_checkstack(fs, 3); /* extra space to call generator */ | 1107 | luaK_checkstack(fs, 3); /* extra space to call generator */ |
1352 | forbody(ls, base, line, nvars - 3, 0); | 1108 | forbody(ls, base, line, nvars - 3, 0); |
1353 | } | 1109 | } |
@@ -1371,77 +1127,65 @@ static void forstat (LexState *ls, int line) { | |||
1371 | } | 1127 | } |
1372 | 1128 | ||
1373 | 1129 | ||
1374 | static void test_then_block (LexState *ls, int *escapelist) { | 1130 | static int test_then_block (LexState *ls) { |
1375 | /* test_then_block -> [IF | ELSEIF] cond THEN block */ | 1131 | /* test_then_block -> [IF | ELSEIF] cond THEN block */ |
1376 | BlockCnt bl; | 1132 | int condexit; |
1377 | FuncState *fs = ls->fs; | ||
1378 | expdesc v; | ||
1379 | int jf; /* instruction to skip 'then' code (if condition is false) */ | ||
1380 | luaX_next(ls); /* skip IF or ELSEIF */ | 1133 | luaX_next(ls); /* skip IF or ELSEIF */ |
1381 | expr(ls, &v); /* read condition */ | 1134 | condexit = cond(ls); |
1382 | checknext(ls, TK_THEN); | 1135 | checknext(ls, TK_THEN); |
1383 | if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { | 1136 | block(ls); /* `then' part */ |
1384 | luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ | 1137 | return condexit; |
1385 | enterblock(fs, &bl, 0); /* must enter block before 'goto' */ | ||
1386 | gotostat(ls, v.t); /* handle goto/break */ | ||
1387 | skipnoopstat(ls); /* skip other no-op statements */ | ||
1388 | if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ | ||
1389 | leaveblock(fs); | ||
1390 | return; /* and that is it */ | ||
1391 | } | ||
1392 | else /* must skip over 'then' part if condition is false */ | ||
1393 | jf = luaK_jump(fs); | ||
1394 | } | ||
1395 | else { /* regular case (not goto/break) */ | ||
1396 | luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ | ||
1397 | enterblock(fs, &bl, 0); | ||
1398 | jf = v.f; | ||
1399 | } | ||
1400 | statlist(ls); /* `then' part */ | ||
1401 | leaveblock(fs); | ||
1402 | if (ls->t.token == TK_ELSE || | ||
1403 | ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ | ||
1404 | luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ | ||
1405 | luaK_patchtohere(fs, jf); | ||
1406 | } | 1138 | } |
1407 | 1139 | ||
1408 | 1140 | ||
1409 | static void ifstat (LexState *ls, int line) { | 1141 | static void ifstat (LexState *ls, int line) { |
1410 | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ | 1142 | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ |
1411 | FuncState *fs = ls->fs; | 1143 | FuncState *fs = ls->fs; |
1412 | int escapelist = NO_JUMP; /* exit list for finished parts */ | 1144 | int flist; |
1413 | test_then_block(ls, &escapelist); /* IF cond THEN block */ | 1145 | int escapelist = NO_JUMP; |
1414 | while (ls->t.token == TK_ELSEIF) | 1146 | flist = test_then_block(ls); /* IF cond THEN block */ |
1415 | test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ | 1147 | while (ls->t.token == TK_ELSEIF) { |
1416 | if (testnext(ls, TK_ELSE)) | 1148 | luaK_concat(fs, &escapelist, luaK_jump(fs)); |
1149 | luaK_patchtohere(fs, flist); | ||
1150 | flist = test_then_block(ls); /* ELSEIF cond THEN block */ | ||
1151 | } | ||
1152 | if (ls->t.token == TK_ELSE) { | ||
1153 | luaK_concat(fs, &escapelist, luaK_jump(fs)); | ||
1154 | luaK_patchtohere(fs, flist); | ||
1155 | luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ | ||
1417 | block(ls); /* `else' part */ | 1156 | block(ls); /* `else' part */ |
1157 | } | ||
1158 | else | ||
1159 | luaK_concat(fs, &escapelist, flist); | ||
1160 | luaK_patchtohere(fs, escapelist); | ||
1418 | check_match(ls, TK_END, TK_IF, line); | 1161 | check_match(ls, TK_END, TK_IF, line); |
1419 | luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ | ||
1420 | } | 1162 | } |
1421 | 1163 | ||
1422 | 1164 | ||
1423 | static void localfunc (LexState *ls) { | 1165 | static void localfunc (LexState *ls) { |
1424 | expdesc b; | 1166 | expdesc v, b; |
1425 | FuncState *fs = ls->fs; | 1167 | FuncState *fs = ls->fs; |
1426 | new_localvar(ls, str_checkname(ls)); /* new local variable */ | 1168 | new_localvar(ls, str_checkname(ls), 0); |
1427 | adjustlocalvars(ls, 1); /* enter its scope */ | 1169 | init_exp(&v, VLOCAL, fs->freereg); |
1428 | body(ls, &b, 0, ls->linenumber); /* function created in next register */ | 1170 | luaK_reserveregs(fs, 1); |
1171 | adjustlocalvars(ls, 1); | ||
1172 | body(ls, &b, 0, ls->linenumber); | ||
1173 | luaK_storevar(fs, &v, &b); | ||
1429 | /* debug information will only see the variable after this point! */ | 1174 | /* debug information will only see the variable after this point! */ |
1430 | getlocvar(fs, b.u.info)->startpc = fs->pc; | 1175 | getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; |
1431 | } | 1176 | } |
1432 | 1177 | ||
1433 | 1178 | ||
1434 | static void localstat (LexState *ls) { | 1179 | static void localstat (LexState *ls) { |
1435 | /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */ | 1180 | /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ |
1436 | int nvars = 0; | 1181 | int nvars = 0; |
1437 | int nexps; | 1182 | int nexps; |
1438 | expdesc e; | 1183 | expdesc e; |
1439 | do { | 1184 | do { |
1440 | new_localvar(ls, str_checkname(ls)); | 1185 | new_localvar(ls, str_checkname(ls), nvars++); |
1441 | nvars++; | ||
1442 | } while (testnext(ls, ',')); | 1186 | } while (testnext(ls, ',')); |
1443 | if (testnext(ls, '=')) | 1187 | if (testnext(ls, '=')) |
1444 | nexps = explist(ls, &e); | 1188 | nexps = explist1(ls, &e); |
1445 | else { | 1189 | else { |
1446 | e.k = VVOID; | 1190 | e.k = VVOID; |
1447 | nexps = 0; | 1191 | nexps = 0; |
@@ -1452,26 +1196,26 @@ static void localstat (LexState *ls) { | |||
1452 | 1196 | ||
1453 | 1197 | ||
1454 | static int funcname (LexState *ls, expdesc *v) { | 1198 | static int funcname (LexState *ls, expdesc *v) { |
1455 | /* funcname -> NAME {fieldsel} [`:' NAME] */ | 1199 | /* funcname -> NAME {field} [`:' NAME] */ |
1456 | int ismethod = 0; | 1200 | int needself = 0; |
1457 | singlevar(ls, v); | 1201 | singlevar(ls, v); |
1458 | while (ls->t.token == '.') | 1202 | while (ls->t.token == '.') |
1459 | fieldsel(ls, v); | 1203 | field(ls, v); |
1460 | if (ls->t.token == ':') { | 1204 | if (ls->t.token == ':') { |
1461 | ismethod = 1; | 1205 | needself = 1; |
1462 | fieldsel(ls, v); | 1206 | field(ls, v); |
1463 | } | 1207 | } |
1464 | return ismethod; | 1208 | return needself; |
1465 | } | 1209 | } |
1466 | 1210 | ||
1467 | 1211 | ||
1468 | static void funcstat (LexState *ls, int line) { | 1212 | static void funcstat (LexState *ls, int line) { |
1469 | /* funcstat -> FUNCTION funcname body */ | 1213 | /* funcstat -> FUNCTION funcname body */ |
1470 | int ismethod; | 1214 | int needself; |
1471 | expdesc v, b; | 1215 | expdesc v, b; |
1472 | luaX_next(ls); /* skip FUNCTION */ | 1216 | luaX_next(ls); /* skip FUNCTION */ |
1473 | ismethod = funcname(ls, &v); | 1217 | needself = funcname(ls, &v); |
1474 | body(ls, &b, ismethod, line); | 1218 | body(ls, &b, needself, line); |
1475 | luaK_storevar(ls->fs, &v, &b); | 1219 | luaK_storevar(ls->fs, &v, &b); |
1476 | luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ | 1220 | luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ |
1477 | } | 1221 | } |
@@ -1481,27 +1225,26 @@ static void exprstat (LexState *ls) { | |||
1481 | /* stat -> func | assignment */ | 1225 | /* stat -> func | assignment */ |
1482 | FuncState *fs = ls->fs; | 1226 | FuncState *fs = ls->fs; |
1483 | struct LHS_assign v; | 1227 | struct LHS_assign v; |
1484 | suffixedexp(ls, &v.v); | 1228 | primaryexp(ls, &v.v); |
1485 | if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ | 1229 | if (v.v.k == VCALL) /* stat -> func */ |
1230 | SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ | ||
1231 | else { /* stat -> assignment */ | ||
1486 | v.prev = NULL; | 1232 | v.prev = NULL; |
1487 | assignment(ls, &v, 1); | 1233 | assignment(ls, &v, 1); |
1488 | } | 1234 | } |
1489 | else { /* stat -> func */ | ||
1490 | check_condition(ls, v.v.k == VCALL, "syntax error"); | ||
1491 | SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ | ||
1492 | } | ||
1493 | } | 1235 | } |
1494 | 1236 | ||
1495 | 1237 | ||
1496 | static void retstat (LexState *ls) { | 1238 | static void retstat (LexState *ls) { |
1497 | /* stat -> RETURN [explist] [';'] */ | 1239 | /* stat -> RETURN explist */ |
1498 | FuncState *fs = ls->fs; | 1240 | FuncState *fs = ls->fs; |
1499 | expdesc e; | 1241 | expdesc e; |
1500 | int first, nret; /* registers with returned values */ | 1242 | int first, nret; /* registers with returned values */ |
1501 | if (block_follow(ls, 1) || ls->t.token == ';') | 1243 | luaX_next(ls); /* skip RETURN */ |
1244 | if (block_follow(ls->t.token) || ls->t.token == ';') | ||
1502 | first = nret = 0; /* return no values */ | 1245 | first = nret = 0; /* return no values */ |
1503 | else { | 1246 | else { |
1504 | nret = explist(ls, &e); /* optional return values */ | 1247 | nret = explist1(ls, &e); /* optional return values */ |
1505 | if (hasmultret(e.k)) { | 1248 | if (hasmultret(e.k)) { |
1506 | luaK_setmultret(fs, &e); | 1249 | luaK_setmultret(fs, &e); |
1507 | if (e.k == VCALL && nret == 1) { /* tail call? */ | 1250 | if (e.k == VCALL && nret == 1) { /* tail call? */ |
@@ -1522,43 +1265,37 @@ static void retstat (LexState *ls) { | |||
1522 | } | 1265 | } |
1523 | } | 1266 | } |
1524 | luaK_ret(fs, first, nret); | 1267 | luaK_ret(fs, first, nret); |
1525 | testnext(ls, ';'); /* skip optional semicolon */ | ||
1526 | } | 1268 | } |
1527 | 1269 | ||
1528 | 1270 | ||
1529 | static void statement (LexState *ls) { | 1271 | static int statement (LexState *ls) { |
1530 | int line = ls->linenumber; /* may be needed for error messages */ | 1272 | int line = ls->linenumber; /* may be needed for error messages */ |
1531 | enterlevel(ls); | ||
1532 | switch (ls->t.token) { | 1273 | switch (ls->t.token) { |
1533 | case ';': { /* stat -> ';' (empty statement) */ | ||
1534 | luaX_next(ls); /* skip ';' */ | ||
1535 | break; | ||
1536 | } | ||
1537 | case TK_IF: { /* stat -> ifstat */ | 1274 | case TK_IF: { /* stat -> ifstat */ |
1538 | ifstat(ls, line); | 1275 | ifstat(ls, line); |
1539 | break; | 1276 | return 0; |
1540 | } | 1277 | } |
1541 | case TK_WHILE: { /* stat -> whilestat */ | 1278 | case TK_WHILE: { /* stat -> whilestat */ |
1542 | whilestat(ls, line); | 1279 | whilestat(ls, line); |
1543 | break; | 1280 | return 0; |
1544 | } | 1281 | } |
1545 | case TK_DO: { /* stat -> DO block END */ | 1282 | case TK_DO: { /* stat -> DO block END */ |
1546 | luaX_next(ls); /* skip DO */ | 1283 | luaX_next(ls); /* skip DO */ |
1547 | block(ls); | 1284 | block(ls); |
1548 | check_match(ls, TK_END, TK_DO, line); | 1285 | check_match(ls, TK_END, TK_DO, line); |
1549 | break; | 1286 | return 0; |
1550 | } | 1287 | } |
1551 | case TK_FOR: { /* stat -> forstat */ | 1288 | case TK_FOR: { /* stat -> forstat */ |
1552 | forstat(ls, line); | 1289 | forstat(ls, line); |
1553 | break; | 1290 | return 0; |
1554 | } | 1291 | } |
1555 | case TK_REPEAT: { /* stat -> repeatstat */ | 1292 | case TK_REPEAT: { /* stat -> repeatstat */ |
1556 | repeatstat(ls, line); | 1293 | repeatstat(ls, line); |
1557 | break; | 1294 | return 0; |
1558 | } | 1295 | } |
1559 | case TK_FUNCTION: { /* stat -> funcstat */ | 1296 | case TK_FUNCTION: { |
1560 | funcstat(ls, line); | 1297 | funcstat(ls, line); /* stat -> funcstat */ |
1561 | break; | 1298 | return 0; |
1562 | } | 1299 | } |
1563 | case TK_LOCAL: { /* stat -> localstat */ | 1300 | case TK_LOCAL: { /* stat -> localstat */ |
1564 | luaX_next(ls); /* skip LOCAL */ | 1301 | luaX_next(ls); /* skip LOCAL */ |
@@ -1566,73 +1303,37 @@ static void statement (LexState *ls) { | |||
1566 | localfunc(ls); | 1303 | localfunc(ls); |
1567 | else | 1304 | else |
1568 | localstat(ls); | 1305 | localstat(ls); |
1569 | break; | 1306 | return 0; |
1570 | } | ||
1571 | case TK_DBCOLON: { /* stat -> label */ | ||
1572 | luaX_next(ls); /* skip double colon */ | ||
1573 | labelstat(ls, str_checkname(ls), line); | ||
1574 | break; | ||
1575 | } | 1307 | } |
1576 | case TK_RETURN: { /* stat -> retstat */ | 1308 | case TK_RETURN: { /* stat -> retstat */ |
1577 | luaX_next(ls); /* skip RETURN */ | ||
1578 | retstat(ls); | 1309 | retstat(ls); |
1579 | break; | 1310 | return 1; /* must be last statement */ |
1580 | } | 1311 | } |
1581 | case TK_BREAK: /* stat -> breakstat */ | 1312 | case TK_BREAK: { /* stat -> breakstat */ |
1582 | case TK_GOTO: { /* stat -> 'goto' NAME */ | 1313 | luaX_next(ls); /* skip BREAK */ |
1583 | gotostat(ls, luaK_jump(ls->fs)); | 1314 | breakstat(ls); |
1584 | break; | 1315 | return 1; /* must be last statement */ |
1585 | } | 1316 | } |
1586 | default: { /* stat -> func | assignment */ | 1317 | default: { |
1587 | exprstat(ls); | 1318 | exprstat(ls); |
1588 | break; | 1319 | return 0; /* to avoid warnings */ |
1589 | } | 1320 | } |
1590 | } | 1321 | } |
1591 | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && | ||
1592 | ls->fs->freereg >= ls->fs->nactvar); | ||
1593 | ls->fs->freereg = ls->fs->nactvar; /* free registers */ | ||
1594 | leavelevel(ls); | ||
1595 | } | ||
1596 | |||
1597 | /* }====================================================================== */ | ||
1598 | |||
1599 | |||
1600 | /* | ||
1601 | ** compiles the main function, which is a regular vararg function with an | ||
1602 | ** upvalue named LUA_ENV | ||
1603 | */ | ||
1604 | static void mainfunc (LexState *ls, FuncState *fs) { | ||
1605 | BlockCnt bl; | ||
1606 | expdesc v; | ||
1607 | open_func(ls, fs, &bl); | ||
1608 | fs->f->is_vararg = 1; /* main function is always vararg */ | ||
1609 | init_exp(&v, VLOCAL, 0); /* create and... */ | ||
1610 | newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ | ||
1611 | luaX_next(ls); /* read first token */ | ||
1612 | statlist(ls); /* parse main body */ | ||
1613 | check(ls, TK_EOS); | ||
1614 | close_func(ls); | ||
1615 | } | 1322 | } |
1616 | 1323 | ||
1617 | 1324 | ||
1618 | Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, | 1325 | static void chunk (LexState *ls) { |
1619 | Dyndata *dyd, const char *name, int firstchar) { | 1326 | /* chunk -> { stat [`;'] } */ |
1620 | LexState lexstate; | 1327 | int islast = 0; |
1621 | FuncState funcstate; | 1328 | enterlevel(ls); |
1622 | Closure *cl = luaF_newLclosure(L, 1); /* create main closure */ | 1329 | while (!islast && !block_follow(ls->t.token)) { |
1623 | /* anchor closure (to avoid being collected) */ | 1330 | islast = statement(ls); |
1624 | setclLvalue(L, L->top, cl); | 1331 | testnext(ls, ';'); |
1625 | incr_top(L); | 1332 | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && |
1626 | funcstate.f = cl->l.p = luaF_newproto(L); | 1333 | ls->fs->freereg >= ls->fs->nactvar); |
1627 | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ | 1334 | ls->fs->freereg = ls->fs->nactvar; /* free registers */ |
1628 | lexstate.buff = buff; | 1335 | } |
1629 | lexstate.dyd = dyd; | 1336 | leavelevel(ls); |
1630 | dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; | ||
1631 | luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); | ||
1632 | mainfunc(&lexstate, &funcstate); | ||
1633 | lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); | ||
1634 | /* all scopes should be correctly finished */ | ||
1635 | lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); | ||
1636 | return cl; /* it's on the stack too */ | ||
1637 | } | 1337 | } |
1638 | 1338 | ||
1339 | /* }====================================================================== */ | ||