lua.stx 21 KB

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