lua.stx 22 KB

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