lparser.c 36 KB

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