lparser.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. ** $Id: lparser.c,v 1.193 2002/08/22 19:51: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. 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 (testnext(ls, ',')) {
  315. luaK_exp2nextreg(ls->fs, v);
  316. expr(ls, v);
  317. n++;
  318. }
  319. return n;
  320. }
  321. static void funcargs (LexState *ls, expdesc *f) {
  322. FuncState *fs = ls->fs;
  323. expdesc args;
  324. int base, nparams;
  325. int line = ls->linenumber;
  326. switch (ls->t.token) {
  327. case '(': { /* funcargs -> `(' [ explist1 ] `)' */
  328. if (line != ls->lastline)
  329. luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
  330. next(ls);
  331. if (ls->t.token == ')') /* arg list is empty? */
  332. args.k = VVOID;
  333. else {
  334. explist1(ls, &args);
  335. luaK_setcallreturns(fs, &args, LUA_MULTRET);
  336. }
  337. check_match(ls, ')', '(', line);
  338. break;
  339. }
  340. case '{': { /* funcargs -> constructor */
  341. constructor(ls, &args);
  342. break;
  343. }
  344. case TK_STRING: { /* funcargs -> STRING */
  345. codestring(ls, &args, ls->t.seminfo.ts);
  346. next(ls); /* must use `seminfo' before `next' */
  347. break;
  348. }
  349. default: {
  350. luaX_syntaxerror(ls, "function arguments expected");
  351. return;
  352. }
  353. }
  354. lua_assert(f->k == VNONRELOC);
  355. base = f->info; /* base register for call */
  356. if (args.k == VCALL)
  357. nparams = LUA_MULTRET; /* open call */
  358. else {
  359. if (args.k != VVOID)
  360. luaK_exp2nextreg(fs, &args); /* close last argument */
  361. nparams = fs->freereg - (base+1);
  362. }
  363. init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
  364. fs->f->lineinfo[f->info] = line;
  365. fs->freereg = base+1; /* call remove function and arguments and leaves
  366. (unless changed) one result */
  367. }
  368. /*
  369. ** {======================================================================
  370. ** Rules for Constructors
  371. ** =======================================================================
  372. */
  373. struct ConsControl {
  374. expdesc v; /* last list item read */
  375. expdesc *t; /* table descriptor */
  376. int nh; /* total number of `record' elements */
  377. int na; /* total number of array elements */
  378. int tostore; /* number of array elements pending to be stored */
  379. };
  380. static void recfield (LexState *ls, struct ConsControl *cc) {
  381. /* recfield -> (NAME | `['exp1`]') = exp1 */
  382. FuncState *fs = ls->fs;
  383. int reg = ls->fs->freereg;
  384. expdesc key, val;
  385. if (ls->t.token == TK_NAME) {
  386. luaX_checklimit(ls, cc->nh, MAX_INT, "items in a constructor");
  387. cc->nh++;
  388. checkname(ls, &key);
  389. }
  390. else /* ls->t.token == '[' */
  391. luaY_index(ls, &key);
  392. check(ls, '=');
  393. luaK_exp2RK(fs, &key);
  394. expr(ls, &val);
  395. luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key),
  396. luaK_exp2RK(fs, &val));
  397. fs->freereg = reg; /* free registers */
  398. }
  399. static void closelistfield (FuncState *fs, struct ConsControl *cc) {
  400. if (cc->v.k == VVOID) return; /* there is no list item */
  401. luaK_exp2nextreg(fs, &cc->v);
  402. cc->v.k = VVOID;
  403. if (cc->tostore == LFIELDS_PER_FLUSH) {
  404. luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1); /* flush */
  405. cc->tostore = 0; /* no more items pending */
  406. fs->freereg = cc->t->info + 1; /* free registers */
  407. }
  408. }
  409. static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
  410. if (cc->tostore == 0) return;
  411. if (cc->v.k == VCALL) {
  412. luaK_setcallreturns(fs, &cc->v, LUA_MULTRET);
  413. luaK_codeABx(fs, OP_SETLISTO, cc->t->info, cc->na-1);
  414. }
  415. else {
  416. if (cc->v.k != VVOID)
  417. luaK_exp2nextreg(fs, &cc->v);
  418. luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1);
  419. }
  420. fs->freereg = cc->t->info + 1; /* free registers */
  421. }
  422. static void listfield (LexState *ls, struct ConsControl *cc) {
  423. expr(ls, &cc->v);
  424. luaX_checklimit(ls, cc->na, MAXARG_Bx, "items in a constructor");
  425. cc->na++;
  426. cc->tostore++;
  427. }
  428. static void constructor (LexState *ls, expdesc *t) {
  429. /* constructor -> ?? */
  430. FuncState *fs = ls->fs;
  431. int line = ls->linenumber;
  432. int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  433. struct ConsControl cc;
  434. cc.na = cc.nh = cc.tostore = 0;
  435. cc.t = t;
  436. init_exp(t, VRELOCABLE, pc);
  437. init_exp(&cc.v, VVOID, 0); /* no value (yet) */
  438. luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
  439. check(ls, '{');
  440. do {
  441. lua_assert(cc.v.k == VVOID || cc.tostore > 0);
  442. testnext(ls, ';'); /* compatibility only */
  443. if (ls->t.token == '}') break;
  444. closelistfield(fs, &cc);
  445. switch(ls->t.token) {
  446. case TK_NAME: { /* may be listfields or recfields */
  447. lookahead(ls);
  448. if (ls->lookahead.token != '=') /* expression? */
  449. listfield(ls, &cc);
  450. else
  451. recfield(ls, &cc);
  452. break;
  453. }
  454. case '[': { /* constructor_item -> recfield */
  455. recfield(ls, &cc);
  456. break;
  457. }
  458. default: { /* constructor_part -> listfield */
  459. listfield(ls, &cc);
  460. break;
  461. }
  462. }
  463. } while (testnext(ls, ',') || testnext(ls, ';'));
  464. check_match(ls, '}', '{', line);
  465. lastlistfield(fs, &cc);
  466. if (cc.na > 0)
  467. SETARG_B(fs->f->code[pc], luaO_log2(cc.na-1)+2); /* set initial table size */
  468. SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */
  469. }
  470. /* }====================================================================== */
  471. /*
  472. ** {======================================================================
  473. ** Expression parsing
  474. ** =======================================================================
  475. */
  476. static void prefixexp (LexState *ls, expdesc *v) {
  477. /* prefixexp -> NAME | '(' expr ')' */
  478. switch (ls->t.token) {
  479. case '(': {
  480. int line = ls->linenumber;
  481. next(ls);
  482. expr(ls, v);
  483. check_match(ls, ')', '(', line);
  484. luaK_dischargevars(ls->fs, v);
  485. return;
  486. }
  487. case TK_NAME: {
  488. singlevar(ls, v, 1);
  489. return;
  490. }
  491. case '%': { /* for compatibility only */
  492. next(ls); /* skip `%' */
  493. singlevar(ls, v, 1);
  494. check_condition(ls, v->k == VUPVAL, "global upvalues are obsolete");
  495. return;
  496. }
  497. default: {
  498. luaX_syntaxerror(ls, "unexpected symbol");
  499. return;
  500. }
  501. }
  502. }
  503. static void primaryexp (LexState *ls, expdesc *v) {
  504. /* primaryexp ->
  505. prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
  506. FuncState *fs = ls->fs;
  507. prefixexp(ls, v);
  508. for (;;) {
  509. switch (ls->t.token) {
  510. case '.': { /* field */
  511. luaY_field(ls, v);
  512. break;
  513. }
  514. case '[': { /* `[' exp1 `]' */
  515. expdesc key;
  516. luaK_exp2anyreg(fs, v);
  517. luaY_index(ls, &key);
  518. luaK_indexed(fs, v, &key);
  519. break;
  520. }
  521. case ':': { /* `:' NAME funcargs */
  522. expdesc key;
  523. next(ls);
  524. checkname(ls, &key);
  525. luaK_self(fs, v, &key);
  526. funcargs(ls, v);
  527. break;
  528. }
  529. case '(': case TK_STRING: case '{': { /* funcargs */
  530. luaK_exp2nextreg(fs, v);
  531. funcargs(ls, v);
  532. break;
  533. }
  534. default: return;
  535. }
  536. }
  537. }
  538. static void simpleexp (LexState *ls, expdesc *v) {
  539. /* simpleexp -> NUMBER | STRING | NIL | constructor | FUNCTION body
  540. | primaryexp */
  541. switch (ls->t.token) {
  542. case TK_NUMBER: {
  543. init_exp(v, VK, luaK_numberK(ls->fs, ls->t.seminfo.r));
  544. next(ls); /* must use `seminfo' before `next' */
  545. break;
  546. }
  547. case TK_STRING: {
  548. codestring(ls, v, ls->t.seminfo.ts);
  549. next(ls); /* must use `seminfo' before `next' */
  550. break;
  551. }
  552. case TK_NIL: {
  553. init_exp(v, VNIL, 0);
  554. next(ls);
  555. break;
  556. }
  557. case TK_TRUE: {
  558. init_exp(v, VTRUE, 0);
  559. next(ls);
  560. break;
  561. }
  562. case TK_FALSE: {
  563. init_exp(v, VFALSE, 0);
  564. next(ls);
  565. break;
  566. }
  567. case '{': { /* constructor */
  568. constructor(ls, v);
  569. break;
  570. }
  571. case TK_FUNCTION: {
  572. next(ls);
  573. body(ls, v, 0, ls->linenumber);
  574. break;
  575. }
  576. default: {
  577. primaryexp(ls, v);
  578. break;
  579. }
  580. }
  581. }
  582. static UnOpr getunopr (int op) {
  583. switch (op) {
  584. case TK_NOT: return OPR_NOT;
  585. case '-': return OPR_MINUS;
  586. default: return OPR_NOUNOPR;
  587. }
  588. }
  589. static BinOpr getbinopr (int op) {
  590. switch (op) {
  591. case '+': return OPR_ADD;
  592. case '-': return OPR_SUB;
  593. case '*': return OPR_MULT;
  594. case '/': return OPR_DIV;
  595. case '^': return OPR_POW;
  596. case TK_CONCAT: return OPR_CONCAT;
  597. case TK_NE: return OPR_NE;
  598. case TK_EQ: return OPR_EQ;
  599. case '<': return OPR_LT;
  600. case TK_LE: return OPR_LE;
  601. case '>': return OPR_GT;
  602. case TK_GE: return OPR_GE;
  603. case TK_AND: return OPR_AND;
  604. case TK_OR: return OPR_OR;
  605. default: return OPR_NOBINOPR;
  606. }
  607. }
  608. static const struct {
  609. lu_byte left; /* left priority for each binary operator */
  610. lu_byte right; /* right priority */
  611. } priority[] = { /* ORDER OPR */
  612. {6, 6}, {6, 6}, {7, 7}, {7, 7}, /* arithmetic */
  613. {10, 9}, {5, 4}, /* power and concat (right associative) */
  614. {3, 3}, {3, 3}, /* equality */
  615. {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
  616. {2, 2}, {1, 1} /* logical (and/or) */
  617. };
  618. #define UNARY_PRIORITY 8 /* priority for unary operators */
  619. /*
  620. ** subexpr -> (simplexep | unop subexpr) { binop subexpr }
  621. ** where `binop' is any binary operator with a priority higher than `limit'
  622. */
  623. static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
  624. BinOpr op;
  625. UnOpr uop = getunopr(ls->t.token);
  626. if (uop != OPR_NOUNOPR) {
  627. next(ls);
  628. subexpr(ls, v, UNARY_PRIORITY);
  629. luaK_prefix(ls->fs, uop, v);
  630. }
  631. else simpleexp(ls, v);
  632. /* expand while operators have priorities higher than `limit' */
  633. op = getbinopr(ls->t.token);
  634. while (op != OPR_NOBINOPR && cast(int, priority[op].left) > limit) {
  635. expdesc v2;
  636. BinOpr nextop;
  637. next(ls);
  638. luaK_infix(ls->fs, op, v);
  639. /* read sub-expression with higher priority */
  640. nextop = subexpr(ls, &v2, cast(int, priority[op].right));
  641. luaK_posfix(ls->fs, op, v, &v2);
  642. op = nextop;
  643. }
  644. return op; /* return first untreated operator */
  645. }
  646. static void expr (LexState *ls, expdesc *v) {
  647. subexpr(ls, v, -1);
  648. }
  649. /* }==================================================================== */
  650. /*
  651. ** {======================================================================
  652. ** Rules for Statements
  653. ** =======================================================================
  654. */
  655. static int block_follow (int token) {
  656. switch (token) {
  657. case TK_ELSE: case TK_ELSEIF: case TK_END:
  658. case TK_UNTIL: case TK_EOS:
  659. return 1;
  660. default: return 0;
  661. }
  662. }
  663. static void block (LexState *ls) {
  664. /* block -> chunk */
  665. FuncState *fs = ls->fs;
  666. BlockCnt bl;
  667. enterblock(fs, &bl, 0);
  668. chunk(ls);
  669. lua_assert(bl.breaklist == NO_JUMP);
  670. leaveblock(fs);
  671. }
  672. /*
  673. ** structure to chain all variables in the left-hand side of an
  674. ** assignment
  675. */
  676. struct LHS_assign {
  677. struct LHS_assign *prev;
  678. expdesc v; /* variable (global, local, upvalue, or indexed) */
  679. };
  680. /*
  681. ** check whether, in an assignment to a local variable, the local variable
  682. ** is needed in a previous assignment (to a table). If so, save original
  683. ** local value in a safe place and use this safe copy in the previous
  684. ** assignment.
  685. */
  686. static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
  687. FuncState *fs = ls->fs;
  688. int extra = fs->freereg; /* eventual position to save local variable */
  689. int conflict = 0;
  690. for (; lh; lh = lh->prev) {
  691. if (lh->v.k == VINDEXED) {
  692. if (lh->v.info == v->info) { /* conflict? */
  693. conflict = 1;
  694. lh->v.info = extra; /* previous assignment will use safe copy */
  695. }
  696. if (lh->v.aux == v->info) { /* conflict? */
  697. conflict = 1;
  698. lh->v.aux = extra; /* previous assignment will use safe copy */
  699. }
  700. }
  701. }
  702. if (conflict) {
  703. luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
  704. luaK_reserveregs(fs, 1);
  705. }
  706. }
  707. static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  708. expdesc e;
  709. check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
  710. "syntax error");
  711. if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
  712. struct LHS_assign nv;
  713. nv.prev = lh;
  714. primaryexp(ls, &nv.v);
  715. if (nv.v.k == VLOCAL)
  716. check_conflict(ls, lh, &nv.v);
  717. assignment(ls, &nv, nvars+1);
  718. }
  719. else { /* assignment -> `=' explist1 */
  720. int nexps;
  721. check(ls, '=');
  722. nexps = explist1(ls, &e);
  723. if (nexps != nvars) {
  724. adjust_assign(ls, nvars, nexps, &e);
  725. if (nexps > nvars)
  726. ls->fs->freereg -= nexps - nvars; /* remove extra values */
  727. }
  728. else {
  729. luaK_setcallreturns(ls->fs, &e, 1); /* close last expression */
  730. luaK_storevar(ls->fs, &lh->v, &e);
  731. return; /* avoid default */
  732. }
  733. }
  734. init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
  735. luaK_storevar(ls->fs, &lh->v, &e);
  736. }
  737. static void cond (LexState *ls, expdesc *v) {
  738. /* cond -> exp */
  739. expr(ls, v); /* read condition */
  740. if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */
  741. luaK_goiftrue(ls->fs, v);
  742. luaK_patchtohere(ls->fs, v->t);
  743. }
  744. /*
  745. ** The while statement optimizes its code by coding the condition
  746. ** after its body (and thus avoiding one jump in the loop).
  747. */
  748. /*
  749. ** maximum size of expressions for optimizing `while' code
  750. */
  751. #ifndef MAXEXPWHILE
  752. #define MAXEXPWHILE 100
  753. #endif
  754. /*
  755. ** the call `luaK_goiffalse' may grow the size of an expression by
  756. ** at most this:
  757. */
  758. #define EXTRAEXP 5
  759. static void whilestat (LexState *ls, int line) {
  760. /* whilestat -> WHILE cond DO block END */
  761. Instruction codeexp[MAXEXPWHILE + EXTRAEXP];
  762. int lineexp = 0;
  763. int i;
  764. int sizeexp;
  765. FuncState *fs = ls->fs;
  766. int whileinit, blockinit, expinit;
  767. expdesc v;
  768. BlockCnt bl;
  769. next(ls); /* skip WHILE */
  770. whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
  771. expinit = luaK_getlabel(fs);
  772. expr(ls, &v); /* parse condition */
  773. if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
  774. lineexp = ls->linenumber;
  775. luaK_goiffalse(fs, &v);
  776. luaK_concat(fs, &v.f, fs->jpc);
  777. fs->jpc = NO_JUMP;
  778. sizeexp = fs->pc - expinit; /* size of expression code */
  779. if (sizeexp > MAXEXPWHILE)
  780. luaX_syntaxerror(ls, "`while' condition too complex");
  781. for (i = 0; i < sizeexp; i++) /* save `exp' code */
  782. codeexp[i] = fs->f->code[expinit + i];
  783. fs->pc = expinit; /* remove `exp' code */
  784. enterblock(fs, &bl, 1);
  785. check(ls, TK_DO);
  786. blockinit = luaK_getlabel(fs);
  787. block(ls);
  788. luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
  789. /* move `exp' back to code */
  790. if (v.t != NO_JUMP) v.t += fs->pc - expinit;
  791. if (v.f != NO_JUMP) v.f += fs->pc - expinit;
  792. for (i=0; i<sizeexp; i++)
  793. luaK_code(fs, codeexp[i], lineexp);
  794. check_match(ls, TK_END, TK_WHILE, line);
  795. leaveblock(fs);
  796. luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
  797. luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
  798. }
  799. static void repeatstat (LexState *ls, int line) {
  800. /* repeatstat -> REPEAT block UNTIL cond */
  801. FuncState *fs = ls->fs;
  802. int repeat_init = luaK_getlabel(fs);
  803. expdesc v;
  804. BlockCnt bl;
  805. enterblock(fs, &bl, 1);
  806. next(ls);
  807. block(ls);
  808. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  809. cond(ls, &v);
  810. luaK_patchlist(fs, v.f, repeat_init);
  811. leaveblock(fs);
  812. }
  813. static int exp1 (LexState *ls) {
  814. expdesc e;
  815. int k;
  816. expr(ls, &e);
  817. k = e.k;
  818. luaK_exp2nextreg(ls->fs, &e);
  819. return k;
  820. }
  821. static void fornum (LexState *ls, TString *varname, int line) {
  822. /* fornum -> NAME = exp1,exp1[,exp1] DO body */
  823. FuncState *fs = ls->fs;
  824. int prep, endfor;
  825. int base = fs->freereg;
  826. new_localvar(ls, varname, 0);
  827. new_localvarstr(ls, "(for limit)", 1);
  828. new_localvarstr(ls, "(for step)", 2);
  829. check(ls, '=');
  830. exp1(ls); /* initial value */
  831. check(ls, ',');
  832. exp1(ls); /* limit */
  833. if (testnext(ls, ','))
  834. exp1(ls); /* optional step */
  835. else { /* default step = 1 */
  836. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  837. luaK_reserveregs(fs, 1);
  838. }
  839. adjustlocalvars(ls, 3); /* scope for control variables */
  840. luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
  841. luaK_jump(fs);
  842. prep = luaK_getlabel(fs);
  843. check(ls, TK_DO);
  844. block(ls);
  845. luaK_patchtohere(fs, prep-1);
  846. endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
  847. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  848. luaK_patchlist(fs, endfor, prep);
  849. }
  850. static void forlist (LexState *ls, TString *indexname) {
  851. /* forlist -> NAME {,NAME} IN explist1 DO body */
  852. FuncState *fs = ls->fs;
  853. expdesc e;
  854. int line;
  855. int nvars = 0;
  856. int prep;
  857. int base = fs->freereg;
  858. new_localvarstr(ls, "(for generator)", nvars++);
  859. new_localvarstr(ls, "(for state)", nvars++);
  860. new_localvar(ls, indexname, nvars++);
  861. while (testnext(ls, ','))
  862. new_localvar(ls, str_checkname(ls), nvars++);
  863. check(ls, TK_IN);
  864. line = ls->linenumber;
  865. adjust_assign(ls, 3, explist1(ls, &e), &e);
  866. luaK_reserveregs(fs, nvars - 3); /* registers for other variables */
  867. luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
  868. adjustlocalvars(ls, nvars); /* scope for all variables */
  869. check(ls, TK_DO);
  870. prep = luaK_getlabel(fs);
  871. block(ls);
  872. luaK_patchtohere(fs, prep-1);
  873. removevars(fs->ls, fs->nactvar - nvars); /* deactivate locals for next op. */
  874. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
  875. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  876. luaK_patchlist(fs, luaK_jump(fs), prep);
  877. }
  878. static void forstat (LexState *ls, int line) {
  879. /* forstat -> fornum | forlist */
  880. FuncState *fs = ls->fs;
  881. TString *varname;
  882. BlockCnt bl;
  883. enterblock(fs, &bl, 1);
  884. next(ls); /* skip `for' */
  885. varname = str_checkname(ls); /* first variable name */
  886. switch (ls->t.token) {
  887. case '=': fornum(ls, varname, line); break;
  888. case ',': case TK_IN: forlist(ls, varname); break;
  889. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  890. }
  891. check_match(ls, TK_END, TK_FOR, line);
  892. leaveblock(fs);
  893. }
  894. static void test_then_block (LexState *ls, expdesc *v) {
  895. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  896. next(ls); /* skip IF or ELSEIF */
  897. cond(ls, v);
  898. check(ls, TK_THEN);
  899. block(ls); /* `then' part */
  900. }
  901. static void ifstat (LexState *ls, int line) {
  902. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  903. FuncState *fs = ls->fs;
  904. expdesc v;
  905. int escapelist = NO_JUMP;
  906. test_then_block(ls, &v); /* IF cond THEN block */
  907. while (ls->t.token == TK_ELSEIF) {
  908. luaK_concat(fs, &escapelist, luaK_jump(fs));
  909. luaK_patchtohere(fs, v.f);
  910. test_then_block(ls, &v); /* ELSEIF cond THEN block */
  911. }
  912. if (ls->t.token == TK_ELSE) {
  913. luaK_concat(fs, &escapelist, luaK_jump(fs));
  914. luaK_patchtohere(fs, v.f);
  915. next(ls); /* skip ELSE (after patch, for correct line info) */
  916. block(ls); /* `else' part */
  917. }
  918. else
  919. luaK_concat(fs, &escapelist, v.f);
  920. luaK_patchtohere(fs, escapelist);
  921. check_match(ls, TK_END, TK_IF, line);
  922. }
  923. static void localfunc (LexState *ls) {
  924. expdesc v, b;
  925. new_localvar(ls, str_checkname(ls), 0);
  926. init_exp(&v, VLOCAL, ls->fs->freereg++);
  927. adjustlocalvars(ls, 1);
  928. body(ls, &b, 0, ls->linenumber);
  929. luaK_storevar(ls->fs, &v, &b);
  930. }
  931. static void localstat (LexState *ls) {
  932. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  933. int nvars = 0;
  934. int nexps;
  935. expdesc e;
  936. do {
  937. new_localvar(ls, str_checkname(ls), nvars++);
  938. } while (testnext(ls, ','));
  939. if (testnext(ls, '='))
  940. nexps = explist1(ls, &e);
  941. else {
  942. e.k = VVOID;
  943. nexps = 0;
  944. }
  945. adjust_assign(ls, nvars, nexps, &e);
  946. adjustlocalvars(ls, nvars);
  947. }
  948. static int funcname (LexState *ls, expdesc *v) {
  949. /* funcname -> NAME {field} [`:' NAME] */
  950. int needself = 0;
  951. singlevar(ls, v, 1);
  952. while (ls->t.token == '.')
  953. luaY_field(ls, v);
  954. if (ls->t.token == ':') {
  955. needself = 1;
  956. luaY_field(ls, v);
  957. }
  958. return needself;
  959. }
  960. static void funcstat (LexState *ls, int line) {
  961. /* funcstat -> FUNCTION funcname body */
  962. int needself;
  963. expdesc v, b;
  964. next(ls); /* skip FUNCTION */
  965. needself = funcname(ls, &v);
  966. body(ls, &b, needself, line);
  967. luaK_storevar(ls->fs, &v, &b);
  968. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  969. }
  970. static void exprstat (LexState *ls) {
  971. /* stat -> func | assignment */
  972. FuncState *fs = ls->fs;
  973. struct LHS_assign v;
  974. primaryexp(ls, &v.v);
  975. if (v.v.k == VCALL) { /* stat -> func */
  976. luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
  977. }
  978. else { /* stat -> assignment */
  979. v.prev = NULL;
  980. assignment(ls, &v, 1);
  981. }
  982. }
  983. static void retstat (LexState *ls) {
  984. /* stat -> RETURN explist */
  985. FuncState *fs = ls->fs;
  986. expdesc e;
  987. int first, nret; /* registers with returned values */
  988. next(ls); /* skip RETURN */
  989. if (block_follow(ls->t.token) || ls->t.token == ';')
  990. first = nret = 0; /* return no values */
  991. else {
  992. nret = explist1(ls, &e); /* optional return values */
  993. if (e.k == VCALL) {
  994. luaK_setcallreturns(fs, &e, LUA_MULTRET);
  995. if (nret == 1) { /* tail call? */
  996. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  997. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  998. }
  999. first = fs->nactvar;
  1000. nret = LUA_MULTRET; /* return all values */
  1001. }
  1002. else {
  1003. if (nret == 1) /* only one single value? */
  1004. first = luaK_exp2anyreg(fs, &e);
  1005. else {
  1006. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1007. first = fs->nactvar; /* return all `active' values */
  1008. lua_assert(nret == fs->freereg - first);
  1009. }
  1010. }
  1011. }
  1012. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1013. }
  1014. static void breakstat (LexState *ls) {
  1015. /* stat -> BREAK [NAME] */
  1016. FuncState *fs = ls->fs;
  1017. BlockCnt *bl = fs->bl;
  1018. int upval = 0;
  1019. next(ls); /* skip BREAK */
  1020. while (bl && !bl->isbreakable) {
  1021. upval |= bl->upval;
  1022. bl = bl->previous;
  1023. }
  1024. if (!bl)
  1025. luaX_syntaxerror(ls, "no loop to break");
  1026. if (upval)
  1027. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1028. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1029. }
  1030. static int statement (LexState *ls) {
  1031. int line = ls->linenumber; /* may be needed for error messages */
  1032. switch (ls->t.token) {
  1033. case TK_IF: { /* stat -> ifstat */
  1034. ifstat(ls, line);
  1035. return 0;
  1036. }
  1037. case TK_WHILE: { /* stat -> whilestat */
  1038. whilestat(ls, line);
  1039. return 0;
  1040. }
  1041. case TK_DO: { /* stat -> DO block END */
  1042. next(ls); /* skip DO */
  1043. block(ls);
  1044. check_match(ls, TK_END, TK_DO, line);
  1045. return 0;
  1046. }
  1047. case TK_FOR: { /* stat -> forstat */
  1048. forstat(ls, line);
  1049. return 0;
  1050. }
  1051. case TK_REPEAT: { /* stat -> repeatstat */
  1052. repeatstat(ls, line);
  1053. return 0;
  1054. }
  1055. case TK_FUNCTION: {
  1056. funcstat(ls, line); /* stat -> funcstat */
  1057. return 0;
  1058. }
  1059. case TK_LOCAL: { /* stat -> localstat */
  1060. next(ls); /* skip LOCAL */
  1061. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1062. localfunc(ls);
  1063. else
  1064. localstat(ls);
  1065. return 0;
  1066. }
  1067. case TK_RETURN: { /* stat -> retstat */
  1068. retstat(ls);
  1069. return 1; /* must be last statement */
  1070. }
  1071. case TK_BREAK: { /* stat -> breakstat */
  1072. breakstat(ls);
  1073. return 1; /* must be last statement */
  1074. }
  1075. default: {
  1076. exprstat(ls);
  1077. return 0; /* to avoid warnings */
  1078. }
  1079. }
  1080. }
  1081. static void parlist (LexState *ls) {
  1082. /* parlist -> [ param { `,' param } ] */
  1083. int nparams = 0;
  1084. int dots = 0;
  1085. if (ls->t.token != ')') { /* is `parlist' not empty? */
  1086. do {
  1087. switch (ls->t.token) {
  1088. case TK_DOTS: dots = 1; next(ls); break;
  1089. case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
  1090. default: luaX_syntaxerror(ls, "<name> or `...' expected");
  1091. }
  1092. } while (!dots && testnext(ls, ','));
  1093. }
  1094. code_params(ls, nparams, dots);
  1095. }
  1096. static void body (LexState *ls, expdesc *e, int needself, int line) {
  1097. /* body -> `(' parlist `)' chunk END */
  1098. FuncState new_fs;
  1099. open_func(ls, &new_fs);
  1100. new_fs.f->lineDefined = line;
  1101. check(ls, '(');
  1102. if (needself)
  1103. create_local(ls, "self");
  1104. parlist(ls);
  1105. check(ls, ')');
  1106. chunk(ls);
  1107. check_match(ls, TK_END, TK_FUNCTION, line);
  1108. close_func(ls);
  1109. pushclosure(ls, &new_fs, e);
  1110. }
  1111. /* }====================================================================== */
  1112. static void chunk (LexState *ls) {
  1113. /* chunk -> { stat [`;'] } */
  1114. int islast = 0;
  1115. while (!islast && !block_follow(ls->t.token)) {
  1116. islast = statement(ls);
  1117. testnext(ls, ';');
  1118. lua_assert(ls->fs->freereg >= ls->fs->nactvar);
  1119. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1120. }
  1121. }