lparser.c 36 KB

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