lparser.c 34 KB

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