/src/testdir/build/lua-master/source/lparser.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lparser.c $ |
3 | | ** Lua Parser |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lparser_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <limits.h> |
14 | | #include <string.h> |
15 | | |
16 | | #include "lua.h" |
17 | | |
18 | | #include "lcode.h" |
19 | | #include "ldebug.h" |
20 | | #include "ldo.h" |
21 | | #include "lfunc.h" |
22 | | #include "llex.h" |
23 | | #include "lmem.h" |
24 | | #include "lobject.h" |
25 | | #include "lopcodes.h" |
26 | | #include "lparser.h" |
27 | | #include "lstate.h" |
28 | | #include "lstring.h" |
29 | | #include "ltable.h" |
30 | | |
31 | | |
32 | | |
33 | | /* maximum number of local variables per function (must be smaller |
34 | | than 250, due to the bytecode format) */ |
35 | 240k | #define MAXVARS 200 |
36 | | |
37 | | |
38 | 444k | #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) |
39 | | |
40 | | |
41 | | /* because all strings are unified by the scanner, the parser |
42 | | can use pointer equality for string equality */ |
43 | 123M | #define eqstr(a,b) ((a) == (b)) |
44 | | |
45 | | |
46 | | /* |
47 | | ** nodes for block list (list of active blocks) |
48 | | */ |
49 | | typedef struct BlockCnt { |
50 | | struct BlockCnt *previous; /* chain */ |
51 | | int firstlabel; /* index of first label in this block */ |
52 | | int firstgoto; /* index of first pending goto in this block */ |
53 | | lu_byte nactvar; /* # active locals outside the block */ |
54 | | lu_byte upval; /* true if some variable in the block is an upvalue */ |
55 | | lu_byte isloop; /* true if 'block' is a loop */ |
56 | | lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ |
57 | | } BlockCnt; |
58 | | |
59 | | |
60 | | |
61 | | /* |
62 | | ** prototypes for recursive non-terminal functions |
63 | | */ |
64 | | static void statement (LexState *ls); |
65 | | static void expr (LexState *ls, expdesc *v); |
66 | | |
67 | | |
68 | 2.62k | static l_noret error_expected (LexState *ls, int token) { |
69 | 2.62k | luaX_syntaxerror(ls, |
70 | 2.62k | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); |
71 | 2.62k | } |
72 | | |
73 | | |
74 | 18 | static l_noret errorlimit (FuncState *fs, int limit, const char *what) { |
75 | 18 | lua_State *L = fs->ls->L; |
76 | 18 | const char *msg; |
77 | 18 | int line = fs->f->linedefined; |
78 | 18 | const char *where = (line == 0) |
79 | 18 | ? "main function" |
80 | 18 | : luaO_pushfstring(L, "function at line %d", line); |
81 | 18 | msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", |
82 | 18 | what, limit, where); |
83 | 18 | luaX_syntaxerror(fs->ls, msg); |
84 | 18 | } |
85 | | |
86 | | |
87 | 296k | static void checklimit (FuncState *fs, int v, int l, const char *what) { |
88 | 296k | if (v > l) errorlimit(fs, l, what); |
89 | 296k | } |
90 | | |
91 | | |
92 | | /* |
93 | | ** Test whether next token is 'c'; if so, skip it. |
94 | | */ |
95 | 1.76M | static int testnext (LexState *ls, int c) { |
96 | 1.76M | if (ls->t.token == c) { |
97 | 671k | luaX_next(ls); |
98 | 671k | return 1; |
99 | 671k | } |
100 | 1.09M | else return 0; |
101 | 1.76M | } |
102 | | |
103 | | |
104 | | /* |
105 | | ** Check that next token is 'c'. |
106 | | */ |
107 | 13.6M | static void check (LexState *ls, int c) { |
108 | 13.6M | if (ls->t.token != c) |
109 | 1.86k | error_expected(ls, c); |
110 | 13.6M | } |
111 | | |
112 | | |
113 | | /* |
114 | | ** Check that next token is 'c' and skip it. |
115 | | */ |
116 | 678k | static void checknext (LexState *ls, int c) { |
117 | 678k | check(ls, c); |
118 | 678k | luaX_next(ls); |
119 | 678k | } |
120 | | |
121 | | |
122 | 772k | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } |
123 | | |
124 | | |
125 | | /* |
126 | | ** Check that next token is 'what' and skip it. In case of error, |
127 | | ** raise an error that the expected 'what' should match a 'who' |
128 | | ** in line 'where' (if that is not the current line). |
129 | | */ |
130 | 291k | static void check_match (LexState *ls, int what, int who, int where) { |
131 | 291k | if (l_unlikely(!testnext(ls, what))) { |
132 | 910 | if (where == ls->linenumber) /* all in the same line? */ |
133 | 762 | error_expected(ls, what); /* do not need a complex message */ |
134 | 148 | else { |
135 | 148 | luaX_syntaxerror(ls, luaO_pushfstring(ls->L, |
136 | 148 | "%s expected (to close %s at line %d)", |
137 | 148 | luaX_token2str(ls, what), luaX_token2str(ls, who), where)); |
138 | 148 | } |
139 | 910 | } |
140 | 291k | } |
141 | | |
142 | | |
143 | 12.9M | static TString *str_checkname (LexState *ls) { |
144 | 12.9M | TString *ts; |
145 | 12.9M | check(ls, TK_NAME); |
146 | 12.9M | ts = ls->t.seminfo.ts; |
147 | 12.9M | luaX_next(ls); |
148 | 12.9M | return ts; |
149 | 12.9M | } |
150 | | |
151 | | |
152 | 27.2M | static void init_exp (expdesc *e, expkind k, int i) { |
153 | 27.2M | e->f = e->t = NO_JUMP; |
154 | 27.2M | e->k = k; |
155 | 27.2M | e->u.info = i; |
156 | 27.2M | } |
157 | | |
158 | | |
159 | 13.3M | static void codestring (expdesc *e, TString *s) { |
160 | 13.3M | e->f = e->t = NO_JUMP; |
161 | 13.3M | e->k = VKSTR; |
162 | 13.3M | e->u.strval = s; |
163 | 13.3M | } |
164 | | |
165 | | |
166 | 51.7k | static void codename (LexState *ls, expdesc *e) { |
167 | 51.7k | codestring(e, str_checkname(ls)); |
168 | 51.7k | } |
169 | | |
170 | | |
171 | | /* |
172 | | ** Register a new local variable in the active 'Proto' (for debug |
173 | | ** information). |
174 | | */ |
175 | 214k | static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { |
176 | 214k | Proto *f = fs->f; |
177 | 214k | int oldsize = f->sizelocvars; |
178 | 214k | luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, |
179 | 214k | LocVar, SHRT_MAX, "local variables"); |
180 | 515k | while (oldsize < f->sizelocvars) |
181 | 300k | f->locvars[oldsize++].varname = NULL; |
182 | 214k | f->locvars[fs->ndebugvars].varname = varname; |
183 | 214k | f->locvars[fs->ndebugvars].startpc = fs->pc; |
184 | 214k | luaC_objbarrier(ls->L, f, varname); |
185 | 0 | return fs->ndebugvars++; |
186 | 214k | } |
187 | | |
188 | | |
189 | | /* |
190 | | ** Create a new local variable with the given 'name'. Return its index |
191 | | ** in the function. |
192 | | */ |
193 | 240k | static int new_localvar (LexState *ls, TString *name) { |
194 | 240k | lua_State *L = ls->L; |
195 | 240k | FuncState *fs = ls->fs; |
196 | 240k | Dyndata *dyd = ls->dyd; |
197 | 240k | Vardesc *var; |
198 | 240k | checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, |
199 | 240k | MAXVARS, "local variables"); |
200 | 240k | luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, |
201 | 240k | dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); |
202 | 240k | var = &dyd->actvar.arr[dyd->actvar.n++]; |
203 | 240k | var->vd.kind = VDKREG; /* default */ |
204 | 240k | var->vd.name = name; |
205 | 240k | return dyd->actvar.n - 1 - fs->firstlocal; |
206 | 240k | } |
207 | | |
208 | | #define new_localvarliteral(ls,v) \ |
209 | 154k | new_localvar(ls, \ |
210 | 154k | luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); |
211 | | |
212 | | |
213 | | |
214 | | /* |
215 | | ** Return the "variable description" (Vardesc) of a given variable. |
216 | | ** (Unless noted otherwise, all variables are referred to by their |
217 | | ** compiler indices.) |
218 | | */ |
219 | 114M | static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { |
220 | 114M | return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; |
221 | 114M | } |
222 | | |
223 | | |
224 | | /* |
225 | | ** Convert 'nvar', a compiler index level, to its corresponding |
226 | | ** register. For that, search for the highest variable below that level |
227 | | ** that is in a register and uses its register index ('ridx') plus one. |
228 | | */ |
229 | 37.0M | static int reglevel (FuncState *fs, int nvar) { |
230 | 37.4M | while (nvar-- > 0) { |
231 | 18.3M | Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ |
232 | 18.3M | if (vd->vd.kind != RDKCTC) /* is in a register? */ |
233 | 17.9M | return vd->vd.ridx + 1; |
234 | 18.3M | } |
235 | 19.0M | return 0; /* no variables in registers */ |
236 | 37.0M | } |
237 | | |
238 | | |
239 | | /* |
240 | | ** Return the number of variables in the register stack for the given |
241 | | ** function. |
242 | | */ |
243 | 36.4M | int luaY_nvarstack (FuncState *fs) { |
244 | 36.4M | return reglevel(fs, fs->nactvar); |
245 | 36.4M | } |
246 | | |
247 | | |
248 | | /* |
249 | | ** Get the debug-information entry for current variable 'vidx'. |
250 | | */ |
251 | 212k | static LocVar *localdebuginfo (FuncState *fs, int vidx) { |
252 | 212k | Vardesc *vd = getlocalvardesc(fs, vidx); |
253 | 212k | if (vd->vd.kind == RDKCTC) |
254 | 0 | return NULL; /* no debug info. for constants */ |
255 | 212k | else { |
256 | 212k | int idx = vd->vd.pidx; |
257 | 212k | lua_assert(idx < fs->ndebugvars); |
258 | 212k | return &fs->f->locvars[idx]; |
259 | 212k | } |
260 | 212k | } |
261 | | |
262 | | |
263 | | /* |
264 | | ** Create an expression representing variable 'vidx' |
265 | | */ |
266 | 219k | static void init_var (FuncState *fs, expdesc *e, int vidx) { |
267 | 219k | e->f = e->t = NO_JUMP; |
268 | 219k | e->k = VLOCAL; |
269 | 219k | e->u.var.vidx = vidx; |
270 | 219k | e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; |
271 | 219k | } |
272 | | |
273 | | |
274 | | /* |
275 | | ** Raises an error if variable described by 'e' is read only |
276 | | */ |
277 | 391k | static void check_readonly (LexState *ls, expdesc *e) { |
278 | 391k | FuncState *fs = ls->fs; |
279 | 391k | TString *varname = NULL; /* to be set if variable is const */ |
280 | 391k | switch (e->k) { |
281 | 0 | case VCONST: { |
282 | 0 | varname = ls->dyd->actvar.arr[e->u.info].vd.name; |
283 | 0 | break; |
284 | 0 | } |
285 | 16.5k | case VLOCAL: { |
286 | 16.5k | Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); |
287 | 16.5k | if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ |
288 | 0 | varname = vardesc->vd.name; |
289 | 16.5k | break; |
290 | 0 | } |
291 | 4.71k | case VUPVAL: { |
292 | 4.71k | Upvaldesc *up = &fs->f->upvalues[e->u.info]; |
293 | 4.71k | if (up->kind != VDKREG) |
294 | 0 | varname = up->name; |
295 | 4.71k | break; |
296 | 0 | } |
297 | 370k | default: |
298 | 370k | return; /* other cases cannot be read-only */ |
299 | 391k | } |
300 | 21.3k | if (varname) { |
301 | 0 | const char *msg = luaO_pushfstring(ls->L, |
302 | 0 | "attempt to assign to const variable '%s'", getstr(varname)); |
303 | 0 | luaK_semerror(ls, msg); /* error */ |
304 | 0 | } |
305 | 21.3k | } |
306 | | |
307 | | |
308 | | /* |
309 | | ** Start the scope for the last 'nvars' created variables. |
310 | | */ |
311 | 120k | static void adjustlocalvars (LexState *ls, int nvars) { |
312 | 120k | FuncState *fs = ls->fs; |
313 | 120k | int reglevel = luaY_nvarstack(fs); |
314 | 120k | int i; |
315 | 359k | for (i = 0; i < nvars; i++) { |
316 | 239k | int vidx = fs->nactvar++; |
317 | 239k | Vardesc *var = getlocalvardesc(fs, vidx); |
318 | 239k | var->vd.ridx = reglevel++; |
319 | 239k | var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); |
320 | 239k | } |
321 | 120k | } |
322 | | |
323 | | |
324 | | /* |
325 | | ** Close the scope for all variables up to level 'tolevel'. |
326 | | ** (debug info.) |
327 | | */ |
328 | 266k | static void removevars (FuncState *fs, int tolevel) { |
329 | 266k | fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); |
330 | 490k | while (fs->nactvar > tolevel) { |
331 | 224k | LocVar *var = localdebuginfo(fs, --fs->nactvar); |
332 | 224k | if (var) /* does it have debug information? */ |
333 | 223k | var->endpc = fs->pc; |
334 | 224k | } |
335 | 266k | } |
336 | | |
337 | | |
338 | | /* |
339 | | ** Search the upvalues of the function 'fs' for one |
340 | | ** with the given 'name'. |
341 | | */ |
342 | 26.2M | static int searchupvalue (FuncState *fs, TString *name) { |
343 | 26.2M | int i; |
344 | 26.2M | Upvaldesc *up = fs->f->upvalues; |
345 | 41.4M | for (i = 0; i < fs->nups; i++) { |
346 | 27.6M | if (eqstr(up[i].name, name)) return i; |
347 | 27.6M | } |
348 | 13.8M | return -1; /* not found */ |
349 | 26.2M | } |
350 | | |
351 | | |
352 | 49.3k | static Upvaldesc *allocupvalue (FuncState *fs) { |
353 | 49.3k | Proto *f = fs->f; |
354 | 49.3k | int oldsize = f->sizeupvalues; |
355 | 49.3k | checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); |
356 | 49.3k | luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, |
357 | 49.3k | Upvaldesc, MAXUPVAL, "upvalues"); |
358 | 229k | while (oldsize < f->sizeupvalues) |
359 | 179k | f->upvalues[oldsize++].name = NULL; |
360 | 49.3k | return &f->upvalues[fs->nups++]; |
361 | 49.3k | } |
362 | | |
363 | | |
364 | 26.2k | static int newupvalue (FuncState *fs, TString *name, expdesc *v) { |
365 | 26.2k | Upvaldesc *up = allocupvalue(fs); |
366 | 26.2k | FuncState *prev = fs->prev; |
367 | 26.2k | if (v->k == VLOCAL) { |
368 | 1.94k | up->instack = 1; |
369 | 1.94k | up->idx = v->u.var.ridx; |
370 | 1.94k | up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; |
371 | 1.94k | lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); |
372 | 1.94k | } |
373 | 24.3k | else { |
374 | 24.3k | up->instack = 0; |
375 | 24.3k | up->idx = cast_byte(v->u.info); |
376 | 24.3k | up->kind = prev->f->upvalues[v->u.info].kind; |
377 | 24.3k | lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); |
378 | 24.3k | } |
379 | 26.2k | up->name = name; |
380 | 26.2k | luaC_objbarrier(fs->ls->L, fs->f, name); |
381 | 0 | return fs->nups - 1; |
382 | 26.2k | } |
383 | | |
384 | | |
385 | | /* |
386 | | ** Look for an active local variable with the name 'n' in the |
387 | | ** function 'fs'. If found, initialize 'var' with it and return |
388 | | ** its expression kind; otherwise return -1. |
389 | | */ |
390 | 26.6M | static int searchvar (FuncState *fs, TString *n, expdesc *var) { |
391 | 26.6M | int i; |
392 | 121M | for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { |
393 | 95.5M | Vardesc *vd = getlocalvardesc(fs, i); |
394 | 95.5M | if (eqstr(n, vd->vd.name)) { /* found? */ |
395 | 434k | if (vd->vd.kind == RDKCTC) /* compile-time constant? */ |
396 | 214k | init_exp(var, VCONST, fs->firstlocal + i); |
397 | 219k | else /* real variable */ |
398 | 219k | init_var(fs, var, i); |
399 | 434k | return var->k; |
400 | 434k | } |
401 | 95.5M | } |
402 | 26.2M | return -1; /* not found */ |
403 | 26.6M | } |
404 | | |
405 | | |
406 | | /* |
407 | | ** Mark block where variable at given level was defined |
408 | | ** (to emit close instructions later). |
409 | | */ |
410 | 3.38k | static void markupval (FuncState *fs, int level) { |
411 | 3.38k | BlockCnt *bl = fs->bl; |
412 | 11.0k | while (bl->nactvar > level) |
413 | 7.67k | bl = bl->previous; |
414 | 3.38k | bl->upval = 1; |
415 | 3.38k | fs->needclose = 1; |
416 | 3.38k | } |
417 | | |
418 | | |
419 | | /* |
420 | | ** Mark that current block has a to-be-closed variable. |
421 | | */ |
422 | 36.1k | static void marktobeclosed (FuncState *fs) { |
423 | 36.1k | BlockCnt *bl = fs->bl; |
424 | 36.1k | bl->upval = 1; |
425 | 36.1k | bl->insidetbc = 1; |
426 | 36.1k | fs->needclose = 1; |
427 | 36.1k | } |
428 | | |
429 | | |
430 | | /* |
431 | | ** Find a variable with the given name 'n'. If it is an upvalue, add |
432 | | ** this upvalue into all intermediate functions. If it is a global, set |
433 | | ** 'var' as 'void' as a flag. |
434 | | */ |
435 | 39.2M | static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { |
436 | 39.2M | if (fs == NULL) /* no more levels? */ |
437 | 12.6M | init_exp(var, VVOID, 0); /* default is global */ |
438 | 26.6M | else { |
439 | 26.6M | int v = searchvar(fs, n, var); /* look up locals at current level */ |
440 | 26.6M | if (v >= 0) { /* found? */ |
441 | 434k | if (v == VLOCAL && !base) |
442 | 3.38k | markupval(fs, var->u.var.vidx); /* local will be used as an upval */ |
443 | 434k | } |
444 | 26.2M | else { /* not found as local at current level; try upvalues */ |
445 | 26.2M | int idx = searchupvalue(fs, n); /* try existing upvalues */ |
446 | 26.2M | if (idx < 0) { /* not found? */ |
447 | 13.8M | singlevaraux(fs->prev, n, var, 0); /* try upper levels */ |
448 | 13.8M | if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ |
449 | 31.8k | idx = newupvalue(fs, n, var); /* will be a new upvalue */ |
450 | 13.7M | else /* it is a global or a constant */ |
451 | 13.7M | return; /* don't need to do anything at this level */ |
452 | 13.8M | } |
453 | 12.4M | init_exp(var, VUPVAL, idx); /* new or old upvalue */ |
454 | 12.4M | } |
455 | 26.6M | } |
456 | 39.2M | } |
457 | | |
458 | | |
459 | | /* |
460 | | ** Find a variable with the given name 'n', handling global variables |
461 | | ** too. |
462 | | */ |
463 | 862k | static void singlevar (LexState *ls, expdesc *var) { |
464 | 862k | TString *varname = str_checkname(ls); |
465 | 862k | FuncState *fs = ls->fs; |
466 | 862k | singlevaraux(fs, varname, var, 1); |
467 | 862k | if (var->k == VVOID) { /* global name? */ |
468 | 779k | expdesc key; |
469 | 779k | singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ |
470 | 779k | lua_assert(var->k != VVOID); /* this one must exist */ |
471 | 779k | luaK_exp2anyregup(fs, var); /* but could be a constant */ |
472 | 779k | codestring(&key, varname); /* key is variable name */ |
473 | 779k | luaK_indexed(fs, var, &key); /* env[varname] */ |
474 | 779k | } |
475 | 862k | } |
476 | | |
477 | | |
478 | | /* |
479 | | ** Adjust the number of results from an expression list 'e' with 'nexps' |
480 | | ** expressions to 'nvars' values. |
481 | | */ |
482 | 53.7k | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { |
483 | 53.7k | FuncState *fs = ls->fs; |
484 | 53.7k | int needed = nvars - nexps; /* extra values needed */ |
485 | 53.7k | if (hasmultret(e->k)) { /* last expression has multiple returns? */ |
486 | 938 | int extra = needed + 1; /* discount last expression itself */ |
487 | 938 | if (extra < 0) |
488 | 253 | extra = 0; |
489 | 938 | luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ |
490 | 938 | } |
491 | 52.8k | else { |
492 | 52.8k | if (e->k != VVOID) /* at least one expression? */ |
493 | 49.1k | luaK_exp2nextreg(fs, e); /* close last expression */ |
494 | 52.8k | if (needed > 0) /* missing values? */ |
495 | 42.3k | luaK_nil(fs, fs->freereg, needed); /* complete with nils */ |
496 | 52.8k | } |
497 | 53.7k | if (needed > 0) |
498 | 42.8k | luaK_reserveregs(fs, needed); /* registers for extra values */ |
499 | 10.8k | else /* adding 'needed' is actually a subtraction */ |
500 | 10.8k | fs->freereg += needed; /* remove extra values */ |
501 | 53.7k | } |
502 | | |
503 | | |
504 | 15.0M | #define enterlevel(ls) luaE_incCstack(ls->L) |
505 | | |
506 | | |
507 | 15.0M | #define leavelevel(ls) ((ls)->L->nCcalls--) |
508 | | |
509 | | |
510 | | /* |
511 | | ** Generates an error that a goto jumps into the scope of some |
512 | | ** local variable. |
513 | | */ |
514 | 0 | static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { |
515 | 0 | const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); |
516 | 0 | const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'"; |
517 | 0 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); |
518 | 0 | luaK_semerror(ls, msg); /* raise the error */ |
519 | 0 | } |
520 | | |
521 | | |
522 | | /* |
523 | | ** Solves the goto at index 'g' to given 'label' and removes it |
524 | | ** from the list of pending gotos. |
525 | | ** If it jumps into the scope of some variable, raises an error. |
526 | | */ |
527 | 54.0k | static void solvegoto (LexState *ls, int g, Labeldesc *label) { |
528 | 54.0k | int i; |
529 | 54.0k | Labellist *gl = &ls->dyd->gt; /* list of gotos */ |
530 | 54.0k | Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ |
531 | 54.0k | lua_assert(eqstr(gt->name, label->name)); |
532 | 54.0k | if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ |
533 | 0 | jumpscopeerror(ls, gt); |
534 | 54.0k | luaK_patchlist(ls->fs, gt->pc, label->pc); |
535 | 65.4k | for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ |
536 | 11.4k | gl->arr[i] = gl->arr[i + 1]; |
537 | 54.0k | gl->n--; |
538 | 54.0k | } |
539 | | |
540 | | |
541 | | /* |
542 | | ** Search for an active label with the given name. |
543 | | */ |
544 | 7.18k | static Labeldesc *findlabel (LexState *ls, TString *name) { |
545 | 7.18k | int i; |
546 | 7.18k | Dyndata *dyd = ls->dyd; |
547 | | /* check labels in current function for a match */ |
548 | 8.44k | for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { |
549 | 7.24k | Labeldesc *lb = &dyd->label.arr[i]; |
550 | 7.24k | if (eqstr(lb->name, name)) /* correct label? */ |
551 | 5.97k | return lb; |
552 | 7.24k | } |
553 | 1.20k | return NULL; /* label not found */ |
554 | 7.18k | } |
555 | | |
556 | | |
557 | | /* |
558 | | ** Adds a new label/goto in the corresponding list. |
559 | | */ |
560 | | static int newlabelentry (LexState *ls, Labellist *l, TString *name, |
561 | 114k | int line, int pc) { |
562 | 114k | int n = l->n; |
563 | 114k | luaM_growvector(ls->L, l->arr, n, l->size, |
564 | 114k | Labeldesc, SHRT_MAX, "labels/gotos"); |
565 | 114k | l->arr[n].name = name; |
566 | 114k | l->arr[n].line = line; |
567 | 114k | l->arr[n].nactvar = ls->fs->nactvar; |
568 | 114k | l->arr[n].close = 0; |
569 | 114k | l->arr[n].pc = pc; |
570 | 114k | l->n = n + 1; |
571 | 114k | return n; |
572 | 114k | } |
573 | | |
574 | | |
575 | 59.0k | static int newgotoentry (LexState *ls, TString *name, int line, int pc) { |
576 | 59.0k | return newlabelentry(ls, &ls->dyd->gt, name, line, pc); |
577 | 59.0k | } |
578 | | |
579 | | |
580 | | /* |
581 | | ** Solves forward jumps. Check whether new label 'lb' matches any |
582 | | ** pending gotos in current block and solves them. Return true |
583 | | ** if any of the gotos need to close upvalues. |
584 | | */ |
585 | 55.1k | static int solvegotos (LexState *ls, Labeldesc *lb) { |
586 | 55.1k | Labellist *gl = &ls->dyd->gt; |
587 | 55.1k | int i = ls->fs->bl->firstgoto; |
588 | 55.1k | int needsclose = 0; |
589 | 110k | while (i < gl->n) { |
590 | 55.1k | if (eqstr(gl->arr[i].name, lb->name)) { |
591 | 55.1k | needsclose |= gl->arr[i].close; |
592 | 55.1k | solvegoto(ls, i, lb); /* will remove 'i' from the list */ |
593 | 55.1k | } |
594 | 6 | else |
595 | 6 | i++; |
596 | 55.1k | } |
597 | 55.1k | return needsclose; |
598 | 55.1k | } |
599 | | |
600 | | |
601 | | /* |
602 | | ** Create a new label with the given 'name' at the given 'line'. |
603 | | ** 'last' tells whether label is the last non-op statement in its |
604 | | ** block. Solves all pending gotos to this new label and adds |
605 | | ** a close instruction if necessary. |
606 | | ** Returns true iff it added a close instruction. |
607 | | */ |
608 | | static int createlabel (LexState *ls, TString *name, int line, |
609 | 55.1k | int last) { |
610 | 55.1k | FuncState *fs = ls->fs; |
611 | 55.1k | Labellist *ll = &ls->dyd->label; |
612 | 55.1k | int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); |
613 | 55.1k | if (last) { /* label is last no-op statement in the block? */ |
614 | | /* assume that locals are already out of scope */ |
615 | 8 | ll->arr[l].nactvar = fs->bl->nactvar; |
616 | 8 | } |
617 | 55.1k | if (solvegotos(ls, &ll->arr[l])) { /* need close? */ |
618 | 465 | luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); |
619 | 465 | return 1; |
620 | 465 | } |
621 | 54.6k | return 0; |
622 | 55.1k | } |
623 | | |
624 | | |
625 | | /* |
626 | | ** Adjust pending gotos to outer level of a block. |
627 | | */ |
628 | 236k | static void movegotosout (FuncState *fs, BlockCnt *bl) { |
629 | 236k | int i; |
630 | 236k | Labellist *gl = &fs->ls->dyd->gt; |
631 | | /* correct pending gotos to current block */ |
632 | 389k | for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ |
633 | 153k | Labeldesc *gt = &gl->arr[i]; |
634 | | /* leaving a variable scope? */ |
635 | 153k | if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar)) |
636 | 39.1k | gt->close |= bl->upval; /* jump may need a close */ |
637 | 153k | gt->nactvar = bl->nactvar; /* update goto level */ |
638 | 153k | } |
639 | 236k | } |
640 | | |
641 | | |
642 | 273k | static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { |
643 | 273k | bl->isloop = isloop; |
644 | 273k | bl->nactvar = fs->nactvar; |
645 | 273k | bl->firstlabel = fs->ls->dyd->label.n; |
646 | 273k | bl->firstgoto = fs->ls->dyd->gt.n; |
647 | 273k | bl->upval = 0; |
648 | 273k | bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); |
649 | 273k | bl->previous = fs->bl; |
650 | 273k | fs->bl = bl; |
651 | 273k | lua_assert(fs->freereg == luaY_nvarstack(fs)); |
652 | 273k | } |
653 | | |
654 | | |
655 | | /* |
656 | | ** generates an error for an undefined 'goto'. |
657 | | */ |
658 | 104 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { |
659 | 104 | const char *msg; |
660 | 104 | if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { |
661 | 104 | msg = "break outside loop at line %d"; |
662 | 104 | msg = luaO_pushfstring(ls->L, msg, gt->line); |
663 | 104 | } |
664 | 0 | else { |
665 | 0 | msg = "no visible label '%s' for <goto> at line %d"; |
666 | 0 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); |
667 | 0 | } |
668 | 104 | luaK_semerror(ls, msg); |
669 | 104 | } |
670 | | |
671 | | |
672 | 256k | static void leaveblock (FuncState *fs) { |
673 | 256k | BlockCnt *bl = fs->bl; |
674 | 256k | LexState *ls = fs->ls; |
675 | 256k | int hasclose = 0; |
676 | 256k | int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ |
677 | 256k | removevars(fs, bl->nactvar); /* remove block locals */ |
678 | 256k | lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ |
679 | 256k | if (bl->isloop) /* has to fix pending breaks? */ |
680 | 52.9k | hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); |
681 | 256k | if (!hasclose && bl->previous && bl->upval) /* still need a 'close'? */ |
682 | 36.5k | luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); |
683 | 256k | fs->freereg = stklevel; /* free registers */ |
684 | 256k | ls->dyd->label.n = bl->firstlabel; /* remove local labels */ |
685 | 256k | fs->bl = bl->previous; /* current block now is previous one */ |
686 | 256k | if (bl->previous) /* was it a nested block? */ |
687 | 228k | movegotosout(fs, bl); /* update pending gotos to enclosing block */ |
688 | 28.0k | else { |
689 | 28.0k | if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ |
690 | 104 | undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ |
691 | 28.0k | } |
692 | 256k | } |
693 | | |
694 | | |
695 | | /* |
696 | | ** adds a new prototype into list of prototypes |
697 | | */ |
698 | 26.2k | static Proto *addprototype (LexState *ls) { |
699 | 26.2k | Proto *clp; |
700 | 26.2k | lua_State *L = ls->L; |
701 | 26.2k | FuncState *fs = ls->fs; |
702 | 26.2k | Proto *f = fs->f; /* prototype of current function */ |
703 | 26.2k | if (fs->np >= f->sizep) { |
704 | 7.27k | int oldsize = f->sizep; |
705 | 7.27k | luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); |
706 | 53.1k | while (oldsize < f->sizep) |
707 | 45.8k | f->p[oldsize++] = NULL; |
708 | 7.27k | } |
709 | 26.2k | f->p[fs->np++] = clp = luaF_newproto(L); |
710 | 26.2k | luaC_objbarrier(L, f, clp); |
711 | 0 | return clp; |
712 | 26.2k | } |
713 | | |
714 | | |
715 | | /* |
716 | | ** codes instruction to create new closure in parent function. |
717 | | ** The OP_CLOSURE instruction uses the last available register, |
718 | | ** so that, if it invokes the GC, the GC knows which registers |
719 | | ** are in use at that time. |
720 | | |
721 | | */ |
722 | 27.9k | static void codeclosure (LexState *ls, expdesc *v) { |
723 | 27.9k | FuncState *fs = ls->fs->prev; |
724 | 27.9k | init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); |
725 | 27.9k | luaK_exp2nextreg(fs, v); /* fix it at the last register */ |
726 | 27.9k | } |
727 | | |
728 | | |
729 | 43.4k | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { |
730 | 43.4k | Proto *f = fs->f; |
731 | 43.4k | fs->prev = ls->fs; /* linked list of funcstates */ |
732 | 43.4k | fs->ls = ls; |
733 | 43.4k | ls->fs = fs; |
734 | 43.4k | fs->pc = 0; |
735 | 43.4k | fs->previousline = f->linedefined; |
736 | 43.4k | fs->iwthabs = 0; |
737 | 43.4k | fs->lasttarget = 0; |
738 | 43.4k | fs->freereg = 0; |
739 | 43.4k | fs->nk = 0; |
740 | 43.4k | fs->nabslineinfo = 0; |
741 | 43.4k | fs->np = 0; |
742 | 43.4k | fs->nups = 0; |
743 | 43.4k | fs->ndebugvars = 0; |
744 | 43.4k | fs->nactvar = 0; |
745 | 43.4k | fs->needclose = 0; |
746 | 43.4k | fs->firstlocal = ls->dyd->actvar.n; |
747 | 43.4k | fs->firstlabel = ls->dyd->label.n; |
748 | 43.4k | fs->bl = NULL; |
749 | 43.4k | f->source = ls->source; |
750 | 43.4k | luaC_objbarrier(ls->L, f, f->source); |
751 | 0 | f->maxstacksize = 2; /* registers 0/1 are always valid */ |
752 | 43.4k | enterblock(fs, bl, 0); |
753 | 43.4k | } |
754 | | |
755 | | |
756 | 28.0k | static void close_func (LexState *ls) { |
757 | 28.0k | lua_State *L = ls->L; |
758 | 28.0k | FuncState *fs = ls->fs; |
759 | 28.0k | Proto *f = fs->f; |
760 | 28.0k | luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ |
761 | 28.0k | leaveblock(fs); |
762 | 28.0k | lua_assert(fs->bl == NULL); |
763 | 27.9k | luaK_finish(fs); |
764 | 27.9k | luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); |
765 | 27.9k | luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); |
766 | 27.9k | luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, |
767 | 27.9k | fs->nabslineinfo, AbsLineInfo); |
768 | 27.9k | luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); |
769 | 27.9k | luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); |
770 | 27.9k | luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); |
771 | 27.9k | luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); |
772 | 27.9k | ls->fs = fs->prev; |
773 | 27.9k | luaC_checkGC(L); |
774 | 27.9k | } |
775 | | |
776 | | |
777 | | |
778 | | /*============================================================*/ |
779 | | /* GRAMMAR RULES */ |
780 | | /*============================================================*/ |
781 | | |
782 | | |
783 | | /* |
784 | | ** check whether current token is in the follow set of a block. |
785 | | ** 'until' closes syntactical blocks, but do not close scope, |
786 | | ** so it is handled in separate. |
787 | | */ |
788 | 1.00M | static int block_follow (LexState *ls, int withuntil) { |
789 | 1.00M | switch (ls->t.token) { |
790 | 3.12k | case TK_ELSE: case TK_ELSEIF: |
791 | 158k | case TK_END: case TK_EOS: |
792 | 158k | return 1; |
793 | 6.42k | case TK_UNTIL: return withuntil; |
794 | 840k | default: return 0; |
795 | 1.00M | } |
796 | 1.00M | } |
797 | | |
798 | | |
799 | 137k | static void statlist (LexState *ls) { |
800 | | /* statlist -> { stat [';'] } */ |
801 | 936k | while (!block_follow(ls, 1)) { |
802 | 831k | if (ls->t.token == TK_RETURN) { |
803 | 32.4k | statement(ls); |
804 | 32.4k | return; /* 'return' must be last statement */ |
805 | 32.4k | } |
806 | 799k | statement(ls); |
807 | 799k | } |
808 | 137k | } |
809 | | |
810 | | |
811 | 12.0k | static void fieldsel (LexState *ls, expdesc *v) { |
812 | | /* fieldsel -> ['.' | ':'] NAME */ |
813 | 12.0k | FuncState *fs = ls->fs; |
814 | 12.0k | expdesc key; |
815 | 12.0k | luaK_exp2anyregup(fs, v); |
816 | 12.0k | luaX_next(ls); /* skip the dot or colon */ |
817 | 12.0k | codename(ls, &key); |
818 | 12.0k | luaK_indexed(fs, v, &key); |
819 | 12.0k | } |
820 | | |
821 | | |
822 | 37.8k | static void yindex (LexState *ls, expdesc *v) { |
823 | | /* index -> '[' expr ']' */ |
824 | 37.8k | luaX_next(ls); /* skip the '[' */ |
825 | 37.8k | expr(ls, v); |
826 | 37.8k | luaK_exp2val(ls->fs, v); |
827 | 37.8k | checknext(ls, ']'); |
828 | 37.8k | } |
829 | | |
830 | | |
831 | | /* |
832 | | ** {====================================================================== |
833 | | ** Rules for Constructors |
834 | | ** ======================================================================= |
835 | | */ |
836 | | |
837 | | |
838 | | typedef struct ConsControl { |
839 | | expdesc v; /* last list item read */ |
840 | | expdesc *t; /* table descriptor */ |
841 | | int nh; /* total number of 'record' elements */ |
842 | | int na; /* number of array elements already stored */ |
843 | | int tostore; /* number of array elements pending to be stored */ |
844 | | } ConsControl; |
845 | | |
846 | | |
847 | 12.5k | static void recfield (LexState *ls, ConsControl *cc) { |
848 | | /* recfield -> (NAME | '['exp']') = exp */ |
849 | 12.5k | FuncState *fs = ls->fs; |
850 | 12.5k | int reg = ls->fs->freereg; |
851 | 12.5k | expdesc tab, key, val; |
852 | 12.5k | if (ls->t.token == TK_NAME) { |
853 | 7.18k | checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); |
854 | 7.18k | codename(ls, &key); |
855 | 7.18k | } |
856 | 5.40k | else /* ls->t.token == '[' */ |
857 | 5.40k | yindex(ls, &key); |
858 | 12.5k | cc->nh++; |
859 | 12.5k | checknext(ls, '='); |
860 | 12.5k | tab = *cc->t; |
861 | 12.5k | luaK_indexed(fs, &tab, &key); |
862 | 12.5k | expr(ls, &val); |
863 | 12.5k | luaK_storevar(fs, &tab, &val); |
864 | 12.5k | fs->freereg = reg; /* free registers */ |
865 | 12.5k | } |
866 | | |
867 | | |
868 | 233k | static void closelistfield (FuncState *fs, ConsControl *cc) { |
869 | 233k | if (cc->v.k == VVOID) return; /* there is no list item */ |
870 | 210k | luaK_exp2nextreg(fs, &cc->v); |
871 | 210k | cc->v.k = VVOID; |
872 | 210k | if (cc->tostore == LFIELDS_PER_FLUSH) { |
873 | 3.93k | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ |
874 | 3.93k | cc->na += cc->tostore; |
875 | 3.93k | cc->tostore = 0; /* no more items pending */ |
876 | 3.93k | } |
877 | 210k | } |
878 | | |
879 | | |
880 | 32.7k | static void lastlistfield (FuncState *fs, ConsControl *cc) { |
881 | 32.7k | if (cc->tostore == 0) return; |
882 | 10.9k | if (hasmultret(cc->v.k)) { |
883 | 501 | luaK_setmultret(fs, &cc->v); |
884 | 501 | luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); |
885 | 501 | cc->na--; /* do not count last expression (unknown number of elements) */ |
886 | 501 | } |
887 | 10.4k | else { |
888 | 10.4k | if (cc->v.k != VVOID) |
889 | 9.21k | luaK_exp2nextreg(fs, &cc->v); |
890 | 10.4k | luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); |
891 | 10.4k | } |
892 | 10.9k | cc->na += cc->tostore; |
893 | 10.9k | } |
894 | | |
895 | | |
896 | 220k | static void listfield (LexState *ls, ConsControl *cc) { |
897 | | /* listfield -> exp */ |
898 | 220k | expr(ls, &cc->v); |
899 | 220k | cc->tostore++; |
900 | 220k | } |
901 | | |
902 | | |
903 | 233k | static void field (LexState *ls, ConsControl *cc) { |
904 | | /* field -> listfield | recfield */ |
905 | 233k | switch(ls->t.token) { |
906 | 221k | case TK_NAME: { /* may be 'listfield' or 'recfield' */ |
907 | 221k | if (luaX_lookahead(ls) != '=') /* expression? */ |
908 | 214k | listfield(ls, cc); |
909 | 7.18k | else |
910 | 7.18k | recfield(ls, cc); |
911 | 221k | break; |
912 | 0 | } |
913 | 5.40k | case '[': { |
914 | 5.40k | recfield(ls, cc); |
915 | 5.40k | break; |
916 | 0 | } |
917 | 6.46k | default: { |
918 | 6.46k | listfield(ls, cc); |
919 | 6.46k | break; |
920 | 0 | } |
921 | 233k | } |
922 | 233k | } |
923 | | |
924 | | |
925 | 32.1k | static void constructor (LexState *ls, expdesc *t) { |
926 | | /* constructor -> '{' [ field { sep field } [sep] ] '}' |
927 | | sep -> ',' | ';' */ |
928 | 32.1k | FuncState *fs = ls->fs; |
929 | 32.1k | int line = ls->linenumber; |
930 | 32.1k | int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); |
931 | 32.1k | ConsControl cc; |
932 | 32.1k | luaK_code(fs, 0); /* space for extra arg. */ |
933 | 32.1k | cc.na = cc.nh = cc.tostore = 0; |
934 | 32.1k | cc.t = t; |
935 | 32.1k | init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ |
936 | 32.1k | luaK_reserveregs(fs, 1); |
937 | 32.1k | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
938 | 32.1k | checknext(ls, '{'); |
939 | 247k | do { |
940 | 247k | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
941 | 247k | if (ls->t.token == '}') break; |
942 | 230k | closelistfield(fs, &cc); |
943 | 230k | field(ls, &cc); |
944 | 230k | } while (testnext(ls, ',') || testnext(ls, ';')); |
945 | 32.1k | check_match(ls, '}', '{', line); |
946 | 32.1k | lastlistfield(fs, &cc); |
947 | 32.1k | luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); |
948 | 32.1k | } |
949 | | |
950 | | /* }====================================================================== */ |
951 | | |
952 | | |
953 | 19.4k | static void setvararg (FuncState *fs, int nparams) { |
954 | 19.4k | fs->f->is_vararg = 1; |
955 | 19.4k | luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); |
956 | 19.4k | } |
957 | | |
958 | | |
959 | 29.2k | static void parlist (LexState *ls) { |
960 | | /* parlist -> [ {NAME ','} (NAME | '...') ] */ |
961 | 29.2k | FuncState *fs = ls->fs; |
962 | 29.2k | Proto *f = fs->f; |
963 | 29.2k | int nparams = 0; |
964 | 29.2k | int isvararg = 0; |
965 | 29.2k | if (ls->t.token != ')') { /* is 'parlist' not empty? */ |
966 | 17.7k | do { |
967 | 17.7k | switch (ls->t.token) { |
968 | 15.7k | case TK_NAME: { |
969 | 15.7k | new_localvar(ls, str_checkname(ls)); |
970 | 15.7k | nparams++; |
971 | 15.7k | break; |
972 | 0 | } |
973 | 1.92k | case TK_DOTS: { |
974 | 1.92k | luaX_next(ls); |
975 | 1.92k | isvararg = 1; |
976 | 1.92k | break; |
977 | 0 | } |
978 | 0 | default: luaX_syntaxerror(ls, "<name> or '...' expected"); |
979 | 17.7k | } |
980 | 17.7k | } while (!isvararg && testnext(ls, ',')); |
981 | 5.84k | } |
982 | 29.2k | adjustlocalvars(ls, nparams); |
983 | 29.2k | f->numparams = cast_byte(fs->nactvar); |
984 | 29.2k | if (isvararg) |
985 | 1.92k | setvararg(fs, f->numparams); /* declared vararg */ |
986 | 29.2k | luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ |
987 | 29.2k | } |
988 | | |
989 | | |
990 | 29.2k | static void body (LexState *ls, expdesc *e, int ismethod, int line) { |
991 | | /* body -> '(' parlist ')' block END */ |
992 | 29.2k | FuncState new_fs; |
993 | 29.2k | BlockCnt bl; |
994 | 29.2k | new_fs.f = addprototype(ls); |
995 | 29.2k | new_fs.f->linedefined = line; |
996 | 29.2k | open_func(ls, &new_fs, &bl); |
997 | 29.2k | checknext(ls, '('); |
998 | 29.2k | if (ismethod) { |
999 | 244 | new_localvarliteral(ls, "self"); /* create 'self' parameter */ |
1000 | 244 | adjustlocalvars(ls, 1); |
1001 | 244 | } |
1002 | 29.2k | parlist(ls); |
1003 | 29.2k | checknext(ls, ')'); |
1004 | 29.2k | statlist(ls); |
1005 | 29.2k | new_fs.f->lastlinedefined = ls->linenumber; |
1006 | 29.2k | check_match(ls, TK_END, TK_FUNCTION, line); |
1007 | 29.2k | codeclosure(ls, e); |
1008 | 29.2k | close_func(ls); |
1009 | 29.2k | } |
1010 | | |
1011 | | |
1012 | 444k | static int explist (LexState *ls, expdesc *v) { |
1013 | | /* explist -> expr { ',' expr } */ |
1014 | 444k | int n = 1; /* at least one expression */ |
1015 | 444k | expr(ls, v); |
1016 | 532k | while (testnext(ls, ',')) { |
1017 | 88.3k | luaK_exp2nextreg(ls->fs, v); |
1018 | 88.3k | expr(ls, v); |
1019 | 88.3k | n++; |
1020 | 88.3k | } |
1021 | 444k | return n; |
1022 | 444k | } |
1023 | | |
1024 | | |
1025 | 369k | static void funcargs (LexState *ls, expdesc *f, int line) { |
1026 | 369k | FuncState *fs = ls->fs; |
1027 | 369k | expdesc args; |
1028 | 369k | int base, nparams; |
1029 | 369k | switch (ls->t.token) { |
1030 | 13.1k | case '(': { /* funcargs -> '(' [ explist ] ')' */ |
1031 | 13.1k | luaX_next(ls); |
1032 | 13.1k | if (ls->t.token == ')') /* arg list is empty? */ |
1033 | 846 | args.k = VVOID; |
1034 | 12.3k | else { |
1035 | 12.3k | explist(ls, &args); |
1036 | 12.3k | if (hasmultret(args.k)) |
1037 | 2.14k | luaK_setmultret(fs, &args); |
1038 | 12.3k | } |
1039 | 13.1k | check_match(ls, ')', '(', line); |
1040 | 13.1k | break; |
1041 | 0 | } |
1042 | 18.7k | case '{': { /* funcargs -> constructor */ |
1043 | 18.7k | constructor(ls, &args); |
1044 | 18.7k | break; |
1045 | 0 | } |
1046 | 337k | case TK_STRING: { /* funcargs -> STRING */ |
1047 | 337k | codestring(&args, ls->t.seminfo.ts); |
1048 | 337k | luaX_next(ls); /* must use 'seminfo' before 'next' */ |
1049 | 337k | break; |
1050 | 0 | } |
1051 | 0 | default: { |
1052 | 0 | luaX_syntaxerror(ls, "function arguments expected"); |
1053 | 0 | } |
1054 | 369k | } |
1055 | 362k | lua_assert(f->k == VNONRELOC); |
1056 | 362k | base = f->u.info; /* base register for call */ |
1057 | 362k | if (hasmultret(args.k)) |
1058 | 2.14k | nparams = LUA_MULTRET; /* open call */ |
1059 | 360k | else { |
1060 | 360k | if (args.k != VVOID) |
1061 | 359k | luaK_exp2nextreg(fs, &args); /* close last argument */ |
1062 | 360k | nparams = fs->freereg - (base+1); |
1063 | 360k | } |
1064 | 362k | init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); |
1065 | 362k | luaK_fixline(fs, line); |
1066 | 362k | fs->freereg = base+1; /* call remove function and arguments and leaves |
1067 | | (unless changed) one result */ |
1068 | 362k | } |
1069 | | |
1070 | | |
1071 | | |
1072 | | |
1073 | | /* |
1074 | | ** {====================================================================== |
1075 | | ** Expression parsing |
1076 | | ** ======================================================================= |
1077 | | */ |
1078 | | |
1079 | | |
1080 | 12.8M | static void primaryexp (LexState *ls, expdesc *v) { |
1081 | | /* primaryexp -> NAME | '(' expr ')' */ |
1082 | 12.8M | switch (ls->t.token) { |
1083 | 64.0k | case '(': { |
1084 | 64.0k | int line = ls->linenumber; |
1085 | 64.0k | luaX_next(ls); |
1086 | 64.0k | expr(ls, v); |
1087 | 64.0k | check_match(ls, ')', '(', line); |
1088 | 64.0k | luaK_dischargevars(ls->fs, v); |
1089 | 64.0k | return; |
1090 | 0 | } |
1091 | 12.8M | case TK_NAME: { |
1092 | 12.8M | singlevar(ls, v); |
1093 | 12.8M | return; |
1094 | 0 | } |
1095 | 1.36k | default: { |
1096 | 1.36k | luaX_syntaxerror(ls, "unexpected symbol"); |
1097 | 0 | } |
1098 | 12.8M | } |
1099 | 12.8M | } |
1100 | | |
1101 | | |
1102 | 12.8M | static void suffixedexp (LexState *ls, expdesc *v) { |
1103 | | /* suffixedexp -> |
1104 | | primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ |
1105 | 12.8M | FuncState *fs = ls->fs; |
1106 | 12.8M | int line = ls->linenumber; |
1107 | 12.8M | primaryexp(ls, v); |
1108 | 13.4M | for (;;) { |
1109 | 13.4M | switch (ls->t.token) { |
1110 | 10.7k | case '.': { /* fieldsel */ |
1111 | 10.7k | fieldsel(ls, v); |
1112 | 10.7k | break; |
1113 | 0 | } |
1114 | 32.4k | case '[': { /* '[' exp ']' */ |
1115 | 32.4k | expdesc key; |
1116 | 32.4k | luaK_exp2anyregup(fs, v); |
1117 | 32.4k | yindex(ls, &key); |
1118 | 32.4k | luaK_indexed(fs, v, &key); |
1119 | 32.4k | break; |
1120 | 0 | } |
1121 | 32.4k | case ':': { /* ':' NAME funcargs */ |
1122 | 32.4k | expdesc key; |
1123 | 32.4k | luaX_next(ls); |
1124 | 32.4k | codename(ls, &key); |
1125 | 32.4k | luaK_self(fs, v, &key); |
1126 | 32.4k | funcargs(ls, v, line); |
1127 | 32.4k | break; |
1128 | 0 | } |
1129 | 533k | case '(': case TK_STRING: case '{': { /* funcargs */ |
1130 | 533k | luaK_exp2nextreg(fs, v); |
1131 | 533k | funcargs(ls, v, line); |
1132 | 533k | break; |
1133 | 524k | } |
1134 | 12.8M | default: return; |
1135 | 13.4M | } |
1136 | 13.4M | } |
1137 | 12.8M | } |
1138 | | |
1139 | | |
1140 | 13.9M | static void simpleexp (LexState *ls, expdesc *v) { |
1141 | | /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | |
1142 | | constructor | FUNCTION body | suffixedexp */ |
1143 | 13.9M | switch (ls->t.token) { |
1144 | 153k | case TK_FLT: { |
1145 | 153k | init_exp(v, VKFLT, 0); |
1146 | 153k | v->u.nval = ls->t.seminfo.r; |
1147 | 153k | break; |
1148 | 0 | } |
1149 | 1.03M | case TK_INT: { |
1150 | 1.03M | init_exp(v, VKINT, 0); |
1151 | 1.03M | v->u.ival = ls->t.seminfo.i; |
1152 | 1.03M | break; |
1153 | 0 | } |
1154 | 137k | case TK_STRING: { |
1155 | 137k | codestring(v, ls->t.seminfo.ts); |
1156 | 137k | break; |
1157 | 0 | } |
1158 | 81.3k | case TK_NIL: { |
1159 | 81.3k | init_exp(v, VNIL, 0); |
1160 | 81.3k | break; |
1161 | 0 | } |
1162 | 9.03k | case TK_TRUE: { |
1163 | 9.03k | init_exp(v, VTRUE, 0); |
1164 | 9.03k | break; |
1165 | 0 | } |
1166 | 3.18k | case TK_FALSE: { |
1167 | 3.18k | init_exp(v, VFALSE, 0); |
1168 | 3.18k | break; |
1169 | 0 | } |
1170 | 5.15k | case TK_DOTS: { /* vararg */ |
1171 | 5.15k | FuncState *fs = ls->fs; |
1172 | 5.15k | check_condition(ls, fs->f->is_vararg, |
1173 | 5.15k | "cannot use '...' outside a vararg function"); |
1174 | 5.12k | init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); |
1175 | 5.12k | break; |
1176 | 5.15k | } |
1177 | 14.9k | case '{': { /* constructor */ |
1178 | 14.9k | constructor(ls, v); |
1179 | 14.9k | return; |
1180 | 5.15k | } |
1181 | 20.5k | case TK_FUNCTION: { |
1182 | 20.5k | luaX_next(ls); |
1183 | 20.5k | body(ls, v, 0, ls->linenumber); |
1184 | 20.5k | return; |
1185 | 5.15k | } |
1186 | 12.4M | default: { |
1187 | 12.4M | suffixedexp(ls, v); |
1188 | 12.4M | return; |
1189 | 5.15k | } |
1190 | 13.9M | } |
1191 | 1.42M | luaX_next(ls); |
1192 | 1.42M | } |
1193 | | |
1194 | | |
1195 | 14.2M | static UnOpr getunopr (int op) { |
1196 | 14.2M | switch (op) { |
1197 | 12.2k | case TK_NOT: return OPR_NOT; |
1198 | 172k | case '-': return OPR_MINUS; |
1199 | 130k | case '~': return OPR_BNOT; |
1200 | 43.8k | case '#': return OPR_LEN; |
1201 | 13.9M | default: return OPR_NOUNOPR; |
1202 | 14.2M | } |
1203 | 14.2M | } |
1204 | | |
1205 | | |
1206 | 14.2M | static BinOpr getbinopr (int op) { |
1207 | 14.2M | switch (op) { |
1208 | 90.2k | case '+': return OPR_ADD; |
1209 | 106k | case '-': return OPR_SUB; |
1210 | 94.6k | case '*': return OPR_MUL; |
1211 | 37.9k | case '%': return OPR_MOD; |
1212 | 479k | case '^': return OPR_POW; |
1213 | 5.78M | case '/': return OPR_DIV; |
1214 | 62.2k | case TK_IDIV: return OPR_IDIV; |
1215 | 7.55k | case '&': return OPR_BAND; |
1216 | 32.9k | case '|': return OPR_BOR; |
1217 | 131k | case '~': return OPR_BXOR; |
1218 | 503k | case TK_SHL: return OPR_SHL; |
1219 | 28.5k | case TK_SHR: return OPR_SHR; |
1220 | 59.6k | case TK_CONCAT: return OPR_CONCAT; |
1221 | 5.89k | case TK_NE: return OPR_NE; |
1222 | 15.9k | case TK_EQ: return OPR_EQ; |
1223 | 5.47M | case '<': return OPR_LT; |
1224 | 5.00k | case TK_LE: return OPR_LE; |
1225 | 142k | case '>': return OPR_GT; |
1226 | 4.52k | case TK_GE: return OPR_GE; |
1227 | 20.5k | case TK_AND: return OPR_AND; |
1228 | 167k | case TK_OR: return OPR_OR; |
1229 | 993k | default: return OPR_NOBINOPR; |
1230 | 14.2M | } |
1231 | 14.2M | } |
1232 | | |
1233 | | |
1234 | | /* |
1235 | | ** Priority table for binary operators. |
1236 | | */ |
1237 | | static const struct { |
1238 | | lu_byte left; /* left priority for each binary operator */ |
1239 | | lu_byte right; /* right priority */ |
1240 | | } priority[] = { /* ORDER OPR */ |
1241 | | {10, 10}, {10, 10}, /* '+' '-' */ |
1242 | | {11, 11}, {11, 11}, /* '*' '%' */ |
1243 | | {14, 13}, /* '^' (right associative) */ |
1244 | | {11, 11}, {11, 11}, /* '/' '//' */ |
1245 | | {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ |
1246 | | {7, 7}, {7, 7}, /* '<<' '>>' */ |
1247 | | {9, 8}, /* '..' (right associative) */ |
1248 | | {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ |
1249 | | {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ |
1250 | | {2, 2}, {1, 1} /* and, or */ |
1251 | | }; |
1252 | | |
1253 | 359k | #define UNARY_PRIORITY 12 /* priority for unary operators */ |
1254 | | |
1255 | | |
1256 | | /* |
1257 | | ** subexpr -> (simpleexp | unop subexpr) { binop subexpr } |
1258 | | ** where 'binop' is any binary operator with a priority higher than 'limit' |
1259 | | */ |
1260 | 14.2M | static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { |
1261 | 14.2M | BinOpr op; |
1262 | 14.2M | UnOpr uop; |
1263 | 14.2M | enterlevel(ls); |
1264 | 14.2M | uop = getunopr(ls->t.token); |
1265 | 14.2M | if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ |
1266 | 359k | int line = ls->linenumber; |
1267 | 359k | luaX_next(ls); /* skip operator */ |
1268 | 359k | subexpr(ls, v, UNARY_PRIORITY); |
1269 | 359k | luaK_prefix(ls->fs, uop, v, line); |
1270 | 359k | } |
1271 | 13.9M | else simpleexp(ls, v); |
1272 | | /* expand while operators have priorities higher than 'limit' */ |
1273 | 14.2M | op = getbinopr(ls->t.token); |
1274 | 27.1M | while (op != OPR_NOBINOPR && priority[op].left > limit) { |
1275 | 12.9M | expdesc v2; |
1276 | 12.9M | BinOpr nextop; |
1277 | 12.9M | int line = ls->linenumber; |
1278 | 12.9M | luaX_next(ls); /* skip operator */ |
1279 | 12.9M | luaK_infix(ls->fs, op, v); |
1280 | | /* read sub-expression with higher priority */ |
1281 | 12.9M | nextop = subexpr(ls, &v2, priority[op].right); |
1282 | 12.9M | luaK_posfix(ls->fs, op, v, &v2, line); |
1283 | 12.9M | op = nextop; |
1284 | 12.9M | } |
1285 | 14.2M | leavelevel(ls); |
1286 | 14.2M | return op; /* return first untreated operator */ |
1287 | 14.2M | } |
1288 | | |
1289 | | |
1290 | 975k | static void expr (LexState *ls, expdesc *v) { |
1291 | 975k | subexpr(ls, v, 0); |
1292 | 975k | } |
1293 | | |
1294 | | /* }==================================================================== */ |
1295 | | |
1296 | | |
1297 | | |
1298 | | /* |
1299 | | ** {====================================================================== |
1300 | | ** Rules for Statements |
1301 | | ** ======================================================================= |
1302 | | */ |
1303 | | |
1304 | | |
1305 | 54.7k | static void block (LexState *ls) { |
1306 | | /* block -> statlist */ |
1307 | 54.7k | FuncState *fs = ls->fs; |
1308 | 54.7k | BlockCnt bl; |
1309 | 54.7k | enterblock(fs, &bl, 0); |
1310 | 54.7k | statlist(ls); |
1311 | 54.7k | leaveblock(fs); |
1312 | 54.7k | } |
1313 | | |
1314 | | |
1315 | | /* |
1316 | | ** structure to chain all variables in the left-hand side of an |
1317 | | ** assignment |
1318 | | */ |
1319 | | struct LHS_assign { |
1320 | | struct LHS_assign *prev; |
1321 | | expdesc v; /* variable (global, local, upvalue, or indexed) */ |
1322 | | }; |
1323 | | |
1324 | | |
1325 | | /* |
1326 | | ** check whether, in an assignment to an upvalue/local variable, the |
1327 | | ** upvalue/local variable is begin used in a previous assignment to a |
1328 | | ** table. If so, save original upvalue/local value in a safe place and |
1329 | | ** use this safe copy in the previous assignment. |
1330 | | */ |
1331 | 3.79k | static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { |
1332 | 3.79k | FuncState *fs = ls->fs; |
1333 | 3.79k | int extra = fs->freereg; /* eventual position to save local variable */ |
1334 | 3.79k | int conflict = 0; |
1335 | 166k | for (; lh; lh = lh->prev) { /* check all previous assignments */ |
1336 | 162k | if (vkisindexed(lh->v.k)) { /* assignment to table field? */ |
1337 | 61.9k | if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ |
1338 | 3.15k | if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { |
1339 | 179 | conflict = 1; /* table is the upvalue being assigned now */ |
1340 | 179 | lh->v.k = VINDEXSTR; |
1341 | 179 | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
1342 | 179 | } |
1343 | 3.15k | } |
1344 | 58.7k | else { /* table is a register */ |
1345 | 58.7k | if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { |
1346 | 1.15k | conflict = 1; /* table is the local being assigned now */ |
1347 | 1.15k | lh->v.u.ind.t = extra; /* assignment will use safe copy */ |
1348 | 1.15k | } |
1349 | | /* is index the local being assigned? */ |
1350 | 58.7k | if (lh->v.k == VINDEXED && v->k == VLOCAL && |
1351 | 58.7k | lh->v.u.ind.idx == v->u.var.ridx) { |
1352 | 64 | conflict = 1; |
1353 | 64 | lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ |
1354 | 64 | } |
1355 | 58.7k | } |
1356 | 61.9k | } |
1357 | 162k | } |
1358 | 3.79k | if (conflict) { |
1359 | | /* copy upvalue/local value to a temporary (in position 'extra') */ |
1360 | 1.04k | if (v->k == VLOCAL) |
1361 | 890 | luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); |
1362 | 157 | else |
1363 | 157 | luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); |
1364 | 1.04k | luaK_reserveregs(fs, 1); |
1365 | 1.04k | } |
1366 | 3.79k | } |
1367 | | |
1368 | | /* |
1369 | | ** Parse and compile a multiple assignment. The first "variable" |
1370 | | ** (a 'suffixedexp') was already read by the caller. |
1371 | | ** |
1372 | | ** assignment -> suffixedexp restassign |
1373 | | ** restassign -> ',' suffixedexp restassign | '=' explist |
1374 | | */ |
1375 | 386k | static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { |
1376 | 386k | expdesc e; |
1377 | 386k | check_condition(ls, vkisvar(lh->v.k), "syntax error"); |
1378 | 386k | check_readonly(ls, &lh->v); |
1379 | 386k | if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ |
1380 | 26.6k | struct LHS_assign nv; |
1381 | 26.6k | nv.prev = lh; |
1382 | 26.6k | suffixedexp(ls, &nv.v); |
1383 | 26.6k | if (!vkisindexed(nv.v.k)) |
1384 | 3.79k | check_conflict(ls, lh, &nv.v); |
1385 | 26.6k | enterlevel(ls); /* control recursion depth */ |
1386 | 26.6k | restassign(ls, &nv, nvars+1); |
1387 | 26.6k | leavelevel(ls); |
1388 | 26.6k | } |
1389 | 359k | else { /* restassign -> '=' explist */ |
1390 | 359k | int nexps; |
1391 | 359k | checknext(ls, '='); |
1392 | 359k | nexps = explist(ls, &e); |
1393 | 359k | if (nexps != nvars) |
1394 | 9.53k | adjust_assign(ls, nvars, nexps, &e); |
1395 | 349k | else { |
1396 | 349k | luaK_setoneret(ls->fs, &e); /* close last expression */ |
1397 | 349k | luaK_storevar(ls->fs, &lh->v, &e); |
1398 | 349k | return; /* avoid default */ |
1399 | 349k | } |
1400 | 359k | } |
1401 | 36.1k | init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ |
1402 | 36.1k | luaK_storevar(ls->fs, &lh->v, &e); |
1403 | 36.1k | } |
1404 | | |
1405 | | |
1406 | 16.2k | static int cond (LexState *ls) { |
1407 | | /* cond -> exp */ |
1408 | 16.2k | expdesc v; |
1409 | 16.2k | expr(ls, &v); /* read condition */ |
1410 | 16.2k | if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ |
1411 | 16.2k | luaK_goiftrue(ls->fs, &v); |
1412 | 16.2k | return v.f; |
1413 | 16.2k | } |
1414 | | |
1415 | | |
1416 | 6.94k | static void gotostat (LexState *ls) { |
1417 | 6.94k | FuncState *fs = ls->fs; |
1418 | 6.94k | int line = ls->linenumber; |
1419 | 6.94k | TString *name = str_checkname(ls); /* label's name */ |
1420 | 6.94k | Labeldesc *lb = findlabel(ls, name); |
1421 | 6.94k | if (lb == NULL) /* no label? */ |
1422 | | /* forward jump; will be resolved when the label is declared */ |
1423 | 969 | newgotoentry(ls, name, line, luaK_jump(fs)); |
1424 | 5.97k | else { /* found a label */ |
1425 | | /* backward jump; will be resolved here */ |
1426 | 5.97k | int lblevel = reglevel(fs, lb->nactvar); /* label level */ |
1427 | 5.97k | if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ |
1428 | 1.12k | luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); |
1429 | | /* create jump and link it to the label */ |
1430 | 5.97k | luaK_patchlist(fs, luaK_jump(fs), lb->pc); |
1431 | 5.97k | } |
1432 | 6.94k | } |
1433 | | |
1434 | | |
1435 | | /* |
1436 | | ** Break statement. Semantically equivalent to "goto break". |
1437 | | */ |
1438 | 2.90k | static void breakstat (LexState *ls) { |
1439 | 2.90k | int line = ls->linenumber; |
1440 | 2.90k | luaX_next(ls); /* skip break */ |
1441 | 2.90k | newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); |
1442 | 2.90k | } |
1443 | | |
1444 | | |
1445 | | /* |
1446 | | ** Check whether there is already a label with the given 'name'. |
1447 | | */ |
1448 | 235 | static void checkrepeated (LexState *ls, TString *name) { |
1449 | 235 | Labeldesc *lb = findlabel(ls, name); |
1450 | 235 | if (l_unlikely(lb != NULL)) { /* already defined? */ |
1451 | 0 | const char *msg = "label '%s' already defined on line %d"; |
1452 | 0 | msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); |
1453 | 0 | luaK_semerror(ls, msg); /* error */ |
1454 | 0 | } |
1455 | 235 | } |
1456 | | |
1457 | | |
1458 | 235 | static void labelstat (LexState *ls, TString *name, int line) { |
1459 | | /* label -> '::' NAME '::' */ |
1460 | 235 | checknext(ls, TK_DBCOLON); /* skip double colon */ |
1461 | 268 | while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) |
1462 | 33 | statement(ls); /* skip other no-op statements */ |
1463 | 235 | checkrepeated(ls, name); /* check for repeated labels */ |
1464 | 235 | createlabel(ls, name, line, block_follow(ls, 0)); |
1465 | 235 | } |
1466 | | |
1467 | | |
1468 | 9.20k | static void whilestat (LexState *ls, int line) { |
1469 | | /* whilestat -> WHILE cond DO block END */ |
1470 | 9.20k | FuncState *fs = ls->fs; |
1471 | 9.20k | int whileinit; |
1472 | 9.20k | int condexit; |
1473 | 9.20k | BlockCnt bl; |
1474 | 9.20k | luaX_next(ls); /* skip WHILE */ |
1475 | 9.20k | whileinit = luaK_getlabel(fs); |
1476 | 9.20k | condexit = cond(ls); |
1477 | 9.20k | enterblock(fs, &bl, 1); |
1478 | 9.20k | checknext(ls, TK_DO); |
1479 | 9.20k | block(ls); |
1480 | 9.20k | luaK_jumpto(fs, whileinit); |
1481 | 9.20k | check_match(ls, TK_END, TK_WHILE, line); |
1482 | 9.20k | leaveblock(fs); |
1483 | 9.20k | luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ |
1484 | 9.20k | } |
1485 | | |
1486 | | |
1487 | 7.02k | static void repeatstat (LexState *ls, int line) { |
1488 | | /* repeatstat -> REPEAT block UNTIL cond */ |
1489 | 7.02k | int condexit; |
1490 | 7.02k | FuncState *fs = ls->fs; |
1491 | 7.02k | int repeat_init = luaK_getlabel(fs); |
1492 | 7.02k | BlockCnt bl1, bl2; |
1493 | 7.02k | enterblock(fs, &bl1, 1); /* loop block */ |
1494 | 7.02k | enterblock(fs, &bl2, 0); /* scope block */ |
1495 | 7.02k | luaX_next(ls); /* skip REPEAT */ |
1496 | 7.02k | statlist(ls); |
1497 | 7.02k | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
1498 | 7.02k | condexit = cond(ls); /* read condition (inside scope block) */ |
1499 | 7.02k | leaveblock(fs); /* finish scope */ |
1500 | 7.02k | if (bl2.upval) { /* upvalues? */ |
1501 | 293 | int exit = luaK_jump(fs); /* normal exit must jump over fix */ |
1502 | 293 | luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ |
1503 | 293 | luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); |
1504 | 293 | condexit = luaK_jump(fs); /* repeat after closing upvalues */ |
1505 | 293 | luaK_patchtohere(fs, exit); /* normal exit comes to here */ |
1506 | 293 | } |
1507 | 7.02k | luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ |
1508 | 7.02k | leaveblock(fs); /* finish loop */ |
1509 | 7.02k | } |
1510 | | |
1511 | | |
1512 | | /* |
1513 | | ** Read an expression and generate code to put its results in next |
1514 | | ** stack slot. |
1515 | | ** |
1516 | | */ |
1517 | 5.59k | static void exp1 (LexState *ls) { |
1518 | 5.59k | expdesc e; |
1519 | 5.59k | expr(ls, &e); |
1520 | 5.59k | luaK_exp2nextreg(ls->fs, &e); |
1521 | 5.59k | lua_assert(e.k == VNONRELOC); |
1522 | 5.59k | } |
1523 | | |
1524 | | |
1525 | | /* |
1526 | | ** Fix for instruction at position 'pc' to jump to 'dest'. |
1527 | | ** (Jump addresses are relative in Lua). 'back' true means |
1528 | | ** a back jump. |
1529 | | */ |
1530 | 77.5k | static void fixforjump (FuncState *fs, int pc, int dest, int back) { |
1531 | 77.5k | Instruction *jmp = &fs->f->code[pc]; |
1532 | 77.5k | int offset = dest - (pc + 1); |
1533 | 77.5k | if (back) |
1534 | 38.7k | offset = -offset; |
1535 | 77.5k | if (l_unlikely(offset > MAXARG_Bx)) |
1536 | 1 | luaX_syntaxerror(fs->ls, "control structure too long"); |
1537 | 77.5k | SETARG_Bx(*jmp, offset); |
1538 | 77.5k | } |
1539 | | |
1540 | | |
1541 | | /* |
1542 | | ** Generate code for a 'for' loop. |
1543 | | */ |
1544 | 39.3k | static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { |
1545 | | /* forbody -> DO block */ |
1546 | 39.3k | static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; |
1547 | 39.3k | static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; |
1548 | 39.3k | BlockCnt bl; |
1549 | 39.3k | FuncState *fs = ls->fs; |
1550 | 39.3k | int prep, endfor; |
1551 | 39.3k | checknext(ls, TK_DO); |
1552 | 39.3k | prep = luaK_codeABx(fs, forprep[isgen], base, 0); |
1553 | 39.3k | enterblock(fs, &bl, 0); /* scope for declared variables */ |
1554 | 39.3k | adjustlocalvars(ls, nvars); |
1555 | 39.3k | luaK_reserveregs(fs, nvars); |
1556 | 39.3k | block(ls); |
1557 | 39.3k | leaveblock(fs); /* end of scope for declared variables */ |
1558 | 39.3k | fixforjump(fs, prep, luaK_getlabel(fs), 0); |
1559 | 39.3k | if (isgen) { /* generic for? */ |
1560 | 35.9k | luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); |
1561 | 35.9k | luaK_fixline(fs, line); |
1562 | 35.9k | } |
1563 | 39.3k | endfor = luaK_codeABx(fs, forloop[isgen], base, 0); |
1564 | 39.3k | fixforjump(fs, endfor, prep + 1, 1); |
1565 | 39.3k | luaK_fixline(fs, line); |
1566 | 39.3k | } |
1567 | | |
1568 | | |
1569 | 3.41k | static void fornum (LexState *ls, TString *varname, int line) { |
1570 | | /* fornum -> NAME = exp,exp[,exp] forbody */ |
1571 | 3.41k | FuncState *fs = ls->fs; |
1572 | 3.41k | int base = fs->freereg; |
1573 | 3.41k | new_localvarliteral(ls, "(for state)"); |
1574 | 3.41k | new_localvarliteral(ls, "(for state)"); |
1575 | 3.41k | new_localvarliteral(ls, "(for state)"); |
1576 | 3.41k | new_localvar(ls, varname); |
1577 | 3.41k | checknext(ls, '='); |
1578 | 3.41k | exp1(ls); /* initial value */ |
1579 | 3.41k | checknext(ls, ','); |
1580 | 3.41k | exp1(ls); /* limit */ |
1581 | 3.41k | if (testnext(ls, ',')) |
1582 | 1.18k | exp1(ls); /* optional step */ |
1583 | 2.22k | else { /* default step = 1 */ |
1584 | 2.22k | luaK_int(fs, fs->freereg, 1); |
1585 | 2.22k | luaK_reserveregs(fs, 1); |
1586 | 2.22k | } |
1587 | 3.41k | adjustlocalvars(ls, 3); /* control variables */ |
1588 | 3.41k | forbody(ls, base, line, 1, 0); |
1589 | 3.41k | } |
1590 | | |
1591 | | |
1592 | 35.9k | static void forlist (LexState *ls, TString *indexname) { |
1593 | | /* forlist -> NAME {,NAME} IN explist forbody */ |
1594 | 35.9k | FuncState *fs = ls->fs; |
1595 | 35.9k | expdesc e; |
1596 | 35.9k | int nvars = 5; /* gen, state, control, toclose, 'indexname' */ |
1597 | 35.9k | int line; |
1598 | 35.9k | int base = fs->freereg; |
1599 | | /* create control variables */ |
1600 | 35.9k | new_localvarliteral(ls, "(for state)"); |
1601 | 35.9k | new_localvarliteral(ls, "(for state)"); |
1602 | 35.9k | new_localvarliteral(ls, "(for state)"); |
1603 | 35.9k | new_localvarliteral(ls, "(for state)"); |
1604 | | /* create declared variables */ |
1605 | 35.9k | new_localvar(ls, indexname); |
1606 | 40.2k | while (testnext(ls, ',')) { |
1607 | 4.30k | new_localvar(ls, str_checkname(ls)); |
1608 | 4.30k | nvars++; |
1609 | 4.30k | } |
1610 | 35.9k | checknext(ls, TK_IN); |
1611 | 35.9k | line = ls->linenumber; |
1612 | 35.9k | adjust_assign(ls, 4, explist(ls, &e), &e); |
1613 | 35.9k | adjustlocalvars(ls, 4); /* control variables */ |
1614 | 35.9k | marktobeclosed(fs); /* last control var. must be closed */ |
1615 | 35.9k | luaK_checkstack(fs, 3); /* extra space to call generator */ |
1616 | 35.9k | forbody(ls, base, line, nvars - 4, 1); |
1617 | 35.9k | } |
1618 | | |
1619 | | |
1620 | 39.3k | static void forstat (LexState *ls, int line) { |
1621 | | /* forstat -> FOR (fornum | forlist) END */ |
1622 | 39.3k | FuncState *fs = ls->fs; |
1623 | 39.3k | TString *varname; |
1624 | 39.3k | BlockCnt bl; |
1625 | 39.3k | enterblock(fs, &bl, 1); /* scope for loop and control variables */ |
1626 | 39.3k | luaX_next(ls); /* skip 'for' */ |
1627 | 39.3k | varname = str_checkname(ls); /* first variable name */ |
1628 | 39.3k | switch (ls->t.token) { |
1629 | 3.41k | case '=': fornum(ls, varname, line); break; |
1630 | 35.9k | case ',': case TK_IN: forlist(ls, varname); break; |
1631 | 0 | default: luaX_syntaxerror(ls, "'=' or 'in' expected"); |
1632 | 39.3k | } |
1633 | 38.7k | check_match(ls, TK_END, TK_FOR, line); |
1634 | 38.7k | leaveblock(fs); /* loop scope ('break' jumps to this point) */ |
1635 | 38.7k | } |
1636 | | |
1637 | | |
1638 | 83.1k | static void test_then_block (LexState *ls, int *escapelist) { |
1639 | | /* test_then_block -> [IF | ELSEIF] cond THEN block */ |
1640 | 83.1k | BlockCnt bl; |
1641 | 83.1k | FuncState *fs = ls->fs; |
1642 | 83.1k | expdesc v; |
1643 | 83.1k | int jf; /* instruction to skip 'then' code (if condition is false) */ |
1644 | 83.1k | luaX_next(ls); /* skip IF or ELSEIF */ |
1645 | 83.1k | expr(ls, &v); /* read condition */ |
1646 | 83.1k | checknext(ls, TK_THEN); |
1647 | 83.1k | if (ls->t.token == TK_BREAK) { /* 'if x then break' ? */ |
1648 | 55.1k | int line = ls->linenumber; |
1649 | 55.1k | luaK_goiffalse(ls->fs, &v); /* will jump if condition is true */ |
1650 | 55.1k | luaX_next(ls); /* skip 'break' */ |
1651 | 55.1k | enterblock(fs, &bl, 0); /* must enter block before 'goto' */ |
1652 | 55.1k | newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t); |
1653 | 55.3k | while (testnext(ls, ';')) {} /* skip semicolons */ |
1654 | 55.1k | if (block_follow(ls, 0)) { /* jump is the entire block? */ |
1655 | 54.2k | leaveblock(fs); |
1656 | 54.2k | return; /* and that is it */ |
1657 | 54.2k | } |
1658 | 949 | else /* must skip over 'then' part if condition is false */ |
1659 | 949 | jf = luaK_jump(fs); |
1660 | 55.1k | } |
1661 | 27.9k | else { /* regular case (not a break) */ |
1662 | 27.9k | luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ |
1663 | 27.9k | enterblock(fs, &bl, 0); |
1664 | 27.9k | jf = v.f; |
1665 | 27.9k | } |
1666 | 28.9k | statlist(ls); /* 'then' part */ |
1667 | 28.9k | leaveblock(fs); |
1668 | 28.9k | if (ls->t.token == TK_ELSE || |
1669 | 28.9k | ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ |
1670 | 2.56k | luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ |
1671 | 28.9k | luaK_patchtohere(fs, jf); |
1672 | 28.9k | } |
1673 | | |
1674 | | |
1675 | 83.1k | static void ifstat (LexState *ls, int line) { |
1676 | | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ |
1677 | 83.1k | FuncState *fs = ls->fs; |
1678 | 83.1k | int escapelist = NO_JUMP; /* exit list for finished parts */ |
1679 | 83.1k | test_then_block(ls, &escapelist); /* IF cond THEN block */ |
1680 | 83.1k | while (ls->t.token == TK_ELSEIF) |
1681 | 2 | test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ |
1682 | 83.1k | if (testnext(ls, TK_ELSE)) |
1683 | 3.23k | block(ls); /* 'else' part */ |
1684 | 83.1k | check_match(ls, TK_END, TK_IF, line); |
1685 | 83.1k | luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ |
1686 | 83.1k | } |
1687 | | |
1688 | | |
1689 | 2.79k | static void localfunc (LexState *ls) { |
1690 | 2.79k | expdesc b; |
1691 | 2.79k | FuncState *fs = ls->fs; |
1692 | 2.79k | int fvar = fs->nactvar; /* function's variable index */ |
1693 | 2.79k | new_localvar(ls, str_checkname(ls)); /* new local variable */ |
1694 | 2.79k | adjustlocalvars(ls, 1); /* enter its scope */ |
1695 | 2.79k | body(ls, &b, 0, ls->linenumber); /* function created in next register */ |
1696 | | /* debug information will only see the variable after this point! */ |
1697 | 2.79k | localdebuginfo(fs, fvar)->startpc = fs->pc; |
1698 | 2.79k | } |
1699 | | |
1700 | | |
1701 | 23.6k | static int getlocalattribute (LexState *ls) { |
1702 | | /* ATTRIB -> ['<' Name '>'] */ |
1703 | 23.6k | if (testnext(ls, '<')) { |
1704 | 1.66k | const char *attr = getstr(str_checkname(ls)); |
1705 | 1.66k | checknext(ls, '>'); |
1706 | 1.66k | if (strcmp(attr, "const") == 0) |
1707 | 1.46k | return RDKCONST; /* read-only variable */ |
1708 | 195 | else if (strcmp(attr, "close") == 0) |
1709 | 194 | return RDKTOCLOSE; /* to-be-closed variable */ |
1710 | 1 | else |
1711 | 1 | luaK_semerror(ls, |
1712 | 1 | luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); |
1713 | 1.66k | } |
1714 | 21.9k | return VDKREG; /* regular variable */ |
1715 | 23.6k | } |
1716 | | |
1717 | | |
1718 | 9.14k | static void checktoclose (FuncState *fs, int level) { |
1719 | 9.14k | if (level != -1) { /* is there a to-be-closed variable? */ |
1720 | 194 | marktobeclosed(fs); |
1721 | 194 | luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); |
1722 | 194 | } |
1723 | 9.14k | } |
1724 | | |
1725 | | |
1726 | 9.15k | static void localstat (LexState *ls) { |
1727 | | /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */ |
1728 | 9.15k | FuncState *fs = ls->fs; |
1729 | 9.15k | int toclose = -1; /* index of to-be-closed variable (if any) */ |
1730 | 9.15k | Vardesc *var; /* last variable */ |
1731 | 9.15k | int vidx, kind; /* index and kind of last variable */ |
1732 | 9.15k | int nvars = 0; |
1733 | 9.15k | int nexps; |
1734 | 9.15k | expdesc e; |
1735 | 23.6k | do { |
1736 | 23.6k | vidx = new_localvar(ls, str_checkname(ls)); |
1737 | 23.6k | kind = getlocalattribute(ls); |
1738 | 23.6k | getlocalvardesc(fs, vidx)->vd.kind = kind; |
1739 | 23.6k | if (kind == RDKTOCLOSE) { /* to-be-closed? */ |
1740 | 194 | if (toclose != -1) /* one already present? */ |
1741 | 0 | luaK_semerror(ls, "multiple to-be-closed variables in local list"); |
1742 | 194 | toclose = fs->nactvar + nvars; |
1743 | 194 | } |
1744 | 23.6k | nvars++; |
1745 | 23.6k | } while (testnext(ls, ',')); |
1746 | 9.15k | if (testnext(ls, '=')) |
1747 | 5.40k | nexps = explist(ls, &e); |
1748 | 3.74k | else { |
1749 | 3.74k | e.k = VVOID; |
1750 | 3.74k | nexps = 0; |
1751 | 3.74k | } |
1752 | 9.15k | var = getlocalvardesc(fs, vidx); /* get last variable */ |
1753 | 9.15k | if (nvars == nexps && /* no adjustments? */ |
1754 | 9.15k | var->vd.kind == RDKCONST && /* last variable is const? */ |
1755 | 9.15k | luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ |
1756 | 835 | var->vd.kind = RDKCTC; /* variable is a compile-time constant */ |
1757 | 835 | adjustlocalvars(ls, nvars - 1); /* exclude last variable */ |
1758 | 835 | fs->nactvar++; /* but count it */ |
1759 | 835 | } |
1760 | 8.31k | else { |
1761 | 8.31k | adjust_assign(ls, nvars, nexps, &e); |
1762 | 8.31k | adjustlocalvars(ls, nvars); |
1763 | 8.31k | } |
1764 | 9.15k | checktoclose(fs, toclose); |
1765 | 9.15k | } |
1766 | | |
1767 | | |
1768 | 5.92k | static int funcname (LexState *ls, expdesc *v) { |
1769 | | /* funcname -> NAME {fieldsel} [':' NAME] */ |
1770 | 5.92k | int ismethod = 0; |
1771 | 5.92k | singlevar(ls, v); |
1772 | 7.01k | while (ls->t.token == '.') |
1773 | 1.09k | fieldsel(ls, v); |
1774 | 5.92k | if (ls->t.token == ':') { |
1775 | 244 | ismethod = 1; |
1776 | 244 | fieldsel(ls, v); |
1777 | 244 | } |
1778 | 5.92k | return ismethod; |
1779 | 5.92k | } |
1780 | | |
1781 | | |
1782 | 5.92k | static void funcstat (LexState *ls, int line) { |
1783 | | /* funcstat -> FUNCTION funcname body */ |
1784 | 5.92k | int ismethod; |
1785 | 5.92k | expdesc v, b; |
1786 | 5.92k | luaX_next(ls); /* skip FUNCTION */ |
1787 | 5.92k | ismethod = funcname(ls, &v); |
1788 | 5.92k | body(ls, &b, ismethod, line); |
1789 | 5.92k | check_readonly(ls, &v); |
1790 | 5.92k | luaK_storevar(ls->fs, &v, &b); |
1791 | 5.92k | luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ |
1792 | 5.92k | } |
1793 | | |
1794 | | |
1795 | 415k | static void exprstat (LexState *ls) { |
1796 | | /* stat -> func | assignment */ |
1797 | 415k | FuncState *fs = ls->fs; |
1798 | 415k | struct LHS_assign v; |
1799 | 415k | suffixedexp(ls, &v.v); |
1800 | 415k | if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ |
1801 | 359k | v.prev = NULL; |
1802 | 359k | restassign(ls, &v, 1); |
1803 | 359k | } |
1804 | 55.7k | else { /* stat -> func */ |
1805 | 55.7k | Instruction *inst; |
1806 | 55.7k | check_condition(ls, v.v.k == VCALL, "syntax error"); |
1807 | 54.4k | inst = &getinstruction(fs, &v.v); |
1808 | 54.4k | SETARG_C(*inst, 1); /* call statement uses no results */ |
1809 | 54.4k | } |
1810 | 415k | } |
1811 | | |
1812 | | |
1813 | 30.1k | static void retstat (LexState *ls) { |
1814 | | /* stat -> RETURN [explist] [';'] */ |
1815 | 30.1k | FuncState *fs = ls->fs; |
1816 | 30.1k | expdesc e; |
1817 | 30.1k | int nret; /* number of values being returned */ |
1818 | 30.1k | int first = luaY_nvarstack(fs); /* first slot to be returned */ |
1819 | 30.1k | if (block_follow(ls, 1) || ls->t.token == ';') |
1820 | 25.9k | nret = 0; /* return no values */ |
1821 | 4.20k | else { |
1822 | 4.20k | nret = explist(ls, &e); /* optional return values */ |
1823 | 4.20k | if (hasmultret(e.k)) { |
1824 | 571 | luaK_setmultret(fs, &e); |
1825 | 571 | if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ |
1826 | 210 | SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); |
1827 | 210 | lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); |
1828 | 210 | } |
1829 | 571 | nret = LUA_MULTRET; /* return all values */ |
1830 | 571 | } |
1831 | 3.63k | else { |
1832 | 3.63k | if (nret == 1) /* only one single value? */ |
1833 | 1.22k | first = luaK_exp2anyreg(fs, &e); /* can use original slot */ |
1834 | 2.40k | else { /* values must go to the top of the stack */ |
1835 | 2.40k | luaK_exp2nextreg(fs, &e); |
1836 | 2.40k | lua_assert(nret == fs->freereg - first); |
1837 | 2.40k | } |
1838 | 3.63k | } |
1839 | 4.20k | } |
1840 | 30.1k | luaK_ret(fs, first, nret); |
1841 | 30.1k | testnext(ls, ';'); /* skip optional semicolon */ |
1842 | 30.1k | } |
1843 | | |
1844 | | |
1845 | 757k | static void statement (LexState *ls) { |
1846 | 757k | int line = ls->linenumber; /* may be needed for error messages */ |
1847 | 757k | enterlevel(ls); |
1848 | 757k | switch (ls->t.token) { |
1849 | 192k | case ';': { /* stat -> ';' (empty statement) */ |
1850 | 192k | luaX_next(ls); /* skip ';' */ |
1851 | 192k | break; |
1852 | 0 | } |
1853 | 81.1k | case TK_IF: { /* stat -> ifstat */ |
1854 | 81.1k | ifstat(ls, line); |
1855 | 81.1k | break; |
1856 | 0 | } |
1857 | 8.67k | case TK_WHILE: { /* stat -> whilestat */ |
1858 | 8.67k | whilestat(ls, line); |
1859 | 8.67k | break; |
1860 | 0 | } |
1861 | 1.59k | case TK_DO: { /* stat -> DO block END */ |
1862 | 1.59k | luaX_next(ls); /* skip DO */ |
1863 | 1.59k | block(ls); |
1864 | 1.59k | check_match(ls, TK_END, TK_DO, line); |
1865 | 1.59k | break; |
1866 | 0 | } |
1867 | 38.2k | case TK_FOR: { /* stat -> forstat */ |
1868 | 38.2k | forstat(ls, line); |
1869 | 38.2k | break; |
1870 | 0 | } |
1871 | 6.23k | case TK_REPEAT: { /* stat -> repeatstat */ |
1872 | 6.23k | repeatstat(ls, line); |
1873 | 6.23k | break; |
1874 | 0 | } |
1875 | 3.63k | case TK_FUNCTION: { /* stat -> funcstat */ |
1876 | 3.63k | funcstat(ls, line); |
1877 | 3.63k | break; |
1878 | 0 | } |
1879 | 8.09k | case TK_LOCAL: { /* stat -> localstat */ |
1880 | 8.09k | luaX_next(ls); /* skip LOCAL */ |
1881 | 8.09k | if (testnext(ls, TK_FUNCTION)) /* local function? */ |
1882 | 2.43k | localfunc(ls); |
1883 | 5.65k | else |
1884 | 5.65k | localstat(ls); |
1885 | 8.09k | break; |
1886 | 0 | } |
1887 | 0 | case TK_DBCOLON: { /* stat -> label */ |
1888 | 0 | luaX_next(ls); /* skip double colon */ |
1889 | 0 | labelstat(ls, str_checkname(ls), line); |
1890 | 0 | break; |
1891 | 0 | } |
1892 | 30.1k | case TK_RETURN: { /* stat -> retstat */ |
1893 | 30.1k | luaX_next(ls); /* skip RETURN */ |
1894 | 30.1k | retstat(ls); |
1895 | 30.1k | break; |
1896 | 0 | } |
1897 | 1.72k | case TK_BREAK: { /* stat -> breakstat */ |
1898 | 1.72k | breakstat(ls); |
1899 | 1.72k | break; |
1900 | 0 | } |
1901 | 0 | case TK_GOTO: { /* stat -> 'goto' NAME */ |
1902 | 0 | luaX_next(ls); /* skip 'goto' */ |
1903 | 0 | gotostat(ls); |
1904 | 0 | break; |
1905 | 0 | } |
1906 | 385k | default: { /* stat -> func | assignment */ |
1907 | 385k | exprstat(ls); |
1908 | 385k | break; |
1909 | 0 | } |
1910 | 757k | } |
1911 | 741k | lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && |
1912 | 741k | ls->fs->freereg >= luaY_nvarstack(ls->fs)); |
1913 | 741k | ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ |
1914 | 741k | leavelevel(ls); |
1915 | 741k | } |
1916 | | |
1917 | | /* }====================================================================== */ |
1918 | | |
1919 | | |
1920 | | /* |
1921 | | ** compiles the main function, which is a regular vararg function with an |
1922 | | ** upvalue named LUA_ENV |
1923 | | */ |
1924 | 17.1k | static void mainfunc (LexState *ls, FuncState *fs) { |
1925 | 17.1k | BlockCnt bl; |
1926 | 17.1k | Upvaldesc *env; |
1927 | 17.1k | open_func(ls, fs, &bl); |
1928 | 17.1k | setvararg(fs, 0); /* main function is always declared vararg */ |
1929 | 17.1k | env = allocupvalue(fs); /* ...set environment upvalue */ |
1930 | 17.1k | env->instack = 1; |
1931 | 17.1k | env->idx = 0; |
1932 | 17.1k | env->kind = VDKREG; |
1933 | 17.1k | env->name = ls->envn; |
1934 | 17.1k | luaC_objbarrier(ls->L, fs->f, env->name); |
1935 | 0 | luaX_next(ls); /* read first token */ |
1936 | 17.1k | statlist(ls); /* parse main body */ |
1937 | 17.1k | check(ls, TK_EOS); |
1938 | 17.1k | close_func(ls); |
1939 | 17.1k | } |
1940 | | |
1941 | | |
1942 | | LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, |
1943 | 17.1k | Dyndata *dyd, const char *name, int firstchar) { |
1944 | 17.1k | LexState lexstate; |
1945 | 17.1k | FuncState funcstate; |
1946 | 17.1k | LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ |
1947 | 17.1k | setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ |
1948 | 17.1k | luaD_inctop(L); |
1949 | 17.1k | lexstate.h = luaH_new(L); /* create table for scanner */ |
1950 | 17.1k | sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ |
1951 | 17.1k | luaD_inctop(L); |
1952 | 17.1k | funcstate.f = cl->p = luaF_newproto(L); |
1953 | 17.1k | luaC_objbarrier(L, cl, cl->p); |
1954 | 0 | funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ |
1955 | 17.1k | luaC_objbarrier(L, funcstate.f, funcstate.f->source); |
1956 | 0 | lexstate.buff = buff; |
1957 | 17.1k | lexstate.dyd = dyd; |
1958 | 17.1k | dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; |
1959 | 17.1k | luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); |
1960 | 17.1k | mainfunc(&lexstate, &funcstate); |
1961 | 17.1k | lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); |
1962 | | /* all scopes should be correctly finished */ |
1963 | 2.45k | lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); |
1964 | 2.45k | L->top.p--; /* remove scanner's table */ |
1965 | 2.45k | return cl; /* closure is on the stack, too */ |
1966 | 2.45k | } |
1967 | | |