lua.stx 22 KB

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