lparser.c 36 KB

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