2
0

lua.stx 22 KB

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