lparser.c 36 KB

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