lparser.c 36 KB

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