lua.stx 21 KB

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