lparser.c 34 KB

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