lparser.c 35 KB

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