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