lua.stx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. %{
  2. /*
  3. ** $Id: lua.stx,v 1.3 1997/09/19 21:17:52 roberto Exp roberto $
  4. ** Syntax analizer and code generator
  5. ** See Copyright Notice in lua.h
  6. */
  7. #include <stdlib.h>
  8. #include "lauxlib.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lopcodes.h"
  14. #include "lparser.h"
  15. #include "lstring.h"
  16. #include "lua.h"
  17. #include "luadebug.h"
  18. #include "lzio.h"
  19. /* to avoid warnings generated by yacc */
  20. int luaY_parse (void);
  21. #define malloc luaM_malloc
  22. #define realloc luaM_realloc
  23. #define free luaM_free
  24. /* maximum number of local variables */
  25. #define MAXLOCALS 32
  26. /* maximum number of variables in a multiple assignment */
  27. #define MAXVAR 32
  28. /* maximum number of nested functions */
  29. #define MAXSTATES 6
  30. /* maximum number of upvalues */
  31. #define MAXUPVALUES 8
  32. /*
  33. ** Variable descriptor: if n>0, represents global variable indexed
  34. ** by (n-1); if n<0, represents local variable index (-n)-1;
  35. ** if n==0, represents an indexed variable (table and index on top of stack)
  36. ** Must be long to store negative Word values.
  37. */
  38. typedef long vardesc;
  39. /* state needed to generate code for a given function */
  40. typedef struct State {
  41. TProtoFunc *f; /* current function header */
  42. int pc; /* next position to code */
  43. TaggedString *localvar[MAXLOCALS]; /* store local variable names */
  44. int stacksize; /* number of values on activation register */
  45. int maxstacksize; /* maximum number of values on activation register */
  46. int nlocalvar; /* number of active local variables */
  47. int nvars; /* number of entries in f->locvars */
  48. int maxcode; /* size of f->code */
  49. int maxvars; /* size of f->locvars (-1 if no debug information) */
  50. int maxconsts; /* size of f->consts */
  51. vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
  52. vardesc upvalues[MAXUPVALUES]; /* upvalues */
  53. } State;
  54. static State *mainState, *currState;
  55. void luaY_syntaxerror (char *s, char *token)
  56. {
  57. if (token[0] == 0)
  58. token = "<eof>";
  59. luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s",
  60. s, token, luaX_linenumber, mainState->f->fileName->str);
  61. }
  62. void luaY_error (char *s)
  63. {
  64. luaY_syntaxerror(s, luaX_lasttoken());
  65. }
  66. static void code_byte (Byte c)
  67. {
  68. if (currState->pc >= currState->maxcode)
  69. currState->maxcode = luaM_growvector(&currState->f->code,
  70. currState->maxcode, Byte, codeEM, MAX_INT);
  71. currState->f->code[currState->pc++] = c;
  72. }
  73. static void code_word_at (int pc, int n)
  74. {
  75. Word w = n;
  76. if (w != n)
  77. luaY_error("block too big");
  78. currState->f->code[pc] = n&0xFF;
  79. currState->f->code[pc+1] = n>>8;
  80. }
  81. static void code_word (int n)
  82. {
  83. code_byte(n&0xFF);
  84. code_byte(n>>8);
  85. }
  86. static void deltastack (int delta)
  87. {
  88. currState->stacksize += delta;
  89. if (currState->stacksize > currState->maxstacksize) {
  90. if (currState->stacksize > 255)
  91. luaY_error("expression too complex");
  92. currState->maxstacksize = currState->stacksize;
  93. }
  94. }
  95. static void code_opcode (OpCode op, int delta)
  96. {
  97. code_byte(op);
  98. deltastack(delta);
  99. }
  100. static void code_opborw(OpCode opbyte, int arg, int delta)
  101. {
  102. if (arg <= 255) {
  103. code_opcode(opbyte, delta);
  104. code_byte(arg);
  105. }
  106. else {
  107. code_opcode(opbyte+1, delta);
  108. code_word(arg);
  109. }
  110. }
  111. static void code_oparg (OpCode firstop, OpCode opbyte, int arg, int delta)
  112. {
  113. if (firstop+arg < opbyte)
  114. code_opcode(firstop+arg, delta);
  115. else
  116. code_opborw(opbyte, arg, delta);
  117. }
  118. static void code_pop (OpCode op)
  119. {
  120. code_opcode(op, -1);
  121. }
  122. /* binary operations get 2 arguments and leave one, so they pop one */
  123. #define code_binop(op) code_pop(op)
  124. #define code_neutralop(op) code_byte(op)
  125. /* unary operations get 1 argument and leave one, so they are neutral */
  126. #define code_unop(op) code_neutralop(op)
  127. static void code_constant (int c)
  128. {
  129. code_opborw(PUSHCONSTANTB, c, 1);
  130. }
  131. static int next_constant (State *cs)
  132. {
  133. TProtoFunc *f = cs->f;
  134. if (f->nconsts >= cs->maxconsts) {
  135. cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject,
  136. constantEM, MAX_WORD);
  137. }
  138. return f->nconsts++;
  139. }
  140. static int string_constant (TaggedString *s, State *cs)
  141. {
  142. TProtoFunc *f = cs->f;
  143. int c = s->u.s.constindex;
  144. if (!(0 <= c && c < f->nconsts &&
  145. ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
  146. c = next_constant(cs);
  147. ttype(&f->consts[c]) = LUA_T_STRING;
  148. tsvalue(&f->consts[c]) = s;
  149. s->u.s.constindex = c; /* hint for next time */
  150. }
  151. return c;
  152. }
  153. static void code_string (TaggedString *s)
  154. {
  155. code_constant(string_constant(s, currState));
  156. }
  157. #define LIM 13
  158. static int real_constant (real r)
  159. {
  160. /* check whether 'r' has appeared within the last LIM entries */
  161. TObject *cnt = currState->f->consts;
  162. int c = currState->f->nconsts;
  163. int lim = c < LIM ? 0 : c-LIM;
  164. while (--c >= lim) {
  165. if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
  166. return c;
  167. }
  168. /* not found; create a luaM_new entry */
  169. c = next_constant(currState);
  170. cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
  171. ttype(&cnt[c]) = LUA_T_NUMBER;
  172. nvalue(&cnt[c]) = r;
  173. return c;
  174. }
  175. static void code_number (real f)
  176. {
  177. Word i;
  178. if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f)
  179. code_oparg(PUSH0, PUSHBYTE, i, 1); /* f has an (short) integer value */
  180. else
  181. code_constant(real_constant(f));
  182. }
  183. static void flush_record (int n)
  184. {
  185. if (n == 0) return;
  186. code_opcode(SETMAP, -2*n);
  187. code_byte(n);
  188. }
  189. static void flush_list (int m, int n)
  190. {
  191. if (n == 0) return;
  192. if (m == 0)
  193. code_opcode(SETLIST0, -n);
  194. else if (m < 255) {
  195. code_opcode(SETLIST, -n);
  196. code_byte(m);
  197. }
  198. else
  199. luaY_error("list constructor too long");
  200. code_byte(n);
  201. }
  202. static void luaI_registerlocalvar (TaggedString *varname, int line)
  203. {
  204. if (currState->maxvars != -1) { /* debug information? */
  205. if (currState->nvars >= currState->maxvars)
  206. currState->maxvars = luaM_growvector(&currState->f->locvars,
  207. currState->maxvars, LocVar, "", MAX_WORD);
  208. currState->f->locvars[currState->nvars].varname = varname;
  209. currState->f->locvars[currState->nvars].line = line;
  210. currState->nvars++;
  211. }
  212. }
  213. static void luaI_unregisterlocalvar (int line)
  214. {
  215. luaI_registerlocalvar(NULL, line);
  216. }
  217. static void store_localvar (TaggedString *name, int n)
  218. {
  219. if (currState->nlocalvar+n < MAXLOCALS)
  220. currState->localvar[currState->nlocalvar+n] = name;
  221. else
  222. luaY_error("too many local variables");
  223. luaI_registerlocalvar(name, luaX_linenumber);
  224. }
  225. static void add_localvar (TaggedString *name)
  226. {
  227. store_localvar(name, 0);
  228. currState->nlocalvar++;
  229. }
  230. static void add_varbuffer (vardesc var, int n)
  231. {
  232. if (n >= MAXVAR)
  233. luaY_error("variable buffer overflow");
  234. currState->varbuffer[n] = var;
  235. }
  236. static int aux_localname (TaggedString *n, State *st)
  237. {
  238. int i;
  239. for (i=st->nlocalvar-1; i >= 0; i--)
  240. if (n == st->localvar[i]) return i; /* local var index */
  241. return -1; /* not found */
  242. }
  243. static vardesc singlevar (TaggedString *n, State *st)
  244. {
  245. int i = aux_localname(n, st);
  246. if (i == -1) { /* check shadowing */
  247. int l;
  248. for (l=1; l<=(st-mainState); l++)
  249. if (aux_localname(n, st-l) >= 0)
  250. luaY_syntaxerror("cannot access a variable in outer scope", n->str);
  251. return string_constant(n, st)+1; /* positive value */
  252. }
  253. else return -(i+1); /* negative value */
  254. }
  255. static int indexupvalue (TaggedString *n)
  256. {
  257. vardesc v = singlevar(n, currState-1);
  258. int i;
  259. for (i=0; i<currState->f->nupvalues; i++) {
  260. if (currState->upvalues[i] == v)
  261. return i;
  262. }
  263. /* new one */
  264. if (++currState->f->nupvalues > MAXUPVALUES)
  265. luaY_error("too many upvalues in a single function");
  266. currState->upvalues[i] = v; /* i = currState->f->nupvalues - 1 */
  267. return i;
  268. }
  269. static void pushupvalue (TaggedString *n)
  270. {
  271. int i;
  272. if (currState == mainState)
  273. luaY_error("cannot access upvalue in main");
  274. if (aux_localname(n, currState) >= 0)
  275. luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
  276. i = indexupvalue(n);
  277. code_oparg(PUSHUPVALUE0, PUSHUPVALUE, i, 1);
  278. }
  279. void luaY_codedebugline (int line)
  280. {
  281. static int lastline = 0;
  282. if (lua_debug && line != lastline) {
  283. code_neutralop(SETLINE);
  284. code_word(line);
  285. lastline = line;
  286. }
  287. }
  288. static void adjuststack (int n)
  289. {
  290. if (n > 0)
  291. code_oparg(POP1-1, POPS, n, -n); /* POP1-1 = POP0 */
  292. else if (n < 0)
  293. code_oparg(PUSHNIL-1, PUSHNILS, -n, -n); /* PUSHNIL1-1 = PUSHNIL0 */
  294. }
  295. static long adjust_functioncall (long exp, int i)
  296. {
  297. if (exp <= 0)
  298. return -exp; /* exp is -list length */
  299. else {
  300. int temp = currState->f->code[exp];
  301. currState->f->code[exp] = i;
  302. if (i != MULT_RET)
  303. deltastack(i);
  304. return temp+i;
  305. }
  306. }
  307. static void adjust_mult_assign (int vars, long exps)
  308. {
  309. if (exps > 0) { /* must correct function call */
  310. int diff = currState->f->code[exps] - vars;
  311. if (diff < 0)
  312. adjust_functioncall(exps, -diff);
  313. else {
  314. adjust_functioncall(exps, 0);
  315. adjuststack(diff);
  316. }
  317. }
  318. else adjuststack((-exps)-vars);
  319. }
  320. static void code_args (int dots)
  321. {
  322. if (!dots) {
  323. code_opcode(ARGS, currState->nlocalvar);
  324. code_byte(currState->nlocalvar);
  325. }
  326. else {
  327. code_opcode(VARARGS, currState->nlocalvar+1);
  328. code_byte(currState->nlocalvar);
  329. add_localvar(luaS_new("arg"));
  330. }
  331. }
  332. static void lua_pushvar (vardesc number)
  333. {
  334. if (number > 0) /* global var */
  335. code_opborw(GETGLOBALB, number-1, 1);
  336. else if (number < 0) /* local var */
  337. code_oparg(PUSHLOCAL0, PUSHLOCAL, (-number)-1, 1);
  338. else
  339. code_pop(GETTABLE);
  340. }
  341. static void storevar (vardesc number)
  342. {
  343. if (number == 0) /* indexed var */
  344. code_opcode(SETTABLE0, -3);
  345. else if (number > 0) /* global var */
  346. code_opborw(SETGLOBALB, number-1, -1);
  347. else /* number < 0 - local var */
  348. code_oparg(SETLOCAL0, SETLOCAL, (-number)-1, -1);
  349. }
  350. /* returns how many elements are left as 'garbage' on the stack */
  351. static int lua_codestore (int i, int left)
  352. {
  353. if (currState->varbuffer[i] != 0 || /* global or local var or */
  354. left+i == 0) { /* indexed var without values in between */
  355. storevar(currState->varbuffer[i]);
  356. return left;
  357. }
  358. else { /* indexed var with values in between*/
  359. code_pop(SETTABLE);
  360. code_byte(left+i); /* number of elements between table/index and value */
  361. return left+2; /* table/index are not poped, since they are not on top */
  362. }
  363. }
  364. static void codeIf (int thenAdd, int elseAdd)
  365. {
  366. int elseinit = elseAdd+sizeof(Word)+1;
  367. if (currState->pc == elseinit) { /* no else part */
  368. currState->pc -= sizeof(Word)+1;
  369. elseinit = currState->pc;
  370. }
  371. else {
  372. currState->f->code[elseAdd] = JMP;
  373. code_word_at(elseAdd+1, currState->pc-elseinit);
  374. }
  375. currState->f->code[thenAdd] = IFFJMP;
  376. code_word_at(thenAdd+1, elseinit-(thenAdd+sizeof(Word)+1));
  377. }
  378. static void code_shortcircuit (int pc, Byte jmp)
  379. {
  380. currState->f->code[pc] = jmp;
  381. code_word_at(pc+1, currState->pc - (pc + sizeof(Word)+1));
  382. }
  383. static void codereturn (void)
  384. {
  385. code_neutralop(RETCODE);
  386. code_byte(currState->nlocalvar);
  387. currState->stacksize = currState->nlocalvar;
  388. }
  389. static void func_onstack (TProtoFunc *f)
  390. {
  391. int i;
  392. int nupvalues = (currState+1)->f->nupvalues;
  393. int c = next_constant(currState);
  394. ttype(&currState->f->consts[c]) = LUA_T_PROTO;
  395. currState->f->consts[c].value.tf = (currState+1)->f;
  396. for (i=0; i<nupvalues; i++)
  397. lua_pushvar((currState+1)->upvalues[i]);
  398. code_opborw(CLOSUREB, c, 1-nupvalues);
  399. }
  400. static void init_state (TaggedString *filename)
  401. {
  402. TProtoFunc *f = luaF_newproto();
  403. currState->stacksize = 0;
  404. currState->maxstacksize = 0;
  405. currState->nlocalvar = 0;
  406. currState->f = f;
  407. f->fileName = filename;
  408. currState->pc = 0;
  409. currState->maxcode = 0;
  410. f->code = NULL;
  411. currState->maxconsts = 0;
  412. if (lua_debug) {
  413. currState->nvars = 0;
  414. currState->maxvars = 0;
  415. }
  416. else
  417. currState->maxvars = -1; /* flag no debug information */
  418. code_byte(0); /* to be filled with stacksize */
  419. }
  420. static void init_func (void)
  421. {
  422. if (currState-mainState >= MAXSTATES-1)
  423. luaY_error("too many nested functions");
  424. currState++;
  425. init_state(mainState->f->fileName);
  426. luaY_codedebugline(luaX_linenumber);
  427. currState->f->lineDefined = luaX_linenumber;
  428. }
  429. static TProtoFunc *close_func (void)
  430. {
  431. TProtoFunc *f = currState->f;
  432. code_neutralop(ENDCODE);
  433. f->code[0] = currState->maxstacksize;
  434. f->code = luaM_reallocvector(f->code, currState->pc, Byte);
  435. f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
  436. if (currState->maxvars != -1) { /* debug information? */
  437. luaI_registerlocalvar(NULL, -1); /* flag end of vector */
  438. f->locvars = luaM_reallocvector(f->locvars, currState->nvars, LocVar);
  439. }
  440. currState--;
  441. return f;
  442. }
  443. /*
  444. ** Parse LUA code.
  445. */
  446. TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
  447. {
  448. State state[MAXSTATES];
  449. currState = mainState = &state[0];
  450. luaX_setinput(z);
  451. init_state(luaS_new(chunkname));
  452. if (luaY_parse ()) lua_error("parse error");
  453. return close_func();
  454. }
  455. %}
  456. %union
  457. {
  458. int vInt;
  459. real vReal;
  460. char *pChar;
  461. long vLong;
  462. TaggedString *pTStr;
  463. TProtoFunc *pFunc;
  464. }
  465. %start chunk
  466. %token WRONGTOKEN
  467. %token NIL
  468. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  469. %token RETURN
  470. %token LOCAL
  471. %token FUNCTION
  472. %token DOTS
  473. %token <vReal> NUMBER
  474. %token <pTStr> NAME STRING
  475. %type <vInt> PrepJump, PrepJumpPop
  476. %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
  477. counter (which has list length); if <= 0, -list lenght */
  478. %type <vLong> functioncall, expr /* if != 0, points to function return
  479. counter */
  480. %type <vInt> varlist1, funcParams, funcvalue
  481. %type <vInt> fieldlist, localdeclist, decinit
  482. %type <vInt> ffieldlist, ffieldlist1, semicolonpart
  483. %type <vInt> lfieldlist, lfieldlist1
  484. %type <vInt> parlist1, par
  485. %type <vLong> var, singlevar, funcname /* vardesc */
  486. %type <pFunc> body
  487. %left AND OR
  488. %left EQ NE '>' '<' LE GE
  489. %left CONC
  490. %left '+' '-'
  491. %left '*' '/'
  492. %left UNARY NOT
  493. %right '^'
  494. %% /* beginning of rules section */
  495. chunk : statlist ret
  496. ;
  497. statlist : /* empty */
  498. | statlist stat sc
  499. ;
  500. sc : /* empty */ | ';' ;
  501. stat : IF expr1 THEN PrepJumpPop block PrepJump elsepart END
  502. { codeIf($4, $6); }
  503. | WHILE {$<vInt>$=currState->pc;} expr1 DO PrepJumpPop block PrepJump END
  504. {
  505. currState->f->code[$5] = IFFJMP;
  506. code_word_at($5+1, currState->pc - ($5+sizeof(Word)+1));
  507. currState->f->code[$7] = UPJMP;
  508. code_word_at($7+1, currState->pc - ($<vInt>2));
  509. }
  510. | REPEAT {$<vInt>$=currState->pc;} block UNTIL expr1 PrepJumpPop
  511. {
  512. currState->f->code[$6] = IFFUPJMP;
  513. code_word_at($6+1, currState->pc - ($<vInt>2));
  514. }
  515. | varlist1 '=' exprlist1
  516. {{
  517. int i;
  518. int left = 0;
  519. adjust_mult_assign($1, $3);
  520. for (i=$1-1; i>=0; i--)
  521. left = lua_codestore(i, left);
  522. adjuststack(left); /* remove eventual 'garbage' left on stack */
  523. }}
  524. | functioncall
  525. | LOCAL localdeclist decinit
  526. {
  527. currState->nlocalvar += $2;
  528. adjust_mult_assign($2, $3);
  529. }
  530. | FUNCTION funcname body
  531. {
  532. func_onstack($3);
  533. storevar($2);
  534. }
  535. ;
  536. block : {$<vInt>$ = currState->nlocalvar;} chunk
  537. {
  538. adjuststack(currState->nlocalvar - $<vInt>1);
  539. for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--)
  540. luaI_unregisterlocalvar(luaX_linenumber);
  541. }
  542. ;
  543. funcname : var { init_func(); $$ = $1; }
  544. | varexp ':' NAME
  545. {
  546. code_string($3);
  547. $$ = 0; /* indexed variable */
  548. init_func();
  549. add_localvar(luaS_new("self"));
  550. }
  551. ;
  552. body : '(' parlist ')' chunk END { $$ = close_func(); }
  553. ;
  554. elsepart : /* empty */
  555. | ELSE block
  556. | ELSEIF expr1 THEN PrepJumpPop block PrepJump elsepart
  557. { codeIf($4, $6); }
  558. ;
  559. ret : /* empty */
  560. | RETURN exprlist sc
  561. {
  562. adjust_functioncall($2, MULT_RET);
  563. codereturn();
  564. }
  565. ;
  566. PrepJump : /* empty */
  567. {
  568. $$ = currState->pc;
  569. code_byte(0); /* open space */
  570. code_word(0);
  571. }
  572. ;
  573. PrepJumpPop : PrepJump { $$ = $1; deltastack(-1); /* pop condition */ }
  574. ;
  575. expr1 : expr { adjust_functioncall($1, 1); }
  576. ;
  577. expr : '(' expr ')' { $$ = $2; }
  578. | expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
  579. | expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
  580. | expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
  581. | expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
  582. | expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
  583. | expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
  584. | expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
  585. | expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
  586. | expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
  587. | expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
  588. | expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
  589. | expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
  590. | '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
  591. | NOT expr1 { code_unop(NOTOP); $$ = 0;}
  592. | table { $$ = 0; }
  593. | varexp { $$ = 0;}
  594. | NUMBER { code_number($1); $$ = 0; }
  595. | STRING { code_string($1); $$ = 0; }
  596. | NIL {code_opcode(PUSHNIL, 1); $$ = 0; }
  597. | functioncall { $$ = $1; }
  598. | expr1 AND PrepJumpPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
  599. | expr1 OR PrepJumpPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
  600. | FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
  601. ;
  602. table :
  603. { code_opcode(CREATEARRAY, 1); $<vInt>$ = currState->pc; code_word(0); }
  604. '{' fieldlist '}'
  605. { code_word_at($<vInt>1, $3); }
  606. ;
  607. functioncall : funcvalue funcParams
  608. {
  609. code_opcode(CALLFUNC, -($1+$2+1)); /* ajdust counts results */
  610. code_byte($1+$2);
  611. $$ = currState->pc;
  612. code_byte(0); /* may be adjusted by other rules */
  613. }
  614. ;
  615. funcvalue : varexp { $$ = 0; }
  616. | varexp ':' NAME
  617. {
  618. code_opborw(PUSHSELFB, string_constant($3, currState), 1);
  619. $$ = 1;
  620. }
  621. ;
  622. funcParams : '(' exprlist ')'
  623. { $$ = adjust_functioncall($2, 1); }
  624. | table { $$ = 1; }
  625. ;
  626. exprlist : /* empty */ { $$ = 0; }
  627. | exprlist1 { $$ = $1; }
  628. ;
  629. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  630. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  631. {
  632. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  633. else
  634. {
  635. currState->f->code[$4] = $<vLong>3; /* store list length */
  636. $$ = $4;
  637. }
  638. }
  639. ;
  640. parlist : /* empty */ { code_args(0); }
  641. | parlist1 { code_args($1); }
  642. ;
  643. parlist1 : par { $$ = $1; }
  644. | parlist1 ',' par
  645. {
  646. if ($1)
  647. luaY_error("invalid parameter list");
  648. $$ = $3;
  649. }
  650. ;
  651. par : NAME { add_localvar($1); $$ = 0; }
  652. | DOTS { $$ = 1; }
  653. ;
  654. fieldlist : lfieldlist
  655. { flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH); }
  656. semicolonpart
  657. { $$ = $1+$3; }
  658. | ffieldlist1 lastcomma
  659. { $$ = $1; flush_record($1%RFIELDS_PER_FLUSH); }
  660. ;
  661. semicolonpart : /* empty */
  662. { $$ = 0; }
  663. | ';' ffieldlist
  664. { $$ = $2; flush_record($2%RFIELDS_PER_FLUSH); }
  665. ;
  666. lastcomma : /* empty */
  667. | ','
  668. ;
  669. ffieldlist : /* empty */ { $$ = 0; }
  670. | ffieldlist1 lastcomma { $$ = $1; }
  671. ;
  672. ffieldlist1 : ffield {$$=1;}
  673. | ffieldlist1 ',' ffield
  674. {
  675. $$=$1+1;
  676. if ($$%RFIELDS_PER_FLUSH == 0)
  677. flush_record(RFIELDS_PER_FLUSH);
  678. }
  679. ;
  680. ffield : ffieldkey '=' expr1
  681. ;
  682. ffieldkey : '[' expr1 ']'
  683. | NAME { code_string($1); }
  684. ;
  685. lfieldlist : /* empty */ { $$ = 0; }
  686. | lfieldlist1 lastcomma { $$ = $1; }
  687. ;
  688. lfieldlist1 : expr1 {$$=1;}
  689. | lfieldlist1 ',' expr1
  690. {
  691. $$=$1+1;
  692. if ($$%LFIELDS_PER_FLUSH == 0)
  693. flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
  694. }
  695. ;
  696. varlist1 : var
  697. {
  698. $$ = 1;
  699. add_varbuffer($1, 0);
  700. }
  701. | varlist1 ',' var
  702. {
  703. add_varbuffer($3, $1);
  704. $$ = $1+1;
  705. }
  706. ;
  707. var : singlevar { $$ = $1; }
  708. | varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */
  709. | varexp '.' NAME { code_string($3); $$ = 0; }/* ind. var. */
  710. ;
  711. singlevar : NAME { $$ = singlevar($1, currState); }
  712. ;
  713. varexp : var { lua_pushvar($1); }
  714. | '%' NAME { pushupvalue($2); }
  715. ;
  716. localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
  717. | localdeclist ',' NAME
  718. {
  719. store_localvar($3, $1);
  720. $$ = $1+1;
  721. }
  722. ;
  723. decinit : /* empty */ { $$ = 0; }
  724. | '=' exprlist1 { $$ = $2; }
  725. ;
  726. %%