lparser.c 36 KB

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