lua.stx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 3.50 1997/07/31 20:46:59 roberto Exp roberto $";
  3. #include <stdlib.h>
  4. #include "luadebug.h"
  5. #include "luamem.h"
  6. #include "lex.h"
  7. #include "opcode.h"
  8. #include "hash.h"
  9. #include "inout.h"
  10. #include "tree.h"
  11. #include "table.h"
  12. #include "lua.h"
  13. #include "func.h"
  14. /* to avoid warnings generated by yacc */
  15. int yyparse (void);
  16. #define malloc luaI_malloc
  17. #define realloc luaI_realloc
  18. #define free luaI_free
  19. #ifndef LISTING
  20. #define LISTING 0
  21. #endif
  22. #ifndef CODE_BLOCK
  23. #define CODE_BLOCK 1000
  24. #endif
  25. #define MAXLOCALS 32
  26. /* state needed to generate code for a given function */
  27. struct State {
  28. TFunc *f; /* current function header */
  29. int codesize;
  30. int pc; /* next position to code */
  31. TaggedString *localvar[MAXLOCALS]; /* store local variable names */
  32. int nlocalvar; /* number of active local variables */
  33. int maxconsts; /* size of consts vector */
  34. int nvars; /* total number of local variables (for debugging information) */
  35. int maxvars; /* = -1 if no debug information */
  36. } stateMain, stateFunc, *currState;
  37. #define MAXVAR 32
  38. static Long varbuffer[MAXVAR]; /* variables in an assignment list;
  39. it's long to store negative Word values */
  40. static int nvarbuffer=0; /* number of variables at a list */
  41. int lua_debug = 0;
  42. /* Internal functions */
  43. static void yyerror (char *s)
  44. {
  45. luaI_syntaxerror(s);
  46. }
  47. static void check_space (int i)
  48. {
  49. if (currState->pc+i >= currState->codesize)
  50. currState->codesize = growvector(&currState->f->code, currState->codesize,
  51. Byte, codeEM, MAX_INT);
  52. }
  53. static void code_byte (Byte c)
  54. {
  55. check_space(1);
  56. currState->f->code[currState->pc++] = c;
  57. }
  58. static void code_word_at (int pc, int n)
  59. {
  60. Word w = n;
  61. if (w != n)
  62. yyerror("block too big");
  63. currState->f->code[pc] = n&0xFF;
  64. currState->f->code[pc+1] = n>>8;
  65. }
  66. static void code_word (int n)
  67. {
  68. code_byte(n&0xFF);
  69. code_byte(n>>8);
  70. }
  71. static void code_constant (int c)
  72. {
  73. if (c <= 255) {
  74. code_byte(PUSHCONSTANTB);
  75. code_byte(c);
  76. }
  77. else {
  78. code_byte(PUSHCONSTANT);
  79. code_word(c);
  80. }
  81. }
  82. static int next_constant (void)
  83. {
  84. TFunc *f = currState->f;
  85. if (f->nconsts >= currState->maxconsts) {
  86. currState->maxconsts =
  87. growvector(&f->consts, currState->maxconsts, TObject,
  88. constantEM, MAX_WORD);
  89. }
  90. return f->nconsts++;
  91. }
  92. static int string_constant (TaggedString *s)
  93. {
  94. TFunc *f = currState->f;
  95. int c = s->u.s.constindex;
  96. if (!(0 <= c && c < f->nconsts &&
  97. ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
  98. c = next_constant();
  99. ttype(&f->consts[c]) = LUA_T_STRING;
  100. tsvalue(&f->consts[c]) = s;
  101. s->u.s.constindex = c; /* hint for next time */
  102. }
  103. luaI_releasestring(s);
  104. return c;
  105. }
  106. static void code_string (TaggedString *s)
  107. {
  108. code_constant(string_constant(s));
  109. }
  110. #define LIM 10
  111. static int real_constant (real r)
  112. {
  113. /* check whether 'r' has appeared within the last LIM entries */
  114. TObject *cnt = currState->f->consts;
  115. int c = currState->f->nconsts;
  116. int lim = c < LIM ? 0 : c-LIM;
  117. while (--c >= lim) {
  118. if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
  119. return c;
  120. }
  121. /* not found; create a new entry */
  122. c = next_constant();
  123. cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
  124. ttype(&cnt[c]) = LUA_T_NUMBER;
  125. nvalue(&cnt[c]) = r;
  126. return c;
  127. }
  128. static void code_number (real f)
  129. {
  130. Word i;
  131. if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) {
  132. /* f has an (short) integer value */
  133. if (i <= 2) code_byte(PUSH0 + i);
  134. else if (i <= 255) {
  135. code_byte(PUSHBYTE);
  136. code_byte(i);
  137. }
  138. else {
  139. code_byte(PUSHWORD);
  140. code_word(i);
  141. }
  142. }
  143. else
  144. code_constant(real_constant(f));
  145. }
  146. static void flush_record (int n)
  147. {
  148. if (n == 0) return;
  149. code_byte(STOREMAP);
  150. code_byte(n);
  151. }
  152. static void flush_list (int m, int n)
  153. {
  154. if (n == 0) return;
  155. if (m == 0)
  156. code_byte(STORELIST0);
  157. else
  158. if (m < 255)
  159. {
  160. code_byte(STORELIST);
  161. code_byte(m);
  162. }
  163. else
  164. yyerror ("list constructor too long");
  165. code_byte(n);
  166. }
  167. static void luaI_registerlocalvar (TaggedString *varname, int line)
  168. {
  169. if (currState->maxvars != -1) { /* debug information? */
  170. if (currState->nvars >= currState->maxvars)
  171. currState->maxvars = growvector(&currState->f->locvars,
  172. currState->maxvars, LocVar, "", MAX_WORD);
  173. currState->f->locvars[currState->nvars].varname = varname;
  174. currState->f->locvars[currState->nvars].line = line;
  175. currState->nvars++;
  176. }
  177. }
  178. static void luaI_unregisterlocalvar (int line)
  179. {
  180. luaI_registerlocalvar(NULL, line);
  181. }
  182. static void store_localvar (TaggedString *name, int n)
  183. {
  184. luaI_fixstring(name); /* local var names cannot be GC */
  185. if (currState->nlocalvar+n < MAXLOCALS)
  186. currState->localvar[currState->nlocalvar+n] = name;
  187. else
  188. yyerror ("too many local variables");
  189. luaI_registerlocalvar(name, lua_linenumber);
  190. }
  191. static void add_localvar (TaggedString *name)
  192. {
  193. store_localvar(name, 0);
  194. currState->nlocalvar++;
  195. }
  196. static void add_varbuffer (Long var)
  197. {
  198. if (nvarbuffer < MAXVAR)
  199. varbuffer[nvarbuffer++] = var;
  200. else
  201. yyerror ("variable buffer overflow");
  202. }
  203. /*
  204. ** Search a local name and if find return its index. If do not find return -1
  205. */
  206. static int lua_localname (TaggedString *n)
  207. {
  208. int i;
  209. for (i=currState->nlocalvar-1; i >= 0; i--)
  210. if (n == currState->localvar[i]) return i; /* local var */
  211. return -1; /* global var */
  212. }
  213. /*
  214. ** Push a variable given a number. If number is positive, push global variable
  215. ** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
  216. ** Otherwise, if zero, push indexed variable (record).
  217. */
  218. static void lua_pushvar (Long number)
  219. {
  220. if (number > 0) /* global var */
  221. {
  222. code_byte(PUSHGLOBAL);
  223. code_word(number-1);
  224. }
  225. else if (number < 0) /* local var */
  226. {
  227. number = (-number) - 1;
  228. if (number < 10) code_byte(PUSHLOCAL0 + number);
  229. else
  230. {
  231. code_byte(PUSHLOCAL);
  232. code_byte(number);
  233. }
  234. }
  235. else
  236. {
  237. code_byte(PUSHINDEXED);
  238. }
  239. }
  240. static void lua_codeadjust (int n)
  241. {
  242. n += currState->nlocalvar;
  243. if (n == 0)
  244. code_byte(ADJUST0);
  245. else {
  246. code_byte(ADJUST);
  247. code_byte(n);
  248. }
  249. }
  250. void luaI_codedebugline (int line)
  251. {
  252. static int lastline = 0;
  253. if (lua_debug && line != lastline)
  254. {
  255. code_byte(SETLINE);
  256. code_word(line);
  257. lastline = line;
  258. }
  259. }
  260. static int adjust_functioncall (Long exp, int i)
  261. {
  262. if (exp <= 0)
  263. return -exp; /* exp is -list length */
  264. else {
  265. int temp = currState->f->code[exp];
  266. currState->f->code[exp] = i;
  267. return temp+i;
  268. }
  269. }
  270. static void adjust_mult_assign (int vars, Long exps, int temps)
  271. {
  272. if (exps > 0) { /* must correct function call */
  273. int diff = vars - currState->f->code[exps];
  274. if (diff >= 0)
  275. adjust_functioncall(exps, diff);
  276. else {
  277. adjust_functioncall(exps, 0);
  278. lua_codeadjust(temps);
  279. }
  280. }
  281. else if (vars != -exps)
  282. lua_codeadjust(temps);
  283. }
  284. static int close_parlist (int dots)
  285. {
  286. if (!dots)
  287. lua_codeadjust(0);
  288. else {
  289. code_byte(VARARGS);
  290. code_byte(currState->nlocalvar);
  291. add_localvar(luaI_createstring("arg"));
  292. }
  293. return lua_linenumber;
  294. }
  295. static void storesinglevar (Long v)
  296. {
  297. if (v > 0) /* global var */
  298. {
  299. code_byte(STOREGLOBAL);
  300. code_word(v-1);
  301. }
  302. else if (v < 0) /* local var */
  303. {
  304. int number = (-v) - 1;
  305. if (number < 10) code_byte(STORELOCAL0 + number);
  306. else
  307. {
  308. code_byte(STORELOCAL);
  309. code_byte(number);
  310. }
  311. }
  312. else
  313. code_byte(STOREINDEXED0);
  314. }
  315. static void lua_codestore (int i)
  316. {
  317. if (varbuffer[i] != 0) /* global or local var */
  318. storesinglevar(varbuffer[i]);
  319. else /* indexed var */
  320. {
  321. int j;
  322. int upper=0; /* number of indexed variables upper */
  323. int param; /* number of itens until indexed expression */
  324. for (j=i+1; j <nvarbuffer; j++)
  325. if (varbuffer[j] == 0) upper++;
  326. param = upper*2 + i;
  327. if (param == 0)
  328. code_byte(STOREINDEXED0);
  329. else
  330. {
  331. code_byte(STOREINDEXED);
  332. code_byte(param);
  333. }
  334. }
  335. }
  336. static void codeIf (Long thenAdd, Long elseAdd)
  337. {
  338. Long elseinit = elseAdd+sizeof(Word)+1;
  339. if (currState->pc == elseinit) { /* no else */
  340. currState->pc -= sizeof(Word)+1;
  341. elseinit = currState->pc;
  342. }
  343. else {
  344. currState->f->code[elseAdd] = JMP;
  345. code_word_at(elseAdd+1, currState->pc-elseinit);
  346. }
  347. currState->f->code[thenAdd] = IFFJMP;
  348. code_word_at(thenAdd+1, elseinit-(thenAdd+sizeof(Word)+1));
  349. }
  350. static void code_shortcircuit (int pc, Byte jmp)
  351. {
  352. currState->f->code[pc] = jmp;
  353. code_word_at(pc+1, currState->pc - (pc + sizeof(Word)+1));
  354. }
  355. static void init_state (TFunc *f)
  356. {
  357. currState->nlocalvar = 0;
  358. currState->f = f;
  359. currState->pc = 0;
  360. currState->codesize = CODE_BLOCK;
  361. f->code = newvector(CODE_BLOCK, Byte);
  362. currState->maxconsts = 0;
  363. if (lua_debug) {
  364. currState->nvars = 0;
  365. currState->maxvars = 0;
  366. }
  367. else
  368. currState->maxvars = -1; /* flag no debug information */
  369. }
  370. static void init_func (Long v)
  371. {
  372. TFunc *f = new(TFunc);
  373. int c = next_constant();
  374. ttype(&currState->f->consts[c]) = LUA_T_FUNCTION;
  375. currState->f->consts[c].value.tf = f;
  376. code_constant(c);
  377. storesinglevar(v);
  378. currState = &stateFunc;
  379. luaI_initTFunc(f);
  380. init_state(f);
  381. luaI_codedebugline(lua_linenumber);
  382. }
  383. static void codereturn (void)
  384. {
  385. if (currState->nlocalvar == 0)
  386. code_byte(RETCODE0);
  387. else
  388. {
  389. code_byte(RETCODE);
  390. code_byte(currState->nlocalvar);
  391. }
  392. }
  393. static void close_func (void)
  394. {
  395. codereturn();
  396. code_byte(ENDCODE);
  397. currState->f->code = shrinkvector(currState->f->code, currState->pc, Byte);
  398. currState->f->consts = shrinkvector(currState->f->consts,
  399. currState->f->nconsts, TObject);
  400. if (currState->maxvars != -1) { /* debug information? */
  401. luaI_registerlocalvar(NULL, -1); /* flag end of vector */
  402. currState->f->locvars = shrinkvector(currState->f->locvars,
  403. currState->nvars, LocVar);
  404. }
  405. }
  406. /*
  407. ** Parse LUA code.
  408. */
  409. void lua_parse (TFunc *tf)
  410. {
  411. currState = &stateMain;
  412. init_state(tf);
  413. if (yyparse ()) lua_error("parse error");
  414. currState = &stateMain;
  415. close_func();
  416. }
  417. %}
  418. %union
  419. {
  420. int vInt;
  421. real vReal;
  422. char *pChar;
  423. Long vLong;
  424. TaggedString *pTStr;
  425. }
  426. %start chunk
  427. %token WRONGTOKEN
  428. %token NIL
  429. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  430. %token RETURN
  431. %token LOCAL
  432. %token FUNCTION
  433. %token DOTS
  434. %token <vReal> NUMBER
  435. %token <pTStr> NAME STRING
  436. %type <vLong> PrepJump
  437. %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
  438. counter (which has list length); if <= 0, -list lenght */
  439. %type <vLong> functioncall, expr /* if != 0, points to function return
  440. counter */
  441. %type <vInt> varlist1, funcParams, funcvalue
  442. %type <vInt> fieldlist, localdeclist, decinit
  443. %type <vInt> ffieldlist, ffieldlist1, semicolonpart
  444. %type <vInt> lfieldlist, lfieldlist1
  445. %type <vInt> parlist, parlist1, par
  446. %type <vLong> var, singlevar
  447. %left AND OR
  448. %left EQ NE '>' '<' LE GE
  449. %left CONC
  450. %left '+' '-'
  451. %left '*' '/'
  452. %left UNARY NOT
  453. %right '^'
  454. %% /* beginning of rules section */
  455. chunk : chunklist ret ;
  456. chunklist : /* empty */
  457. | chunklist stat sc
  458. | chunklist function
  459. ;
  460. function : FUNCTION funcname body
  461. ;
  462. funcname : var { init_func($1); }
  463. | varexp ':' NAME
  464. {
  465. code_string($3);
  466. init_func(0); /* indexed variable */
  467. add_localvar(luaI_createstring("self"));
  468. }
  469. ;
  470. body : '(' parlist ')' block END
  471. {
  472. close_func();
  473. currState->f->lineDefined = $2;
  474. currState = &stateMain; /* change back to main code */
  475. }
  476. ;
  477. statlist : /* empty */
  478. | statlist stat sc
  479. ;
  480. sc : /* empty */ | ';' ;
  481. stat : IF expr1 THEN PrepJump block PrepJump elsepart END
  482. { codeIf($4, $6); }
  483. | WHILE {$<vLong>$=currState->pc;} expr1 DO PrepJump block PrepJump END
  484. {
  485. currState->f->code[$5] = IFFJMP;
  486. code_word_at($5+1, currState->pc - ($5+sizeof(Word)+1));
  487. currState->f->code[$7] = UPJMP;
  488. code_word_at($7+1, currState->pc - ($<vLong>2));
  489. }
  490. | REPEAT {$<vLong>$=currState->pc;} block UNTIL expr1 PrepJump
  491. {
  492. currState->f->code[$6] = IFFUPJMP;
  493. code_word_at($6+1, currState->pc - ($<vLong>2));
  494. }
  495. | varlist1 '=' exprlist1
  496. {
  497. {
  498. int i;
  499. adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
  500. for (i=nvarbuffer-1; i>=0; i--)
  501. lua_codestore(i);
  502. if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
  503. lua_codeadjust(0);
  504. }
  505. }
  506. | functioncall {;}
  507. | LOCAL localdeclist decinit
  508. { currState->nlocalvar += $2;
  509. adjust_mult_assign($2, $3, 0);
  510. }
  511. ;
  512. elsepart : /* empty */
  513. | ELSE block
  514. | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
  515. { codeIf($4, $6); }
  516. ;
  517. block : {$<vInt>$ = currState->nlocalvar;} statlist ret
  518. {
  519. if (currState->nlocalvar != $<vInt>1) {
  520. for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--)
  521. luaI_unregisterlocalvar(lua_linenumber);
  522. lua_codeadjust(0);
  523. }
  524. }
  525. ;
  526. ret : /* empty */
  527. | RETURN exprlist sc
  528. {
  529. adjust_functioncall($2, MULT_RET);
  530. codereturn();
  531. }
  532. ;
  533. PrepJump : /* empty */
  534. {
  535. $$ = currState->pc;
  536. code_byte(0); /* open space */
  537. code_word(0);
  538. }
  539. ;
  540. expr1 : expr { adjust_functioncall($1, 1); }
  541. ;
  542. expr : '(' expr ')' { $$ = $2; }
  543. | expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
  544. | expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
  545. | expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
  546. | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
  547. | expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
  548. | expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
  549. | expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
  550. | expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
  551. | expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
  552. | expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
  553. | expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
  554. | expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
  555. | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
  556. | table { $$ = 0; }
  557. | varexp { $$ = 0;}
  558. | NUMBER { code_number($1); $$ = 0; }
  559. | STRING
  560. {
  561. code_string($1);
  562. $$ = 0;
  563. }
  564. | NIL {code_byte(PUSHNIL); $$ = 0; }
  565. | functioncall { $$ = $1; }
  566. | NOT expr1 { code_byte(NOTOP); $$ = 0;}
  567. | expr1 AND PrepJump expr1
  568. {
  569. code_shortcircuit($3, ONFJMP);
  570. $$ = 0;
  571. }
  572. | expr1 OR PrepJump expr1
  573. {
  574. code_shortcircuit($3, ONTJMP);
  575. $$ = 0;
  576. }
  577. ;
  578. table :
  579. {
  580. code_byte(CREATEARRAY);
  581. $<vLong>$ = currState->pc; code_word(0);
  582. }
  583. '{' fieldlist '}'
  584. {
  585. code_word_at($<vLong>1, $3);
  586. }
  587. ;
  588. functioncall : funcvalue funcParams
  589. {
  590. code_byte(CALLFUNC);
  591. code_byte($1+$2);
  592. $$ = currState->pc;
  593. code_byte(0); /* may be modified by other rules */
  594. }
  595. ;
  596. funcvalue : varexp { $$ = 0; }
  597. | varexp ':' NAME
  598. {
  599. code_byte(PUSHSELF);
  600. code_word(string_constant($3));
  601. $$ = 1;
  602. }
  603. ;
  604. funcParams : '(' exprlist ')'
  605. { $$ = adjust_functioncall($2, 1); }
  606. | table { $$ = 1; }
  607. ;
  608. exprlist : /* empty */ { $$ = 0; }
  609. | exprlist1 { $$ = $1; }
  610. ;
  611. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  612. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  613. {
  614. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  615. else
  616. {
  617. adjust_functioncall($4, $<vLong>3);
  618. $$ = $4;
  619. }
  620. }
  621. ;
  622. parlist : /* empty */ { $$ = close_parlist(0); }
  623. | parlist1 { $$ = close_parlist($1); }
  624. ;
  625. parlist1 : par { $$ = $1; }
  626. | parlist1 ',' par
  627. {
  628. if ($1)
  629. lua_error("invalid parameter list");
  630. $$ = $3;
  631. }
  632. ;
  633. par : NAME { add_localvar($1); $$ = 0; }
  634. | DOTS { $$ = 1; }
  635. ;
  636. fieldlist : lfieldlist
  637. { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  638. semicolonpart
  639. { $$ = $1+$3; }
  640. | ffieldlist1 lastcomma
  641. { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
  642. ;
  643. semicolonpart : /* empty */
  644. { $$ = 0; }
  645. | ';' ffieldlist
  646. { $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
  647. ;
  648. lastcomma : /* empty */
  649. | ','
  650. ;
  651. ffieldlist : /* empty */ { $$ = 0; }
  652. | ffieldlist1 lastcomma { $$ = $1; }
  653. ;
  654. ffieldlist1 : ffield {$$=1;}
  655. | ffieldlist1 ',' ffield
  656. {
  657. $$=$1+1;
  658. if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
  659. }
  660. ;
  661. ffield : ffieldkey '=' expr1
  662. ;
  663. ffieldkey : '[' expr1 ']'
  664. | NAME { code_string($1); }
  665. ;
  666. lfieldlist : /* empty */ { $$ = 0; }
  667. | lfieldlist1 lastcomma { $$ = $1; }
  668. ;
  669. lfieldlist1 : expr1 {$$=1;}
  670. | lfieldlist1 ',' expr1
  671. {
  672. $$=$1+1;
  673. if ($$%FIELDS_PER_FLUSH == 0)
  674. flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
  675. }
  676. ;
  677. varlist1 : var
  678. {
  679. nvarbuffer = 0;
  680. add_varbuffer($1);
  681. $$ = ($1 == 0) ? 1 : 0;
  682. }
  683. | varlist1 ',' var
  684. {
  685. add_varbuffer($3);
  686. $$ = ($3 == 0) ? $1 + 1 : $1;
  687. }
  688. ;
  689. var : singlevar { $$ = $1; }
  690. | varexp '[' expr1 ']'
  691. {
  692. $$ = 0; /* indexed variable */
  693. }
  694. | varexp '.' NAME
  695. {
  696. code_string($3);
  697. $$ = 0; /* indexed variable */
  698. }
  699. ;
  700. singlevar : NAME
  701. {
  702. int local = lua_localname($1);
  703. if (local == -1) /* global var */
  704. $$ = luaI_findsymbol($1)+1; /* return positive value */
  705. else
  706. $$ = -(local+1); /* return negative value */
  707. luaI_fixstring($1); /* cannot GC variable names */
  708. }
  709. ;
  710. varexp : var { lua_pushvar($1); }
  711. ;
  712. localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
  713. | localdeclist ',' NAME
  714. {
  715. store_localvar($3, $1);
  716. $$ = $1+1;
  717. }
  718. ;
  719. decinit : /* empty */ { $$ = 0; }
  720. | '=' exprlist1 { $$ = $2; }
  721. ;
  722. %%