lua.stx 22 KB

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