GCC Code Coverage Report


Directory: ./
File: firmware/ext/lua/lparser.c
Date: 2025-12-08 15:38:50
Warnings: 3 unchecked decisions!
Coverage Exec Excl Total
Lines: 69.8% 766 0 1097
Functions: 81.2% 78 0 96
Branches: 51.5% 211 0 410
Decisions: 54.2% 162 - 299

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