lua.stx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 3.1 1994/11/02 20:30:53 roberto Exp $";
  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. codereturn();
  243. s_tag($<vWord>3) = LUA_T_FUNCTION;
  244. s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte));
  245. if (s_bvalue($<vWord>3) == NULL)
  246. {
  247. lua_error("not enough memory");
  248. err = 1;
  249. }
  250. memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte));
  251. code = basepc; maxcode=maxcurr;
  252. #if LISTING
  253. PrintCode(code,code+pc);
  254. #endif
  255. }
  256. ;
  257. method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME
  258. {
  259. init_function();
  260. pc=0; basepc=code; maxcurr=maxcode;
  261. nlocalvar=0;
  262. localvar[nlocalvar]=lua_findsymbol("self"); /* self param. */
  263. add_nlocalvar(1);
  264. $<vWord>$ = lua_findconstant($5);
  265. }
  266. '(' parlist ')'
  267. {
  268. if (lua_debug)
  269. {
  270. code_byte(SETFUNCTION);
  271. code_code((Byte *)lua_file[lua_nfile-1]);
  272. code_word($<vWord>6);
  273. }
  274. lua_codeadjust (0);
  275. }
  276. block
  277. END
  278. {
  279. Byte *b;
  280. codereturn();
  281. b = calloc (pc, sizeof(Byte));
  282. if (b == NULL)
  283. {
  284. lua_error("not enough memory");
  285. err = 1;
  286. }
  287. memcpy (b, basepc, pc*sizeof(Byte));
  288. code = basepc; maxcode=maxcurr;
  289. #if LISTING
  290. PrintCode(code,code+pc);
  291. #endif
  292. /* assign function to table field */
  293. pc=maincode; basepc=initcode; maxcurr=maxmain;
  294. nlocalvar=0;
  295. lua_pushvar($<vWord>3+1);
  296. code_byte(PUSHSTRING);
  297. code_word($<vWord>6);
  298. code_byte(PUSHFUNCTION);
  299. code_code(b);
  300. code_byte(STOREINDEXED0);
  301. maincode=pc; initcode=basepc; maxmain=maxcurr;
  302. }
  303. ;
  304. statlist : /* empty */
  305. | statlist stat sc
  306. ;
  307. stat : {
  308. if (lua_debug)
  309. {
  310. code_byte(SETLINE); code_word(lua_linenumber);
  311. }
  312. }
  313. stat1
  314. sc : /* empty */ | ';' ;
  315. stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
  316. {
  317. {
  318. Long elseinit = $6+sizeof(Word)+1;
  319. if (pc - elseinit == 0) /* no else */
  320. {
  321. pc -= sizeof(Word)+1;
  322. elseinit = pc;
  323. }
  324. else
  325. {
  326. basepc[$6] = JMP;
  327. code_word_at(basepc+$6+1, pc - elseinit);
  328. }
  329. basepc[$4] = IFFJMP;
  330. code_word_at(basepc+$4+1,elseinit-($4+sizeof(Word)+1));
  331. }
  332. }
  333. | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
  334. {
  335. basepc[$5] = IFFJMP;
  336. code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
  337. basepc[$7] = UPJMP;
  338. code_word_at(basepc+$7+1, pc - ($<vLong>2));
  339. }
  340. | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
  341. {
  342. basepc[$6] = IFFUPJMP;
  343. code_word_at(basepc+$6+1, pc - ($<vLong>2));
  344. }
  345. | varlist1 '=' exprlist1
  346. {
  347. {
  348. int i;
  349. adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
  350. for (i=nvarbuffer-1; i>=0; i--)
  351. lua_codestore (i);
  352. if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
  353. lua_codeadjust (0);
  354. }
  355. }
  356. | functioncall { code_byte(0); }
  357. | LOCAL localdeclist decinit
  358. { add_nlocalvar($2);
  359. adjust_mult_assign($2, $3, 0);
  360. }
  361. ;
  362. elsepart : /* empty */
  363. | ELSE block
  364. | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
  365. {
  366. {
  367. Long elseinit = $6+sizeof(Word)+1;
  368. if (pc - elseinit == 0) /* no else */
  369. {
  370. pc -= sizeof(Word)+1;
  371. elseinit = pc;
  372. }
  373. else
  374. {
  375. basepc[$6] = JMP;
  376. code_word_at(basepc+$6+1, pc - elseinit);
  377. }
  378. basepc[$4] = IFFJMP;
  379. code_word_at(basepc+$4+1, elseinit - ($4 + sizeof(Word)+1));
  380. }
  381. }
  382. ;
  383. block : {$<vInt>$ = nlocalvar;} statlist ret
  384. {
  385. if (nlocalvar != $<vInt>1)
  386. {
  387. nlocalvar = $<vInt>1;
  388. lua_codeadjust (0);
  389. }
  390. }
  391. ;
  392. ret : /* empty */
  393. | { if (lua_debug){code_byte(SETLINE);code_word(lua_linenumber);}}
  394. RETURN exprlist sc
  395. {
  396. if ($3 < 0) code_byte(MULT_RET);
  397. codereturn();
  398. }
  399. ;
  400. PrepJump : /* empty */
  401. {
  402. $$ = pc;
  403. code_byte(0); /* open space */
  404. code_word (0);
  405. }
  406. expr1 : expr { if ($1 == 0) code_byte(1); }
  407. ;
  408. expr : '(' expr ')' { $$ = $2; }
  409. | expr1 EQ expr1 { code_byte(EQOP); $$ = 1; }
  410. | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; }
  411. | expr1 '>' expr1 { code_byte(LEOP); code_byte(NOTOP); $$ = 1; }
  412. | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; }
  413. | expr1 LE expr1 { code_byte(LEOP); $$ = 1; }
  414. | expr1 GE expr1 { code_byte(LTOP); code_byte(NOTOP); $$ = 1; }
  415. | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; }
  416. | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; }
  417. | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; }
  418. | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; }
  419. | expr1 '^' expr1 { code_byte(POWOP); $$ = 1; }
  420. | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; }
  421. | '+' expr1 %prec UNARY { $$ = 1; }
  422. | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;}
  423. | table { $$ = 1; }
  424. | varexp { $$ = 1;}
  425. | NUMBER { code_number($1); $$ = 1; }
  426. | STRING
  427. {
  428. code_byte(PUSHSTRING);
  429. code_word(lua_findconstant($1));
  430. $$ = 1;
  431. }
  432. | NIL {code_byte(PUSHNIL); $$ = 1; }
  433. | functioncall
  434. {
  435. $$ = 0;
  436. if (lua_debug)
  437. {
  438. code_byte(SETLINE); code_word(lua_linenumber);
  439. }
  440. }
  441. | NOT expr1 { code_byte(NOTOP); $$ = 1;}
  442. | expr1 AND PrepJump {code_byte(POP); } expr1
  443. {
  444. basepc[$3] = ONFJMP;
  445. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  446. $$ = 1;
  447. }
  448. | expr1 OR PrepJump {code_byte(POP); } expr1
  449. {
  450. basepc[$3] = ONTJMP;
  451. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  452. $$ = 1;
  453. }
  454. ;
  455. table :
  456. {
  457. code_byte(CREATEARRAY);
  458. $<vLong>$ = pc; code_word(0);
  459. }
  460. '{' fieldlist '}'
  461. {
  462. code_word_at(basepc+$<vLong>1, $3);
  463. }
  464. ;
  465. functioncall : funcvalue funcParams
  466. { code_byte(CALLFUNC); code_byte($1+$2); }
  467. ;
  468. funcvalue : varexp { $$ = 0; }
  469. | varexp ':' NAME
  470. {
  471. code_byte(PUSHSTRING);
  472. code_word(lua_findconstant($3));
  473. code_byte(PUSHSELF);
  474. $$ = 1;
  475. }
  476. ;
  477. funcParams : '(' exprlist ')'
  478. { if ($2<0) { code_byte(1); $$ = -$2; } else $$ = $2; }
  479. | table { $$ = 1; }
  480. ;
  481. exprlist : /* empty */ { $$ = 0; }
  482. | exprlist1 { $$ = $1; }
  483. ;
  484. exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
  485. | exprlist1 ',' { if ($1 < 0) code_byte(1); } expr
  486. {
  487. int r = $1 < 0 ? -$1 : $1;
  488. $$ = ($4 == 0) ? -(r+1) : r+1;
  489. }
  490. ;
  491. parlist : /* empty */
  492. | parlist1
  493. ;
  494. parlist1 : NAME
  495. {
  496. localvar[nlocalvar]=lua_findsymbol($1);
  497. add_nlocalvar(1);
  498. }
  499. | parlist1 ',' NAME
  500. {
  501. localvar[nlocalvar]=lua_findsymbol($3);
  502. add_nlocalvar(1);
  503. }
  504. ;
  505. fieldlist : /* empty */ { $$ = 0; }
  506. | lfieldlist1 lastcomma
  507. { $$ = $1; flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  508. | ffieldlist1 lastcomma
  509. { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
  510. | lfieldlist1 ';'
  511. { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  512. ffieldlist1 lastcomma
  513. { $$ = $1+$4; flush_record($4%FIELDS_PER_FLUSH); }
  514. ;
  515. lastcomma : /* empty */
  516. | ','
  517. ;
  518. ffieldlist1 : ffield {$$=1;}
  519. | ffieldlist1 ',' ffield
  520. {
  521. $$=$1+1;
  522. if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
  523. }
  524. ;
  525. ffield : NAME {$<vWord>$ = lua_findconstant($1);} '=' expr1
  526. {
  527. push_field($<vWord>2);
  528. }
  529. ;
  530. lfieldlist1 : expr1 {$$=1;}
  531. | lfieldlist1 ',' expr1
  532. {
  533. $$=$1+1;
  534. if ($$%FIELDS_PER_FLUSH == 0)
  535. flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
  536. }
  537. ;
  538. varlist1 : var
  539. {
  540. nvarbuffer = 0;
  541. varbuffer[nvarbuffer] = $1; incr_nvarbuffer();
  542. $$ = ($1 == 0) ? 1 : 0;
  543. }
  544. | varlist1 ',' var
  545. {
  546. varbuffer[nvarbuffer] = $3; incr_nvarbuffer();
  547. $$ = ($3 == 0) ? $1 + 1 : $1;
  548. }
  549. ;
  550. var : singlevar { $$ = $1; }
  551. | varexp '[' expr1 ']'
  552. {
  553. $$ = 0; /* indexed variable */
  554. }
  555. | varexp '.' NAME
  556. {
  557. code_byte(PUSHSTRING);
  558. code_word(lua_findconstant($3));
  559. $$ = 0; /* indexed variable */
  560. }
  561. ;
  562. singlevar : NAME
  563. {
  564. Word s = lua_findsymbol($1);
  565. int local = lua_localname (s);
  566. if (local == -1) /* global var */
  567. $$ = s + 1; /* return positive value */
  568. else
  569. $$ = -(local+1); /* return negative value */
  570. }
  571. ;
  572. varexp : var { lua_pushvar($1); }
  573. ;
  574. localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;}
  575. | localdeclist ',' NAME
  576. {
  577. localvar[nlocalvar+$1]=lua_findsymbol($3);
  578. $$ = $1+1;
  579. }
  580. ;
  581. decinit : /* empty */ { $$ = 0; }
  582. | '=' exprlist1 { $$ = $2; }
  583. ;
  584. setdebug : DEBUG {lua_debug = $1;}
  585. %%
  586. /*
  587. ** Search a local name and if find return its index. If do not find return -1
  588. */
  589. static int lua_localname (Word n)
  590. {
  591. int i;
  592. for (i=nlocalvar-1; i >= 0; i--)
  593. if (n == localvar[i]) return i; /* local var */
  594. return -1; /* global var */
  595. }
  596. /*
  597. ** Push a variable given a number. If number is positive, push global variable
  598. ** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
  599. ** Otherwise, if zero, push indexed variable (record).
  600. */
  601. static void lua_pushvar (long number)
  602. {
  603. if (number > 0) /* global var */
  604. {
  605. code_byte(PUSHGLOBAL);
  606. code_word(number-1);
  607. }
  608. else if (number < 0) /* local var */
  609. {
  610. number = (-number) - 1;
  611. if (number < 10) code_byte(PUSHLOCAL0 + number);
  612. else
  613. {
  614. code_byte(PUSHLOCAL);
  615. code_byte(number);
  616. }
  617. }
  618. else
  619. {
  620. code_byte(PUSHINDEXED);
  621. }
  622. }
  623. static void lua_codeadjust (int n)
  624. {
  625. if (n+nlocalvar == 0)
  626. code_byte(ADJUST0);
  627. else
  628. {
  629. code_byte(ADJUST);
  630. code_byte(n+nlocalvar);
  631. }
  632. }
  633. static void codereturn (void)
  634. {
  635. if (lua_debug) code_byte(RESET);
  636. if (nlocalvar == 0)
  637. code_byte(RETCODE0);
  638. else
  639. {
  640. code_byte(RETCODE);
  641. code_byte(nlocalvar);
  642. }
  643. }
  644. static void adjust_mult_assign (int vars, int exps, int temps)
  645. {
  646. if (exps < 0)
  647. {
  648. int r = vars - (-exps-1);
  649. if (r >= 0)
  650. code_byte(r);
  651. else
  652. {
  653. code_byte(0);
  654. lua_codeadjust(temps);
  655. }
  656. }
  657. else if (vars != exps)
  658. lua_codeadjust(temps);
  659. }
  660. static void lua_codestore (int i)
  661. {
  662. if (varbuffer[i] > 0) /* global var */
  663. {
  664. code_byte(STOREGLOBAL);
  665. code_word(varbuffer[i]-1);
  666. }
  667. else if (varbuffer[i] < 0) /* local var */
  668. {
  669. int number = (-varbuffer[i]) - 1;
  670. if (number < 10) code_byte(STORELOCAL0 + number);
  671. else
  672. {
  673. code_byte(STORELOCAL);
  674. code_byte(number);
  675. }
  676. }
  677. else /* indexed var */
  678. {
  679. int j;
  680. int upper=0; /* number of indexed variables upper */
  681. int param; /* number of itens until indexed expression */
  682. for (j=i+1; j <nvarbuffer; j++)
  683. if (varbuffer[j] == 0) upper++;
  684. param = upper*2 + i;
  685. if (param == 0)
  686. code_byte(STOREINDEXED0);
  687. else
  688. {
  689. code_byte(STOREINDEXED);
  690. code_byte(param);
  691. }
  692. }
  693. }
  694. void yyerror (char *s)
  695. {
  696. static char msg[256];
  697. sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
  698. s, lua_lasttext (), lua_linenumber, lua_filename());
  699. lua_error (msg);
  700. err = 1;
  701. }
  702. int yywrap (void)
  703. {
  704. return 1;
  705. }
  706. /*
  707. ** Parse LUA code and returns global statements.
  708. */
  709. Byte *lua_parse (void)
  710. {
  711. Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
  712. maincode = 0;
  713. maxmain = CODE_BLOCK;
  714. if (init == NULL)
  715. {
  716. lua_error("not enough memory");
  717. return NULL;
  718. }
  719. err = 0;
  720. if (yyparse () || (err==1)) return NULL;
  721. initcode[maincode++] = RETCODE0;
  722. init = initcode;
  723. #if LISTING
  724. { static void PrintCode (Byte *code, Byte *end);
  725. PrintCode(init,init+maincode); }
  726. #endif
  727. return init;
  728. }
  729. #if LISTING
  730. static void PrintCode (Byte *code, Byte *end)
  731. {
  732. Byte *p = code;
  733. printf ("\n\nCODE\n");
  734. while (p != end)
  735. {
  736. switch ((OpCode)*p)
  737. {
  738. case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
  739. case PUSH0: case PUSH1: case PUSH2:
  740. printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
  741. p++;
  742. break;
  743. case PUSHBYTE:
  744. printf ("%d PUSHBYTE %d\n", p-code, *(++p));
  745. p++;
  746. break;
  747. case PUSHWORD:
  748. {
  749. CodeWord c;
  750. int n = p-code;
  751. p++;
  752. get_word(c,p);
  753. printf ("%d PUSHWORD %d\n", n, c.w);
  754. }
  755. break;
  756. case PUSHFLOAT:
  757. {
  758. CodeFloat c;
  759. int n = p-code;
  760. p++;
  761. get_float(c,p);
  762. printf ("%d PUSHFLOAT %f\n", n, c.f);
  763. }
  764. break;
  765. case PUSHSTRING:
  766. {
  767. CodeWord c;
  768. int n = p-code;
  769. p++;
  770. get_word(c,p);
  771. printf ("%d PUSHSTRING %d\n", n, c.w);
  772. }
  773. break;
  774. case PUSHFUNCTION:
  775. {
  776. CodeCode c;
  777. int n = p-code;
  778. p++;
  779. get_code(c,p);
  780. printf ("%d PUSHFUNCTION %p\n", n, c.b);
  781. }
  782. break;
  783. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
  784. case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
  785. case PUSHLOCAL8: case PUSHLOCAL9:
  786. printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
  787. p++;
  788. break;
  789. case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
  790. p++;
  791. break;
  792. case PUSHGLOBAL:
  793. {
  794. CodeWord c;
  795. int n = p-code;
  796. p++;
  797. get_word(c,p);
  798. printf ("%d PUSHGLOBAL %d\n", n, c.w);
  799. }
  800. break;
  801. case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
  802. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
  803. case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
  804. case STORELOCAL8: case STORELOCAL9:
  805. printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
  806. p++;
  807. break;
  808. case STORELOCAL:
  809. printf ("%d STORELOCAL %d\n", p-code, *(++p));
  810. p++;
  811. break;
  812. case STOREGLOBAL:
  813. {
  814. CodeWord c;
  815. int n = p-code;
  816. p++;
  817. get_word(c,p);
  818. printf ("%d STOREGLOBAL %d\n", n, c.w);
  819. }
  820. break;
  821. case PUSHSELF: printf ("%d PUSHSELF\n", (p++)-code); break;
  822. case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
  823. case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
  824. p++;
  825. break;
  826. case STORELIST0:
  827. printf("%d STORELIST0 %d\n", p-code, *(++p));
  828. p++;
  829. break;
  830. case STORELIST:
  831. printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
  832. p+=3;
  833. break;
  834. case STORERECORD:
  835. printf("%d STORERECORD %d\n", p-code, *(++p));
  836. p += *p*sizeof(Word) + 1;
  837. break;
  838. case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
  839. case ADJUST:
  840. printf ("%d ADJUST %d\n", p-code, *(++p));
  841. p++;
  842. break;
  843. case CREATEARRAY:
  844. {
  845. CodeWord c;
  846. int n = p-code;
  847. p++;
  848. get_word(c,p);
  849. printf ("%d CREATEARRAY %d\n", n, c.w);
  850. break;
  851. }
  852. case EQOP: printf ("%d EQOP\n", (p++)-code); break;
  853. case LTOP: printf ("%d LTOP\n", (p++)-code); break;
  854. case LEOP: printf ("%d LEOP\n", (p++)-code); break;
  855. case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
  856. case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
  857. case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
  858. case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
  859. case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
  860. case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
  861. case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
  862. case ONTJMP:
  863. {
  864. CodeWord c;
  865. int n = p-code;
  866. p++;
  867. get_word(c,p);
  868. printf ("%d ONTJMP %d\n", n, c.w);
  869. }
  870. break;
  871. case ONFJMP:
  872. {
  873. CodeWord c;
  874. int n = p-code;
  875. p++;
  876. get_word(c,p);
  877. printf ("%d ONFJMP %d\n", n, c.w);
  878. }
  879. break;
  880. case JMP:
  881. {
  882. CodeWord c;
  883. int n = p-code;
  884. p++;
  885. get_word(c,p);
  886. printf ("%d JMP %d\n", n, c.w);
  887. }
  888. break;
  889. case UPJMP:
  890. {
  891. CodeWord c;
  892. int n = p-code;
  893. p++;
  894. get_word(c,p);
  895. printf ("%d UPJMP %d\n", n, c.w);
  896. }
  897. break;
  898. case IFFJMP:
  899. {
  900. CodeWord c;
  901. int n = p-code;
  902. p++;
  903. get_word(c,p);
  904. printf ("%d IFFJMP %d\n", n, c.w);
  905. }
  906. break;
  907. case IFFUPJMP:
  908. {
  909. CodeWord c;
  910. int n = p-code;
  911. p++;
  912. get_word(c,p);
  913. printf ("%d IFFUPJMP %d\n", n, c.w);
  914. }
  915. break;
  916. case POP: printf ("%d POP\n", (p++)-code); break;
  917. case CALLFUNC:
  918. printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
  919. p+=3;
  920. break;
  921. case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
  922. case RETCODE:
  923. printf ("%d RETCODE %d\n", p-code, *(++p));
  924. p++;
  925. break;
  926. case SETFUNCTION:
  927. {
  928. CodeCode c1;
  929. CodeWord c2;
  930. int n = p-code;
  931. p++;
  932. get_code(c1,p);
  933. get_word(c2,p);
  934. printf ("%d SETFUNCTION %s %d\n", n, (char *)c1.b, c2.w);
  935. }
  936. break;
  937. case SETLINE:
  938. {
  939. CodeWord c;
  940. int n = p-code;
  941. p++;
  942. get_word(c,p);
  943. printf ("%d SETLINE %d\n", n, c.w);
  944. }
  945. break;
  946. case RESET: printf ("%d RESET\n", (p++)-code); break;
  947. default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
  948. }
  949. }
  950. }
  951. #endif