lua.stx 22 KB

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