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