lparser.c 35 KB

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