lparser.c 34 KB

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