lua.stx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 3.17 1995/01/13 22:11:12 roberto Exp celes $";
  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. /* to avoid warnings generated by yacc */
  14. int yyparse (void);
  15. #define malloc luaI_malloc
  16. #define realloc luaI_realloc
  17. #define free luaI_free
  18. #ifndef LISTING
  19. #define LISTING 0
  20. #endif
  21. #ifndef CODE_BLOCK
  22. #define CODE_BLOCK 256
  23. #endif
  24. static int maxcode;
  25. static int maxmain;
  26. static Long maxcurr; /* to allow maxcurr *= 2 without overflow */
  27. static Byte *funcCode = NULL;
  28. static Byte **initcode;
  29. static Byte *basepc;
  30. static int maincode;
  31. static int pc;
  32. #define MAXVAR 32
  33. static Long varbuffer[MAXVAR]; /* variables in an assignment list;
  34. it's long to store negative Word values */
  35. static int nvarbuffer=0; /* number of variables at a list */
  36. #define MAXLOCALS 32
  37. static Word localvar[MAXLOCALS]; /* store local variable names */
  38. static int nlocalvar=0; /* number of local variables */
  39. #define MAXFIELDS FIELDS_PER_FLUSH*2
  40. static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
  41. static int nfields=0;
  42. /* Internal functions */
  43. static void code_byte (Byte c)
  44. {
  45. if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
  46. {
  47. if (maxcurr >= MAX_INT)
  48. lua_error("code size overflow");
  49. maxcurr *= 2;
  50. if (maxcurr >= MAX_INT)
  51. maxcurr = MAX_INT;
  52. basepc = growvector(basepc, maxcurr, Byte);
  53. }
  54. basepc[pc++] = c;
  55. }
  56. static void code_word (Word n)
  57. {
  58. CodeWord code;
  59. code.w = n;
  60. code_byte(code.m.c1);
  61. code_byte(code.m.c2);
  62. }
  63. static void code_float (float n)
  64. {
  65. CodeFloat code;
  66. code.f = n;
  67. code_byte(code.m.c1);
  68. code_byte(code.m.c2);
  69. code_byte(code.m.c3);
  70. code_byte(code.m.c4);
  71. }
  72. static void code_code (Byte *b)
  73. {
  74. CodeCode code;
  75. code.b = b;
  76. code_byte(code.m.c1);
  77. code_byte(code.m.c2);
  78. code_byte(code.m.c3);
  79. code_byte(code.m.c4);
  80. }
  81. static void code_word_at (Byte *p, Word n)
  82. {
  83. CodeWord code;
  84. code.w = n;
  85. *p++ = code.m.c1;
  86. *p++ = code.m.c2;
  87. }
  88. static void push_field (Word name)
  89. {
  90. if (nfields < MAXFIELDS)
  91. fields[nfields++] = name;
  92. else
  93. lua_error ("too many fields in nested constructors");
  94. }
  95. static void flush_record (int n)
  96. {
  97. int i;
  98. if (n == 0) return;
  99. code_byte(STORERECORD);
  100. code_byte(n);
  101. for (i=0; i<n; i++)
  102. code_word(fields[--nfields]);
  103. }
  104. static void flush_list (int m, int n)
  105. {
  106. if (n == 0) return;
  107. if (m == 0)
  108. code_byte(STORELIST0);
  109. else
  110. if (m < 255)
  111. {
  112. code_byte(STORELIST);
  113. code_byte(m);
  114. }
  115. else
  116. lua_error ("list constructor too long");
  117. code_byte(n);
  118. }
  119. static void add_localvar (Word name)
  120. {
  121. if (nlocalvar < MAXLOCALS)
  122. localvar[nlocalvar++] = name;
  123. else
  124. lua_error ("too many local variables");
  125. }
  126. static void store_localvar (Word name, int n)
  127. {
  128. if (nlocalvar+n < MAXLOCALS)
  129. localvar[nlocalvar+n] = name;
  130. else
  131. lua_error ("too many local variables");
  132. }
  133. static void add_varbuffer (Long var)
  134. {
  135. if (nvarbuffer < MAXVAR)
  136. varbuffer[nvarbuffer++] = var;
  137. else
  138. lua_error ("variable buffer overflow");
  139. }
  140. static void code_number (float f)
  141. {
  142. Word i = (Word)f;
  143. if (f == (float)i) /* f has an (short) integer value */
  144. {
  145. if (i <= 2) code_byte(PUSH0 + i);
  146. else if (i <= 255)
  147. {
  148. code_byte(PUSHBYTE);
  149. code_byte(i);
  150. }
  151. else
  152. {
  153. code_byte(PUSHWORD);
  154. code_word(i);
  155. }
  156. }
  157. else
  158. {
  159. code_byte(PUSHFLOAT);
  160. code_float(f);
  161. }
  162. }
  163. /*
  164. ** Search a local name and if find return its index. If do not find return -1
  165. */
  166. static int lua_localname (Word n)
  167. {
  168. int i;
  169. for (i=nlocalvar-1; i >= 0; i--)
  170. if (n == localvar[i]) return i; /* local var */
  171. return -1; /* global var */
  172. }
  173. /*
  174. ** Push a variable given a number. If number is positive, push global variable
  175. ** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
  176. ** Otherwise, if zero, push indexed variable (record).
  177. */
  178. static void lua_pushvar (Long number)
  179. {
  180. if (number > 0) /* global var */
  181. {
  182. code_byte(PUSHGLOBAL);
  183. code_word(number-1);
  184. }
  185. else if (number < 0) /* local var */
  186. {
  187. number = (-number) - 1;
  188. if (number < 10) code_byte(PUSHLOCAL0 + number);
  189. else
  190. {
  191. code_byte(PUSHLOCAL);
  192. code_byte(number);
  193. }
  194. }
  195. else
  196. {
  197. code_byte(PUSHINDEXED);
  198. }
  199. }
  200. static void lua_codeadjust (int n)
  201. {
  202. if (n+nlocalvar == 0)
  203. code_byte(ADJUST0);
  204. else
  205. {
  206. code_byte(ADJUST);
  207. code_byte(n+nlocalvar);
  208. }
  209. }
  210. static void init_function (TreeNode *func)
  211. {
  212. if (funcCode == NULL) /* first function */
  213. {
  214. funcCode = newvector(CODE_BLOCK, Byte);
  215. maxcode = CODE_BLOCK;
  216. }
  217. pc=0; basepc=funcCode; maxcurr=maxcode;
  218. nlocalvar=0;
  219. if (lua_debug)
  220. {
  221. code_byte(SETFUNCTION);
  222. code_code((Byte *)luaI_strdup(lua_file[lua_nfile-1]));
  223. code_word(luaI_findconstant(func));
  224. }
  225. }
  226. static void codereturn (void)
  227. {
  228. if (lua_debug) code_byte(RESET);
  229. if (nlocalvar == 0)
  230. code_byte(RETCODE0);
  231. else
  232. {
  233. code_byte(RETCODE);
  234. code_byte(nlocalvar);
  235. }
  236. }
  237. static void codedebugline (void)
  238. {
  239. if (lua_debug)
  240. {
  241. code_byte(SETLINE);
  242. code_word(lua_linenumber);
  243. }
  244. }
  245. static void adjust_mult_assign (int vars, int exps, int temps)
  246. {
  247. if (exps < 0)
  248. {
  249. int r = vars - (-exps-1);
  250. if (r >= 0)
  251. code_byte(r);
  252. else
  253. {
  254. code_byte(0);
  255. lua_codeadjust(temps);
  256. }
  257. }
  258. else if (vars != exps)
  259. lua_codeadjust(temps);
  260. }
  261. static void lua_codestore (int i)
  262. {
  263. if (varbuffer[i] > 0) /* global var */
  264. {
  265. code_byte(STOREGLOBAL);
  266. code_word(varbuffer[i]-1);
  267. }
  268. else if (varbuffer[i] < 0) /* local var */
  269. {
  270. int number = (-varbuffer[i]) - 1;
  271. if (number < 10) code_byte(STORELOCAL0 + number);
  272. else
  273. {
  274. code_byte(STORELOCAL);
  275. code_byte(number);
  276. }
  277. }
  278. else /* indexed var */
  279. {
  280. int j;
  281. int upper=0; /* number of indexed variables upper */
  282. int param; /* number of itens until indexed expression */
  283. for (j=i+1; j <nvarbuffer; j++)
  284. if (varbuffer[j] == 0) upper++;
  285. param = upper*2 + i;
  286. if (param == 0)
  287. code_byte(STOREINDEXED0);
  288. else
  289. {
  290. code_byte(STOREINDEXED);
  291. code_byte(param);
  292. }
  293. }
  294. }
  295. static void codeIf (Long thenAdd, Long elseAdd)
  296. {
  297. Long elseinit = elseAdd+sizeof(Word)+1;
  298. if (pc == elseinit) /* no else */
  299. {
  300. pc -= sizeof(Word)+1;
  301. elseinit = pc;
  302. }
  303. else
  304. {
  305. basepc[elseAdd] = JMP;
  306. code_word_at(basepc+elseAdd+1, pc-elseinit);
  307. }
  308. basepc[thenAdd] = IFFJMP;
  309. code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
  310. }
  311. static void yyerror (char *s)
  312. {
  313. static char msg[256];
  314. sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
  315. s, lua_lasttext (), lua_linenumber, lua_filename());
  316. lua_error (msg);
  317. }
  318. /*
  319. ** Parse LUA code.
  320. */
  321. void lua_parse (Byte **code)
  322. {
  323. initcode = code;
  324. *initcode = newvector(CODE_BLOCK, Byte);
  325. maincode = 0;
  326. maxmain = CODE_BLOCK;
  327. if (yyparse ()) lua_error("parse error");
  328. (*initcode)[maincode++] = RETCODE0;
  329. #if LISTING
  330. { static void PrintCode (Byte *c, Byte *end);
  331. PrintCode(*initcode,*initcode+maincode); }
  332. #endif
  333. }
  334. %}
  335. %union
  336. {
  337. int vInt;
  338. float vFloat;
  339. char *pChar;
  340. Word vWord;
  341. Long vLong;
  342. Byte *pByte;
  343. TreeNode *pNode;
  344. }
  345. %start functionlist
  346. %token WRONGTOKEN
  347. %token NIL
  348. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  349. %token RETURN
  350. %token LOCAL
  351. %token FUNCTION
  352. %token <vFloat> NUMBER
  353. %token <vWord> STRING
  354. %token <pNode> NAME
  355. %token <vInt> DEBUG
  356. %type <vLong> PrepJump
  357. %type <vInt> expr, exprlist, exprlist1, varlist1, funcParams, funcvalue
  358. %type <vInt> fieldlist, localdeclist, decinit
  359. %type <vInt> ffieldlist1
  360. %type <vInt> lfieldlist1
  361. %type <vLong> var, singlevar
  362. %type <pByte> body
  363. %left AND OR
  364. %left EQ NE '>' '<' LE GE
  365. %left CONC
  366. %left '+' '-'
  367. %left '*' '/'
  368. %left UNARY NOT
  369. %right '^'
  370. %% /* beginning of rules section */
  371. functionlist : /* empty */
  372. | functionlist
  373. {
  374. pc=maincode; basepc=*initcode; maxcurr=maxmain;
  375. nlocalvar=0;
  376. }
  377. stat sc
  378. {
  379. maincode=pc; *initcode=basepc; maxmain=maxcurr;
  380. }
  381. | functionlist function
  382. | functionlist method
  383. | functionlist setdebug
  384. ;
  385. function : FUNCTION NAME
  386. {
  387. init_function($2);
  388. }
  389. body
  390. {
  391. Word func = luaI_findsymbol($2);
  392. s_tag(func) = LUA_T_FUNCTION;
  393. s_bvalue(func) = $4;
  394. }
  395. ;
  396. method : FUNCTION NAME ':' NAME
  397. {
  398. init_function($4);
  399. add_localvar(luaI_findsymbolbyname("self"));
  400. }
  401. body
  402. {
  403. /* assign function to table field */
  404. pc=maincode; basepc=*initcode; maxcurr=maxmain;
  405. nlocalvar=0;
  406. lua_pushvar(luaI_findsymbol($2)+1);
  407. code_byte(PUSHSTRING);
  408. code_word(luaI_findconstant($4));
  409. code_byte(PUSHFUNCTION);
  410. code_code($6);
  411. code_byte(STOREINDEXED0);
  412. maincode=pc; *initcode=basepc; maxmain=maxcurr;
  413. }
  414. ;
  415. body : '(' parlist ')' block END
  416. {
  417. codereturn();
  418. $$ = newvector(pc, Byte);
  419. memcpy($$, basepc, pc*sizeof(Byte));
  420. funcCode = basepc; maxcode=maxcurr;
  421. #if LISTING
  422. PrintCode(funcCode,funcCode+pc);
  423. #endif
  424. }
  425. ;
  426. statlist : /* empty */
  427. | statlist stat sc
  428. ;
  429. sc : /* empty */ | ';' ;
  430. stat : { codedebugline(); } stat1 ;
  431. cond : { codedebugline(); } expr1 ;
  432. stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
  433. { codeIf($4, $6); }
  434. | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
  435. {
  436. basepc[$5] = IFFJMP;
  437. code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
  438. basepc[$7] = UPJMP;
  439. code_word_at(basepc+$7+1, pc - ($<vLong>2));
  440. }
  441. | REPEAT {$<vLong>$=pc;} block UNTIL cond PrepJump
  442. {
  443. basepc[$6] = IFFUPJMP;
  444. code_word_at(basepc+$6+1, pc - ($<vLong>2));
  445. }
  446. | varlist1 '=' exprlist1
  447. {
  448. {
  449. int i;
  450. adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
  451. for (i=nvarbuffer-1; i>=0; i--)
  452. lua_codestore (i);
  453. if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
  454. lua_codeadjust (0);
  455. }
  456. }
  457. | functioncall { code_byte(0); }
  458. | LOCAL localdeclist decinit
  459. { nlocalvar += $2;
  460. adjust_mult_assign($2, $3, 0);
  461. }
  462. ;
  463. elsepart : /* empty */
  464. | ELSE block
  465. | ELSEIF cond THEN PrepJump block PrepJump elsepart
  466. { codeIf($4, $6); }
  467. ;
  468. block : {$<vInt>$ = nlocalvar;} statlist ret
  469. {
  470. if (nlocalvar != $<vInt>1)
  471. {
  472. nlocalvar = $<vInt>1;
  473. lua_codeadjust (0);
  474. }
  475. }
  476. ;
  477. ret : /* empty */
  478. | RETURN { codedebugline(); } exprlist sc
  479. {
  480. if ($3 < 0) code_byte(MULT_RET);
  481. codereturn();
  482. }
  483. ;
  484. PrepJump : /* empty */
  485. {
  486. $$ = pc;
  487. code_byte(0); /* open space */
  488. code_word (0);
  489. }
  490. expr1 : expr { if ($1 == 0) code_byte(1); }
  491. ;
  492. expr : '(' expr ')' { $$ = $2; }
  493. | expr1 EQ expr1 { code_byte(EQOP); $$ = 1; }
  494. | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; }
  495. | expr1 '>' expr1 { code_byte(GTOP); $$ = 1; }
  496. | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; }
  497. | expr1 LE expr1 { code_byte(LEOP); $$ = 1; }
  498. | expr1 GE expr1 { code_byte(GEOP); $$ = 1; }
  499. | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; }
  500. | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; }
  501. | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; }
  502. | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; }
  503. | expr1 '^' expr1 { code_byte(POWOP); $$ = 1; }
  504. | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; }
  505. | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;}
  506. | table { $$ = 1; }
  507. | varexp { $$ = 1;}
  508. | NUMBER { code_number($1); $$ = 1; }
  509. | STRING
  510. {
  511. code_byte(PUSHSTRING);
  512. code_word($1);
  513. $$ = 1;
  514. }
  515. | NIL {code_byte(PUSHNIL); $$ = 1; }
  516. | functioncall { $$ = 0; }
  517. | NOT expr1 { code_byte(NOTOP); $$ = 1;}
  518. | expr1 AND PrepJump {code_byte(POP); } expr1
  519. {
  520. basepc[$3] = ONFJMP;
  521. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  522. $$ = 1;
  523. }
  524. | expr1 OR PrepJump {code_byte(POP); } expr1
  525. {
  526. basepc[$3] = ONTJMP;
  527. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  528. $$ = 1;
  529. }
  530. ;
  531. table :
  532. {
  533. code_byte(CREATEARRAY);
  534. $<vLong>$ = pc; code_word(0);
  535. }
  536. '{' fieldlist '}'
  537. {
  538. code_word_at(basepc+$<vLong>1, $3);
  539. }
  540. ;
  541. functioncall : funcvalue funcParams
  542. { code_byte(CALLFUNC); code_byte($1+$2); }
  543. ;
  544. funcvalue : varexp { $$ = 0; }
  545. | varexp ':' NAME
  546. {
  547. code_byte(PUSHSELF);
  548. code_word(luaI_findconstant($3));
  549. $$ = 1;
  550. }
  551. ;
  552. funcParams : '(' exprlist ')'
  553. { if ($2<0) { code_byte(1); $$ = -$2; } else $$ = $2; }
  554. | table { $$ = 1; }
  555. ;
  556. exprlist : /* empty */ { $$ = 0; }
  557. | exprlist1 { $$ = $1; }
  558. ;
  559. exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
  560. | exprlist1 ',' { if ($1 < 0) code_byte(1); } expr
  561. {
  562. int r = $1 < 0 ? -$1 : $1;
  563. $$ = ($4 == 0) ? -(r+1) : r+1;
  564. }
  565. ;
  566. parlist : /* empty */ { lua_codeadjust(0); }
  567. | parlist1 { lua_codeadjust(0); }
  568. ;
  569. parlist1 : NAME
  570. {
  571. add_localvar(luaI_findsymbol($1));
  572. }
  573. | parlist1 ',' NAME
  574. {
  575. add_localvar(luaI_findsymbol($3));
  576. }
  577. ;
  578. fieldlist : /* empty */ { $$ = 0; }
  579. | lfieldlist1 lastcomma
  580. { $$ = $1; flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  581. | ffieldlist1 lastcomma
  582. { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
  583. | lfieldlist1 ';'
  584. { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  585. ffieldlist1 lastcomma
  586. { $$ = $1+$4; flush_record($4%FIELDS_PER_FLUSH); }
  587. ;
  588. lastcomma : /* empty */
  589. | ','
  590. ;
  591. ffieldlist1 : ffield {$$=1;}
  592. | ffieldlist1 ',' ffield
  593. {
  594. $$=$1+1;
  595. if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
  596. }
  597. ;
  598. ffield : NAME '=' expr1
  599. {
  600. push_field(luaI_findconstant($1));
  601. }
  602. ;
  603. lfieldlist1 : expr1 {$$=1;}
  604. | lfieldlist1 ',' expr1
  605. {
  606. $$=$1+1;
  607. if ($$%FIELDS_PER_FLUSH == 0)
  608. flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
  609. }
  610. ;
  611. varlist1 : var
  612. {
  613. nvarbuffer = 0;
  614. add_varbuffer($1);
  615. $$ = ($1 == 0) ? 1 : 0;
  616. }
  617. | varlist1 ',' var
  618. {
  619. add_varbuffer($3);
  620. $$ = ($3 == 0) ? $1 + 1 : $1;
  621. }
  622. ;
  623. var : singlevar { $$ = $1; }
  624. | varexp '[' expr1 ']'
  625. {
  626. $$ = 0; /* indexed variable */
  627. }
  628. | varexp '.' NAME
  629. {
  630. code_byte(PUSHSTRING);
  631. code_word(luaI_findconstant($3));
  632. $$ = 0; /* indexed variable */
  633. }
  634. ;
  635. singlevar : NAME
  636. {
  637. Word s = luaI_findsymbol($1);
  638. int local = lua_localname (s);
  639. if (local == -1) /* global var */
  640. $$ = s + 1; /* return positive value */
  641. else
  642. $$ = -(local+1); /* return negative value */
  643. }
  644. ;
  645. varexp : var { lua_pushvar($1); }
  646. ;
  647. localdeclist : NAME {store_localvar(luaI_findsymbol($1), 0); $$ = 1;}
  648. | localdeclist ',' NAME
  649. {
  650. store_localvar(luaI_findsymbol($3), $1);
  651. $$ = $1+1;
  652. }
  653. ;
  654. decinit : /* empty */ { $$ = 0; }
  655. | '=' exprlist1 { $$ = $2; }
  656. ;
  657. setdebug : DEBUG {lua_debug = $1;}
  658. %%
  659. #if LISTING
  660. static void PrintCode (Byte *code, Byte *end)
  661. {
  662. Byte *p = code;
  663. printf ("\n\nCODE\n");
  664. while (p != end)
  665. {
  666. switch ((OpCode)*p)
  667. {
  668. case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
  669. case PUSH0: case PUSH1: case PUSH2:
  670. printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
  671. p++;
  672. break;
  673. case PUSHBYTE:
  674. printf ("%d PUSHBYTE %d\n", p-code, *(++p));
  675. p++;
  676. break;
  677. case PUSHWORD:
  678. {
  679. CodeWord c;
  680. int n = p-code;
  681. p++;
  682. get_word(c,p);
  683. printf ("%d PUSHWORD %d\n", n, c.w);
  684. }
  685. break;
  686. case PUSHFLOAT:
  687. {
  688. CodeFloat c;
  689. int n = p-code;
  690. p++;
  691. get_float(c,p);
  692. printf ("%d PUSHFLOAT %f\n", n, c.f);
  693. }
  694. break;
  695. case PUSHSTRING:
  696. {
  697. CodeWord c;
  698. int n = p-code;
  699. p++;
  700. get_word(c,p);
  701. printf ("%d PUSHSTRING %d\n", n, c.w);
  702. }
  703. break;
  704. case PUSHFUNCTION:
  705. {
  706. CodeCode c;
  707. int n = p-code;
  708. p++;
  709. get_code(c,p);
  710. printf ("%d PUSHFUNCTION %p\n", n, c.b);
  711. }
  712. break;
  713. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
  714. case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
  715. case PUSHLOCAL8: case PUSHLOCAL9:
  716. printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
  717. p++;
  718. break;
  719. case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
  720. p++;
  721. break;
  722. case PUSHGLOBAL:
  723. {
  724. CodeWord c;
  725. int n = p-code;
  726. p++;
  727. get_word(c,p);
  728. printf ("%d PUSHGLOBAL %d\n", n, c.w);
  729. }
  730. break;
  731. case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
  732. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
  733. case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
  734. case STORELOCAL8: case STORELOCAL9:
  735. printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
  736. p++;
  737. break;
  738. case STORELOCAL:
  739. printf ("%d STORELOCAL %d\n", p-code, *(++p));
  740. p++;
  741. break;
  742. case STOREGLOBAL:
  743. {
  744. CodeWord c;
  745. int n = p-code;
  746. p++;
  747. get_word(c,p);
  748. printf ("%d STOREGLOBAL %d\n", n, c.w);
  749. }
  750. break;
  751. case PUSHSELF:
  752. {
  753. CodeWord c;
  754. int n = p-code;
  755. p++;
  756. get_word(c,p);
  757. printf ("%d PUSHSELF %d\n", n, c.w);
  758. }
  759. break;
  760. case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
  761. case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
  762. p++;
  763. break;
  764. case STORELIST0:
  765. printf("%d STORELIST0 %d\n", p-code, *(++p));
  766. p++;
  767. break;
  768. case STORELIST:
  769. printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
  770. p+=3;
  771. break;
  772. case STORERECORD:
  773. printf("%d STORERECORD %d\n", p-code, *(++p));
  774. p += *p*sizeof(Word) + 1;
  775. break;
  776. case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
  777. case ADJUST:
  778. printf ("%d ADJUST %d\n", p-code, *(++p));
  779. p++;
  780. break;
  781. case CREATEARRAY:
  782. {
  783. CodeWord c;
  784. int n = p-code;
  785. p++;
  786. get_word(c,p);
  787. printf ("%d CREATEARRAY %d\n", n, c.w);
  788. break;
  789. }
  790. case EQOP: printf ("%d EQOP\n", (p++)-code); break;
  791. case LTOP: printf ("%d LTOP\n", (p++)-code); break;
  792. case LEOP: printf ("%d LEOP\n", (p++)-code); break;
  793. case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
  794. case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
  795. case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
  796. case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
  797. case POWOP: printf ("%d POWOP\n", (p++)-code); break;
  798. case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
  799. case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
  800. case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
  801. case ONTJMP:
  802. {
  803. CodeWord c;
  804. int n = p-code;
  805. p++;
  806. get_word(c,p);
  807. printf ("%d ONTJMP %d\n", n, c.w);
  808. }
  809. break;
  810. case ONFJMP:
  811. {
  812. CodeWord c;
  813. int n = p-code;
  814. p++;
  815. get_word(c,p);
  816. printf ("%d ONFJMP %d\n", n, c.w);
  817. }
  818. break;
  819. case JMP:
  820. {
  821. CodeWord c;
  822. int n = p-code;
  823. p++;
  824. get_word(c,p);
  825. printf ("%d JMP %d\n", n, c.w);
  826. }
  827. break;
  828. case UPJMP:
  829. {
  830. CodeWord c;
  831. int n = p-code;
  832. p++;
  833. get_word(c,p);
  834. printf ("%d UPJMP %d\n", n, c.w);
  835. }
  836. break;
  837. case IFFJMP:
  838. {
  839. CodeWord c;
  840. int n = p-code;
  841. p++;
  842. get_word(c,p);
  843. printf ("%d IFFJMP %d\n", n, c.w);
  844. }
  845. break;
  846. case IFFUPJMP:
  847. {
  848. CodeWord c;
  849. int n = p-code;
  850. p++;
  851. get_word(c,p);
  852. printf ("%d IFFUPJMP %d\n", n, c.w);
  853. }
  854. break;
  855. case POP: printf ("%d POP\n", (p++)-code); break;
  856. case CALLFUNC:
  857. printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
  858. p+=3;
  859. break;
  860. case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
  861. case RETCODE:
  862. printf ("%d RETCODE %d\n", p-code, *(++p));
  863. p++;
  864. break;
  865. case SETFUNCTION:
  866. {
  867. CodeCode c1;
  868. CodeWord c2;
  869. int n = p-code;
  870. p++;
  871. get_code(c1,p);
  872. get_word(c2,p);
  873. printf ("%d SETFUNCTION %s %d\n", n, (char *)c1.b, c2.w);
  874. }
  875. break;
  876. case SETLINE:
  877. {
  878. CodeWord c;
  879. int n = p-code;
  880. p++;
  881. get_word(c,p);
  882. printf ("%d SETLINE %d\n", n, c.w);
  883. }
  884. break;
  885. case RESET: printf ("%d RESET\n", (p++)-code); break;
  886. default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
  887. }
  888. }
  889. }
  890. #endif