lua.stx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 3.39 1996/09/24 17:29:50 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 chunk
  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. %type <vLong> PrepJump
  396. %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
  397. counter (which has list length); if <= 0, -list lenght */
  398. %type <vLong> functioncall, expr /* if != 0, points to function return
  399. counter */
  400. %type <vInt> varlist1, funcParams, funcvalue
  401. %type <vInt> fieldlist, localdeclist, decinit
  402. %type <vInt> ffieldlist, ffieldlist1, semicolonpart
  403. %type <vInt> lfieldlist, lfieldlist1
  404. %type <vInt> parlist, parlist1, par
  405. %type <vLong> var, singlevar, funcname
  406. %type <pFunc> body
  407. %left AND OR
  408. %left EQ NE '>' '<' LE GE
  409. %left CONC
  410. %left '+' '-'
  411. %left '*' '/'
  412. %left UNARY NOT
  413. %right '^'
  414. %% /* beginning of rules section */
  415. chunk : chunklist ret
  416. chunklist : /* empty */
  417. | chunklist stat sc
  418. | chunklist function
  419. ;
  420. function : FUNCTION funcname body
  421. {
  422. code_byte(PUSHFUNCTION);
  423. code_code($3);
  424. storesinglevar($2);
  425. }
  426. ;
  427. funcname : var { $$ =$1; init_func(); }
  428. | varexp ':' NAME
  429. {
  430. code_byte(PUSHSTRING);
  431. code_word(luaI_findconstant($3));
  432. $$ = 0; /* indexed variable */
  433. init_func();
  434. add_localvar(luaI_createfixedstring("self"));
  435. }
  436. ;
  437. body : '(' parlist ')' block END
  438. {
  439. codereturn();
  440. $$ = new(TFunc);
  441. luaI_initTFunc($$);
  442. $$->size = pc;
  443. $$->code = newvector(pc, Byte);
  444. $$->fileName = lua_parsedfile;
  445. $$->lineDefined = $2;
  446. memcpy($$->code, basepc, pc*sizeof(Byte));
  447. if (lua_debug)
  448. luaI_closelocalvars($$);
  449. /* save func values */
  450. funcCode = basepc; maxcode=maxcurr;
  451. #if LISTING
  452. PrintCode(funcCode,funcCode+pc);
  453. #endif
  454. change2main(); /* change back to main code */
  455. }
  456. ;
  457. statlist : /* empty */
  458. | statlist stat sc
  459. ;
  460. sc : /* empty */ | ';' ;
  461. stat : IF expr1 THEN PrepJump block PrepJump elsepart END
  462. { codeIf($4, $6); }
  463. | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
  464. {
  465. basepc[$5] = IFFJMP;
  466. code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
  467. basepc[$7] = UPJMP;
  468. code_word_at(basepc+$7+1, pc - ($<vLong>2));
  469. }
  470. | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
  471. {
  472. basepc[$6] = IFFUPJMP;
  473. code_word_at(basepc+$6+1, pc - ($<vLong>2));
  474. }
  475. | varlist1 '=' exprlist1
  476. {
  477. {
  478. int i;
  479. adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
  480. for (i=nvarbuffer-1; i>=0; i--)
  481. lua_codestore (i);
  482. if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
  483. lua_codeadjust (0);
  484. }
  485. }
  486. | functioncall
  487. | LOCAL localdeclist decinit
  488. { nlocalvar += $2;
  489. adjust_mult_assign($2, $3, 0);
  490. }
  491. ;
  492. elsepart : /* empty */
  493. | ELSE block
  494. | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
  495. { codeIf($4, $6); }
  496. ;
  497. block : {$<vInt>$ = nlocalvar;} statlist ret
  498. {
  499. if (nlocalvar != $<vInt>1)
  500. {
  501. if (lua_debug)
  502. for (; nlocalvar > $<vInt>1; nlocalvar--)
  503. luaI_unregisterlocalvar(lua_linenumber);
  504. else
  505. nlocalvar = $<vInt>1;
  506. lua_codeadjust (0);
  507. }
  508. }
  509. ;
  510. ret : /* empty */
  511. | RETURN exprlist sc
  512. {
  513. adjust_functioncall($2, MULT_RET);
  514. codereturn();
  515. }
  516. ;
  517. PrepJump : /* empty */
  518. {
  519. $$ = pc;
  520. code_byte(0); /* open space */
  521. code_word (0);
  522. }
  523. ;
  524. expr1 : expr { adjust_functioncall($1, 1); }
  525. ;
  526. expr : '(' expr ')' { $$ = $2; }
  527. | expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
  528. | expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
  529. | expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
  530. | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
  531. | expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
  532. | expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
  533. | expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
  534. | expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
  535. | expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
  536. | expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
  537. | expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
  538. | expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
  539. | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
  540. | table { $$ = 0; }
  541. | varexp { $$ = 0;}
  542. | NUMBER { code_number($1); $$ = 0; }
  543. | STRING
  544. {
  545. code_byte(PUSHSTRING);
  546. code_word($1);
  547. $$ = 0;
  548. }
  549. | NIL {code_byte(PUSHNIL); $$ = 0; }
  550. | functioncall { $$ = $1; }
  551. | NOT expr1 { code_byte(NOTOP); $$ = 0;}
  552. | expr1 AND PrepJump {code_byte(POP); } expr1
  553. {
  554. basepc[$3] = ONFJMP;
  555. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  556. $$ = 0;
  557. }
  558. | expr1 OR PrepJump {code_byte(POP); } expr1
  559. {
  560. basepc[$3] = ONTJMP;
  561. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  562. $$ = 0;
  563. }
  564. ;
  565. table :
  566. {
  567. code_byte(CREATEARRAY);
  568. $<vLong>$ = pc; code_word(0);
  569. }
  570. '{' fieldlist '}'
  571. {
  572. code_word_at(basepc+$<vLong>1, $3);
  573. }
  574. ;
  575. functioncall : funcvalue funcParams
  576. {
  577. code_byte(CALLFUNC);
  578. code_byte($1+$2);
  579. $$ = pc;
  580. code_byte(0); /* may be modified by other rules */
  581. }
  582. ;
  583. funcvalue : varexp { $$ = 0; }
  584. | varexp ':' NAME
  585. {
  586. code_byte(PUSHSELF);
  587. code_word(luaI_findconstant($3));
  588. $$ = 1;
  589. }
  590. ;
  591. funcParams : '(' exprlist ')'
  592. { $$ = adjust_functioncall($2, 1); }
  593. | table { $$ = 1; }
  594. ;
  595. exprlist : /* empty */ { $$ = 0; }
  596. | exprlist1 { $$ = $1; }
  597. ;
  598. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  599. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  600. {
  601. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  602. else
  603. {
  604. adjust_functioncall($4, $<vLong>3);
  605. $$ = $4;
  606. }
  607. }
  608. ;
  609. parlist : /* empty */ { $$ = close_parlist(0); }
  610. | parlist1 { $$ = close_parlist($1); }
  611. ;
  612. parlist1 : par { $$ = $1; }
  613. | parlist1 ',' par
  614. {
  615. if ($1)
  616. lua_error("invalid parameter list");
  617. $$ = $3;
  618. }
  619. ;
  620. par : NAME { add_localvar($1); $$ = 0; }
  621. | DOTS { $$ = 1; }
  622. ;
  623. fieldlist : lfieldlist
  624. { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  625. semicolonpart
  626. { $$ = $1+$3; }
  627. | ffieldlist1 lastcomma
  628. { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
  629. ;
  630. semicolonpart : /* empty */
  631. { $$ = 0; }
  632. | ';' ffieldlist
  633. { $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
  634. ;
  635. lastcomma : /* empty */
  636. | ','
  637. ;
  638. ffieldlist : /* empty */ { $$ = 0; }
  639. | ffieldlist1 lastcomma { $$ = $1; }
  640. ;
  641. ffieldlist1 : ffield {$$=1;}
  642. | ffieldlist1 ',' ffield
  643. {
  644. $$=$1+1;
  645. if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
  646. }
  647. ;
  648. ffield : NAME '=' expr1
  649. {
  650. push_field(luaI_findconstant($1));
  651. }
  652. ;
  653. lfieldlist : /* empty */ { $$ = 0; }
  654. | lfieldlist1 lastcomma { $$ = $1; }
  655. ;
  656. lfieldlist1 : expr1 {$$=1;}
  657. | lfieldlist1 ',' expr1
  658. {
  659. $$=$1+1;
  660. if ($$%FIELDS_PER_FLUSH == 0)
  661. flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
  662. }
  663. ;
  664. varlist1 : var
  665. {
  666. nvarbuffer = 0;
  667. add_varbuffer($1);
  668. $$ = ($1 == 0) ? 1 : 0;
  669. }
  670. | varlist1 ',' var
  671. {
  672. add_varbuffer($3);
  673. $$ = ($3 == 0) ? $1 + 1 : $1;
  674. }
  675. ;
  676. var : singlevar { $$ = $1; }
  677. | varexp '[' expr1 ']'
  678. {
  679. $$ = 0; /* indexed variable */
  680. }
  681. | varexp '.' NAME
  682. {
  683. code_byte(PUSHSTRING);
  684. code_word(luaI_findconstant($3));
  685. $$ = 0; /* indexed variable */
  686. }
  687. ;
  688. singlevar : NAME
  689. {
  690. int local = lua_localname($1);
  691. if (local == -1) /* global var */
  692. $$ = luaI_findsymbol($1)+1; /* return positive value */
  693. else
  694. $$ = -(local+1); /* return negative value */
  695. }
  696. ;
  697. varexp : var { lua_pushvar($1); }
  698. ;
  699. localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
  700. | localdeclist ',' NAME
  701. {
  702. store_localvar($3, $1);
  703. $$ = $1+1;
  704. }
  705. ;
  706. decinit : /* empty */ { $$ = 0; }
  707. | '=' exprlist1 { $$ = $2; }
  708. ;
  709. %%