lparser.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. ** $Id: lparser.c,v 1.192 2002/08/20 20:03:05 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. 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_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key),
  399. luaK_exp2RK(fs, &val));
  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. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  972. }
  973. static void exprstat (LexState *ls) {
  974. /* stat -> func | assignment */
  975. FuncState *fs = ls->fs;
  976. struct LHS_assign v;
  977. primaryexp(ls, &v.v);
  978. if (v.v.k == VCALL) { /* stat -> func */
  979. luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
  980. }
  981. else { /* stat -> assignment */
  982. v.prev = NULL;
  983. assignment(ls, &v, 1);
  984. }
  985. }
  986. static void retstat (LexState *ls) {
  987. /* stat -> RETURN explist */
  988. FuncState *fs = ls->fs;
  989. expdesc e;
  990. int first, nret; /* registers with returned values */
  991. next(ls); /* skip RETURN */
  992. if (block_follow(ls->t.token) || ls->t.token == ';')
  993. first = nret = 0; /* return no values */
  994. else {
  995. nret = explist1(ls, &e); /* optional return values */
  996. if (e.k == VCALL) {
  997. luaK_setcallreturns(fs, &e, LUA_MULTRET);
  998. if (nret == 1) { /* tail call? */
  999. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1000. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1001. }
  1002. first = fs->nactvar;
  1003. nret = LUA_MULTRET; /* return all values */
  1004. }
  1005. else {
  1006. if (nret == 1) /* only one single value? */
  1007. first = luaK_exp2anyreg(fs, &e);
  1008. else {
  1009. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1010. first = fs->nactvar; /* return all `active' values */
  1011. lua_assert(nret == fs->freereg - first);
  1012. }
  1013. }
  1014. }
  1015. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1016. }
  1017. static void breakstat (LexState *ls) {
  1018. /* stat -> BREAK [NAME] */
  1019. FuncState *fs = ls->fs;
  1020. BlockCnt *bl = fs->bl;
  1021. int upval = 0;
  1022. next(ls); /* skip BREAK */
  1023. while (bl && !bl->isbreakable) {
  1024. upval |= bl->upval;
  1025. bl = bl->previous;
  1026. }
  1027. if (!bl)
  1028. luaX_syntaxerror(ls, "no loop to break");
  1029. if (upval)
  1030. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1031. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1032. }
  1033. static int statement (LexState *ls) {
  1034. int line = ls->linenumber; /* may be needed for error messages */
  1035. switch (ls->t.token) {
  1036. case TK_IF: { /* stat -> ifstat */
  1037. ifstat(ls, line);
  1038. return 0;
  1039. }
  1040. case TK_WHILE: { /* stat -> whilestat */
  1041. whilestat(ls, line);
  1042. return 0;
  1043. }
  1044. case TK_DO: { /* stat -> DO block END */
  1045. next(ls); /* skip DO */
  1046. block(ls);
  1047. check_match(ls, TK_END, TK_DO, line);
  1048. return 0;
  1049. }
  1050. case TK_FOR: { /* stat -> forstat */
  1051. forstat(ls, line);
  1052. return 0;
  1053. }
  1054. case TK_REPEAT: { /* stat -> repeatstat */
  1055. repeatstat(ls, line);
  1056. return 0;
  1057. }
  1058. case TK_FUNCTION: {
  1059. funcstat(ls, line); /* stat -> funcstat */
  1060. return 0;
  1061. }
  1062. case TK_LOCAL: { /* stat -> localstat */
  1063. next(ls); /* skip LOCAL */
  1064. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1065. localfunc(ls);
  1066. else
  1067. localstat(ls);
  1068. return 0;
  1069. }
  1070. case TK_RETURN: { /* stat -> retstat */
  1071. retstat(ls);
  1072. return 1; /* must be last statement */
  1073. }
  1074. case TK_BREAK: { /* stat -> breakstat */
  1075. breakstat(ls);
  1076. return 1; /* must be last statement */
  1077. }
  1078. default: {
  1079. exprstat(ls);
  1080. return 0; /* to avoid warnings */
  1081. }
  1082. }
  1083. }
  1084. static void parlist (LexState *ls) {
  1085. /* parlist -> [ param { `,' param } ] */
  1086. int nparams = 0;
  1087. int dots = 0;
  1088. if (ls->t.token != ')') { /* is `parlist' not empty? */
  1089. do {
  1090. switch (ls->t.token) {
  1091. case TK_DOTS: dots = 1; next(ls); break;
  1092. case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
  1093. default: luaX_syntaxerror(ls, "<name> or `...' expected");
  1094. }
  1095. } while (!dots && testnext(ls, ','));
  1096. }
  1097. code_params(ls, nparams, dots);
  1098. }
  1099. static void body (LexState *ls, expdesc *e, int needself, int line) {
  1100. /* body -> `(' parlist `)' chunk END */
  1101. FuncState new_fs;
  1102. open_func(ls, &new_fs);
  1103. new_fs.f->lineDefined = line;
  1104. check(ls, '(');
  1105. if (needself)
  1106. create_local(ls, "self");
  1107. parlist(ls);
  1108. check(ls, ')');
  1109. chunk(ls);
  1110. check_match(ls, TK_END, TK_FUNCTION, line);
  1111. close_func(ls);
  1112. pushclosure(ls, &new_fs, e);
  1113. }
  1114. /* }====================================================================== */
  1115. static void chunk (LexState *ls) {
  1116. /* chunk -> { stat [`;'] } */
  1117. int islast = 0;
  1118. while (!islast && !block_follow(ls->t.token)) {
  1119. islast = statement(ls);
  1120. testnext(ls, ';');
  1121. lua_assert(ls->fs->freereg >= ls->fs->nactvar);
  1122. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1123. }
  1124. }