lua.stx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. %{
  2. /*
  3. ** $Id: lua.stx,v 1.18 1997/11/19 17:29:23 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 "lstate.h"
  17. #include "lstring.h"
  18. #include "lua.h"
  19. #include "luadebug.h"
  20. #include "lzio.h"
  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 FuncState {
  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. } FuncState;
  64. #define YYPURE 1
  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, L->lexstate->linenumber, L->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 (L->currState->pc+n > L->currState->maxcode)
  79. L->currState->maxcode = luaM_growvector(&L->currState->f->code,
  80. L->currState->maxcode, Byte, codeEM, MAX_INT);
  81. }
  82. static void movecode_up (int d, int s, int n)
  83. {
  84. while (n--)
  85. L->currState->f->code[d+n] = L->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. L->currState->f->code[d+i] = L->currState->f->code[s+i];
  92. }
  93. static void code_byte (Byte c)
  94. {
  95. check_pc(1);
  96. L->currState->f->code[L->currState->pc++] = c;
  97. }
  98. static void deltastack (int delta)
  99. {
  100. L->currState->stacksize += delta;
  101. if (L->currState->stacksize > L->currState->maxstacksize) {
  102. if (L->currState->stacksize > 255)
  103. luaY_error("function/expression too complex (limit 256)");
  104. L->currState->maxstacksize = L->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. L->currState->f->code[pc] = op+1+arg;
  112. return 1;
  113. }
  114. else if (arg <= 255) {
  115. L->currState->f->code[pc] = op;
  116. L->currState->f->code[pc+1] = arg;
  117. return 2;
  118. }
  119. else if (arg <= MAX_WORD) {
  120. L->currState->f->code[pc] = op+1+builtin;
  121. L->currState->f->code[pc+1] = arg&0xFF;
  122. L->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, L->currState->pc-(pc+2));
  132. L->currState->pc--;
  133. }
  134. else if (arg > 255) { /* open space */
  135. check_pc(1);
  136. movecode_up(pc+1, pc, L->currState->pc-pc);
  137. L->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. L->currState->pc += code_oparg_at(L->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 (FuncState *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, FuncState *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, L->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 = L->currState->f->consts;
  198. int c = L->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(L->currState);
  206. cnt = L->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 (L->currState->maxvars != -1) { /* debug information? */
  233. if (L->currState->nvars >= L->currState->maxvars)
  234. L->currState->maxvars = luaM_growvector(&L->currState->f->locvars,
  235. L->currState->maxvars, LocVar, "", MAX_WORD);
  236. L->currState->f->locvars[L->currState->nvars].varname = varname;
  237. L->currState->f->locvars[L->currState->nvars].line = line;
  238. L->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 (L->currState->nlocalvar+n < MAXLOCALS)
  248. L->currState->localvar[L->currState->nlocalvar+n] = name;
  249. else
  250. luaY_error("too many local variables (limit 32)");
  251. luaI_registerlocalvar(name, L->lexstate->linenumber);
  252. }
  253. static void add_localvar (TaggedString *name)
  254. {
  255. store_localvar(name, 0);
  256. L->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. L->currState->varbuffer[n] = var2store(var);
  274. }
  275. static int aux_localname (TaggedString *n, FuncState *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, FuncState *st)
  283. {
  284. int i = aux_localname(n, st);
  285. if (i == -1) { /* check shadowing */
  286. int l;
  287. for (l=1; l<=(st-L->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, L->currState-1);
  297. int i;
  298. for (i=0; i<L->currState->nupvalues; i++) {
  299. if (L->currState->upvalues[i] == v)
  300. return i;
  301. }
  302. /* new one */
  303. if (++(L->currState->nupvalues) > MAXUPVALUES)
  304. luaY_error("too many upvalues in a single function (limit 16)");
  305. L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
  306. return i;
  307. }
  308. static void pushupvalue (TaggedString *n)
  309. {
  310. int i;
  311. if (L->currState == L->mainState)
  312. luaY_error("cannot access upvalue in main");
  313. if (aux_localname(n, L->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. if (lua_debug && line != L->lexstate->lastline) {
  321. code_oparg(SETLINE, 0, line, 0);
  322. L->lexstate->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 = L->currState->f->code[exp];
  338. int nparams = L->currState->f->code[exp-1];
  339. exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
  340. L->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 = L->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. L->currState->nlocalvar += nparams;
  363. if (!dots)
  364. code_oparg(ARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar);
  365. else {
  366. code_oparg(VARARGS, 0, L->currState->nlocalvar, L->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 (L->currState->varbuffer[i] != 0 || /* global or local var or */
  394. left+i == 0) { /* indexed var without values in between */
  395. storevar(L->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 = L->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 (L->currState->pc == elseinit) { /* no else part */
  418. L->currState->pc -= JMPSIZE;
  419. elseinit = L->currState->pc;
  420. }
  421. else
  422. elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
  423. fix_jump(thenAdd, IFFJMP, elseinit);
  424. }
  425. static void code_shortcircuit (int pc, OpCode op)
  426. {
  427. fix_jump(pc, op, L->currState->pc);
  428. }
  429. static void codereturn (void)
  430. {
  431. code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
  432. L->currState->stacksize = L->currState->nlocalvar;
  433. }
  434. static void func_onstack (TProtoFunc *f)
  435. {
  436. int i;
  437. int nupvalues = (L->currState+1)->nupvalues;
  438. int c = next_constant(L->currState);
  439. ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
  440. L->currState->f->consts[c].value.tf = (L->currState+1)->f;
  441. for (i=0; i<nupvalues; i++)
  442. lua_pushvar((L->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. L->currState->stacksize = 0;
  450. L->currState->maxstacksize = 0;
  451. L->currState->nlocalvar = 0;
  452. L->currState->nupvalues = 0;
  453. L->currState->f = f;
  454. f->fileName = filename;
  455. L->currState->pc = 0;
  456. L->currState->maxcode = 0;
  457. f->code = NULL;
  458. L->currState->maxconsts = 0;
  459. if (lua_debug) {
  460. L->currState->nvars = 0;
  461. L->currState->maxvars = 0;
  462. }
  463. else
  464. L->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 (L->currState-L->mainState >= MAXSTATES-1)
  470. luaY_error("too many nested functions (limit 6)");
  471. L->currState++;
  472. init_state(L->mainState->f->fileName);
  473. luaY_codedebugline(L->lexstate->linenumber);
  474. L->currState->f->lineDefined = L->lexstate->linenumber;
  475. }
  476. static TProtoFunc *close_func (void)
  477. {
  478. TProtoFunc *f = L->currState->f;
  479. code_neutralop(ENDCODE);
  480. f->code[0] = L->currState->maxstacksize;
  481. f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
  482. f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
  483. if (L->currState->maxvars != -1) { /* debug information? */
  484. luaI_registerlocalvar(NULL, -1); /* flag end of vector */
  485. f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
  486. }
  487. L->currState--;
  488. return f;
  489. }
  490. /*
  491. ** Parse LUA code.
  492. */
  493. TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
  494. {
  495. struct LexState lexstate;
  496. FuncState state[MAXSTATES];
  497. L->currState = L->mainState = &state[0];
  498. L->lexstate = &lexstate;
  499. luaX_setinput(z);
  500. init_state(luaS_new(chunkname));
  501. if (luaY_parse()) lua_error("parse error");
  502. return close_func();
  503. }
  504. %}
  505. %union
  506. {
  507. int vInt;
  508. real vReal;
  509. char *pChar;
  510. long vLong;
  511. TaggedString *pTStr;
  512. TProtoFunc *pFunc;
  513. }
  514. %start chunk
  515. %token WRONGTOKEN
  516. %token NIL
  517. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  518. %token RETURN
  519. %token LOCAL
  520. %token FUNCTION
  521. %token DOTS
  522. %token <vReal> NUMBER
  523. %token <pTStr> NAME STRING
  524. %type <vInt> SaveWord, cond, GetPC, SaveWordPop, SaveWordPush
  525. %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
  526. counter (which has list length); if <= 0, -list lenght */
  527. %type <vLong> functioncall, expr /* if != 0, points to function return
  528. counter */
  529. %type <vInt> varlist1, funcParams, funcvalue
  530. %type <vInt> fieldlist, localnamelist, decinit
  531. %type <vInt> ffieldlist, ffieldlist1, semicolonpart
  532. %type <vInt> lfieldlist, lfieldlist1
  533. %type <vLong> var, funcname /* vardesc */
  534. %type <pFunc> body
  535. %left AND OR
  536. %left EQ NE '>' '<' LE GE
  537. %left CONC
  538. %left '+' '-'
  539. %left '*' '/'
  540. %left UNARY NOT
  541. %right '^'
  542. %% /* beginning of rules section */
  543. chunk : statlist ret
  544. ;
  545. statlist : /* empty */
  546. | statlist stat sc
  547. ;
  548. sc : /* empty */ | ';' ;
  549. stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
  550. | DO block END
  551. | WHILE GetPC cond DO block END
  552. {{
  553. int expsize = $3-$2;
  554. int newpos = $2+JMPSIZE;
  555. check_pc(expsize);
  556. memcpy(&L->currState->f->code[L->currState->pc],
  557. &L->currState->f->code[$2], expsize);
  558. movecode_down($2, $3, L->currState->pc-$2);
  559. newpos += fix_jump($2, JMP, L->currState->pc-expsize);
  560. fix_upjmp(IFTUPJMP, newpos);
  561. }}
  562. | REPEAT GetPC block UNTIL expr1
  563. {
  564. fix_upjmp(IFFUPJMP, $2);
  565. deltastack(-1); /* pops condition */
  566. }
  567. | varlist1 '=' exprlist1
  568. {{
  569. int i;
  570. int left = 0;
  571. adjust_mult_assign($1, $3);
  572. for (i=$1-1; i>=0; i--)
  573. left = lua_codestore(i, left);
  574. adjuststack(left); /* remove eventual 'garbage' left on stack */
  575. }}
  576. | functioncall { adjust_functioncall($1, 0); }
  577. | LOCAL localnamelist decinit
  578. {
  579. L->currState->nlocalvar += $2;
  580. adjust_mult_assign($2, $3);
  581. }
  582. | FUNCTION funcname body { func_onstack($3); storevar($2); }
  583. ;
  584. block : {$<vInt>$ = L->currState->nlocalvar;} chunk
  585. {
  586. adjuststack(L->currState->nlocalvar - $<vInt>1);
  587. for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
  588. luaI_unregisterlocalvar(L->lexstate->linenumber);
  589. }
  590. ;
  591. funcname : var { $$ = var2store($1); init_func(); }
  592. | varexp ':' NAME
  593. {
  594. code_string($3);
  595. $$ = 0; /* flag indexed variable */
  596. init_func();
  597. add_localvar(luaS_new("self"));
  598. }
  599. ;
  600. body : '(' parlist ')' chunk END { $$ = close_func(); }
  601. ;
  602. elsepart : /* empty */
  603. | ELSE block
  604. | ELSEIF cond THEN block SaveWord elsepart { codeIf($2, $5); }
  605. ;
  606. ret : /* empty */
  607. | RETURN exprlist sc
  608. {
  609. adjust_functioncall($2, MULT_RET);
  610. codereturn();
  611. }
  612. ;
  613. GetPC : /* empty */ { $$ = L->currState->pc; }
  614. ;
  615. SaveWord : /* empty */
  616. { $$ = L->currState->pc;
  617. check_pc(JMPSIZE);
  618. L->currState->pc += JMPSIZE; /* open space */
  619. }
  620. ;
  621. SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ }
  622. ;
  623. SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ }
  624. ;
  625. cond : expr1 SaveWordPop { $$ = $2; }
  626. ;
  627. expr1 : expr { adjust_functioncall($1, 1); }
  628. ;
  629. expr : '(' expr ')' { $$ = $2; }
  630. | expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
  631. | expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
  632. | expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
  633. | expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
  634. | expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
  635. | expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
  636. | expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
  637. | expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
  638. | expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
  639. | expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
  640. | expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
  641. | expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
  642. | '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
  643. | NOT expr1 { code_unop(NOTOP); $$ = 0;}
  644. | table { $$ = 0; }
  645. | varexp { $$ = 0;}
  646. | NUMBER { code_number($1); $$ = 0; }
  647. | STRING { code_string($1); $$ = 0; }
  648. | NIL { adjuststack(-1); $$ = 0; }
  649. | functioncall { $$ = $1; }
  650. | FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
  651. | expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
  652. | expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
  653. ;
  654. table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); }
  655. ;
  656. functioncall : funcvalue funcParams
  657. {
  658. code_byte(0); /* save space for opcode */
  659. code_byte($1+$2); /* number of parameters */
  660. $$ = L->currState->pc;
  661. code_byte(0); /* must be adjusted by other rules */
  662. }
  663. ;
  664. funcvalue : varexp { $$ = 0; }
  665. | varexp ':' NAME
  666. {
  667. code_oparg(PUSHSELF, 0, string_constant($3, L->currState), 1);
  668. $$ = 1;
  669. }
  670. ;
  671. funcParams : '(' exprlist ')' { $$ = adjust_functioncall($2, 1); }
  672. | table { $$ = 1; }
  673. ;
  674. exprlist : /* empty */ { $$ = 0; }
  675. | exprlist1 { $$ = $1; }
  676. ;
  677. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  678. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  679. {
  680. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  681. else {
  682. L->currState->f->code[$4] = $<vLong>3; /* store list length */
  683. $$ = $4;
  684. }
  685. }
  686. ;
  687. parlist : /* empty */ { code_args(0, 0); }
  688. | DOTS { code_args(0, 1); }
  689. | localnamelist { code_args($1, 0); }
  690. | localnamelist ',' DOTS { code_args($1, 1); }
  691. ;
  692. fieldlist : lfieldlist
  693. { flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH); }
  694. semicolonpart
  695. { $$ = $1+$3; }
  696. | ffieldlist1 lastcomma
  697. { $$ = $1; flush_record($1%RFIELDS_PER_FLUSH); }
  698. ;
  699. semicolonpart : /* empty */ { $$ = 0; }
  700. | ';' ffieldlist { $$ = $2; flush_record($2%RFIELDS_PER_FLUSH); }
  701. ;
  702. lastcomma : /* empty */
  703. | ','
  704. ;
  705. ffieldlist : /* empty */ { $$ = 0; }
  706. | ffieldlist1 lastcomma { $$ = $1; }
  707. ;
  708. ffieldlist1 : ffield {$$=1;}
  709. | ffieldlist1 ',' ffield
  710. {
  711. $$=$1+1;
  712. if ($$%RFIELDS_PER_FLUSH == 0)
  713. flush_record(RFIELDS_PER_FLUSH);
  714. }
  715. ;
  716. ffield : ffieldkey '=' expr1
  717. ;
  718. ffieldkey : '[' expr1 ']'
  719. | NAME { code_string($1); }
  720. ;
  721. lfieldlist : /* empty */ { $$ = 0; }
  722. | lfieldlist1 lastcomma { $$ = $1; }
  723. ;
  724. lfieldlist1 : expr1 {$$=1;}
  725. | lfieldlist1 ',' expr1
  726. {
  727. $$=$1+1;
  728. if ($$%LFIELDS_PER_FLUSH == 0)
  729. flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
  730. }
  731. ;
  732. varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
  733. | varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
  734. ;
  735. var : NAME { $$ = singlevar($1, L->currState); }
  736. | varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */
  737. | varexp '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
  738. ;
  739. varexp : var { lua_pushvar($1); }
  740. | '%' NAME { pushupvalue($2); }
  741. ;
  742. localnamelist : NAME {store_localvar($1, 0); $$ = 1;}
  743. | localnamelist ',' NAME { store_localvar($3, $1); $$ = $1+1; }
  744. ;
  745. decinit : /* empty */ { $$ = 0; }
  746. | '=' exprlist1 { $$ = $2; }
  747. ;
  748. %%