lparser.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. ** $Id: lparser.c,v 1.195 2002/10/08 18:46:08 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 int testnext (LexState *ls, int c) {
  54. if (ls->t.token == c) {
  55. next(ls);
  56. return 1;
  57. }
  58. else return 0;
  59. }
  60. static void check (LexState *ls, int c) {
  61. if (!testnext(ls, c))
  62. error_expected(ls, c);
  63. }
  64. #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
  65. static void check_match (LexState *ls, int what, int who, int where) {
  66. if (!testnext(ls, what)) {
  67. if (where == ls->linenumber)
  68. error_expected(ls, what);
  69. else {
  70. luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
  71. "`%s' expected (to close `%s' at line %d)",
  72. luaX_token2str(ls, what), luaX_token2str(ls, who), where));
  73. }
  74. }
  75. }
  76. static TString *str_checkname (LexState *ls) {
  77. TString *ts;
  78. check_condition(ls, (ls->t.token == TK_NAME), "<name> expected");
  79. ts = ls->t.seminfo.ts;
  80. next(ls);
  81. return 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. }
  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 singlevaraux (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. singlevaraux(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 singlevar (LexState *ls, expdesc *var, int base) {
  174. singlevaraux(ls->fs, str_checkname(ls), var, base);
  175. }
  176. static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
  177. FuncState *fs = ls->fs;
  178. int extra = nvars - nexps;
  179. if (e->k == VCALL) {
  180. extra++; /* includes call itself */
  181. if (extra <= 0) extra = 0;
  182. else luaK_reserveregs(fs, extra-1);
  183. luaK_setcallreturns(fs, e, extra); /* call provides the difference */
  184. }
  185. else {
  186. if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
  187. if (extra > 0) {
  188. int reg = fs->freereg;
  189. luaK_reserveregs(fs, extra);
  190. luaK_nil(fs, reg, extra);
  191. }
  192. }
  193. }
  194. static void code_params (LexState *ls, int nparams, int dots) {
  195. FuncState *fs = ls->fs;
  196. adjustlocalvars(ls, nparams);
  197. luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters");
  198. fs->f->numparams = cast(lu_byte, fs->nactvar);
  199. fs->f->is_vararg = cast(lu_byte, dots);
  200. if (dots)
  201. create_local(ls, "arg");
  202. luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
  203. }
  204. static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable) {
  205. bl->breaklist = NO_JUMP;
  206. bl->isbreakable = isbreakable;
  207. bl->nactvar = fs->nactvar;
  208. bl->upval = 0;
  209. bl->previous = fs->bl;
  210. fs->bl = bl;
  211. lua_assert(fs->freereg == fs->nactvar);
  212. }
  213. static void leaveblock (FuncState *fs) {
  214. BlockCnt *bl = fs->bl;
  215. fs->bl = bl->previous;
  216. removevars(fs->ls, bl->nactvar);
  217. if (bl->upval)
  218. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  219. lua_assert(bl->nactvar == fs->nactvar);
  220. fs->freereg = fs->nactvar; /* free registers */
  221. luaK_patchtohere(fs, bl->breaklist);
  222. }
  223. static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
  224. FuncState *fs = ls->fs;
  225. Proto *f = fs->f;
  226. int i;
  227. luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
  228. MAXARG_Bx, "constant table overflow");
  229. f->p[fs->np++] = func->f;
  230. init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
  231. for (i=0; i<func->f->nupvalues; i++) {
  232. luaK_exp2nextreg(fs, &func->upvalues[i]);
  233. fs->freereg--; /* CLOSURE will use these values */
  234. }
  235. }
  236. static void open_func (LexState *ls, FuncState *fs) {
  237. Proto *f = luaF_newproto(ls->L);
  238. fs->f = f;
  239. fs->prev = ls->fs; /* linked list of funcstates */
  240. fs->ls = ls;
  241. fs->L = ls->L;
  242. ls->fs = fs;
  243. fs->pc = 0;
  244. fs->lasttarget = 0;
  245. fs->jpc = NO_JUMP;
  246. fs->freereg = 0;
  247. fs->nk = 0;
  248. fs->h = luaH_new(ls->L, 0, 0);
  249. fs->np = 0;
  250. fs->nlocvars = 0;
  251. fs->nactvar = 0;
  252. fs->bl = NULL;
  253. f->code = NULL;
  254. f->source = ls->source;
  255. f->maxstacksize = 2; /* registers 0/1 are always valid */
  256. f->numparams = 0; /* default for main chunk */
  257. f->is_vararg = 0; /* default for main chunk */
  258. }
  259. static void close_func (LexState *ls) {
  260. lua_State *L = ls->L;
  261. FuncState *fs = ls->fs;
  262. Proto *f = fs->f;
  263. removevars(ls, 0);
  264. luaK_codeABC(fs, OP_RETURN, 0, 1, 0); /* final return */
  265. luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
  266. f->sizecode = fs->pc;
  267. luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
  268. f->sizelineinfo = 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, Mbuffer *buff) {
  280. struct LexState lexstate;
  281. struct FuncState funcstate;
  282. lexstate.buff = buff;
  283. luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z)));
  284. open_func(&lexstate, &funcstate);
  285. next(&lexstate); /* read first token */
  286. chunk(&lexstate);
  287. check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
  288. close_func(&lexstate);
  289. lua_assert(funcstate.prev == NULL);
  290. lua_assert(funcstate.f->nupvalues == 0);
  291. return funcstate.f;
  292. }
  293. /*============================================================*/
  294. /* GRAMMAR RULES */
  295. /*============================================================*/
  296. static void luaY_field (LexState *ls, expdesc *v) {
  297. /* field -> ['.' | ':'] NAME */
  298. FuncState *fs = ls->fs;
  299. expdesc key;
  300. luaK_exp2anyreg(fs, v);
  301. next(ls); /* skip the dot or colon */
  302. checkname(ls, &key);
  303. luaK_indexed(fs, v, &key);
  304. }
  305. static void luaY_index (LexState *ls, expdesc *v) {
  306. /* index -> '[' expr ']' */
  307. next(ls); /* skip the '[' */
  308. expr(ls, v);
  309. luaK_exp2val(ls->fs, v);
  310. check(ls, ']');
  311. }
  312. static int explist1 (LexState *ls, expdesc *v) {
  313. /* explist1 -> expr { `,' expr } */
  314. int n = 1; /* at least one expression */
  315. expr(ls, v);
  316. while (testnext(ls, ',')) {
  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_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key),
  398. luaK_exp2RK(fs, &val));
  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. do {
  443. lua_assert(cc.v.k == VVOID || cc.tostore > 0);
  444. testnext(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. } while (testnext(ls, ',') || testnext(ls, ';'));
  466. check_match(ls, '}', '{', line);
  467. lastlistfield(fs, &cc);
  468. if (cc.na > 0)
  469. SETARG_B(fs->f->code[pc], luaO_log2(cc.na-1)+2); /* set initial table size */
  470. SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */
  471. }
  472. /* }====================================================================== */
  473. /*
  474. ** {======================================================================
  475. ** Expression parsing
  476. ** =======================================================================
  477. */
  478. static void prefixexp (LexState *ls, expdesc *v) {
  479. /* prefixexp -> NAME | '(' expr ')' */
  480. switch (ls->t.token) {
  481. case '(': {
  482. int line = ls->linenumber;
  483. next(ls);
  484. expr(ls, v);
  485. check_match(ls, ')', '(', line);
  486. luaK_dischargevars(ls->fs, v);
  487. return;
  488. }
  489. case TK_NAME: {
  490. singlevar(ls, v, 1);
  491. return;
  492. }
  493. case '%': { /* for compatibility only */
  494. next(ls); /* skip `%' */
  495. singlevar(ls, v, 1);
  496. check_condition(ls, v->k == VUPVAL, "global upvalues are obsolete");
  497. return;
  498. }
  499. default: {
  500. luaX_syntaxerror(ls, "unexpected symbol");
  501. return;
  502. }
  503. }
  504. }
  505. static void primaryexp (LexState *ls, expdesc *v) {
  506. /* primaryexp ->
  507. prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
  508. FuncState *fs = ls->fs;
  509. prefixexp(ls, v);
  510. for (;;) {
  511. switch (ls->t.token) {
  512. case '.': { /* field */
  513. luaY_field(ls, v);
  514. break;
  515. }
  516. case '[': { /* `[' exp1 `]' */
  517. expdesc key;
  518. luaK_exp2anyreg(fs, v);
  519. luaY_index(ls, &key);
  520. luaK_indexed(fs, v, &key);
  521. break;
  522. }
  523. case ':': { /* `:' NAME funcargs */
  524. expdesc key;
  525. next(ls);
  526. checkname(ls, &key);
  527. luaK_self(fs, v, &key);
  528. funcargs(ls, v);
  529. break;
  530. }
  531. case '(': case TK_STRING: case '{': { /* funcargs */
  532. luaK_exp2nextreg(fs, v);
  533. funcargs(ls, v);
  534. break;
  535. }
  536. default: return;
  537. }
  538. }
  539. }
  540. static void simpleexp (LexState *ls, expdesc *v) {
  541. /* simpleexp -> NUMBER | STRING | NIL | constructor | FUNCTION body
  542. | primaryexp */
  543. switch (ls->t.token) {
  544. case TK_NUMBER: {
  545. init_exp(v, VK, luaK_numberK(ls->fs, ls->t.seminfo.r));
  546. next(ls); /* must use `seminfo' before `next' */
  547. break;
  548. }
  549. case TK_STRING: {
  550. codestring(ls, v, ls->t.seminfo.ts);
  551. next(ls); /* must use `seminfo' before `next' */
  552. break;
  553. }
  554. case TK_NIL: {
  555. init_exp(v, VNIL, 0);
  556. next(ls);
  557. break;
  558. }
  559. case TK_TRUE: {
  560. init_exp(v, VTRUE, 0);
  561. next(ls);
  562. break;
  563. }
  564. case TK_FALSE: {
  565. init_exp(v, VFALSE, 0);
  566. next(ls);
  567. break;
  568. }
  569. case '{': { /* constructor */
  570. constructor(ls, v);
  571. break;
  572. }
  573. case TK_FUNCTION: {
  574. next(ls);
  575. body(ls, v, 0, ls->linenumber);
  576. break;
  577. }
  578. default: {
  579. primaryexp(ls, v);
  580. break;
  581. }
  582. }
  583. }
  584. static UnOpr getunopr (int op) {
  585. switch (op) {
  586. case TK_NOT: return OPR_NOT;
  587. case '-': return OPR_MINUS;
  588. default: return OPR_NOUNOPR;
  589. }
  590. }
  591. static BinOpr getbinopr (int op) {
  592. switch (op) {
  593. case '+': return OPR_ADD;
  594. case '-': return OPR_SUB;
  595. case '*': return OPR_MULT;
  596. case '/': return OPR_DIV;
  597. case '^': return OPR_POW;
  598. case TK_CONCAT: return OPR_CONCAT;
  599. case TK_NE: return OPR_NE;
  600. case TK_EQ: return OPR_EQ;
  601. case '<': return OPR_LT;
  602. case TK_LE: return OPR_LE;
  603. case '>': return OPR_GT;
  604. case TK_GE: return OPR_GE;
  605. case TK_AND: return OPR_AND;
  606. case TK_OR: return OPR_OR;
  607. default: return OPR_NOBINOPR;
  608. }
  609. }
  610. static const struct {
  611. lu_byte left; /* left priority for each binary operator */
  612. lu_byte right; /* right priority */
  613. } priority[] = { /* ORDER OPR */
  614. {6, 6}, {6, 6}, {7, 7}, {7, 7}, /* arithmetic */
  615. {10, 9}, {5, 4}, /* power and concat (right associative) */
  616. {3, 3}, {3, 3}, /* equality */
  617. {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
  618. {2, 2}, {1, 1} /* logical (and/or) */
  619. };
  620. #define UNARY_PRIORITY 8 /* priority for unary operators */
  621. /*
  622. ** subexpr -> (simplexep | unop subexpr) { binop subexpr }
  623. ** where `binop' is any binary operator with a priority higher than `limit'
  624. */
  625. static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
  626. BinOpr op;
  627. UnOpr uop = getunopr(ls->t.token);
  628. if (uop != OPR_NOUNOPR) {
  629. next(ls);
  630. subexpr(ls, v, UNARY_PRIORITY);
  631. luaK_prefix(ls->fs, uop, v);
  632. }
  633. else simpleexp(ls, v);
  634. /* expand while operators have priorities higher than `limit' */
  635. op = getbinopr(ls->t.token);
  636. while (op != OPR_NOBINOPR && cast(int, priority[op].left) > limit) {
  637. expdesc v2;
  638. BinOpr nextop;
  639. next(ls);
  640. luaK_infix(ls->fs, op, v);
  641. /* read sub-expression with higher priority */
  642. nextop = subexpr(ls, &v2, cast(int, priority[op].right));
  643. luaK_posfix(ls->fs, op, v, &v2);
  644. op = nextop;
  645. }
  646. return op; /* return first untreated operator */
  647. }
  648. static void expr (LexState *ls, expdesc *v) {
  649. subexpr(ls, v, -1);
  650. }
  651. /* }==================================================================== */
  652. /*
  653. ** {======================================================================
  654. ** Rules for Statements
  655. ** =======================================================================
  656. */
  657. static int block_follow (int token) {
  658. switch (token) {
  659. case TK_ELSE: case TK_ELSEIF: case TK_END:
  660. case TK_UNTIL: case TK_EOS:
  661. return 1;
  662. default: return 0;
  663. }
  664. }
  665. static void block (LexState *ls) {
  666. /* block -> chunk */
  667. FuncState *fs = ls->fs;
  668. BlockCnt bl;
  669. enterblock(fs, &bl, 0);
  670. chunk(ls);
  671. lua_assert(bl.breaklist == NO_JUMP);
  672. leaveblock(fs);
  673. }
  674. /*
  675. ** structure to chain all variables in the left-hand side of an
  676. ** assignment
  677. */
  678. struct LHS_assign {
  679. struct LHS_assign *prev;
  680. expdesc v; /* variable (global, local, upvalue, or indexed) */
  681. };
  682. /*
  683. ** check whether, in an assignment to a local variable, the local variable
  684. ** is needed in a previous assignment (to a table). If so, save original
  685. ** local value in a safe place and use this safe copy in the previous
  686. ** assignment.
  687. */
  688. static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
  689. FuncState *fs = ls->fs;
  690. int extra = fs->freereg; /* eventual position to save local variable */
  691. int conflict = 0;
  692. for (; lh; lh = lh->prev) {
  693. if (lh->v.k == VINDEXED) {
  694. if (lh->v.info == v->info) { /* conflict? */
  695. conflict = 1;
  696. lh->v.info = extra; /* previous assignment will use safe copy */
  697. }
  698. if (lh->v.aux == v->info) { /* conflict? */
  699. conflict = 1;
  700. lh->v.aux = extra; /* previous assignment will use safe copy */
  701. }
  702. }
  703. }
  704. if (conflict) {
  705. luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
  706. luaK_reserveregs(fs, 1);
  707. }
  708. }
  709. static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  710. expdesc e;
  711. check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
  712. "syntax error");
  713. if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
  714. struct LHS_assign nv;
  715. nv.prev = lh;
  716. primaryexp(ls, &nv.v);
  717. if (nv.v.k == VLOCAL)
  718. check_conflict(ls, lh, &nv.v);
  719. assignment(ls, &nv, nvars+1);
  720. }
  721. else { /* assignment -> `=' explist1 */
  722. int nexps;
  723. check(ls, '=');
  724. nexps = explist1(ls, &e);
  725. if (nexps != nvars) {
  726. adjust_assign(ls, nvars, nexps, &e);
  727. if (nexps > nvars)
  728. ls->fs->freereg -= nexps - nvars; /* remove extra values */
  729. }
  730. else {
  731. luaK_setcallreturns(ls->fs, &e, 1); /* close last expression */
  732. luaK_storevar(ls->fs, &lh->v, &e);
  733. return; /* avoid default */
  734. }
  735. }
  736. init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
  737. luaK_storevar(ls->fs, &lh->v, &e);
  738. }
  739. static void cond (LexState *ls, expdesc *v) {
  740. /* cond -> exp */
  741. expr(ls, v); /* read condition */
  742. if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */
  743. luaK_goiftrue(ls->fs, v);
  744. luaK_patchtohere(ls->fs, v->t);
  745. }
  746. /*
  747. ** The while statement optimizes its code by coding the condition
  748. ** after its body (and thus avoiding one jump in the loop).
  749. */
  750. /*
  751. ** maximum size of expressions for optimizing `while' code
  752. */
  753. #ifndef MAXEXPWHILE
  754. #define MAXEXPWHILE 100
  755. #endif
  756. /*
  757. ** the call `luaK_goiffalse' may grow the size of an expression by
  758. ** at most this:
  759. */
  760. #define EXTRAEXP 5
  761. static void whilestat (LexState *ls, int line) {
  762. /* whilestat -> WHILE cond DO block END */
  763. Instruction codeexp[MAXEXPWHILE + EXTRAEXP];
  764. int lineexp = 0;
  765. int i;
  766. int sizeexp;
  767. FuncState *fs = ls->fs;
  768. int whileinit, blockinit, expinit;
  769. expdesc v;
  770. BlockCnt bl;
  771. next(ls); /* skip WHILE */
  772. whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
  773. expinit = luaK_getlabel(fs);
  774. expr(ls, &v); /* parse condition */
  775. if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
  776. lineexp = ls->linenumber;
  777. luaK_goiffalse(fs, &v);
  778. luaK_concat(fs, &v.f, fs->jpc);
  779. fs->jpc = NO_JUMP;
  780. sizeexp = fs->pc - expinit; /* size of expression code */
  781. if (sizeexp > MAXEXPWHILE)
  782. luaX_syntaxerror(ls, "`while' condition too complex");
  783. for (i = 0; i < sizeexp; i++) /* save `exp' code */
  784. codeexp[i] = fs->f->code[expinit + i];
  785. fs->pc = expinit; /* remove `exp' code */
  786. enterblock(fs, &bl, 1);
  787. check(ls, TK_DO);
  788. blockinit = luaK_getlabel(fs);
  789. block(ls);
  790. luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
  791. /* move `exp' back to code */
  792. if (v.t != NO_JUMP) v.t += fs->pc - expinit;
  793. if (v.f != NO_JUMP) v.f += fs->pc - expinit;
  794. for (i=0; i<sizeexp; i++)
  795. luaK_code(fs, codeexp[i], lineexp);
  796. check_match(ls, TK_END, TK_WHILE, line);
  797. leaveblock(fs);
  798. luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
  799. luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
  800. }
  801. static void repeatstat (LexState *ls, int line) {
  802. /* repeatstat -> REPEAT block UNTIL cond */
  803. FuncState *fs = ls->fs;
  804. int repeat_init = luaK_getlabel(fs);
  805. expdesc v;
  806. BlockCnt bl;
  807. enterblock(fs, &bl, 1);
  808. next(ls);
  809. block(ls);
  810. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  811. cond(ls, &v);
  812. luaK_patchlist(fs, v.f, repeat_init);
  813. leaveblock(fs);
  814. }
  815. static int exp1 (LexState *ls) {
  816. expdesc e;
  817. int k;
  818. expr(ls, &e);
  819. k = e.k;
  820. luaK_exp2nextreg(ls->fs, &e);
  821. return k;
  822. }
  823. static void fornum (LexState *ls, TString *varname, int line) {
  824. /* fornum -> NAME = exp1,exp1[,exp1] DO body */
  825. FuncState *fs = ls->fs;
  826. int prep, endfor;
  827. int base = fs->freereg;
  828. new_localvar(ls, varname, 0);
  829. new_localvarstr(ls, "(for limit)", 1);
  830. new_localvarstr(ls, "(for step)", 2);
  831. check(ls, '=');
  832. exp1(ls); /* initial value */
  833. check(ls, ',');
  834. exp1(ls); /* limit */
  835. if (testnext(ls, ','))
  836. exp1(ls); /* optional step */
  837. else { /* default step = 1 */
  838. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  839. luaK_reserveregs(fs, 1);
  840. }
  841. adjustlocalvars(ls, 3); /* scope for control variables */
  842. luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
  843. luaK_jump(fs);
  844. prep = luaK_getlabel(fs);
  845. check(ls, TK_DO);
  846. block(ls);
  847. luaK_patchtohere(fs, prep-1);
  848. endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
  849. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  850. luaK_patchlist(fs, endfor, prep);
  851. }
  852. static void forlist (LexState *ls, TString *indexname) {
  853. /* forlist -> NAME {,NAME} IN explist1 DO body */
  854. FuncState *fs = ls->fs;
  855. expdesc e;
  856. int line;
  857. int nvars = 0;
  858. int prep;
  859. int base = fs->freereg;
  860. new_localvarstr(ls, "(for generator)", nvars++);
  861. new_localvarstr(ls, "(for state)", nvars++);
  862. new_localvar(ls, indexname, nvars++);
  863. while (testnext(ls, ','))
  864. new_localvar(ls, str_checkname(ls), nvars++);
  865. check(ls, TK_IN);
  866. line = ls->linenumber;
  867. adjust_assign(ls, 3, explist1(ls, &e), &e);
  868. luaK_reserveregs(fs, nvars - 3); /* registers for other variables */
  869. luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
  870. adjustlocalvars(ls, nvars); /* scope for all variables */
  871. check(ls, TK_DO);
  872. prep = luaK_getlabel(fs);
  873. block(ls);
  874. luaK_patchtohere(fs, prep-1);
  875. removevars(fs->ls, fs->nactvar - nvars); /* deactivate locals for next op. */
  876. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
  877. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  878. luaK_patchlist(fs, luaK_jump(fs), prep);
  879. }
  880. static void forstat (LexState *ls, int line) {
  881. /* forstat -> fornum | forlist */
  882. FuncState *fs = ls->fs;
  883. TString *varname;
  884. BlockCnt bl;
  885. enterblock(fs, &bl, 1);
  886. next(ls); /* skip `for' */
  887. varname = str_checkname(ls); /* first variable name */
  888. switch (ls->t.token) {
  889. case '=': fornum(ls, varname, line); break;
  890. case ',': case TK_IN: forlist(ls, varname); break;
  891. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  892. }
  893. check_match(ls, TK_END, TK_FOR, line);
  894. leaveblock(fs);
  895. }
  896. static void test_then_block (LexState *ls, expdesc *v) {
  897. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  898. next(ls); /* skip IF or ELSEIF */
  899. cond(ls, v);
  900. check(ls, TK_THEN);
  901. block(ls); /* `then' part */
  902. }
  903. static void ifstat (LexState *ls, int line) {
  904. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  905. FuncState *fs = ls->fs;
  906. expdesc v;
  907. int escapelist = NO_JUMP;
  908. test_then_block(ls, &v); /* IF cond THEN block */
  909. while (ls->t.token == TK_ELSEIF) {
  910. luaK_concat(fs, &escapelist, luaK_jump(fs));
  911. luaK_patchtohere(fs, v.f);
  912. test_then_block(ls, &v); /* ELSEIF cond THEN block */
  913. }
  914. if (ls->t.token == TK_ELSE) {
  915. luaK_concat(fs, &escapelist, luaK_jump(fs));
  916. luaK_patchtohere(fs, v.f);
  917. next(ls); /* skip ELSE (after patch, for correct line info) */
  918. block(ls); /* `else' part */
  919. }
  920. else
  921. luaK_concat(fs, &escapelist, v.f);
  922. luaK_patchtohere(fs, escapelist);
  923. check_match(ls, TK_END, TK_IF, line);
  924. }
  925. static void localfunc (LexState *ls) {
  926. expdesc v, b;
  927. new_localvar(ls, str_checkname(ls), 0);
  928. init_exp(&v, VLOCAL, ls->fs->freereg++);
  929. adjustlocalvars(ls, 1);
  930. body(ls, &b, 0, ls->linenumber);
  931. luaK_storevar(ls->fs, &v, &b);
  932. }
  933. static void localstat (LexState *ls) {
  934. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  935. int nvars = 0;
  936. int nexps;
  937. expdesc e;
  938. do {
  939. new_localvar(ls, str_checkname(ls), nvars++);
  940. } while (testnext(ls, ','));
  941. if (testnext(ls, '='))
  942. nexps = explist1(ls, &e);
  943. else {
  944. e.k = VVOID;
  945. nexps = 0;
  946. }
  947. adjust_assign(ls, nvars, nexps, &e);
  948. adjustlocalvars(ls, nvars);
  949. }
  950. static int funcname (LexState *ls, expdesc *v) {
  951. /* funcname -> NAME {field} [`:' NAME] */
  952. int needself = 0;
  953. singlevar(ls, v, 1);
  954. while (ls->t.token == '.')
  955. luaY_field(ls, v);
  956. if (ls->t.token == ':') {
  957. needself = 1;
  958. luaY_field(ls, v);
  959. }
  960. return needself;
  961. }
  962. static void funcstat (LexState *ls, int line) {
  963. /* funcstat -> FUNCTION funcname body */
  964. int needself;
  965. expdesc v, b;
  966. next(ls); /* skip FUNCTION */
  967. needself = funcname(ls, &v);
  968. body(ls, &b, needself, line);
  969. luaK_storevar(ls->fs, &v, &b);
  970. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  971. }
  972. static void exprstat (LexState *ls) {
  973. /* stat -> func | assignment */
  974. FuncState *fs = ls->fs;
  975. struct LHS_assign v;
  976. primaryexp(ls, &v.v);
  977. if (v.v.k == VCALL) { /* stat -> func */
  978. luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
  979. }
  980. else { /* stat -> assignment */
  981. v.prev = NULL;
  982. assignment(ls, &v, 1);
  983. }
  984. }
  985. static void retstat (LexState *ls) {
  986. /* stat -> RETURN explist */
  987. FuncState *fs = ls->fs;
  988. expdesc e;
  989. int first, nret; /* registers with returned values */
  990. next(ls); /* skip RETURN */
  991. if (block_follow(ls->t.token) || ls->t.token == ';')
  992. first = nret = 0; /* return no values */
  993. else {
  994. nret = explist1(ls, &e); /* optional return values */
  995. if (e.k == VCALL) {
  996. luaK_setcallreturns(fs, &e, LUA_MULTRET);
  997. if (nret == 1) { /* tail call? */
  998. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  999. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1000. }
  1001. first = fs->nactvar;
  1002. nret = LUA_MULTRET; /* return all values */
  1003. }
  1004. else {
  1005. if (nret == 1) /* only one single value? */
  1006. first = luaK_exp2anyreg(fs, &e);
  1007. else {
  1008. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1009. first = fs->nactvar; /* return all `active' values */
  1010. lua_assert(nret == fs->freereg - first);
  1011. }
  1012. }
  1013. }
  1014. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1015. }
  1016. static void breakstat (LexState *ls) {
  1017. /* stat -> BREAK [NAME] */
  1018. FuncState *fs = ls->fs;
  1019. BlockCnt *bl = fs->bl;
  1020. int upval = 0;
  1021. next(ls); /* skip BREAK */
  1022. while (bl && !bl->isbreakable) {
  1023. upval |= bl->upval;
  1024. bl = bl->previous;
  1025. }
  1026. if (!bl)
  1027. luaX_syntaxerror(ls, "no loop to break");
  1028. if (upval)
  1029. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1030. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1031. }
  1032. static int statement (LexState *ls) {
  1033. int line = ls->linenumber; /* may be needed for error messages */
  1034. switch (ls->t.token) {
  1035. case TK_IF: { /* stat -> ifstat */
  1036. ifstat(ls, line);
  1037. return 0;
  1038. }
  1039. case TK_WHILE: { /* stat -> whilestat */
  1040. whilestat(ls, line);
  1041. return 0;
  1042. }
  1043. case TK_DO: { /* stat -> DO block END */
  1044. next(ls); /* skip DO */
  1045. block(ls);
  1046. check_match(ls, TK_END, TK_DO, line);
  1047. return 0;
  1048. }
  1049. case TK_FOR: { /* stat -> forstat */
  1050. forstat(ls, line);
  1051. return 0;
  1052. }
  1053. case TK_REPEAT: { /* stat -> repeatstat */
  1054. repeatstat(ls, line);
  1055. return 0;
  1056. }
  1057. case TK_FUNCTION: {
  1058. funcstat(ls, line); /* stat -> funcstat */
  1059. return 0;
  1060. }
  1061. case TK_LOCAL: { /* stat -> localstat */
  1062. next(ls); /* skip LOCAL */
  1063. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1064. localfunc(ls);
  1065. else
  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; next(ls); break;
  1091. case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
  1092. default: luaX_syntaxerror(ls, "<name> or `...' expected");
  1093. }
  1094. } while (!dots && testnext(ls, ','));
  1095. }
  1096. code_params(ls, nparams, dots);
  1097. }
  1098. static void body (LexState *ls, expdesc *e, int needself, int line) {
  1099. /* body -> `(' parlist `)' chunk END */
  1100. FuncState new_fs;
  1101. open_func(ls, &new_fs);
  1102. new_fs.f->lineDefined = line;
  1103. check(ls, '(');
  1104. if (needself)
  1105. create_local(ls, "self");
  1106. parlist(ls);
  1107. check(ls, ')');
  1108. chunk(ls);
  1109. check_match(ls, TK_END, TK_FUNCTION, line);
  1110. close_func(ls);
  1111. pushclosure(ls, &new_fs, e);
  1112. }
  1113. /* }====================================================================== */
  1114. static void chunk (LexState *ls) {
  1115. /* chunk -> { stat [`;'] } */
  1116. int islast = 0;
  1117. while (!islast && !block_follow(ls->t.token)) {
  1118. islast = statement(ls);
  1119. testnext(ls, ';');
  1120. lua_assert(ls->fs->freereg >= ls->fs->nactvar);
  1121. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1122. }
  1123. }