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