lparser.c 36 KB

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