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