lparser.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. ** $Id: lparser.c,v 1.78 2000/04/07 13:13:11 roberto Exp roberto $
  3. ** LL(1) Parser and code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define LUA_REENTRANT
  9. #include "lcode.h"
  10. #include "ldo.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. ** Constructors descriptor:
  21. ** `n' indicates number of elements, and `k' signals whether
  22. ** it is a list constructor (k = 0) or a record constructor (k = 1)
  23. ** or empty (k = ';' or '}')
  24. */
  25. typedef struct Constdesc {
  26. int n;
  27. int k;
  28. } Constdesc;
  29. typedef struct Breaklabel {
  30. struct Breaklabel *previous; /* chain */
  31. int breaklist;
  32. int stacklevel;
  33. } Breaklabel;
  34. /*
  35. ** prototypes for recursive non-terminal functions
  36. */
  37. static void body (LexState *ls, int needself, int line);
  38. static void chunk (LexState *ls);
  39. static void constructor (LexState *ls);
  40. static void expr (LexState *ls, expdesc *v);
  41. static void exp1 (LexState *ls);
  42. static void next (LexState *ls) {
  43. ls->token = luaX_lex(ls);
  44. }
  45. static void error_expected (LexState *ls, int token) {
  46. char buff[100], t[TOKEN_LEN];
  47. luaX_token2str(token, t);
  48. sprintf(buff, "`%.20s' expected", t);
  49. luaK_error(ls, buff);
  50. }
  51. static void error_unexpected (LexState *ls) {
  52. luaK_error(ls, "unexpected token");
  53. }
  54. static void check (LexState *ls, int c) {
  55. if (ls->token != c)
  56. error_expected(ls, c);
  57. next(ls);
  58. }
  59. static void checklimit (LexState *ls, int val, int limit, const char *msg) {
  60. if (val > limit) {
  61. char buff[100];
  62. sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
  63. luaK_error(ls, buff);
  64. }
  65. }
  66. static void setline (LexState *ls) {
  67. FuncState *fs = ls->fs;
  68. if (ls->L->debug && ls->linenumber != fs->lastsetline) {
  69. checklimit(ls, ls->linenumber, MAXARG_U, "lines in a chunk");
  70. luaK_code1(fs, OP_SETLINE, ls->linenumber);
  71. fs->lastsetline = ls->linenumber;
  72. }
  73. }
  74. static int optional (LexState *ls, int c) {
  75. if (ls->token == c) {
  76. next(ls);
  77. return 1;
  78. }
  79. else return 0;
  80. }
  81. static void check_match (LexState *ls, int what, int who, int where) {
  82. if (ls->token != what) {
  83. if (where == ls->linenumber)
  84. error_expected(ls, what);
  85. else {
  86. char buff[100];
  87. char t_what[TOKEN_LEN], t_who[TOKEN_LEN];
  88. luaX_token2str(what, t_what);
  89. luaX_token2str(who, t_who);
  90. sprintf(buff, "`%.20s' expected (to close `%.20s' at line %d)",
  91. t_what, t_who, where);
  92. luaK_error(ls, buff);
  93. }
  94. }
  95. next(ls);
  96. }
  97. static void setline_and_next (LexState *ls) {
  98. setline(ls);
  99. next(ls);
  100. }
  101. static void check_END (LexState *ls, int who, int where) {
  102. setline(ls); /* setline for END */
  103. check_match(ls, TK_END, who, where);
  104. }
  105. static int string_constant (FuncState *fs, TString *s) {
  106. Proto *f = fs->f;
  107. int c = s->constindex;
  108. if (c >= f->nkstr || f->kstr[c] != s) {
  109. luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *,
  110. constantEM, MAXARG_U);
  111. c = f->nkstr++;
  112. f->kstr[c] = s;
  113. s->constindex = c; /* hint for next time */
  114. }
  115. return c;
  116. }
  117. static void code_string (LexState *ls, TString *s) {
  118. luaK_kstr(ls, string_constant(ls->fs, s));
  119. }
  120. static int checkname (LexState *ls) {
  121. int sc;
  122. if (ls->token != TK_NAME)
  123. luaK_error(ls, "<name> expected");
  124. sc = string_constant(ls->fs, ls->seminfo.ts);
  125. next(ls);
  126. return sc;
  127. }
  128. static TString *str_checkname (LexState *ls) {
  129. int i = checkname(ls); /* this call may realloc `f->consts' */
  130. return ls->fs->f->kstr[i];
  131. }
  132. static void luaI_registerlocalvar (LexState *ls, TString *varname,
  133. int line) {
  134. FuncState *fs = ls->fs;
  135. if (fs->nvars != -1) { /* debug information? */
  136. Proto *f = fs->f;
  137. luaM_growvector(ls->L, f->locvars, fs->nvars, 1, LocVar, "", MAX_INT);
  138. f->locvars[fs->nvars].varname = varname;
  139. f->locvars[fs->nvars].line = line;
  140. fs->nvars++;
  141. }
  142. }
  143. static void luaI_unregisterlocalvar (LexState *ls, int line) {
  144. luaI_registerlocalvar(ls, NULL, line);
  145. }
  146. static void store_localvar (LexState *ls, TString *name, int n) {
  147. FuncState *fs = ls->fs;
  148. checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
  149. fs->localvar[fs->nlocalvar+n] = name;
  150. }
  151. static void adjustlocalvars (LexState *ls, int nvars, int line) {
  152. FuncState *fs = ls->fs;
  153. int i;
  154. fs->nlocalvar += nvars;
  155. for (i=fs->nlocalvar-nvars; i<fs->nlocalvar; i++)
  156. luaI_registerlocalvar(ls, fs->localvar[i], line);
  157. }
  158. static void add_localvar (LexState *ls, TString *name) {
  159. store_localvar(ls, name, 0);
  160. adjustlocalvars(ls, 1, 0);
  161. }
  162. static int aux_localname (FuncState *fs, TString *n) {
  163. int i;
  164. for (i=fs->nlocalvar-1; i >= 0; i--)
  165. if (n == fs->localvar[i]) return i; /* local var index */
  166. return -1; /* not found */
  167. }
  168. static void singlevar (LexState *ls, TString *n, expdesc *var, int prev) {
  169. FuncState *fs = prev ? ls->fs->prev : ls->fs;
  170. int i = aux_localname(fs, n);
  171. if (i >= 0) { /* local value? */
  172. var->k = VLOCAL;
  173. var->u.index = i;
  174. }
  175. else {
  176. FuncState *level = fs;
  177. while ((level = level->prev) != NULL) /* check shadowing */
  178. if (aux_localname(level, n) >= 0)
  179. luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
  180. var->k = VGLOBAL;
  181. var->u.index = string_constant(fs, n);
  182. }
  183. }
  184. static int indexupvalue (LexState *ls, TString *n) {
  185. FuncState *fs = ls->fs;
  186. expdesc v;
  187. int i;
  188. singlevar(ls, n, &v, 1);
  189. for (i=0; i<fs->nupvalues; i++) {
  190. if (fs->upvalues[i].k == v.k && fs->upvalues[i].u.index == v.u.index)
  191. return i;
  192. }
  193. /* new one */
  194. ++(fs->nupvalues);
  195. checklimit(ls, fs->nupvalues, MAXUPVALUES, "upvalues");
  196. fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */
  197. return i;
  198. }
  199. static void pushupvalue (LexState *ls, TString *n) {
  200. FuncState *fs = ls->fs;
  201. if (fs->prev == NULL)
  202. luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
  203. if (aux_localname(ls->fs, n) >= 0)
  204. luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
  205. luaK_code1(fs, OP_PUSHUPVALUE, indexupvalue(ls, n));
  206. }
  207. static void adjust_mult_assign (LexState *ls, int nvars, int nexps) {
  208. FuncState *fs = ls->fs;
  209. int diff = nexps - nvars;
  210. if (nexps == 0 || !luaK_lastisopen(fs)) { /* list is empty or closed */
  211. /* push or pop eventual difference between list lengths */
  212. luaK_adjuststack(fs, diff);
  213. }
  214. else { /* list ends in a function call; must correct it */
  215. diff--; /* do not count function call itself */
  216. if (diff <= 0) { /* more variables than values? */
  217. /* function call must provide extra values */
  218. luaK_setcallreturns(fs, -diff);
  219. }
  220. else { /* more values than variables */
  221. luaK_setcallreturns(fs, 0); /* call should provide no value */
  222. luaK_adjuststack(fs, diff); /* pop eventual extra values */
  223. }
  224. }
  225. }
  226. static void code_args (LexState *ls, int nparams, int dots) {
  227. FuncState *fs = ls->fs;
  228. adjustlocalvars(ls, nparams, 0);
  229. checklimit(ls, fs->nlocalvar, MAXPARAMS, "parameters");
  230. nparams = fs->nlocalvar; /* `self' could be there already */
  231. fs->f->numparams = nparams;
  232. fs->f->is_vararg = dots;
  233. if (!dots)
  234. luaK_deltastack(fs, nparams);
  235. else {
  236. luaK_deltastack(fs, nparams+1);
  237. add_localvar(ls, luaS_newfixed(ls->L, "arg"));
  238. }
  239. }
  240. static int getvarname (LexState *ls, expdesc *var) {
  241. switch (var->k) {
  242. case VGLOBAL:
  243. return var->u.index;
  244. case VLOCAL:
  245. return string_constant(ls->fs, ls->fs->localvar[var->u.index]);
  246. break;
  247. default:
  248. error_unexpected(ls); /* there is no `var name' */
  249. return 0; /* to avoid warnings */
  250. }
  251. }
  252. static void enterbreak (FuncState *fs, Breaklabel *bl) {
  253. bl->stacklevel = fs->stacklevel;
  254. bl->breaklist = NO_JUMP;
  255. bl->previous = fs->bl;
  256. fs->bl = bl;
  257. }
  258. static void leavebreak (FuncState *fs, Breaklabel *bl) {
  259. fs->bl = bl->previous;
  260. LUA_ASSERT(fs->L, bl->stacklevel == fs->stacklevel, "wrong levels");
  261. luaK_patchlist(fs, bl->breaklist, luaK_getlabel(fs));
  262. }
  263. static void func_onstack (LexState *ls, FuncState *func) {
  264. FuncState *fs = ls->fs;
  265. Proto *f = fs->f;
  266. int i;
  267. for (i=0; i<func->nupvalues; i++)
  268. luaK_tostack(ls, &func->upvalues[i], 1);
  269. luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *,
  270. constantEM, MAXARG_A);
  271. f->kproto[f->nkproto++] = func->f;
  272. luaK_code2(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues);
  273. }
  274. static void init_state (LexState *ls, FuncState *fs, TString *source) {
  275. lua_State *L = ls->L;
  276. Proto *f = luaF_newproto(ls->L);
  277. fs->prev = ls->fs; /* linked list of funcstates */
  278. fs->ls = ls;
  279. fs->L = ls->L;
  280. ls->fs = fs;
  281. fs->stacklevel = 0;
  282. fs->nlocalvar = 0;
  283. fs->nupvalues = 0;
  284. fs->lastsetline = 0;
  285. fs->bl = NULL;
  286. fs->f = f;
  287. f->source = source;
  288. fs->pc = 0;
  289. fs->lasttarget = 0;
  290. fs->jlt = NO_JUMP;
  291. f->code = NULL;
  292. f->maxstacksize = 0;
  293. f->numparams = 0; /* default for main chunk */
  294. f->is_vararg = 0; /* default for main chunk */
  295. fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */
  296. }
  297. static void close_func (LexState *ls) {
  298. lua_State *L = ls->L;
  299. FuncState *fs = ls->fs;
  300. Proto *f = fs->f;
  301. luaK_code0(fs, OP_END);
  302. luaK_getlabel(fs); /* close eventual list of pending jumps */
  303. luaM_reallocvector(L, f->code, fs->pc, Instruction);
  304. luaM_reallocvector(L, f->kstr, f->nkstr, TString *);
  305. luaM_reallocvector(L, f->knum, f->nknum, Number);
  306. luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
  307. if (fs->nvars != -1) { /* debug information? */
  308. luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
  309. luaM_reallocvector(L, f->locvars, fs->nvars, LocVar);
  310. }
  311. ls->fs = fs->prev;
  312. LUA_ASSERT(L, fs->bl == NULL, "wrong list end");
  313. }
  314. Proto *luaY_parser (lua_State *L, ZIO *z) {
  315. struct LexState lexstate;
  316. struct FuncState funcstate;
  317. luaX_setinput(L, &lexstate, z);
  318. init_state(&lexstate, &funcstate, luaS_new(L, zname(z)));
  319. next(&lexstate); /* read first token */
  320. chunk(&lexstate);
  321. if (lexstate.token != TK_EOS)
  322. luaK_error(&lexstate, "<eof> expected");
  323. close_func(&lexstate);
  324. LUA_ASSERT(L, funcstate.prev == NULL, "wrong list end");
  325. return funcstate.f;
  326. }
  327. /*============================================================*/
  328. /* GRAMAR RULES */
  329. /*============================================================*/
  330. static int explist1 (LexState *ls) {
  331. /* explist1 -> expr { ',' expr } */
  332. int n = 1; /* at least one expression */
  333. expdesc v;
  334. expr(ls, &v);
  335. while (ls->token == ',') {
  336. luaK_tostack(ls, &v, 1); /* gets only 1 value from previous expression */
  337. next(ls); /* skip comma */
  338. expr(ls, &v);
  339. n++;
  340. }
  341. luaK_tostack(ls, &v, 0);
  342. return n;
  343. }
  344. static int explist (LexState *ls) {
  345. /* explist -> [ explist1 ] */
  346. switch (ls->token) {
  347. case TK_ELSE: case TK_ELSEIF: case TK_END: case TK_UNTIL:
  348. case TK_EOS: case ';': case ')':
  349. return 0; /* empty list */
  350. default:
  351. return explist1(ls);
  352. }
  353. }
  354. static void funcargs (LexState *ls, int slf) {
  355. FuncState *fs = ls->fs;
  356. int slevel = fs->stacklevel - slf - 1; /* where is func in the stack */
  357. switch (ls->token) {
  358. case '(': { /* funcargs -> '(' explist ')' */
  359. int line = ls->linenumber;
  360. int nargs;
  361. next(ls);
  362. nargs = explist(ls);
  363. check_match(ls, ')', '(', line);
  364. #ifdef LUA_COMPAT_ARGRET
  365. if (nargs > 0) /* arg list is not empty? */
  366. luaK_setcallreturns(fs, 1); /* last call returns only 1 value */
  367. #endif
  368. break;
  369. }
  370. case '{': /* funcargs -> constructor */
  371. constructor(ls);
  372. break;
  373. case TK_STRING: /* funcargs -> STRING */
  374. code_string(ls, ls->seminfo.ts); /* must use `seminfo' before `next' */
  375. next(ls);
  376. break;
  377. default:
  378. luaK_error(ls, "function arguments expected");
  379. break;
  380. }
  381. fs->stacklevel = slevel; /* call will remove function and arguments */
  382. luaK_code2(fs, OP_CALL, slevel, MULT_RET);
  383. }
  384. static void var_or_func_tail (LexState *ls, expdesc *v) {
  385. for (;;) {
  386. switch (ls->token) {
  387. case '.': /* var_or_func_tail -> '.' NAME */
  388. next(ls);
  389. luaK_tostack(ls, v, 1); /* `v' must be on stack */
  390. luaK_kstr(ls, checkname(ls));
  391. v->k = VINDEXED;
  392. break;
  393. case '[': /* var_or_func_tail -> '[' exp1 ']' */
  394. next(ls);
  395. luaK_tostack(ls, v, 1); /* `v' must be on stack */
  396. v->k = VINDEXED;
  397. exp1(ls);
  398. check(ls, ']');
  399. break;
  400. case ':': { /* var_or_func_tail -> ':' NAME funcargs */
  401. int name;
  402. next(ls);
  403. name = checkname(ls);
  404. luaK_tostack(ls, v, 1); /* `v' must be on stack */
  405. luaK_code1(ls->fs, OP_PUSHSELF, name);
  406. funcargs(ls, 1);
  407. v->k = VEXP;
  408. v->u.l.t = v->u.l.f = NO_JUMP;
  409. break;
  410. }
  411. case '(': case TK_STRING: case '{': /* var_or_func_tail -> funcargs */
  412. luaK_tostack(ls, v, 1); /* `v' must be on stack */
  413. funcargs(ls, 0);
  414. v->k = VEXP;
  415. v->u.l.t = v->u.l.f = NO_JUMP;
  416. break;
  417. default: return; /* should be follow... */
  418. }
  419. }
  420. }
  421. static void var_or_func (LexState *ls, expdesc *v) {
  422. /* var_or_func -> ['%'] NAME var_or_func_tail */
  423. if (optional(ls, '%')) { /* upvalue? */
  424. pushupvalue(ls, str_checkname(ls));
  425. v->k = VEXP;
  426. v->u.l.t = v->u.l.f = NO_JUMP;
  427. }
  428. else /* variable name */
  429. singlevar(ls, str_checkname(ls), v, 0);
  430. var_or_func_tail(ls, v);
  431. }
  432. /*
  433. ** {======================================================================
  434. ** Rules for Constructors
  435. ** =======================================================================
  436. */
  437. static void recfield (LexState *ls) {
  438. /* recfield -> (NAME | '['exp1']') = exp1 */
  439. switch (ls->token) {
  440. case TK_NAME:
  441. luaK_kstr(ls, checkname(ls));
  442. break;
  443. case '[':
  444. next(ls);
  445. exp1(ls);
  446. check(ls, ']');
  447. break;
  448. default: luaK_error(ls, "<name> or `[' expected");
  449. }
  450. check(ls, '=');
  451. exp1(ls);
  452. }
  453. static int recfields (LexState *ls) {
  454. /* recfields -> { ',' recfield } [','] */
  455. FuncState *fs = ls->fs;
  456. int n = 1; /* one has been read before */
  457. int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */
  458. while (ls->token == ',') {
  459. next(ls);
  460. if (ls->token == ';' || ls->token == '}')
  461. break;
  462. recfield(ls);
  463. n++;
  464. if (++mod_n == RFIELDS_PER_FLUSH) {
  465. luaK_code1(fs, OP_SETMAP, RFIELDS_PER_FLUSH-1);
  466. mod_n = 0;
  467. }
  468. }
  469. if (mod_n)
  470. luaK_code1(fs, OP_SETMAP, mod_n-1);
  471. return n;
  472. }
  473. static int listfields (LexState *ls) {
  474. /* listfields -> { ',' exp1 } [','] */
  475. FuncState *fs = ls->fs;
  476. int n = 1; /* one has been read before */
  477. int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */
  478. while (ls->token == ',') {
  479. next(ls);
  480. if (ls->token == ';' || ls->token == '}')
  481. break;
  482. exp1(ls);
  483. n++;
  484. checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH,
  485. "items in a list initializer");
  486. if (++mod_n == LFIELDS_PER_FLUSH) {
  487. luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1);
  488. mod_n = 0;
  489. }
  490. }
  491. if (mod_n > 0)
  492. luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1);
  493. return n;
  494. }
  495. static void constructor_part (LexState *ls, Constdesc *cd) {
  496. switch (ls->token) {
  497. case ';': case '}': /* constructor_part -> empty */
  498. cd->n = 0;
  499. cd->k = ls->token;
  500. return;
  501. case TK_NAME: {
  502. expdesc v;
  503. expr(ls, &v);
  504. if (ls->token == '=') {
  505. luaK_kstr(ls, getvarname(ls, &v));
  506. next(ls); /* skip '=' */
  507. exp1(ls);
  508. cd->n = recfields(ls);
  509. cd->k = 1; /* record */
  510. }
  511. else {
  512. luaK_tostack(ls, &v, 0);
  513. cd->n = listfields(ls);
  514. cd->k = 0; /* list */
  515. }
  516. break;
  517. }
  518. case '[': /* constructor_part -> recfield recfields */
  519. recfield(ls);
  520. cd->n = recfields(ls);
  521. cd->k = 1; /* record */
  522. break;
  523. default: /* constructor_part -> exp1 listfields */
  524. exp1(ls);
  525. cd->n = listfields(ls);
  526. cd->k = 0; /* list */
  527. break;
  528. }
  529. }
  530. static void constructor (LexState *ls) {
  531. /* constructor -> '{' constructor_part [';' constructor_part] '}' */
  532. FuncState *fs = ls->fs;
  533. int line = ls->linenumber;
  534. int pc = luaK_code1(fs, OP_CREATETABLE, 0);
  535. int nelems;
  536. Constdesc cd;
  537. check(ls, '{');
  538. constructor_part(ls, &cd);
  539. nelems = cd.n;
  540. if (ls->token == ';') {
  541. Constdesc other_cd;
  542. next(ls);
  543. constructor_part(ls, &other_cd);
  544. if (cd.k == other_cd.k) /* repeated parts? */
  545. luaK_error(ls, "invalid constructor syntax");
  546. nelems += other_cd.n;
  547. }
  548. check_match(ls, '}', '{', line);
  549. /* set initial table size */
  550. SETARG_U(fs->f->code[pc], nelems);
  551. }
  552. /* }====================================================================== */
  553. /*
  554. ** {======================================================================
  555. ** Expression parsing
  556. ** =======================================================================
  557. */
  558. static void simpleexp (LexState *ls, expdesc *v) {
  559. FuncState *fs = ls->fs;
  560. setline(ls);
  561. switch (ls->token) {
  562. case TK_NUMBER: { /* simpleexp -> NUMBER */
  563. Number r = ls->seminfo.r;
  564. next(ls);
  565. luaK_number(fs, r);
  566. break;
  567. }
  568. case TK_STRING: /* simpleexp -> STRING */
  569. code_string(ls, ls->seminfo.ts); /* must use `seminfo' before `next' */
  570. next(ls);
  571. break;
  572. case TK_NIL: /* simpleexp -> NIL */
  573. luaK_adjuststack(fs, -1);
  574. next(ls);
  575. break;
  576. case '{': /* simpleexp -> constructor */
  577. constructor(ls);
  578. break;
  579. case TK_FUNCTION: /* simpleexp -> FUNCTION body */
  580. next(ls);
  581. body(ls, 0, ls->linenumber);
  582. break;
  583. case '(': /* simpleexp -> '(' expr ')' */
  584. next(ls);
  585. expr(ls, v);
  586. check(ls, ')');
  587. return;
  588. case TK_NAME: case '%':
  589. var_or_func(ls, v);
  590. return;
  591. default:
  592. luaK_error(ls, "<expression> expected");
  593. return;
  594. }
  595. v->k = VEXP;
  596. v->u.l.t = v->u.l.f = NO_JUMP;
  597. }
  598. static void exp1 (LexState *ls) {
  599. expdesc v;
  600. expr(ls, &v);
  601. luaK_tostack(ls, &v, 1);
  602. }
  603. /*
  604. ** gets priorities of an operator. Returns the priority to the left, and
  605. ** sets `rp' to the priority to the right.
  606. */
  607. static int get_priority (int op, int *rp) {
  608. switch (op) {
  609. case '^': *rp = 8; return 9; /* right associative */
  610. #define UNARY_PRIORITY 7
  611. case '*': case '/': *rp = 6; return 6;
  612. case '+': case '-': *rp = 5; return 5;
  613. case TK_CONCAT: *rp = 3; return 4; /* right associative (?) */
  614. case TK_EQ: case TK_NE: case '>': case '<': case TK_LE: case TK_GE:
  615. *rp = 2; return 2;
  616. case TK_AND: case TK_OR: *rp = 1; return 1;
  617. default: *rp = -1; return -1;
  618. }
  619. }
  620. /*
  621. ** subexpr -> (simplexep | (NOT | '-') subexpr) { binop subexpr }
  622. ** where `binop' is any binary operator with a priority higher than `limit'
  623. */
  624. static void subexpr (LexState *ls, expdesc *v, int limit) {
  625. int rp;
  626. if (ls->token == '-' || ls->token == TK_NOT) {
  627. int op = ls->token; /* operator */
  628. next(ls);
  629. subexpr(ls, v, UNARY_PRIORITY);
  630. luaK_prefix(ls, op, v);
  631. }
  632. else simpleexp(ls, v);
  633. /* expand while operators have priorities higher than `limit' */
  634. while (get_priority(ls->token, &rp) > limit) {
  635. expdesc v2;
  636. int op = ls->token; /* current operator (with priority == `rp') */
  637. next(ls);
  638. luaK_infix(ls, op, v);
  639. subexpr(ls, &v2, rp); /* read sub-expression with priority > `rp' */
  640. luaK_posfix(ls, op, v, &v2);
  641. }
  642. }
  643. static void expr (LexState *ls, expdesc *v) {
  644. subexpr(ls, v, -1);
  645. }
  646. /* }==================================================================== */
  647. /*
  648. ** {======================================================================
  649. ** Rules for Statements
  650. ** =======================================================================
  651. */
  652. static void block (LexState *ls) {
  653. /* block -> chunk */
  654. FuncState *fs = ls->fs;
  655. int nlocalvar = fs->nlocalvar;
  656. chunk(ls);
  657. luaK_adjuststack(fs, fs->nlocalvar - nlocalvar); /* remove local variables */
  658. for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
  659. luaI_unregisterlocalvar(ls, fs->lastsetline);
  660. }
  661. static int assignment (LexState *ls, expdesc *v, int nvars) {
  662. int left = 0;
  663. checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment");
  664. if (ls->token == ',') { /* assignment -> ',' NAME assignment */
  665. expdesc nv;
  666. next(ls);
  667. var_or_func(ls, &nv);
  668. if (nv.k == VEXP)
  669. luaK_error(ls, "syntax error");
  670. left = assignment(ls, &nv, nvars+1);
  671. }
  672. else { /* assignment -> '=' explist1 */
  673. int nexps;;
  674. if (ls->token != '=')
  675. error_unexpected(ls);
  676. next(ls);
  677. nexps = explist1(ls);
  678. adjust_mult_assign(ls, nvars, nexps);
  679. }
  680. if (v->k != VINDEXED)
  681. luaK_storevar(ls, v);
  682. else { /* there may be garbage between table-index and value */
  683. luaK_code2(ls->fs, OP_SETTABLE, left+nvars+2, 1);
  684. left += 2;
  685. }
  686. return left;
  687. }
  688. static void whilestat (LexState *ls, int line) {
  689. /* whilestat -> WHILE exp1 DO block END */
  690. FuncState *fs = ls->fs;
  691. int while_init = luaK_getlabel(fs);
  692. expdesc v;
  693. Breaklabel bl;
  694. enterbreak(fs, &bl);
  695. setline_and_next(ls); /* trace WHILE when looping */
  696. expr(ls, &v); /* read condition */
  697. luaK_goiftrue(fs, &v, 0);
  698. check(ls, TK_DO);
  699. block(ls);
  700. luaK_patchlist(fs, luaK_jump(fs), while_init);
  701. luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
  702. check_END(ls, TK_WHILE, line);
  703. leavebreak(fs, &bl);
  704. }
  705. static void repeatstat (LexState *ls, int line) {
  706. /* repeatstat -> REPEAT block UNTIL exp1 */
  707. FuncState *fs = ls->fs;
  708. int repeat_init = luaK_getlabel(fs);
  709. expdesc v;
  710. Breaklabel bl;
  711. enterbreak(fs, &bl);
  712. setline_and_next(ls); /* trace REPEAT when looping */
  713. block(ls);
  714. if (ls->token == TK_END) {
  715. luaK_patchlist(fs, luaK_jump(fs), repeat_init);
  716. next(ls);
  717. }
  718. else {
  719. check_match(ls, TK_UNTIL, TK_REPEAT, line);
  720. expr(ls, &v);
  721. luaK_goiftrue(fs, &v, 0);
  722. luaK_patchlist(fs, v.u.l.f, repeat_init);
  723. }
  724. leavebreak(fs, &bl);
  725. }
  726. static void test_and_bock (LexState *ls, expdesc *v) {
  727. setline_and_next(ls); /* skip IF or ELSEIF */
  728. expr(ls, v); /* cond */
  729. luaK_goiftrue(ls->fs, v, 0);
  730. setline(ls); /* to trace the THEN */
  731. check(ls, TK_THEN);
  732. block(ls); /* `then' part */
  733. }
  734. static void ifstat (LexState *ls, int line) {
  735. /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
  736. FuncState *fs = ls->fs;
  737. expdesc v;
  738. int escapelist = NO_JUMP;
  739. test_and_bock(ls, &v); /* IF cond THEN block */
  740. while (ls->token == TK_ELSEIF) {
  741. luaK_concat(fs, &escapelist, luaK_jump(fs));
  742. luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
  743. test_and_bock(ls, &v); /* ELSEIF cond THEN block */
  744. }
  745. if (ls->token == TK_ELSE) {
  746. luaK_concat(fs, &escapelist, luaK_jump(fs));
  747. luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
  748. setline_and_next(ls); /* skip ELSE */
  749. block(ls); /* `else' part */
  750. }
  751. else
  752. luaK_concat(fs, &escapelist, v.u.l.f);
  753. luaK_patchlist(fs, escapelist, luaK_getlabel(fs));
  754. check_END(ls, TK_IF, line);
  755. }
  756. static int localnamelist (LexState *ls) {
  757. /* localnamelist -> NAME {',' NAME} */
  758. int i = 1;
  759. store_localvar(ls, str_checkname(ls), 0);
  760. while (ls->token == ',') {
  761. next(ls);
  762. store_localvar(ls, str_checkname(ls), i++);
  763. }
  764. return i;
  765. }
  766. static int decinit (LexState *ls) {
  767. /* decinit -> ['=' explist1] */
  768. if (ls->token == '=') {
  769. next(ls);
  770. return explist1(ls);
  771. }
  772. else
  773. return 0; /* no initializations */
  774. }
  775. static void localstat (LexState *ls) {
  776. /* stat -> LOCAL localnamelist decinit */
  777. FuncState *fs = ls->fs;
  778. int nvars;
  779. int nexps;
  780. setline_and_next(ls); /* skip LOCAL */
  781. nvars = localnamelist(ls);
  782. nexps = decinit(ls);
  783. adjustlocalvars(ls, nvars, fs->lastsetline);
  784. adjust_mult_assign(ls, nvars, nexps);
  785. }
  786. static int funcname (LexState *ls, expdesc *v) {
  787. /* funcname -> NAME [':' NAME | '.' NAME] */
  788. int needself = 0;
  789. singlevar(ls, str_checkname(ls), v, 0);
  790. if (ls->token == ':' || ls->token == '.') {
  791. needself = (ls->token == ':');
  792. next(ls);
  793. luaK_tostack(ls, v, 1);
  794. luaK_kstr(ls, checkname(ls));
  795. v->k = VINDEXED;
  796. }
  797. return needself;
  798. }
  799. static void funcstat (LexState *ls, int line) {
  800. /* funcstat -> FUNCTION funcname body */
  801. int needself;
  802. expdesc v;
  803. if (ls->fs->prev) /* inside other function? */
  804. luaK_error(ls, "cannot nest this kind of function declaration");
  805. setline_and_next(ls); /* skip FUNCTION */
  806. needself = funcname(ls, &v);
  807. body(ls, needself, line);
  808. luaK_storevar(ls, &v);
  809. }
  810. static void namestat (LexState *ls) {
  811. /* stat -> func | ['%'] NAME assignment */
  812. FuncState *fs = ls->fs;
  813. expdesc v;
  814. setline(ls);
  815. var_or_func(ls, &v);
  816. if (v.k == VEXP) { /* stat -> func */
  817. if (!luaK_lastisopen(fs)) /* is just an upvalue? */
  818. luaK_error(ls, "syntax error");
  819. luaK_setcallreturns(fs, 0); /* call statement uses no results */
  820. }
  821. else { /* stat -> ['%'] NAME assignment */
  822. int left = assignment(ls, &v, 1);
  823. luaK_adjuststack(fs, left); /* remove eventual garbage left on stack */
  824. }
  825. }
  826. static int stat (LexState *ls) {
  827. int line = ls->linenumber; /* may be needed for error messages */
  828. switch (ls->token) {
  829. case TK_IF: /* stat -> ifstat */
  830. ifstat(ls, line);
  831. break;
  832. case TK_WHILE: /* stat -> whilestat */
  833. whilestat(ls, line);
  834. break;
  835. case TK_DO: { /* stat -> DO block END */
  836. setline_and_next(ls); /* skip DO */
  837. block(ls);
  838. check_END(ls, TK_DO, line);
  839. break;
  840. }
  841. case TK_REPEAT: /* stat -> repeatstat */
  842. repeatstat(ls, line);
  843. break;
  844. case TK_FUNCTION: /* stat -> funcstat */
  845. funcstat(ls, line);
  846. break;
  847. case TK_LOCAL: /* stat -> localstat */
  848. localstat(ls);
  849. break;
  850. case TK_NAME: case '%': /* stat -> namestat */
  851. namestat(ls);
  852. break;
  853. default:
  854. return 0; /* no statement */
  855. }
  856. return 1;
  857. }
  858. static void parlist (LexState *ls) {
  859. int nparams = 0;
  860. int dots = 0;
  861. switch (ls->token) {
  862. case TK_DOTS: /* parlist -> DOTS */
  863. next(ls);
  864. dots = 1;
  865. break;
  866. case TK_NAME: /* parlist, tailparlist -> NAME [',' tailparlist] */
  867. init:
  868. store_localvar(ls, str_checkname(ls), nparams++);
  869. if (ls->token == ',') {
  870. next(ls);
  871. switch (ls->token) {
  872. case TK_DOTS: /* tailparlist -> DOTS */
  873. next(ls);
  874. dots = 1;
  875. break;
  876. case TK_NAME: /* tailparlist -> NAME [',' tailparlist] */
  877. goto init;
  878. default: luaK_error(ls, "<name> or `...' expected");
  879. }
  880. }
  881. break;
  882. case ')': break; /* parlist -> empty */
  883. default: luaK_error(ls, "<name> or `...' expected");
  884. }
  885. code_args(ls, nparams, dots);
  886. }
  887. static void body (LexState *ls, int needself, int line) {
  888. /* body -> '(' parlist ')' chunk END */
  889. FuncState new_fs;
  890. init_state(ls, &new_fs, ls->fs->f->source);
  891. new_fs.f->lineDefined = line;
  892. check(ls, '(');
  893. if (needself)
  894. add_localvar(ls, luaS_newfixed(ls->L, "self"));
  895. parlist(ls);
  896. check(ls, ')');
  897. chunk(ls);
  898. check_END(ls, TK_FUNCTION, line);
  899. close_func(ls);
  900. func_onstack(ls, &new_fs);
  901. }
  902. static void ret (LexState *ls) {
  903. /* ret -> [RETURN explist sc | BREAK sc] */
  904. FuncState *fs = ls->fs;
  905. switch (ls->token) {
  906. case TK_RETURN: {
  907. setline_and_next(ls); /* skip RETURN */
  908. explist(ls);
  909. luaK_code1(fs, OP_RETURN, ls->fs->nlocalvar);
  910. fs->stacklevel = fs->nlocalvar; /* removes all temp values */
  911. optional(ls, ';');
  912. break;
  913. }
  914. case TK_BREAK: {
  915. Breaklabel *bl = fs->bl;
  916. int currentlevel = fs->stacklevel;
  917. if (bl == NULL)
  918. luaK_error(ls, "break not inside while or repeat loop");
  919. setline_and_next(ls); /* skip BREAK */
  920. luaK_adjuststack(fs, currentlevel - bl->stacklevel);
  921. luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
  922. optional(ls, ';');
  923. fs->stacklevel = currentlevel;
  924. break;
  925. }
  926. }
  927. }
  928. /* }====================================================================== */
  929. static void chunk (LexState *ls) {
  930. /* chunk -> { stat [;] } ret */
  931. while (stat(ls)) {
  932. LUA_ASSERT(ls->L, ls->fs->stacklevel == ls->fs->nlocalvar,
  933. "stack size != # local vars");
  934. optional(ls, ';');
  935. }
  936. ret(ls); /* optional return */
  937. }