lparser.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. ** $Id: lparser.c,v 2.55 2007/10/18 11:01:52 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, "%s expected", luaX_token2str(ls, token)));
  49. }
  50. static void errorlimit (FuncState *fs, int limit, const char *what) {
  51. const char *msg;
  52. const char *where = (fs->f->linedefined == 0) ?
  53. "main function" :
  54. luaO_pushfstring(fs->L, "function at line %d", fs->f->linedefined);
  55. msg = luaO_pushfstring(fs->L, "too many %s (limit is %d) in %s",
  56. what, limit, where);
  57. luaX_syntaxerror(fs->ls, msg);
  58. }
  59. static int testnext (LexState *ls, int c) {
  60. if (ls->t.token == c) {
  61. luaX_next(ls);
  62. return 1;
  63. }
  64. else return 0;
  65. }
  66. static void check (LexState *ls, int c) {
  67. if (ls->t.token != c)
  68. error_expected(ls, c);
  69. }
  70. static void checknext (LexState *ls, int c) {
  71. check(ls, c);
  72. luaX_next(ls);
  73. }
  74. #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
  75. static void check_match (LexState *ls, int what, int who, int where) {
  76. if (!testnext(ls, what)) {
  77. if (where == ls->linenumber)
  78. error_expected(ls, what);
  79. else {
  80. luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
  81. "%s expected (to close %s at line %d)",
  82. luaX_token2str(ls, what), luaX_token2str(ls, who), where));
  83. }
  84. }
  85. }
  86. static TString *str_checkname (LexState *ls) {
  87. TString *ts;
  88. check(ls, TK_NAME);
  89. ts = ls->t.seminfo.ts;
  90. luaX_next(ls);
  91. return ts;
  92. }
  93. static void init_exp (expdesc *e, expkind k, int i) {
  94. e->f = e->t = NO_JUMP;
  95. e->k = k;
  96. e->u.s.info = i;
  97. }
  98. static void codestring (LexState *ls, expdesc *e, TString *s) {
  99. init_exp(e, VK, luaK_stringK(ls->fs, s));
  100. }
  101. static void checkname(LexState *ls, expdesc *e) {
  102. codestring(ls, e, str_checkname(ls));
  103. }
  104. static int registerlocalvar (LexState *ls, TString *varname) {
  105. FuncState *fs = ls->fs;
  106. Proto *f = fs->f;
  107. int oldsize = f->sizelocvars;
  108. luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
  109. LocVar, SHRT_MAX, "local variables");
  110. while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
  111. f->locvars[fs->nlocvars].varname = varname;
  112. luaC_objbarrier(ls->L, f, varname);
  113. return fs->nlocvars++;
  114. }
  115. #define new_localvarliteral(ls,v,n) \
  116. new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
  117. static void new_localvar (LexState *ls, TString *name, int n) {
  118. FuncState *fs = ls->fs;
  119. luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
  120. fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));
  121. }
  122. static void adjustlocalvars (LexState *ls, int nvars) {
  123. FuncState *fs = ls->fs;
  124. fs->nactvar = cast_byte(fs->nactvar + nvars);
  125. for (; nvars; nvars--) {
  126. getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
  127. }
  128. }
  129. static void removevars (FuncState *fs, int tolevel) {
  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, "upvalues");
  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. global_State *g = G(ls->L);
  216. ++g->nCcalls;
  217. luaY_checklimit(ls->fs, g->nCcalls, LUAI_MAXCCALLS, "syntax levels");
  218. }
  219. #define leavelevel(ls) (G((ls)->L)->nCcalls--)
  220. static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
  221. bl->breaklist = NO_JUMP;
  222. bl->isbreakable = isbreakable;
  223. bl->nactvar = fs->nactvar;
  224. bl->upval = 0;
  225. bl->previous = fs->bl;
  226. fs->bl = bl;
  227. lua_assert(fs->freereg == fs->nactvar);
  228. }
  229. static void leaveblock (FuncState *fs) {
  230. BlockCnt *bl = fs->bl;
  231. fs->bl = bl->previous;
  232. removevars(fs, bl->nactvar);
  233. if (bl->upval)
  234. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  235. /* a block either controls scope or breaks (never both) */
  236. lua_assert(!bl->isbreakable || !bl->upval);
  237. lua_assert(bl->nactvar == fs->nactvar);
  238. fs->freereg = fs->nactvar; /* free registers */
  239. luaK_patchtohere(fs, bl->breaklist);
  240. }
  241. static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
  242. FuncState *fs = ls->fs->prev;
  243. Proto *f = fs->f;
  244. int oldsize = f->sizep;
  245. int i;
  246. luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
  247. MAXARG_Bx, "constants");
  248. while (oldsize < f->sizep) f->p[oldsize++] = NULL;
  249. f->p[fs->np++] = func->f;
  250. luaC_objbarrier(ls->L, f, func->f);
  251. init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
  252. for (i=0; i<func->f->nups; i++) {
  253. OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
  254. luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
  255. }
  256. }
  257. static void open_func (LexState *ls, FuncState *fs) {
  258. lua_State *L = ls->L;
  259. Proto *f;
  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 = 0;
  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. fs->h = luaH_new(L);
  274. /* anchor table of constants (to avoid being collected) */
  275. sethvalue2s(L, L->top, fs->h);
  276. incr_top(L);
  277. f = luaF_newproto(L);
  278. fs->f = f;
  279. f->source = ls->source;
  280. f->maxstacksize = 2; /* registers 0/1 are always valid */
  281. /* anchor prototype (to avoid being collected) */
  282. setptvalue2s(L, L->top, f);
  283. incr_top(L);
  284. }
  285. static void close_func (LexState *ls) {
  286. lua_State *L = ls->L;
  287. FuncState *fs = ls->fs;
  288. Proto *f = fs->f;
  289. removevars(fs, 0);
  290. luaK_ret(fs, 0, 0); /* final return */
  291. luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
  292. f->sizecode = fs->pc;
  293. luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
  294. f->sizelineinfo = fs->pc;
  295. luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
  296. f->sizek = fs->nk;
  297. luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
  298. f->sizep = fs->np;
  299. luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
  300. f->sizelocvars = fs->nlocvars;
  301. luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
  302. f->sizeupvalues = f->nups;
  303. lua_assert(luaG_checkcode(f));
  304. lua_assert(fs->bl == NULL);
  305. ls->fs = fs->prev;
  306. L->top -= 2; /* remove table and prototype from the stack */
  307. /* last token read was anchored in defunct function; must reanchor it */
  308. if (fs) anchor_token(ls);
  309. }
  310. Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
  311. struct LexState lexstate;
  312. struct FuncState funcstate;
  313. TString *tname = luaS_new(L, name);
  314. setsvalue2s(L, L->top, tname); /* protect name */
  315. incr_top(L);
  316. lexstate.buff = buff;
  317. luaX_setinput(L, &lexstate, z, tname);
  318. open_func(&lexstate, &funcstate);
  319. funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
  320. luaX_next(&lexstate); /* read first token */
  321. chunk(&lexstate);
  322. check(&lexstate, TK_EOS);
  323. close_func(&lexstate);
  324. L->top--;
  325. lua_assert(funcstate.prev == NULL);
  326. lua_assert(funcstate.f->nups == 0);
  327. lua_assert(lexstate.fs == NULL);
  328. return funcstate.f;
  329. }
  330. /*============================================================*/
  331. /* GRAMMAR RULES */
  332. /*============================================================*/
  333. static void field (LexState *ls, expdesc *v) {
  334. /* field -> ['.' | ':'] NAME */
  335. FuncState *fs = ls->fs;
  336. expdesc key;
  337. luaK_exp2anyreg(fs, v);
  338. luaX_next(ls); /* skip the dot or colon */
  339. checkname(ls, &key);
  340. luaK_indexed(fs, v, &key);
  341. }
  342. static void yindex (LexState *ls, expdesc *v) {
  343. /* index -> '[' expr ']' */
  344. luaX_next(ls); /* skip the '[' */
  345. expr(ls, v);
  346. luaK_exp2val(ls->fs, v);
  347. checknext(ls, ']');
  348. }
  349. /*
  350. ** {======================================================================
  351. ** Rules for Constructors
  352. ** =======================================================================
  353. */
  354. struct ConsControl {
  355. expdesc v; /* last list item read */
  356. expdesc *t; /* table descriptor */
  357. int nh; /* total number of `record' elements */
  358. int na; /* total number of array elements */
  359. int tostore; /* number of array elements pending to be stored */
  360. };
  361. static void recfield (LexState *ls, struct ConsControl *cc) {
  362. /* recfield -> (NAME | `['exp1`]') = exp1 */
  363. FuncState *fs = ls->fs;
  364. int reg = ls->fs->freereg;
  365. expdesc key, val;
  366. int rkkey;
  367. if (ls->t.token == TK_NAME) {
  368. luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
  369. checkname(ls, &key);
  370. }
  371. else /* ls->t.token == '[' */
  372. yindex(ls, &key);
  373. cc->nh++;
  374. checknext(ls, '=');
  375. rkkey = luaK_exp2RK(fs, &key);
  376. expr(ls, &val);
  377. luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
  378. fs->freereg = reg; /* free registers */
  379. }
  380. static void closelistfield (FuncState *fs, struct ConsControl *cc) {
  381. if (cc->v.k == VVOID) return; /* there is no list item */
  382. luaK_exp2nextreg(fs, &cc->v);
  383. cc->v.k = VVOID;
  384. if (cc->tostore == LFIELDS_PER_FLUSH) {
  385. luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */
  386. cc->tostore = 0; /* no more items pending */
  387. }
  388. }
  389. static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
  390. if (cc->tostore == 0) return;
  391. if (hasmultret(cc->v.k)) {
  392. luaK_setmultret(fs, &cc->v);
  393. luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);
  394. cc->na--; /* do not count last expression (unknown number of elements) */
  395. }
  396. else {
  397. if (cc->v.k != VVOID)
  398. luaK_exp2nextreg(fs, &cc->v);
  399. luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);
  400. }
  401. }
  402. static void listfield (LexState *ls, struct ConsControl *cc) {
  403. expr(ls, &cc->v);
  404. luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
  405. cc->na++;
  406. cc->tostore++;
  407. }
  408. static void constructor (LexState *ls, expdesc *t) {
  409. /* constructor -> ?? */
  410. FuncState *fs = ls->fs;
  411. int line = ls->linenumber;
  412. int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  413. struct ConsControl cc;
  414. cc.na = cc.nh = cc.tostore = 0;
  415. cc.t = t;
  416. init_exp(t, VRELOCABLE, pc);
  417. init_exp(&cc.v, VVOID, 0); /* no value (yet) */
  418. luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
  419. checknext(ls, '{');
  420. do {
  421. lua_assert(cc.v.k == VVOID || cc.tostore > 0);
  422. if (ls->t.token == '}') break;
  423. closelistfield(fs, &cc);
  424. switch(ls->t.token) {
  425. case TK_NAME: { /* may be listfields or recfields */
  426. if (luaX_lookahead(ls) != '=') /* 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. luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - G(ls->L)->nCcalls,
  806. "variable names");
  807. assignment(ls, &nv, nvars+1);
  808. }
  809. else { /* assignment -> `=' explist1 */
  810. int nexps;
  811. checknext(ls, '=');
  812. nexps = explist1(ls, &e);
  813. if (nexps != nvars) {
  814. adjust_assign(ls, nvars, nexps, &e);
  815. if (nexps > nvars)
  816. ls->fs->freereg -= nexps - nvars; /* remove extra values */
  817. }
  818. else {
  819. luaK_setoneret(ls->fs, &e); /* close last expression */
  820. luaK_storevar(ls->fs, &lh->v, &e);
  821. return; /* avoid default */
  822. }
  823. }
  824. init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
  825. luaK_storevar(ls->fs, &lh->v, &e);
  826. }
  827. static int cond (LexState *ls) {
  828. /* cond -> exp */
  829. expdesc v;
  830. expr(ls, &v); /* read condition */
  831. if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
  832. luaK_goiftrue(ls->fs, &v);
  833. return v.f;
  834. }
  835. static void breakstat (LexState *ls) {
  836. FuncState *fs = ls->fs;
  837. BlockCnt *bl = fs->bl;
  838. int upval = 0;
  839. while (bl && !bl->isbreakable) {
  840. upval |= bl->upval;
  841. bl = bl->previous;
  842. }
  843. if (!bl)
  844. luaX_syntaxerror(ls, "no loop to break");
  845. if (upval)
  846. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  847. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  848. }
  849. static void whilestat (LexState *ls, int line) {
  850. /* whilestat -> WHILE cond DO block END */
  851. FuncState *fs = ls->fs;
  852. int whileinit;
  853. int condexit;
  854. BlockCnt bl;
  855. luaX_next(ls); /* skip WHILE */
  856. whileinit = luaK_getlabel(fs);
  857. condexit = cond(ls);
  858. enterblock(fs, &bl, 1);
  859. checknext(ls, TK_DO);
  860. block(ls);
  861. luaK_patchlist(fs, luaK_jump(fs), whileinit);
  862. check_match(ls, TK_END, TK_WHILE, line);
  863. leaveblock(fs);
  864. luaK_patchtohere(fs, condexit); /* false conditions finish the loop */
  865. }
  866. static void repeatstat (LexState *ls, int line) {
  867. /* repeatstat -> REPEAT block UNTIL cond */
  868. int condexit;
  869. FuncState *fs = ls->fs;
  870. int repeat_init = luaK_getlabel(fs);
  871. BlockCnt bl1, bl2;
  872. enterblock(fs, &bl1, 1); /* loop block */
  873. enterblock(fs, &bl2, 0); /* scope block */
  874. luaX_next(ls); /* skip REPEAT */
  875. chunk(ls);
  876. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  877. condexit = cond(ls); /* read condition (inside scope block) */
  878. if (!bl2.upval) { /* no upvalues? */
  879. leaveblock(fs); /* finish scope */
  880. luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */
  881. }
  882. else { /* complete semantics when there are upvalues */
  883. breakstat(ls); /* if condition then break */
  884. luaK_patchtohere(ls->fs, condexit); /* else... */
  885. leaveblock(fs); /* finish scope... */
  886. luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */
  887. }
  888. leaveblock(fs); /* finish loop */
  889. }
  890. static int exp1 (LexState *ls) {
  891. expdesc e;
  892. int k;
  893. expr(ls, &e);
  894. k = e.k;
  895. luaK_exp2nextreg(ls->fs, &e);
  896. return k;
  897. }
  898. static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  899. /* forbody -> DO block */
  900. BlockCnt bl;
  901. FuncState *fs = ls->fs;
  902. int prep, endfor;
  903. adjustlocalvars(ls, 3); /* control variables */
  904. checknext(ls, TK_DO);
  905. prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
  906. enterblock(fs, &bl, 0); /* scope for declared variables */
  907. adjustlocalvars(ls, nvars);
  908. luaK_reserveregs(fs, nvars);
  909. block(ls);
  910. leaveblock(fs); /* end of scope for declared variables */
  911. luaK_patchtohere(fs, prep);
  912. endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  913. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  914. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  915. luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  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. /* }====================================================================== */