lua.stx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 3.24 1995/10/26 14:21:56 roberto Exp roberto $";
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "mem.h"
  7. #include "opcode.h"
  8. #include "hash.h"
  9. #include "inout.h"
  10. #include "tree.h"
  11. #include "table.h"
  12. #include "lua.h"
  13. #include "func.h"
  14. /* to avoid warnings generated by yacc */
  15. int yyparse (void);
  16. #define malloc luaI_malloc
  17. #define realloc luaI_realloc
  18. #define free luaI_free
  19. #ifndef LISTING
  20. #define LISTING 0
  21. #endif
  22. #ifndef CODE_BLOCK
  23. #define CODE_BLOCK 256
  24. #endif
  25. static int maxcode;
  26. static int maxmain;
  27. static Long maxcurr; /* to allow maxcurr *= 2 without overflow */
  28. static Byte *funcCode = NULL;
  29. static Byte **initcode;
  30. static Byte *basepc;
  31. static int maincode;
  32. static int pc;
  33. #define MAXVAR 32
  34. static Long varbuffer[MAXVAR]; /* variables in an assignment list;
  35. it's long to store negative Word values */
  36. static int nvarbuffer=0; /* number of variables at a list */
  37. #define MAXLOCALS 32
  38. static Word localvar[MAXLOCALS]; /* store local variable names */
  39. static int nlocalvar=0; /* number of local variables */
  40. #define MAXFIELDS FIELDS_PER_FLUSH*2
  41. static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
  42. static int nfields=0;
  43. /* Internal functions */
  44. static void yyerror (char *s)
  45. {
  46. static char msg[256];
  47. sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
  48. s, lua_lasttext (), lua_linenumber, lua_parsedfile);
  49. lua_error (msg);
  50. }
  51. static void code_byte (Byte c)
  52. {
  53. if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
  54. {
  55. if (maxcurr >= MAX_INT)
  56. lua_error("code size overflow");
  57. maxcurr *= 2;
  58. if (maxcurr >= MAX_INT)
  59. maxcurr = MAX_INT;
  60. basepc = growvector(basepc, maxcurr, Byte);
  61. }
  62. basepc[pc++] = c;
  63. }
  64. static void code_word (Word n)
  65. {
  66. CodeWord code;
  67. code.w = n;
  68. code_byte(code.m.c1);
  69. code_byte(code.m.c2);
  70. }
  71. static void code_float (float n)
  72. {
  73. CodeFloat code;
  74. code.f = n;
  75. code_byte(code.m.c1);
  76. code_byte(code.m.c2);
  77. code_byte(code.m.c3);
  78. code_byte(code.m.c4);
  79. }
  80. static void code_code (TFunc *tf)
  81. {
  82. CodeCode code;
  83. code.tf = tf;
  84. code_byte(code.m.c1);
  85. code_byte(code.m.c2);
  86. code_byte(code.m.c3);
  87. code_byte(code.m.c4);
  88. }
  89. static void code_word_at (Byte *p, Word n)
  90. {
  91. CodeWord code;
  92. code.w = 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. lua_error ("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. lua_error ("list constructor too long");
  125. code_byte(n);
  126. }
  127. static void add_localvar (Word name)
  128. {
  129. if (nlocalvar < MAXLOCALS)
  130. localvar[nlocalvar++] = name;
  131. else
  132. lua_error ("too many local variables");
  133. }
  134. static void store_localvar (Word name, int n)
  135. {
  136. if (nlocalvar+n < MAXLOCALS)
  137. localvar[nlocalvar+n] = name;
  138. else
  139. lua_error ("too many local variables");
  140. }
  141. static void add_varbuffer (Long var)
  142. {
  143. if (nvarbuffer < MAXVAR)
  144. varbuffer[nvarbuffer++] = var;
  145. else
  146. lua_error ("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 (Word 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. lua_debug = 0;
  352. initcode = &(tf->code);
  353. *initcode = newvector(CODE_BLOCK, Byte);
  354. maincode = 0;
  355. maxmain = CODE_BLOCK;
  356. change2main();
  357. if (yyparse ()) lua_error("parse error");
  358. savemain();
  359. (*initcode)[maincode++] = RETCODE0;
  360. tf->size = maincode;
  361. #if LISTING
  362. { static void PrintCode (Byte *c, Byte *end);
  363. PrintCode(*initcode,*initcode+maincode); }
  364. #endif
  365. }
  366. %}
  367. %union
  368. {
  369. int vInt;
  370. float vFloat;
  371. char *pChar;
  372. Word vWord;
  373. Long vLong;
  374. TFunc *pFunc;
  375. TreeNode *pNode;
  376. }
  377. %start functionlist
  378. %token WRONGTOKEN
  379. %token NIL
  380. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  381. %token RETURN
  382. %token LOCAL
  383. %token FUNCTION
  384. %token <vFloat> NUMBER
  385. %token <vWord> STRING
  386. %token <pNode> NAME
  387. %token <vInt> DEBUG
  388. %type <vLong> PrepJump
  389. %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
  390. counter (which has list length); if <= 0, -list lenght */
  391. %type <vLong> functioncall, expr /* if != 0, points to function return
  392. counter */
  393. %type <vInt> varlist1, funcParams, funcvalue
  394. %type <vInt> fieldlist, localdeclist, decinit
  395. %type <vInt> ffieldlist, ffieldlist1, semicolonpart
  396. %type <vInt> lfieldlist, lfieldlist1
  397. %type <vInt> parlist
  398. %type <vLong> var, singlevar, funcname
  399. %type <pFunc> body
  400. %left AND OR
  401. %left EQ NE '>' '<' LE GE
  402. %left CONC
  403. %left '+' '-'
  404. %left '*' '/'
  405. %left UNARY NOT
  406. %right '^'
  407. %% /* beginning of rules section */
  408. functionlist : /* empty */
  409. | functionlist globalstat
  410. | functionlist function
  411. ;
  412. globalstat : stat sc
  413. | setdebug
  414. ;
  415. function : FUNCTION funcname body
  416. {
  417. code_byte(PUSHFUNCTION);
  418. code_code($3);
  419. storesinglevar($2);
  420. }
  421. ;
  422. funcname : var { $$ =$1; init_func(); }
  423. | varexp ':' NAME
  424. {
  425. code_byte(PUSHSTRING);
  426. code_word(luaI_findconstant($3));
  427. $$ = 0; /* indexed variable */
  428. init_func();
  429. add_localvar(luaI_findsymbolbyname("self"));
  430. }
  431. ;
  432. body : '(' parlist ')' block END
  433. {
  434. codereturn();
  435. $$ = new(TFunc);
  436. $$->size = pc;
  437. $$->code = newvector(pc, Byte);
  438. $$->fileName = lua_parsedfile;
  439. $$->lineDefined = $2;
  440. memcpy($$->code, basepc, pc*sizeof(Byte));
  441. /* save func values */
  442. funcCode = basepc; maxcode=maxcurr;
  443. #if LISTING
  444. PrintCode(funcCode,funcCode+pc);
  445. #endif
  446. change2main(); /* change back to main code */
  447. }
  448. ;
  449. statlist : /* empty */
  450. | statlist stat sc
  451. ;
  452. sc : /* empty */ | ';' ;
  453. stat : IF expr1 THEN PrepJump block PrepJump elsepart END
  454. { codeIf($4, $6); }
  455. | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
  456. {
  457. basepc[$5] = IFFJMP;
  458. code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
  459. basepc[$7] = UPJMP;
  460. code_word_at(basepc+$7+1, pc - ($<vLong>2));
  461. }
  462. | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
  463. {
  464. basepc[$6] = IFFUPJMP;
  465. code_word_at(basepc+$6+1, pc - ($<vLong>2));
  466. }
  467. | varlist1 '=' exprlist1
  468. {
  469. {
  470. int i;
  471. adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
  472. for (i=nvarbuffer-1; i>=0; i--)
  473. lua_codestore (i);
  474. if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
  475. lua_codeadjust (0);
  476. }
  477. }
  478. | functioncall
  479. | LOCAL localdeclist decinit
  480. { nlocalvar += $2;
  481. adjust_mult_assign($2, $3, 0);
  482. }
  483. ;
  484. elsepart : /* empty */
  485. | ELSE block
  486. | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
  487. { codeIf($4, $6); }
  488. ;
  489. block : {$<vInt>$ = nlocalvar;} statlist ret
  490. {
  491. if (nlocalvar != $<vInt>1)
  492. {
  493. nlocalvar = $<vInt>1;
  494. lua_codeadjust (0);
  495. }
  496. }
  497. ;
  498. ret : /* empty */
  499. | RETURN exprlist sc
  500. {
  501. adjust_functioncall($2, MULT_RET);
  502. codereturn();
  503. }
  504. ;
  505. PrepJump : /* empty */
  506. {
  507. $$ = pc;
  508. code_byte(0); /* open space */
  509. code_word (0);
  510. }
  511. expr1 : expr { adjust_functioncall($1, 1); }
  512. ;
  513. expr : '(' expr ')' { $$ = $2; }
  514. | expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
  515. | expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
  516. | expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
  517. | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
  518. | expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
  519. | expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
  520. | expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
  521. | expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
  522. | expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
  523. | expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
  524. | expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
  525. | expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
  526. | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
  527. | table { $$ = 0; }
  528. | varexp { $$ = 0;}
  529. | NUMBER { code_number($1); $$ = 0; }
  530. | STRING
  531. {
  532. code_byte(PUSHSTRING);
  533. code_word($1);
  534. $$ = 0;
  535. }
  536. | NIL {code_byte(PUSHNIL); $$ = 0; }
  537. | functioncall { $$ = $1; }
  538. | NOT expr1 { code_byte(NOTOP); $$ = 0;}
  539. | expr1 AND PrepJump {code_byte(POP); } expr1
  540. {
  541. basepc[$3] = ONFJMP;
  542. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  543. $$ = 0;
  544. }
  545. | expr1 OR PrepJump {code_byte(POP); } expr1
  546. {
  547. basepc[$3] = ONTJMP;
  548. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  549. $$ = 0;
  550. }
  551. ;
  552. table :
  553. {
  554. code_byte(CREATEARRAY);
  555. $<vLong>$ = pc; code_word(0);
  556. }
  557. '{' fieldlist '}'
  558. {
  559. code_word_at(basepc+$<vLong>1, $3);
  560. }
  561. ;
  562. functioncall : funcvalue funcParams
  563. {
  564. code_byte(CALLFUNC);
  565. code_byte($1+$2);
  566. $$ = pc;
  567. code_byte(0); /* may be modified by other rules */
  568. }
  569. ;
  570. funcvalue : varexp { $$ = 0; }
  571. | varexp ':' NAME
  572. {
  573. code_byte(PUSHSELF);
  574. code_word(luaI_findconstant($3));
  575. $$ = 1;
  576. }
  577. ;
  578. funcParams : '(' exprlist ')'
  579. { $$ = adjust_functioncall($2, 1); }
  580. | table { $$ = 1; }
  581. ;
  582. exprlist : /* empty */ { $$ = 0; }
  583. | exprlist1 { $$ = $1; }
  584. ;
  585. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  586. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  587. {
  588. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  589. else
  590. {
  591. adjust_functioncall($4, $<vLong>3);
  592. $$ = $4;
  593. }
  594. }
  595. ;
  596. parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; }
  597. | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; }
  598. ;
  599. parlist1 : NAME
  600. {
  601. add_localvar(luaI_findsymbol($1));
  602. }
  603. | parlist1 ',' NAME
  604. {
  605. add_localvar(luaI_findsymbol($3));
  606. }
  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. Word s = luaI_findsymbol($1);
  676. int local = lua_localname (s);
  677. if (local == -1) /* global var */
  678. $$ = s + 1; /* return positive value */
  679. else
  680. $$ = -(local+1); /* return negative value */
  681. }
  682. ;
  683. varexp : var { lua_pushvar($1); }
  684. ;
  685. localdeclist : NAME {store_localvar(luaI_findsymbol($1), 0); $$ = 1;}
  686. | localdeclist ',' NAME
  687. {
  688. store_localvar(luaI_findsymbol($3), $1);
  689. $$ = $1+1;
  690. }
  691. ;
  692. decinit : /* empty */ { $$ = 0; }
  693. | '=' exprlist1 { $$ = $2; }
  694. ;
  695. setdebug : DEBUG { lua_debug = $1; }
  696. ;
  697. %%
  698. #if LISTING
  699. static void PrintCode (Byte *code, Byte *end)
  700. {
  701. Byte *p = code;
  702. printf ("\n\nCODE\n");
  703. while (p != end)
  704. {
  705. switch ((OpCode)*p)
  706. {
  707. case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
  708. case PUSH0: case PUSH1: case PUSH2:
  709. printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
  710. p++;
  711. break;
  712. case PUSHBYTE:
  713. printf ("%d PUSHBYTE %d\n", p-code, *(++p));
  714. p++;
  715. break;
  716. case PUSHWORD:
  717. {
  718. CodeWord c;
  719. int n = p-code;
  720. p++;
  721. get_word(c,p);
  722. printf ("%d PUSHWORD %d\n", n, c.w);
  723. }
  724. break;
  725. case PUSHFLOAT:
  726. {
  727. CodeFloat c;
  728. int n = p-code;
  729. p++;
  730. get_float(c,p);
  731. printf ("%d PUSHFLOAT %f\n", n, c.f);
  732. }
  733. break;
  734. case PUSHSTRING:
  735. {
  736. CodeWord c;
  737. int n = p-code;
  738. p++;
  739. get_word(c,p);
  740. printf ("%d PUSHSTRING %d\n", n, c.w);
  741. }
  742. break;
  743. case PUSHFUNCTION:
  744. {
  745. CodeCode c;
  746. int n = p-code;
  747. p++;
  748. get_code(c,p);
  749. printf ("%d PUSHFUNCTION %p\n", n, c.tf);
  750. }
  751. break;
  752. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
  753. case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
  754. case PUSHLOCAL8: case PUSHLOCAL9:
  755. printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
  756. p++;
  757. break;
  758. case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
  759. p++;
  760. break;
  761. case PUSHGLOBAL:
  762. {
  763. CodeWord c;
  764. int n = p-code;
  765. p++;
  766. get_word(c,p);
  767. printf ("%d PUSHGLOBAL %d\n", n, c.w);
  768. }
  769. break;
  770. case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
  771. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
  772. case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
  773. case STORELOCAL8: case STORELOCAL9:
  774. printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
  775. p++;
  776. break;
  777. case STORELOCAL:
  778. printf ("%d STORELOCAL %d\n", p-code, *(++p));
  779. p++;
  780. break;
  781. case STOREGLOBAL:
  782. {
  783. CodeWord c;
  784. int n = p-code;
  785. p++;
  786. get_word(c,p);
  787. printf ("%d STOREGLOBAL %d\n", n, c.w);
  788. }
  789. break;
  790. case PUSHSELF:
  791. {
  792. CodeWord c;
  793. int n = p-code;
  794. p++;
  795. get_word(c,p);
  796. printf ("%d PUSHSELF %d\n", n, c.w);
  797. }
  798. break;
  799. case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
  800. case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
  801. p++;
  802. break;
  803. case STORELIST0:
  804. printf("%d STORELIST0 %d\n", p-code, *(++p));
  805. p++;
  806. break;
  807. case STORELIST:
  808. printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
  809. p+=3;
  810. break;
  811. case STORERECORD:
  812. printf("%d STORERECORD %d\n", p-code, *(++p));
  813. p += *p*sizeof(Word) + 1;
  814. break;
  815. case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
  816. case ADJUST:
  817. printf ("%d ADJUST %d\n", p-code, *(++p));
  818. p++;
  819. break;
  820. case CREATEARRAY:
  821. {
  822. CodeWord c;
  823. int n = p-code;
  824. p++;
  825. get_word(c,p);
  826. printf ("%d CREATEARRAY %d\n", n, c.w);
  827. break;
  828. }
  829. case EQOP: printf ("%d EQOP\n", (p++)-code); break;
  830. case LTOP: printf ("%d LTOP\n", (p++)-code); break;
  831. case LEOP: printf ("%d LEOP\n", (p++)-code); break;
  832. case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
  833. case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
  834. case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
  835. case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
  836. case POWOP: printf ("%d POWOP\n", (p++)-code); break;
  837. case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
  838. case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
  839. case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
  840. case ONTJMP:
  841. {
  842. CodeWord c;
  843. int n = p-code;
  844. p++;
  845. get_word(c,p);
  846. printf ("%d ONTJMP %d\n", n, c.w);
  847. }
  848. break;
  849. case ONFJMP:
  850. {
  851. CodeWord c;
  852. int n = p-code;
  853. p++;
  854. get_word(c,p);
  855. printf ("%d ONFJMP %d\n", n, c.w);
  856. }
  857. break;
  858. case JMP:
  859. {
  860. CodeWord c;
  861. int n = p-code;
  862. p++;
  863. get_word(c,p);
  864. printf ("%d JMP %d\n", n, c.w);
  865. }
  866. break;
  867. case UPJMP:
  868. {
  869. CodeWord c;
  870. int n = p-code;
  871. p++;
  872. get_word(c,p);
  873. printf ("%d UPJMP %d\n", n, c.w);
  874. }
  875. break;
  876. case IFFJMP:
  877. {
  878. CodeWord c;
  879. int n = p-code;
  880. p++;
  881. get_word(c,p);
  882. printf ("%d IFFJMP %d\n", n, c.w);
  883. }
  884. break;
  885. case IFFUPJMP:
  886. {
  887. CodeWord c;
  888. int n = p-code;
  889. p++;
  890. get_word(c,p);
  891. printf ("%d IFFUPJMP %d\n", n, c.w);
  892. }
  893. break;
  894. case POP: printf ("%d POP\n", (p++)-code); break;
  895. case CALLFUNC:
  896. printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
  897. p+=3;
  898. break;
  899. case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
  900. case RETCODE:
  901. printf ("%d RETCODE %d\n", p-code, *(++p));
  902. p++;
  903. break;
  904. case SETLINE:
  905. {
  906. CodeWord c;
  907. int n = p-code;
  908. p++;
  909. get_word(c,p);
  910. printf ("%d SETLINE %d\n", n, c.w);
  911. }
  912. break;
  913. default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
  914. }
  915. }
  916. }
  917. #endif