lparser.c 34 KB

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