lua.stx 22 KB

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