lparser.c 35 KB

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