lua.stx 22 KB

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