lparser.c 36 KB

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