lparser.c 36 KB

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