lparser.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. ** $Id: lparser.c,v 2.5 2004/05/31 18:51:50 roberto Exp $
  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_setlist(fs, cc->t->info, cc->na, cc->tostore); /* flush */
  385. cc->tostore = 0; /* no more items pending */
  386. }
  387. }
  388. static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
  389. if (cc->tostore == 0) return;
  390. if (hasmultret(cc->v.k)) {
  391. luaK_setmultret(fs, &cc->v);
  392. luaK_setlist(fs, cc->t->info, cc->na, LUA_MULTRET);
  393. cc->na--; /* do not count last expression (unknown number of elements) */
  394. }
  395. else {
  396. if (cc->v.k != VVOID)
  397. luaK_exp2nextreg(fs, &cc->v);
  398. luaK_setlist(fs, cc->t->info, cc->na, cc->tostore);
  399. }
  400. }
  401. static void listfield (LexState *ls, struct ConsControl *cc) {
  402. expr(ls, &cc->v);
  403. luaY_checklimit(ls->fs, cc->na, MAXARG_Bx, "items in a constructor");
  404. cc->na++;
  405. cc->tostore++;
  406. }
  407. static void constructor (LexState *ls, expdesc *t) {
  408. /* constructor -> ?? */
  409. FuncState *fs = ls->fs;
  410. int line = ls->linenumber;
  411. int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  412. struct ConsControl cc;
  413. cc.na = cc.nh = cc.tostore = 0;
  414. cc.t = t;
  415. init_exp(t, VRELOCABLE, pc);
  416. init_exp(&cc.v, VVOID, 0); /* no value (yet) */
  417. luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
  418. check(ls, '{');
  419. do {
  420. lua_assert(cc.v.k == VVOID || cc.tostore > 0);
  421. testnext(ls, ';'); /* compatibility only */
  422. if (ls->t.token == '}') break;
  423. closelistfield(fs, &cc);
  424. switch(ls->t.token) {
  425. case TK_NAME: { /* may be listfields or recfields */
  426. lookahead(ls);
  427. if (ls->lookahead.token != '=') /* expression? */
  428. listfield(ls, &cc);
  429. else
  430. recfield(ls, &cc);
  431. break;
  432. }
  433. case '[': { /* constructor_item -> recfield */
  434. recfield(ls, &cc);
  435. break;
  436. }
  437. default: { /* constructor_part -> listfield */
  438. listfield(ls, &cc);
  439. break;
  440. }
  441. }
  442. } while (testnext(ls, ',') || testnext(ls, ';'));
  443. check_match(ls, '}', '{', line);
  444. lastlistfield(fs, &cc);
  445. SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
  446. SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */
  447. }
  448. /* }====================================================================== */
  449. static void parlist (LexState *ls) {
  450. /* parlist -> [ param { `,' param } ] */
  451. FuncState *fs = ls->fs;
  452. Proto *f = fs->f;
  453. int nparams = 0;
  454. f->is_vararg = 0;
  455. if (ls->t.token != ')') { /* is `parlist' not empty? */
  456. do {
  457. switch (ls->t.token) {
  458. case TK_NAME: { /* param -> NAME */
  459. new_localvar(ls, str_checkname(ls), nparams++);
  460. break;
  461. }
  462. case TK_DOTS: { /* param -> `...' */
  463. next(ls);
  464. /* use `arg' as default name */
  465. new_localvarliteral(ls, "arg", nparams++);
  466. f->is_vararg = 1;
  467. break;
  468. }
  469. default: luaX_syntaxerror(ls, "<name> or `...' expected");
  470. }
  471. } while (!f->is_vararg && testnext(ls, ','));
  472. }
  473. adjustlocalvars(ls, nparams);
  474. f->numparams = fs->nactvar - f->is_vararg;
  475. luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
  476. }
  477. static void body (LexState *ls, expdesc *e, int needself, int line) {
  478. /* body -> `(' parlist `)' chunk END */
  479. FuncState new_fs;
  480. open_func(ls, &new_fs);
  481. new_fs.f->lineDefined = line;
  482. check(ls, '(');
  483. if (needself) {
  484. new_localvarliteral(ls, "self", 0);
  485. adjustlocalvars(ls, 1);
  486. }
  487. parlist(ls);
  488. check(ls, ')');
  489. chunk(ls);
  490. check_match(ls, TK_END, TK_FUNCTION, line);
  491. close_func(ls);
  492. pushclosure(ls, &new_fs, e);
  493. }
  494. static int explist1 (LexState *ls, expdesc *v) {
  495. /* explist1 -> expr { `,' expr } */
  496. int n = 1; /* at least one expression */
  497. expr(ls, v);
  498. while (testnext(ls, ',')) {
  499. luaK_exp2nextreg(ls->fs, v);
  500. expr(ls, v);
  501. n++;
  502. }
  503. return n;
  504. }
  505. static void funcargs (LexState *ls, expdesc *f) {
  506. FuncState *fs = ls->fs;
  507. expdesc args;
  508. int base, nparams;
  509. int line = ls->linenumber;
  510. switch (ls->t.token) {
  511. case '(': { /* funcargs -> `(' [ explist1 ] `)' */
  512. if (line != ls->lastline)
  513. luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
  514. next(ls);
  515. if (ls->t.token == ')') /* arg list is empty? */
  516. args.k = VVOID;
  517. else {
  518. explist1(ls, &args);
  519. luaK_setmultret(fs, &args);
  520. }
  521. check_match(ls, ')', '(', line);
  522. break;
  523. }
  524. case '{': { /* funcargs -> constructor */
  525. constructor(ls, &args);
  526. break;
  527. }
  528. case TK_STRING: { /* funcargs -> STRING */
  529. codestring(ls, &args, ls->t.seminfo.ts);
  530. next(ls); /* must use `seminfo' before `next' */
  531. break;
  532. }
  533. default: {
  534. luaX_syntaxerror(ls, "function arguments expected");
  535. return;
  536. }
  537. }
  538. lua_assert(f->k == VNONRELOC);
  539. base = f->info; /* base register for call */
  540. if (hasmultret(args.k))
  541. nparams = LUA_MULTRET; /* open call */
  542. else {
  543. if (args.k != VVOID)
  544. luaK_exp2nextreg(fs, &args); /* close last argument */
  545. nparams = fs->freereg - (base+1);
  546. }
  547. init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
  548. luaK_fixline(fs, line);
  549. fs->freereg = base+1; /* call remove function and arguments and leaves
  550. (unless changed) one result */
  551. }
  552. /*
  553. ** {======================================================================
  554. ** Expression parsing
  555. ** =======================================================================
  556. */
  557. static void prefixexp (LexState *ls, expdesc *v) {
  558. /* prefixexp -> NAME | '(' expr ')' */
  559. switch (ls->t.token) {
  560. case '(': {
  561. int line = ls->linenumber;
  562. next(ls);
  563. expr(ls, v);
  564. check_match(ls, ')', '(', line);
  565. luaK_dischargevars(ls->fs, v);
  566. return;
  567. }
  568. case TK_NAME: {
  569. singlevar(ls, v, 1);
  570. return;
  571. }
  572. default: {
  573. luaX_syntaxerror(ls, "unexpected symbol");
  574. return;
  575. }
  576. }
  577. }
  578. static void primaryexp (LexState *ls, expdesc *v) {
  579. /* primaryexp ->
  580. prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
  581. FuncState *fs = ls->fs;
  582. prefixexp(ls, v);
  583. for (;;) {
  584. switch (ls->t.token) {
  585. case '.': { /* field */
  586. luaY_field(ls, v);
  587. break;
  588. }
  589. case '[': { /* `[' exp1 `]' */
  590. expdesc key;
  591. luaK_exp2anyreg(fs, v);
  592. luaY_index(ls, &key);
  593. luaK_indexed(fs, v, &key);
  594. break;
  595. }
  596. case ':': { /* `:' NAME funcargs */
  597. expdesc key;
  598. next(ls);
  599. checkname(ls, &key);
  600. luaK_self(fs, v, &key);
  601. funcargs(ls, v);
  602. break;
  603. }
  604. case '(': case TK_STRING: case '{': { /* funcargs */
  605. luaK_exp2nextreg(fs, v);
  606. funcargs(ls, v);
  607. break;
  608. }
  609. default: return;
  610. }
  611. }
  612. }
  613. static void simpleexp (LexState *ls, expdesc *v) {
  614. /* simpleexp -> NUMBER | STRING | NIL | constructor | FUNCTION body
  615. | primaryexp */
  616. switch (ls->t.token) {
  617. case TK_NUMBER: {
  618. init_exp(v, VK, luaK_numberK(ls->fs, ls->t.seminfo.r));
  619. break;
  620. }
  621. case TK_STRING: {
  622. codestring(ls, v, ls->t.seminfo.ts);
  623. break;
  624. }
  625. case TK_NIL: {
  626. init_exp(v, VNIL, 0);
  627. break;
  628. }
  629. case TK_TRUE: {
  630. init_exp(v, VTRUE, 0);
  631. break;
  632. }
  633. case TK_FALSE: {
  634. init_exp(v, VFALSE, 0);
  635. break;
  636. }
  637. case TK_DOTS: { /* vararg */
  638. FuncState *fs = ls->fs;
  639. check_condition(ls, fs->f->is_vararg,
  640. "cannot use `...' outside a vararg function");
  641. fs->f->is_vararg = NEWSTYLEVARARG;
  642. init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
  643. break;
  644. }
  645. case '{': { /* constructor */
  646. constructor(ls, v);
  647. return;
  648. }
  649. case TK_FUNCTION: {
  650. next(ls);
  651. body(ls, v, 0, ls->linenumber);
  652. return;
  653. }
  654. default: {
  655. primaryexp(ls, v);
  656. return;
  657. }
  658. }
  659. next(ls);
  660. }
  661. static UnOpr getunopr (int op) {
  662. switch (op) {
  663. case TK_NOT: return OPR_NOT;
  664. case '-': return OPR_MINUS;
  665. default: return OPR_NOUNOPR;
  666. }
  667. }
  668. static BinOpr getbinopr (int op) {
  669. switch (op) {
  670. case '+': return OPR_ADD;
  671. case '-': return OPR_SUB;
  672. case '*': return OPR_MULT;
  673. case '/': return OPR_DIV;
  674. case '^': return OPR_POW;
  675. case TK_CONCAT: return OPR_CONCAT;
  676. case TK_NE: return OPR_NE;
  677. case TK_EQ: return OPR_EQ;
  678. case '<': return OPR_LT;
  679. case TK_LE: return OPR_LE;
  680. case '>': return OPR_GT;
  681. case TK_GE: return OPR_GE;
  682. case TK_AND: return OPR_AND;
  683. case TK_OR: return OPR_OR;
  684. default: return OPR_NOBINOPR;
  685. }
  686. }
  687. static const struct {
  688. lu_byte left; /* left priority for each binary operator */
  689. lu_byte right; /* right priority */
  690. } priority[] = { /* ORDER OPR */
  691. {6, 6}, {6, 6}, {7, 7}, {7, 7}, /* arithmetic */
  692. {10, 9}, {5, 4}, /* power and concat (right associative) */
  693. {3, 3}, {3, 3}, /* equality */
  694. {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
  695. {2, 2}, {1, 1} /* logical (and/or) */
  696. };
  697. #define UNARY_PRIORITY 8 /* priority for unary operators */
  698. /*
  699. ** subexpr -> (simplexep | unop subexpr) { binop subexpr }
  700. ** where `binop' is any binary operator with a priority higher than `limit'
  701. */
  702. static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
  703. BinOpr op;
  704. UnOpr uop;
  705. enterlevel(ls);
  706. uop = getunopr(ls->t.token);
  707. if (uop != OPR_NOUNOPR) {
  708. next(ls);
  709. subexpr(ls, v, UNARY_PRIORITY);
  710. luaK_prefix(ls->fs, uop, v);
  711. }
  712. else simpleexp(ls, v);
  713. /* expand while operators have priorities higher than `limit' */
  714. op = getbinopr(ls->t.token);
  715. while (op != OPR_NOBINOPR && priority[op].left > limit) {
  716. expdesc v2;
  717. BinOpr nextop;
  718. next(ls);
  719. luaK_infix(ls->fs, op, v);
  720. /* read sub-expression with higher priority */
  721. nextop = subexpr(ls, &v2, priority[op].right);
  722. luaK_posfix(ls->fs, op, v, &v2);
  723. op = nextop;
  724. }
  725. leavelevel(ls);
  726. return op; /* return first untreated operator */
  727. }
  728. static void expr (LexState *ls, expdesc *v) {
  729. subexpr(ls, v, 0);
  730. }
  731. /* }==================================================================== */
  732. /*
  733. ** {======================================================================
  734. ** Rules for Statements
  735. ** =======================================================================
  736. */
  737. static int block_follow (int token) {
  738. switch (token) {
  739. case TK_ELSE: case TK_ELSEIF: case TK_END:
  740. case TK_UNTIL: case TK_EOS:
  741. return 1;
  742. default: return 0;
  743. }
  744. }
  745. static void block (LexState *ls) {
  746. /* block -> chunk */
  747. FuncState *fs = ls->fs;
  748. BlockCnt bl;
  749. enterblock(fs, &bl, 0);
  750. chunk(ls);
  751. lua_assert(bl.breaklist == NO_JUMP);
  752. leaveblock(fs);
  753. }
  754. /*
  755. ** structure to chain all variables in the left-hand side of an
  756. ** assignment
  757. */
  758. struct LHS_assign {
  759. struct LHS_assign *prev;
  760. expdesc v; /* variable (global, local, upvalue, or indexed) */
  761. };
  762. /*
  763. ** check whether, in an assignment to a local variable, the local variable
  764. ** is needed in a previous assignment (to a table). If so, save original
  765. ** local value in a safe place and use this safe copy in the previous
  766. ** assignment.
  767. */
  768. static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
  769. FuncState *fs = ls->fs;
  770. int extra = fs->freereg; /* eventual position to save local variable */
  771. int conflict = 0;
  772. for (; lh; lh = lh->prev) {
  773. if (lh->v.k == VINDEXED) {
  774. if (lh->v.info == v->info) { /* conflict? */
  775. conflict = 1;
  776. lh->v.info = extra; /* previous assignment will use safe copy */
  777. }
  778. if (lh->v.aux == v->info) { /* conflict? */
  779. conflict = 1;
  780. lh->v.aux = extra; /* previous assignment will use safe copy */
  781. }
  782. }
  783. }
  784. if (conflict) {
  785. luaK_codeABC(fs, OP_MOVE, fs->freereg, v->info, 0); /* make copy */
  786. luaK_reserveregs(fs, 1);
  787. }
  788. }
  789. static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  790. expdesc e;
  791. check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
  792. "syntax error");
  793. if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
  794. struct LHS_assign nv;
  795. nv.prev = lh;
  796. primaryexp(ls, &nv.v);
  797. if (nv.v.k == VLOCAL)
  798. check_conflict(ls, lh, &nv.v);
  799. assignment(ls, &nv, nvars+1);
  800. }
  801. else { /* assignment -> `=' explist1 */
  802. int nexps;
  803. check(ls, '=');
  804. nexps = explist1(ls, &e);
  805. if (nexps != nvars) {
  806. adjust_assign(ls, nvars, nexps, &e);
  807. if (nexps > nvars)
  808. ls->fs->freereg -= nexps - nvars; /* remove extra values */
  809. }
  810. else {
  811. luaK_setoneret(ls->fs, &e); /* close last expression */
  812. luaK_storevar(ls->fs, &lh->v, &e);
  813. return; /* avoid default */
  814. }
  815. }
  816. init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
  817. luaK_storevar(ls->fs, &lh->v, &e);
  818. }
  819. static int cond (LexState *ls) {
  820. /* cond -> exp */
  821. expdesc v;
  822. expr(ls, &v); /* read condition */
  823. if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
  824. luaK_goiftrue(ls->fs, &v);
  825. luaK_patchtohere(ls->fs, v.t);
  826. return v.f;
  827. }
  828. /*
  829. ** The while statement optimizes its code by coding the condition
  830. ** after its body (and thus avoiding one jump in the loop).
  831. */
  832. /*
  833. ** the call `luaK_goiffalse' may grow the size of an expression by
  834. ** at most this:
  835. */
  836. #define EXTRAEXP 5
  837. static void whilestat (LexState *ls, int line) {
  838. /* whilestat -> WHILE cond DO block END */
  839. Instruction codeexp[MAXEXPWHILE + EXTRAEXP];
  840. int lineexp;
  841. int i;
  842. int sizeexp;
  843. FuncState *fs = ls->fs;
  844. int whileinit, blockinit, expinit;
  845. expdesc v;
  846. BlockCnt bl;
  847. next(ls); /* skip WHILE */
  848. whileinit = luaK_jump(fs); /* jump to condition (which will be moved) */
  849. expinit = luaK_getlabel(fs);
  850. expr(ls, &v); /* parse condition */
  851. if (v.k == VK) v.k = VTRUE; /* `trues' are all equal here */
  852. lineexp = ls->linenumber;
  853. luaK_goiffalse(fs, &v);
  854. luaK_concat(fs, &v.f, fs->jpc);
  855. fs->jpc = NO_JUMP;
  856. sizeexp = fs->pc - expinit; /* size of expression code */
  857. if (sizeexp > MAXEXPWHILE)
  858. luaX_syntaxerror(ls, "`while' condition too complex");
  859. for (i = 0; i < sizeexp; i++) /* save `exp' code */
  860. codeexp[i] = fs->f->code[expinit + i];
  861. fs->pc = expinit; /* remove `exp' code */
  862. enterblock(fs, &bl, 1);
  863. check(ls, TK_DO);
  864. blockinit = luaK_getlabel(fs);
  865. block(ls);
  866. luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */
  867. /* move `exp' back to code */
  868. if (v.t != NO_JUMP) v.t += fs->pc - expinit;
  869. if (v.f != NO_JUMP) v.f += fs->pc - expinit;
  870. for (i=0; i<sizeexp; i++)
  871. luaK_code(fs, codeexp[i], lineexp);
  872. check_match(ls, TK_END, TK_WHILE, line);
  873. leaveblock(fs);
  874. luaK_patchlist(fs, v.t, blockinit); /* true conditions go back to loop */
  875. luaK_patchtohere(fs, v.f); /* false conditions finish the loop */
  876. }
  877. static void repeatstat (LexState *ls, int line) {
  878. /* repeatstat -> REPEAT block UNTIL cond */
  879. FuncState *fs = ls->fs;
  880. int repeat_init = luaK_getlabel(fs);
  881. int flist;
  882. BlockCnt bl;
  883. enterblock(fs, &bl, 1);
  884. next(ls);
  885. block(ls);
  886. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  887. flist = cond(ls);
  888. luaK_patchlist(fs, flist, repeat_init);
  889. leaveblock(fs);
  890. }
  891. static int exp1 (LexState *ls) {
  892. expdesc e;
  893. int k;
  894. expr(ls, &e);
  895. k = e.k;
  896. luaK_exp2nextreg(ls->fs, &e);
  897. return k;
  898. }
  899. static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  900. /* forbody -> DO block */
  901. BlockCnt bl;
  902. FuncState *fs = ls->fs;
  903. int prep, endfor;
  904. adjustlocalvars(ls, 3); /* control variables */
  905. check(ls, TK_DO);
  906. prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP);
  907. enterblock(fs, &bl, 0); /* scope for declared variables */
  908. adjustlocalvars(ls, nvars);
  909. luaK_reserveregs(fs, nvars);
  910. block(ls);
  911. leaveblock(fs); /* end of scope for declared variables */
  912. luaK_patchtohere(fs, prep);
  913. endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
  914. luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  915. luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
  916. luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  917. }
  918. static void fornum (LexState *ls, TString *varname, int line) {
  919. /* fornum -> NAME = exp1,exp1[,exp1] forbody */
  920. FuncState *fs = ls->fs;
  921. int base = fs->freereg;
  922. new_localvarliteral(ls, "(for index)", 0);
  923. new_localvarliteral(ls, "(for limit)", 1);
  924. new_localvarliteral(ls, "(for step)", 2);
  925. new_localvar(ls, varname, 3);
  926. check(ls, '=');
  927. exp1(ls); /* initial value */
  928. check(ls, ',');
  929. exp1(ls); /* limit */
  930. if (testnext(ls, ','))
  931. exp1(ls); /* optional step */
  932. else { /* default step = 1 */
  933. luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
  934. luaK_reserveregs(fs, 1);
  935. }
  936. forbody(ls, base, line, 1, 1);
  937. }
  938. static void forlist (LexState *ls, TString *indexname) {
  939. /* forlist -> NAME {,NAME} IN explist1 forbody */
  940. FuncState *fs = ls->fs;
  941. expdesc e;
  942. int nvars = 0;
  943. int line;
  944. int base = fs->freereg;
  945. /* create control variables */
  946. new_localvarliteral(ls, "(for generator)", nvars++);
  947. new_localvarliteral(ls, "(for state)", nvars++);
  948. new_localvarliteral(ls, "(for control)", nvars++);
  949. /* create declared variables */
  950. new_localvar(ls, indexname, nvars++);
  951. while (testnext(ls, ','))
  952. new_localvar(ls, str_checkname(ls), nvars++);
  953. check(ls, TK_IN);
  954. line = ls->linenumber;
  955. adjust_assign(ls, 3, explist1(ls, &e), &e);
  956. luaK_checkstack(fs, 3); /* extra space to call generator */
  957. forbody(ls, base, line, nvars - 3, 0);
  958. }
  959. static void forstat (LexState *ls, int line) {
  960. /* forstat -> FOR (fornum | forlist) END */
  961. FuncState *fs = ls->fs;
  962. TString *varname;
  963. BlockCnt bl;
  964. enterblock(fs, &bl, 1); /* scope for loop and control variables */
  965. next(ls); /* skip `for' */
  966. varname = str_checkname(ls); /* first variable name */
  967. switch (ls->t.token) {
  968. case '=': fornum(ls, varname, line); break;
  969. case ',': case TK_IN: forlist(ls, varname); break;
  970. default: luaX_syntaxerror(ls, "`=' or `in' expected");
  971. }
  972. check_match(ls, TK_END, TK_FOR, line);
  973. leaveblock(fs); /* loop scope (`break' jumps to this point) */
  974. }
  975. static int test_then_block (LexState *ls) {
  976. /* test_then_block -> [IF | ELSEIF] cond THEN block */
  977. int flist;
  978. next(ls); /* skip IF or ELSEIF */
  979. flist = cond(ls);
  980. check(ls, TK_THEN);
  981. block(ls); /* `then' part */
  982. return flist;
  983. }
  984. static void ifstat (LexState *ls, int line) {
  985. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  986. FuncState *fs = ls->fs;
  987. int flist;
  988. int escapelist = NO_JUMP;
  989. flist = test_then_block(ls); /* IF cond THEN block */
  990. while (ls->t.token == TK_ELSEIF) {
  991. luaK_concat(fs, &escapelist, luaK_jump(fs));
  992. luaK_patchtohere(fs, flist);
  993. flist = test_then_block(ls); /* ELSEIF cond THEN block */
  994. }
  995. if (ls->t.token == TK_ELSE) {
  996. luaK_concat(fs, &escapelist, luaK_jump(fs));
  997. luaK_patchtohere(fs, flist);
  998. next(ls); /* skip ELSE (after patch, for correct line info) */
  999. block(ls); /* `else' part */
  1000. }
  1001. else
  1002. luaK_concat(fs, &escapelist, flist);
  1003. luaK_patchtohere(fs, escapelist);
  1004. check_match(ls, TK_END, TK_IF, line);
  1005. }
  1006. static void localfunc (LexState *ls) {
  1007. expdesc v, b;
  1008. FuncState *fs = ls->fs;
  1009. new_localvar(ls, str_checkname(ls), 0);
  1010. init_exp(&v, VLOCAL, fs->freereg);
  1011. luaK_reserveregs(fs, 1);
  1012. adjustlocalvars(ls, 1);
  1013. body(ls, &b, 0, ls->linenumber);
  1014. luaK_storevar(fs, &v, &b);
  1015. /* debug information will only see the variable after this point! */
  1016. getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
  1017. }
  1018. static void localstat (LexState *ls) {
  1019. /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
  1020. int nvars = 0;
  1021. int nexps;
  1022. expdesc e;
  1023. do {
  1024. new_localvar(ls, str_checkname(ls), nvars++);
  1025. } while (testnext(ls, ','));
  1026. if (testnext(ls, '='))
  1027. nexps = explist1(ls, &e);
  1028. else {
  1029. e.k = VVOID;
  1030. nexps = 0;
  1031. }
  1032. adjust_assign(ls, nvars, nexps, &e);
  1033. adjustlocalvars(ls, nvars);
  1034. }
  1035. static int funcname (LexState *ls, expdesc *v) {
  1036. /* funcname -> NAME {field} [`:' NAME] */
  1037. int needself = 0;
  1038. singlevar(ls, v, 1);
  1039. while (ls->t.token == '.')
  1040. luaY_field(ls, v);
  1041. if (ls->t.token == ':') {
  1042. needself = 1;
  1043. luaY_field(ls, v);
  1044. }
  1045. return needself;
  1046. }
  1047. static void funcstat (LexState *ls, int line) {
  1048. /* funcstat -> FUNCTION funcname body */
  1049. int needself;
  1050. expdesc v, b;
  1051. next(ls); /* skip FUNCTION */
  1052. needself = funcname(ls, &v);
  1053. body(ls, &b, needself, line);
  1054. luaK_storevar(ls->fs, &v, &b);
  1055. luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
  1056. }
  1057. static void exprstat (LexState *ls) {
  1058. /* stat -> func | assignment */
  1059. FuncState *fs = ls->fs;
  1060. struct LHS_assign v;
  1061. primaryexp(ls, &v.v);
  1062. if (v.v.k == VCALL) /* stat -> func */
  1063. SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
  1064. else { /* stat -> assignment */
  1065. v.prev = NULL;
  1066. assignment(ls, &v, 1);
  1067. }
  1068. }
  1069. static void retstat (LexState *ls) {
  1070. /* stat -> RETURN explist */
  1071. FuncState *fs = ls->fs;
  1072. expdesc e;
  1073. int first, nret; /* registers with returned values */
  1074. next(ls); /* skip RETURN */
  1075. if (block_follow(ls->t.token) || ls->t.token == ';')
  1076. first = nret = 0; /* return no values */
  1077. else {
  1078. nret = explist1(ls, &e); /* optional return values */
  1079. if (hasmultret(e.k)) {
  1080. luaK_setmultret(fs, &e);
  1081. if (e.k == VCALL && nret == 1) { /* tail call? */
  1082. SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
  1083. lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
  1084. }
  1085. first = fs->nactvar;
  1086. nret = LUA_MULTRET; /* return all values */
  1087. }
  1088. else {
  1089. if (nret == 1) /* only one single value? */
  1090. first = luaK_exp2anyreg(fs, &e);
  1091. else {
  1092. luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
  1093. first = fs->nactvar; /* return all `active' values */
  1094. lua_assert(nret == fs->freereg - first);
  1095. }
  1096. }
  1097. }
  1098. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  1099. }
  1100. static void breakstat (LexState *ls) {
  1101. /* stat -> BREAK [NAME] */
  1102. FuncState *fs = ls->fs;
  1103. BlockCnt *bl = fs->bl;
  1104. int upval = 0;
  1105. next(ls); /* skip BREAK */
  1106. while (bl && !bl->isbreakable) {
  1107. upval |= bl->upval;
  1108. bl = bl->previous;
  1109. }
  1110. if (!bl)
  1111. luaX_syntaxerror(ls, "no loop to break");
  1112. if (upval)
  1113. luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
  1114. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  1115. }
  1116. static int statement (LexState *ls) {
  1117. int line = ls->linenumber; /* may be needed for error messages */
  1118. switch (ls->t.token) {
  1119. case TK_IF: { /* stat -> ifstat */
  1120. ifstat(ls, line);
  1121. return 0;
  1122. }
  1123. case TK_WHILE: { /* stat -> whilestat */
  1124. whilestat(ls, line);
  1125. return 0;
  1126. }
  1127. case TK_DO: { /* stat -> DO block END */
  1128. next(ls); /* skip DO */
  1129. block(ls);
  1130. check_match(ls, TK_END, TK_DO, line);
  1131. return 0;
  1132. }
  1133. case TK_FOR: { /* stat -> forstat */
  1134. forstat(ls, line);
  1135. return 0;
  1136. }
  1137. case TK_REPEAT: { /* stat -> repeatstat */
  1138. repeatstat(ls, line);
  1139. return 0;
  1140. }
  1141. case TK_FUNCTION: {
  1142. funcstat(ls, line); /* stat -> funcstat */
  1143. return 0;
  1144. }
  1145. case TK_LOCAL: { /* stat -> localstat */
  1146. next(ls); /* skip LOCAL */
  1147. if (testnext(ls, TK_FUNCTION)) /* local function? */
  1148. localfunc(ls);
  1149. else
  1150. localstat(ls);
  1151. return 0;
  1152. }
  1153. case TK_RETURN: { /* stat -> retstat */
  1154. retstat(ls);
  1155. return 1; /* must be last statement */
  1156. }
  1157. case TK_BREAK: { /* stat -> breakstat */
  1158. breakstat(ls);
  1159. return 1; /* must be last statement */
  1160. }
  1161. default: {
  1162. exprstat(ls);
  1163. return 0; /* to avoid warnings */
  1164. }
  1165. }
  1166. }
  1167. static void chunk (LexState *ls) {
  1168. /* chunk -> { stat [`;'] } */
  1169. int islast = 0;
  1170. enterlevel(ls);
  1171. while (!islast && !block_follow(ls->t.token)) {
  1172. islast = statement(ls);
  1173. testnext(ls, ';');
  1174. lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
  1175. ls->fs->freereg >= ls->fs->nactvar);
  1176. ls->fs->freereg = ls->fs->nactvar; /* free registers */
  1177. }
  1178. leavelevel(ls);
  1179. }
  1180. /* }====================================================================== */