lua.stx 23 KB

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