lparser.c 35 KB

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