lua.stx 16 KB

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