lua.stx 21 KB

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