lparser.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /*
  2. ** $Id: lparser.c,v 1.176 2002/04/10 19:14:45 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_setcallreturns(ls->fs, &e, 1); /* close last expression */
  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, int line) {
  853. /* fornum -> NAME = exp1,exp1[,exp1] DO body */
  854. FuncState *fs = ls->fs;
  855. int prep, endfor;
  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. endfor = luaK_codeAsBc(fs, OP_FORLOOP, base, NO_JUMP);
  878. luaK_patchlist(fs, endfor, prep);
  879. fs->f->lineinfo[endfor] = line; /* pretend that `OP_FOR' starts the loop */
  880. }
  881. static void forlist (LexState *ls, TString *indexname) {
  882. /* forlist -> NAME {,NAME} IN explist1 DO body */
  883. FuncState *fs = ls->fs;
  884. expdesc e;
  885. int nvars = 0;
  886. int prep;
  887. int base = fs->freereg;
  888. new_localvarstr(ls, "(for generator)", nvars++);
  889. new_localvarstr(ls, "(for state)", nvars++);
  890. new_localvar(ls, indexname, nvars++);
  891. while (optional(ls, ',')) {
  892. new_localvar(ls, str_checkname(ls), nvars++);
  893. next(ls);
  894. }
  895. check(ls, TK_IN);
  896. adjust_assign(ls, 3, explist1(ls, &e), &e);
  897. luaK_reserveregs(fs, nvars - 3); /* registers for other variables */
  898. luaK_codeABC(fs, OP_TFORPREP, base, 0, 0);
  899. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
  900. adjustlocalvars(ls, nvars); /* scope for all variables */
  901. prep = luaK_jump(fs);
  902. check(ls, TK_DO);
  903. block(ls);
  904. luaK_patchlist(fs, luaK_jump(fs), prep-1);
  905. luaK_patchtohere(fs, prep);
  906. }
  907. static void forstat (LexState *ls, int line) {
  908. /* forstat -> fornum | forlist */
  909. FuncState *fs = ls->fs;
  910. TString *varname;
  911. BlockCnt bl;
  912. enterblock(fs, &bl, 1);
  913. next(ls); /* skip `for' */
  914. varname = str_checkname(ls); /* first variable name */
  915. next(ls); /* skip var name */
  916. switch (ls->t.token) {
  917. case '=': fornum(ls, varname, line); break;
  918. case ',': case TK_IN: forlist(ls, varname); break;
  919. default: luaK_error(ls, "`=' or `in' expected");
  920. }
  921. check_match(ls, TK_END, TK_FOR, line);
  922. leaveblock(fs);
  923. }
  924. static void test_then_block (LexState *ls, expdesc *v) {
  925. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  926. next(ls); /* skip IF or ELSEIF */
  927. cond(ls, v);
  928. check(ls, TK_THEN);
  929. block(ls); /* `then' part */
  930. }
  931. static void ifstat (LexState *ls, int line) {
  932. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  933. FuncState *fs = ls->fs;
  934. expdesc v;
  935. int escapelist = NO_JUMP;
  936. test_then_block(ls, &v); /* IF cond THEN block */
  937. while (ls->t.token == TK_ELSEIF) {
  938. luaK_concat(fs, &escapelist, luaK_jump(fs));
  939. luaK_patchtohere(fs, v.f);
  940. test_then_block(ls, &v); /* ELSEIF cond THEN block */
  941. }
  942. if (ls->t.token == TK_ELSE) {
  943. luaK_concat(fs, &escapelist, luaK_jump(fs));
  944. luaK_patchtohere(fs, v.f);
  945. next(ls); /* skip ELSE */
  946. block(ls); /* `else' part */
  947. }
  948. else
  949. luaK_concat(fs, &escapelist, v.f);
  950. luaK_patchtohere(fs, escapelist);
  951. check_match(ls, TK_END, TK_IF, line);
  952. }
  953. static void localstat (LexState *ls) {
  954. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  955. int nvars = 0;
  956. int nexps;
  957. expdesc e;
  958. do {
  959. next(ls); /* skip LOCAL or `,' */
  960. new_localvar(ls, str_checkname(ls), nvars++);
  961. next(ls); /* skip var name */
  962. } while (ls->t.token == ',');
  963. if (optional(ls, '='))
  964. nexps = explist1(ls, &e);
  965. else {
  966. e.k = VVOID;
  967. nexps = 0;
  968. }
  969. adjust_assign(ls, nvars, nexps, &e);
  970. adjustlocalvars(ls, nvars);
  971. }
  972. static void globalstat (LexState *ls) {
  973. /* stat -> GLOBAL NAME {`,' NAME} [IN exp] | GLOBAL IN exp */
  974. FuncState *fs = ls->fs;
  975. int nvars = 0;
  976. next(ls); /* skip GLOBAL */
  977. if (ls->t.token == TK_NAME) {
  978. do {
  979. vardesc *v = new_var(ls, nvars++);
  980. v->i = luaK_stringK(ls->fs, str_checkname(ls));
  981. next(ls); /* skip name */
  982. } while (optional(ls, ','));
  983. }
  984. if (!optional(ls, TK_IN)) { /* free globals? */
  985. if (nvars == 0) /* default - free is invalid */
  986. error_expected(ls, TK_IN);
  987. adjustglobalvars(ls, nvars, NO_REG); /* mark globals as free */
  988. }
  989. else {
  990. int baselocal = fs->freereg;
  991. int k = exp1(ls);
  992. if (nvars == 0)
  993. fs->defaultglob = (k == VNIL) ? NO_REG1 : baselocal;
  994. adjustglobalvars(ls, nvars, baselocal);
  995. create_local(ls, "*");
  996. }
  997. }
  998. static int funcname (LexState *ls, expdesc *v) {
  999. /* funcname -> NAME {field} [`:' NAME] */
  1000. int needself = 0;
  1001. singlevar(ls->fs, str_checkname(ls), v);
  1002. next(ls); /* skip var name */
  1003. while (ls->t.token == '.') {
  1004. luaY_field(ls, v);
  1005. }
  1006. if (ls->t.token == ':') {
  1007. needself = 1;
  1008. luaY_field(ls, v);
  1009. }
  1010. return needself;
  1011. }
  1012. static void funcstat (LexState *ls, int line) {
  1013. /* funcstat -> FUNCTION funcname body */
  1014. int needself;
  1015. expdesc v, b;
  1016. next(ls); /* skip FUNCTION */
  1017. needself = funcname(ls, &v);
  1018. body(ls, &b, needself, line);
  1019. luaK_storevar(ls->fs, &v, &b);
  1020. }
  1021. static void exprstat (LexState *ls) {
  1022. /* stat -> func | assignment */
  1023. FuncState *fs = ls->fs;
  1024. struct LHS_assign v;
  1025. primaryexp(ls, &v.v);
  1026. if (v.v.k == VCALL) { /* stat -> func */
  1027. luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
  1028. }
  1029. else { /* stat -> assignment */
  1030. v.prev = NULL;
  1031. assignment(ls, &v, 1);
  1032. }
  1033. }
  1034. static void retstat (LexState *ls) {
  1035. /* stat -> RETURN explist */
  1036. FuncState *fs = ls->fs;
  1037. expdesc e;
  1038. int first, nret; /* registers with returned values */
  1039. next(ls); /* skip RETURN */
  1040. if (block_follow(ls->t.token) || ls->t.token == ';')
  1041. first = nret = 0; /* return no values */
  1042. else {
  1043. nret = explist1(ls, &e); /* optional return values */
  1044. if (e.k == VCALL) {
  1045. if (nret == 1) { /* tail call? */
  1046. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1047. return;
  1048. }
  1049. luaK_setcallreturns(fs, &e, LUA_MULTRET);
  1050. first = fs->nactloc;
  1051. nret = LUA_MULTRET; /* return all values */
  1052. }
  1053. else {
  1054. if (nret == 1) /* only one single value? */
  1055. first = luaK_exp2anyreg(fs, &e);
  1056. else {
  1057. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1058. first = fs->nactloc; /* return all `active' values */
  1059. lua_assert(nret == fs->freereg - first);
  1060. }
  1061. }
  1062. }
  1063. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1064. }
  1065. static void breakstat (LexState *ls) {
  1066. /* stat -> BREAK [NAME] */
  1067. FuncState *fs = ls->fs;
  1068. BlockCnt *bl = fs->bl;
  1069. int upval = 0;
  1070. next(ls); /* skip BREAK */
  1071. while (bl && !bl->isbreakable) {
  1072. upval |= bl->upval;
  1073. bl = bl->previous;
  1074. }
  1075. if (!bl)
  1076. luaK_error(ls, "no loop to break");
  1077. if (upval)
  1078. luaK_codeABC(fs, OP_CLOSE, bl->nactloc, 0, 0);
  1079. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1080. }
  1081. static int statement (LexState *ls) {
  1082. int line = ls->linenumber; /* may be needed for error messages */
  1083. switch (ls->t.token) {
  1084. case TK_IF: { /* stat -> ifstat */
  1085. ifstat(ls, line);
  1086. return 0;
  1087. }
  1088. case TK_WHILE: { /* stat -> whilestat */
  1089. whilestat(ls, line);
  1090. return 0;
  1091. }
  1092. case TK_DO: { /* stat -> DO block END */
  1093. next(ls); /* skip DO */
  1094. block(ls);
  1095. check_match(ls, TK_END, TK_DO, line);
  1096. return 0;
  1097. }
  1098. case TK_FOR: { /* stat -> forstat */
  1099. forstat(ls, line);
  1100. return 0;
  1101. }
  1102. case TK_REPEAT: { /* stat -> repeatstat */
  1103. repeatstat(ls, line);
  1104. return 0;
  1105. }
  1106. case TK_FUNCTION: {
  1107. funcstat(ls, line); /* stat -> funcstat */
  1108. return 0;
  1109. }
  1110. case TK_LOCAL: { /* stat -> localstat */
  1111. localstat(ls);
  1112. return 0;
  1113. }
  1114. case TK_GLOBAL: { /* stat -> globalstat */
  1115. globalstat(ls);
  1116. return 0;
  1117. }
  1118. case TK_RETURN: { /* stat -> retstat */
  1119. retstat(ls);
  1120. return 1; /* must be last statement */
  1121. }
  1122. case TK_BREAK: { /* stat -> breakstat */
  1123. breakstat(ls);
  1124. return 1; /* must be last statement */
  1125. }
  1126. default: {
  1127. exprstat(ls);
  1128. return 0; /* to avoid warnings */
  1129. }
  1130. }
  1131. }
  1132. static void parlist (LexState *ls) {
  1133. /* parlist -> [ param { `,' param } ] */
  1134. int nparams = 0;
  1135. int dots = 0;
  1136. if (ls->t.token != ')') { /* is `parlist' not empty? */
  1137. do {
  1138. switch (ls->t.token) {
  1139. case TK_DOTS: dots = 1; break;
  1140. case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
  1141. default: luaK_error(ls, "<name> or `...' expected");
  1142. }
  1143. next(ls);
  1144. } while (!dots && optional(ls, ','));
  1145. }
  1146. code_params(ls, nparams, dots);
  1147. }
  1148. static void body (LexState *ls, expdesc *e, int needself, int line) {
  1149. /* body -> `(' parlist `)' chunk END */
  1150. FuncState new_fs;
  1151. open_func(ls, &new_fs);
  1152. new_fs.f->lineDefined = line;
  1153. check(ls, '(');
  1154. if (needself)
  1155. create_local(ls, "self");
  1156. parlist(ls);
  1157. check(ls, ')');
  1158. chunk(ls);
  1159. check_match(ls, TK_END, TK_FUNCTION, line);
  1160. close_func(ls);
  1161. pushclosure(ls, &new_fs, e);
  1162. }
  1163. /* }====================================================================== */
  1164. static void chunk (LexState *ls) {
  1165. /* chunk -> { stat [`;'] } */
  1166. int islast = 0;
  1167. while (!islast && !block_follow(ls->t.token)) {
  1168. islast = statement(ls);
  1169. optional(ls, ';');
  1170. lua_assert(ls->fs->freereg >= ls->fs->nactloc);
  1171. ls->fs->freereg = ls->fs->nactloc; /* free registers */
  1172. }
  1173. }