lparser.c 35 KB

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