lua.stx 16 KB

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