lua.stx 22 KB

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