lparser.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /*
  2. ** $Id: lparser.c,v 2.21 2005/04/27 18:53:53 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. /*
  25. ** nodes for block list (list of active blocks)
  26. */
  27. typedef struct BlockCnt {
  28. struct BlockCnt *previous; /* chain */
  29. int breaklist; /* list of jumps out of this loop */
  30. lu_byte nactvar; /* # active locals outside the breakable structure */
  31. lu_byte upval; /* true if some variable in the block is an upvalue */
  32. lu_byte isbreakable; /* true if `block' is a loop */
  33. } BlockCnt;
  34. /*
  35. ** prototypes for recursive non-terminal functions
  36. */
  37. static void chunk (LexState *ls);
  38. static void expr (LexState *ls, expdesc *v);
  39. static void next (LexState *ls) {
  40. ls->lastline = ls->linenumber;
  41. if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
  42. ls->t = ls->lookahead; /* use this one */
  43. ls->lookahead.token = TK_EOS; /* and discharge it */
  44. }
  45. else
  46. ls->t.token = luaX_lex(ls, &ls->t.seminfo); /* read next token */
  47. }
  48. static void lookahead (LexState *ls) {
  49. lua_assert(ls->lookahead.token == TK_EOS);
  50. ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
  51. }
  52. static void anchor_token (LexState *ls) {
  53. if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
  54. TString *ts = ls->t.seminfo.ts;
  55. luaX_newstring(ls, getstr(ts), ts->tsv.len);
  56. }
  57. }
  58. static void error_expected (LexState *ls, int token) {
  59. luaX_syntaxerror(ls,
  60. luaO_pushfstring(ls->L, "`%s' expected", luaX_token2str(ls, token)));
  61. }
  62. static void errorlimit (FuncState *fs, int limit, const char *what) {
  63. const char *msg = (fs->f->lineDefined == 0) ?
  64. luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
  65. luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
  66. fs->f->lineDefined, limit, what);
  67. luaX_lexerror(fs->ls, msg, 0);
  68. }
  69. static int testnext (LexState *ls, int c) {
  70. if (ls->t.token == c) {
  71. next(ls);
  72. return 1;
  73. }
  74. else return 0;
  75. }
  76. static void check (LexState *ls, int c) {
  77. if (ls->t.token != c)
  78. error_expected(ls, c);
  79. }
  80. static void checknext (LexState *ls, int c) {
  81. check(ls, c);
  82. next(ls);
  83. }
  84. #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
  85. static void check_match (LexState *ls, int what, int who, int where) {
  86. if (!testnext(ls, what)) {
  87. if (where == ls->linenumber)
  88. error_expected(ls, what);
  89. else {
  90. luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
  91. "`%s' expected (to close `%s' at line %d)",
  92. luaX_token2str(ls, what), luaX_token2str(ls, who), where));
  93. }
  94. }
  95. }
  96. static TString *str_checkname (LexState *ls) {
  97. TString *ts;
  98. check(ls, TK_NAME);
  99. ts = ls->t.seminfo.ts;
  100. next(ls);
  101. return ts;
  102. }
  103. static void init_exp (expdesc *e, expkind k, int i) {
  104. e->f = e->t = NO_JUMP;
  105. e->k = k;
  106. e->info = i;
  107. }
  108. static void codestring (LexState *ls, expdesc *e, TString *s) {
  109. init_exp(e, VK, luaK_stringK(ls->fs, s));
  110. }
  111. static void checkname(LexState *ls, expdesc *e) {
  112. codestring(ls, e, str_checkname(ls));
  113. }
  114. static int registerlocalvar (LexState *ls, TString *varname) {
  115. FuncState *fs = ls->fs;
  116. Proto *f = fs->f;
  117. int oldsize = f->sizelocvars;
  118. luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
  119. LocVar, SHRT_MAX, "too many local variables");
  120. while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
  121. f->locvars[fs->nlocvars].varname = varname;
  122. luaC_objbarrier(ls->L, f, varname);
  123. return fs->nlocvars++;
  124. }
  125. #define new_localvarliteral(ls,v,n) \
  126. new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
  127. static void new_localvar (LexState *ls, TString *name, int n) {
  128. FuncState *fs = ls->fs;
  129. luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
  130. fs->actvar[fs->nactvar+n] = cast(unsigned short, 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, LUAI_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 enterlevel (LexState *ls) {
  226. if (++ls->L->nCcalls > LUAI_MAXCCALLS)
  227. luaX_lexerror(ls, "chunk has too many syntax levels", 0);
  228. }
  229. #define leavelevel(ls) ((ls)->L->nCcalls--)
  230. static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable) {
  231. bl->breaklist = NO_JUMP;
  232. bl->isbreakable = isbreakable;
  233. bl->nactvar = fs->nactvar;
  234. bl->upval = 0;
  235. bl->previous = fs->bl;
  236. fs->bl = bl;
  237. lua_assert(fs->freereg == fs->nactvar);
  238. }
  239. static void leaveblock (FuncState *fs) {
  240. BlockCnt *bl = fs->bl;
  241. fs->bl = bl->previous;
  242. removevars(fs->ls, bl->nactvar);
  243. if (bl->upval)
  244. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  245. lua_assert(bl->nactvar == fs->nactvar);
  246. fs->freereg = fs->nactvar; /* free registers */
  247. luaK_patchtohere(fs, bl->breaklist);
  248. }
  249. static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
  250. FuncState *fs = ls->fs;
  251. Proto *f = fs->f;
  252. int oldsize = f->sizep;
  253. int i;
  254. luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
  255. MAXARG_Bx, "constant table overflow");
  256. while (oldsize < f->sizep) f->p[oldsize++] = NULL;
  257. f->p[fs->np++] = func->f;
  258. luaC_objbarrier(ls->L, f, func->f);
  259. init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
  260. for (i=0; i<func->f->nups; i++) {
  261. OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
  262. luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
  263. }
  264. }
  265. static void open_func (LexState *ls, FuncState *fs) {
  266. lua_State *L = ls->L;
  267. Proto *f = luaF_newproto(L);
  268. fs->f = f;
  269. fs->prev = ls->fs; /* linked list of funcstates */
  270. fs->ls = ls;
  271. fs->L = L;
  272. ls->fs = fs;
  273. fs->pc = 0;
  274. fs->lasttarget = 0;
  275. fs->jpc = NO_JUMP;
  276. fs->freereg = 0;
  277. fs->nk = 0;
  278. fs->np = 0;
  279. fs->nlocvars = 0;
  280. fs->nactvar = 0;
  281. fs->bl = NULL;
  282. f->source = ls->source;
  283. f->maxstacksize = 2; /* registers 0/1 are always valid */
  284. fs->h = luaH_new(L, 0, 0);
  285. /* anchor table of constants and prototype (to avoid being collected) */
  286. sethvalue2s(L, L->top, fs->h);
  287. incr_top(L);
  288. setptvalue2s(L, L->top, f);
  289. incr_top(L);
  290. }
  291. static void close_func (LexState *ls) {
  292. lua_State *L = ls->L;
  293. FuncState *fs = ls->fs;
  294. Proto *f = fs->f;
  295. removevars(ls, 0);
  296. luaK_codeABC(fs, OP_RETURN, 0, 1, 0); /* final return */
  297. luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
  298. f->sizecode = fs->pc;
  299. luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
  300. f->sizelineinfo = fs->pc;
  301. luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
  302. f->sizek = fs->nk;
  303. luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
  304. f->sizep = fs->np;
  305. luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
  306. f->sizelocvars = fs->nlocvars;
  307. luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
  308. f->sizeupvalues = f->nups;
  309. lua_assert(luaG_checkcode(f));
  310. lua_assert(fs->bl == NULL);
  311. ls->fs = fs->prev;
  312. L->top -= 2; /* remove table and prototype from the stack */
  313. /* last token read was anchored in defunct function; must reanchor it */
  314. if (fs) anchor_token(ls);
  315. }
  316. Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
  317. struct LexState lexstate;
  318. struct FuncState funcstate;
  319. lexstate.buff = buff;
  320. luaX_setinput(L, &lexstate, z, luaS_new(L, name));
  321. open_func(&lexstate, &funcstate);
  322. funcstate.f->is_vararg = NEWSTYLEVARARG;
  323. next(&lexstate); /* read first token */
  324. chunk(&lexstate);
  325. check(&lexstate, TK_EOS);
  326. close_func(&lexstate);
  327. lua_assert(funcstate.prev == NULL);
  328. lua_assert(funcstate.f->nups == 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 | true | false | ... |
  618. constructor | FUNCTION body | 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. case '*': return OPR_SIZE;
  669. default: return OPR_NOUNOPR;
  670. }
  671. }
  672. static BinOpr getbinopr (int op) {
  673. switch (op) {
  674. case '+': return OPR_ADD;
  675. case '-': return OPR_SUB;
  676. case '*': return OPR_MULT;
  677. case '/': return OPR_DIV;
  678. case '%': return OPR_MOD;
  679. case '^': return OPR_POW;
  680. case TK_CONCAT: return OPR_CONCAT;
  681. case TK_NE: return OPR_NE;
  682. case TK_EQ: return OPR_EQ;
  683. case '<': return OPR_LT;
  684. case TK_LE: return OPR_LE;
  685. case '>': return OPR_GT;
  686. case TK_GE: return OPR_GE;
  687. case TK_AND: return OPR_AND;
  688. case TK_OR: return OPR_OR;
  689. default: return OPR_NOBINOPR;
  690. }
  691. }
  692. static const struct {
  693. lu_byte left; /* left priority for each binary operator */
  694. lu_byte right; /* right priority */
  695. } priority[] = { /* ORDER OPR */
  696. {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */
  697. {10, 9}, {5, 4}, /* power and concat (right associative) */
  698. {3, 3}, {3, 3}, /* equality and inequality */
  699. {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
  700. {2, 2}, {1, 1} /* logical (and/or) */
  701. };
  702. #define UNARY_PRIORITY 8 /* priority for unary operators */
  703. /*
  704. ** subexpr -> (simplexep | unop subexpr) { binop subexpr }
  705. ** where `binop' is any binary operator with a priority higher than `limit'
  706. */
  707. static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
  708. BinOpr op;
  709. UnOpr uop;
  710. enterlevel(ls);
  711. uop = getunopr(ls->t.token);
  712. if (uop != OPR_NOUNOPR) {
  713. next(ls);
  714. subexpr(ls, v, UNARY_PRIORITY);
  715. luaK_prefix(ls->fs, uop, v);
  716. }
  717. else simpleexp(ls, v);
  718. /* expand while operators have priorities higher than `limit' */
  719. op = getbinopr(ls->t.token);
  720. while (op != OPR_NOBINOPR && priority[op].left > limit) {
  721. expdesc v2;
  722. BinOpr nextop;
  723. next(ls);
  724. luaK_infix(ls->fs, op, v);
  725. /* read sub-expression with higher priority */
  726. nextop = subexpr(ls, &v2, priority[op].right);
  727. luaK_posfix(ls->fs, op, v, &v2);
  728. op = nextop;
  729. }
  730. leavelevel(ls);
  731. return op; /* return first untreated operator */
  732. }
  733. static void expr (LexState *ls, expdesc *v) {
  734. subexpr(ls, v, 0);
  735. }
  736. /* }==================================================================== */
  737. /*
  738. ** {======================================================================
  739. ** Rules for Statements
  740. ** =======================================================================
  741. */
  742. static int block_follow (int token) {
  743. switch (token) {
  744. case TK_ELSE: case TK_ELSEIF: case TK_END:
  745. case TK_UNTIL: case TK_EOS:
  746. return 1;
  747. default: return 0;
  748. }
  749. }
  750. static void block (LexState *ls) {
  751. /* block -> chunk */
  752. FuncState *fs = ls->fs;
  753. BlockCnt bl;
  754. enterblock(fs, &bl, 0);
  755. chunk(ls);
  756. lua_assert(bl.breaklist == NO_JUMP);
  757. leaveblock(fs);
  758. }
  759. /*
  760. ** structure to chain all variables in the left-hand side of an
  761. ** assignment
  762. */
  763. struct LHS_assign {
  764. struct LHS_assign *prev;
  765. expdesc v; /* variable (global, local, upvalue, or indexed) */
  766. };
  767. /*
  768. ** check whether, in an assignment to a local variable, the local variable
  769. ** is needed in a previous assignment (to a table). If so, save original
  770. ** local value in a safe place and use this safe copy in the previous
  771. ** assignment.
  772. */
  773. static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
  774. FuncState *fs = ls->fs;
  775. int extra = fs->freereg; /* eventual position to save local variable */
  776. int conflict = 0;
  777. for (; lh; lh = lh->prev) {
  778. if (lh->v.k == VINDEXED) {
  779. if (lh->v.info == v->info) { /* conflict? */
  780. conflict = 1;
  781. lh->v.info = extra; /* previous assignment will use safe copy */
  782. }
  783. if (lh->v.aux == v->info) { /* conflict? */
  784. conflict = 1;
  785. lh->v.aux = extra; /* previous assignment will use safe copy */
  786. }
  787. }
  788. }
  789. if (conflict) {
  790. luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
  791. luaK_reserveregs(fs, 1);
  792. }
  793. }
  794. static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  795. expdesc e;
  796. check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
  797. "syntax error");
  798. if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
  799. struct LHS_assign nv;
  800. nv.prev = lh;
  801. primaryexp(ls, &nv.v);
  802. if (nv.v.k == VLOCAL)
  803. check_conflict(ls, lh, &nv.v);
  804. assignment(ls, &nv, nvars+1);
  805. }
  806. else { /* assignment -> `=' explist1 */
  807. int nexps;
  808. checknext(ls, '=');
  809. nexps = explist1(ls, &e);
  810. if (nexps != nvars) {
  811. adjust_assign(ls, nvars, nexps, &e);
  812. if (nexps > nvars)
  813. ls->fs->freereg -= nexps - nvars; /* remove extra values */
  814. }
  815. else {
  816. luaK_setoneret(ls->fs, &e); /* close last expression */
  817. luaK_storevar(ls->fs, &lh->v, &e);
  818. return; /* avoid default */
  819. }
  820. }
  821. init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
  822. luaK_storevar(ls->fs, &lh->v, &e);
  823. }
  824. static int cond (LexState *ls) {
  825. /* cond -> exp */
  826. expdesc v;
  827. expr(ls, &v); /* read condition */
  828. if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
  829. luaK_goiftrue(ls->fs, &v);
  830. luaK_patchtohere(ls->fs, v.t);
  831. return v.f;
  832. }
  833. /*
  834. ** The while statement optimizes its code by coding the condition
  835. ** after its body (and thus avoiding one jump in the loop).
  836. */
  837. /*
  838. ** the call `luaK_goiffalse' may grow the size of an expression by
  839. ** at most this:
  840. */
  841. #define EXTRAEXP 5
  842. static void whilestat (LexState *ls, int line) {
  843. /* whilestat -> WHILE cond DO block END */
  844. Instruction codeexp[LUAI_MAXEXPWHILE + EXTRAEXP];
  845. int lineexp;
  846. int i;
  847. int sizeexp;
  848. FuncState *fs = ls->fs;
  849. int whileinit, blockinit, expinit;
  850. expdesc v;
  851. BlockCnt bl;
  852. next(ls); /* skip WHILE */
  853. whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
  854. expinit = luaK_getlabel(fs);
  855. expr(ls, &v); /* parse condition */
  856. if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
  857. lineexp = ls->linenumber;
  858. luaK_goiffalse(fs, &v);
  859. luaK_concat(fs, &v.f, fs->jpc);
  860. fs->jpc = NO_JUMP;
  861. sizeexp = fs->pc - expinit; /* size of expression code */
  862. if (sizeexp > LUAI_MAXEXPWHILE)
  863. luaX_syntaxerror(ls, "`while' condition too complex");
  864. for (i = 0; i < sizeexp; i++) /* save `exp' code */
  865. codeexp[i] = fs->f->code[expinit + i];
  866. fs->pc = expinit; /* remove `exp' code */
  867. enterblock(fs, &bl, 1);
  868. checknext(ls, TK_DO);
  869. blockinit = luaK_getlabel(fs);
  870. block(ls);
  871. luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
  872. /* move `exp' back to code */
  873. if (v.t != NO_JUMP) v.t += fs->pc - expinit;
  874. if (v.f != NO_JUMP) v.f += fs->pc - expinit;
  875. for (i=0; i<sizeexp; i++)
  876. luaK_code(fs, codeexp[i], lineexp);
  877. check_match(ls, TK_END, TK_WHILE, line);
  878. leaveblock(fs);
  879. luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
  880. luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
  881. }
  882. static void repeatstat (LexState *ls, int line) {
  883. /* repeatstat -> REPEAT block UNTIL cond */
  884. FuncState *fs = ls->fs;
  885. int repeat_init = luaK_getlabel(fs);
  886. int flist;
  887. BlockCnt bl;
  888. enterblock(fs, &bl, 1);
  889. next(ls);
  890. block(ls);
  891. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  892. flist = cond(ls);
  893. luaK_patchlist(fs, flist, repeat_init);
  894. leaveblock(fs);
  895. }
  896. static int exp1 (LexState *ls) {
  897. expdesc e;
  898. int k;
  899. expr(ls, &e);
  900. k = e.k;
  901. luaK_exp2nextreg(ls->fs, &e);
  902. return k;
  903. }
  904. static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  905. /* forbody -> DO block */
  906. BlockCnt bl;
  907. FuncState *fs = ls->fs;
  908. int prep, endfor;
  909. adjustlocalvars(ls, 3); /* control variables */
  910. checknext(ls, TK_DO);
  911. prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP);
  912. enterblock(fs, &bl, 0); /* scope for declared variables */
  913. adjustlocalvars(ls, nvars);
  914. luaK_reserveregs(fs, nvars);
  915. block(ls);
  916. leaveblock(fs); /* end of scope for declared variables */
  917. luaK_patchtohere(fs, prep);
  918. endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  919. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  920. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  921. luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  922. }
  923. static void fornum (LexState *ls, TString *varname, int line) {
  924. /* fornum -> NAME = exp1,exp1[,exp1] forbody */
  925. FuncState *fs = ls->fs;
  926. int base = fs->freereg;
  927. new_localvarliteral(ls, "(for index)", 0);
  928. new_localvarliteral(ls, "(for limit)", 1);
  929. new_localvarliteral(ls, "(for step)", 2);
  930. new_localvar(ls, varname, 3);
  931. checknext(ls, '=');
  932. exp1(ls); /* initial value */
  933. checknext(ls, ',');
  934. exp1(ls); /* limit */
  935. if (testnext(ls, ','))
  936. exp1(ls); /* optional step */
  937. else { /* default step = 1 */
  938. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  939. luaK_reserveregs(fs, 1);
  940. }
  941. forbody(ls, base, line, 1, 1);
  942. }
  943. static void forlist (LexState *ls, TString *indexname) {
  944. /* forlist -> NAME {,NAME} IN explist1 forbody */
  945. FuncState *fs = ls->fs;
  946. expdesc e;
  947. int nvars = 0;
  948. int line;
  949. int base = fs->freereg;
  950. /* create control variables */
  951. new_localvarliteral(ls, "(for generator)", nvars++);
  952. new_localvarliteral(ls, "(for state)", nvars++);
  953. new_localvarliteral(ls, "(for control)", nvars++);
  954. /* create declared variables */
  955. new_localvar(ls, indexname, nvars++);
  956. while (testnext(ls, ','))
  957. new_localvar(ls, str_checkname(ls), nvars++);
  958. checknext(ls, TK_IN);
  959. line = ls->linenumber;
  960. adjust_assign(ls, 3, explist1(ls, &e), &e);
  961. luaK_checkstack(fs, 3); /* extra space to call generator */
  962. forbody(ls, base, line, nvars - 3, 0);
  963. }
  964. static void forstat (LexState *ls, int line) {
  965. /* forstat -> FOR (fornum | forlist) END */
  966. FuncState *fs = ls->fs;
  967. TString *varname;
  968. BlockCnt bl;
  969. enterblock(fs, &bl, 1); /* scope for loop and control variables */
  970. next(ls); /* skip `for' */
  971. varname = str_checkname(ls); /* first variable name */
  972. switch (ls->t.token) {
  973. case '=': fornum(ls, varname, line); break;
  974. case ',': case TK_IN: forlist(ls, varname); break;
  975. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  976. }
  977. check_match(ls, TK_END, TK_FOR, line);
  978. leaveblock(fs); /* loop scope (`break' jumps to this point) */
  979. }
  980. static int test_then_block (LexState *ls) {
  981. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  982. int flist;
  983. next(ls); /* skip IF or ELSEIF */
  984. flist = cond(ls);
  985. checknext(ls, TK_THEN);
  986. block(ls); /* `then' part */
  987. return flist;
  988. }
  989. static void ifstat (LexState *ls, int line) {
  990. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  991. FuncState *fs = ls->fs;
  992. int flist;
  993. int escapelist = NO_JUMP;
  994. flist = test_then_block(ls); /* IF cond THEN block */
  995. while (ls->t.token == TK_ELSEIF) {
  996. luaK_concat(fs, &escapelist, luaK_jump(fs));
  997. luaK_patchtohere(fs, flist);
  998. flist = test_then_block(ls); /* ELSEIF cond THEN block */
  999. }
  1000. if (ls->t.token == TK_ELSE) {
  1001. luaK_concat(fs, &escapelist, luaK_jump(fs));
  1002. luaK_patchtohere(fs, flist);
  1003. next(ls); /* skip ELSE (after patch, for correct line info) */
  1004. block(ls); /* `else' part */
  1005. }
  1006. else
  1007. luaK_concat(fs, &escapelist, flist);
  1008. luaK_patchtohere(fs, escapelist);
  1009. check_match(ls, TK_END, TK_IF, line);
  1010. }
  1011. static void localfunc (LexState *ls) {
  1012. expdesc v, b;
  1013. FuncState *fs = ls->fs;
  1014. new_localvar(ls, str_checkname(ls), 0);
  1015. init_exp(&v, VLOCAL, fs->freereg);
  1016. luaK_reserveregs(fs, 1);
  1017. adjustlocalvars(ls, 1);
  1018. body(ls, &b, 0, ls->linenumber);
  1019. luaK_storevar(fs, &v, &b);
  1020. /* debug information will only see the variable after this point! */
  1021. getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
  1022. }
  1023. static void localstat (LexState *ls) {
  1024. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  1025. int nvars = 0;
  1026. int nexps;
  1027. expdesc e;
  1028. do {
  1029. new_localvar(ls, str_checkname(ls), nvars++);
  1030. } while (testnext(ls, ','));
  1031. if (testnext(ls, '='))
  1032. nexps = explist1(ls, &e);
  1033. else {
  1034. e.k = VVOID;
  1035. nexps = 0;
  1036. }
  1037. adjust_assign(ls, nvars, nexps, &e);
  1038. adjustlocalvars(ls, nvars);
  1039. }
  1040. static int funcname (LexState *ls, expdesc *v) {
  1041. /* funcname -> NAME {field} [`:' NAME] */
  1042. int needself = 0;
  1043. singlevar(ls, v, 1);
  1044. while (ls->t.token == '.')
  1045. field(ls, v);
  1046. if (ls->t.token == ':') {
  1047. needself = 1;
  1048. field(ls, v);
  1049. }
  1050. return needself;
  1051. }
  1052. static void funcstat (LexState *ls, int line) {
  1053. /* funcstat -> FUNCTION funcname body */
  1054. int needself;
  1055. expdesc v, b;
  1056. next(ls); /* skip FUNCTION */
  1057. needself = funcname(ls, &v);
  1058. body(ls, &b, needself, line);
  1059. luaK_storevar(ls->fs, &v, &b);
  1060. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  1061. }
  1062. static void exprstat (LexState *ls) {
  1063. /* stat -> func | assignment */
  1064. FuncState *fs = ls->fs;
  1065. struct LHS_assign v;
  1066. primaryexp(ls, &v.v);
  1067. if (v.v.k == VCALL) /* stat -> func */
  1068. SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
  1069. else { /* stat -> assignment */
  1070. v.prev = NULL;
  1071. assignment(ls, &v, 1);
  1072. }
  1073. }
  1074. static void retstat (LexState *ls) {
  1075. /* stat -> RETURN explist */
  1076. FuncState *fs = ls->fs;
  1077. expdesc e;
  1078. int first, nret; /* registers with returned values */
  1079. next(ls); /* skip RETURN */
  1080. if (block_follow(ls->t.token) || ls->t.token == ';')
  1081. first = nret = 0; /* return no values */
  1082. else {
  1083. nret = explist1(ls, &e); /* optional return values */
  1084. if (hasmultret(e.k)) {
  1085. luaK_setmultret(fs, &e);
  1086. if (e.k == VCALL && nret == 1) { /* tail call? */
  1087. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1088. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1089. }
  1090. first = fs->nactvar;
  1091. nret = LUA_MULTRET; /* return all values */
  1092. }
  1093. else {
  1094. if (nret == 1) /* only one single value? */
  1095. first = luaK_exp2anyreg(fs, &e);
  1096. else {
  1097. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1098. first = fs->nactvar; /* return all `active' values */
  1099. lua_assert(nret == fs->freereg - first);
  1100. }
  1101. }
  1102. }
  1103. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1104. }
  1105. static void breakstat (LexState *ls) {
  1106. /* stat -> BREAK */
  1107. FuncState *fs = ls->fs;
  1108. BlockCnt *bl = fs->bl;
  1109. int upval = 0;
  1110. next(ls); /* skip BREAK */
  1111. while (bl && !bl->isbreakable) {
  1112. upval |= bl->upval;
  1113. bl = bl->previous;
  1114. }
  1115. if (!bl)
  1116. luaX_syntaxerror(ls, "no loop to break");
  1117. if (upval)
  1118. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1119. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1120. }
  1121. static int statement (LexState *ls) {
  1122. int line = ls->linenumber; /* may be needed for error messages */
  1123. switch (ls->t.token) {
  1124. case TK_IF: { /* stat -> ifstat */
  1125. ifstat(ls, line);
  1126. return 0;
  1127. }
  1128. case TK_WHILE: { /* stat -> whilestat */
  1129. whilestat(ls, line);
  1130. return 0;
  1131. }
  1132. case TK_DO: { /* stat -> DO block END */
  1133. next(ls); /* skip DO */
  1134. block(ls);
  1135. check_match(ls, TK_END, TK_DO, line);
  1136. return 0;
  1137. }
  1138. case TK_FOR: { /* stat -> forstat */
  1139. forstat(ls, line);
  1140. return 0;
  1141. }
  1142. case TK_REPEAT: { /* stat -> repeatstat */
  1143. repeatstat(ls, line);
  1144. return 0;
  1145. }
  1146. case TK_FUNCTION: {
  1147. funcstat(ls, line); /* stat -> funcstat */
  1148. return 0;
  1149. }
  1150. case TK_LOCAL: { /* stat -> localstat */
  1151. next(ls); /* skip LOCAL */
  1152. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1153. localfunc(ls);
  1154. else
  1155. localstat(ls);
  1156. return 0;
  1157. }
  1158. case TK_RETURN: { /* stat -> retstat */
  1159. retstat(ls);
  1160. return 1; /* must be last statement */
  1161. }
  1162. case TK_BREAK: { /* stat -> breakstat */
  1163. breakstat(ls);
  1164. return 1; /* must be last statement */
  1165. }
  1166. default: {
  1167. exprstat(ls);
  1168. return 0; /* to avoid warnings */
  1169. }
  1170. }
  1171. }
  1172. static void chunk (LexState *ls) {
  1173. /* chunk -> { stat [`;'] } */
  1174. int islast = 0;
  1175. enterlevel(ls);
  1176. while (!islast && !block_follow(ls->t.token)) {
  1177. islast = statement(ls);
  1178. testnext(ls, ';');
  1179. lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
  1180. ls->fs->freereg >= ls->fs->nactvar);
  1181. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1182. }
  1183. leavelevel(ls);
  1184. }
  1185. /* }====================================================================== */