lua.stx 16 KB

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