lua.stx 16 KB

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