lua.stx 16 KB

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