lparser.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /*
  2. ** $Id: lparser.c,v 1.210 2003/05/14 12:32:46 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 int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
  125. int i;
  126. Proto *f = fs->f;
  127. for (i=0; i<f->nups; i++) {
  128. if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->info) {
  129. lua_assert(fs->f->upvalues[i] == name);
  130. return i;
  131. }
  132. }
  133. /* new one */
  134. luaX_checklimit(fs->ls, f->nups + 1, MAXUPVALUES, "upvalues");
  135. luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues,
  136. TString *, MAX_INT, "");
  137. fs->f->upvalues[f->nups] = name;
  138. fs->upvalues[f->nups] = *v;
  139. return f->nups++;
  140. }
  141. static int searchvar (FuncState *fs, TString *n) {
  142. int i;
  143. for (i=fs->nactvar-1; i >= 0; i--) {
  144. if (n == getlocvar(fs, i).varname)
  145. return i;
  146. }
  147. return -1; /* not found */
  148. }
  149. static void markupval (FuncState *fs, int level) {
  150. BlockCnt *bl = fs->bl;
  151. while (bl && bl->nactvar > level) bl = bl->previous;
  152. if (bl) bl->upval = 1;
  153. }
  154. static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
  155. if (fs == NULL) /* no more levels? */
  156. init_exp(var, VGLOBAL, NO_REG); /* default is global variable */
  157. else {
  158. int v = searchvar(fs, n); /* look up at current level */
  159. if (v >= 0) {
  160. init_exp(var, VLOCAL, v);
  161. if (!base)
  162. markupval(fs, v); /* local will be used as an upval */
  163. }
  164. else { /* not found at current level; try upper one */
  165. singlevaraux(fs->prev, n, var, 0);
  166. if (var->k == VGLOBAL) {
  167. if (base)
  168. var->info = luaK_stringK(fs, n); /* info points to global name */
  169. }
  170. else { /* LOCAL or UPVAL */
  171. var->info = indexupvalue(fs, n, var);
  172. var->k = VUPVAL; /* upvalue in this level */
  173. }
  174. }
  175. }
  176. }
  177. static TString *singlevar (LexState *ls, expdesc *var, int base) {
  178. TString *varname = str_checkname(ls);
  179. singlevaraux(ls->fs, varname, var, base);
  180. return varname;
  181. }
  182. static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
  183. FuncState *fs = ls->fs;
  184. int extra = nvars - nexps;
  185. if (e->k == VCALL) {
  186. extra++; /* includes call itself */
  187. if (extra <= 0) extra = 0;
  188. else luaK_reserveregs(fs, extra-1);
  189. luaK_setcallreturns(fs, e, extra); /* call provides the difference */
  190. }
  191. else {
  192. if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
  193. if (extra > 0) {
  194. int reg = fs->freereg;
  195. luaK_reserveregs(fs, extra);
  196. luaK_nil(fs, reg, extra);
  197. }
  198. }
  199. }
  200. static void code_params (LexState *ls, int nparams, TString *dots) {
  201. FuncState *fs = ls->fs;
  202. Proto *f = fs->f;
  203. adjustlocalvars(ls, nparams);
  204. luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters");
  205. f->numparams = cast(lu_byte, fs->nactvar);
  206. if (!dots)
  207. f->is_vararg = 0;
  208. else {
  209. f->is_vararg = 1;
  210. new_localvar(ls, dots, 0);
  211. adjustlocalvars(ls, 1);
  212. }
  213. luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
  214. }
  215. static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable) {
  216. bl->breaklist = NO_JUMP;
  217. bl->isbreakable = isbreakable;
  218. bl->nactvar = fs->nactvar;
  219. bl->upval = 0;
  220. bl->previous = fs->bl;
  221. fs->bl = bl;
  222. lua_assert(fs->freereg == fs->nactvar);
  223. }
  224. static void leaveblock (FuncState *fs) {
  225. BlockCnt *bl = fs->bl;
  226. fs->bl = bl->previous;
  227. removevars(fs->ls, bl->nactvar);
  228. if (bl->upval)
  229. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  230. lua_assert(bl->nactvar == fs->nactvar);
  231. fs->freereg = fs->nactvar; /* free registers */
  232. luaK_patchtohere(fs, bl->breaklist);
  233. }
  234. static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
  235. FuncState *fs = ls->fs;
  236. Proto *f = fs->f;
  237. int i;
  238. luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
  239. MAXARG_Bx, "constant table overflow");
  240. f->p[fs->np++] = func->f;
  241. init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
  242. for (i=0; i<func->f->nups; i++) {
  243. OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
  244. luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
  245. }
  246. }
  247. static void open_func (LexState *ls, FuncState *fs) {
  248. Proto *f = luaF_newproto(ls->L);
  249. fs->f = f;
  250. fs->prev = ls->fs; /* linked list of funcstates */
  251. fs->ls = ls;
  252. fs->L = ls->L;
  253. ls->fs = fs;
  254. fs->pc = 0;
  255. fs->lasttarget = 0;
  256. fs->jpc = NO_JUMP;
  257. fs->freereg = 0;
  258. fs->nk = 0;
  259. fs->h = luaH_new(ls->L, 0, 0);
  260. fs->np = 0;
  261. fs->nlocvars = 0;
  262. fs->nactvar = 0;
  263. fs->bl = NULL;
  264. f->source = ls->source;
  265. f->maxstacksize = 2; /* registers 0/1 are always valid */
  266. }
  267. static void close_func (LexState *ls) {
  268. lua_State *L = ls->L;
  269. FuncState *fs = ls->fs;
  270. Proto *f = fs->f;
  271. removevars(ls, 0);
  272. luaK_codeABC(fs, OP_RETURN, 0, 1, 0); /* final return */
  273. luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
  274. f->sizecode = fs->pc;
  275. luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
  276. f->sizelineinfo = fs->pc;
  277. luaM_reallocvector(L, f->k, f->sizek, fs->nk, TObject);
  278. f->sizek = fs->nk;
  279. luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
  280. f->sizep = fs->np;
  281. luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
  282. f->sizelocvars = fs->nlocvars;
  283. luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
  284. f->sizeupvalues = f->nups;
  285. lua_assert(luaG_checkcode(f));
  286. lua_assert(fs->bl == NULL);
  287. ls->fs = fs->prev;
  288. }
  289. Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff) {
  290. struct LexState lexstate;
  291. struct FuncState funcstate;
  292. lexstate.buff = buff;
  293. lexstate.nestlevel = 0;
  294. luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z)));
  295. open_func(&lexstate, &funcstate);
  296. next(&lexstate); /* read first token */
  297. chunk(&lexstate);
  298. check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
  299. close_func(&lexstate);
  300. lua_assert(funcstate.prev == NULL);
  301. lua_assert(funcstate.f->nups == 0);
  302. lua_assert(lexstate.nestlevel == 0);
  303. return funcstate.f;
  304. }
  305. /*============================================================*/
  306. /* GRAMMAR RULES */
  307. /*============================================================*/
  308. static void luaY_field (LexState *ls, expdesc *v) {
  309. /* field -> ['.' | ':'] NAME */
  310. FuncState *fs = ls->fs;
  311. expdesc key;
  312. luaK_exp2anyreg(fs, v);
  313. next(ls); /* skip the dot or colon */
  314. checkname(ls, &key);
  315. luaK_indexed(fs, v, &key);
  316. }
  317. static void luaY_index (LexState *ls, expdesc *v) {
  318. /* index -> '[' expr ']' */
  319. next(ls); /* skip the '[' */
  320. expr(ls, v);
  321. luaK_exp2val(ls->fs, v);
  322. check(ls, ']');
  323. }
  324. /*
  325. ** {======================================================================
  326. ** Rules for Constructors
  327. ** =======================================================================
  328. */
  329. struct ConsControl {
  330. expdesc v; /* last list item read */
  331. expdesc *t; /* table descriptor */
  332. int nh; /* total number of `record' elements */
  333. int na; /* total number of array elements */
  334. int tostore; /* number of array elements pending to be stored */
  335. };
  336. static void recfield (LexState *ls, struct ConsControl *cc) {
  337. /* recfield -> (NAME | `['exp1`]') = exp1 */
  338. FuncState *fs = ls->fs;
  339. int reg = ls->fs->freereg;
  340. expdesc key, val;
  341. if (ls->t.token == TK_NAME) {
  342. luaX_checklimit(ls, cc->nh, MAX_INT, "items in a constructor");
  343. cc->nh++;
  344. checkname(ls, &key);
  345. }
  346. else /* ls->t.token == '[' */
  347. luaY_index(ls, &key);
  348. check(ls, '=');
  349. luaK_exp2RK(fs, &key);
  350. expr(ls, &val);
  351. luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key),
  352. luaK_exp2RK(fs, &val));
  353. fs->freereg = reg; /* free registers */
  354. }
  355. static void closelistfield (FuncState *fs, struct ConsControl *cc) {
  356. if (cc->v.k == VVOID) return; /* there is no list item */
  357. luaK_exp2nextreg(fs, &cc->v);
  358. cc->v.k = VVOID;
  359. if (cc->tostore == LFIELDS_PER_FLUSH) {
  360. luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1); /* flush */
  361. cc->tostore = 0; /* no more items pending */
  362. fs->freereg = cc->t->info + 1; /* free registers */
  363. }
  364. }
  365. static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
  366. if (cc->tostore == 0) return;
  367. if (cc->v.k == VCALL) {
  368. luaK_setcallreturns(fs, &cc->v, LUA_MULTRET);
  369. luaK_codeABx(fs, OP_SETLISTO, cc->t->info, cc->na-1);
  370. }
  371. else {
  372. if (cc->v.k != VVOID)
  373. luaK_exp2nextreg(fs, &cc->v);
  374. luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1);
  375. }
  376. fs->freereg = cc->t->info + 1; /* free registers */
  377. }
  378. static void listfield (LexState *ls, struct ConsControl *cc) {
  379. expr(ls, &cc->v);
  380. luaX_checklimit(ls, cc->na, MAXARG_Bx, "items in a constructor");
  381. cc->na++;
  382. cc->tostore++;
  383. }
  384. static void constructor (LexState *ls, expdesc *t) {
  385. /* constructor -> ?? */
  386. FuncState *fs = ls->fs;
  387. int line = ls->linenumber;
  388. int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  389. struct ConsControl cc;
  390. cc.na = cc.nh = cc.tostore = 0;
  391. cc.t = t;
  392. init_exp(t, VRELOCABLE, pc);
  393. init_exp(&cc.v, VVOID, 0); /* no value (yet) */
  394. luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
  395. check(ls, '{');
  396. do {
  397. lua_assert(cc.v.k == VVOID || cc.tostore > 0);
  398. testnext(ls, ';'); /* compatibility only */
  399. if (ls->t.token == '}') break;
  400. closelistfield(fs, &cc);
  401. switch(ls->t.token) {
  402. case TK_NAME: { /* may be listfields or recfields */
  403. lookahead(ls);
  404. if (ls->lookahead.token != '=') /* expression? */
  405. listfield(ls, &cc);
  406. else
  407. recfield(ls, &cc);
  408. break;
  409. }
  410. case '[': { /* constructor_item -> recfield */
  411. recfield(ls, &cc);
  412. break;
  413. }
  414. default: { /* constructor_part -> listfield */
  415. listfield(ls, &cc);
  416. break;
  417. }
  418. }
  419. } while (testnext(ls, ',') || testnext(ls, ';'));
  420. check_match(ls, '}', '{', line);
  421. lastlistfield(fs, &cc);
  422. SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
  423. SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */
  424. }
  425. /* }====================================================================== */
  426. static void parlist (LexState *ls) {
  427. /* parlist -> [ param { `,' param } ] */
  428. int nparams = 0;
  429. TString *dots = NULL;
  430. if (ls->t.token != ')') { /* is `parlist' not empty? */
  431. do {
  432. switch (ls->t.token) {
  433. case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
  434. case TK_DOTS: {
  435. next(ls);
  436. dots = (testnext(ls, '=')) ? str_checkname(ls) :
  437. luaS_new(ls->L, "arg");
  438. break;
  439. }
  440. default: luaX_syntaxerror(ls, "<name> or `...' expected");
  441. }
  442. } while (!dots && testnext(ls, ','));
  443. }
  444. code_params(ls, nparams, dots);
  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, 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 && cast(int, 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, cast(int, 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, -1);
  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 void cond (LexState *ls, expdesc *v) {
  785. /* cond -> exp */
  786. expr(ls, v); /* read condition */
  787. if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */
  788. luaK_goiftrue(ls->fs, v);
  789. luaK_patchtohere(ls->fs, v->t);
  790. }
  791. /*
  792. ** The while statement optimizes its code by coding the condition
  793. ** after its body (and thus avoiding one jump in the loop).
  794. */
  795. /*
  796. ** maximum size of expressions for optimizing `while' code
  797. */
  798. #ifndef MAXEXPWHILE
  799. #define MAXEXPWHILE 100
  800. #endif
  801. /*
  802. ** the call `luaK_goiffalse' may grow the size of an expression by
  803. ** at most this:
  804. */
  805. #define EXTRAEXP 5
  806. static void whilestat (LexState *ls, int line) {
  807. /* whilestat -> WHILE cond DO block END */
  808. Instruction codeexp[MAXEXPWHILE + EXTRAEXP];
  809. int lineexp;
  810. int i;
  811. int sizeexp;
  812. FuncState *fs = ls->fs;
  813. int whileinit, blockinit, expinit;
  814. expdesc v;
  815. BlockCnt bl;
  816. next(ls); /* skip WHILE */
  817. whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
  818. expinit = luaK_getlabel(fs);
  819. expr(ls, &v); /* parse condition */
  820. if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
  821. lineexp = ls->linenumber;
  822. luaK_goiffalse(fs, &v);
  823. luaK_concat(fs, &v.f, fs->jpc);
  824. fs->jpc = NO_JUMP;
  825. sizeexp = fs->pc - expinit; /* size of expression code */
  826. if (sizeexp > MAXEXPWHILE)
  827. luaX_syntaxerror(ls, "`while' condition too complex");
  828. for (i = 0; i < sizeexp; i++) /* save `exp' code */
  829. codeexp[i] = fs->f->code[expinit + i];
  830. fs->pc = expinit; /* remove `exp' code */
  831. enterblock(fs, &bl, 1);
  832. check(ls, TK_DO);
  833. blockinit = luaK_getlabel(fs);
  834. block(ls);
  835. luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
  836. /* move `exp' back to code */
  837. if (v.t != NO_JUMP) v.t += fs->pc - expinit;
  838. if (v.f != NO_JUMP) v.f += fs->pc - expinit;
  839. for (i=0; i<sizeexp; i++)
  840. luaK_code(fs, codeexp[i], lineexp);
  841. check_match(ls, TK_END, TK_WHILE, line);
  842. leaveblock(fs);
  843. luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
  844. luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
  845. }
  846. static void repeatstat (LexState *ls, int line) {
  847. /* repeatstat -> REPEAT block UNTIL cond */
  848. FuncState *fs = ls->fs;
  849. int repeat_init = luaK_getlabel(fs);
  850. expdesc v;
  851. BlockCnt bl;
  852. enterblock(fs, &bl, 1);
  853. next(ls);
  854. block(ls);
  855. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  856. cond(ls, &v);
  857. luaK_patchlist(fs, v.f, repeat_init);
  858. leaveblock(fs);
  859. }
  860. static int exp1 (LexState *ls) {
  861. expdesc e;
  862. int k;
  863. expr(ls, &e);
  864. k = e.k;
  865. luaK_exp2nextreg(ls->fs, &e);
  866. return k;
  867. }
  868. static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  869. /* forbody -> DO block */
  870. BlockCnt bl;
  871. FuncState *fs = ls->fs;
  872. int prep, endfor;
  873. adjustlocalvars(ls, 3); /* control variables */
  874. check(ls, TK_DO);
  875. prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP);
  876. enterblock(fs, &bl, 0); /* scope for declared variables */
  877. adjustlocalvars(ls, nvars);
  878. luaK_reserveregs(fs, nvars);
  879. block(ls);
  880. leaveblock(fs); /* end of scope for declared variables */
  881. luaK_patchtohere(fs, prep);
  882. endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  883. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  884. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  885. luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  886. }
  887. static void fornum (LexState *ls, TString *varname, int line) {
  888. /* fornum -> NAME = exp1,exp1[,exp1] forbody */
  889. FuncState *fs = ls->fs;
  890. int base = fs->freereg;
  891. new_localvarstr(ls, "(for index)", 0);
  892. new_localvarstr(ls, "(for limit)", 1);
  893. new_localvarstr(ls, "(for step)", 2);
  894. new_localvar(ls, varname, 3);
  895. check(ls, '=');
  896. exp1(ls); /* initial value */
  897. check(ls, ',');
  898. exp1(ls); /* limit */
  899. if (testnext(ls, ','))
  900. exp1(ls); /* optional step */
  901. else { /* default step = 1 */
  902. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  903. luaK_reserveregs(fs, 1);
  904. }
  905. forbody(ls, base, line, 1, 1);
  906. }
  907. static void forlist (LexState *ls, TString *indexname) {
  908. /* forlist -> NAME {,NAME} IN explist1 forbody */
  909. FuncState *fs = ls->fs;
  910. expdesc e;
  911. int nvars = 0;
  912. int line;
  913. int base = fs->freereg;
  914. /* create control variables */
  915. new_localvarstr(ls, "(for generator)", nvars++);
  916. new_localvarstr(ls, "(for state)", nvars++);
  917. new_localvarstr(ls, "(for control)", nvars++);
  918. /* create declared variables */
  919. new_localvar(ls, indexname, nvars++);
  920. while (testnext(ls, ','))
  921. new_localvar(ls, str_checkname(ls), nvars++);
  922. check(ls, TK_IN);
  923. line = ls->linenumber;
  924. adjust_assign(ls, 3, explist1(ls, &e), &e);
  925. luaK_checkstack(fs, 3); /* extra space to call generator */
  926. forbody(ls, base, line, nvars - 3, 0);
  927. }
  928. static void forstat (LexState *ls, int line) {
  929. /* forstat -> FOR (fornum | forlist) END */
  930. FuncState *fs = ls->fs;
  931. TString *varname;
  932. BlockCnt bl;
  933. enterblock(fs, &bl, 1); /* scope for loop and control variables */
  934. next(ls); /* skip `for' */
  935. varname = str_checkname(ls); /* first variable name */
  936. switch (ls->t.token) {
  937. case '=': fornum(ls, varname, line); break;
  938. case ',': case TK_IN: forlist(ls, varname); break;
  939. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  940. }
  941. check_match(ls, TK_END, TK_FOR, line);
  942. leaveblock(fs); /* loop scope (`break' jumps to this point) */
  943. }
  944. static void test_then_block (LexState *ls, expdesc *v) {
  945. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  946. next(ls); /* skip IF or ELSEIF */
  947. cond(ls, v);
  948. check(ls, TK_THEN);
  949. block(ls); /* `then' part */
  950. }
  951. static void ifstat (LexState *ls, int line) {
  952. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  953. FuncState *fs = ls->fs;
  954. expdesc v;
  955. int escapelist = NO_JUMP;
  956. test_then_block(ls, &v); /* IF cond THEN block */
  957. while (ls->t.token == TK_ELSEIF) {
  958. luaK_concat(fs, &escapelist, luaK_jump(fs));
  959. luaK_patchtohere(fs, v.f);
  960. test_then_block(ls, &v); /* ELSEIF cond THEN block */
  961. }
  962. if (ls->t.token == TK_ELSE) {
  963. luaK_concat(fs, &escapelist, luaK_jump(fs));
  964. luaK_patchtohere(fs, v.f);
  965. next(ls); /* skip ELSE (after patch, for correct line info) */
  966. block(ls); /* `else' part */
  967. }
  968. else
  969. luaK_concat(fs, &escapelist, v.f);
  970. luaK_patchtohere(fs, escapelist);
  971. check_match(ls, TK_END, TK_IF, line);
  972. }
  973. static void localfunc (LexState *ls) {
  974. expdesc v, b;
  975. new_localvar(ls, str_checkname(ls), 0);
  976. init_exp(&v, VLOCAL, ls->fs->freereg++);
  977. adjustlocalvars(ls, 1);
  978. body(ls, &b, 0, ls->linenumber);
  979. luaK_storevar(ls->fs, &v, &b);
  980. }
  981. static void localstat (LexState *ls) {
  982. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  983. int nvars = 0;
  984. int nexps;
  985. expdesc e;
  986. do {
  987. new_localvar(ls, str_checkname(ls), nvars++);
  988. } while (testnext(ls, ','));
  989. if (testnext(ls, '='))
  990. nexps = explist1(ls, &e);
  991. else {
  992. e.k = VVOID;
  993. nexps = 0;
  994. }
  995. adjust_assign(ls, nvars, nexps, &e);
  996. adjustlocalvars(ls, nvars);
  997. }
  998. static int funcname (LexState *ls, expdesc *v) {
  999. /* funcname -> NAME {field} [`:' NAME] */
  1000. int needself = 0;
  1001. singlevar(ls, v, 1);
  1002. while (ls->t.token == '.')
  1003. luaY_field(ls, v);
  1004. if (ls->t.token == ':') {
  1005. needself = 1;
  1006. luaY_field(ls, v);
  1007. }
  1008. return needself;
  1009. }
  1010. static void funcstat (LexState *ls, int line) {
  1011. /* funcstat -> FUNCTION funcname body */
  1012. int needself;
  1013. expdesc v, b;
  1014. next(ls); /* skip FUNCTION */
  1015. needself = funcname(ls, &v);
  1016. body(ls, &b, needself, line);
  1017. luaK_storevar(ls->fs, &v, &b);
  1018. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  1019. }
  1020. static void exprstat (LexState *ls) {
  1021. /* stat -> func | assignment */
  1022. FuncState *fs = ls->fs;
  1023. struct LHS_assign v;
  1024. primaryexp(ls, &v.v);
  1025. if (v.v.k == VCALL) { /* stat -> func */
  1026. luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
  1027. }
  1028. else { /* stat -> assignment */
  1029. v.prev = NULL;
  1030. assignment(ls, &v, 1);
  1031. }
  1032. }
  1033. static void retstat (LexState *ls) {
  1034. /* stat -> RETURN explist */
  1035. FuncState *fs = ls->fs;
  1036. expdesc e;
  1037. int first, nret; /* registers with returned values */
  1038. next(ls); /* skip RETURN */
  1039. if (block_follow(ls->t.token) || ls->t.token == ';')
  1040. first = nret = 0; /* return no values */
  1041. else {
  1042. nret = explist1(ls, &e); /* optional return values */
  1043. if (e.k == VCALL) {
  1044. luaK_setcallreturns(fs, &e, LUA_MULTRET);
  1045. if (nret == 1) { /* tail call? */
  1046. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1047. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1048. }
  1049. first = fs->nactvar;
  1050. nret = LUA_MULTRET; /* return all values */
  1051. }
  1052. else {
  1053. if (nret == 1) /* only one single value? */
  1054. first = luaK_exp2anyreg(fs, &e);
  1055. else {
  1056. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1057. first = fs->nactvar; /* return all `active' values */
  1058. lua_assert(nret == fs->freereg - first);
  1059. }
  1060. }
  1061. }
  1062. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1063. }
  1064. static void breakstat (LexState *ls) {
  1065. /* stat -> BREAK [NAME] */
  1066. FuncState *fs = ls->fs;
  1067. BlockCnt *bl = fs->bl;
  1068. int upval = 0;
  1069. next(ls); /* skip BREAK */
  1070. while (bl && !bl->isbreakable) {
  1071. upval |= bl->upval;
  1072. bl = bl->previous;
  1073. }
  1074. if (!bl)
  1075. luaX_syntaxerror(ls, "no loop to break");
  1076. if (upval)
  1077. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1078. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1079. }
  1080. static int statement (LexState *ls) {
  1081. int line = ls->linenumber; /* may be needed for error messages */
  1082. switch (ls->t.token) {
  1083. case TK_IF: { /* stat -> ifstat */
  1084. ifstat(ls, line);
  1085. return 0;
  1086. }
  1087. case TK_WHILE: { /* stat -> whilestat */
  1088. whilestat(ls, line);
  1089. return 0;
  1090. }
  1091. case TK_DO: { /* stat -> DO block END */
  1092. next(ls); /* skip DO */
  1093. block(ls);
  1094. check_match(ls, TK_END, TK_DO, line);
  1095. return 0;
  1096. }
  1097. case TK_FOR: { /* stat -> forstat */
  1098. forstat(ls, line);
  1099. return 0;
  1100. }
  1101. case TK_REPEAT: { /* stat -> repeatstat */
  1102. repeatstat(ls, line);
  1103. return 0;
  1104. }
  1105. case TK_FUNCTION: {
  1106. funcstat(ls, line); /* stat -> funcstat */
  1107. return 0;
  1108. }
  1109. case TK_LOCAL: { /* stat -> localstat */
  1110. next(ls); /* skip LOCAL */
  1111. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1112. localfunc(ls);
  1113. else
  1114. localstat(ls);
  1115. return 0;
  1116. }
  1117. case TK_RETURN: { /* stat -> retstat */
  1118. retstat(ls);
  1119. return 1; /* must be last statement */
  1120. }
  1121. case TK_BREAK: { /* stat -> breakstat */
  1122. breakstat(ls);
  1123. return 1; /* must be last statement */
  1124. }
  1125. default: {
  1126. exprstat(ls);
  1127. return 0; /* to avoid warnings */
  1128. }
  1129. }
  1130. }
  1131. static void chunk (LexState *ls) {
  1132. /* chunk -> { stat [`;'] } */
  1133. int islast = 0;
  1134. enterlevel(ls);
  1135. while (!islast && !block_follow(ls->t.token)) {
  1136. islast = statement(ls);
  1137. testnext(ls, ';');
  1138. lua_assert(ls->fs->freereg >= ls->fs->nactvar);
  1139. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1140. }
  1141. leavelevel(ls);
  1142. }
  1143. /* }====================================================================== */