lua.stx 21 KB

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