History

Lua 5.0

Lua Team committed 23 years ago

Commit 488a64b1 parent c2bb718138 files · +16186 −12300Browse files

Showing the changes to src/lparser.c (1 of 138 files). Show all

Unified · Split

src/lparser.c+787−582
@@ -1,16 +1,18 @@
11 /*
2−** $Id: lparser.c,v 1.117 2000/11/29 11:57:42 roberto Exp $
3−** LL(1) Parser and code generator for Lua
2+** $Id: lparser.c,v 1.208 2003/04/03 13:35:34 roberto Exp $
3+** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
66
77
8−#include <stdio.h>
98 #include <string.h>
109
10+#define lparser_c
11+
1112 #include "lua.h"
1213
1314 #include "lcode.h"
15+#include "ldebug.h"
1416 #include "lfunc.h"
1517 #include "llex.h"
1618 #include "lmem.h"
@@ -21,35 +23,34 @@ · 4 unmodified lines
2123 #include "lstring.h"
2224
2325
24−/*
25−** Constructors descriptor:
26−** `n' indicates number of elements, and `k' signals whether
27−** it is a list constructor (k = 0) or a record constructor (k = 1)
28−** or empty (k = ';' or '}')
29−*/
30−typedef struct Constdesc {
31− int n;
32− int k;
33−} Constdesc;
3426
3527
36−typedef struct Breaklabel {
37− struct Breaklabel *previous; /* chain */
38− int breaklist;
39− int stacklevel;
40−} Breaklabel;
28+#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]])
29+
30+
31+#define enterlevel(ls) if (++(ls)->nestlevel > LUA_MAXPARSERLEVEL) \
32+ luaX_syntaxerror(ls, "too many syntax levels");
33+#define leavelevel(ls) ((ls)->nestlevel--)
4134
4235
36+/*
37+** nodes for block list (list of active blocks)
38+*/
39+typedef struct BlockCnt {
40+ struct BlockCnt *previous; /* chain */
41+ int breaklist; /* list of jumps out of this loop */
42+ int nactvar; /* # active local variables outside the breakable structure */
43+ int upval; /* true if some variable in the block is an upvalue */
44+ int isbreakable; /* true if `block' is a loop */
45+} BlockCnt;
46+
4347
4448
4549 /*
4650 ** prototypes for recursive non-terminal functions
4751 */
48−static void body (LexState *ls, int needself, int line);
4952 static void chunk (LexState *ls);
50−static void constructor (LexState *ls);
5153 static void expr (LexState *ls, expdesc *v);
52−static void exp1 (LexState *ls);
5354
5455
5556
@@ -65,32 +66,18 @@ static void next (LexState *ls) { · 9 unmodified lines
6566
6667
6768 static void lookahead (LexState *ls) {
68− LUA_ASSERT(ls->lookahead.token == TK_EOS, "two look-aheads");
69+ lua_assert(ls->lookahead.token == TK_EOS);
6970 ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
7071 }
7172
7273
7374 static void error_expected (LexState *ls, int token) {
74− char buff[100], t[TOKEN_LEN];
75− luaX_token2str(token, t);
76− sprintf(buff, "`%.20s' expected", t);
77− luaK_error(ls, buff);
75+ luaX_syntaxerror(ls,
76+ luaO_pushfstring(ls->L, "`%s' expected", luaX_token2str(ls, token)));
7877 }
7978
8079
81−static void check (LexState *ls, int c) {
82− if (ls->t.token != c)
83− error_expected(ls, c);
84− next(ls);
85−}
86−
87−
88−static void check_condition (LexState *ls, int c, const char *msg) {
89− if (!c) luaK_error(ls, msg);
90−}
91−
92−
93−static int optional (LexState *ls, int c) {
80+static int testnext (LexState *ls, int c) {
9481 if (ls->t.token == c) {
9582 next(ls);
9683 return 1;
@@ -99,40 +86,26 @@ static int optional (LexState *ls, int c) { · 2 unmodified lines
9986 }
10087
10188
89+static void check (LexState *ls, int c) {
90+ if (!testnext(ls, c))
91+ error_expected(ls, c);
92+}
93+
94+
95+#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
96+
97+
98+
10299 static void check_match (LexState *ls, int what, int who, int where) {
103− if (ls->t.token != what) {
100+ if (!testnext(ls, what)) {
104101 if (where == ls->linenumber)
105102 error_expected(ls, what);
106103 else {
107− char buff[100];
108− char t_what[TOKEN_LEN], t_who[TOKEN_LEN];
109− luaX_token2str(what, t_what);
110− luaX_token2str(who, t_who);
111− sprintf(buff, "`%.20s' expected (to close `%.20s' at line %d)",
112− t_what, t_who, where);
113− luaK_error(ls, buff);
104+ luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
105+ "`%s' expected (to close `%s' at line %d)",
106+ luaX_token2str(ls, what), luaX_token2str(ls, who), where));
114107 }
115108 }
116− next(ls);
117−}
118−
119−
120−static int string_constant (FuncState *fs, TString *s) {
121− Proto *f = fs->f;
122− int c = s->u.s.constindex;
123− if (c >= f->nkstr || f->kstr[c] != s) {
124− luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *,
125− "constant table overflow", MAXARG_U);
126− c = f->nkstr++;
127− f->kstr[c] = s;
128− s->u.s.constindex = c; /* hint for next time */
129− }
130− return c;
131−}
132−
133−
134−static void code_string (LexState *ls, TString *s) {
135− luaK_kstr(ls, string_constant(ls->fs, s));
136109 }
137110
138111
@@ -145,182 +118,224 @@ static TString *str_checkname (LexState *ls) { · 6 unmodified lines
145118 }
146119
147120
148−static int checkname (LexState *ls) {
149− return string_constant(ls->fs, str_checkname(ls));
121+static void init_exp (expdesc *e, expkind k, int i) {
122+ e->f = e->t = NO_JUMP;
123+ e->k = k;
124+ e->info = i;
125+}
126+
127+
128+static void codestring (LexState *ls, expdesc *e, TString *s) {
129+ init_exp(e, VK, luaK_stringK(ls->fs, s));
130+}
131+
132+
133+static void checkname(LexState *ls, expdesc *e) {
134+ codestring(ls, e, str_checkname(ls));
150135 }
151136
152137
153138 static int luaI_registerlocalvar (LexState *ls, TString *varname) {
154− Proto *f = ls->fs->f;
155− luaM_growvector(ls->L, f->locvars, f->nlocvars, 1, LocVar, "", MAX_INT);
156− f->locvars[f->nlocvars].varname = varname;
157− return f->nlocvars++;
139+ FuncState *fs = ls->fs;
140+ Proto *f = fs->f;
141+ luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
142+ LocVar, MAX_INT, "");
143+ f->locvars[fs->nlocvars].varname = varname;
144+ return fs->nlocvars++;
158145 }
159146
160147
161148 static void new_localvar (LexState *ls, TString *name, int n) {
162149 FuncState *fs = ls->fs;
163− luaX_checklimit(ls, fs->nactloc+n+1, MAXLOCALS, "local variables");
164− fs->actloc[fs->nactloc+n] = luaI_registerlocalvar(ls, name);
150+ luaX_checklimit(ls, fs->nactvar+n+1, MAXVARS, "local variables");
151+ fs->actvar[fs->nactvar+n] = luaI_registerlocalvar(ls, name);
165152 }
166153
167154
168155 static void adjustlocalvars (LexState *ls, int nvars) {
169156 FuncState *fs = ls->fs;
170− while (nvars--)
171− fs->f->locvars[fs->actloc[fs->nactloc++]].startpc = fs->pc;
157+ fs->nactvar += nvars;
158+ for (; nvars; nvars--) {
159+ getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
160+ }
172161 }
173162
174163
175−static void removelocalvars (LexState *ls, int nvars) {
164+static void removevars (LexState *ls, int tolevel) {
176165 FuncState *fs = ls->fs;
177− while (nvars--)
178− fs->f->locvars[fs->actloc[--fs->nactloc]].endpc = fs->pc;
166+ while (fs->nactvar > tolevel)
167+ getlocvar(fs, --fs->nactvar).endpc = fs->pc;
179168 }
180169
181170
182171 static void new_localvarstr (LexState *ls, const char *name, int n) {
183− new_localvar(ls, luaS_newfixed(ls->L, name), n);
172+ new_localvar(ls, luaS_new(ls->L, name), n);
184173 }
185174
186175
187−static int search_local (LexState *ls, TString *n, expdesc *var) {
188− FuncState *fs;
189− int level = 0;
190− for (fs=ls->fs; fs; fs=fs->prev) {
191− int i;
192− for (i=fs->nactloc-1; i >= 0; i--) {
193− if (n == fs->f->locvars[fs->actloc[i]].varname) {
194− var->k = VLOCAL;
195− var->u.index = i;
196− return level;
197− }
198− }
199− level++; /* `var' not found; check outer level */
200− }
201− var->k = VGLOBAL; /* not found in any level; must be global */
202− return -1;
176+static void create_local (LexState *ls, const char *name) {
177+ new_localvarstr(ls, name, 0);
178+ adjustlocalvars(ls, 1);
203179 }
204180
205181
206−static void singlevar (LexState *ls, TString *n, expdesc *var) {
207− int level = search_local(ls, n, var);
208− if (level >= 1) /* neither local (0) nor global (-1)? */
209− luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
210− else if (level == -1) /* global? */
211− var->u.index = string_constant(ls->fs, n);
182+static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
183+ int i;
184+ Proto *f = fs->f;
185+ for (i=0; i<f->nups; i++) {
186+ if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->info) {
187+ lua_assert(fs->f->upvalues[i] == name);
188+ return i;
189+ }
190+ }
191+ /* new one */
192+ luaX_checklimit(fs->ls, f->nups + 1, MAXUPVALUES, "upvalues");
193+ luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues,
194+ TString *, MAX_INT, "");
195+ fs->f->upvalues[f->nups] = name;
196+ fs->upvalues[f->nups] = *v;
197+ return f->nups++;
212198 }
213199
214200
215−static int indexupvalue (LexState *ls, expdesc *v) {
216− FuncState *fs = ls->fs;
201+static int searchvar (FuncState *fs, TString *n) {
217202 int i;
218− for (i=0; i<fs->nupvalues; i++) {
219− if (fs->upvalues[i].k == v->k && fs->upvalues[i].u.index == v->u.index)
203+ for (i=fs->nactvar-1; i >= 0; i--) {
204+ if (n == getlocvar(fs, i).varname)
220205 return i;
221206 }
222− /* new one */
223− luaX_checklimit(ls, fs->nupvalues+1, MAXUPVALUES, "upvalues");
224− fs->upvalues[fs->nupvalues] = *v;
225− return fs->nupvalues++;
207+ return -1; /* not found */
226208 }
227209
228210
229−static void pushupvalue (LexState *ls, TString *n) {
230− FuncState *fs = ls->fs;
231− expdesc v;
232− int level = search_local(ls, n, &v);
233− if (level == -1) { /* global? */
234− if (fs->prev == NULL)
235− luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
236− v.u.index = string_constant(fs->prev, n);
211+static void markupval (FuncState *fs, int level) {
212+ BlockCnt *bl = fs->bl;
213+ while (bl && bl->nactvar > level) bl = bl->previous;
214+ if (bl) bl->upval = 1;
215+}
216+
217+
218+static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
219+ if (fs == NULL) /* no more levels? */
220+ init_exp(var, VGLOBAL, NO_REG); /* default is global variable */
221+ else {
222+ int v = searchvar(fs, n); /* look up at current level */
223+ if (v >= 0) {
224+ init_exp(var, VLOCAL, v);
225+ if (!base)
226+ markupval(fs, v); /* local will be used as an upval */
227+ }
228+ else { /* not found at current level; try upper one */
229+ singlevaraux(fs->prev, n, var, 0);
230+ if (var->k == VGLOBAL) {
231+ if (base)
232+ var->info = luaK_stringK(fs, n); /* info points to global name */
233+ }
234+ else { /* LOCAL or UPVAL */
235+ var->info = indexupvalue(fs, n, var);
236+ var->k = VUPVAL; /* upvalue in this level */
237+ }
238+ }
237239 }
238− else if (level != 1)
239− luaX_syntaxerror(ls,
240− "upvalue must be global or local to immediately outer scope", n->str);
241− luaK_code1(fs, OP_PUSHUPVALUE, indexupvalue(ls, &v));
242240 }
243241
244242
245−static void adjust_mult_assign (LexState *ls, int nvars, int nexps) {
243+static TString *singlevar (LexState *ls, expdesc *var, int base) {
244+ TString *varname = str_checkname(ls);
245+ singlevaraux(ls->fs, varname, var, base);
246+ return varname;
247+}
248+
249+
250+static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
246251 FuncState *fs = ls->fs;
247− int diff = nexps - nvars;
248− if (nexps > 0 && luaK_lastisopen(fs)) { /* list ends in a function call */
249− diff--; /* do not count function call itself */
250− if (diff <= 0) { /* more variables than values? */
251− luaK_setcallreturns(fs, -diff); /* function call provide extra values */
252− diff = 0; /* no more difference */
252+ int extra = nvars - nexps;
253+ if (e->k == VCALL) {
254+ extra++; /* includes call itself */
255+ if (extra <= 0) extra = 0;
256+ else luaK_reserveregs(fs, extra-1);
257+ luaK_setcallreturns(fs, e, extra); /* call provides the difference */
258+ }
259+ else {
260+ if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
261+ if (extra > 0) {
262+ int reg = fs->freereg;
263+ luaK_reserveregs(fs, extra);
264+ luaK_nil(fs, reg, extra);
253265 }
254− else /* more values than variables */
255− luaK_setcallreturns(fs, 0); /* call should provide no value */
256266 }
257− /* push or pop eventual difference between list lengths */
258− luaK_adjuststack(fs, diff);
259267 }
260268
261269
262270 static void code_params (LexState *ls, int nparams, int dots) {
263271 FuncState *fs = ls->fs;
264272 adjustlocalvars(ls, nparams);
265− luaX_checklimit(ls, fs->nactloc, MAXPARAMS, "parameters");
266− fs->f->numparams = fs->nactloc; /* `self' could be there already */
267− fs->f->is_vararg = dots;
268− if (dots) {
269− new_localvarstr(ls, "arg", 0);
270− adjustlocalvars(ls, 1);
271− }
272− luaK_deltastack(fs, fs->nactloc); /* count parameters in the stack */
273+ luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters");
274+ fs->f->numparams = cast(lu_byte, fs->nactvar);
275+ fs->f->is_vararg = cast(lu_byte, dots);
276+ if (dots)
277+ create_local(ls, "arg");
278+ luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
273279 }
274280
275281
276−static void enterbreak (FuncState *fs, Breaklabel *bl) {
277− bl->stacklevel = fs->stacklevel;
282+static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable) {
278283 bl->breaklist = NO_JUMP;
284+ bl->isbreakable = isbreakable;
285+ bl->nactvar = fs->nactvar;
286+ bl->upval = 0;
279287 bl->previous = fs->bl;
280288 fs->bl = bl;
289+ lua_assert(fs->freereg == fs->nactvar);
281290 }
282291
283292
284−static void leavebreak (FuncState *fs, Breaklabel *bl) {
293+static void leaveblock (FuncState *fs) {
294+ BlockCnt *bl = fs->bl;
285295 fs->bl = bl->previous;
286− LUA_ASSERT(bl->stacklevel == fs->stacklevel, "wrong levels");
287− luaK_patchlist(fs, bl->breaklist, luaK_getlabel(fs));
296+ removevars(fs->ls, bl->nactvar);
297+ if (bl->upval)
298+ luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
299+ lua_assert(bl->nactvar == fs->nactvar);
300+ fs->freereg = fs->nactvar; /* free registers */
301+ luaK_patchtohere(fs, bl->breaklist);
288302 }
289303
290304
291−static void pushclosure (LexState *ls, FuncState *func) {
305+static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
292306 FuncState *fs = ls->fs;
293307 Proto *f = fs->f;
294308 int i;
295− for (i=0; i<func->nupvalues; i++)
296− luaK_tostack(ls, &func->upvalues[i], 1);
297− luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *,
298− "constant table overflow", MAXARG_A);
299− f->kproto[f->nkproto++] = func->f;
300− luaK_code2(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues);
309+ luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
310+ MAXARG_Bx, "constant table overflow");
311+ f->p[fs->np++] = func->f;
312+ init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
313+ for (i=0; i<func->f->nups; i++) {
314+ OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
315+ luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
316+ }
301317 }
302318
303319
304320 static void open_func (LexState *ls, FuncState *fs) {
305321 Proto *f = luaF_newproto(ls->L);
322+ fs->f = f;
306323 fs->prev = ls->fs; /* linked list of funcstates */
307324 fs->ls = ls;
308325 fs->L = ls->L;
309326 ls->fs = fs;
310− fs->stacklevel = 0;
311− fs->nactloc = 0;
312− fs->nupvalues = 0;
313− fs->bl = NULL;
314− fs->f = f;
315− f->source = ls->source;
316327 fs->pc = 0;
317328 fs->lasttarget = 0;
318− fs->lastline = 0;
319− fs->jlt = NO_JUMP;
320− f->code = NULL;
321− f->maxstacksize = 0;
322− f->numparams = 0; /* default for main chunk */
323− f->is_vararg = 0; /* default for main chunk */
329+ fs->jpc = NO_JUMP;
330+ fs->freereg = 0;
331+ fs->nk = 0;
332+ fs->h = luaH_new(ls->L, 0, 0);
333+ fs->np = 0;
334+ fs->nlocvars = 0;
335+ fs->nactvar = 0;
336+ fs->bl = NULL;
337+ f->source = ls->source;
338+ f->maxstacksize = 2; /* registers 0/1 are always valid */
324339 }
325340
326341
@@ -328,33 +343,40 @@ static void close_func (LexState *ls) { · 1 unmodified line
328343 lua_State *L = ls->L;
329344 FuncState *fs = ls->fs;
330345 Proto *f = fs->f;
331− luaK_code0(fs, OP_END);
332− luaK_getlabel(fs); /* close eventual list of pending jumps */
333− luaM_reallocvector(L, f->code, fs->pc, Instruction);
334− luaM_reallocvector(L, f->kstr, f->nkstr, TString *);
335− luaM_reallocvector(L, f->knum, f->nknum, Number);
336− luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
337− removelocalvars(ls, fs->nactloc);
338− luaM_reallocvector(L, f->locvars, f->nlocvars, LocVar);
339− luaM_reallocvector(L, f->lineinfo, f->nlineinfo+1, int);
340− f->lineinfo[f->nlineinfo++] = MAX_INT; /* end flag */
341− luaF_protook(L, f, fs->pc); /* proto is ok now */
346+ removevars(ls, 0);
347+ luaK_codeABC(fs, OP_RETURN, 0, 1, 0); /* final return */
348+ luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
349+ f->sizecode = fs->pc;
350+ luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
351+ f->sizelineinfo = fs->pc;
352+ luaM_reallocvector(L, f->k, f->sizek, fs->nk, TObject);
353+ f->sizek = fs->nk;
354+ luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
355+ f->sizep = fs->np;
356+ luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
357+ f->sizelocvars = fs->nlocvars;
358+ luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
359+ f->sizeupvalues = f->nups;
360+ lua_assert(luaG_checkcode(f));
361+ lua_assert(fs->bl == NULL);
342362 ls->fs = fs->prev;
343− LUA_ASSERT(fs->bl == NULL, "wrong list end");
344363 }
345364
346365
347−Proto *luaY_parser (lua_State *L, ZIO *z) {
366+Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff) {
348367 struct LexState lexstate;
349368 struct FuncState funcstate;
369+ lexstate.buff = buff;
370+ lexstate.nestlevel = 0;
350371 luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z)));
351372 open_func(&lexstate, &funcstate);
352373 next(&lexstate); /* read first token */
353374 chunk(&lexstate);
354375 check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
355376 close_func(&lexstate);
356− LUA_ASSERT(funcstate.prev == NULL, "wrong list end");
357− LUA_ASSERT(funcstate.nupvalues == 0, "no upvalues in main");
377+ lua_assert(funcstate.prev == NULL);
378+ lua_assert(funcstate.f->nups == 0);
379+ lua_assert(lexstate.nestlevel == 0);
358380 return funcstate.f;
359381 }
360382
@@ -365,235 +387,239 @@ Proto *luaY_parser (lua_State *L, ZIO *z) { · 4 unmodified lines
365387 /*============================================================*/
366388
367389
368−static int explist1 (LexState *ls) {
369− /* explist1 -> expr { ',' expr } */
370− int n = 1; /* at least one expression */
371− expdesc v;
372− expr(ls, &v);
373− while (ls->t.token == ',') {
374− luaK_tostack(ls, &v, 1); /* gets only 1 value from previous expression */
375− next(ls); /* skip comma */
376− expr(ls, &v);
377− n++;
378− }
379− luaK_tostack(ls, &v, 0); /* keep open number of values of last expression */
380− return n;
390+static void luaY_field (LexState *ls, expdesc *v) {
391+ /* field -> ['.' | ':'] NAME */
392+ FuncState *fs = ls->fs;
393+ expdesc key;
394+ luaK_exp2anyreg(fs, v);
395+ next(ls); /* skip the dot or colon */
396+ checkname(ls, &key);
397+ luaK_indexed(fs, v, &key);
381398 }
382399
383400
384−static void funcargs (LexState *ls, int slf) {
401+static void luaY_index (LexState *ls, expdesc *v) {
402+ /* index -> '[' expr ']' */
403+ next(ls); /* skip the '[' */
404+ expr(ls, v);
405+ luaK_exp2val(ls->fs, v);
406+ check(ls, ']');
407+}
408+
409+
410+/*
411+** {======================================================================
412+** Rules for Constructors
413+** =======================================================================
414+*/
415+
416+
417+struct ConsControl {
418+ expdesc v; /* last list item read */
419+ expdesc *t; /* table descriptor */
420+ int nh; /* total number of `record' elements */
421+ int na; /* total number of array elements */
422+ int tostore; /* number of array elements pending to be stored */
423+};
424+
425+
426+static void recfield (LexState *ls, struct ConsControl *cc) {
427+ /* recfield -> (NAME | `['exp1`]') = exp1 */
385428 FuncState *fs = ls->fs;
386− int slevel = fs->stacklevel - slf - 1; /* where is func in the stack */
387− switch (ls->t.token) {
388− case '(': { /* funcargs -> '(' [ explist1 ] ')' */
389− int line = ls->linenumber;
390− int nargs = 0;
391− next(ls);
392− if (ls->t.token != ')') /* arg list not empty? */
393− nargs = explist1(ls);
394− check_match(ls, ')', '(', line);
395−#ifdef LUA_COMPAT_ARGRET
396− if (nargs > 0) /* arg list is not empty? */
397− luaK_setcallreturns(fs, 1); /* last call returns only 1 value */
398−#else
399− UNUSED(nargs); /* to avoid warnings */
400−#endif
401− break;
402− }
403− case '{': { /* funcargs -> constructor */
404− constructor(ls);
405− break;
406− }
407− case TK_STRING: { /* funcargs -> STRING */
408− code_string(ls, ls->t.seminfo.ts); /* must use `seminfo' before `next' */
409− next(ls);
410− break;
411− }
412− default: {
413− luaK_error(ls, "function arguments expected");
414− break;
415− }
429+ int reg = ls->fs->freereg;
430+ expdesc key, val;
431+ if (ls->t.token == TK_NAME) {
432+ luaX_checklimit(ls, cc->nh, MAX_INT, "items in a constructor");
433+ cc->nh++;
434+ checkname(ls, &key);
435+ }
436+ else /* ls->t.token == '[' */
437+ luaY_index(ls, &key);
438+ check(ls, '=');
439+ luaK_exp2RK(fs, &key);
440+ expr(ls, &val);
441+ luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key),
442+ luaK_exp2RK(fs, &val));
443+ fs->freereg = reg; /* free registers */
444+}
445+
446+
447+static void closelistfield (FuncState *fs, struct ConsControl *cc) {
448+ if (cc->v.k == VVOID) return; /* there is no list item */
449+ luaK_exp2nextreg(fs, &cc->v);
450+ cc->v.k = VVOID;
451+ if (cc->tostore == LFIELDS_PER_FLUSH) {
452+ luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1); /* flush */
453+ cc->tostore = 0; /* no more items pending */
454+ fs->freereg = cc->t->info + 1; /* free registers */
416455 }
417− fs->stacklevel = slevel; /* call will remove function and arguments */
418− luaK_code2(fs, OP_CALL, slevel, MULT_RET);
419456 }
420457
421458
422−static void var_or_func_tail (LexState *ls, expdesc *v) {
423− for (;;) {
424− switch (ls->t.token) {
425− case '.': { /* var_or_func_tail -> '.' NAME */
426− next(ls);
427− luaK_tostack(ls, v, 1); /* `v' must be on stack */
428− luaK_kstr(ls, checkname(ls));
429− v->k = VINDEXED;
430− break;
431− }
432− case '[': { /* var_or_func_tail -> '[' exp1 ']' */
433− next(ls);
434− luaK_tostack(ls, v, 1); /* `v' must be on stack */
435− v->k = VINDEXED;
436− exp1(ls);
437− check(ls, ']');
459+static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
460+ if (cc->tostore == 0) return;
461+ if (cc->v.k == VCALL) {
462+ luaK_setcallreturns(fs, &cc->v, LUA_MULTRET);
463+ luaK_codeABx(fs, OP_SETLISTO, cc->t->info, cc->na-1);
464+ }
465+ else {
466+ if (cc->v.k != VVOID)
467+ luaK_exp2nextreg(fs, &cc->v);
468+ luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1);
469+ }
470+ fs->freereg = cc->t->info + 1; /* free registers */
471+}
472+
473+
474+static void listfield (LexState *ls, struct ConsControl *cc) {
475+ expr(ls, &cc->v);
476+ luaX_checklimit(ls, cc->na, MAXARG_Bx, "items in a constructor");
477+ cc->na++;
478+ cc->tostore++;
479+}
480+
481+
482+static void constructor (LexState *ls, expdesc *t) {
483+ /* constructor -> ?? */
484+ FuncState *fs = ls->fs;
485+ int line = ls->linenumber;
486+ int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
487+ struct ConsControl cc;
488+ cc.na = cc.nh = cc.tostore = 0;
489+ cc.t = t;
490+ init_exp(t, VRELOCABLE, pc);
491+ init_exp(&cc.v, VVOID, 0); /* no value (yet) */
492+ luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
493+ check(ls, '{');
494+ do {
495+ lua_assert(cc.v.k == VVOID || cc.tostore > 0);
496+ testnext(ls, ';'); /* compatibility only */
497+ if (ls->t.token == '}') break;
498+ closelistfield(fs, &cc);
499+ switch(ls->t.token) {
500+ case TK_NAME: { /* may be listfields or recfields */
501+ lookahead(ls);
502+ if (ls->lookahead.token != '=') /* expression? */
503+ listfield(ls, &cc);
504+ else
505+ recfield(ls, &cc);
438506 break;
439507 }
440− case ':': { /* var_or_func_tail -> ':' NAME funcargs */
441− int name;
442− next(ls);
443− name = checkname(ls);
444− luaK_tostack(ls, v, 1); /* `v' must be on stack */
445− luaK_code1(ls->fs, OP_PUSHSELF, name);
446− funcargs(ls, 1);
447− v->k = VEXP;
448− v->u.l.t = v->u.l.f = NO_JUMP;
508+ case '[': { /* constructor_item -> recfield */
509+ recfield(ls, &cc);
449510 break;
450511 }
451− case '(': case TK_STRING: case '{': { /* var_or_func_tail -> funcargs */
452− luaK_tostack(ls, v, 1); /* `v' must be on stack */
453− funcargs(ls, 0);
454− v->k = VEXP;
455− v->u.l.t = v->u.l.f = NO_JUMP;
512+ default: { /* constructor_part -> listfield */
513+ listfield(ls, &cc);
456514 break;
457515 }
458− default: return; /* should be follow... */
459516 }
460− }
461−}
462−
463−
464−static void var_or_func (LexState *ls, expdesc *v) {
465− /* var_or_func -> ['%'] NAME var_or_func_tail */
466− if (optional(ls, '%')) { /* upvalue? */
467− pushupvalue(ls, str_checkname(ls));
468− v->k = VEXP;
469− v->u.l.t = v->u.l.f = NO_JUMP;
470− }
471− else /* variable name */
472− singlevar(ls, str_checkname(ls), v);
473− var_or_func_tail(ls, v);
517+ } while (testnext(ls, ',') || testnext(ls, ';'));
518+ check_match(ls, '}', '{', line);
519+ lastlistfield(fs, &cc);
520+ SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
521+ SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */
474522 }
475523
524+/* }====================================================================== */
476525
477526
478−/*
479−** {======================================================================
480−** Rules for Constructors
481−** =======================================================================
482−*/
483−
484527
485−static void recfield (LexState *ls) {
486− /* recfield -> (NAME | '['exp1']') = exp1 */
487− switch (ls->t.token) {
488− case TK_NAME: {
489− luaK_kstr(ls, checkname(ls));
490− break;
491− }
492− case '[': {
493− next(ls);
494− exp1(ls);
495− check(ls, ']');
496− break;
497− }
498− default: luaK_error(ls, "<name> or `[' expected");
528+static void parlist (LexState *ls) {
529+ /* parlist -> [ param { `,' param } ] */
530+ int nparams = 0;
531+ int dots = 0;
532+ if (ls->t.token != ')') { /* is `parlist' not empty? */
533+ do {
534+ switch (ls->t.token) {
535+ case TK_DOTS: dots = 1; next(ls); break;
536+ case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
537+ default: luaX_syntaxerror(ls, "<name> or `...' expected");
538+ }
539+ } while (!dots && testnext(ls, ','));
499540 }
500− check(ls, '=');
501− exp1(ls);
541+ code_params(ls, nparams, dots);
502542 }
503543
504544
505−static int recfields (LexState *ls) {
506− /* recfields -> recfield { ',' recfield } [','] */
507− FuncState *fs = ls->fs;
508− int n = 1; /* at least one element */
509− recfield(ls);
510− while (ls->t.token == ',') {
511− next(ls);
512− if (ls->t.token == ';' || ls->t.token == '}')
513− break;
514− recfield(ls);
515− n++;
516− if (n%RFIELDS_PER_FLUSH == 0)
517− luaK_code1(fs, OP_SETMAP, RFIELDS_PER_FLUSH);
518− }
519− luaK_code1(fs, OP_SETMAP, n%RFIELDS_PER_FLUSH);
520− return n;
545+static void body (LexState *ls, expdesc *e, int needself, int line) {
546+ /* body -> `(' parlist `)' chunk END */
547+ FuncState new_fs;
548+ open_func(ls, &new_fs);
549+ new_fs.f->lineDefined = line;
550+ check(ls, '(');
551+ if (needself)
552+ create_local(ls, "self");
553+ parlist(ls);
554+ check(ls, ')');
555+ chunk(ls);
556+ check_match(ls, TK_END, TK_FUNCTION, line);
557+ close_func(ls);
558+ pushclosure(ls, &new_fs, e);
521559 }
522560
523561
524−static int listfields (LexState *ls) {
525− /* listfields -> exp1 { ',' exp1 } [','] */
526− FuncState *fs = ls->fs;
527− int n = 1; /* at least one element */
528− exp1(ls);
529− while (ls->t.token == ',') {
530− next(ls);
531− if (ls->t.token == ';' || ls->t.token == '}')
532− break;
533− exp1(ls);
562+static int explist1 (LexState *ls, expdesc *v) {
563+ /* explist1 -> expr { `,' expr } */
564+ int n = 1; /* at least one expression */
565+ expr(ls, v);
566+ while (testnext(ls, ',')) {
567+ luaK_exp2nextreg(ls->fs, v);
568+ expr(ls, v);
534569 n++;
535− luaX_checklimit(ls, n/LFIELDS_PER_FLUSH, MAXARG_A,
536− "`item groups' in a list initializer");
537− if (n%LFIELDS_PER_FLUSH == 0)
538− luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
539570 }
540− luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH, n%LFIELDS_PER_FLUSH);
541571 return n;
542572 }
543573
544574
545−
546−static void constructor_part (LexState *ls, Constdesc *cd) {
575+static void funcargs (LexState *ls, expdesc *f) {
576+ FuncState *fs = ls->fs;
577+ expdesc args;
578+ int base, nparams;
579+ int line = ls->linenumber;
547580 switch (ls->t.token) {
548− case ';': case '}': { /* constructor_part -> empty */
549− cd->n = 0;
550− cd->k = ls->t.token;
581+ case '(': { /* funcargs -> `(' [ explist1 ] `)' */
582+ if (line != ls->lastline)
583+ luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
584+ next(ls);
585+ if (ls->t.token == ')') /* arg list is empty? */
586+ args.k = VVOID;
587+ else {
588+ explist1(ls, &args);
589+ luaK_setcallreturns(fs, &args, LUA_MULTRET);
590+ }
591+ check_match(ls, ')', '(', line);
551592 break;
552593 }
553− case TK_NAME: { /* may be listfields or recfields */
554− lookahead(ls);
555− if (ls->lookahead.token != '=') /* expression? */
556− goto case_default;
557− /* else go through to recfields */
558− }
559− case '[': { /* constructor_part -> recfields */
560− cd->n = recfields(ls);
561− cd->k = 1; /* record */
594+ case '{': { /* funcargs -> constructor */
595+ constructor(ls, &args);
562596 break;
563597 }
564− default: { /* constructor_part -> listfields */
565− case_default:
566− cd->n = listfields(ls);
567− cd->k = 0; /* list */
598+ case TK_STRING: { /* funcargs -> STRING */
599+ codestring(ls, &args, ls->t.seminfo.ts);
600+ next(ls); /* must use `seminfo' before `next' */
568601 break;
569602 }
603+ default: {
604+ luaX_syntaxerror(ls, "function arguments expected");
605+ return;
606+ }
570607 }
571−}
572−
573−
574−static void constructor (LexState *ls) {
575− /* constructor -> '{' constructor_part [';' constructor_part] '}' */
576− FuncState *fs = ls->fs;
577− int line = ls->linenumber;
578− int pc = luaK_code1(fs, OP_CREATETABLE, 0);
579− int nelems;
580− Constdesc cd;
581− check(ls, '{');
582− constructor_part(ls, &cd);
583− nelems = cd.n;
584− if (optional(ls, ';')) {
585− Constdesc other_cd;
586− constructor_part(ls, &other_cd);
587− check_condition(ls, (cd.k != other_cd.k), "invalid constructor syntax");
588− nelems += other_cd.n;
608+ lua_assert(f->k == VNONRELOC);
609+ base = f->info; /* base register for call */
610+ if (args.k == VCALL)
611+ nparams = LUA_MULTRET; /* open call */
612+ else {
613+ if (args.k != VVOID)
614+ luaK_exp2nextreg(fs, &args); /* close last argument */
615+ nparams = fs->freereg - (base+1);
589616 }
590− check_match(ls, '}', '{', line);
591− luaX_checklimit(ls, nelems, MAXARG_U, "elements in a table constructor");
592− SETARG_U(fs->f->code[pc], nelems); /* set initial table size */
617+ init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
618+ luaK_fixline(fs, line);
619+ fs->freereg = base+1; /* call remove function and arguments and leaves
620+ (unless changed) one result */
593621 }
594622
595−/* }====================================================================== */
596−
597623
598624
599625
@@ -604,58 +630,121 @@ static void constructor (LexState *ls) { · 4 unmodified lines
604630 */
605631
606632
607−static void simpleexp (LexState *ls, expdesc *v) {
608− FuncState *fs = ls->fs;
633+static void prefixexp (LexState *ls, expdesc *v) {
634+ /* prefixexp -> NAME | '(' expr ')' */
609635 switch (ls->t.token) {
610− case TK_NUMBER: { /* simpleexp -> NUMBER */
611− Number r = ls->t.seminfo.r;
636+ case '(': {
637+ int line = ls->linenumber;
612638 next(ls);
613− luaK_number(fs, r);
639+ expr(ls, v);
640+ check_match(ls, ')', '(', line);
641+ luaK_dischargevars(ls->fs, v);
642+ return;
643+ }
644+ case TK_NAME: {
645+ singlevar(ls, v, 1);
646+ return;
647+ }
648+#ifdef LUA_COMPATUPSYNTAX
649+ case '%': { /* for compatibility only */
650+ TString *varname;
651+ int line = ls->linenumber;
652+ next(ls); /* skip `%' */
653+ varname = singlevar(ls, v, 1);
654+ if (v->k != VUPVAL)
655+ luaX_errorline(ls, "global upvalues are obsolete",
656+ getstr(varname), line);
657+ return;
658+ }
659+#endif
660+ default: {
661+ luaX_syntaxerror(ls, "unexpected symbol");
662+ return;
663+ }
664+ }
665+}
666+
667+
668+static void primaryexp (LexState *ls, expdesc *v) {
669+ /* primaryexp ->
670+ prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
671+ FuncState *fs = ls->fs;
672+ prefixexp(ls, v);
673+ for (;;) {
674+ switch (ls->t.token) {
675+ case '.': { /* field */
676+ luaY_field(ls, v);
677+ break;
678+ }
679+ case '[': { /* `[' exp1 `]' */
680+ expdesc key;
681+ luaK_exp2anyreg(fs, v);
682+ luaY_index(ls, &key);
683+ luaK_indexed(fs, v, &key);
684+ break;
685+ }
686+ case ':': { /* `:' NAME funcargs */
687+ expdesc key;
688+ next(ls);
689+ checkname(ls, &key);
690+ luaK_self(fs, v, &key);
691+ funcargs(ls, v);
692+ break;
693+ }
694+ case '(': case TK_STRING: case '{': { /* funcargs */
695+ luaK_exp2nextreg(fs, v);
696+ funcargs(ls, v);
697+ break;
698+ }
699+ default: return;
700+ }
701+ }
702+}
703+
704+
705+static void simpleexp (LexState *ls, expdesc *v) {
706+ /* simpleexp -> NUMBER | STRING | NIL | constructor | FUNCTION body
707+ | primaryexp */
708+ switch (ls->t.token) {
709+ case TK_NUMBER: {
710+ init_exp(v, VK, luaK_numberK(ls->fs, ls->t.seminfo.r));
711+ next(ls); /* must use `seminfo' before `next' */
614712 break;
615713 }
616− case TK_STRING: { /* simpleexp -> STRING */
617− code_string(ls, ls->t.seminfo.ts); /* must use `seminfo' before `next' */
618− next(ls);
714+ case TK_STRING: {
715+ codestring(ls, v, ls->t.seminfo.ts);
716+ next(ls); /* must use `seminfo' before `next' */
619717 break;
620718 }
621− case TK_NIL: { /* simpleexp -> NIL */
622− luaK_adjuststack(fs, -1);
719+ case TK_NIL: {
720+ init_exp(v, VNIL, 0);
623721 next(ls);
624722 break;
625723 }
626− case '{': { /* simpleexp -> constructor */
627− constructor(ls);
724+ case TK_TRUE: {
725+ init_exp(v, VTRUE, 0);
726+ next(ls);
628727 break;
629728 }
630− case TK_FUNCTION: { /* simpleexp -> FUNCTION body */
729+ case TK_FALSE: {
730+ init_exp(v, VFALSE, 0);
631731 next(ls);
632− body(ls, 0, ls->linenumber);
633732 break;
634733 }
635− case '(': { /* simpleexp -> '(' expr ')' */
636− next(ls);
637− expr(ls, v);
638− check(ls, ')');
639− return;
734+ case '{': { /* constructor */
735+ constructor(ls, v);
736+ break;
640737 }
641− case TK_NAME: case '%': {
642− var_or_func(ls, v);
643− return;
738+ case TK_FUNCTION: {
739+ next(ls);
740+ body(ls, v, 0, ls->linenumber);
741+ break;
644742 }
645743 default: {
646− luaK_error(ls, "<expression> expected");
647− return;
744+ primaryexp(ls, v);
745+ break;
648746 }
649747 }
650− v->k = VEXP;
651− v->u.l.t = v->u.l.f = NO_JUMP;
652−}
653−
654−
655−static void exp1 (LexState *ls) {
656− expdesc v;
657− expr(ls, &v);
658− luaK_tostack(ls, &v, 1);
659748 }
660749
661750
@@ -690,17 +779,17 @@ static BinOpr getbinopr (int op) { · 28 unmodified lines
690779
691780
692781 static const struct {
693− char left; /* left priority for each binary operator */
694− char right; /* right priority */
782+ lu_byte left; /* left priority for each binary operator */
783+ lu_byte right; /* right priority */
695784 } priority[] = { /* ORDER OPR */
696− {5, 5}, {5, 5}, {6, 6}, {6, 6}, /* arithmetic */
697− {9, 8}, {4, 3}, /* power and concat (right associative) */
698− {2, 2}, {2, 2}, /* equality */
699− {2, 2}, {2, 2}, {2, 2}, {2, 2}, /* order */
700− {1, 1}, {1, 1} /* logical */
785+ {6, 6}, {6, 6}, {7, 7}, {7, 7}, /* arithmetic */
786+ {10, 9}, {5, 4}, /* power and concat (right associative) */
787+ {3, 3}, {3, 3}, /* equality */
788+ {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
789+ {2, 2}, {1, 1} /* logical (and/or) */
701790 };
702791
703−#define UNARY_PRIORITY 7 /* priority for unary operators */
792+#define UNARY_PRIORITY 8 /* priority for unary operators */
704793
705794
706795 /*
@@ -709,25 +798,28 @@ static const struct { · 2 unmodified lines
709798 */
710799 static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
711800 BinOpr op;
712− UnOpr uop = getunopr(ls->t.token);
801+ UnOpr uop;
802+ enterlevel(ls);
803+ uop = getunopr(ls->t.token);
713804 if (uop != OPR_NOUNOPR) {
714805 next(ls);
715806 subexpr(ls, v, UNARY_PRIORITY);
716− luaK_prefix(ls, uop, v);
807+ luaK_prefix(ls->fs, uop, v);
717808 }
718809 else simpleexp(ls, v);
719810 /* expand while operators have priorities higher than `limit' */
720811 op = getbinopr(ls->t.token);
721− while (op != OPR_NOBINOPR && priority[op].left > limit) {
812+ while (op != OPR_NOBINOPR && cast(int, priority[op].left) > limit) {
722813 expdesc v2;
723814 BinOpr nextop;
724815 next(ls);
725− luaK_infix(ls, op, v);
816+ luaK_infix(ls->fs, op, v);
726817 /* read sub-expression with higher priority */
727− nextop = subexpr(ls, &v2, priority[op].right);
728− luaK_posfix(ls, op, v, &v2);
818+ nextop = subexpr(ls, &v2, cast(int, priority[op].right));
819+ luaK_posfix(ls->fs, op, v, &v2);
729820 op = nextop;
730821 }
822+ leavelevel(ls);
731823 return op; /* return first untreated operator */
732824 }
733825
@@ -739,6 +831,7 @@ static void expr (LexState *ls, expdesc *v) { · 5 unmodified lines
739831 /* }==================================================================== */
740832
741833
834+
742835 /*
743836 ** {======================================================================
744837 ** Rules for Statements
@@ -759,61 +852,151 @@ static int block_follow (int token) { · 14 unmodified lines
759852 static void block (LexState *ls) {
760853 /* block -> chunk */
761854 FuncState *fs = ls->fs;
762− int nactloc = fs->nactloc;
855+ BlockCnt bl;
856+ enterblock(fs, &bl, 0);
763857 chunk(ls);
764− luaK_adjuststack(fs, fs->nactloc - nactloc); /* remove local variables */
765− removelocalvars(ls, fs->nactloc - nactloc);
858+ lua_assert(bl.breaklist == NO_JUMP);
859+ leaveblock(fs);
766860 }
767861
768862
769−static int assignment (LexState *ls, expdesc *v, int nvars) {
770− int left = 0; /* number of values left in the stack after assignment */
771− luaX_checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment");
772− if (ls->t.token == ',') { /* assignment -> ',' NAME assignment */
773− expdesc nv;
774− next(ls);
775− var_or_func(ls, &nv);
776− check_condition(ls, (nv.k != VEXP), "syntax error");
777− left = assignment(ls, &nv, nvars+1);
863+/*
864+** structure to chain all variables in the left-hand side of an
865+** assignment
866+*/
867+struct LHS_assign {
868+ struct LHS_assign *prev;
869+ expdesc v; /* variable (global, local, upvalue, or indexed) */
870+};
871+
872+
873+/*
874+** check whether, in an assignment to a local variable, the local variable
875+** is needed in a previous assignment (to a table). If so, save original
876+** local value in a safe place and use this safe copy in the previous
877+** assignment.
878+*/
879+static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
880+ FuncState *fs = ls->fs;
881+ int extra = fs->freereg; /* eventual position to save local variable */
882+ int conflict = 0;
883+ for (; lh; lh = lh->prev) {
884+ if (lh->v.k == VINDEXED) {
885+ if (lh->v.info == v->info) { /* conflict? */
886+ conflict = 1;
887+ lh->v.info = extra; /* previous assignment will use safe copy */
888+ }
889+ if (lh->v.aux == v->info) { /* conflict? */
890+ conflict = 1;
891+ lh->v.aux = extra; /* previous assignment will use safe copy */
892+ }
893+ }
894+ }
895+ if (conflict) {
896+ luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
897+ luaK_reserveregs(fs, 1);
778898 }
779− else { /* assignment -> '=' explist1 */
899+}
900+
901+
902+static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
903+ expdesc e;
904+ check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
905+ "syntax error");
906+ if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
907+ struct LHS_assign nv;
908+ nv.prev = lh;
909+ primaryexp(ls, &nv.v);
910+ if (nv.v.k == VLOCAL)
911+ check_conflict(ls, lh, &nv.v);
912+ assignment(ls, &nv, nvars+1);
913+ }
914+ else { /* assignment -> `=' explist1 */
780915 int nexps;
781916 check(ls, '=');
782− nexps = explist1(ls);
783− adjust_mult_assign(ls, nvars, nexps);
784− }
785− if (v->k != VINDEXED)
786− luaK_storevar(ls, v);
787− else { /* there may be garbage between table-index and value */
788− luaK_code2(ls->fs, OP_SETTABLE, left+nvars+2, 1);
789− left += 2;
917+ nexps = explist1(ls, &e);
918+ if (nexps != nvars) {
919+ adjust_assign(ls, nvars, nexps, &e);
920+ if (nexps > nvars)
921+ ls->fs->freereg -= nexps - nvars; /* remove extra values */
922+ }
923+ else {
924+ luaK_setcallreturns(ls->fs, &e, 1); /* close last expression */
925+ luaK_storevar(ls->fs, &lh->v, &e);
926+ return; /* avoid default */
927+ }
790928 }
791− return left;
929+ init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
930+ luaK_storevar(ls->fs, &lh->v, &e);
792931 }
793932
794933
795934 static void cond (LexState *ls, expdesc *v) {
796935 /* cond -> exp */
797936 expr(ls, v); /* read condition */
798− luaK_goiftrue(ls->fs, v, 0);
937+ if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */
938+ luaK_goiftrue(ls->fs, v);
939+ luaK_patchtohere(ls->fs, v->t);
799940 }
800941
801942
943+/*
944+** The while statement optimizes its code by coding the condition
945+** after its body (and thus avoiding one jump in the loop).
946+*/
947+
948+/*
949+** maximum size of expressions for optimizing `while' code
950+*/
951+#ifndef MAXEXPWHILE
952+#define MAXEXPWHILE 100
953+#endif
954+
955+/*
956+** the call `luaK_goiffalse' may grow the size of an expression by
957+** at most this:
958+*/
959+#define EXTRAEXP 5
960+
802961 static void whilestat (LexState *ls, int line) {
803962 /* whilestat -> WHILE cond DO block END */
963+ Instruction codeexp[MAXEXPWHILE + EXTRAEXP];
964+ int lineexp;
965+ int i;
966+ int sizeexp;
804967 FuncState *fs = ls->fs;
805− int while_init = luaK_getlabel(fs);
968+ int whileinit, blockinit, expinit;
806969 expdesc v;
807− Breaklabel bl;
808− enterbreak(fs, &bl);
809− next(ls);
810− cond(ls, &v);
970+ BlockCnt bl;
971+ next(ls); /* skip WHILE */
972+ whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
973+ expinit = luaK_getlabel(fs);
974+ expr(ls, &v); /* parse condition */
975+ if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
976+ lineexp = ls->linenumber;
977+ luaK_goiffalse(fs, &v);
978+ luaK_concat(fs, &v.f, fs->jpc);
979+ fs->jpc = NO_JUMP;
980+ sizeexp = fs->pc - expinit; /* size of expression code */
981+ if (sizeexp > MAXEXPWHILE)
982+ luaX_syntaxerror(ls, "`while' condition too complex");
983+ for (i = 0; i < sizeexp; i++) /* save `exp' code */
984+ codeexp[i] = fs->f->code[expinit + i];
985+ fs->pc = expinit; /* remove `exp' code */
986+ enterblock(fs, &bl, 1);
811987 check(ls, TK_DO);
988+ blockinit = luaK_getlabel(fs);
812989 block(ls);
813− luaK_patchlist(fs, luaK_jump(fs), while_init);
814− luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
990+ luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
991+ /* move `exp' back to code */
992+ if (v.t != NO_JUMP) v.t += fs->pc - expinit;
993+ if (v.f != NO_JUMP) v.f += fs->pc - expinit;
994+ for (i=0; i<sizeexp; i++)
995+ luaK_code(fs, codeexp[i], lineexp);
815996 check_match(ls, TK_END, TK_WHILE, line);
816− leavebreak(fs, &bl);
997+ leaveblock(fs);
998+ luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
999+ luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
8171000 }
8181001
8191002
@@ -822,64 +1005,86 @@ static void repeatstat (LexState *ls, int line) { · 2 unmodified lines
8221005 FuncState *fs = ls->fs;
8231006 int repeat_init = luaK_getlabel(fs);
8241007 expdesc v;
825− Breaklabel bl;
826− enterbreak(fs, &bl);
1008+ BlockCnt bl;
1009+ enterblock(fs, &bl, 1);
8271010 next(ls);
8281011 block(ls);
8291012 check_match(ls, TK_UNTIL, TK_REPEAT, line);
8301013 cond(ls, &v);
831− luaK_patchlist(fs, v.u.l.f, repeat_init);
832− leavebreak(fs, &bl);
1014+ luaK_patchlist(fs, v.f, repeat_init);
1015+ leaveblock(fs);
8331016 }
8341017
8351018
836−static void forbody (LexState *ls, int nvar, OpCode prepfor, OpCode loopfor) {
837− /* forbody -> DO block END */
1019+static int exp1 (LexState *ls) {
1020+ expdesc e;
1021+ int k;
1022+ expr(ls, &e);
1023+ k = e.k;
1024+ luaK_exp2nextreg(ls->fs, &e);
1025+ return k;
1026+}
1027+
1028+
1029+static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
1030+ BlockCnt bl;
8381031 FuncState *fs = ls->fs;
839− int prep = luaK_code1(fs, prepfor, NO_JUMP);
840− int blockinit = luaK_getlabel(fs);
1032+ int prep, endfor;
1033+ adjustlocalvars(ls, nvars); /* scope for all variables */
8411034 check(ls, TK_DO);
842− adjustlocalvars(ls, nvar); /* scope for control variables */
1035+ enterblock(fs, &bl, 1); /* loop block */
1036+ prep = luaK_getlabel(fs);
8431037 block(ls);
844− luaK_patchlist(fs, luaK_code1(fs, loopfor, NO_JUMP), blockinit);
845− luaK_patchlist(fs, prep, luaK_getlabel(fs));
846− removelocalvars(ls, nvar);
1038+ luaK_patchtohere(fs, prep-1);
1039+ endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
1040+ luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
1041+ luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
1042+ luaK_patchlist(fs, (isnum) ? endfor : luaK_jump(fs), prep);
1043+ leaveblock(fs);
8471044 }
8481045
8491046
850−static void fornum (LexState *ls, TString *varname) {
851− /* fornum -> NAME = exp1,exp1[,exp1] forbody */
1047+static void fornum (LexState *ls, TString *varname, int line) {
1048+ /* fornum -> NAME = exp1,exp1[,exp1] DO body */
8521049 FuncState *fs = ls->fs;
1050+ int base = fs->freereg;
1051+ new_localvar(ls, varname, 0);
1052+ new_localvarstr(ls, "(for limit)", 1);
1053+ new_localvarstr(ls, "(for step)", 2);
8531054 check(ls, '=');
8541055 exp1(ls); /* initial value */
8551056 check(ls, ',');
8561057 exp1(ls); /* limit */
857− if (optional(ls, ','))
1058+ if (testnext(ls, ','))
8581059 exp1(ls); /* optional step */
859− else
860− luaK_code1(fs, OP_PUSHINT, 1); /* default step */
861− new_localvar(ls, varname, 0);
862− new_localvarstr(ls, "(limit)", 1);
863− new_localvarstr(ls, "(step)", 2);
864− forbody(ls, 3, OP_FORPREP, OP_FORLOOP);
1060+ else { /* default step = 1 */
1061+ luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
1062+ luaK_reserveregs(fs, 1);
1063+ }
1064+ luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
1065+ luaK_jump(fs);
1066+ forbody(ls, base, line, 3, 1);
8651067 }
8661068
8671069
8681070 static void forlist (LexState *ls, TString *indexname) {
869− /* forlist -> NAME,NAME IN exp1 forbody */
870− TString *valname;
871− check(ls, ',');
872− valname = str_checkname(ls);
873− /* next test is dirty, but avoids `in' being a reserved word */
874− check_condition(ls,
875− (ls->t.token == TK_NAME && ls->t.seminfo.ts == luaS_new(ls->L, "in")),
876− "`in' expected");
877− next(ls); /* skip `in' */
878− exp1(ls); /* table */
879− new_localvarstr(ls, "(table)", 0);
880− new_localvar(ls, indexname, 1);
881− new_localvar(ls, valname, 2);
882− forbody(ls, 3, OP_LFORPREP, OP_LFORLOOP);
1071+ /* forlist -> NAME {,NAME} IN explist1 DO body */
1072+ FuncState *fs = ls->fs;
1073+ expdesc e;
1074+ int nvars = 0;
1075+ int line;
1076+ int base = fs->freereg;
1077+ new_localvarstr(ls, "(for generator)", nvars++);
1078+ new_localvarstr(ls, "(for state)", nvars++);
1079+ new_localvar(ls, indexname, nvars++);
1080+ while (testnext(ls, ','))
1081+ new_localvar(ls, str_checkname(ls), nvars++);
1082+ check(ls, TK_IN);
1083+ line = ls->linenumber;
1084+ adjust_assign(ls, nvars, explist1(ls, &e), &e);
1085+ luaK_checkstack(fs, 3); /* extra space to call generator */
1086+ luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
1087+ forbody(ls, base, line, nvars, 0);
8831088 }
8841089
8851090
@@ -887,17 +1092,17 @@ static void forstat (LexState *ls, int line) { · 1 unmodified line
8871092 /* forstat -> fornum | forlist */
8881093 FuncState *fs = ls->fs;
8891094 TString *varname;
890− Breaklabel bl;
891− enterbreak(fs, &bl);
1095+ BlockCnt bl;
1096+ enterblock(fs, &bl, 0); /* block to control variable scope */
8921097 next(ls); /* skip `for' */
8931098 varname = str_checkname(ls); /* first variable name */
8941099 switch (ls->t.token) {
895− case '=': fornum(ls, varname); break;
896− case ',': forlist(ls, varname); break;
897− default: luaK_error(ls, "`=' or `,' expected");
1100+ case '=': fornum(ls, varname, line); break;
1101+ case ',': case TK_IN: forlist(ls, varname); break;
1102+ default: luaX_syntaxerror(ls, "`=' or `in' expected");
8981103 }
8991104 check_match(ls, TK_END, TK_FOR, line);
900− leavebreak(fs, &bl);
1105+ leaveblock(fs);
9011106 }
9021107
9031108
@@ -918,49 +1123,60 @@ static void ifstat (LexState *ls, int line) { · 14 unmodified lines
9181123 test_then_block(ls, &v); /* IF cond THEN block */
9191124 while (ls->t.token == TK_ELSEIF) {
9201125 luaK_concat(fs, &escapelist, luaK_jump(fs));
921− luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
1126+ luaK_patchtohere(fs, v.f);
9221127 test_then_block(ls, &v); /* ELSEIF cond THEN block */
9231128 }
9241129 if (ls->t.token == TK_ELSE) {
9251130 luaK_concat(fs, &escapelist, luaK_jump(fs));
926− luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
927− next(ls); /* skip ELSE */
1131+ luaK_patchtohere(fs, v.f);
1132+ next(ls); /* skip ELSE (after patch, for correct line info) */
9281133 block(ls); /* `else' part */
9291134 }
9301135 else
931− luaK_concat(fs, &escapelist, v.u.l.f);
932− luaK_patchlist(fs, escapelist, luaK_getlabel(fs));
1136+ luaK_concat(fs, &escapelist, v.f);
1137+ luaK_patchtohere(fs, escapelist);
9331138 check_match(ls, TK_END, TK_IF, line);
9341139 }
9351140
9361141
1142+static void localfunc (LexState *ls) {
1143+ expdesc v, b;
1144+ new_localvar(ls, str_checkname(ls), 0);
1145+ init_exp(&v, VLOCAL, ls->fs->freereg++);
1146+ adjustlocalvars(ls, 1);
1147+ body(ls, &b, 0, ls->linenumber);
1148+ luaK_storevar(ls->fs, &v, &b);
1149+}
1150+
1151+
9371152 static void localstat (LexState *ls) {
938− /* stat -> LOCAL NAME {',' NAME} ['=' explist1] */
1153+ /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
9391154 int nvars = 0;
9401155 int nexps;
1156+ expdesc e;
9411157 do {
942− next(ls); /* skip LOCAL or ',' */
9431158 new_localvar(ls, str_checkname(ls), nvars++);
944− } while (ls->t.token == ',');
945− if (optional(ls, '='))
946− nexps = explist1(ls);
947− else
1159+ } while (testnext(ls, ','));
1160+ if (testnext(ls, '='))
1161+ nexps = explist1(ls, &e);
1162+ else {
1163+ e.k = VVOID;
9481164 nexps = 0;
949− adjust_mult_assign(ls, nvars, nexps);
1165+ }
1166+ adjust_assign(ls, nvars, nexps, &e);
9501167 adjustlocalvars(ls, nvars);
9511168 }
9521169
9531170
9541171 static int funcname (LexState *ls, expdesc *v) {
955− /* funcname -> NAME [':' NAME | '.' NAME] */
1172+ /* funcname -> NAME {field} [`:' NAME] */
9561173 int needself = 0;
957− singlevar(ls, str_checkname(ls), v);
958− if (ls->t.token == ':' || ls->t.token == '.') {
959− needself = (ls->t.token == ':');
960− next(ls);
961− luaK_tostack(ls, v, 1);
962− luaK_kstr(ls, checkname(ls));
963− v->k = VINDEXED;
1174+ singlevar(ls, v, 1);
1175+ while (ls->t.token == '.')
1176+ luaY_field(ls, v);
1177+ if (ls->t.token == ':') {
1178+ needself = 1;
1179+ luaY_field(ls, v);
9641180 }
9651181 return needself;
9661182 }
@@ -969,26 +1185,26 @@ static int funcname (LexState *ls, expdesc *v) { · 2 unmodified lines
9691185 static void funcstat (LexState *ls, int line) {
9701186 /* funcstat -> FUNCTION funcname body */
9711187 int needself;
972− expdesc v;
1188+ expdesc v, b;
9731189 next(ls); /* skip FUNCTION */
9741190 needself = funcname(ls, &v);
975− body(ls, needself, line);
976− luaK_storevar(ls, &v);
1191+ body(ls, &b, needself, line);
1192+ luaK_storevar(ls->fs, &v, &b);
1193+ luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
9771194 }
9781195
9791196
980−static void namestat (LexState *ls) {
981− /* stat -> func | ['%'] NAME assignment */
1197+static void exprstat (LexState *ls) {
1198+ /* stat -> func | assignment */
9821199 FuncState *fs = ls->fs;
983− expdesc v;
984− var_or_func(ls, &v);
985− if (v.k == VEXP) { /* stat -> func */
986− check_condition(ls, luaK_lastisopen(fs), "syntax error"); /* an upvalue? */
987− luaK_setcallreturns(fs, 0); /* call statement uses no results */
1200+ struct LHS_assign v;
1201+ primaryexp(ls, &v.v);
1202+ if (v.v.k == VCALL) { /* stat -> func */
1203+ luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
9881204 }
989− else { /* stat -> ['%'] NAME assignment */
990− int left = assignment(ls, &v, 1);
991− luaK_adjuststack(fs, left); /* remove eventual garbage left on stack */
1205+ else { /* stat -> assignment */
1206+ v.prev = NULL;
1207+ assignment(ls, &v, 1);
9921208 }
9931209 }
9941210
@@ -996,30 +1212,55 @@ static void namestat (LexState *ls) { · 1 unmodified line
9961212 static void retstat (LexState *ls) {
9971213 /* stat -> RETURN explist */
9981214 FuncState *fs = ls->fs;
1215+ expdesc e;
1216+ int first, nret; /* registers with returned values */
9991217 next(ls); /* skip RETURN */
1000− if (!block_follow(ls->t.token) && ls->t.token != ';')
1001− explist1(ls); /* optional return values */
1002− luaK_code1(fs, OP_RETURN, ls->fs->nactloc);
1003− fs->stacklevel = fs->nactloc; /* removes all temp values */
1218+ if (block_follow(ls->t.token) || ls->t.token == ';')
1219+ first = nret = 0; /* return no values */
1220+ else {
1221+ nret = explist1(ls, &e); /* optional return values */
1222+ if (e.k == VCALL) {
1223+ luaK_setcallreturns(fs, &e, LUA_MULTRET);
1224+ if (nret == 1) { /* tail call? */
1225+ SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
1226+ lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
1227+ }
1228+ first = fs->nactvar;
1229+ nret = LUA_MULTRET; /* return all values */
1230+ }
1231+ else {
1232+ if (nret == 1) /* only one single value? */
1233+ first = luaK_exp2anyreg(fs, &e);
1234+ else {
1235+ luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
1236+ first = fs->nactvar; /* return all `active' values */
1237+ lua_assert(nret == fs->freereg - first);
1238+ }
1239+ }
1240+ }
1241+ luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
10041242 }
10051243
10061244
10071245 static void breakstat (LexState *ls) {
10081246 /* stat -> BREAK [NAME] */
10091247 FuncState *fs = ls->fs;
1010− int currentlevel = fs->stacklevel;
1011− Breaklabel *bl = fs->bl;
1012− if (!bl)
1013− luaK_error(ls, "no loop to break");
1248+ BlockCnt *bl = fs->bl;
1249+ int upval = 0;
10141250 next(ls); /* skip BREAK */
1015− luaK_adjuststack(fs, currentlevel - bl->stacklevel);
1251+ while (bl && !bl->isbreakable) {
1252+ upval |= bl->upval;
1253+ bl = bl->previous;
1254+ }
1255+ if (!bl)
1256+ luaX_syntaxerror(ls, "no loop to break");
1257+ if (upval)
1258+ luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
10161259 luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
1017− /* correct stack for compiler and symbolic execution */
1018− luaK_adjuststack(fs, bl->stacklevel - currentlevel);
10191260 }
10201261
10211262
1022−static int stat (LexState *ls) {
1263+static int statement (LexState *ls) {
10231264 int line = ls->linenumber; /* may be needed for error messages */
10241265 switch (ls->t.token) {
10251266 case TK_IF: { /* stat -> ifstat */
@@ -1044,16 +1285,16 @@ static int stat (LexState *ls) { · 18 unmodified lines
10441285 repeatstat(ls, line);
10451286 return 0;
10461287 }
1047− case TK_FUNCTION: { /* stat -> funcstat */
1048− funcstat(ls, line);
1288+ case TK_FUNCTION: {
1289+ funcstat(ls, line); /* stat -> funcstat */
10491290 return 0;
10501291 }
10511292 case TK_LOCAL: { /* stat -> localstat */
1052− localstat(ls);
1053− return 0;
1054− }
1055− case TK_NAME: case '%': { /* stat -> namestat */
1056− namestat(ls);
1293+ next(ls); /* skip LOCAL */
1294+ if (testnext(ls, TK_FUNCTION)) /* local function? */
1295+ localfunc(ls);
1296+ else
1297+ localstat(ls);
10571298 return 0;
10581299 }
10591300 case TK_RETURN: { /* stat -> retstat */
@@ -1065,60 +1306,24 @@ static int stat (LexState *ls) { · 5 unmodified lines
10651306 return 1; /* must be last statement */
10661307 }
10671308 default: {
1068− luaK_error(ls, "<statement> expected");
1309+ exprstat(ls);
10691310 return 0; /* to avoid warnings */
10701311 }
10711312 }
10721313 }
10731314
10741315
1075−static void parlist (LexState *ls) {
1076− /* parlist -> [ param { ',' param } ] */
1077− int nparams = 0;
1078− int dots = 0;
1079− if (ls->t.token != ')') { /* is `parlist' not empty? */
1080− do {
1081− switch (ls->t.token) {
1082− case TK_DOTS: next(ls); dots = 1; break;
1083− case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
1084− default: luaK_error(ls, "<name> or `...' expected");
1085− }
1086− } while (!dots && optional(ls, ','));
1087− }
1088− code_params(ls, nparams, dots);
1089−}
1090−
1091−
1092−static void body (LexState *ls, int needself, int line) {
1093− /* body -> '(' parlist ')' chunk END */
1094− FuncState new_fs;
1095− open_func(ls, &new_fs);
1096− new_fs.f->lineDefined = line;
1097− check(ls, '(');
1098− if (needself) {
1099− new_localvarstr(ls, "self", 0);
1100− adjustlocalvars(ls, 1);
1101− }
1102− parlist(ls);
1103− check(ls, ')');
1104− chunk(ls);
1105− check_match(ls, TK_END, TK_FUNCTION, line);
1106− close_func(ls);
1107− pushclosure(ls, &new_fs);
1108−}
1109−
1110−
1111−/* }====================================================================== */
1112−
1113−
11141316 static void chunk (LexState *ls) {
1115− /* chunk -> { stat [';'] } */
1317+ /* chunk -> { stat [`;'] } */
11161318 int islast = 0;
1319+ enterlevel(ls);
11171320 while (!islast && !block_follow(ls->t.token)) {
1118− islast = stat(ls);
1119− optional(ls, ';');
1120− LUA_ASSERT(ls->fs->stacklevel == ls->fs->nactloc,
1121− "stack size != # local vars");
1321+ islast = statement(ls);
1322+ testnext(ls, ';');
1323+ lua_assert(ls->fs->freereg >= ls->fs->nactvar);
1324+ ls->fs->freereg = ls->fs->nactvar; /* free registers */
11221325 }
1326+ leavelevel(ls);
11231327 }
11241328
1329+/* }====================================================================== */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t