lua.stx 22 KB

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