lparser.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. ** $Id: lparser.c,v 1.39 1999/08/16 20:52:00 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. #include "lauxlib.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lopcodes.h"
  14. #include "lparser.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "lua.h"
  18. #include "luadebug.h"
  19. #include "lzio.h"
  20. /* size of a "normal" jump instruction: OpCode + 1 byte */
  21. #define JMPSIZE 2
  22. /* maximum number of local variables */
  23. #ifndef MAXLOCALS
  24. #define MAXLOCALS 200 /* arbitrary limit (<256) */
  25. #endif
  26. /* maximum number of upvalues */
  27. #ifndef MAXUPVALUES
  28. #define MAXUPVALUES 32 /* arbitrary limit (<256) */
  29. #endif
  30. /* maximum number of variables in the left side of an assignment */
  31. #ifndef MAXVARSLH
  32. #define MAXVARSLH 100 /* arbitrary limit (<255) */
  33. #endif
  34. /* maximum number of parameters in a function */
  35. #ifndef MAXPARAMS
  36. #define MAXPARAMS 100 /* arbitrary limit (<ZEROVARARG) */
  37. #endif
  38. /*
  39. ** Variable descriptor:
  40. ** must include an `exp' option because LL(1) cannot distinguish
  41. ** between variables, upvalues and function calls on first sight.
  42. */
  43. typedef enum {
  44. VGLOBAL, /* info is constant index of global name */
  45. VLOCAL, /* info is stack index */
  46. VDOT, /* info is constant index of index name */
  47. VINDEXED, /* no info (table and index are on the stack) */
  48. VEXP /* info is pc index of `nparam' of a call (or 0 if exp is closed) */
  49. } varkind;
  50. typedef struct vardesc {
  51. varkind k;
  52. int info;
  53. } vardesc;
  54. /*
  55. ** Expression List descriptor:
  56. ** tells number of expressions in the list,
  57. ** and, if last expression is open (a function call),
  58. ** where is its pc index of "nparam"
  59. */
  60. typedef struct listdesc {
  61. int n;
  62. int pc; /* 0 if last expression is closed */
  63. } listdesc;
  64. /*
  65. ** Constructors descriptor:
  66. ** `n' indicates number of elements, and `k' signals whether
  67. ** it is a list constructor (k = 0) or a record constructor (k = 1)
  68. ** or empty (k = ';' or '}')
  69. */
  70. typedef struct constdesc {
  71. int n;
  72. int k;
  73. } constdesc;
  74. /* state needed to generate code for a given function */
  75. typedef struct FuncState {
  76. TProtoFunc *f; /* current function header */
  77. struct FuncState *prev; /* enclosuring function */
  78. int pc; /* next position to code */
  79. int stacksize; /* number of values on activation register */
  80. int maxstacksize; /* maximum number of values on activation register */
  81. int nlocalvar; /* number of active local variables */
  82. int nupvalues; /* number of upvalues */
  83. int nvars; /* number of entries in f->locvars (-1 if no debug information) */
  84. int lastsetline; /* line where last SETLINE was issued */
  85. vardesc upvalues[MAXUPVALUES]; /* upvalues */
  86. TaggedString *localvar[MAXLOCALS]; /* store local variable names */
  87. } FuncState;
  88. /*
  89. ** prototypes for non-terminal functions
  90. */
  91. static int assignment (LexState *ls, vardesc *v, int nvars);
  92. static int cond (LexState *ls);
  93. static int funcname (LexState *ls, vardesc *v);
  94. static int funcparams (LexState *ls, int slf);
  95. static int listfields (LexState *ls);
  96. static int localnamelist (LexState *ls);
  97. static int optional (LexState *ls, int c);
  98. static int recfields (LexState *ls);
  99. static int stat (LexState *ls);
  100. static void block (LexState *ls);
  101. static void body (LexState *ls, int needself, int line);
  102. static void chunk (LexState *ls);
  103. static void constructor (LexState *ls);
  104. static void decinit (LexState *ls, listdesc *d);
  105. static void exp0 (LexState *ls, vardesc *v);
  106. static void exp1 (LexState *ls);
  107. static void exp2 (LexState *ls, vardesc *v);
  108. static void explist (LexState *ls, listdesc *e);
  109. static void explist1 (LexState *ls, listdesc *e);
  110. static void ifpart (LexState *ls, int line);
  111. static void parlist (LexState *ls);
  112. static void part (LexState *ls, constdesc *cd);
  113. static void recfield (LexState *ls);
  114. static void ret (LexState *ls);
  115. static void statlist (LexState *ls);
  116. static void var_or_func (LexState *ls, vardesc *v);
  117. static void var_or_func_tail (LexState *ls, vardesc *v);
  118. static void checklimit (LexState *ls, int val, int limit, const char *msg) {
  119. if (val > limit) {
  120. char buff[100];
  121. sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
  122. luaX_error(ls, buff);
  123. }
  124. }
  125. static void check_pc (FuncState *fs, int n) {
  126. luaM_growvector(fs->f->code, fs->pc, n, Byte, codeEM, MAX_INT);
  127. }
  128. static void code_byte (FuncState *fs, Byte c) {
  129. check_pc(fs, 1);
  130. fs->f->code[fs->pc++] = c;
  131. }
  132. static void deltastack (LexState *ls, int delta) {
  133. FuncState *fs = ls->fs;
  134. fs->stacksize += delta;
  135. if (fs->stacksize > fs->maxstacksize) {
  136. if (fs->stacksize > MAX_BYTE)
  137. luaX_error(ls, "function/expression too complex");
  138. fs->maxstacksize = fs->stacksize;
  139. }
  140. }
  141. static void code_oparg_at (LexState *ls, int pc, OpCode op,
  142. int arg, int delta) {
  143. Byte *code = ls->fs->f->code;
  144. deltastack(ls, delta);
  145. if (arg <= MAX_BYTE) {
  146. code[pc] = (Byte)op;
  147. code[pc+1] = (Byte)arg;
  148. }
  149. else if (arg > MAX_ARG)
  150. luaX_error(ls, "code too long");
  151. else { /* MAX_BYTE < arg < MAX_ARG */
  152. if (arg > MAX_WORD) {
  153. code[pc] = (Byte)LONGARG;
  154. code[pc+1] = (Byte)(arg>>16);
  155. pc += 2;
  156. }
  157. code[pc] = (Byte)(op-1); /* opcode for word argument */
  158. code[pc+1] = (Byte)((arg&0xFFFF)>>8);
  159. code[pc+2] = (Byte)(arg&0xFF);
  160. }
  161. }
  162. static int codesize (int arg) {
  163. if (arg <= MAX_BYTE) return 2; /* opcode + 1 byte */
  164. else if (arg <= MAX_WORD) return 3; /* opcode + 1 word (2 bytes) */
  165. else return 5; /* LONGARG + 1 byte + opcode + 1 word (2 bytes) */
  166. }
  167. static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
  168. int tomove = codesize(arg)-2;
  169. if (tomove > 0) { /* need to open space? */
  170. FuncState *fs = ls->fs;
  171. TProtoFunc *f = fs->f;
  172. check_pc(fs, tomove);
  173. luaO_memup(f->code+pc+tomove, f->code+pc, fs->pc-pc);
  174. fs->pc += tomove;
  175. }
  176. code_oparg_at(ls, pc, op, arg, 0);
  177. return tomove;
  178. }
  179. static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
  180. int size = codesize(arg);
  181. check_pc(ls->fs, size);
  182. code_oparg_at(ls, ls->fs->pc, op, arg, delta);
  183. ls->fs->pc += size;
  184. }
  185. static void code_opcode (LexState *ls, OpCode op, int delta) {
  186. deltastack(ls, delta);
  187. code_byte(ls->fs, (Byte)op);
  188. }
  189. static void code_constant (LexState *ls, int c) {
  190. code_oparg(ls, PUSHCONSTANT, c, 1);
  191. }
  192. static int next_constant (FuncState *fs) {
  193. TProtoFunc *f = fs->f;
  194. luaM_growvector(f->consts, f->nconsts, 1, TObject, constantEM, MAX_ARG);
  195. return f->nconsts++;
  196. }
  197. static int string_constant (FuncState *fs, TaggedString *s) {
  198. TProtoFunc *f = fs->f;
  199. int c = s->constindex;
  200. if (!(c < f->nconsts &&
  201. ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
  202. c = next_constant(fs);
  203. ttype(&f->consts[c]) = LUA_T_STRING;
  204. tsvalue(&f->consts[c]) = s;
  205. s->constindex = c; /* hint for next time */
  206. }
  207. return c;
  208. }
  209. static void code_string (LexState *ls, TaggedString *s) {
  210. code_constant(ls, string_constant(ls->fs, s));
  211. }
  212. #define LIM 20
  213. static int real_constant (FuncState *fs, real r) {
  214. /* check whether 'r' has appeared within the last LIM entries */
  215. TObject *cnt = fs->f->consts;
  216. int c = fs->f->nconsts;
  217. int lim = c < LIM ? 0 : c-LIM;
  218. while (--c >= lim) {
  219. if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
  220. return c;
  221. }
  222. /* not found; create a new entry */
  223. c = next_constant(fs);
  224. cnt = fs->f->consts; /* 'next_constant' may have reallocated this vector */
  225. ttype(&cnt[c]) = LUA_T_NUMBER;
  226. nvalue(&cnt[c]) = r;
  227. return c;
  228. }
  229. static void code_number (LexState *ls, real f) {
  230. real af = (f<0) ? -f : f;
  231. if (0 <= af && af <= (real)MAX_WORD && (int)af == af) {
  232. /* abs(f) has a short integer value */
  233. code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1);
  234. }
  235. else
  236. code_constant(ls, real_constant(ls->fs, f));
  237. }
  238. static void flush_record (LexState *ls, int n) {
  239. if (n > 0)
  240. code_oparg(ls, SETMAP, n-1, -2*n);
  241. }
  242. static void flush_list (LexState *ls, int m, int n) {
  243. if (n > 0) {
  244. code_oparg(ls, SETLIST, m, -n);
  245. code_byte(ls->fs, (Byte)n);
  246. }
  247. }
  248. static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname,
  249. int line) {
  250. if (fs->nvars != -1) { /* debug information? */
  251. TProtoFunc *f = fs->f;
  252. luaM_growvector(f->locvars, fs->nvars, 1, LocVar, "", MAX_INT);
  253. f->locvars[fs->nvars].varname = varname;
  254. f->locvars[fs->nvars].line = line;
  255. fs->nvars++;
  256. }
  257. }
  258. static void luaI_unregisterlocalvar (FuncState *fs, int line) {
  259. luaI_registerlocalvar(fs, NULL, line);
  260. }
  261. static void store_localvar (LexState *ls, TaggedString *name, int n) {
  262. FuncState *fs = ls->fs;
  263. checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
  264. fs->localvar[fs->nlocalvar+n] = name;
  265. luaI_registerlocalvar(fs, name, ls->linenumber);
  266. }
  267. static void add_localvar (LexState *ls, TaggedString *name) {
  268. store_localvar(ls, name, 0);
  269. ls->fs->nlocalvar++;
  270. }
  271. static void correctvarlines (LexState *ls, int nvars) {
  272. FuncState *fs = ls->fs;
  273. if (fs->nvars != -1) { /* debug information? */
  274. for (; nvars; nvars--) { /* correct line information */
  275. fs->f->locvars[fs->nvars-nvars].line = fs->lastsetline;
  276. }
  277. }
  278. }
  279. static int aux_localname (FuncState *fs, TaggedString *n) {
  280. int i;
  281. for (i=fs->nlocalvar-1; i >= 0; i--)
  282. if (n == fs->localvar[i]) return i; /* local var index */
  283. return -1; /* not found */
  284. }
  285. static void singlevar (LexState *ls, TaggedString *n, vardesc *var, int prev) {
  286. FuncState *fs = prev ? ls->fs->prev : ls->fs;
  287. int i = aux_localname(fs, n);
  288. if (i >= 0) { /* local value? */
  289. var->k = VLOCAL;
  290. var->info = i;
  291. }
  292. else {
  293. FuncState *level = fs;
  294. while ((level = level->prev) != NULL) /* check shadowing */
  295. if (aux_localname(level, n) >= 0)
  296. luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
  297. var->k = VGLOBAL;
  298. var->info = string_constant(fs, n);
  299. }
  300. }
  301. static int indexupvalue (LexState *ls, TaggedString *n) {
  302. FuncState *fs = ls->fs;
  303. vardesc v;
  304. int i;
  305. singlevar(ls, n, &v, 1);
  306. for (i=0; i<fs->nupvalues; i++) {
  307. if (fs->upvalues[i].k == v.k && fs->upvalues[i].info == v.info)
  308. return i;
  309. }
  310. /* new one */
  311. ++(fs->nupvalues);
  312. checklimit(ls, fs->nupvalues, MAXUPVALUES, "upvalues");
  313. fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */
  314. return i;
  315. }
  316. static void pushupvalue (LexState *ls, TaggedString *n) {
  317. if (ls->fs->prev == NULL)
  318. luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
  319. if (aux_localname(ls->fs, n) >= 0)
  320. luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
  321. code_oparg(ls, PUSHUPVALUE, indexupvalue(ls, n), 1);
  322. }
  323. static void check_debugline (LexState *ls) {
  324. if (L->debug && ls->linenumber != ls->fs->lastsetline) {
  325. code_oparg(ls, SETLINE, ls->linenumber, 0);
  326. ls->fs->lastsetline = ls->linenumber;
  327. }
  328. }
  329. static void adjuststack (LexState *ls, int n) {
  330. if (n > 0)
  331. code_oparg(ls, POP, n, -n);
  332. else if (n < 0)
  333. code_oparg(ls, PUSHNIL, (-n)-1, -n);
  334. }
  335. static void close_exp (LexState *ls, int pc, int nresults) {
  336. if (pc > 0) { /* expression is an open function call? */
  337. Byte *code = ls->fs->f->code;
  338. code[pc-1] = (Byte)nresults; /* set nresults */
  339. /* push results, pop params (at code[pc]) and function */
  340. deltastack(ls, nresults-(code[pc]+1));
  341. }
  342. #ifdef DEBUG
  343. code_oparg(ls, CHECKSTACK, ls->fs->stacksize, 0);
  344. #endif
  345. }
  346. static void adjust_mult_assign (LexState *ls, int nvars, listdesc *d) {
  347. int diff = d->n - nvars;
  348. if (d->pc == 0) { /* list is closed */
  349. /* push or pop eventual difference between list lengths */
  350. adjuststack(ls, diff);
  351. }
  352. else { /* must correct function call */
  353. diff--; /* do not count function call itself */
  354. if (diff <= 0) { /* more variables than values? */
  355. /* function call must provide extra values */
  356. close_exp(ls, d->pc, -diff);
  357. }
  358. else { /* more values than variables */
  359. close_exp(ls, d->pc, 0); /* call should provide no value */
  360. adjuststack(ls, diff); /* pop eventual extra values */
  361. }
  362. }
  363. }
  364. static void code_args (LexState *ls, int nparams, int dots) {
  365. FuncState *fs = ls->fs;
  366. fs->nlocalvar += nparams; /* `self' may already be there */
  367. checklimit(ls, fs->nlocalvar, MAXPARAMS, "parameters");
  368. nparams = fs->nlocalvar;
  369. if (!dots) {
  370. fs->f->code[1] = (Byte)nparams; /* fill-in arg information */
  371. deltastack(ls, nparams);
  372. }
  373. else {
  374. fs->f->code[1] = (Byte)(nparams+ZEROVARARG);
  375. deltastack(ls, nparams+1);
  376. add_localvar(ls, luaS_new("arg"));
  377. }
  378. }
  379. static void unloaddot (LexState *ls, vardesc *v) {
  380. /* dotted variables <a.x> must be stored like regular indexed vars <a["x"]> */
  381. if (v->k == VDOT) {
  382. code_constant(ls, v->info);
  383. v->k = VINDEXED;
  384. }
  385. }
  386. static void lua_pushvar (LexState *ls, vardesc *var) {
  387. switch (var->k) {
  388. case VLOCAL:
  389. code_oparg(ls, PUSHLOCAL, var->info, 1);
  390. break;
  391. case VGLOBAL:
  392. code_oparg(ls, GETGLOBAL, var->info, 1);
  393. break;
  394. case VDOT:
  395. code_oparg(ls, GETDOTTED, var->info, 0);
  396. break;
  397. case VINDEXED:
  398. code_opcode(ls, GETTABLE, -1);
  399. break;
  400. case VEXP:
  401. close_exp(ls, var->info, 1); /* function must return 1 value */
  402. break;
  403. }
  404. var->k = VEXP;
  405. var->info = 0; /* now this is a closed expression */
  406. }
  407. static void storevar (LexState *ls, const vardesc *var) {
  408. switch (var->k) {
  409. case VLOCAL:
  410. code_oparg(ls, SETLOCAL, var->info, -1);
  411. break;
  412. case VGLOBAL:
  413. code_oparg(ls, SETGLOBAL, var->info, -1);
  414. break;
  415. case VINDEXED:
  416. code_opcode(ls, SETTABLEPOP, -3);
  417. break;
  418. default:
  419. LUA_INTERNALERROR("invalid var kind to store");
  420. }
  421. }
  422. static int fix_jump (LexState *ls, int pc, OpCode op, int n) {
  423. /* jump is relative to position following jump instruction */
  424. return fix_opcode(ls, pc, op, n-(pc+JMPSIZE));
  425. }
  426. static void fix_upjmp (LexState *ls, OpCode op, int pos) {
  427. int delta = ls->fs->pc+JMPSIZE - pos; /* jump is relative */
  428. code_oparg(ls, op, delta+(codesize(delta)-2), 0);
  429. }
  430. static void codeIf (LexState *ls, int thenAdd, int elseAdd) {
  431. FuncState *fs = ls->fs;
  432. int elseinit = elseAdd+JMPSIZE;
  433. if (fs->pc == elseinit) { /* no else part? */
  434. fs->pc -= JMPSIZE;
  435. elseinit = fs->pc;
  436. }
  437. else
  438. elseinit += fix_jump(ls, elseAdd, JMP, fs->pc);
  439. fix_jump(ls, thenAdd, IFFJMP, elseinit);
  440. }
  441. static void func_onstack (LexState *ls, FuncState *func) {
  442. FuncState *fs = ls->fs;
  443. int i;
  444. int c = next_constant(fs);
  445. ttype(&fs->f->consts[c]) = LUA_T_PROTO;
  446. fs->f->consts[c].value.tf = func->f;
  447. if (func->nupvalues == 0)
  448. code_constant(ls, c);
  449. else {
  450. for (i=0; i<func->nupvalues; i++)
  451. lua_pushvar(ls, &func->upvalues[i]);
  452. deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */
  453. code_oparg(ls, CLOSURE, c, -func->nupvalues);
  454. code_byte(fs, (Byte)func->nupvalues);
  455. }
  456. }
  457. static void init_state (LexState *ls, FuncState *fs, TaggedString *source) {
  458. TProtoFunc *f = luaF_newproto();
  459. fs->prev = ls->fs; /* linked list of funcstates */
  460. ls->fs = fs;
  461. fs->stacksize = 0;
  462. fs->maxstacksize = 0;
  463. fs->nlocalvar = 0;
  464. fs->nupvalues = 0;
  465. fs->lastsetline = 0;
  466. fs->f = f;
  467. f->source = source;
  468. fs->pc = 0;
  469. f->code = NULL;
  470. fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */
  471. code_byte(fs, 0); /* to be filled with maxstacksize */
  472. code_byte(fs, 0); /* to be filled with arg information */
  473. /* push function (to avoid GC) */
  474. tfvalue(L->stack.top) = f; ttype(L->stack.top) = LUA_T_PROTO;
  475. incr_top;
  476. }
  477. static void close_func (LexState *ls) {
  478. FuncState *fs = ls->fs;
  479. TProtoFunc *f = fs->f;
  480. code_opcode(ls, ENDCODE, 0);
  481. f->code[0] = (Byte)fs->maxstacksize;
  482. luaM_reallocvector(f->code, fs->pc, Byte);
  483. luaM_reallocvector(f->consts, f->nconsts, TObject);
  484. if (fs->nvars != -1) { /* debug information? */
  485. luaI_registerlocalvar(fs, NULL, -1); /* flag end of vector */
  486. luaM_reallocvector(f->locvars, fs->nvars, LocVar);
  487. }
  488. ls->fs = fs->prev;
  489. L->stack.top--; /* pop function */
  490. }
  491. static const int expfollow [] = {ELSE, ELSEIF, THEN, IF, WHILE, REPEAT,
  492. DO, NAME, LOCAL, FUNCTION, END, UNTIL, RETURN, ')', ']', '}', ';',
  493. EOS, ',', 0};
  494. static int is_in (int tok, const int *toks) {
  495. const int *t;
  496. for (t=toks; *t; t++)
  497. if (*t == tok) return t-toks;
  498. return -1;
  499. }
  500. static void next (LexState *ls) {
  501. ls->token = luaX_lex(ls);
  502. }
  503. static void error_expected (LexState *ls, int token) {
  504. char buff[100], t[TOKEN_LEN];
  505. luaX_token2str(token, t);
  506. sprintf(buff, "`%.20s' expected", t);
  507. luaX_error(ls, buff);
  508. }
  509. static void error_unexpected (LexState *ls) {
  510. luaX_error(ls, "unexpected token");
  511. }
  512. static void error_unmatched (LexState *ls, int what, int who, int where) {
  513. if (where == ls->linenumber)
  514. error_expected(ls, what);
  515. else {
  516. char buff[100];
  517. char t_what[TOKEN_LEN], t_who[TOKEN_LEN];
  518. luaX_token2str(what, t_what);
  519. luaX_token2str(who, t_who);
  520. sprintf(buff, "`%.20s' expected (to close `%.20s' at line %d)",
  521. t_what, t_who, where);
  522. luaX_error(ls, buff);
  523. }
  524. }
  525. static void check (LexState *ls, int c) {
  526. if (ls->token != c)
  527. error_expected(ls, c);
  528. next(ls);
  529. }
  530. static void check_match (LexState *ls, int what, int who, int where) {
  531. if (ls->token != what)
  532. error_unmatched(ls, what, who, where);
  533. check_debugline(ls); /* to 'mark' the 'what' */
  534. next(ls);
  535. }
  536. static int checkname (LexState *ls) {
  537. int sc;
  538. if (ls->token != NAME)
  539. luaX_error(ls, "`NAME' expected");
  540. sc = string_constant(ls->fs, ls->seminfo.ts);
  541. next(ls);
  542. return sc;
  543. }
  544. static TaggedString *str_checkname (LexState *ls) {
  545. int i = checkname(ls); /* this call may realloc `f->consts' */
  546. return tsvalue(&ls->fs->f->consts[i]);
  547. }
  548. static int optional (LexState *ls, int c) {
  549. if (ls->token == c) {
  550. next(ls);
  551. return 1;
  552. }
  553. else return 0;
  554. }
  555. TProtoFunc *luaY_parser (ZIO *z) {
  556. struct LexState lexstate;
  557. struct FuncState funcstate;
  558. luaX_setinput(&lexstate, z);
  559. init_state(&lexstate, &funcstate, luaS_new(zname(z)));
  560. next(&lexstate); /* read first token */
  561. chunk(&lexstate);
  562. if (lexstate.token != EOS)
  563. luaX_error(&lexstate, "<eof> expected");
  564. close_func(&lexstate);
  565. return funcstate.f;
  566. }
  567. /*============================================================*/
  568. /* GRAMAR RULES */
  569. /*============================================================*/
  570. static void chunk (LexState *ls) {
  571. /* chunk -> statlist ret */
  572. statlist(ls);
  573. ret(ls);
  574. }
  575. static void statlist (LexState *ls) {
  576. /* statlist -> { stat [;] } */
  577. while (stat(ls)) {
  578. LUA_ASSERT(ls->fs->stacksize == ls->fs->nlocalvar,
  579. "stack size != # local vars");
  580. optional(ls, ';');
  581. }
  582. }
  583. static int stat (LexState *ls) {
  584. int line = ls->linenumber; /* may be needed for error messages */
  585. FuncState *fs = ls->fs;
  586. switch (ls->token) {
  587. case IF: /* stat -> IF ifpart END */
  588. ifpart(ls, line);
  589. return 1;
  590. case WHILE: { /* stat -> WHILE cond DO block END */
  591. TProtoFunc *f = fs->f;
  592. int while_init = fs->pc;
  593. int cond_end, cond_size;
  594. next(ls);
  595. cond_end = cond(ls);
  596. check(ls, DO);
  597. block(ls);
  598. check_match(ls, END, WHILE, line);
  599. cond_size = cond_end-while_init;
  600. check_pc(fs, cond_size);
  601. memcpy(f->code+fs->pc, f->code+while_init, cond_size);
  602. luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init);
  603. while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size);
  604. fix_upjmp(ls, IFTUPJMP, while_init);
  605. return 1;
  606. }
  607. case DO: { /* stat -> DO block END */
  608. next(ls);
  609. block(ls);
  610. check_match(ls, END, DO, line);
  611. return 1;
  612. }
  613. case REPEAT: { /* stat -> REPEAT block UNTIL exp1 */
  614. int repeat_init = fs->pc;
  615. next(ls);
  616. block(ls);
  617. check_match(ls, UNTIL, REPEAT, line);
  618. exp1(ls);
  619. fix_upjmp(ls, IFFUPJMP, repeat_init);
  620. deltastack(ls, -1); /* pops condition */
  621. return 1;
  622. }
  623. case FUNCTION: { /* stat -> FUNCTION funcname body */
  624. int needself;
  625. vardesc v;
  626. if (ls->fs->prev) /* inside other function? */
  627. return 0;
  628. check_debugline(ls);
  629. next(ls);
  630. needself = funcname(ls, &v);
  631. body(ls, needself, line);
  632. storevar(ls, &v);
  633. return 1;
  634. }
  635. case LOCAL: { /* stat -> LOCAL localnamelist decinit */
  636. listdesc d;
  637. int nvars;
  638. check_debugline(ls);
  639. next(ls);
  640. nvars = localnamelist(ls);
  641. decinit(ls, &d);
  642. fs->nlocalvar += nvars;
  643. correctvarlines(ls, nvars); /* vars will be alive only after decinit */
  644. adjust_mult_assign(ls, nvars, &d);
  645. return 1;
  646. }
  647. case NAME: case '%': { /* stat -> func | ['%'] NAME assignment */
  648. vardesc v;
  649. check_debugline(ls);
  650. var_or_func(ls, &v);
  651. if (v.k == VEXP) { /* stat -> func */
  652. if (v.info == 0) /* is just an upper value? */
  653. luaX_error(ls, "syntax error");
  654. close_exp(ls, v.info, 0);
  655. }
  656. else { /* stat -> ['%'] NAME assignment */
  657. int left = assignment(ls, &v, 1);
  658. adjuststack(ls, left); /* remove eventual garbage left on stack */
  659. }
  660. return 1;
  661. }
  662. case RETURN: case ';': case ELSE: case ELSEIF:
  663. case END: case UNTIL: case EOS: /* 'stat' follow */
  664. return 0;
  665. default:
  666. error_unexpected(ls);
  667. return 0; /* to avoid warnings */
  668. }
  669. }
  670. static int SaveWord (LexState *ls) {
  671. int res = ls->fs->pc;
  672. check_pc(ls->fs, JMPSIZE);
  673. ls->fs->pc += JMPSIZE; /* open space */
  674. return res;
  675. }
  676. static int SaveWordPop (LexState *ls) {
  677. deltastack(ls, -1); /* pop condition */
  678. return SaveWord(ls);
  679. }
  680. static int cond (LexState *ls) {
  681. /* cond -> exp1 */
  682. exp1(ls);
  683. return SaveWordPop(ls);
  684. }
  685. static void block (LexState *ls) {
  686. /* block -> chunk */
  687. FuncState *fs = ls->fs;
  688. int nlocalvar = fs->nlocalvar;
  689. chunk(ls);
  690. adjuststack(ls, fs->nlocalvar - nlocalvar);
  691. for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
  692. luaI_unregisterlocalvar(fs, fs->lastsetline);
  693. }
  694. static int funcname (LexState *ls, vardesc *v) {
  695. /* funcname -> NAME [':' NAME | '.' NAME] */
  696. int needself = 0;
  697. singlevar(ls, str_checkname(ls), v, 0);
  698. if (ls->token == ':' || ls->token == '.') {
  699. needself = (ls->token == ':');
  700. next(ls);
  701. lua_pushvar(ls, v);
  702. code_constant(ls, checkname(ls));
  703. v->k = VINDEXED;
  704. }
  705. return needself;
  706. }
  707. static void body (LexState *ls, int needself, int line) {
  708. /* body -> '(' parlist ')' chunk END */
  709. FuncState newfs;
  710. init_state(ls, &newfs, ls->fs->f->source);
  711. newfs.f->lineDefined = line;
  712. check(ls, '(');
  713. if (needself)
  714. add_localvar(ls, luaS_new("self"));
  715. parlist(ls);
  716. check(ls, ')');
  717. chunk(ls);
  718. check_match(ls, END, FUNCTION, line);
  719. close_func(ls);
  720. func_onstack(ls, &newfs);
  721. }
  722. static void ifpart (LexState *ls, int line) {
  723. /* ifpart -> cond THEN block [ELSE block | ELSEIF ifpart] */
  724. int c;
  725. int e;
  726. next(ls); /* skip IF or ELSEIF */
  727. c = cond(ls);
  728. check(ls, THEN);
  729. block(ls);
  730. e = SaveWord(ls);
  731. if (ls->token == ELSEIF)
  732. ifpart(ls, line);
  733. else {
  734. if (optional(ls, ELSE))
  735. block(ls);
  736. check_match(ls, END, IF, line);
  737. }
  738. codeIf(ls, c, e);
  739. }
  740. static void ret (LexState *ls) {
  741. /* ret -> [RETURN explist sc] */
  742. if (optional(ls, RETURN)) {
  743. listdesc e;
  744. check_debugline(ls);
  745. explist(ls, &e);
  746. if (e.pc > 0) { /* expression is an open function call? */
  747. Byte *code = ls->fs->f->code;
  748. code[e.pc-2] = TAILCALL; /* instead of a conventional CALL */
  749. code[e.pc-1] = (Byte)ls->fs->nlocalvar;
  750. }
  751. else
  752. code_oparg(ls, RETCODE, ls->fs->nlocalvar, 0);
  753. ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */
  754. optional(ls, ';');
  755. }
  756. }
  757. /*
  758. ** For parsing expressions, we use a classic stack with priorities.
  759. ** Each binary operator is represented by its index in "binop" + FIRSTBIN
  760. ** (EQ=2, NE=3, ... '^'=13). The unary NOT is 0 and UNMINUS is 1.
  761. */
  762. #define INDNOT 0
  763. #define INDMINUS 1
  764. /* code of first binary operator */
  765. #define FIRSTBIN 2
  766. /* code for power operator (last operator)
  767. ** '^' needs special treatment because it is right associative
  768. */
  769. #define POW 13
  770. static const int binop [] = {EQ, NE, '>', '<', LE, GE, CONC,
  771. '+', '-', '*', '/', '^', 0};
  772. static const int priority [POW+1] = {5, 5, 1, 1, 1, 1, 1, 1, 2, 3, 3, 4, 4, 6};
  773. static const OpCode opcodes [POW+1] = {NOTOP, MINUSOP, EQOP, NEQOP, GTOP,
  774. LTOP, LEOP, GEOP, CONCOP, ADDOP, SUBOP, MULTOP, DIVOP, POWOP};
  775. #define MAXOPS 20 /* op's stack size (arbitrary limit) */
  776. typedef struct stack_op {
  777. int ops[MAXOPS];
  778. int top;
  779. } stack_op;
  780. static void exp1 (LexState *ls) {
  781. vardesc v;
  782. exp0(ls, &v);
  783. lua_pushvar(ls, &v);
  784. if (is_in(ls->token, expfollow) < 0)
  785. luaX_error(ls, "ill-formed expression");
  786. }
  787. static void exp0 (LexState *ls, vardesc *v) {
  788. /* exp0 -> exp2 {(AND | OR) exp2} */
  789. exp2(ls, v);
  790. while (ls->token == AND || ls->token == OR) {
  791. int op = (ls->token == AND) ? ONFJMP : ONTJMP;
  792. int pc;
  793. lua_pushvar(ls, v);
  794. next(ls);
  795. pc = SaveWordPop(ls);
  796. exp2(ls, v);
  797. lua_pushvar(ls, v);
  798. fix_jump(ls, pc, op, ls->fs->pc);
  799. }
  800. }
  801. static void push (LexState *ls, stack_op *s, int op) {
  802. if (s->top >= MAXOPS)
  803. luaX_error(ls, "expression too complex");
  804. s->ops[s->top++] = op;
  805. }
  806. static void pop_to (LexState *ls, stack_op *s, int prio) {
  807. int op;
  808. while (s->top > 0 && priority[(op=s->ops[s->top-1])] >= prio) {
  809. code_opcode(ls, opcodes[op], op<FIRSTBIN?0:-1);
  810. s->top--;
  811. }
  812. }
  813. static void simpleexp (LexState *ls, vardesc *v, stack_op *s) {
  814. check_debugline(ls);
  815. switch (ls->token) {
  816. case NUMBER: { /* simpleexp -> NUMBER */
  817. real r = ls->seminfo.r;
  818. next(ls);
  819. /* dirty trick: check whether it is a -NUMBER not followed by '^' */
  820. /* (because the priority of '^' is higher than '-'...) */
  821. if (s->top > 0 && s->ops[s->top-1] == INDMINUS && ls->token != '^') {
  822. s->top--; /* remove '-' from stack */
  823. r = -r;
  824. }
  825. code_number(ls, r);
  826. break;
  827. }
  828. case STRING: /* simpleexp -> STRING */
  829. code_string(ls, ls->seminfo.ts); /* must use 'seminfo' before `next' */
  830. next(ls);
  831. break;
  832. case NIL: /* simpleexp -> NIL */
  833. adjuststack(ls, -1);
  834. next(ls);
  835. break;
  836. case '{': /* simpleexp -> constructor */
  837. constructor(ls);
  838. break;
  839. case FUNCTION: /* simpleexp -> FUNCTION body */
  840. next(ls);
  841. body(ls, 0, ls->linenumber);
  842. break;
  843. case '(': /* simpleexp -> '(' exp0 ')' */
  844. next(ls);
  845. exp0(ls, v);
  846. check(ls, ')');
  847. return;
  848. case NAME: case '%':
  849. var_or_func(ls, v);
  850. return;
  851. default:
  852. luaX_error(ls, "<expression> expected");
  853. return;
  854. }
  855. v->k = VEXP; v->info = 0;
  856. }
  857. static void prefixexp (LexState *ls, vardesc *v, stack_op *s) {
  858. /* prefixexp -> {NOT | '-'} simpleexp */
  859. while (ls->token == NOT || ls->token == '-') {
  860. push(ls, s, (ls->token==NOT)?INDNOT:INDMINUS);
  861. next(ls);
  862. }
  863. simpleexp(ls, v, s);
  864. }
  865. static void exp2 (LexState *ls, vardesc *v) {
  866. stack_op s;
  867. int op;
  868. s.top = 0;
  869. prefixexp(ls, v, &s);
  870. while ((op = is_in(ls->token, binop)) >= 0) {
  871. op += FIRSTBIN;
  872. lua_pushvar(ls, v);
  873. /* '^' is right associative, so must 'simulate' a higher priority */
  874. pop_to(ls, &s, (op == POW)?priority[op]+1:priority[op]);
  875. push(ls, &s, op);
  876. next(ls);
  877. prefixexp(ls, v, &s);
  878. lua_pushvar(ls, v);
  879. }
  880. if (s.top > 0) {
  881. lua_pushvar(ls, v);
  882. pop_to(ls, &s, 0);
  883. }
  884. }
  885. static void var_or_func (LexState *ls, vardesc *v) {
  886. /* var_or_func -> ['%'] NAME var_or_func_tail */
  887. if (optional(ls, '%')) { /* upvalue? */
  888. pushupvalue(ls, str_checkname(ls));
  889. v->k = VEXP;
  890. v->info = 0; /* closed expression */
  891. }
  892. else /* variable name */
  893. singlevar(ls, str_checkname(ls), v, 0);
  894. var_or_func_tail(ls, v);
  895. }
  896. static void var_or_func_tail (LexState *ls, vardesc *v) {
  897. for (;;) {
  898. switch (ls->token) {
  899. case '.': /* var_or_func_tail -> '.' NAME */
  900. next(ls);
  901. lua_pushvar(ls, v); /* `v' must be on stack */
  902. v->k = VDOT;
  903. v->info = checkname(ls);
  904. break;
  905. case '[': /* var_or_func_tail -> '[' exp1 ']' */
  906. next(ls);
  907. lua_pushvar(ls, v); /* `v' must be on stack */
  908. exp1(ls);
  909. check(ls, ']');
  910. v->k = VINDEXED;
  911. break;
  912. case ':': /* var_or_func_tail -> ':' NAME funcparams */
  913. next(ls);
  914. lua_pushvar(ls, v); /* `v' must be on stack */
  915. code_oparg(ls, PUSHSELF, checkname(ls), 1);
  916. v->k = VEXP;
  917. v->info = funcparams(ls, 1);
  918. break;
  919. case '(': case STRING: case '{': /* var_or_func_tail -> funcparams */
  920. lua_pushvar(ls, v); /* `v' must be on stack */
  921. v->k = VEXP;
  922. v->info = funcparams(ls, 0);
  923. break;
  924. default: return; /* should be follow... */
  925. }
  926. }
  927. }
  928. static int funcparams (LexState *ls, int slf) {
  929. FuncState *fs = ls->fs;
  930. int nparams = 1; /* in cases STRING and constructor */
  931. switch (ls->token) {
  932. case '(': { /* funcparams -> '(' explist ')' */
  933. int line = ls->linenumber;
  934. listdesc e;
  935. next(ls);
  936. explist(ls, &e);
  937. check_match(ls, ')', '(', line);
  938. close_exp(ls, e.pc, 1);
  939. nparams = e.n;
  940. break;
  941. }
  942. case '{': /* funcparams -> constructor */
  943. constructor(ls);
  944. break;
  945. case STRING: /* funcparams -> STRING */
  946. code_string(ls, ls->seminfo.ts); /* must use 'seminfo' before `next' */
  947. next(ls);
  948. break;
  949. default:
  950. luaX_error(ls, "function arguments expected");
  951. break;
  952. }
  953. code_byte(fs, CALL);
  954. code_byte(fs, 0); /* save space for nresult */
  955. code_byte(fs, (Byte)(nparams+slf));
  956. return fs->pc-1;
  957. }
  958. static void explist (LexState *ls, listdesc *d) {
  959. switch (ls->token) {
  960. case ELSE: case ELSEIF: case END: case UNTIL:
  961. case EOS: case ';': case ')':
  962. d->pc = 0;
  963. d->n = 0;
  964. break;
  965. default:
  966. explist1(ls, d);
  967. }
  968. }
  969. static void explist1 (LexState *ls, listdesc *d) {
  970. vardesc v;
  971. exp0(ls, &v);
  972. d->n = 1;
  973. while (ls->token == ',') {
  974. d->n++;
  975. lua_pushvar(ls, &v);
  976. next(ls);
  977. exp0(ls, &v);
  978. }
  979. if (v.k == VEXP)
  980. d->pc = v.info;
  981. else {
  982. lua_pushvar(ls, &v);
  983. d->pc = 0;
  984. }
  985. }
  986. static void parlist (LexState *ls) {
  987. int nparams = 0;
  988. int dots = 0;
  989. switch (ls->token) {
  990. case DOTS: /* parlist -> DOTS */
  991. next(ls);
  992. dots = 1;
  993. break;
  994. case NAME: /* parlist, tailparlist -> NAME [',' tailparlist] */
  995. init:
  996. store_localvar(ls, str_checkname(ls), nparams++);
  997. if (ls->token == ',') {
  998. next(ls);
  999. switch (ls->token) {
  1000. case DOTS: /* tailparlist -> DOTS */
  1001. next(ls);
  1002. dots = 1;
  1003. break;
  1004. case NAME: /* tailparlist -> NAME [',' tailparlist] */
  1005. goto init;
  1006. default: luaX_error(ls, "NAME or `...' expected");
  1007. }
  1008. }
  1009. break;
  1010. case ')': break; /* parlist -> empty */
  1011. default: luaX_error(ls, "NAME or `...' expected");
  1012. }
  1013. code_args(ls, nparams, dots);
  1014. }
  1015. static int localnamelist (LexState *ls) {
  1016. /* localnamelist -> NAME {',' NAME} */
  1017. int i = 1;
  1018. store_localvar(ls, str_checkname(ls), 0);
  1019. while (ls->token == ',') {
  1020. next(ls);
  1021. store_localvar(ls, str_checkname(ls), i++);
  1022. }
  1023. return i;
  1024. }
  1025. static void decinit (LexState *ls, listdesc *d) {
  1026. /* decinit -> ['=' explist1] */
  1027. if (ls->token == '=') {
  1028. next(ls);
  1029. explist1(ls, d);
  1030. }
  1031. else {
  1032. d->n = 0;
  1033. d->pc = 0;
  1034. }
  1035. }
  1036. static int assignment (LexState *ls, vardesc *v, int nvars) {
  1037. int left = 0;
  1038. checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment");
  1039. unloaddot(ls, v);
  1040. if (ls->token == ',') { /* assignment -> ',' NAME assignment */
  1041. vardesc nv;
  1042. next(ls);
  1043. var_or_func(ls, &nv);
  1044. if (nv.k == VEXP)
  1045. luaX_error(ls, "syntax error");
  1046. left = assignment(ls, &nv, nvars+1);
  1047. }
  1048. else { /* assignment -> '=' explist1 */
  1049. listdesc d;
  1050. check(ls, '=');
  1051. explist1(ls, &d);
  1052. adjust_mult_assign(ls, nvars, &d);
  1053. }
  1054. if (v->k != VINDEXED || left+(nvars-1) == 0) {
  1055. /* global/local var or indexed var without values in between */
  1056. storevar(ls, v);
  1057. }
  1058. else { /* indexed var with values in between*/
  1059. code_oparg(ls, SETTABLE, left+(nvars-1), -1);
  1060. left += 2; /* table&index are not popped, because they aren't on top */
  1061. }
  1062. return left;
  1063. }
  1064. static void constructor (LexState *ls) {
  1065. /* constructor -> '{' part [';' part] '}' */
  1066. int line = ls->linenumber;
  1067. int pc = SaveWord(ls);
  1068. int nelems;
  1069. constdesc cd;
  1070. deltastack(ls, 1);
  1071. check(ls, '{');
  1072. part(ls, &cd);
  1073. nelems = cd.n;
  1074. if (ls->token == ';') {
  1075. constdesc other_cd;
  1076. next(ls);
  1077. part(ls, &other_cd);
  1078. if (cd.k == other_cd.k) /* repeated parts? */
  1079. luaX_error(ls, "invalid constructor syntax");
  1080. nelems += other_cd.n;
  1081. }
  1082. check_match(ls, '}', '{', line);
  1083. fix_opcode(ls, pc, CREATEARRAY, nelems);
  1084. }
  1085. static void part (LexState *ls, constdesc *cd) {
  1086. switch (ls->token) {
  1087. case ';': case '}': /* part -> empty */
  1088. cd->n = 0;
  1089. cd->k = ls->token;
  1090. return;
  1091. case NAME: {
  1092. vardesc v;
  1093. exp0(ls, &v);
  1094. if (ls->token == '=') {
  1095. switch (v.k) {
  1096. case VGLOBAL:
  1097. code_constant(ls, v.info);
  1098. break;
  1099. case VLOCAL:
  1100. code_string(ls, ls->fs->localvar[v.info]);
  1101. break;
  1102. default:
  1103. error_unexpected(ls);
  1104. }
  1105. next(ls);
  1106. exp1(ls);
  1107. cd->n = recfields(ls);
  1108. cd->k = 1; /* record */
  1109. }
  1110. else {
  1111. lua_pushvar(ls, &v);
  1112. cd->n = listfields(ls);
  1113. cd->k = 0; /* list */
  1114. }
  1115. break;
  1116. }
  1117. case '[': /* part -> recfield recfields */
  1118. recfield(ls);
  1119. cd->n = recfields(ls);
  1120. cd->k = 1; /* record */
  1121. break;
  1122. default: /* part -> exp1 listfields */
  1123. exp1(ls);
  1124. cd->n = listfields(ls);
  1125. cd->k = 0; /* list */
  1126. break;
  1127. }
  1128. }
  1129. static int recfields (LexState *ls) {
  1130. /* recfields -> { ',' recfield } [','] */
  1131. int n = 1; /* one has been read before */
  1132. while (ls->token == ',') {
  1133. next(ls);
  1134. if (ls->token == ';' || ls->token == '}')
  1135. break;
  1136. recfield(ls);
  1137. n++;
  1138. if (n%RFIELDS_PER_FLUSH == 0)
  1139. flush_record(ls, RFIELDS_PER_FLUSH);
  1140. }
  1141. flush_record(ls, n%RFIELDS_PER_FLUSH);
  1142. return n;
  1143. }
  1144. static int listfields (LexState *ls) {
  1145. /* listfields -> { ',' exp1 } [','] */
  1146. int n = 1; /* one has been read before */
  1147. while (ls->token == ',') {
  1148. next(ls);
  1149. if (ls->token == ';' || ls->token == '}')
  1150. break;
  1151. exp1(ls);
  1152. n++;
  1153. if (n%LFIELDS_PER_FLUSH == 0)
  1154. flush_list(ls, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
  1155. }
  1156. flush_list(ls, n/LFIELDS_PER_FLUSH, n%LFIELDS_PER_FLUSH);
  1157. return n;
  1158. }
  1159. static void recfield (LexState *ls) {
  1160. /* recfield -> (NAME | '['exp1']') = exp1 */
  1161. switch (ls->token) {
  1162. case NAME:
  1163. code_constant(ls, checkname(ls));
  1164. break;
  1165. case '[':
  1166. next(ls);
  1167. exp1(ls);
  1168. check(ls, ']');
  1169. break;
  1170. default: luaX_error(ls, "NAME or `[' expected");
  1171. }
  1172. check(ls, '=');
  1173. exp1(ls);
  1174. }