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