lparser.c 35 KB

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