lua.stx 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 2.6 1994/08/03 14:15:46 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 <vWord> STRING
  207. %token <pChar> NAME
  208. %token <vInt> DEBUG
  209. %type <vLong> PrepJump
  210. %type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor
  211. %type <vInt> fieldlist, localdeclist
  212. %type <vInt> ffieldlist, ffieldlist1
  213. %type <vInt> lfieldlist, lfieldlist1
  214. %type <vInt> functionvalue
  215. %type <vLong> var, singlevar, objectname
  216. %left AND OR
  217. %left '=' 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_word(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. | typeconstructor { 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 '=' 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. | typeconstructor { $$ = $1; }
  440. | '@' '(' dimension ')'
  441. {
  442. code_byte(CREATEARRAY);
  443. $$ = 1;
  444. }
  445. | var { lua_pushvar ($1); $$ = 1;}
  446. | NUMBER { code_number($1); $$ = 1; }
  447. | STRING
  448. {
  449. code_byte(PUSHSTRING);
  450. code_word($1);
  451. $$ = 1;
  452. incr_ntemp();
  453. }
  454. | NIL {code_byte(PUSHNIL); $$ = 1; incr_ntemp();}
  455. | functioncall
  456. {
  457. $$ = 0;
  458. if (lua_debug)
  459. {
  460. code_byte(SETLINE); code_word(lua_linenumber);
  461. }
  462. }
  463. | NOT expr1 { code_byte(NOTOP); $$ = 1;}
  464. | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1
  465. {
  466. basepc[$3] = ONFJMP;
  467. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  468. $$ = 1;
  469. }
  470. | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1
  471. {
  472. basepc[$3] = ONTJMP;
  473. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  474. $$ = 1;
  475. }
  476. ;
  477. typeconstructor: '@'
  478. {
  479. code_byte(PUSHWORD);
  480. $<vLong>$ = pc; code_word(0);
  481. incr_ntemp();
  482. code_byte(CREATEARRAY);
  483. }
  484. objectname fieldlist
  485. {
  486. code_word_at(basepc+$<vLong>2, $4);
  487. if ($3 < 0) /* there is no function to be called */
  488. {
  489. $$ = 1;
  490. }
  491. else
  492. {
  493. lua_pushvar ($3+1);
  494. code_byte(PUSHMARK);
  495. incr_ntemp();
  496. code_byte(PUSHOBJECT);
  497. incr_ntemp();
  498. code_byte(CALLFUNC);
  499. ntemp -= 4;
  500. $$ = 0;
  501. if (lua_debug)
  502. {
  503. code_byte(SETLINE); code_word(lua_linenumber);
  504. }
  505. }
  506. }
  507. ;
  508. dimension : /* empty */ { code_byte(PUSHNIL); incr_ntemp();}
  509. | expr1
  510. ;
  511. functioncall : functionvalue
  512. {
  513. code_byte(PUSHMARK); $<vInt>$ = ntemp; incr_ntemp();
  514. if ($1 != 0) lua_pushvar($1);
  515. }
  516. '(' exprlist ')' { code_byte(CALLFUNC); ntemp = $<vInt>2-1;}
  517. functionvalue : var {lua_pushvar ($1); $$ = 0; }
  518. | singlevar ':' NAME
  519. {
  520. $$ = $1;
  521. lua_pushvar($1);
  522. code_byte(PUSHSTRING);
  523. code_word(lua_findconstant($3));
  524. incr_ntemp();
  525. lua_pushvar(0);
  526. }
  527. ;
  528. exprlist : /* empty */ { $$ = 1; }
  529. | exprlist1 { $$ = $1; }
  530. ;
  531. exprlist1 : expr { $$ = $1; }
  532. | exprlist1 ',' {if (!$1){lua_codeadjust (ntemp+1); incr_ntemp();}}
  533. expr {$$ = $4;}
  534. ;
  535. parlist : /* empty */
  536. | parlist1
  537. ;
  538. parlist1 : NAME
  539. {
  540. localvar[nlocalvar]=lua_findsymbol($1);
  541. add_nlocalvar(1);
  542. }
  543. | parlist1 ',' NAME
  544. {
  545. localvar[nlocalvar]=lua_findsymbol($3);
  546. add_nlocalvar(1);
  547. }
  548. ;
  549. objectname : /* empty */ {$$=-1;}
  550. | NAME {$$=lua_findsymbol($1);}
  551. ;
  552. fieldlist : '{' ffieldlist '}'
  553. {
  554. flush_record($2%FIELDS_PER_FLUSH);
  555. $$ = $2;
  556. }
  557. | '[' lfieldlist ']'
  558. {
  559. flush_list($2/FIELDS_PER_FLUSH, $2%FIELDS_PER_FLUSH);
  560. $$ = $2;
  561. }
  562. ;
  563. ffieldlist : /* empty */ { $$ = 0; }
  564. | ffieldlist1 { $$ = $1; }
  565. ;
  566. ffieldlist1 : ffield {$$=1;}
  567. | ffieldlist1 ',' ffield
  568. {
  569. $$=$1+1;
  570. if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
  571. }
  572. ;
  573. ffield : NAME {$<vWord>$ = lua_findconstant($1);} '=' expr1
  574. {
  575. push_field($<vWord>2);
  576. }
  577. ;
  578. lfieldlist : /* empty */ { $$ = 0; }
  579. | lfieldlist1 { $$ = $1; }
  580. ;
  581. lfieldlist1 : expr1 {$$=1;}
  582. | lfieldlist1 ',' expr1
  583. {
  584. $$=$1+1;
  585. if ($$%FIELDS_PER_FLUSH == 0)
  586. flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
  587. }
  588. ;
  589. varlist1 : var
  590. {
  591. nvarbuffer = 0;
  592. varbuffer[nvarbuffer] = $1; incr_nvarbuffer();
  593. $$ = ($1 == 0) ? 1 : 0;
  594. }
  595. | varlist1 ',' var
  596. {
  597. varbuffer[nvarbuffer] = $3; incr_nvarbuffer();
  598. $$ = ($3 == 0) ? $1 + 1 : $1;
  599. }
  600. ;
  601. var : singlevar { $$ = $1; }
  602. | var {lua_pushvar ($1);} '[' expr1 ']'
  603. {
  604. $$ = 0; /* indexed variable */
  605. }
  606. | var {lua_pushvar ($1);} '.' NAME
  607. {
  608. code_byte(PUSHSTRING);
  609. code_word(lua_findconstant($4)); incr_ntemp();
  610. $$ = 0; /* indexed variable */
  611. }
  612. ;
  613. singlevar : NAME
  614. {
  615. Word s = lua_findsymbol($1);
  616. int local = lua_localname (s);
  617. if (local == -1) /* global var */
  618. $$ = s + 1; /* return positive value */
  619. else
  620. $$ = -(local+1); /* return negative value */
  621. }
  622. ;
  623. localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;}
  624. | localdeclist ',' NAME
  625. {
  626. localvar[nlocalvar+$1]=lua_findsymbol($3);
  627. $$ = $1+1;
  628. }
  629. ;
  630. decinit : /* empty */
  631. | '=' exprlist1
  632. ;
  633. setdebug : DEBUG {lua_debug = $1;}
  634. %%
  635. /*
  636. ** Search a local name and if find return its index. If do not find return -1
  637. */
  638. static int lua_localname (Word n)
  639. {
  640. int i;
  641. for (i=nlocalvar-1; i >= 0; i--)
  642. if (n == localvar[i]) return i; /* local var */
  643. return -1; /* global var */
  644. }
  645. /*
  646. ** Push a variable given a number. If number is positive, push global variable
  647. ** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
  648. ** Otherwise, if zero, push indexed variable (record).
  649. */
  650. static void lua_pushvar (long number)
  651. {
  652. if (number > 0) /* global var */
  653. {
  654. code_byte(PUSHGLOBAL);
  655. code_word(number-1);
  656. incr_ntemp();
  657. }
  658. else if (number < 0) /* local var */
  659. {
  660. number = (-number) - 1;
  661. if (number < 10) code_byte(PUSHLOCAL0 + number);
  662. else
  663. {
  664. code_byte(PUSHLOCAL);
  665. code_byte(number);
  666. }
  667. incr_ntemp();
  668. }
  669. else
  670. {
  671. code_byte(PUSHINDEXED);
  672. ntemp--;
  673. }
  674. }
  675. static void lua_codeadjust (int n)
  676. {
  677. code_byte(ADJUST);
  678. code_byte(n + nlocalvar);
  679. }
  680. static void lua_codestore (int i)
  681. {
  682. if (varbuffer[i] > 0) /* global var */
  683. {
  684. code_byte(STOREGLOBAL);
  685. code_word(varbuffer[i]-1);
  686. }
  687. else if (varbuffer[i] < 0) /* local var */
  688. {
  689. int number = (-varbuffer[i]) - 1;
  690. if (number < 10) code_byte(STORELOCAL0 + number);
  691. else
  692. {
  693. code_byte(STORELOCAL);
  694. code_byte(number);
  695. }
  696. }
  697. else /* indexed var */
  698. {
  699. int j;
  700. int upper=0; /* number of indexed variables upper */
  701. int param; /* number of itens until indexed expression */
  702. for (j=i+1; j <nvarbuffer; j++)
  703. if (varbuffer[j] == 0) upper++;
  704. param = upper*2 + i;
  705. if (param == 0)
  706. code_byte(STOREINDEXED0);
  707. else
  708. {
  709. code_byte(STOREINDEXED);
  710. code_byte(param);
  711. }
  712. }
  713. }
  714. void yyerror (char *s)
  715. {
  716. static char msg[256];
  717. sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
  718. s, lua_lasttext (), lua_linenumber, lua_filename());
  719. lua_error (msg);
  720. err = 1;
  721. }
  722. int yywrap (void)
  723. {
  724. return 1;
  725. }
  726. /*
  727. ** Parse LUA code and execute global statement.
  728. ** Return 0 on success or 1 on error.
  729. */
  730. int lua_parse (void)
  731. {
  732. Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
  733. maincode = 0;
  734. maxmain = CODE_BLOCK;
  735. if (init == NULL)
  736. {
  737. lua_error("not enough memory");
  738. return 1;
  739. }
  740. err = 0;
  741. if (yyparse () || (err==1)) return 1;
  742. initcode[maincode++] = HALT;
  743. init = initcode;
  744. #if LISTING
  745. PrintCode(init,init+maincode);
  746. #endif
  747. if (lua_execute (init)) return 1;
  748. free(init);
  749. return 0;
  750. }
  751. #if LISTING
  752. static void PrintCode (Byte *code, Byte *end)
  753. {
  754. Byte *p = code;
  755. printf ("\n\nCODE\n");
  756. while (p != end)
  757. {
  758. switch ((OpCode)*p)
  759. {
  760. case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
  761. case PUSH0: case PUSH1: case PUSH2:
  762. printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
  763. p++;
  764. break;
  765. case PUSHBYTE:
  766. printf ("%d PUSHBYTE %d\n", p-code, *(++p));
  767. p++;
  768. break;
  769. case PUSHWORD:
  770. {
  771. CodeWord c;
  772. int n = p-code;
  773. p++;
  774. get_word(c,p);
  775. printf ("%d PUSHWORD %d\n", n, c.w);
  776. }
  777. break;
  778. case PUSHFLOAT:
  779. {
  780. CodeFloat c;
  781. int n = p-code;
  782. p++;
  783. get_float(c,p);
  784. printf ("%d PUSHFLOAT %f\n", n, c.f);
  785. }
  786. break;
  787. case PUSHSTRING:
  788. {
  789. CodeWord c;
  790. int n = p-code;
  791. p++;
  792. get_word(c,p);
  793. printf ("%d PUSHSTRING %d\n", n, c.w);
  794. }
  795. break;
  796. case PUSHFUNCTION:
  797. {
  798. CodeCode c;
  799. int n = p-code;
  800. p++;
  801. get_code(c,p);
  802. printf ("%d PUSHFUNCTION %p\n", n, c.b);
  803. }
  804. break;
  805. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
  806. case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
  807. case PUSHLOCAL8: case PUSHLOCAL9:
  808. printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
  809. p++;
  810. break;
  811. case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
  812. p++;
  813. break;
  814. case PUSHGLOBAL:
  815. {
  816. CodeWord c;
  817. int n = p-code;
  818. p++;
  819. get_word(c,p);
  820. printf ("%d PUSHGLOBAL %d\n", n, c.w);
  821. }
  822. break;
  823. case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
  824. case PUSHMARK: printf ("%d PUSHMARK\n", (p++)-code); break;
  825. case PUSHOBJECT: printf ("%d PUSHOBJECT\n", (p++)-code); break;
  826. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
  827. case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
  828. case STORELOCAL8: case STORELOCAL9:
  829. printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
  830. p++;
  831. break;
  832. case STORELOCAL:
  833. printf ("%d STORELOCAL %d\n", p-code, *(++p));
  834. p++;
  835. break;
  836. case STOREGLOBAL:
  837. {
  838. CodeWord c;
  839. int n = p-code;
  840. p++;
  841. get_word(c,p);
  842. printf ("%d STOREGLOBAL %d\n", n, c.w);
  843. }
  844. break;
  845. case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
  846. case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
  847. p++;
  848. break;
  849. case STORELIST0:
  850. printf("%d STORELIST0 %d\n", p-code, *(++p));
  851. p++;
  852. break;
  853. case STORELIST:
  854. printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
  855. p+=3;
  856. break;
  857. case STORERECORD:
  858. printf("%d STORERECORD %d\n", p-code, *(++p));
  859. p += *p*sizeof(Word) + 1;
  860. break;
  861. case ADJUST:
  862. printf ("%d ADJUST %d\n", p-code, *(++p));
  863. p++;
  864. break;
  865. case CREATEARRAY: printf ("%d CREATEARRAY\n", (p++)-code); break;
  866. case EQOP: printf ("%d EQOP\n", (p++)-code); break;
  867. case LTOP: printf ("%d LTOP\n", (p++)-code); break;
  868. case LEOP: printf ("%d LEOP\n", (p++)-code); break;
  869. case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
  870. case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
  871. case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
  872. case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
  873. case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
  874. case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
  875. case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
  876. case ONTJMP:
  877. {
  878. CodeWord c;
  879. int n = p-code;
  880. p++;
  881. get_word(c,p);
  882. printf ("%d ONTJMP %d\n", n, c.w);
  883. }
  884. break;
  885. case ONFJMP:
  886. {
  887. CodeWord c;
  888. int n = p-code;
  889. p++;
  890. get_word(c,p);
  891. printf ("%d ONFJMP %d\n", n, c.w);
  892. }
  893. break;
  894. case JMP:
  895. {
  896. CodeWord c;
  897. int n = p-code;
  898. p++;
  899. get_word(c,p);
  900. printf ("%d JMP %d\n", n, c.w);
  901. }
  902. break;
  903. case UPJMP:
  904. {
  905. CodeWord c;
  906. int n = p-code;
  907. p++;
  908. get_word(c,p);
  909. printf ("%d UPJMP %d\n", n, c.w);
  910. }
  911. break;
  912. case IFFJMP:
  913. {
  914. CodeWord c;
  915. int n = p-code;
  916. p++;
  917. get_word(c,p);
  918. printf ("%d IFFJMP %d\n", n, c.w);
  919. }
  920. break;
  921. case IFFUPJMP:
  922. {
  923. CodeWord c;
  924. int n = p-code;
  925. p++;
  926. get_word(c,p);
  927. printf ("%d IFFUPJMP %d\n", n, c.w);
  928. }
  929. break;
  930. case POP: printf ("%d POP\n", (p++)-code); break;
  931. case CALLFUNC: printf ("%d CALLFUNC\n", (p++)-code); break;
  932. case RETCODE:
  933. printf ("%d RETCODE %d\n", p-code, *(++p));
  934. p++;
  935. break;
  936. case HALT: printf ("%d HALT\n", (p++)-code); break;
  937. case SETFUNCTION:
  938. {
  939. CodeWord c1, c2;
  940. int n = p-code;
  941. p++;
  942. get_word(c1,p);
  943. get_word(c2,p);
  944. printf ("%d SETFUNCTION %d %d\n", n, c1.w, c2.w);
  945. }
  946. break;
  947. case SETLINE:
  948. {
  949. CodeWord c;
  950. int n = p-code;
  951. p++;
  952. get_word(c,p);
  953. printf ("%d SETLINE %d\n", n, c.w);
  954. }
  955. break;
  956. case RESET: printf ("%d RESET\n", (p++)-code); break;
  957. default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
  958. }
  959. }
  960. }
  961. #endif