lparser.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. ** $Id: lparser.c,v 1.190 2002/07/04 18:23:42 roberto Exp $
  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 = 1; /* register 0 is 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. lua_assert(G(L)->roottable == fs->h);
  266. G(L)->roottable = fs->h->next;
  267. luaH_free(L, fs->h);
  268. luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
  269. luaM_reallocvector(L, f->lineinfo, f->sizecode, fs->pc, int);
  270. f->sizecode = fs->pc;
  271. luaM_reallocvector(L, f->k, f->sizek, fs->nk, TObject);
  272. f->sizek = fs->nk;
  273. luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
  274. f->sizep = fs->np;
  275. luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
  276. f->sizelocvars = fs->nlocvars;
  277. lua_assert(luaG_checkcode(f));
  278. lua_assert(fs->bl == NULL);
  279. ls->fs = fs->prev;
  280. }
  281. Proto *luaY_parser (lua_State *L, ZIO *z) {
  282. struct LexState lexstate;
  283. struct FuncState funcstate;
  284. luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z)));
  285. open_func(&lexstate, &funcstate);
  286. next(&lexstate); /* read first token */
  287. chunk(&lexstate);
  288. check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
  289. close_func(&lexstate);
  290. lua_assert(funcstate.prev == NULL);
  291. lua_assert(funcstate.f->nupvalues == 0);
  292. return funcstate.f;
  293. }
  294. /*============================================================*/
  295. /* GRAMMAR RULES */
  296. /*============================================================*/
  297. static void luaY_field (LexState *ls, expdesc *v) {
  298. /* field -> ['.' | ':'] NAME */
  299. FuncState *fs = ls->fs;
  300. expdesc key;
  301. luaK_exp2anyreg(fs, v);
  302. next(ls); /* skip the dot or colon */
  303. checkname(ls, &key);
  304. luaK_indexed(fs, v, &key);
  305. }
  306. static void luaY_index (LexState *ls, expdesc *v) {
  307. /* index -> '[' expr ']' */
  308. next(ls); /* skip the '[' */
  309. expr(ls, v);
  310. luaK_exp2val(ls->fs, v);
  311. check(ls, ']');
  312. }
  313. static int explist1 (LexState *ls, expdesc *v) {
  314. /* explist1 -> expr { `,' expr } */
  315. int n = 1; /* at least one expression */
  316. expr(ls, v);
  317. while (testnext(ls, ',')) {
  318. luaK_exp2nextreg(ls->fs, v);
  319. expr(ls, v);
  320. n++;
  321. }
  322. return n;
  323. }
  324. static void funcargs (LexState *ls, expdesc *f) {
  325. FuncState *fs = ls->fs;
  326. expdesc args;
  327. int base, nparams;
  328. int line = ls->linenumber;
  329. switch (ls->t.token) {
  330. case '(': { /* funcargs -> `(' [ explist1 ] `)' */
  331. if (line != ls->lastline)
  332. luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
  333. next(ls);
  334. if (ls->t.token == ')') /* arg list is empty? */
  335. args.k = VVOID;
  336. else {
  337. explist1(ls, &args);
  338. luaK_setcallreturns(fs, &args, LUA_MULTRET);
  339. }
  340. check_match(ls, ')', '(', line);
  341. break;
  342. }
  343. case '{': { /* funcargs -> constructor */
  344. constructor(ls, &args);
  345. break;
  346. }
  347. case TK_STRING: { /* funcargs -> STRING */
  348. codestring(ls, &args, ls->t.seminfo.ts);
  349. next(ls); /* must use `seminfo' before `next' */
  350. break;
  351. }
  352. default: {
  353. luaX_syntaxerror(ls, "function arguments expected");
  354. return;
  355. }
  356. }
  357. lua_assert(f->k == VNONRELOC);
  358. base = f->info; /* base register for call */
  359. if (args.k == VCALL)
  360. nparams = LUA_MULTRET; /* open call */
  361. else {
  362. if (args.k != VVOID)
  363. luaK_exp2nextreg(fs, &args); /* close last argument */
  364. nparams = fs->freereg - (base+1);
  365. }
  366. init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
  367. fs->f->lineinfo[f->info] = line;
  368. fs->freereg = base+1; /* call remove function and arguments and leaves
  369. (unless changed) one result */
  370. }
  371. /*
  372. ** {======================================================================
  373. ** Rules for Constructors
  374. ** =======================================================================
  375. */
  376. struct ConsControl {
  377. expdesc v; /* last list item read */
  378. expdesc *t; /* table descriptor */
  379. int nh; /* total number of `record' elements */
  380. int na; /* total number of array elements */
  381. int tostore; /* number of array elements pending to be stored */
  382. };
  383. static void recfield (LexState *ls, struct ConsControl *cc) {
  384. /* recfield -> (NAME | `['exp1`]') = exp1 */
  385. FuncState *fs = ls->fs;
  386. int reg = ls->fs->freereg;
  387. expdesc key, val;
  388. if (ls->t.token == TK_NAME) {
  389. luaX_checklimit(ls, cc->nh, MAX_INT, "items in a constructor");
  390. cc->nh++;
  391. checkname(ls, &key);
  392. }
  393. else /* ls->t.token == '[' */
  394. luaY_index(ls, &key);
  395. check(ls, '=');
  396. luaK_exp2RK(fs, &key);
  397. expr(ls, &val);
  398. luaK_exp2anyreg(fs, &val);
  399. luaK_codeABC(fs, OP_SETTABLE, val.info, cc->t->info, luaK_exp2RK(fs, &key));
  400. fs->freereg = reg; /* free registers */
  401. }
  402. static void closelistfield (FuncState *fs, struct ConsControl *cc) {
  403. if (cc->v.k == VVOID) return; /* there is no list item */
  404. luaK_exp2nextreg(fs, &cc->v);
  405. cc->v.k = VVOID;
  406. if (cc->tostore == LFIELDS_PER_FLUSH) {
  407. luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1); /* flush */
  408. cc->tostore = 0; /* no more items pending */
  409. fs->freereg = cc->t->info + 1; /* free registers */
  410. }
  411. }
  412. static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
  413. if (cc->tostore == 0) return;
  414. if (cc->v.k == VCALL) {
  415. luaK_setcallreturns(fs, &cc->v, LUA_MULTRET);
  416. luaK_codeABx(fs, OP_SETLISTO, cc->t->info, cc->na-1);
  417. }
  418. else {
  419. if (cc->v.k != VVOID)
  420. luaK_exp2nextreg(fs, &cc->v);
  421. luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1);
  422. }
  423. fs->freereg = cc->t->info + 1; /* free registers */
  424. }
  425. static void listfield (LexState *ls, struct ConsControl *cc) {
  426. expr(ls, &cc->v);
  427. luaX_checklimit(ls, cc->na, MAXARG_Bx, "items in a constructor");
  428. cc->na++;
  429. cc->tostore++;
  430. }
  431. static void constructor (LexState *ls, expdesc *t) {
  432. /* constructor -> ?? */
  433. FuncState *fs = ls->fs;
  434. int line = ls->linenumber;
  435. int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  436. struct ConsControl cc;
  437. cc.na = cc.nh = cc.tostore = 0;
  438. cc.t = t;
  439. init_exp(t, VRELOCABLE, pc);
  440. init_exp(&cc.v, VVOID, 0); /* no value (yet) */
  441. luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
  442. check(ls, '{');
  443. do {
  444. lua_assert(cc.v.k == VVOID || cc.tostore > 0);
  445. testnext(ls, ';'); /* compatibility only */
  446. if (ls->t.token == '}') break;
  447. closelistfield(fs, &cc);
  448. switch(ls->t.token) {
  449. case TK_NAME: { /* may be listfields or recfields */
  450. lookahead(ls);
  451. if (ls->lookahead.token != '=') /* expression? */
  452. listfield(ls, &cc);
  453. else
  454. recfield(ls, &cc);
  455. break;
  456. }
  457. case '[': { /* constructor_item -> recfield */
  458. recfield(ls, &cc);
  459. break;
  460. }
  461. default: { /* constructor_part -> listfield */
  462. listfield(ls, &cc);
  463. break;
  464. }
  465. }
  466. } while (testnext(ls, ',') || testnext(ls, ';'));
  467. check_match(ls, '}', '{', line);
  468. lastlistfield(fs, &cc);
  469. if (cc.na > 0)
  470. SETARG_B(fs->f->code[pc], luaO_log2(cc.na-1)+2); /* set initial table size */
  471. SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */
  472. }
  473. /* }====================================================================== */
  474. /*
  475. ** {======================================================================
  476. ** Expression parsing
  477. ** =======================================================================
  478. */
  479. static void prefixexp (LexState *ls, expdesc *v) {
  480. /* prefixexp -> NAME | '(' expr ')' */
  481. switch (ls->t.token) {
  482. case '(': {
  483. int line = ls->linenumber;
  484. next(ls);
  485. expr(ls, v);
  486. check_match(ls, ')', '(', line);
  487. luaK_dischargevars(ls->fs, v);
  488. return;
  489. }
  490. case TK_NAME: {
  491. singlevar(ls, v, 1);
  492. return;
  493. }
  494. case '%': { /* for compatibility only */
  495. next(ls); /* skip `%' */
  496. singlevar(ls, v, 1);
  497. check_condition(ls, v->k == VUPVAL, "global upvalues are obsolete");
  498. return;
  499. }
  500. default: {
  501. luaX_syntaxerror(ls, "unexpected symbol");
  502. return;
  503. }
  504. }
  505. }
  506. static void primaryexp (LexState *ls, expdesc *v) {
  507. /* primaryexp ->
  508. prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
  509. FuncState *fs = ls->fs;
  510. prefixexp(ls, v);
  511. for (;;) {
  512. switch (ls->t.token) {
  513. case '.': { /* field */
  514. luaY_field(ls, v);
  515. break;
  516. }
  517. case '[': { /* `[' exp1 `]' */
  518. expdesc key;
  519. luaK_exp2anyreg(fs, v);
  520. luaY_index(ls, &key);
  521. luaK_indexed(fs, v, &key);
  522. break;
  523. }
  524. case ':': { /* `:' NAME funcargs */
  525. expdesc key;
  526. next(ls);
  527. checkname(ls, &key);
  528. luaK_self(fs, v, &key);
  529. funcargs(ls, v);
  530. break;
  531. }
  532. case '(': case TK_STRING: case '{': { /* funcargs */
  533. luaK_exp2nextreg(fs, v);
  534. funcargs(ls, v);
  535. break;
  536. }
  537. default: return;
  538. }
  539. }
  540. }
  541. static void simpleexp (LexState *ls, expdesc *v) {
  542. /* simpleexp -> NUMBER | STRING | NIL | constructor | FUNCTION body
  543. | primaryexp */
  544. switch (ls->t.token) {
  545. case TK_NUMBER: {
  546. init_exp(v, VK, luaK_numberK(ls->fs, ls->t.seminfo.r));
  547. next(ls); /* must use `seminfo' before `next' */
  548. break;
  549. }
  550. case TK_STRING: {
  551. codestring(ls, v, ls->t.seminfo.ts);
  552. next(ls); /* must use `seminfo' before `next' */
  553. break;
  554. }
  555. case TK_NIL: {
  556. init_exp(v, VNIL, 0);
  557. next(ls);
  558. break;
  559. }
  560. case TK_TRUE: {
  561. init_exp(v, VTRUE, 0);
  562. next(ls);
  563. break;
  564. }
  565. case TK_FALSE: {
  566. init_exp(v, VFALSE, 0);
  567. next(ls);
  568. break;
  569. }
  570. case '{': { /* constructor */
  571. constructor(ls, v);
  572. break;
  573. }
  574. case TK_FUNCTION: {
  575. next(ls);
  576. body(ls, v, 0, ls->linenumber);
  577. break;
  578. }
  579. default: {
  580. primaryexp(ls, v);
  581. break;
  582. }
  583. }
  584. }
  585. static UnOpr getunopr (int op) {
  586. switch (op) {
  587. case TK_NOT: return OPR_NOT;
  588. case '-': return OPR_MINUS;
  589. default: return OPR_NOUNOPR;
  590. }
  591. }
  592. static BinOpr getbinopr (int op) {
  593. switch (op) {
  594. case '+': return OPR_ADD;
  595. case '-': return OPR_SUB;
  596. case '*': return OPR_MULT;
  597. case '/': return OPR_DIV;
  598. case '^': return OPR_POW;
  599. case TK_CONCAT: return OPR_CONCAT;
  600. case TK_NE: return OPR_NE;
  601. case TK_EQ: return OPR_EQ;
  602. case '<': return OPR_LT;
  603. case TK_LE: return OPR_LE;
  604. case '>': return OPR_GT;
  605. case TK_GE: return OPR_GE;
  606. case TK_AND: return OPR_AND;
  607. case TK_OR: return OPR_OR;
  608. default: return OPR_NOBINOPR;
  609. }
  610. }
  611. static const struct {
  612. lu_byte left; /* left priority for each binary operator */
  613. lu_byte right; /* right priority */
  614. } priority[] = { /* ORDER OPR */
  615. {6, 6}, {6, 6}, {7, 7}, {7, 7}, /* arithmetic */
  616. {10, 9}, {5, 4}, /* power and concat (right associative) */
  617. {3, 3}, {3, 3}, /* equality */
  618. {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
  619. {2, 2}, {1, 1} /* logical (and/or) */
  620. };
  621. #define UNARY_PRIORITY 8 /* priority for unary operators */
  622. /*
  623. ** subexpr -> (simplexep | unop subexpr) { binop subexpr }
  624. ** where `binop' is any binary operator with a priority higher than `limit'
  625. */
  626. static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
  627. BinOpr op;
  628. UnOpr uop = getunopr(ls->t.token);
  629. if (uop != OPR_NOUNOPR) {
  630. next(ls);
  631. subexpr(ls, v, UNARY_PRIORITY);
  632. luaK_prefix(ls->fs, uop, v);
  633. }
  634. else simpleexp(ls, v);
  635. /* expand while operators have priorities higher than `limit' */
  636. op = getbinopr(ls->t.token);
  637. while (op != OPR_NOBINOPR && cast(int, priority[op].left) > limit) {
  638. expdesc v2;
  639. BinOpr nextop;
  640. next(ls);
  641. luaK_infix(ls->fs, op, v);
  642. /* read sub-expression with higher priority */
  643. nextop = subexpr(ls, &v2, cast(int, priority[op].right));
  644. luaK_posfix(ls->fs, op, v, &v2);
  645. op = nextop;
  646. }
  647. return op; /* return first untreated operator */
  648. }
  649. static void expr (LexState *ls, expdesc *v) {
  650. subexpr(ls, v, -1);
  651. }
  652. /* }==================================================================== */
  653. /*
  654. ** {======================================================================
  655. ** Rules for Statements
  656. ** =======================================================================
  657. */
  658. static int block_follow (int token) {
  659. switch (token) {
  660. case TK_ELSE: case TK_ELSEIF: case TK_END:
  661. case TK_UNTIL: case TK_EOS:
  662. return 1;
  663. default: return 0;
  664. }
  665. }
  666. static void block (LexState *ls) {
  667. /* block -> chunk */
  668. FuncState *fs = ls->fs;
  669. BlockCnt bl;
  670. enterblock(fs, &bl, 0);
  671. chunk(ls);
  672. lua_assert(bl.breaklist == NO_JUMP);
  673. leaveblock(fs);
  674. }
  675. /*
  676. ** structure to chain all variables in the left-hand side of an
  677. ** assignment
  678. */
  679. struct LHS_assign {
  680. struct LHS_assign *prev;
  681. expdesc v; /* variable (global, local, upvalue, or indexed) */
  682. };
  683. /*
  684. ** check whether, in an assignment to a local variable, the local variable
  685. ** is needed in a previous assignment (to a table). If so, save original
  686. ** local value in a safe place and use this safe copy in the previous
  687. ** assignment.
  688. */
  689. static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
  690. FuncState *fs = ls->fs;
  691. int extra = fs->freereg; /* eventual position to save local variable */
  692. int conflict = 0;
  693. for (; lh; lh = lh->prev) {
  694. if (lh->v.k == VINDEXED) {
  695. if (lh->v.info == v->info) { /* conflict? */
  696. conflict = 1;
  697. lh->v.info = extra; /* previous assignment will use safe copy */
  698. }
  699. if (lh->v.aux == v->info) { /* conflict? */
  700. conflict = 1;
  701. lh->v.aux = extra; /* previous assignment will use safe copy */
  702. }
  703. }
  704. }
  705. if (conflict) {
  706. luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
  707. luaK_reserveregs(fs, 1);
  708. }
  709. }
  710. static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  711. expdesc e;
  712. check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
  713. "syntax error");
  714. if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
  715. struct LHS_assign nv;
  716. nv.prev = lh;
  717. primaryexp(ls, &nv.v);
  718. if (nv.v.k == VLOCAL)
  719. check_conflict(ls, lh, &nv.v);
  720. assignment(ls, &nv, nvars+1);
  721. }
  722. else { /* assignment -> `=' explist1 */
  723. int nexps;
  724. check(ls, '=');
  725. nexps = explist1(ls, &e);
  726. if (nexps != nvars) {
  727. adjust_assign(ls, nvars, nexps, &e);
  728. if (nexps > nvars)
  729. ls->fs->freereg -= nexps - nvars; /* remove extra values */
  730. }
  731. else {
  732. luaK_setcallreturns(ls->fs, &e, 1); /* close last expression */
  733. luaK_storevar(ls->fs, &lh->v, &e);
  734. return; /* avoid default */
  735. }
  736. }
  737. init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
  738. luaK_storevar(ls->fs, &lh->v, &e);
  739. }
  740. static void cond (LexState *ls, expdesc *v) {
  741. /* cond -> exp */
  742. expr(ls, v); /* read condition */
  743. if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */
  744. luaK_goiftrue(ls->fs, v);
  745. luaK_patchtohere(ls->fs, v->t);
  746. }
  747. /*
  748. ** The while statement optimizes its code by coding the condition
  749. ** after its body (and thus avoiding one jump in the loop).
  750. */
  751. /*
  752. ** maximum size of expressions for optimizing `while' code
  753. */
  754. #ifndef MAXEXPWHILE
  755. #define MAXEXPWHILE 100
  756. #endif
  757. /*
  758. ** the call `luaK_goiffalse' may grow the size of an expression by
  759. ** at most this:
  760. */
  761. #define EXTRAEXP 5
  762. static void whilestat (LexState *ls, int line) {
  763. /* whilestat -> WHILE cond DO block END */
  764. Instruction codeexp[MAXEXPWHILE + EXTRAEXP];
  765. int lineexp = 0;
  766. int i;
  767. int sizeexp;
  768. FuncState *fs = ls->fs;
  769. int whileinit, blockinit, expinit;
  770. expdesc v;
  771. BlockCnt bl;
  772. next(ls); /* skip WHILE */
  773. whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
  774. expinit = luaK_getlabel(fs);
  775. expr(ls, &v); /* parse condition */
  776. if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
  777. lineexp = ls->linenumber;
  778. luaK_goiffalse(fs, &v);
  779. luaK_concat(fs, &v.f, fs->jpc);
  780. fs->jpc = NO_JUMP;
  781. sizeexp = fs->pc - expinit; /* size of expression code */
  782. if (sizeexp > MAXEXPWHILE)
  783. luaX_syntaxerror(ls, "`while' condition too complex");
  784. for (i = 0; i < sizeexp; i++) /* save `exp' code */
  785. codeexp[i] = fs->f->code[expinit + i];
  786. fs->pc = expinit; /* remove `exp' code */
  787. enterblock(fs, &bl, 1);
  788. check(ls, TK_DO);
  789. blockinit = luaK_getlabel(fs);
  790. block(ls);
  791. luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
  792. /* move `exp' back to code */
  793. if (v.t != NO_JUMP) v.t += fs->pc - expinit;
  794. if (v.f != NO_JUMP) v.f += fs->pc - expinit;
  795. for (i=0; i<sizeexp; i++)
  796. luaK_code(fs, codeexp[i], lineexp);
  797. check_match(ls, TK_END, TK_WHILE, line);
  798. leaveblock(fs);
  799. luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
  800. luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
  801. }
  802. static void repeatstat (LexState *ls, int line) {
  803. /* repeatstat -> REPEAT block UNTIL cond */
  804. FuncState *fs = ls->fs;
  805. int repeat_init = luaK_getlabel(fs);
  806. expdesc v;
  807. BlockCnt bl;
  808. enterblock(fs, &bl, 1);
  809. next(ls);
  810. block(ls);
  811. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  812. cond(ls, &v);
  813. luaK_patchlist(fs, v.f, repeat_init);
  814. leaveblock(fs);
  815. }
  816. static int exp1 (LexState *ls) {
  817. expdesc e;
  818. int k;
  819. expr(ls, &e);
  820. k = e.k;
  821. luaK_exp2nextreg(ls->fs, &e);
  822. return k;
  823. }
  824. static void fornum (LexState *ls, TString *varname, int line) {
  825. /* fornum -> NAME = exp1,exp1[,exp1] DO body */
  826. FuncState *fs = ls->fs;
  827. int prep, endfor;
  828. int base = fs->freereg;
  829. new_localvar(ls, varname, 0);
  830. new_localvarstr(ls, "(for limit)", 1);
  831. new_localvarstr(ls, "(for step)", 2);
  832. check(ls, '=');
  833. exp1(ls); /* initial value */
  834. check(ls, ',');
  835. exp1(ls); /* limit */
  836. if (testnext(ls, ','))
  837. exp1(ls); /* optional step */
  838. else { /* default step = 1 */
  839. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  840. luaK_reserveregs(fs, 1);
  841. }
  842. adjustlocalvars(ls, 3); /* scope for control variables */
  843. luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
  844. luaK_jump(fs);
  845. prep = luaK_getlabel(fs);
  846. check(ls, TK_DO);
  847. block(ls);
  848. luaK_patchtohere(fs, prep-1);
  849. endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
  850. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  851. luaK_patchlist(fs, endfor, prep);
  852. }
  853. static void forlist (LexState *ls, TString *indexname) {
  854. /* forlist -> NAME {,NAME} IN explist1 DO body */
  855. FuncState *fs = ls->fs;
  856. expdesc e;
  857. int line;
  858. int nvars = 0;
  859. int prep;
  860. int base = fs->freereg;
  861. new_localvarstr(ls, "(for generator)", nvars++);
  862. new_localvarstr(ls, "(for state)", nvars++);
  863. new_localvar(ls, indexname, nvars++);
  864. while (testnext(ls, ','))
  865. new_localvar(ls, str_checkname(ls), nvars++);
  866. check(ls, TK_IN);
  867. line = ls->linenumber;
  868. adjust_assign(ls, 3, explist1(ls, &e), &e);
  869. luaK_reserveregs(fs, nvars - 3); /* registers for other variables */
  870. luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
  871. adjustlocalvars(ls, nvars); /* scope for all variables */
  872. check(ls, TK_DO);
  873. prep = luaK_getlabel(fs);
  874. block(ls);
  875. luaK_patchtohere(fs, prep-1);
  876. removevars(fs->ls, fs->nactvar - nvars); /* deactivate locals for next op. */
  877. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
  878. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  879. luaK_patchlist(fs, luaK_jump(fs), prep);
  880. }
  881. static void forstat (LexState *ls, int line) {
  882. /* forstat -> fornum | forlist */
  883. FuncState *fs = ls->fs;
  884. TString *varname;
  885. BlockCnt bl;
  886. enterblock(fs, &bl, 1);
  887. next(ls); /* skip `for' */
  888. varname = str_checkname(ls); /* first variable name */
  889. switch (ls->t.token) {
  890. case '=': fornum(ls, varname, line); break;
  891. case ',': case TK_IN: forlist(ls, varname); break;
  892. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  893. }
  894. check_match(ls, TK_END, TK_FOR, line);
  895. leaveblock(fs);
  896. }
  897. static void test_then_block (LexState *ls, expdesc *v) {
  898. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  899. next(ls); /* skip IF or ELSEIF */
  900. cond(ls, v);
  901. check(ls, TK_THEN);
  902. block(ls); /* `then' part */
  903. }
  904. static void ifstat (LexState *ls, int line) {
  905. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  906. FuncState *fs = ls->fs;
  907. expdesc v;
  908. int escapelist = NO_JUMP;
  909. test_then_block(ls, &v); /* IF cond THEN block */
  910. while (ls->t.token == TK_ELSEIF) {
  911. luaK_concat(fs, &escapelist, luaK_jump(fs));
  912. luaK_patchtohere(fs, v.f);
  913. test_then_block(ls, &v); /* ELSEIF cond THEN block */
  914. }
  915. if (ls->t.token == TK_ELSE) {
  916. luaK_concat(fs, &escapelist, luaK_jump(fs));
  917. luaK_patchtohere(fs, v.f);
  918. next(ls); /* skip ELSE (after patch, for correct line info) */
  919. block(ls); /* `else' part */
  920. }
  921. else
  922. luaK_concat(fs, &escapelist, v.f);
  923. luaK_patchtohere(fs, escapelist);
  924. check_match(ls, TK_END, TK_IF, line);
  925. }
  926. static void localfunc (LexState *ls) {
  927. expdesc v, b;
  928. new_localvar(ls, str_checkname(ls), 0);
  929. init_exp(&v, VLOCAL, ls->fs->freereg++);
  930. adjustlocalvars(ls, 1);
  931. body(ls, &b, 0, ls->linenumber);
  932. luaK_storevar(ls->fs, &v, &b);
  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. new_localvar(ls, str_checkname(ls), nvars++);
  941. } while (testnext(ls, ','));
  942. if (testnext(ls, '='))
  943. nexps = explist1(ls, &e);
  944. else {
  945. e.k = VVOID;
  946. nexps = 0;
  947. }
  948. adjust_assign(ls, nvars, nexps, &e);
  949. adjustlocalvars(ls, nvars);
  950. }
  951. static int funcname (LexState *ls, expdesc *v) {
  952. /* funcname -> NAME {field} [`:' NAME] */
  953. int needself = 0;
  954. singlevar(ls, v, 1);
  955. while (ls->t.token == '.')
  956. luaY_field(ls, v);
  957. if (ls->t.token == ':') {
  958. needself = 1;
  959. luaY_field(ls, v);
  960. }
  961. return needself;
  962. }
  963. static void funcstat (LexState *ls, int line) {
  964. /* funcstat -> FUNCTION funcname body */
  965. int needself;
  966. expdesc v, b;
  967. next(ls); /* skip FUNCTION */
  968. needself = funcname(ls, &v);
  969. body(ls, &b, needself, line);
  970. luaK_storevar(ls->fs, &v, &b);
  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. }