lparser.c 36 KB

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