lparser.c 36 KB

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