annotate src/lua/lparser.c @ 26:18eaae41bde3

make distcheck now works
author Robert McIntyre <rlm@mit.edu>
date Sun, 04 Mar 2012 18:30:06 -0600
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $
rlm@1 3 ** Lua Parser
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <string.h>
rlm@1 9
rlm@1 10 #define lparser_c
rlm@1 11 #define LUA_CORE
rlm@1 12
rlm@1 13 #include "lua.h"
rlm@1 14
rlm@1 15 #include "lcode.h"
rlm@1 16 #include "ldebug.h"
rlm@1 17 #include "ldo.h"
rlm@1 18 #include "lfunc.h"
rlm@1 19 #include "llex.h"
rlm@1 20 #include "lmem.h"
rlm@1 21 #include "lobject.h"
rlm@1 22 #include "lopcodes.h"
rlm@1 23 #include "lparser.h"
rlm@1 24 #include "lstate.h"
rlm@1 25 #include "lstring.h"
rlm@1 26 #include "ltable.h"
rlm@1 27
rlm@1 28
rlm@1 29
rlm@1 30 #define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
rlm@1 31
rlm@1 32 #define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]])
rlm@1 33
rlm@1 34 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
rlm@1 35
rlm@1 36
rlm@1 37 /*
rlm@1 38 ** nodes for block list (list of active blocks)
rlm@1 39 */
rlm@1 40 typedef struct BlockCnt {
rlm@1 41 struct BlockCnt *previous; /* chain */
rlm@1 42 int breaklist; /* list of jumps out of this loop */
rlm@1 43 lu_byte nactvar; /* # active locals outside the breakable structure */
rlm@1 44 lu_byte upval; /* true if some variable in the block is an upvalue */
rlm@1 45 lu_byte isbreakable; /* true if `block' is a loop */
rlm@1 46 } BlockCnt;
rlm@1 47
rlm@1 48
rlm@1 49
rlm@1 50 /*
rlm@1 51 ** prototypes for recursive non-terminal functions
rlm@1 52 */
rlm@1 53 static void chunk (LexState *ls);
rlm@1 54 static void expr (LexState *ls, expdesc *v);
rlm@1 55
rlm@1 56
rlm@1 57 static void anchor_token (LexState *ls) {
rlm@1 58 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
rlm@1 59 TString *ts = ls->t.seminfo.ts;
rlm@1 60 luaX_newstring(ls, getstr(ts), ts->tsv.len);
rlm@1 61 }
rlm@1 62 }
rlm@1 63
rlm@1 64
rlm@1 65 static void error_expected (LexState *ls, int token) {
rlm@1 66 luaX_syntaxerror(ls,
rlm@1 67 luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token)));
rlm@1 68 }
rlm@1 69
rlm@1 70
rlm@1 71 static void errorlimit (FuncState *fs, int limit, const char *what) {
rlm@1 72 const char *msg = (fs->f->linedefined == 0) ?
rlm@1 73 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
rlm@1 74 luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
rlm@1 75 fs->f->linedefined, limit, what);
rlm@1 76 luaX_lexerror(fs->ls, msg, 0);
rlm@1 77 }
rlm@1 78
rlm@1 79
rlm@1 80 static int testnext (LexState *ls, int c) {
rlm@1 81 if (ls->t.token == c) {
rlm@1 82 luaX_next(ls);
rlm@1 83 return 1;
rlm@1 84 }
rlm@1 85 else return 0;
rlm@1 86 }
rlm@1 87
rlm@1 88
rlm@1 89 static void check (LexState *ls, int c) {
rlm@1 90 if (ls->t.token != c)
rlm@1 91 error_expected(ls, c);
rlm@1 92 }
rlm@1 93
rlm@1 94 static void checknext (LexState *ls, int c) {
rlm@1 95 check(ls, c);
rlm@1 96 luaX_next(ls);
rlm@1 97 }
rlm@1 98
rlm@1 99
rlm@1 100 #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
rlm@1 101
rlm@1 102
rlm@1 103
rlm@1 104 static void check_match (LexState *ls, int what, int who, int where) {
rlm@1 105 if (!testnext(ls, what)) {
rlm@1 106 if (where == ls->linenumber)
rlm@1 107 error_expected(ls, what);
rlm@1 108 else {
rlm@1 109 luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
rlm@1 110 LUA_QS " expected (to close " LUA_QS " at line %d)",
rlm@1 111 luaX_token2str(ls, what), luaX_token2str(ls, who), where));
rlm@1 112 }
rlm@1 113 }
rlm@1 114 }
rlm@1 115
rlm@1 116
rlm@1 117 static TString *str_checkname (LexState *ls) {
rlm@1 118 TString *ts;
rlm@1 119 check(ls, TK_NAME);
rlm@1 120 ts = ls->t.seminfo.ts;
rlm@1 121 luaX_next(ls);
rlm@1 122 return ts;
rlm@1 123 }
rlm@1 124
rlm@1 125
rlm@1 126 static void init_exp (expdesc *e, expkind k, int i) {
rlm@1 127 e->f = e->t = NO_JUMP;
rlm@1 128 e->k = k;
rlm@1 129 e->u.s.info = i;
rlm@1 130 }
rlm@1 131
rlm@1 132
rlm@1 133 static void codestring (LexState *ls, expdesc *e, TString *s) {
rlm@1 134 init_exp(e, VK, luaK_stringK(ls->fs, s));
rlm@1 135 }
rlm@1 136
rlm@1 137
rlm@1 138 static void checkname(LexState *ls, expdesc *e) {
rlm@1 139 codestring(ls, e, str_checkname(ls));
rlm@1 140 }
rlm@1 141
rlm@1 142
rlm@1 143 static int registerlocalvar (LexState *ls, TString *varname) {
rlm@1 144 FuncState *fs = ls->fs;
rlm@1 145 Proto *f = fs->f;
rlm@1 146 int oldsize = f->sizelocvars;
rlm@1 147 luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
rlm@1 148 LocVar, SHRT_MAX, "too many local variables");
rlm@1 149 while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
rlm@1 150 f->locvars[fs->nlocvars].varname = varname;
rlm@1 151 luaC_objbarrier(ls->L, f, varname);
rlm@1 152 return fs->nlocvars++;
rlm@1 153 }
rlm@1 154
rlm@1 155
rlm@1 156 #define new_localvarliteral(ls,v,n) \
rlm@1 157 new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
rlm@1 158
rlm@1 159
rlm@1 160 static void new_localvar (LexState *ls, TString *name, int n) {
rlm@1 161 FuncState *fs = ls->fs;
rlm@1 162 luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
rlm@1 163 fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));
rlm@1 164 }
rlm@1 165
rlm@1 166
rlm@1 167 static void adjustlocalvars (LexState *ls, int nvars) {
rlm@1 168 FuncState *fs = ls->fs;
rlm@1 169 fs->nactvar = cast_byte(fs->nactvar + nvars);
rlm@1 170 for (; nvars; nvars--) {
rlm@1 171 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
rlm@1 172 }
rlm@1 173 }
rlm@1 174
rlm@1 175
rlm@1 176 static void removevars (LexState *ls, int tolevel) {
rlm@1 177 FuncState *fs = ls->fs;
rlm@1 178 while (fs->nactvar > tolevel)
rlm@1 179 getlocvar(fs, --fs->nactvar).endpc = fs->pc;
rlm@1 180 }
rlm@1 181
rlm@1 182
rlm@1 183 static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
rlm@1 184 int i;
rlm@1 185 Proto *f = fs->f;
rlm@1 186 int oldsize = f->sizeupvalues;
rlm@1 187 for (i=0; i<f->nups; i++) {
rlm@1 188 if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {
rlm@1 189 lua_assert(f->upvalues[i] == name);
rlm@1 190 return i;
rlm@1 191 }
rlm@1 192 }
rlm@1 193 /* new one */
rlm@1 194 luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues");
rlm@1 195 luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,
rlm@1 196 TString *, MAX_INT, "");
rlm@1 197 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;
rlm@1 198 f->upvalues[f->nups] = name;
rlm@1 199 luaC_objbarrier(fs->L, f, name);
rlm@1 200 lua_assert(v->k == VLOCAL || v->k == VUPVAL);
rlm@1 201 fs->upvalues[f->nups].k = cast_byte(v->k);
rlm@1 202 fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
rlm@1 203 return f->nups++;
rlm@1 204 }
rlm@1 205
rlm@1 206
rlm@1 207 static int searchvar (FuncState *fs, TString *n) {
rlm@1 208 int i;
rlm@1 209 for (i=fs->nactvar-1; i >= 0; i--) {
rlm@1 210 if (n == getlocvar(fs, i).varname)
rlm@1 211 return i;
rlm@1 212 }
rlm@1 213 return -1; /* not found */
rlm@1 214 }
rlm@1 215
rlm@1 216
rlm@1 217 static void markupval (FuncState *fs, int level) {
rlm@1 218 BlockCnt *bl = fs->bl;
rlm@1 219 while (bl && bl->nactvar > level) bl = bl->previous;
rlm@1 220 if (bl) bl->upval = 1;
rlm@1 221 }
rlm@1 222
rlm@1 223
rlm@1 224 static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
rlm@1 225 if (fs == NULL) { /* no more levels? */
rlm@1 226 init_exp(var, VGLOBAL, NO_REG); /* default is global variable */
rlm@1 227 return VGLOBAL;
rlm@1 228 }
rlm@1 229 else {
rlm@1 230 int v = searchvar(fs, n); /* look up at current level */
rlm@1 231 if (v >= 0) {
rlm@1 232 init_exp(var, VLOCAL, v);
rlm@1 233 if (!base)
rlm@1 234 markupval(fs, v); /* local will be used as an upval */
rlm@1 235 return VLOCAL;
rlm@1 236 }
rlm@1 237 else { /* not found at current level; try upper one */
rlm@1 238 if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
rlm@1 239 return VGLOBAL;
rlm@1 240 var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */
rlm@1 241 var->k = VUPVAL; /* upvalue in this level */
rlm@1 242 return VUPVAL;
rlm@1 243 }
rlm@1 244 }
rlm@1 245 }
rlm@1 246
rlm@1 247
rlm@1 248 static void singlevar (LexState *ls, expdesc *var) {
rlm@1 249 TString *varname = str_checkname(ls);
rlm@1 250 FuncState *fs = ls->fs;
rlm@1 251 if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
rlm@1 252 var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */
rlm@1 253 }
rlm@1 254
rlm@1 255
rlm@1 256 static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
rlm@1 257 FuncState *fs = ls->fs;
rlm@1 258 int extra = nvars - nexps;
rlm@1 259 if (hasmultret(e->k)) {
rlm@1 260 extra++; /* includes call itself */
rlm@1 261 if (extra < 0) extra = 0;
rlm@1 262 luaK_setreturns(fs, e, extra); /* last exp. provides the difference */
rlm@1 263 if (extra > 1) luaK_reserveregs(fs, extra-1);
rlm@1 264 }
rlm@1 265 else {
rlm@1 266 if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
rlm@1 267 if (extra > 0) {
rlm@1 268 int reg = fs->freereg;
rlm@1 269 luaK_reserveregs(fs, extra);
rlm@1 270 luaK_nil(fs, reg, extra);
rlm@1 271 }
rlm@1 272 }
rlm@1 273 }
rlm@1 274
rlm@1 275
rlm@1 276 static void enterlevel (LexState *ls) {
rlm@1 277 if (++ls->L->nCcalls > LUAI_MAXCCALLS)
rlm@1 278 luaX_lexerror(ls, "chunk has too many syntax levels", 0);
rlm@1 279 }
rlm@1 280
rlm@1 281
rlm@1 282 #define leavelevel(ls) ((ls)->L->nCcalls--)
rlm@1 283
rlm@1 284
rlm@1 285 static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
rlm@1 286 bl->breaklist = NO_JUMP;
rlm@1 287 bl->isbreakable = isbreakable;
rlm@1 288 bl->nactvar = fs->nactvar;
rlm@1 289 bl->upval = 0;
rlm@1 290 bl->previous = fs->bl;
rlm@1 291 fs->bl = bl;
rlm@1 292 lua_assert(fs->freereg == fs->nactvar);
rlm@1 293 }
rlm@1 294
rlm@1 295
rlm@1 296 static void leaveblock (FuncState *fs) {
rlm@1 297 BlockCnt *bl = fs->bl;
rlm@1 298 fs->bl = bl->previous;
rlm@1 299 removevars(fs->ls, bl->nactvar);
rlm@1 300 if (bl->upval)
rlm@1 301 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
rlm@1 302 /* a block either controls scope or breaks (never both) */
rlm@1 303 lua_assert(!bl->isbreakable || !bl->upval);
rlm@1 304 lua_assert(bl->nactvar == fs->nactvar);
rlm@1 305 fs->freereg = fs->nactvar; /* free registers */
rlm@1 306 luaK_patchtohere(fs, bl->breaklist);
rlm@1 307 }
rlm@1 308
rlm@1 309
rlm@1 310 static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
rlm@1 311 FuncState *fs = ls->fs;
rlm@1 312 Proto *f = fs->f;
rlm@1 313 int oldsize = f->sizep;
rlm@1 314 int i;
rlm@1 315 luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
rlm@1 316 MAXARG_Bx, "constant table overflow");
rlm@1 317 while (oldsize < f->sizep) f->p[oldsize++] = NULL;
rlm@1 318 f->p[fs->np++] = func->f;
rlm@1 319 luaC_objbarrier(ls->L, f, func->f);
rlm@1 320 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
rlm@1 321 for (i=0; i<func->f->nups; i++) {
rlm@1 322 OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
rlm@1 323 luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
rlm@1 324 }
rlm@1 325 }
rlm@1 326
rlm@1 327
rlm@1 328 static void open_func (LexState *ls, FuncState *fs) {
rlm@1 329 lua_State *L = ls->L;
rlm@1 330 Proto *f = luaF_newproto(L);
rlm@1 331 fs->f = f;
rlm@1 332 fs->prev = ls->fs; /* linked list of funcstates */
rlm@1 333 fs->ls = ls;
rlm@1 334 fs->L = L;
rlm@1 335 ls->fs = fs;
rlm@1 336 fs->pc = 0;
rlm@1 337 fs->lasttarget = -1;
rlm@1 338 fs->jpc = NO_JUMP;
rlm@1 339 fs->freereg = 0;
rlm@1 340 fs->nk = 0;
rlm@1 341 fs->np = 0;
rlm@1 342 fs->nlocvars = 0;
rlm@1 343 fs->nactvar = 0;
rlm@1 344 fs->bl = NULL;
rlm@1 345 f->source = ls->source;
rlm@1 346 f->maxstacksize = 2; /* registers 0/1 are always valid */
rlm@1 347 fs->h = luaH_new(L, 0, 0);
rlm@1 348 /* anchor table of constants and prototype (to avoid being collected) */
rlm@1 349 sethvalue2s(L, L->top, fs->h);
rlm@1 350 incr_top(L);
rlm@1 351 setptvalue2s(L, L->top, f);
rlm@1 352 incr_top(L);
rlm@1 353 }
rlm@1 354
rlm@1 355
rlm@1 356 static void close_func (LexState *ls) {
rlm@1 357 lua_State *L = ls->L;
rlm@1 358 FuncState *fs = ls->fs;
rlm@1 359 Proto *f = fs->f;
rlm@1 360 removevars(ls, 0);
rlm@1 361 luaK_ret(fs, 0, 0); /* final return */
rlm@1 362 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
rlm@1 363 f->sizecode = fs->pc;
rlm@1 364 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
rlm@1 365 f->sizelineinfo = fs->pc;
rlm@1 366 luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
rlm@1 367 f->sizek = fs->nk;
rlm@1 368 luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
rlm@1 369 f->sizep = fs->np;
rlm@1 370 luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
rlm@1 371 f->sizelocvars = fs->nlocvars;
rlm@1 372 luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
rlm@1 373 f->sizeupvalues = f->nups;
rlm@1 374 lua_assert(luaG_checkcode(f));
rlm@1 375 lua_assert(fs->bl == NULL);
rlm@1 376 ls->fs = fs->prev;
rlm@1 377 L->top -= 2; /* remove table and prototype from the stack */
rlm@1 378 /* last token read was anchored in defunct function; must reanchor it */
rlm@1 379 if (fs) anchor_token(ls);
rlm@1 380 }
rlm@1 381
rlm@1 382
rlm@1 383 Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
rlm@1 384 struct LexState lexstate;
rlm@1 385 struct FuncState funcstate;
rlm@1 386 lexstate.buff = buff;
rlm@1 387 luaX_setinput(L, &lexstate, z, luaS_new(L, name));
rlm@1 388 open_func(&lexstate, &funcstate);
rlm@1 389 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
rlm@1 390 luaX_next(&lexstate); /* read first token */
rlm@1 391 chunk(&lexstate);
rlm@1 392 check(&lexstate, TK_EOS);
rlm@1 393 close_func(&lexstate);
rlm@1 394 lua_assert(funcstate.prev == NULL);
rlm@1 395 lua_assert(funcstate.f->nups == 0);
rlm@1 396 lua_assert(lexstate.fs == NULL);
rlm@1 397 return funcstate.f;
rlm@1 398 }
rlm@1 399
rlm@1 400
rlm@1 401
rlm@1 402 /*============================================================*/
rlm@1 403 /* GRAMMAR RULES */
rlm@1 404 /*============================================================*/
rlm@1 405
rlm@1 406
rlm@1 407 static void field (LexState *ls, expdesc *v) {
rlm@1 408 /* field -> ['.' | ':'] NAME */
rlm@1 409 FuncState *fs = ls->fs;
rlm@1 410 expdesc key;
rlm@1 411 luaK_exp2anyreg(fs, v);
rlm@1 412 luaX_next(ls); /* skip the dot or colon */
rlm@1 413 checkname(ls, &key);
rlm@1 414 luaK_indexed(fs, v, &key);
rlm@1 415 }
rlm@1 416
rlm@1 417
rlm@1 418 static void yindex (LexState *ls, expdesc *v) {
rlm@1 419 /* index -> '[' expr ']' */
rlm@1 420 luaX_next(ls); /* skip the '[' */
rlm@1 421 expr(ls, v);
rlm@1 422 luaK_exp2val(ls->fs, v);
rlm@1 423 checknext(ls, ']');
rlm@1 424 }
rlm@1 425
rlm@1 426
rlm@1 427 /*
rlm@1 428 ** {======================================================================
rlm@1 429 ** Rules for Constructors
rlm@1 430 ** =======================================================================
rlm@1 431 */
rlm@1 432
rlm@1 433
rlm@1 434 struct ConsControl {
rlm@1 435 expdesc v; /* last list item read */
rlm@1 436 expdesc *t; /* table descriptor */
rlm@1 437 int nh; /* total number of `record' elements */
rlm@1 438 int na; /* total number of array elements */
rlm@1 439 int tostore; /* number of array elements pending to be stored */
rlm@1 440 };
rlm@1 441
rlm@1 442
rlm@1 443 static void recfield (LexState *ls, struct ConsControl *cc) {
rlm@1 444 /* recfield -> (NAME | `['exp1`]') = exp1 */
rlm@1 445 FuncState *fs = ls->fs;
rlm@1 446 int reg = ls->fs->freereg;
rlm@1 447 expdesc key, val;
rlm@1 448 int rkkey;
rlm@1 449 if (ls->t.token == TK_NAME) {
rlm@1 450 luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
rlm@1 451 checkname(ls, &key);
rlm@1 452 }
rlm@1 453 else /* ls->t.token == '[' */
rlm@1 454 yindex(ls, &key);
rlm@1 455 cc->nh++;
rlm@1 456 checknext(ls, '=');
rlm@1 457 rkkey = luaK_exp2RK(fs, &key);
rlm@1 458 expr(ls, &val);
rlm@1 459 luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
rlm@1 460 fs->freereg = reg; /* free registers */
rlm@1 461 }
rlm@1 462
rlm@1 463
rlm@1 464 static void closelistfield (FuncState *fs, struct ConsControl *cc) {
rlm@1 465 if (cc->v.k == VVOID) return; /* there is no list item */
rlm@1 466 luaK_exp2nextreg(fs, &cc->v);
rlm@1 467 cc->v.k = VVOID;
rlm@1 468 if (cc->tostore == LFIELDS_PER_FLUSH) {
rlm@1 469 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */
rlm@1 470 cc->tostore = 0; /* no more items pending */
rlm@1 471 }
rlm@1 472 }
rlm@1 473
rlm@1 474
rlm@1 475 static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
rlm@1 476 if (cc->tostore == 0) return;
rlm@1 477 if (hasmultret(cc->v.k)) {
rlm@1 478 luaK_setmultret(fs, &cc->v);
rlm@1 479 luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);
rlm@1 480 cc->na--; /* do not count last expression (unknown number of elements) */
rlm@1 481 }
rlm@1 482 else {
rlm@1 483 if (cc->v.k != VVOID)
rlm@1 484 luaK_exp2nextreg(fs, &cc->v);
rlm@1 485 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);
rlm@1 486 }
rlm@1 487 }
rlm@1 488
rlm@1 489
rlm@1 490 static void listfield (LexState *ls, struct ConsControl *cc) {
rlm@1 491 expr(ls, &cc->v);
rlm@1 492 luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
rlm@1 493 cc->na++;
rlm@1 494 cc->tostore++;
rlm@1 495 }
rlm@1 496
rlm@1 497
rlm@1 498 static void constructor (LexState *ls, expdesc *t) {
rlm@1 499 /* constructor -> ?? */
rlm@1 500 FuncState *fs = ls->fs;
rlm@1 501 int line = ls->linenumber;
rlm@1 502 int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
rlm@1 503 struct ConsControl cc;
rlm@1 504 cc.na = cc.nh = cc.tostore = 0;
rlm@1 505 cc.t = t;
rlm@1 506 init_exp(t, VRELOCABLE, pc);
rlm@1 507 init_exp(&cc.v, VVOID, 0); /* no value (yet) */
rlm@1 508 luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
rlm@1 509 checknext(ls, '{');
rlm@1 510 do {
rlm@1 511 lua_assert(cc.v.k == VVOID || cc.tostore > 0);
rlm@1 512 if (ls->t.token == '}') break;
rlm@1 513 closelistfield(fs, &cc);
rlm@1 514 switch(ls->t.token) {
rlm@1 515 case TK_NAME: { /* may be listfields or recfields */
rlm@1 516 luaX_lookahead(ls);
rlm@1 517 if (ls->lookahead.token != '=') /* expression? */
rlm@1 518 listfield(ls, &cc);
rlm@1 519 else
rlm@1 520 recfield(ls, &cc);
rlm@1 521 break;
rlm@1 522 }
rlm@1 523 case '[': { /* constructor_item -> recfield */
rlm@1 524 recfield(ls, &cc);
rlm@1 525 break;
rlm@1 526 }
rlm@1 527 default: { /* constructor_part -> listfield */
rlm@1 528 listfield(ls, &cc);
rlm@1 529 break;
rlm@1 530 }
rlm@1 531 }
rlm@1 532 } while (testnext(ls, ',') || testnext(ls, ';'));
rlm@1 533 check_match(ls, '}', '{', line);
rlm@1 534 lastlistfield(fs, &cc);
rlm@1 535 SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
rlm@1 536 SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */
rlm@1 537 }
rlm@1 538
rlm@1 539 /* }====================================================================== */
rlm@1 540
rlm@1 541
rlm@1 542
rlm@1 543 static void parlist (LexState *ls) {
rlm@1 544 /* parlist -> [ param { `,' param } ] */
rlm@1 545 FuncState *fs = ls->fs;
rlm@1 546 Proto *f = fs->f;
rlm@1 547 int nparams = 0;
rlm@1 548 f->is_vararg = 0;
rlm@1 549 if (ls->t.token != ')') { /* is `parlist' not empty? */
rlm@1 550 do {
rlm@1 551 switch (ls->t.token) {
rlm@1 552 case TK_NAME: { /* param -> NAME */
rlm@1 553 new_localvar(ls, str_checkname(ls), nparams++);
rlm@1 554 break;
rlm@1 555 }
rlm@1 556 case TK_DOTS: { /* param -> `...' */
rlm@1 557 luaX_next(ls);
rlm@1 558 #if defined(LUA_COMPAT_VARARG)
rlm@1 559 /* use `arg' as default name */
rlm@1 560 new_localvarliteral(ls, "arg", nparams++);
rlm@1 561 f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
rlm@1 562 #endif
rlm@1 563 f->is_vararg |= VARARG_ISVARARG;
rlm@1 564 break;
rlm@1 565 }
rlm@1 566 default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
rlm@1 567 }
rlm@1 568 } while (!f->is_vararg && testnext(ls, ','));
rlm@1 569 }
rlm@1 570 adjustlocalvars(ls, nparams);
rlm@1 571 f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
rlm@1 572 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
rlm@1 573 }
rlm@1 574
rlm@1 575
rlm@1 576 static void body (LexState *ls, expdesc *e, int needself, int line) {
rlm@1 577 /* body -> `(' parlist `)' chunk END */
rlm@1 578 FuncState new_fs;
rlm@1 579 open_func(ls, &new_fs);
rlm@1 580 new_fs.f->linedefined = line;
rlm@1 581 checknext(ls, '(');
rlm@1 582 if (needself) {
rlm@1 583 new_localvarliteral(ls, "self", 0);
rlm@1 584 adjustlocalvars(ls, 1);
rlm@1 585 }
rlm@1 586 parlist(ls);
rlm@1 587 checknext(ls, ')');
rlm@1 588 chunk(ls);
rlm@1 589 new_fs.f->lastlinedefined = ls->linenumber;
rlm@1 590 check_match(ls, TK_END, TK_FUNCTION, line);
rlm@1 591 close_func(ls);
rlm@1 592 pushclosure(ls, &new_fs, e);
rlm@1 593 }
rlm@1 594
rlm@1 595
rlm@1 596 static int explist1 (LexState *ls, expdesc *v) {
rlm@1 597 /* explist1 -> expr { `,' expr } */
rlm@1 598 int n = 1; /* at least one expression */
rlm@1 599 expr(ls, v);
rlm@1 600 while (testnext(ls, ',')) {
rlm@1 601 luaK_exp2nextreg(ls->fs, v);
rlm@1 602 expr(ls, v);
rlm@1 603 n++;
rlm@1 604 }
rlm@1 605 return n;
rlm@1 606 }
rlm@1 607
rlm@1 608
rlm@1 609 static void funcargs (LexState *ls, expdesc *f) {
rlm@1 610 FuncState *fs = ls->fs;
rlm@1 611 expdesc args;
rlm@1 612 int base, nparams;
rlm@1 613 int line = ls->linenumber;
rlm@1 614 switch (ls->t.token) {
rlm@1 615 case '(': { /* funcargs -> `(' [ explist1 ] `)' */
rlm@1 616 if (line != ls->lastline)
rlm@1 617 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
rlm@1 618 luaX_next(ls);
rlm@1 619 if (ls->t.token == ')') /* arg list is empty? */
rlm@1 620 args.k = VVOID;
rlm@1 621 else {
rlm@1 622 explist1(ls, &args);
rlm@1 623 luaK_setmultret(fs, &args);
rlm@1 624 }
rlm@1 625 check_match(ls, ')', '(', line);
rlm@1 626 break;
rlm@1 627 }
rlm@1 628 case '{': { /* funcargs -> constructor */
rlm@1 629 constructor(ls, &args);
rlm@1 630 break;
rlm@1 631 }
rlm@1 632 case TK_STRING: { /* funcargs -> STRING */
rlm@1 633 codestring(ls, &args, ls->t.seminfo.ts);
rlm@1 634 luaX_next(ls); /* must use `seminfo' before `next' */
rlm@1 635 break;
rlm@1 636 }
rlm@1 637 default: {
rlm@1 638 luaX_syntaxerror(ls, "function arguments expected");
rlm@1 639 return;
rlm@1 640 }
rlm@1 641 }
rlm@1 642 lua_assert(f->k == VNONRELOC);
rlm@1 643 base = f->u.s.info; /* base register for call */
rlm@1 644 if (hasmultret(args.k))
rlm@1 645 nparams = LUA_MULTRET; /* open call */
rlm@1 646 else {
rlm@1 647 if (args.k != VVOID)
rlm@1 648 luaK_exp2nextreg(fs, &args); /* close last argument */
rlm@1 649 nparams = fs->freereg - (base+1);
rlm@1 650 }
rlm@1 651 init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
rlm@1 652 luaK_fixline(fs, line);
rlm@1 653 fs->freereg = base+1; /* call remove function and arguments and leaves
rlm@1 654 (unless changed) one result */
rlm@1 655 }
rlm@1 656
rlm@1 657
rlm@1 658
rlm@1 659
rlm@1 660 /*
rlm@1 661 ** {======================================================================
rlm@1 662 ** Expression parsing
rlm@1 663 ** =======================================================================
rlm@1 664 */
rlm@1 665
rlm@1 666
rlm@1 667 static void prefixexp (LexState *ls, expdesc *v) {
rlm@1 668 /* prefixexp -> NAME | '(' expr ')' */
rlm@1 669 switch (ls->t.token) {
rlm@1 670 case '(': {
rlm@1 671 int line = ls->linenumber;
rlm@1 672 luaX_next(ls);
rlm@1 673 expr(ls, v);
rlm@1 674 check_match(ls, ')', '(', line);
rlm@1 675 luaK_dischargevars(ls->fs, v);
rlm@1 676 return;
rlm@1 677 }
rlm@1 678 case TK_NAME: {
rlm@1 679 singlevar(ls, v);
rlm@1 680 return;
rlm@1 681 }
rlm@1 682 default: {
rlm@1 683 luaX_syntaxerror(ls, "unexpected symbol");
rlm@1 684 return;
rlm@1 685 }
rlm@1 686 }
rlm@1 687 }
rlm@1 688
rlm@1 689
rlm@1 690 static void primaryexp (LexState *ls, expdesc *v) {
rlm@1 691 /* primaryexp ->
rlm@1 692 prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
rlm@1 693 FuncState *fs = ls->fs;
rlm@1 694 prefixexp(ls, v);
rlm@1 695 for (;;) {
rlm@1 696 switch (ls->t.token) {
rlm@1 697 case '.': { /* field */
rlm@1 698 field(ls, v);
rlm@1 699 break;
rlm@1 700 }
rlm@1 701 case '[': { /* `[' exp1 `]' */
rlm@1 702 expdesc key;
rlm@1 703 luaK_exp2anyreg(fs, v);
rlm@1 704 yindex(ls, &key);
rlm@1 705 luaK_indexed(fs, v, &key);
rlm@1 706 break;
rlm@1 707 }
rlm@1 708 case ':': { /* `:' NAME funcargs */
rlm@1 709 expdesc key;
rlm@1 710 luaX_next(ls);
rlm@1 711 checkname(ls, &key);
rlm@1 712 luaK_self(fs, v, &key);
rlm@1 713 funcargs(ls, v);
rlm@1 714 break;
rlm@1 715 }
rlm@1 716 case '(': case TK_STRING: case '{': { /* funcargs */
rlm@1 717 luaK_exp2nextreg(fs, v);
rlm@1 718 funcargs(ls, v);
rlm@1 719 break;
rlm@1 720 }
rlm@1 721 default: return;
rlm@1 722 }
rlm@1 723 }
rlm@1 724 }
rlm@1 725
rlm@1 726
rlm@1 727 static void simpleexp (LexState *ls, expdesc *v) {
rlm@1 728 /* simpleexp -> NUMBER | STRING | NIL | true | false | ... |
rlm@1 729 constructor | FUNCTION body | primaryexp */
rlm@1 730 switch (ls->t.token) {
rlm@1 731 case TK_NUMBER: {
rlm@1 732 init_exp(v, VKNUM, 0);
rlm@1 733 v->u.nval = ls->t.seminfo.r;
rlm@1 734 break;
rlm@1 735 }
rlm@1 736 case TK_STRING: {
rlm@1 737 codestring(ls, v, ls->t.seminfo.ts);
rlm@1 738 break;
rlm@1 739 }
rlm@1 740 case TK_NIL: {
rlm@1 741 init_exp(v, VNIL, 0);
rlm@1 742 break;
rlm@1 743 }
rlm@1 744 case TK_TRUE: {
rlm@1 745 init_exp(v, VTRUE, 0);
rlm@1 746 break;
rlm@1 747 }
rlm@1 748 case TK_FALSE: {
rlm@1 749 init_exp(v, VFALSE, 0);
rlm@1 750 break;
rlm@1 751 }
rlm@1 752 case TK_DOTS: { /* vararg */
rlm@1 753 FuncState *fs = ls->fs;
rlm@1 754 check_condition(ls, fs->f->is_vararg,
rlm@1 755 "cannot use " LUA_QL("...") " outside a vararg function");
rlm@1 756 fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */
rlm@1 757 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
rlm@1 758 break;
rlm@1 759 }
rlm@1 760 case '{': { /* constructor */
rlm@1 761 constructor(ls, v);
rlm@1 762 return;
rlm@1 763 }
rlm@1 764 case TK_FUNCTION: {
rlm@1 765 luaX_next(ls);
rlm@1 766 body(ls, v, 0, ls->linenumber);
rlm@1 767 return;
rlm@1 768 }
rlm@1 769 default: {
rlm@1 770 primaryexp(ls, v);
rlm@1 771 return;
rlm@1 772 }
rlm@1 773 }
rlm@1 774 luaX_next(ls);
rlm@1 775 }
rlm@1 776
rlm@1 777
rlm@1 778 static UnOpr getunopr (int op) {
rlm@1 779 switch (op) {
rlm@1 780 case TK_NOT: return OPR_NOT;
rlm@1 781 case '-': return OPR_MINUS;
rlm@1 782 case '#': return OPR_LEN;
rlm@1 783 default: return OPR_NOUNOPR;
rlm@1 784 }
rlm@1 785 }
rlm@1 786
rlm@1 787
rlm@1 788 static BinOpr getbinopr (int op) {
rlm@1 789 switch (op) {
rlm@1 790 case '+': return OPR_ADD;
rlm@1 791 case '-': return OPR_SUB;
rlm@1 792 case '*': return OPR_MUL;
rlm@1 793 case '/': return OPR_DIV;
rlm@1 794 case '%': return OPR_MOD;
rlm@1 795 case '^': return OPR_POW;
rlm@1 796 case TK_CONCAT: return OPR_CONCAT;
rlm@1 797 case TK_NE: return OPR_NE;
rlm@1 798 case TK_EQ: return OPR_EQ;
rlm@1 799 case '<': return OPR_LT;
rlm@1 800 case TK_LE: return OPR_LE;
rlm@1 801 case '>': return OPR_GT;
rlm@1 802 case TK_GE: return OPR_GE;
rlm@1 803 case TK_AND: return OPR_AND;
rlm@1 804 case TK_OR: return OPR_OR;
rlm@1 805 default: return OPR_NOBINOPR;
rlm@1 806 }
rlm@1 807 }
rlm@1 808
rlm@1 809
rlm@1 810 static const struct {
rlm@1 811 lu_byte left; /* left priority for each binary operator */
rlm@1 812 lu_byte right; /* right priority */
rlm@1 813 } priority[] = { /* ORDER OPR */
rlm@1 814 {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */
rlm@1 815 {10, 9}, {5, 4}, /* power and concat (right associative) */
rlm@1 816 {3, 3}, {3, 3}, /* equality and inequality */
rlm@1 817 {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
rlm@1 818 {2, 2}, {1, 1} /* logical (and/or) */
rlm@1 819 };
rlm@1 820
rlm@1 821 #define UNARY_PRIORITY 8 /* priority for unary operators */
rlm@1 822
rlm@1 823
rlm@1 824 /*
rlm@1 825 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
rlm@1 826 ** where `binop' is any binary operator with a priority higher than `limit'
rlm@1 827 */
rlm@1 828 static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
rlm@1 829 BinOpr op;
rlm@1 830 UnOpr uop;
rlm@1 831 enterlevel(ls);
rlm@1 832 uop = getunopr(ls->t.token);
rlm@1 833 if (uop != OPR_NOUNOPR) {
rlm@1 834 luaX_next(ls);
rlm@1 835 subexpr(ls, v, UNARY_PRIORITY);
rlm@1 836 luaK_prefix(ls->fs, uop, v);
rlm@1 837 }
rlm@1 838 else simpleexp(ls, v);
rlm@1 839 /* expand while operators have priorities higher than `limit' */
rlm@1 840 op = getbinopr(ls->t.token);
rlm@1 841 while (op != OPR_NOBINOPR && priority[op].left > limit) {
rlm@1 842 expdesc v2;
rlm@1 843 BinOpr nextop;
rlm@1 844 luaX_next(ls);
rlm@1 845 luaK_infix(ls->fs, op, v);
rlm@1 846 /* read sub-expression with higher priority */
rlm@1 847 nextop = subexpr(ls, &v2, priority[op].right);
rlm@1 848 luaK_posfix(ls->fs, op, v, &v2);
rlm@1 849 op = nextop;
rlm@1 850 }
rlm@1 851 leavelevel(ls);
rlm@1 852 return op; /* return first untreated operator */
rlm@1 853 }
rlm@1 854
rlm@1 855
rlm@1 856 static void expr (LexState *ls, expdesc *v) {
rlm@1 857 subexpr(ls, v, 0);
rlm@1 858 }
rlm@1 859
rlm@1 860 /* }==================================================================== */
rlm@1 861
rlm@1 862
rlm@1 863
rlm@1 864 /*
rlm@1 865 ** {======================================================================
rlm@1 866 ** Rules for Statements
rlm@1 867 ** =======================================================================
rlm@1 868 */
rlm@1 869
rlm@1 870
rlm@1 871 static int block_follow (int token) {
rlm@1 872 switch (token) {
rlm@1 873 case TK_ELSE: case TK_ELSEIF: case TK_END:
rlm@1 874 case TK_UNTIL: case TK_EOS:
rlm@1 875 return 1;
rlm@1 876 default: return 0;
rlm@1 877 }
rlm@1 878 }
rlm@1 879
rlm@1 880
rlm@1 881 static void block (LexState *ls) {
rlm@1 882 /* block -> chunk */
rlm@1 883 FuncState *fs = ls->fs;
rlm@1 884 BlockCnt bl;
rlm@1 885 enterblock(fs, &bl, 0);
rlm@1 886 chunk(ls);
rlm@1 887 lua_assert(bl.breaklist == NO_JUMP);
rlm@1 888 leaveblock(fs);
rlm@1 889 }
rlm@1 890
rlm@1 891
rlm@1 892 /*
rlm@1 893 ** structure to chain all variables in the left-hand side of an
rlm@1 894 ** assignment
rlm@1 895 */
rlm@1 896 struct LHS_assign {
rlm@1 897 struct LHS_assign *prev;
rlm@1 898 expdesc v; /* variable (global, local, upvalue, or indexed) */
rlm@1 899 };
rlm@1 900
rlm@1 901
rlm@1 902 /*
rlm@1 903 ** check whether, in an assignment to a local variable, the local variable
rlm@1 904 ** is needed in a previous assignment (to a table). If so, save original
rlm@1 905 ** local value in a safe place and use this safe copy in the previous
rlm@1 906 ** assignment.
rlm@1 907 */
rlm@1 908 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
rlm@1 909 FuncState *fs = ls->fs;
rlm@1 910 int extra = fs->freereg; /* eventual position to save local variable */
rlm@1 911 int conflict = 0;
rlm@1 912 for (; lh; lh = lh->prev) {
rlm@1 913 if (lh->v.k == VINDEXED) {
rlm@1 914 if (lh->v.u.s.info == v->u.s.info) { /* conflict? */
rlm@1 915 conflict = 1;
rlm@1 916 lh->v.u.s.info = extra; /* previous assignment will use safe copy */
rlm@1 917 }
rlm@1 918 if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */
rlm@1 919 conflict = 1;
rlm@1 920 lh->v.u.s.aux = extra; /* previous assignment will use safe copy */
rlm@1 921 }
rlm@1 922 }
rlm@1 923 }
rlm@1 924 if (conflict) {
rlm@1 925 luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */
rlm@1 926 luaK_reserveregs(fs, 1);
rlm@1 927 }
rlm@1 928 }
rlm@1 929
rlm@1 930
rlm@1 931 static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
rlm@1 932 expdesc e;
rlm@1 933 check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
rlm@1 934 "syntax error");
rlm@1 935 if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
rlm@1 936 struct LHS_assign nv;
rlm@1 937 nv.prev = lh;
rlm@1 938 primaryexp(ls, &nv.v);
rlm@1 939 if (nv.v.k == VLOCAL)
rlm@1 940 check_conflict(ls, lh, &nv.v);
rlm@1 941 luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
rlm@1 942 "variables in assignment");
rlm@1 943 assignment(ls, &nv, nvars+1);
rlm@1 944 }
rlm@1 945 else { /* assignment -> `=' explist1 */
rlm@1 946 int nexps;
rlm@1 947 checknext(ls, '=');
rlm@1 948 nexps = explist1(ls, &e);
rlm@1 949 if (nexps != nvars) {
rlm@1 950 adjust_assign(ls, nvars, nexps, &e);
rlm@1 951 if (nexps > nvars)
rlm@1 952 ls->fs->freereg -= nexps - nvars; /* remove extra values */
rlm@1 953 }
rlm@1 954 else {
rlm@1 955 luaK_setoneret(ls->fs, &e); /* close last expression */
rlm@1 956 luaK_storevar(ls->fs, &lh->v, &e);
rlm@1 957 return; /* avoid default */
rlm@1 958 }
rlm@1 959 }
rlm@1 960 init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
rlm@1 961 luaK_storevar(ls->fs, &lh->v, &e);
rlm@1 962 }
rlm@1 963
rlm@1 964
rlm@1 965 static int cond (LexState *ls) {
rlm@1 966 /* cond -> exp */
rlm@1 967 expdesc v;
rlm@1 968 expr(ls, &v); /* read condition */
rlm@1 969 if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
rlm@1 970 luaK_goiftrue(ls->fs, &v);
rlm@1 971 return v.f;
rlm@1 972 }
rlm@1 973
rlm@1 974
rlm@1 975 static void breakstat (LexState *ls) {
rlm@1 976 FuncState *fs = ls->fs;
rlm@1 977 BlockCnt *bl = fs->bl;
rlm@1 978 int upval = 0;
rlm@1 979 while (bl && !bl->isbreakable) {
rlm@1 980 upval |= bl->upval;
rlm@1 981 bl = bl->previous;
rlm@1 982 }
rlm@1 983 if (!bl)
rlm@1 984 luaX_syntaxerror(ls, "no loop to break");
rlm@1 985 if (upval)
rlm@1 986 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
rlm@1 987 luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
rlm@1 988 }
rlm@1 989
rlm@1 990
rlm@1 991 static void whilestat (LexState *ls, int line) {
rlm@1 992 /* whilestat -> WHILE cond DO block END */
rlm@1 993 FuncState *fs = ls->fs;
rlm@1 994 int whileinit;
rlm@1 995 int condexit;
rlm@1 996 BlockCnt bl;
rlm@1 997 luaX_next(ls); /* skip WHILE */
rlm@1 998 whileinit = luaK_getlabel(fs);
rlm@1 999 condexit = cond(ls);
rlm@1 1000 enterblock(fs, &bl, 1);
rlm@1 1001 checknext(ls, TK_DO);
rlm@1 1002 block(ls);
rlm@1 1003 luaK_patchlist(fs, luaK_jump(fs), whileinit);
rlm@1 1004 check_match(ls, TK_END, TK_WHILE, line);
rlm@1 1005 leaveblock(fs);
rlm@1 1006 luaK_patchtohere(fs, condexit); /* false conditions finish the loop */
rlm@1 1007 }
rlm@1 1008
rlm@1 1009
rlm@1 1010 static void repeatstat (LexState *ls, int line) {
rlm@1 1011 /* repeatstat -> REPEAT block UNTIL cond */
rlm@1 1012 int condexit;
rlm@1 1013 FuncState *fs = ls->fs;
rlm@1 1014 int repeat_init = luaK_getlabel(fs);
rlm@1 1015 BlockCnt bl1, bl2;
rlm@1 1016 enterblock(fs, &bl1, 1); /* loop block */
rlm@1 1017 enterblock(fs, &bl2, 0); /* scope block */
rlm@1 1018 luaX_next(ls); /* skip REPEAT */
rlm@1 1019 chunk(ls);
rlm@1 1020 check_match(ls, TK_UNTIL, TK_REPEAT, line);
rlm@1 1021 condexit = cond(ls); /* read condition (inside scope block) */
rlm@1 1022 if (!bl2.upval) { /* no upvalues? */
rlm@1 1023 leaveblock(fs); /* finish scope */
rlm@1 1024 luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */
rlm@1 1025 }
rlm@1 1026 else { /* complete semantics when there are upvalues */
rlm@1 1027 breakstat(ls); /* if condition then break */
rlm@1 1028 luaK_patchtohere(ls->fs, condexit); /* else... */
rlm@1 1029 leaveblock(fs); /* finish scope... */
rlm@1 1030 luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */
rlm@1 1031 }
rlm@1 1032 leaveblock(fs); /* finish loop */
rlm@1 1033 }
rlm@1 1034
rlm@1 1035
rlm@1 1036 static int exp1 (LexState *ls) {
rlm@1 1037 expdesc e;
rlm@1 1038 int k;
rlm@1 1039 expr(ls, &e);
rlm@1 1040 k = e.k;
rlm@1 1041 luaK_exp2nextreg(ls->fs, &e);
rlm@1 1042 return k;
rlm@1 1043 }
rlm@1 1044
rlm@1 1045
rlm@1 1046 static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
rlm@1 1047 /* forbody -> DO block */
rlm@1 1048 BlockCnt bl;
rlm@1 1049 FuncState *fs = ls->fs;
rlm@1 1050 int prep, endfor;
rlm@1 1051 adjustlocalvars(ls, 3); /* control variables */
rlm@1 1052 checknext(ls, TK_DO);
rlm@1 1053 prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
rlm@1 1054 enterblock(fs, &bl, 0); /* scope for declared variables */
rlm@1 1055 adjustlocalvars(ls, nvars);
rlm@1 1056 luaK_reserveregs(fs, nvars);
rlm@1 1057 block(ls);
rlm@1 1058 leaveblock(fs); /* end of scope for declared variables */
rlm@1 1059 luaK_patchtohere(fs, prep);
rlm@1 1060 endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
rlm@1 1061 luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
rlm@1 1062 luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
rlm@1 1063 luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
rlm@1 1064 }
rlm@1 1065
rlm@1 1066
rlm@1 1067 static void fornum (LexState *ls, TString *varname, int line) {
rlm@1 1068 /* fornum -> NAME = exp1,exp1[,exp1] forbody */
rlm@1 1069 FuncState *fs = ls->fs;
rlm@1 1070 int base = fs->freereg;
rlm@1 1071 new_localvarliteral(ls, "(for index)", 0);
rlm@1 1072 new_localvarliteral(ls, "(for limit)", 1);
rlm@1 1073 new_localvarliteral(ls, "(for step)", 2);
rlm@1 1074 new_localvar(ls, varname, 3);
rlm@1 1075 checknext(ls, '=');
rlm@1 1076 exp1(ls); /* initial value */
rlm@1 1077 checknext(ls, ',');
rlm@1 1078 exp1(ls); /* limit */
rlm@1 1079 if (testnext(ls, ','))
rlm@1 1080 exp1(ls); /* optional step */
rlm@1 1081 else { /* default step = 1 */
rlm@1 1082 luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
rlm@1 1083 luaK_reserveregs(fs, 1);
rlm@1 1084 }
rlm@1 1085 forbody(ls, base, line, 1, 1);
rlm@1 1086 }
rlm@1 1087
rlm@1 1088
rlm@1 1089 static void forlist (LexState *ls, TString *indexname) {
rlm@1 1090 /* forlist -> NAME {,NAME} IN explist1 forbody */
rlm@1 1091 FuncState *fs = ls->fs;
rlm@1 1092 expdesc e;
rlm@1 1093 int nvars = 0;
rlm@1 1094 int line;
rlm@1 1095 int base = fs->freereg;
rlm@1 1096 /* create control variables */
rlm@1 1097 new_localvarliteral(ls, "(for generator)", nvars++);
rlm@1 1098 new_localvarliteral(ls, "(for state)", nvars++);
rlm@1 1099 new_localvarliteral(ls, "(for control)", nvars++);
rlm@1 1100 /* create declared variables */
rlm@1 1101 new_localvar(ls, indexname, nvars++);
rlm@1 1102 while (testnext(ls, ','))
rlm@1 1103 new_localvar(ls, str_checkname(ls), nvars++);
rlm@1 1104 checknext(ls, TK_IN);
rlm@1 1105 line = ls->linenumber;
rlm@1 1106 adjust_assign(ls, 3, explist1(ls, &e), &e);
rlm@1 1107 luaK_checkstack(fs, 3); /* extra space to call generator */
rlm@1 1108 forbody(ls, base, line, nvars - 3, 0);
rlm@1 1109 }
rlm@1 1110
rlm@1 1111
rlm@1 1112 static void forstat (LexState *ls, int line) {
rlm@1 1113 /* forstat -> FOR (fornum | forlist) END */
rlm@1 1114 FuncState *fs = ls->fs;
rlm@1 1115 TString *varname;
rlm@1 1116 BlockCnt bl;
rlm@1 1117 enterblock(fs, &bl, 1); /* scope for loop and control variables */
rlm@1 1118 luaX_next(ls); /* skip `for' */
rlm@1 1119 varname = str_checkname(ls); /* first variable name */
rlm@1 1120 switch (ls->t.token) {
rlm@1 1121 case '=': fornum(ls, varname, line); break;
rlm@1 1122 case ',': case TK_IN: forlist(ls, varname); break;
rlm@1 1123 default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
rlm@1 1124 }
rlm@1 1125 check_match(ls, TK_END, TK_FOR, line);
rlm@1 1126 leaveblock(fs); /* loop scope (`break' jumps to this point) */
rlm@1 1127 }
rlm@1 1128
rlm@1 1129
rlm@1 1130 static int test_then_block (LexState *ls) {
rlm@1 1131 /* test_then_block -> [IF | ELSEIF] cond THEN block */
rlm@1 1132 int condexit;
rlm@1 1133 luaX_next(ls); /* skip IF or ELSEIF */
rlm@1 1134 condexit = cond(ls);
rlm@1 1135 checknext(ls, TK_THEN);
rlm@1 1136 block(ls); /* `then' part */
rlm@1 1137 return condexit;
rlm@1 1138 }
rlm@1 1139
rlm@1 1140
rlm@1 1141 static void ifstat (LexState *ls, int line) {
rlm@1 1142 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
rlm@1 1143 FuncState *fs = ls->fs;
rlm@1 1144 int flist;
rlm@1 1145 int escapelist = NO_JUMP;
rlm@1 1146 flist = test_then_block(ls); /* IF cond THEN block */
rlm@1 1147 while (ls->t.token == TK_ELSEIF) {
rlm@1 1148 luaK_concat(fs, &escapelist, luaK_jump(fs));
rlm@1 1149 luaK_patchtohere(fs, flist);
rlm@1 1150 flist = test_then_block(ls); /* ELSEIF cond THEN block */
rlm@1 1151 }
rlm@1 1152 if (ls->t.token == TK_ELSE) {
rlm@1 1153 luaK_concat(fs, &escapelist, luaK_jump(fs));
rlm@1 1154 luaK_patchtohere(fs, flist);
rlm@1 1155 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
rlm@1 1156 block(ls); /* `else' part */
rlm@1 1157 }
rlm@1 1158 else
rlm@1 1159 luaK_concat(fs, &escapelist, flist);
rlm@1 1160 luaK_patchtohere(fs, escapelist);
rlm@1 1161 check_match(ls, TK_END, TK_IF, line);
rlm@1 1162 }
rlm@1 1163
rlm@1 1164
rlm@1 1165 static void localfunc (LexState *ls) {
rlm@1 1166 expdesc v, b;
rlm@1 1167 FuncState *fs = ls->fs;
rlm@1 1168 new_localvar(ls, str_checkname(ls), 0);
rlm@1 1169 init_exp(&v, VLOCAL, fs->freereg);
rlm@1 1170 luaK_reserveregs(fs, 1);
rlm@1 1171 adjustlocalvars(ls, 1);
rlm@1 1172 body(ls, &b, 0, ls->linenumber);
rlm@1 1173 luaK_storevar(fs, &v, &b);
rlm@1 1174 /* debug information will only see the variable after this point! */
rlm@1 1175 getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
rlm@1 1176 }
rlm@1 1177
rlm@1 1178
rlm@1 1179 static void localstat (LexState *ls) {
rlm@1 1180 /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
rlm@1 1181 int nvars = 0;
rlm@1 1182 int nexps;
rlm@1 1183 expdesc e;
rlm@1 1184 do {
rlm@1 1185 new_localvar(ls, str_checkname(ls), nvars++);
rlm@1 1186 } while (testnext(ls, ','));
rlm@1 1187 if (testnext(ls, '='))
rlm@1 1188 nexps = explist1(ls, &e);
rlm@1 1189 else {
rlm@1 1190 e.k = VVOID;
rlm@1 1191 nexps = 0;
rlm@1 1192 }
rlm@1 1193 adjust_assign(ls, nvars, nexps, &e);
rlm@1 1194 adjustlocalvars(ls, nvars);
rlm@1 1195 }
rlm@1 1196
rlm@1 1197
rlm@1 1198 static int funcname (LexState *ls, expdesc *v) {
rlm@1 1199 /* funcname -> NAME {field} [`:' NAME] */
rlm@1 1200 int needself = 0;
rlm@1 1201 singlevar(ls, v);
rlm@1 1202 while (ls->t.token == '.')
rlm@1 1203 field(ls, v);
rlm@1 1204 if (ls->t.token == ':') {
rlm@1 1205 needself = 1;
rlm@1 1206 field(ls, v);
rlm@1 1207 }
rlm@1 1208 return needself;
rlm@1 1209 }
rlm@1 1210
rlm@1 1211
rlm@1 1212 static void funcstat (LexState *ls, int line) {
rlm@1 1213 /* funcstat -> FUNCTION funcname body */
rlm@1 1214 int needself;
rlm@1 1215 expdesc v, b;
rlm@1 1216 luaX_next(ls); /* skip FUNCTION */
rlm@1 1217 needself = funcname(ls, &v);
rlm@1 1218 body(ls, &b, needself, line);
rlm@1 1219 luaK_storevar(ls->fs, &v, &b);
rlm@1 1220 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
rlm@1 1221 }
rlm@1 1222
rlm@1 1223
rlm@1 1224 static void exprstat (LexState *ls) {
rlm@1 1225 /* stat -> func | assignment */
rlm@1 1226 FuncState *fs = ls->fs;
rlm@1 1227 struct LHS_assign v;
rlm@1 1228 primaryexp(ls, &v.v);
rlm@1 1229 if (v.v.k == VCALL) /* stat -> func */
rlm@1 1230 SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
rlm@1 1231 else { /* stat -> assignment */
rlm@1 1232 v.prev = NULL;
rlm@1 1233 assignment(ls, &v, 1);
rlm@1 1234 }
rlm@1 1235 }
rlm@1 1236
rlm@1 1237
rlm@1 1238 static void retstat (LexState *ls) {
rlm@1 1239 /* stat -> RETURN explist */
rlm@1 1240 FuncState *fs = ls->fs;
rlm@1 1241 expdesc e;
rlm@1 1242 int first, nret; /* registers with returned values */
rlm@1 1243 luaX_next(ls); /* skip RETURN */
rlm@1 1244 if (block_follow(ls->t.token) || ls->t.token == ';')
rlm@1 1245 first = nret = 0; /* return no values */
rlm@1 1246 else {
rlm@1 1247 nret = explist1(ls, &e); /* optional return values */
rlm@1 1248 if (hasmultret(e.k)) {
rlm@1 1249 luaK_setmultret(fs, &e);
rlm@1 1250 if (e.k == VCALL && nret == 1) { /* tail call? */
rlm@1 1251 SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
rlm@1 1252 lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
rlm@1 1253 }
rlm@1 1254 first = fs->nactvar;
rlm@1 1255 nret = LUA_MULTRET; /* return all values */
rlm@1 1256 }
rlm@1 1257 else {
rlm@1 1258 if (nret == 1) /* only one single value? */
rlm@1 1259 first = luaK_exp2anyreg(fs, &e);
rlm@1 1260 else {
rlm@1 1261 luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
rlm@1 1262 first = fs->nactvar; /* return all `active' values */
rlm@1 1263 lua_assert(nret == fs->freereg - first);
rlm@1 1264 }
rlm@1 1265 }
rlm@1 1266 }
rlm@1 1267 luaK_ret(fs, first, nret);
rlm@1 1268 }
rlm@1 1269
rlm@1 1270
rlm@1 1271 static int statement (LexState *ls) {
rlm@1 1272 int line = ls->linenumber; /* may be needed for error messages */
rlm@1 1273 switch (ls->t.token) {
rlm@1 1274 case TK_IF: { /* stat -> ifstat */
rlm@1 1275 ifstat(ls, line);
rlm@1 1276 return 0;
rlm@1 1277 }
rlm@1 1278 case TK_WHILE: { /* stat -> whilestat */
rlm@1 1279 whilestat(ls, line);
rlm@1 1280 return 0;
rlm@1 1281 }
rlm@1 1282 case TK_DO: { /* stat -> DO block END */
rlm@1 1283 luaX_next(ls); /* skip DO */
rlm@1 1284 block(ls);
rlm@1 1285 check_match(ls, TK_END, TK_DO, line);
rlm@1 1286 return 0;
rlm@1 1287 }
rlm@1 1288 case TK_FOR: { /* stat -> forstat */
rlm@1 1289 forstat(ls, line);
rlm@1 1290 return 0;
rlm@1 1291 }
rlm@1 1292 case TK_REPEAT: { /* stat -> repeatstat */
rlm@1 1293 repeatstat(ls, line);
rlm@1 1294 return 0;
rlm@1 1295 }
rlm@1 1296 case TK_FUNCTION: {
rlm@1 1297 funcstat(ls, line); /* stat -> funcstat */
rlm@1 1298 return 0;
rlm@1 1299 }
rlm@1 1300 case TK_LOCAL: { /* stat -> localstat */
rlm@1 1301 luaX_next(ls); /* skip LOCAL */
rlm@1 1302 if (testnext(ls, TK_FUNCTION)) /* local function? */
rlm@1 1303 localfunc(ls);
rlm@1 1304 else
rlm@1 1305 localstat(ls);
rlm@1 1306 return 0;
rlm@1 1307 }
rlm@1 1308 case TK_RETURN: { /* stat -> retstat */
rlm@1 1309 retstat(ls);
rlm@1 1310 return 1; /* must be last statement */
rlm@1 1311 }
rlm@1 1312 case TK_BREAK: { /* stat -> breakstat */
rlm@1 1313 luaX_next(ls); /* skip BREAK */
rlm@1 1314 breakstat(ls);
rlm@1 1315 return 1; /* must be last statement */
rlm@1 1316 }
rlm@1 1317 default: {
rlm@1 1318 exprstat(ls);
rlm@1 1319 return 0; /* to avoid warnings */
rlm@1 1320 }
rlm@1 1321 }
rlm@1 1322 }
rlm@1 1323
rlm@1 1324
rlm@1 1325 static void chunk (LexState *ls) {
rlm@1 1326 /* chunk -> { stat [`;'] } */
rlm@1 1327 int islast = 0;
rlm@1 1328 enterlevel(ls);
rlm@1 1329 while (!islast && !block_follow(ls->t.token)) {
rlm@1 1330 islast = statement(ls);
rlm@1 1331 testnext(ls, ';');
rlm@1 1332 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
rlm@1 1333 ls->fs->freereg >= ls->fs->nactvar);
rlm@1 1334 ls->fs->freereg = ls->fs->nactvar; /* free registers */
rlm@1 1335 }
rlm@1 1336 leavelevel(ls);
rlm@1 1337 }
rlm@1 1338
rlm@1 1339 /* }====================================================================== */