lua.stx 22 KB

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