lparser.c 35 KB

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