/src/lparser.c

http://lvmextend.googlecode.com/ · C · 1415 lines · 1105 code · 184 blank · 126 comment · 192 complexity · 4679f78e8a6bd67462b37882192369c9 MD5 · raw file

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