lparser.c 34 KB

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