lparser.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. ** $Id: lparser.c,v 1.201 2002/12/06 17:09:00 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. OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
  235. luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
  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 forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  861. BlockCnt bl;
  862. FuncState *fs = ls->fs;
  863. int prep, endfor;
  864. adjustlocalvars(ls, nvars); /* scope for all variables */
  865. check(ls, TK_DO);
  866. enterblock(fs, &bl, 1); /* loop block */
  867. prep = luaK_getlabel(fs);
  868. block(ls);
  869. luaK_patchtohere(fs, prep-1);
  870. endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  871. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
  872. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  873. luaK_patchlist(fs, (isnum) ? endfor : luaK_jump(fs), prep);
  874. leaveblock(fs);
  875. }
  876. static void fornum (LexState *ls, TString *varname, int line) {
  877. /* fornum -> NAME = exp1,exp1[,exp1] DO body */
  878. FuncState *fs = ls->fs;
  879. int base = fs->freereg;
  880. new_localvar(ls, varname, 0);
  881. new_localvarstr(ls, "(for limit)", 1);
  882. new_localvarstr(ls, "(for step)", 2);
  883. check(ls, '=');
  884. exp1(ls); /* initial value */
  885. check(ls, ',');
  886. exp1(ls); /* limit */
  887. if (testnext(ls, ','))
  888. exp1(ls); /* optional step */
  889. else { /* default step = 1 */
  890. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  891. luaK_reserveregs(fs, 1);
  892. }
  893. luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
  894. luaK_jump(fs);
  895. forbody(ls, base, line, 3, 1);
  896. }
  897. static void forlist (LexState *ls, TString *indexname) {
  898. /* forlist -> NAME {,NAME} IN explist1 DO body */
  899. FuncState *fs = ls->fs;
  900. expdesc e;
  901. int nvars = 0;
  902. int line;
  903. int base = fs->freereg;
  904. new_localvarstr(ls, "(for generator)", nvars++);
  905. new_localvarstr(ls, "(for state)", nvars++);
  906. new_localvar(ls, indexname, nvars++);
  907. while (testnext(ls, ','))
  908. new_localvar(ls, str_checkname(ls), nvars++);
  909. check(ls, TK_IN);
  910. line = ls->linenumber;
  911. adjust_assign(ls, nvars, explist1(ls, &e), &e);
  912. luaK_checkstack(fs, 3); /* extra space to call generator */
  913. luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
  914. forbody(ls, base, line, nvars, 0);
  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, 0); /* block to control variable scope */
  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. /* }====================================================================== */