lua.stx 16 KB

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