lparser.c 35 KB

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