lparser.c 34 KB

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