lparser.c 36 KB

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