lua.stx 21 KB

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