lua.stx 21 KB

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