lua.stx 21 KB

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