]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/3rdparty/civetweb/src/third_party/lua-5.3.6/src/lparser.c
2 ** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $
4 ** See Copyright Notice in lua.h
32 /* maximum number of local variables per function (must be smaller
33 than 250, due to the bytecode format) */
37 #define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
40 /* because all strings are unified by the scanner, the parser
41 can use pointer equality for string equality */
42 #define eqstr(a,b) ((a) == (b))
46 ** nodes for block list (list of active blocks)
48 typedef struct BlockCnt
{
49 struct BlockCnt
*previous
; /* chain */
50 int firstlabel
; /* index of first label in this block */
51 int firstgoto
; /* index of first pending goto in this block */
52 lu_byte nactvar
; /* # active locals outside the block */
53 lu_byte upval
; /* true if some variable in the block is an upvalue */
54 lu_byte isloop
; /* true if 'block' is a loop */
60 ** prototypes for recursive non-terminal functions
62 static void statement (LexState
*ls
);
63 static void expr (LexState
*ls
, expdesc
*v
);
67 static l_noret
semerror (LexState
*ls
, const char *msg
) {
68 ls
->t
.token
= 0; /* remove "near <token>" from final message */
69 luaX_syntaxerror(ls
, msg
);
73 static l_noret
error_expected (LexState
*ls
, int token
) {
75 luaO_pushfstring(ls
->L
, "%s expected", luaX_token2str(ls
, token
)));
79 static l_noret
errorlimit (FuncState
*fs
, int limit
, const char *what
) {
80 lua_State
*L
= fs
->ls
->L
;
82 int line
= fs
->f
->linedefined
;
83 const char *where
= (line
== 0)
85 : luaO_pushfstring(L
, "function at line %d", line
);
86 msg
= luaO_pushfstring(L
, "too many %s (limit is %d) in %s",
88 luaX_syntaxerror(fs
->ls
, msg
);
92 static void checklimit (FuncState
*fs
, int v
, int l
, const char *what
) {
93 if (v
> l
) errorlimit(fs
, l
, what
);
97 static int testnext (LexState
*ls
, int c
) {
98 if (ls
->t
.token
== c
) {
106 static void check (LexState
*ls
, int c
) {
107 if (ls
->t
.token
!= c
)
108 error_expected(ls
, c
);
112 static void checknext (LexState
*ls
, int c
) {
118 #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
122 static void check_match (LexState
*ls
, int what
, int who
, int where
) {
123 if (!testnext(ls
, what
)) {
124 if (where
== ls
->linenumber
)
125 error_expected(ls
, what
);
127 luaX_syntaxerror(ls
, luaO_pushfstring(ls
->L
,
128 "%s expected (to close %s at line %d)",
129 luaX_token2str(ls
, what
), luaX_token2str(ls
, who
), where
));
135 static TString
*str_checkname (LexState
*ls
) {
138 ts
= ls
->t
.seminfo
.ts
;
144 static void init_exp (expdesc
*e
, expkind k
, int i
) {
145 e
->f
= e
->t
= NO_JUMP
;
151 static void codestring (LexState
*ls
, expdesc
*e
, TString
*s
) {
152 init_exp(e
, VK
, luaK_stringK(ls
->fs
, s
));
156 static void checkname (LexState
*ls
, expdesc
*e
) {
157 codestring(ls
, e
, str_checkname(ls
));
161 static int registerlocalvar (LexState
*ls
, TString
*varname
) {
162 FuncState
*fs
= ls
->fs
;
164 int oldsize
= f
->sizelocvars
;
165 luaM_growvector(ls
->L
, f
->locvars
, fs
->nlocvars
, f
->sizelocvars
,
166 LocVar
, SHRT_MAX
, "local variables");
167 while (oldsize
< f
->sizelocvars
)
168 f
->locvars
[oldsize
++].varname
= NULL
;
169 f
->locvars
[fs
->nlocvars
].varname
= varname
;
170 luaC_objbarrier(ls
->L
, f
, varname
);
171 return fs
->nlocvars
++;
175 static void new_localvar (LexState
*ls
, TString
*name
) {
176 FuncState
*fs
= ls
->fs
;
177 Dyndata
*dyd
= ls
->dyd
;
178 int reg
= registerlocalvar(ls
, name
);
179 checklimit(fs
, dyd
->actvar
.n
+ 1 - fs
->firstlocal
,
180 MAXVARS
, "local variables");
181 luaM_growvector(ls
->L
, dyd
->actvar
.arr
, dyd
->actvar
.n
+ 1,
182 dyd
->actvar
.size
, Vardesc
, MAX_INT
, "local variables");
183 dyd
->actvar
.arr
[dyd
->actvar
.n
++].idx
= cast(short, reg
);
187 static void new_localvarliteral_ (LexState
*ls
, const char *name
, size_t sz
) {
188 new_localvar(ls
, luaX_newstring(ls
, name
, sz
));
191 #define new_localvarliteral(ls,v) \
192 new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1)
195 static LocVar
*getlocvar (FuncState
*fs
, int i
) {
196 int idx
= fs
->ls
->dyd
->actvar
.arr
[fs
->firstlocal
+ i
].idx
;
197 lua_assert(idx
< fs
->nlocvars
);
198 return &fs
->f
->locvars
[idx
];
202 static void adjustlocalvars (LexState
*ls
, int nvars
) {
203 FuncState
*fs
= ls
->fs
;
204 fs
->nactvar
= cast_byte(fs
->nactvar
+ nvars
);
205 for (; nvars
; nvars
--) {
206 getlocvar(fs
, fs
->nactvar
- nvars
)->startpc
= fs
->pc
;
211 static void removevars (FuncState
*fs
, int tolevel
) {
212 fs
->ls
->dyd
->actvar
.n
-= (fs
->nactvar
- tolevel
);
213 while (fs
->nactvar
> tolevel
)
214 getlocvar(fs
, --fs
->nactvar
)->endpc
= fs
->pc
;
218 static int searchupvalue (FuncState
*fs
, TString
*name
) {
220 Upvaldesc
*up
= fs
->f
->upvalues
;
221 for (i
= 0; i
< fs
->nups
; i
++) {
222 if (eqstr(up
[i
].name
, name
)) return i
;
224 return -1; /* not found */
228 static int newupvalue (FuncState
*fs
, TString
*name
, expdesc
*v
) {
230 int oldsize
= f
->sizeupvalues
;
231 checklimit(fs
, fs
->nups
+ 1, MAXUPVAL
, "upvalues");
232 luaM_growvector(fs
->ls
->L
, f
->upvalues
, fs
->nups
, f
->sizeupvalues
,
233 Upvaldesc
, MAXUPVAL
, "upvalues");
234 while (oldsize
< f
->sizeupvalues
)
235 f
->upvalues
[oldsize
++].name
= NULL
;
236 f
->upvalues
[fs
->nups
].instack
= (v
->k
== VLOCAL
);
237 f
->upvalues
[fs
->nups
].idx
= cast_byte(v
->u
.info
);
238 f
->upvalues
[fs
->nups
].name
= name
;
239 luaC_objbarrier(fs
->ls
->L
, f
, name
);
244 static int searchvar (FuncState
*fs
, TString
*n
) {
246 for (i
= cast_int(fs
->nactvar
) - 1; i
>= 0; i
--) {
247 if (eqstr(n
, getlocvar(fs
, i
)->varname
))
250 return -1; /* not found */
255 Mark block where variable at given level was defined
256 (to emit close instructions later).
258 static void markupval (FuncState
*fs
, int level
) {
259 BlockCnt
*bl
= fs
->bl
;
260 while (bl
->nactvar
> level
)
267 Find variable with given name 'n'. If it is an upvalue, add this
268 upvalue into all intermediate functions.
270 static void singlevaraux (FuncState
*fs
, TString
*n
, expdesc
*var
, int base
) {
271 if (fs
== NULL
) /* no more levels? */
272 init_exp(var
, VVOID
, 0); /* default is global */
274 int v
= searchvar(fs
, n
); /* look up locals at current level */
275 if (v
>= 0) { /* found? */
276 init_exp(var
, VLOCAL
, v
); /* variable is local */
278 markupval(fs
, v
); /* local will be used as an upval */
280 else { /* not found as local at current level; try upvalues */
281 int idx
= searchupvalue(fs
, n
); /* try existing upvalues */
282 if (idx
< 0) { /* not found? */
283 singlevaraux(fs
->prev
, n
, var
, 0); /* try upper levels */
284 if (var
->k
== VVOID
) /* not found? */
285 return; /* it is a global */
286 /* else was LOCAL or UPVAL */
287 idx
= newupvalue(fs
, n
, var
); /* will be a new upvalue */
289 init_exp(var
, VUPVAL
, idx
); /* new or old upvalue */
295 static void singlevar (LexState
*ls
, expdesc
*var
) {
296 TString
*varname
= str_checkname(ls
);
297 FuncState
*fs
= ls
->fs
;
298 singlevaraux(fs
, varname
, var
, 1);
299 if (var
->k
== VVOID
) { /* global name? */
301 singlevaraux(fs
, ls
->envn
, var
, 1); /* get environment variable */
302 lua_assert(var
->k
!= VVOID
); /* this one must exist */
303 codestring(ls
, &key
, varname
); /* key is variable name */
304 luaK_indexed(fs
, var
, &key
); /* env[varname] */
309 static void adjust_assign (LexState
*ls
, int nvars
, int nexps
, expdesc
*e
) {
310 FuncState
*fs
= ls
->fs
;
311 int extra
= nvars
- nexps
;
312 if (hasmultret(e
->k
)) {
313 extra
++; /* includes call itself */
314 if (extra
< 0) extra
= 0;
315 luaK_setreturns(fs
, e
, extra
); /* last exp. provides the difference */
316 if (extra
> 1) luaK_reserveregs(fs
, extra
-1);
319 if (e
->k
!= VVOID
) luaK_exp2nextreg(fs
, e
); /* close last expression */
321 int reg
= fs
->freereg
;
322 luaK_reserveregs(fs
, extra
);
323 luaK_nil(fs
, reg
, extra
);
327 ls
->fs
->freereg
-= nexps
- nvars
; /* remove extra values */
331 static void enterlevel (LexState
*ls
) {
332 lua_State
*L
= ls
->L
;
334 checklimit(ls
->fs
, L
->nCcalls
, LUAI_MAXCCALLS
, "C levels");
338 #define leavelevel(ls) ((ls)->L->nCcalls--)
341 static void closegoto (LexState
*ls
, int g
, Labeldesc
*label
) {
343 FuncState
*fs
= ls
->fs
;
344 Labellist
*gl
= &ls
->dyd
->gt
;
345 Labeldesc
*gt
= &gl
->arr
[g
];
346 lua_assert(eqstr(gt
->name
, label
->name
));
347 if (gt
->nactvar
< label
->nactvar
) {
348 TString
*vname
= getlocvar(fs
, gt
->nactvar
)->varname
;
349 const char *msg
= luaO_pushfstring(ls
->L
,
350 "<goto %s> at line %d jumps into the scope of local '%s'",
351 getstr(gt
->name
), gt
->line
, getstr(vname
));
354 luaK_patchlist(fs
, gt
->pc
, label
->pc
);
355 /* remove goto from pending list */
356 for (i
= g
; i
< gl
->n
- 1; i
++)
357 gl
->arr
[i
] = gl
->arr
[i
+ 1];
363 ** try to close a goto with existing labels; this solves backward jumps
365 static int findlabel (LexState
*ls
, int g
) {
367 BlockCnt
*bl
= ls
->fs
->bl
;
368 Dyndata
*dyd
= ls
->dyd
;
369 Labeldesc
*gt
= &dyd
->gt
.arr
[g
];
370 /* check labels in current block for a match */
371 for (i
= bl
->firstlabel
; i
< dyd
->label
.n
; i
++) {
372 Labeldesc
*lb
= &dyd
->label
.arr
[i
];
373 if (eqstr(lb
->name
, gt
->name
)) { /* correct label? */
374 if (gt
->nactvar
> lb
->nactvar
&&
375 (bl
->upval
|| dyd
->label
.n
> bl
->firstlabel
))
376 luaK_patchclose(ls
->fs
, gt
->pc
, lb
->nactvar
);
377 closegoto(ls
, g
, lb
); /* close it */
381 return 0; /* label not found; cannot close goto */
385 static int newlabelentry (LexState
*ls
, Labellist
*l
, TString
*name
,
388 luaM_growvector(ls
->L
, l
->arr
, n
, l
->size
,
389 Labeldesc
, SHRT_MAX
, "labels/gotos");
390 l
->arr
[n
].name
= name
;
391 l
->arr
[n
].line
= line
;
392 l
->arr
[n
].nactvar
= ls
->fs
->nactvar
;
400 ** check whether new label 'lb' matches any pending gotos in current
401 ** block; solves forward jumps
403 static void findgotos (LexState
*ls
, Labeldesc
*lb
) {
404 Labellist
*gl
= &ls
->dyd
->gt
;
405 int i
= ls
->fs
->bl
->firstgoto
;
407 if (eqstr(gl
->arr
[i
].name
, lb
->name
))
408 closegoto(ls
, i
, lb
);
416 ** export pending gotos to outer level, to check them against
417 ** outer labels; if the block being exited has upvalues, and
418 ** the goto exits the scope of any variable (which can be the
419 ** upvalue), close those variables being exited.
421 static void movegotosout (FuncState
*fs
, BlockCnt
*bl
) {
422 int i
= bl
->firstgoto
;
423 Labellist
*gl
= &fs
->ls
->dyd
->gt
;
424 /* correct pending gotos to current block and try to close it
425 with visible labels */
427 Labeldesc
*gt
= &gl
->arr
[i
];
428 if (gt
->nactvar
> bl
->nactvar
) {
430 luaK_patchclose(fs
, gt
->pc
, bl
->nactvar
);
431 gt
->nactvar
= bl
->nactvar
;
433 if (!findlabel(fs
->ls
, i
))
434 i
++; /* move to next one */
439 static void enterblock (FuncState
*fs
, BlockCnt
*bl
, lu_byte isloop
) {
441 bl
->nactvar
= fs
->nactvar
;
442 bl
->firstlabel
= fs
->ls
->dyd
->label
.n
;
443 bl
->firstgoto
= fs
->ls
->dyd
->gt
.n
;
445 bl
->previous
= fs
->bl
;
447 lua_assert(fs
->freereg
== fs
->nactvar
);
452 ** create a label named 'break' to resolve break statements
454 static void breaklabel (LexState
*ls
) {
455 TString
*n
= luaS_new(ls
->L
, "break");
456 int l
= newlabelentry(ls
, &ls
->dyd
->label
, n
, 0, ls
->fs
->pc
);
457 findgotos(ls
, &ls
->dyd
->label
.arr
[l
]);
461 ** generates an error for an undefined 'goto'; choose appropriate
462 ** message when label name is a reserved word (which can only be 'break')
464 static l_noret
undefgoto (LexState
*ls
, Labeldesc
*gt
) {
465 const char *msg
= isreserved(gt
->name
)
466 ? "<%s> at line %d not inside a loop"
467 : "no visible label '%s' for <goto> at line %d";
468 msg
= luaO_pushfstring(ls
->L
, msg
, getstr(gt
->name
), gt
->line
);
473 static void leaveblock (FuncState
*fs
) {
474 BlockCnt
*bl
= fs
->bl
;
475 LexState
*ls
= fs
->ls
;
476 if (bl
->previous
&& bl
->upval
) {
477 /* create a 'jump to here' to close upvalues */
478 int j
= luaK_jump(fs
);
479 luaK_patchclose(fs
, j
, bl
->nactvar
);
480 luaK_patchtohere(fs
, j
);
483 breaklabel(ls
); /* close pending breaks */
484 fs
->bl
= bl
->previous
;
485 removevars(fs
, bl
->nactvar
);
486 lua_assert(bl
->nactvar
== fs
->nactvar
);
487 fs
->freereg
= fs
->nactvar
; /* free registers */
488 ls
->dyd
->label
.n
= bl
->firstlabel
; /* remove local labels */
489 if (bl
->previous
) /* inner block? */
490 movegotosout(fs
, bl
); /* update pending gotos to outer block */
491 else if (bl
->firstgoto
< ls
->dyd
->gt
.n
) /* pending gotos in outer block? */
492 undefgoto(ls
, &ls
->dyd
->gt
.arr
[bl
->firstgoto
]); /* error */
497 ** adds a new prototype into list of prototypes
499 static Proto
*addprototype (LexState
*ls
) {
501 lua_State
*L
= ls
->L
;
502 FuncState
*fs
= ls
->fs
;
503 Proto
*f
= fs
->f
; /* prototype of current function */
504 if (fs
->np
>= f
->sizep
) {
505 int oldsize
= f
->sizep
;
506 luaM_growvector(L
, f
->p
, fs
->np
, f
->sizep
, Proto
*, MAXARG_Bx
, "functions");
507 while (oldsize
< f
->sizep
)
508 f
->p
[oldsize
++] = NULL
;
510 f
->p
[fs
->np
++] = clp
= luaF_newproto(L
);
511 luaC_objbarrier(L
, f
, clp
);
517 ** codes instruction to create new closure in parent function.
518 ** The OP_CLOSURE instruction must use the last available register,
519 ** so that, if it invokes the GC, the GC knows which registers
520 ** are in use at that time.
522 static void codeclosure (LexState
*ls
, expdesc
*v
) {
523 FuncState
*fs
= ls
->fs
->prev
;
524 init_exp(v
, VRELOCABLE
, luaK_codeABx(fs
, OP_CLOSURE
, 0, fs
->np
- 1));
525 luaK_exp2nextreg(fs
, v
); /* fix it at the last register */
529 static void open_func (LexState
*ls
, FuncState
*fs
, BlockCnt
*bl
) {
531 fs
->prev
= ls
->fs
; /* linked list of funcstates */
543 fs
->firstlocal
= ls
->dyd
->actvar
.n
;
546 f
->source
= ls
->source
;
547 luaC_objbarrier(ls
->L
, f
, f
->source
);
548 f
->maxstacksize
= 2; /* registers 0/1 are always valid */
549 enterblock(fs
, bl
, 0);
553 static void close_func (LexState
*ls
) {
554 lua_State
*L
= ls
->L
;
555 FuncState
*fs
= ls
->fs
;
557 luaK_ret(fs
, 0, 0); /* final return */
559 luaM_reallocvector(L
, f
->code
, f
->sizecode
, fs
->pc
, Instruction
);
560 f
->sizecode
= fs
->pc
;
561 luaM_reallocvector(L
, f
->lineinfo
, f
->sizelineinfo
, fs
->pc
, int);
562 f
->sizelineinfo
= fs
->pc
;
563 luaM_reallocvector(L
, f
->k
, f
->sizek
, fs
->nk
, TValue
);
565 luaM_reallocvector(L
, f
->p
, f
->sizep
, fs
->np
, Proto
*);
567 luaM_reallocvector(L
, f
->locvars
, f
->sizelocvars
, fs
->nlocvars
, LocVar
);
568 f
->sizelocvars
= fs
->nlocvars
;
569 luaM_reallocvector(L
, f
->upvalues
, f
->sizeupvalues
, fs
->nups
, Upvaldesc
);
570 f
->sizeupvalues
= fs
->nups
;
571 lua_assert(fs
->bl
== NULL
);
578 /*============================================================*/
580 /*============================================================*/
584 ** check whether current token is in the follow set of a block.
585 ** 'until' closes syntactical blocks, but do not close scope,
586 ** so it is handled in separate.
588 static int block_follow (LexState
*ls
, int withuntil
) {
589 switch (ls
->t
.token
) {
590 case TK_ELSE
: case TK_ELSEIF
:
591 case TK_END
: case TK_EOS
:
593 case TK_UNTIL
: return withuntil
;
599 static void statlist (LexState
*ls
) {
600 /* statlist -> { stat [';'] } */
601 while (!block_follow(ls
, 1)) {
602 if (ls
->t
.token
== TK_RETURN
) {
604 return; /* 'return' must be last statement */
611 static void fieldsel (LexState
*ls
, expdesc
*v
) {
612 /* fieldsel -> ['.' | ':'] NAME */
613 FuncState
*fs
= ls
->fs
;
615 luaK_exp2anyregup(fs
, v
);
616 luaX_next(ls
); /* skip the dot or colon */
618 luaK_indexed(fs
, v
, &key
);
622 static void yindex (LexState
*ls
, expdesc
*v
) {
623 /* index -> '[' expr ']' */
624 luaX_next(ls
); /* skip the '[' */
626 luaK_exp2val(ls
->fs
, v
);
632 ** {======================================================================
633 ** Rules for Constructors
634 ** =======================================================================
639 expdesc v
; /* last list item read */
640 expdesc
*t
; /* table descriptor */
641 int nh
; /* total number of 'record' elements */
642 int na
; /* total number of array elements */
643 int tostore
; /* number of array elements pending to be stored */
647 static void recfield (LexState
*ls
, struct ConsControl
*cc
) {
648 /* recfield -> (NAME | '['exp1']') = exp1 */
649 FuncState
*fs
= ls
->fs
;
650 int reg
= ls
->fs
->freereg
;
653 if (ls
->t
.token
== TK_NAME
) {
654 checklimit(fs
, cc
->nh
, MAX_INT
, "items in a constructor");
657 else /* ls->t.token == '[' */
661 rkkey
= luaK_exp2RK(fs
, &key
);
663 luaK_codeABC(fs
, OP_SETTABLE
, cc
->t
->u
.info
, rkkey
, luaK_exp2RK(fs
, &val
));
664 fs
->freereg
= reg
; /* free registers */
668 static void closelistfield (FuncState
*fs
, struct ConsControl
*cc
) {
669 if (cc
->v
.k
== VVOID
) return; /* there is no list item */
670 luaK_exp2nextreg(fs
, &cc
->v
);
672 if (cc
->tostore
== LFIELDS_PER_FLUSH
) {
673 luaK_setlist(fs
, cc
->t
->u
.info
, cc
->na
, cc
->tostore
); /* flush */
674 cc
->tostore
= 0; /* no more items pending */
679 static void lastlistfield (FuncState
*fs
, struct ConsControl
*cc
) {
680 if (cc
->tostore
== 0) return;
681 if (hasmultret(cc
->v
.k
)) {
682 luaK_setmultret(fs
, &cc
->v
);
683 luaK_setlist(fs
, cc
->t
->u
.info
, cc
->na
, LUA_MULTRET
);
684 cc
->na
--; /* do not count last expression (unknown number of elements) */
687 if (cc
->v
.k
!= VVOID
)
688 luaK_exp2nextreg(fs
, &cc
->v
);
689 luaK_setlist(fs
, cc
->t
->u
.info
, cc
->na
, cc
->tostore
);
694 static void listfield (LexState
*ls
, struct ConsControl
*cc
) {
695 /* listfield -> exp */
697 checklimit(ls
->fs
, cc
->na
, MAX_INT
, "items in a constructor");
703 static void field (LexState
*ls
, struct ConsControl
*cc
) {
704 /* field -> listfield | recfield */
705 switch(ls
->t
.token
) {
706 case TK_NAME
: { /* may be 'listfield' or 'recfield' */
707 if (luaX_lookahead(ls
) != '=') /* expression? */
725 static void constructor (LexState
*ls
, expdesc
*t
) {
726 /* constructor -> '{' [ field { sep field } [sep] ] '}'
728 FuncState
*fs
= ls
->fs
;
729 int line
= ls
->linenumber
;
730 int pc
= luaK_codeABC(fs
, OP_NEWTABLE
, 0, 0, 0);
731 struct ConsControl cc
;
732 cc
.na
= cc
.nh
= cc
.tostore
= 0;
734 init_exp(t
, VRELOCABLE
, pc
);
735 init_exp(&cc
.v
, VVOID
, 0); /* no value (yet) */
736 luaK_exp2nextreg(ls
->fs
, t
); /* fix it at stack top */
739 lua_assert(cc
.v
.k
== VVOID
|| cc
.tostore
> 0);
740 if (ls
->t
.token
== '}') break;
741 closelistfield(fs
, &cc
);
743 } while (testnext(ls
, ',') || testnext(ls
, ';'));
744 check_match(ls
, '}', '{', line
);
745 lastlistfield(fs
, &cc
);
746 SETARG_B(fs
->f
->code
[pc
], luaO_int2fb(cc
.na
)); /* set initial array size */
747 SETARG_C(fs
->f
->code
[pc
], luaO_int2fb(cc
.nh
)); /* set initial table size */
750 /* }====================================================================== */
754 static void parlist (LexState
*ls
) {
755 /* parlist -> [ param { ',' param } ] */
756 FuncState
*fs
= ls
->fs
;
760 if (ls
->t
.token
!= ')') { /* is 'parlist' not empty? */
762 switch (ls
->t
.token
) {
763 case TK_NAME
: { /* param -> NAME */
764 new_localvar(ls
, str_checkname(ls
));
768 case TK_DOTS
: { /* param -> '...' */
770 f
->is_vararg
= 1; /* declared vararg */
773 default: luaX_syntaxerror(ls
, "<name> or '...' expected");
775 } while (!f
->is_vararg
&& testnext(ls
, ','));
777 adjustlocalvars(ls
, nparams
);
778 f
->numparams
= cast_byte(fs
->nactvar
);
779 luaK_reserveregs(fs
, fs
->nactvar
); /* reserve register for parameters */
783 static void body (LexState
*ls
, expdesc
*e
, int ismethod
, int line
) {
784 /* body -> '(' parlist ')' block END */
787 new_fs
.f
= addprototype(ls
);
788 new_fs
.f
->linedefined
= line
;
789 open_func(ls
, &new_fs
, &bl
);
792 new_localvarliteral(ls
, "self"); /* create 'self' parameter */
793 adjustlocalvars(ls
, 1);
798 new_fs
.f
->lastlinedefined
= ls
->linenumber
;
799 check_match(ls
, TK_END
, TK_FUNCTION
, line
);
805 static int explist (LexState
*ls
, expdesc
*v
) {
806 /* explist -> expr { ',' expr } */
807 int n
= 1; /* at least one expression */
809 while (testnext(ls
, ',')) {
810 luaK_exp2nextreg(ls
->fs
, v
);
818 static void funcargs (LexState
*ls
, expdesc
*f
, int line
) {
819 FuncState
*fs
= ls
->fs
;
822 switch (ls
->t
.token
) {
823 case '(': { /* funcargs -> '(' [ explist ] ')' */
825 if (ls
->t
.token
== ')') /* arg list is empty? */
829 luaK_setmultret(fs
, &args
);
831 check_match(ls
, ')', '(', line
);
834 case '{': { /* funcargs -> constructor */
835 constructor(ls
, &args
);
838 case TK_STRING
: { /* funcargs -> STRING */
839 codestring(ls
, &args
, ls
->t
.seminfo
.ts
);
840 luaX_next(ls
); /* must use 'seminfo' before 'next' */
844 luaX_syntaxerror(ls
, "function arguments expected");
847 lua_assert(f
->k
== VNONRELOC
);
848 base
= f
->u
.info
; /* base register for call */
849 if (hasmultret(args
.k
))
850 nparams
= LUA_MULTRET
; /* open call */
853 luaK_exp2nextreg(fs
, &args
); /* close last argument */
854 nparams
= fs
->freereg
- (base
+1);
856 init_exp(f
, VCALL
, luaK_codeABC(fs
, OP_CALL
, base
, nparams
+1, 2));
857 luaK_fixline(fs
, line
);
858 fs
->freereg
= base
+1; /* call remove function and arguments and leaves
859 (unless changed) one result */
866 ** {======================================================================
867 ** Expression parsing
868 ** =======================================================================
872 static void primaryexp (LexState
*ls
, expdesc
*v
) {
873 /* primaryexp -> NAME | '(' expr ')' */
874 switch (ls
->t
.token
) {
876 int line
= ls
->linenumber
;
879 check_match(ls
, ')', '(', line
);
880 luaK_dischargevars(ls
->fs
, v
);
888 luaX_syntaxerror(ls
, "unexpected symbol");
894 static void suffixedexp (LexState
*ls
, expdesc
*v
) {
896 primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
897 FuncState
*fs
= ls
->fs
;
898 int line
= ls
->linenumber
;
901 switch (ls
->t
.token
) {
902 case '.': { /* fieldsel */
906 case '[': { /* '[' exp1 ']' */
908 luaK_exp2anyregup(fs
, v
);
910 luaK_indexed(fs
, v
, &key
);
913 case ':': { /* ':' NAME funcargs */
917 luaK_self(fs
, v
, &key
);
918 funcargs(ls
, v
, line
);
921 case '(': case TK_STRING
: case '{': { /* funcargs */
922 luaK_exp2nextreg(fs
, v
);
923 funcargs(ls
, v
, line
);
932 static void simpleexp (LexState
*ls
, expdesc
*v
) {
933 /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... |
934 constructor | FUNCTION body | suffixedexp */
935 switch (ls
->t
.token
) {
937 init_exp(v
, VKFLT
, 0);
938 v
->u
.nval
= ls
->t
.seminfo
.r
;
942 init_exp(v
, VKINT
, 0);
943 v
->u
.ival
= ls
->t
.seminfo
.i
;
947 codestring(ls
, v
, ls
->t
.seminfo
.ts
);
951 init_exp(v
, VNIL
, 0);
955 init_exp(v
, VTRUE
, 0);
959 init_exp(v
, VFALSE
, 0);
962 case TK_DOTS
: { /* vararg */
963 FuncState
*fs
= ls
->fs
;
964 check_condition(ls
, fs
->f
->is_vararg
,
965 "cannot use '...' outside a vararg function");
966 init_exp(v
, VVARARG
, luaK_codeABC(fs
, OP_VARARG
, 0, 1, 0));
969 case '{': { /* constructor */
975 body(ls
, v
, 0, ls
->linenumber
);
987 static UnOpr
getunopr (int op
) {
989 case TK_NOT
: return OPR_NOT
;
990 case '-': return OPR_MINUS
;
991 case '~': return OPR_BNOT
;
992 case '#': return OPR_LEN
;
993 default: return OPR_NOUNOPR
;
998 static BinOpr
getbinopr (int op
) {
1000 case '+': return OPR_ADD
;
1001 case '-': return OPR_SUB
;
1002 case '*': return OPR_MUL
;
1003 case '%': return OPR_MOD
;
1004 case '^': return OPR_POW
;
1005 case '/': return OPR_DIV
;
1006 case TK_IDIV
: return OPR_IDIV
;
1007 case '&': return OPR_BAND
;
1008 case '|': return OPR_BOR
;
1009 case '~': return OPR_BXOR
;
1010 case TK_SHL
: return OPR_SHL
;
1011 case TK_SHR
: return OPR_SHR
;
1012 case TK_CONCAT
: return OPR_CONCAT
;
1013 case TK_NE
: return OPR_NE
;
1014 case TK_EQ
: return OPR_EQ
;
1015 case '<': return OPR_LT
;
1016 case TK_LE
: return OPR_LE
;
1017 case '>': return OPR_GT
;
1018 case TK_GE
: return OPR_GE
;
1019 case TK_AND
: return OPR_AND
;
1020 case TK_OR
: return OPR_OR
;
1021 default: return OPR_NOBINOPR
;
1026 static const struct {
1027 lu_byte left
; /* left priority for each binary operator */
1028 lu_byte right
; /* right priority */
1029 } priority
[] = { /* ORDER OPR */
1030 {10, 10}, {10, 10}, /* '+' '-' */
1031 {11, 11}, {11, 11}, /* '*' '%' */
1032 {14, 13}, /* '^' (right associative) */
1033 {11, 11}, {11, 11}, /* '/' '//' */
1034 {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */
1035 {7, 7}, {7, 7}, /* '<<' '>>' */
1036 {9, 8}, /* '..' (right associative) */
1037 {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
1038 {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
1039 {2, 2}, {1, 1} /* and, or */
1042 #define UNARY_PRIORITY 12 /* priority for unary operators */
1046 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
1047 ** where 'binop' is any binary operator with a priority higher than 'limit'
1049 static BinOpr
subexpr (LexState
*ls
, expdesc
*v
, int limit
) {
1053 uop
= getunopr(ls
->t
.token
);
1054 if (uop
!= OPR_NOUNOPR
) {
1055 int line
= ls
->linenumber
;
1057 subexpr(ls
, v
, UNARY_PRIORITY
);
1058 luaK_prefix(ls
->fs
, uop
, v
, line
);
1060 else simpleexp(ls
, v
);
1061 /* expand while operators have priorities higher than 'limit' */
1062 op
= getbinopr(ls
->t
.token
);
1063 while (op
!= OPR_NOBINOPR
&& priority
[op
].left
> limit
) {
1066 int line
= ls
->linenumber
;
1068 luaK_infix(ls
->fs
, op
, v
);
1069 /* read sub-expression with higher priority */
1070 nextop
= subexpr(ls
, &v2
, priority
[op
].right
);
1071 luaK_posfix(ls
->fs
, op
, v
, &v2
, line
);
1075 return op
; /* return first untreated operator */
1079 static void expr (LexState
*ls
, expdesc
*v
) {
1083 /* }==================================================================== */
1088 ** {======================================================================
1089 ** Rules for Statements
1090 ** =======================================================================
1094 static void block (LexState
*ls
) {
1095 /* block -> statlist */
1096 FuncState
*fs
= ls
->fs
;
1098 enterblock(fs
, &bl
, 0);
1105 ** structure to chain all variables in the left-hand side of an
1109 struct LHS_assign
*prev
;
1110 expdesc v
; /* variable (global, local, upvalue, or indexed) */
1115 ** check whether, in an assignment to an upvalue/local variable, the
1116 ** upvalue/local variable is begin used in a previous assignment to a
1117 ** table. If so, save original upvalue/local value in a safe place and
1118 ** use this safe copy in the previous assignment.
1120 static void check_conflict (LexState
*ls
, struct LHS_assign
*lh
, expdesc
*v
) {
1121 FuncState
*fs
= ls
->fs
;
1122 int extra
= fs
->freereg
; /* eventual position to save local variable */
1124 for (; lh
; lh
= lh
->prev
) { /* check all previous assignments */
1125 if (lh
->v
.k
== VINDEXED
) { /* assigning to a table? */
1126 /* table is the upvalue/local being assigned now? */
1127 if (lh
->v
.u
.ind
.vt
== v
->k
&& lh
->v
.u
.ind
.t
== v
->u
.info
) {
1129 lh
->v
.u
.ind
.vt
= VLOCAL
;
1130 lh
->v
.u
.ind
.t
= extra
; /* previous assignment will use safe copy */
1132 /* index is the local being assigned? (index cannot be upvalue) */
1133 if (v
->k
== VLOCAL
&& lh
->v
.u
.ind
.idx
== v
->u
.info
) {
1135 lh
->v
.u
.ind
.idx
= extra
; /* previous assignment will use safe copy */
1140 /* copy upvalue/local value to a temporary (in position 'extra') */
1141 OpCode op
= (v
->k
== VLOCAL
) ? OP_MOVE
: OP_GETUPVAL
;
1142 luaK_codeABC(fs
, op
, extra
, v
->u
.info
, 0);
1143 luaK_reserveregs(fs
, 1);
1148 static void assignment (LexState
*ls
, struct LHS_assign
*lh
, int nvars
) {
1150 check_condition(ls
, vkisvar(lh
->v
.k
), "syntax error");
1151 if (testnext(ls
, ',')) { /* assignment -> ',' suffixedexp assignment */
1152 struct LHS_assign nv
;
1154 suffixedexp(ls
, &nv
.v
);
1155 if (nv
.v
.k
!= VINDEXED
)
1156 check_conflict(ls
, lh
, &nv
.v
);
1157 checklimit(ls
->fs
, nvars
+ ls
->L
->nCcalls
, LUAI_MAXCCALLS
,
1159 assignment(ls
, &nv
, nvars
+1);
1161 else { /* assignment -> '=' explist */
1164 nexps
= explist(ls
, &e
);
1166 adjust_assign(ls
, nvars
, nexps
, &e
);
1168 luaK_setoneret(ls
->fs
, &e
); /* close last expression */
1169 luaK_storevar(ls
->fs
, &lh
->v
, &e
);
1170 return; /* avoid default */
1173 init_exp(&e
, VNONRELOC
, ls
->fs
->freereg
-1); /* default assignment */
1174 luaK_storevar(ls
->fs
, &lh
->v
, &e
);
1178 static int cond (LexState
*ls
) {
1181 expr(ls
, &v
); /* read condition */
1182 if (v
.k
== VNIL
) v
.k
= VFALSE
; /* 'falses' are all equal here */
1183 luaK_goiftrue(ls
->fs
, &v
);
1188 static void gotostat (LexState
*ls
, int pc
) {
1189 int line
= ls
->linenumber
;
1192 if (testnext(ls
, TK_GOTO
))
1193 label
= str_checkname(ls
);
1195 luaX_next(ls
); /* skip break */
1196 label
= luaS_new(ls
->L
, "break");
1198 g
= newlabelentry(ls
, &ls
->dyd
->gt
, label
, line
, pc
);
1199 findlabel(ls
, g
); /* close it if label already defined */
1203 /* check for repeated labels on the same block */
1204 static void checkrepeated (FuncState
*fs
, Labellist
*ll
, TString
*label
) {
1206 for (i
= fs
->bl
->firstlabel
; i
< ll
->n
; i
++) {
1207 if (eqstr(label
, ll
->arr
[i
].name
)) {
1208 const char *msg
= luaO_pushfstring(fs
->ls
->L
,
1209 "label '%s' already defined on line %d",
1210 getstr(label
), ll
->arr
[i
].line
);
1211 semerror(fs
->ls
, msg
);
1217 /* skip no-op statements */
1218 static void skipnoopstat (LexState
*ls
) {
1219 while (ls
->t
.token
== ';' || ls
->t
.token
== TK_DBCOLON
)
1224 static void labelstat (LexState
*ls
, TString
*label
, int line
) {
1225 /* label -> '::' NAME '::' */
1226 FuncState
*fs
= ls
->fs
;
1227 Labellist
*ll
= &ls
->dyd
->label
;
1228 int l
; /* index of new label being created */
1229 checkrepeated(fs
, ll
, label
); /* check for repeated labels */
1230 checknext(ls
, TK_DBCOLON
); /* skip double colon */
1231 /* create new entry for this label */
1232 l
= newlabelentry(ls
, ll
, label
, line
, luaK_getlabel(fs
));
1233 skipnoopstat(ls
); /* skip other no-op statements */
1234 if (block_follow(ls
, 0)) { /* label is last no-op statement in the block? */
1235 /* assume that locals are already out of scope */
1236 ll
->arr
[l
].nactvar
= fs
->bl
->nactvar
;
1238 findgotos(ls
, &ll
->arr
[l
]);
1242 static void whilestat (LexState
*ls
, int line
) {
1243 /* whilestat -> WHILE cond DO block END */
1244 FuncState
*fs
= ls
->fs
;
1248 luaX_next(ls
); /* skip WHILE */
1249 whileinit
= luaK_getlabel(fs
);
1250 condexit
= cond(ls
);
1251 enterblock(fs
, &bl
, 1);
1252 checknext(ls
, TK_DO
);
1254 luaK_jumpto(fs
, whileinit
);
1255 check_match(ls
, TK_END
, TK_WHILE
, line
);
1257 luaK_patchtohere(fs
, condexit
); /* false conditions finish the loop */
1261 static void repeatstat (LexState
*ls
, int line
) {
1262 /* repeatstat -> REPEAT block UNTIL cond */
1264 FuncState
*fs
= ls
->fs
;
1265 int repeat_init
= luaK_getlabel(fs
);
1267 enterblock(fs
, &bl1
, 1); /* loop block */
1268 enterblock(fs
, &bl2
, 0); /* scope block */
1269 luaX_next(ls
); /* skip REPEAT */
1271 check_match(ls
, TK_UNTIL
, TK_REPEAT
, line
);
1272 condexit
= cond(ls
); /* read condition (inside scope block) */
1273 if (bl2
.upval
) /* upvalues? */
1274 luaK_patchclose(fs
, condexit
, bl2
.nactvar
);
1275 leaveblock(fs
); /* finish scope */
1276 luaK_patchlist(fs
, condexit
, repeat_init
); /* close the loop */
1277 leaveblock(fs
); /* finish loop */
1281 static int exp1 (LexState
*ls
) {
1285 luaK_exp2nextreg(ls
->fs
, &e
);
1286 lua_assert(e
.k
== VNONRELOC
);
1292 static void forbody (LexState
*ls
, int base
, int line
, int nvars
, int isnum
) {
1293 /* forbody -> DO block */
1295 FuncState
*fs
= ls
->fs
;
1297 adjustlocalvars(ls
, 3); /* control variables */
1298 checknext(ls
, TK_DO
);
1299 prep
= isnum
? luaK_codeAsBx(fs
, OP_FORPREP
, base
, NO_JUMP
) : luaK_jump(fs
);
1300 enterblock(fs
, &bl
, 0); /* scope for declared variables */
1301 adjustlocalvars(ls
, nvars
);
1302 luaK_reserveregs(fs
, nvars
);
1304 leaveblock(fs
); /* end of scope for declared variables */
1305 luaK_patchtohere(fs
, prep
);
1306 if (isnum
) /* numeric for? */
1307 endfor
= luaK_codeAsBx(fs
, OP_FORLOOP
, base
, NO_JUMP
);
1308 else { /* generic for */
1309 luaK_codeABC(fs
, OP_TFORCALL
, base
, 0, nvars
);
1310 luaK_fixline(fs
, line
);
1311 endfor
= luaK_codeAsBx(fs
, OP_TFORLOOP
, base
+ 2, NO_JUMP
);
1313 luaK_patchlist(fs
, endfor
, prep
+ 1);
1314 luaK_fixline(fs
, line
);
1318 static void fornum (LexState
*ls
, TString
*varname
, int line
) {
1319 /* fornum -> NAME = exp1,exp1[,exp1] forbody */
1320 FuncState
*fs
= ls
->fs
;
1321 int base
= fs
->freereg
;
1322 new_localvarliteral(ls
, "(for index)");
1323 new_localvarliteral(ls
, "(for limit)");
1324 new_localvarliteral(ls
, "(for step)");
1325 new_localvar(ls
, varname
);
1327 exp1(ls
); /* initial value */
1329 exp1(ls
); /* limit */
1330 if (testnext(ls
, ','))
1331 exp1(ls
); /* optional step */
1332 else { /* default step = 1 */
1333 luaK_codek(fs
, fs
->freereg
, luaK_intK(fs
, 1));
1334 luaK_reserveregs(fs
, 1);
1336 forbody(ls
, base
, line
, 1, 1);
1340 static void forlist (LexState
*ls
, TString
*indexname
) {
1341 /* forlist -> NAME {,NAME} IN explist forbody */
1342 FuncState
*fs
= ls
->fs
;
1344 int nvars
= 4; /* gen, state, control, plus at least one declared var */
1346 int base
= fs
->freereg
;
1347 /* create control variables */
1348 new_localvarliteral(ls
, "(for generator)");
1349 new_localvarliteral(ls
, "(for state)");
1350 new_localvarliteral(ls
, "(for control)");
1351 /* create declared variables */
1352 new_localvar(ls
, indexname
);
1353 while (testnext(ls
, ',')) {
1354 new_localvar(ls
, str_checkname(ls
));
1357 checknext(ls
, TK_IN
);
1358 line
= ls
->linenumber
;
1359 adjust_assign(ls
, 3, explist(ls
, &e
), &e
);
1360 luaK_checkstack(fs
, 3); /* extra space to call generator */
1361 forbody(ls
, base
, line
, nvars
- 3, 0);
1365 static void forstat (LexState
*ls
, int line
) {
1366 /* forstat -> FOR (fornum | forlist) END */
1367 FuncState
*fs
= ls
->fs
;
1370 enterblock(fs
, &bl
, 1); /* scope for loop and control variables */
1371 luaX_next(ls
); /* skip 'for' */
1372 varname
= str_checkname(ls
); /* first variable name */
1373 switch (ls
->t
.token
) {
1374 case '=': fornum(ls
, varname
, line
); break;
1375 case ',': case TK_IN
: forlist(ls
, varname
); break;
1376 default: luaX_syntaxerror(ls
, "'=' or 'in' expected");
1378 check_match(ls
, TK_END
, TK_FOR
, line
);
1379 leaveblock(fs
); /* loop scope ('break' jumps to this point) */
1383 static void test_then_block (LexState
*ls
, int *escapelist
) {
1384 /* test_then_block -> [IF | ELSEIF] cond THEN block */
1386 FuncState
*fs
= ls
->fs
;
1388 int jf
; /* instruction to skip 'then' code (if condition is false) */
1389 luaX_next(ls
); /* skip IF or ELSEIF */
1390 expr(ls
, &v
); /* read condition */
1391 checknext(ls
, TK_THEN
);
1392 if (ls
->t
.token
== TK_GOTO
|| ls
->t
.token
== TK_BREAK
) {
1393 luaK_goiffalse(ls
->fs
, &v
); /* will jump to label if condition is true */
1394 enterblock(fs
, &bl
, 0); /* must enter block before 'goto' */
1395 gotostat(ls
, v
.t
); /* handle goto/break */
1396 while (testnext(ls
, ';')) {} /* skip colons */
1397 if (block_follow(ls
, 0)) { /* 'goto' is the entire block? */
1399 return; /* and that is it */
1401 else /* must skip over 'then' part if condition is false */
1404 else { /* regular case (not goto/break) */
1405 luaK_goiftrue(ls
->fs
, &v
); /* skip over block if condition is false */
1406 enterblock(fs
, &bl
, 0);
1409 statlist(ls
); /* 'then' part */
1411 if (ls
->t
.token
== TK_ELSE
||
1412 ls
->t
.token
== TK_ELSEIF
) /* followed by 'else'/'elseif'? */
1413 luaK_concat(fs
, escapelist
, luaK_jump(fs
)); /* must jump over it */
1414 luaK_patchtohere(fs
, jf
);
1418 static void ifstat (LexState
*ls
, int line
) {
1419 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
1420 FuncState
*fs
= ls
->fs
;
1421 int escapelist
= NO_JUMP
; /* exit list for finished parts */
1422 test_then_block(ls
, &escapelist
); /* IF cond THEN block */
1423 while (ls
->t
.token
== TK_ELSEIF
)
1424 test_then_block(ls
, &escapelist
); /* ELSEIF cond THEN block */
1425 if (testnext(ls
, TK_ELSE
))
1426 block(ls
); /* 'else' part */
1427 check_match(ls
, TK_END
, TK_IF
, line
);
1428 luaK_patchtohere(fs
, escapelist
); /* patch escape list to 'if' end */
1432 static void localfunc (LexState
*ls
) {
1434 FuncState
*fs
= ls
->fs
;
1435 new_localvar(ls
, str_checkname(ls
)); /* new local variable */
1436 adjustlocalvars(ls
, 1); /* enter its scope */
1437 body(ls
, &b
, 0, ls
->linenumber
); /* function created in next register */
1438 /* debug information will only see the variable after this point! */
1439 getlocvar(fs
, b
.u
.info
)->startpc
= fs
->pc
;
1443 static void localstat (LexState
*ls
) {
1444 /* stat -> LOCAL NAME {',' NAME} ['=' explist] */
1449 new_localvar(ls
, str_checkname(ls
));
1451 } while (testnext(ls
, ','));
1452 if (testnext(ls
, '='))
1453 nexps
= explist(ls
, &e
);
1458 adjust_assign(ls
, nvars
, nexps
, &e
);
1459 adjustlocalvars(ls
, nvars
);
1463 static int funcname (LexState
*ls
, expdesc
*v
) {
1464 /* funcname -> NAME {fieldsel} [':' NAME] */
1467 while (ls
->t
.token
== '.')
1469 if (ls
->t
.token
== ':') {
1477 static void funcstat (LexState
*ls
, int line
) {
1478 /* funcstat -> FUNCTION funcname body */
1481 luaX_next(ls
); /* skip FUNCTION */
1482 ismethod
= funcname(ls
, &v
);
1483 body(ls
, &b
, ismethod
, line
);
1484 luaK_storevar(ls
->fs
, &v
, &b
);
1485 luaK_fixline(ls
->fs
, line
); /* definition "happens" in the first line */
1489 static void exprstat (LexState
*ls
) {
1490 /* stat -> func | assignment */
1491 FuncState
*fs
= ls
->fs
;
1492 struct LHS_assign v
;
1493 suffixedexp(ls
, &v
.v
);
1494 if (ls
->t
.token
== '=' || ls
->t
.token
== ',') { /* stat -> assignment ? */
1496 assignment(ls
, &v
, 1);
1498 else { /* stat -> func */
1499 check_condition(ls
, v
.v
.k
== VCALL
, "syntax error");
1500 SETARG_C(getinstruction(fs
, &v
.v
), 1); /* call statement uses no results */
1505 static void retstat (LexState
*ls
) {
1506 /* stat -> RETURN [explist] [';'] */
1507 FuncState
*fs
= ls
->fs
;
1509 int first
, nret
; /* registers with returned values */
1510 if (block_follow(ls
, 1) || ls
->t
.token
== ';')
1511 first
= nret
= 0; /* return no values */
1513 nret
= explist(ls
, &e
); /* optional return values */
1514 if (hasmultret(e
.k
)) {
1515 luaK_setmultret(fs
, &e
);
1516 if (e
.k
== VCALL
&& nret
== 1) { /* tail call? */
1517 SET_OPCODE(getinstruction(fs
,&e
), OP_TAILCALL
);
1518 lua_assert(GETARG_A(getinstruction(fs
,&e
)) == fs
->nactvar
);
1520 first
= fs
->nactvar
;
1521 nret
= LUA_MULTRET
; /* return all values */
1524 if (nret
== 1) /* only one single value? */
1525 first
= luaK_exp2anyreg(fs
, &e
);
1527 luaK_exp2nextreg(fs
, &e
); /* values must go to the stack */
1528 first
= fs
->nactvar
; /* return all active values */
1529 lua_assert(nret
== fs
->freereg
- first
);
1533 luaK_ret(fs
, first
, nret
);
1534 testnext(ls
, ';'); /* skip optional semicolon */
1538 static void statement (LexState
*ls
) {
1539 int line
= ls
->linenumber
; /* may be needed for error messages */
1541 switch (ls
->t
.token
) {
1542 case ';': { /* stat -> ';' (empty statement) */
1543 luaX_next(ls
); /* skip ';' */
1546 case TK_IF
: { /* stat -> ifstat */
1550 case TK_WHILE
: { /* stat -> whilestat */
1551 whilestat(ls
, line
);
1554 case TK_DO
: { /* stat -> DO block END */
1555 luaX_next(ls
); /* skip DO */
1557 check_match(ls
, TK_END
, TK_DO
, line
);
1560 case TK_FOR
: { /* stat -> forstat */
1564 case TK_REPEAT
: { /* stat -> repeatstat */
1565 repeatstat(ls
, line
);
1568 case TK_FUNCTION
: { /* stat -> funcstat */
1572 case TK_LOCAL
: { /* stat -> localstat */
1573 luaX_next(ls
); /* skip LOCAL */
1574 if (testnext(ls
, TK_FUNCTION
)) /* local function? */
1580 case TK_DBCOLON
: { /* stat -> label */
1581 luaX_next(ls
); /* skip double colon */
1582 labelstat(ls
, str_checkname(ls
), line
);
1585 case TK_RETURN
: { /* stat -> retstat */
1586 luaX_next(ls
); /* skip RETURN */
1590 case TK_BREAK
: /* stat -> breakstat */
1591 case TK_GOTO
: { /* stat -> 'goto' NAME */
1592 gotostat(ls
, luaK_jump(ls
->fs
));
1595 default: { /* stat -> func | assignment */
1600 lua_assert(ls
->fs
->f
->maxstacksize
>= ls
->fs
->freereg
&&
1601 ls
->fs
->freereg
>= ls
->fs
->nactvar
);
1602 ls
->fs
->freereg
= ls
->fs
->nactvar
; /* free registers */
1606 /* }====================================================================== */
1610 ** compiles the main function, which is a regular vararg function with an
1611 ** upvalue named LUA_ENV
1613 static void mainfunc (LexState
*ls
, FuncState
*fs
) {
1616 open_func(ls
, fs
, &bl
);
1617 fs
->f
->is_vararg
= 1; /* main function is always declared vararg */
1618 init_exp(&v
, VLOCAL
, 0); /* create and... */
1619 newupvalue(fs
, ls
->envn
, &v
); /* ...set environment upvalue */
1620 luaC_objbarrier(ls
->L
, fs
->f
, ls
->envn
);
1621 luaX_next(ls
); /* read first token */
1622 statlist(ls
); /* parse main body */
1628 LClosure
*luaY_parser (lua_State
*L
, ZIO
*z
, Mbuffer
*buff
,
1629 Dyndata
*dyd
, const char *name
, int firstchar
) {
1631 FuncState funcstate
;
1632 LClosure
*cl
= luaF_newLclosure(L
, 1); /* create main closure */
1633 setclLvalue(L
, L
->top
, cl
); /* anchor it (to avoid being collected) */
1635 lexstate
.h
= luaH_new(L
); /* create table for scanner */
1636 sethvalue(L
, L
->top
, lexstate
.h
); /* anchor it */
1638 funcstate
.f
= cl
->p
= luaF_newproto(L
);
1639 luaC_objbarrier(L
, cl
, cl
->p
);
1640 funcstate
.f
->source
= luaS_new(L
, name
); /* create and anchor TString */
1641 lua_assert(iswhite(funcstate
.f
)); /* do not need barrier here */
1642 lexstate
.buff
= buff
;
1644 dyd
->actvar
.n
= dyd
->gt
.n
= dyd
->label
.n
= 0;
1645 luaX_setinput(L
, &lexstate
, z
, funcstate
.f
->source
, firstchar
);
1646 mainfunc(&lexstate
, &funcstate
);
1647 lua_assert(!funcstate
.prev
&& funcstate
.nups
== 1 && !lexstate
.fs
);
1648 /* all scopes should be correctly finished */
1649 lua_assert(dyd
->actvar
.n
== 0 && dyd
->gt
.n
== 0 && dyd
->label
.n
== 0);
1650 L
->top
--; /* remove scanner's table */
1651 return cl
; /* closure is on the stack, too */