lua.stx 21 KB

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