lparser.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /*
  2. ** $Id: lparser.c,v 1.208 2003/04/03 13:35:34 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. /* forbody -> DO block */
  874. BlockCnt bl;
  875. FuncState *fs = ls->fs;
  876. int prep, endfor;
  877. adjustlocalvars(ls, 3); /* control variables */
  878. check(ls, TK_DO);
  879. prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP);
  880. enterblock(fs, &bl, 0); /* scope for declared variables */
  881. adjustlocalvars(ls, nvars);
  882. luaK_reserveregs(fs, nvars);
  883. block(ls);
  884. leaveblock(fs); /* end of scope for declared variables */
  885. luaK_patchtohere(fs, prep);
  886. endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  887. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  888. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  889. luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  890. }
  891. static void fornum (LexState *ls, TString *varname, int line) {
  892. /* fornum -> NAME = exp1,exp1[,exp1] forbody */
  893. FuncState *fs = ls->fs;
  894. int base = fs->freereg;
  895. new_localvarstr(ls, "(for index)", 0);
  896. new_localvarstr(ls, "(for limit)", 1);
  897. new_localvarstr(ls, "(for step)", 2);
  898. new_localvar(ls, varname, 3);
  899. check(ls, '=');
  900. exp1(ls); /* initial value */
  901. check(ls, ',');
  902. exp1(ls); /* limit */
  903. if (testnext(ls, ','))
  904. exp1(ls); /* optional step */
  905. else { /* default step = 1 */
  906. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  907. luaK_reserveregs(fs, 1);
  908. }
  909. forbody(ls, base, line, 1, 1);
  910. }
  911. static void forlist (LexState *ls, TString *indexname) {
  912. /* forlist -> NAME {,NAME} IN explist1 forbody */
  913. FuncState *fs = ls->fs;
  914. expdesc e;
  915. int nvars = 0;
  916. int line;
  917. int base = fs->freereg;
  918. /* create control variables */
  919. new_localvarstr(ls, "(for generator)", nvars++);
  920. new_localvarstr(ls, "(for state)", nvars++);
  921. new_localvarstr(ls, "(for control)", nvars++);
  922. /* create declared variables */
  923. new_localvar(ls, indexname, nvars++);
  924. while (testnext(ls, ','))
  925. new_localvar(ls, str_checkname(ls), nvars++);
  926. check(ls, TK_IN);
  927. line = ls->linenumber;
  928. adjust_assign(ls, 3, explist1(ls, &e), &e);
  929. luaK_checkstack(fs, 3); /* extra space to call generator */
  930. forbody(ls, base, line, nvars - 3, 0);
  931. }
  932. static void forstat (LexState *ls, int line) {
  933. /* forstat -> FOR (fornum | forlist) END */
  934. FuncState *fs = ls->fs;
  935. TString *varname;
  936. BlockCnt bl;
  937. enterblock(fs, &bl, 1); /* scope for loop and control variables */
  938. next(ls); /* skip `for' */
  939. varname = str_checkname(ls); /* first variable name */
  940. switch (ls->t.token) {
  941. case '=': fornum(ls, varname, line); break;
  942. case ',': case TK_IN: forlist(ls, varname); break;
  943. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  944. }
  945. check_match(ls, TK_END, TK_FOR, line);
  946. leaveblock(fs); /* loop scope (`break' jumps to this point) */
  947. }
  948. static void test_then_block (LexState *ls, expdesc *v) {
  949. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  950. next(ls); /* skip IF or ELSEIF */
  951. cond(ls, v);
  952. check(ls, TK_THEN);
  953. block(ls); /* `then' part */
  954. }
  955. static void ifstat (LexState *ls, int line) {
  956. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  957. FuncState *fs = ls->fs;
  958. expdesc v;
  959. int escapelist = NO_JUMP;
  960. test_then_block(ls, &v); /* IF cond THEN block */
  961. while (ls->t.token == TK_ELSEIF) {
  962. luaK_concat(fs, &escapelist, luaK_jump(fs));
  963. luaK_patchtohere(fs, v.f);
  964. test_then_block(ls, &v); /* ELSEIF cond THEN block */
  965. }
  966. if (ls->t.token == TK_ELSE) {
  967. luaK_concat(fs, &escapelist, luaK_jump(fs));
  968. luaK_patchtohere(fs, v.f);
  969. next(ls); /* skip ELSE (after patch, for correct line info) */
  970. block(ls); /* `else' part */
  971. }
  972. else
  973. luaK_concat(fs, &escapelist, v.f);
  974. luaK_patchtohere(fs, escapelist);
  975. check_match(ls, TK_END, TK_IF, line);
  976. }
  977. static void localfunc (LexState *ls) {
  978. expdesc v, b;
  979. new_localvar(ls, str_checkname(ls), 0);
  980. init_exp(&v, VLOCAL, ls->fs->freereg++);
  981. adjustlocalvars(ls, 1);
  982. body(ls, &b, 0, ls->linenumber);
  983. luaK_storevar(ls->fs, &v, &b);
  984. }
  985. static void localstat (LexState *ls) {
  986. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  987. int nvars = 0;
  988. int nexps;
  989. expdesc e;
  990. do {
  991. new_localvar(ls, str_checkname(ls), nvars++);
  992. } while (testnext(ls, ','));
  993. if (testnext(ls, '='))
  994. nexps = explist1(ls, &e);
  995. else {
  996. e.k = VVOID;
  997. nexps = 0;
  998. }
  999. adjust_assign(ls, nvars, nexps, &e);
  1000. adjustlocalvars(ls, nvars);
  1001. }
  1002. static int funcname (LexState *ls, expdesc *v) {
  1003. /* funcname -> NAME {field} [`:' NAME] */
  1004. int needself = 0;
  1005. singlevar(ls, v, 1);
  1006. while (ls->t.token == '.')
  1007. luaY_field(ls, v);
  1008. if (ls->t.token == ':') {
  1009. needself = 1;
  1010. luaY_field(ls, v);
  1011. }
  1012. return needself;
  1013. }
  1014. static void funcstat (LexState *ls, int line) {
  1015. /* funcstat -> FUNCTION funcname body */
  1016. int needself;
  1017. expdesc v, b;
  1018. next(ls); /* skip FUNCTION */
  1019. needself = funcname(ls, &v);
  1020. body(ls, &b, needself, line);
  1021. luaK_storevar(ls->fs, &v, &b);
  1022. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  1023. }
  1024. static void exprstat (LexState *ls) {
  1025. /* stat -> func | assignment */
  1026. FuncState *fs = ls->fs;
  1027. struct LHS_assign v;
  1028. primaryexp(ls, &v.v);
  1029. if (v.v.k == VCALL) { /* stat -> func */
  1030. luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */
  1031. }
  1032. else { /* stat -> assignment */
  1033. v.prev = NULL;
  1034. assignment(ls, &v, 1);
  1035. }
  1036. }
  1037. static void retstat (LexState *ls) {
  1038. /* stat -> RETURN explist */
  1039. FuncState *fs = ls->fs;
  1040. expdesc e;
  1041. int first, nret; /* registers with returned values */
  1042. next(ls); /* skip RETURN */
  1043. if (block_follow(ls->t.token) || ls->t.token == ';')
  1044. first = nret = 0; /* return no values */
  1045. else {
  1046. nret = explist1(ls, &e); /* optional return values */
  1047. if (e.k == VCALL) {
  1048. luaK_setcallreturns(fs, &e, LUA_MULTRET);
  1049. if (nret == 1) { /* tail call? */
  1050. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1051. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1052. }
  1053. first = fs->nactvar;
  1054. nret = LUA_MULTRET; /* return all values */
  1055. }
  1056. else {
  1057. if (nret == 1) /* only one single value? */
  1058. first = luaK_exp2anyreg(fs, &e);
  1059. else {
  1060. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1061. first = fs->nactvar; /* return all `active' values */
  1062. lua_assert(nret == fs->freereg - first);
  1063. }
  1064. }
  1065. }
  1066. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1067. }
  1068. static void breakstat (LexState *ls) {
  1069. /* stat -> BREAK [NAME] */
  1070. FuncState *fs = ls->fs;
  1071. BlockCnt *bl = fs->bl;
  1072. int upval = 0;
  1073. next(ls); /* skip BREAK */
  1074. while (bl && !bl->isbreakable) {
  1075. upval |= bl->upval;
  1076. bl = bl->previous;
  1077. }
  1078. if (!bl)
  1079. luaX_syntaxerror(ls, "no loop to break");
  1080. if (upval)
  1081. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1082. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1083. }
  1084. static int statement (LexState *ls) {
  1085. int line = ls->linenumber; /* may be needed for error messages */
  1086. switch (ls->t.token) {
  1087. case TK_IF: { /* stat -> ifstat */
  1088. ifstat(ls, line);
  1089. return 0;
  1090. }
  1091. case TK_WHILE: { /* stat -> whilestat */
  1092. whilestat(ls, line);
  1093. return 0;
  1094. }
  1095. case TK_DO: { /* stat -> DO block END */
  1096. next(ls); /* skip DO */
  1097. block(ls);
  1098. check_match(ls, TK_END, TK_DO, line);
  1099. return 0;
  1100. }
  1101. case TK_FOR: { /* stat -> forstat */
  1102. forstat(ls, line);
  1103. return 0;
  1104. }
  1105. case TK_REPEAT: { /* stat -> repeatstat */
  1106. repeatstat(ls, line);
  1107. return 0;
  1108. }
  1109. case TK_FUNCTION: {
  1110. funcstat(ls, line); /* stat -> funcstat */
  1111. return 0;
  1112. }
  1113. case TK_LOCAL: { /* stat -> localstat */
  1114. next(ls); /* skip LOCAL */
  1115. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1116. localfunc(ls);
  1117. else
  1118. localstat(ls);
  1119. return 0;
  1120. }
  1121. case TK_RETURN: { /* stat -> retstat */
  1122. retstat(ls);
  1123. return 1; /* must be last statement */
  1124. }
  1125. case TK_BREAK: { /* stat -> breakstat */
  1126. breakstat(ls);
  1127. return 1; /* must be last statement */
  1128. }
  1129. default: {
  1130. exprstat(ls);
  1131. return 0; /* to avoid warnings */
  1132. }
  1133. }
  1134. }
  1135. static void chunk (LexState *ls) {
  1136. /* chunk -> { stat [`;'] } */
  1137. int islast = 0;
  1138. enterlevel(ls);
  1139. while (!islast && !block_follow(ls->t.token)) {
  1140. islast = statement(ls);
  1141. testnext(ls, ';');
  1142. lua_assert(ls->fs->freereg >= ls->fs->nactvar);
  1143. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1144. }
  1145. leavelevel(ls);
  1146. }
  1147. /* }====================================================================== */