lua.stx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. %{
  2. /*
  3. ** $Id: lua.stx,v 1.32 1998/01/12 13:00:51 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. FuncState *fs = L->currState;
  84. if (fs->pc+n > fs->maxcode)
  85. fs->maxcode = luaM_growvector(&fs->f->code, fs->maxcode,
  86. Byte, codeEM, MAX_INT);
  87. }
  88. static void code_byte (Byte c)
  89. {
  90. check_pc(1);
  91. L->currState->f->code[L->currState->pc++] = c;
  92. }
  93. static void deltastack (int delta)
  94. {
  95. FuncState *fs = L->currState;
  96. fs->stacksize += delta;
  97. if (fs->stacksize > fs->maxstacksize) {
  98. if (fs->stacksize > 255)
  99. luaY_error("function/expression too complex");
  100. fs->maxstacksize = fs->stacksize;
  101. }
  102. }
  103. static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
  104. {
  105. Byte *code = L->currState->f->code;
  106. deltastack(delta);
  107. if (arg < builtin) {
  108. code[pc] = op+1+arg;
  109. return 1;
  110. }
  111. else if (arg <= 255) {
  112. code[pc] = op;
  113. code[pc+1] = arg;
  114. return 2;
  115. }
  116. else if (arg <= MAX_WORD) {
  117. code[pc] = op+1+builtin;
  118. code[pc+1] = arg&0xFF;
  119. code[pc+2] = arg>>8;
  120. return 3;
  121. }
  122. else luaY_error("code too long " MES_LIM("64K"));
  123. return 0; /* to avoid warnings */
  124. }
  125. static int fix_opcode (int pc, OpCode op, int builtin, int arg)
  126. {
  127. FuncState *fs = L->currState;
  128. if (arg < builtin) { /* close space */
  129. luaO_memdown(fs->f->code+pc+1, fs->f->code+pc+2, fs->pc-(pc+2));
  130. fs->pc--;
  131. }
  132. else if (arg > 255) { /* open space */
  133. check_pc(1);
  134. luaO_memup(fs->f->code+pc+1, fs->f->code+pc, fs->pc-pc);
  135. fs->pc++;
  136. }
  137. return code_oparg_at(pc, op, builtin, arg, 0) - 2;
  138. }
  139. static void code_oparg (OpCode op, int builtin, int arg, int delta)
  140. {
  141. check_pc(3); /* maximum code size */
  142. L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
  143. }
  144. static void code_opcode (OpCode op, int delta)
  145. {
  146. deltastack(delta);
  147. code_byte(op);
  148. }
  149. static void code_pop (OpCode op)
  150. {
  151. code_opcode(op, -1);
  152. }
  153. /* binary operations get 2 arguments and leave one, so they pop one */
  154. #define code_binop(op) code_pop(op)
  155. static void code_neutralop (OpCode op)
  156. {
  157. code_opcode(op, 0);
  158. }
  159. /* unary operations get 1 argument and leave one, so they are neutral */
  160. #define code_unop(op) code_neutralop(op)
  161. static void code_constant (int c)
  162. {
  163. code_oparg(PUSHCONSTANT, 8, c, 1);
  164. }
  165. static int next_constant (FuncState *cs)
  166. {
  167. TProtoFunc *f = cs->f;
  168. if (f->nconsts >= cs->maxconsts) {
  169. cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject,
  170. constantEM, MAX_WORD);
  171. }
  172. return f->nconsts++;
  173. }
  174. static int string_constant (TaggedString *s, FuncState *cs)
  175. {
  176. TProtoFunc *f = cs->f;
  177. int c = s->constindex;
  178. if (!(c < f->nconsts &&
  179. ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
  180. c = next_constant(cs);
  181. ttype(&f->consts[c]) = LUA_T_STRING;
  182. tsvalue(&f->consts[c]) = s;
  183. s->constindex = c; /* hint for next time */
  184. }
  185. return c;
  186. }
  187. static void code_string (TaggedString *s)
  188. {
  189. code_constant(string_constant(s, L->currState));
  190. }
  191. #define LIM 20
  192. static int real_constant (real r)
  193. {
  194. /* check whether 'r' has appeared within the last LIM entries */
  195. TObject *cnt = L->currState->f->consts;
  196. int c = L->currState->f->nconsts;
  197. int lim = c < LIM ? 0 : c-LIM;
  198. while (--c >= lim) {
  199. if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
  200. return c;
  201. }
  202. /* not found; create a luaM_new entry */
  203. c = next_constant(L->currState);
  204. cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
  205. ttype(&cnt[c]) = LUA_T_NUMBER;
  206. nvalue(&cnt[c]) = r;
  207. return c;
  208. }
  209. static void code_number (real f)
  210. {
  211. int i;
  212. if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(int)f) == f)
  213. code_oparg(PUSHNUMBER, 3, i, 1); /* f has an (short) integer value */
  214. else
  215. code_constant(real_constant(f));
  216. }
  217. static void flush_record (int n)
  218. {
  219. if (n > 0)
  220. code_oparg(SETMAP, 1, n-1, -2*n);
  221. }
  222. static void flush_list (int m, int n)
  223. {
  224. if (n == 0) return;
  225. code_oparg(SETLIST, 1, m, -n);
  226. code_byte(n);
  227. }
  228. static void luaI_registerlocalvar (TaggedString *varname, int line)
  229. {
  230. FuncState *fs = L->currState;
  231. if (fs->maxvars != -1) { /* debug information? */
  232. if (fs->nvars >= fs->maxvars)
  233. fs->maxvars = luaM_growvector(&fs->f->locvars, fs->maxvars,
  234. LocVar, "", MAX_WORD);
  235. fs->f->locvars[fs->nvars].varname = varname;
  236. fs->f->locvars[fs->nvars].line = line;
  237. fs->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 (L->currState->nlocalvar+n < MAXLOCALS)
  247. L->currState->localvar[L->currState->nlocalvar+n] = name;
  248. else
  249. luaY_error("too many local variables " MES_LIM(SMAXLOCALS));
  250. luaI_registerlocalvar(name, L->lexstate->linenumber);
  251. }
  252. static void add_localvar (TaggedString *name)
  253. {
  254. store_localvar(name, 0);
  255. L->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 " MES_LIM(SMAXVAR));
  272. L->currState->varbuffer[n] = var2store(var);
  273. }
  274. static int aux_localname (TaggedString *n, FuncState *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, FuncState *st)
  282. {
  283. int i = aux_localname(n, st);
  284. if (i == -1) { /* check shadowing */
  285. int l;
  286. for (l=1; l<=(st-L->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, L->currState-1);
  296. int i;
  297. for (i=0; i<L->currState->nupvalues; i++) {
  298. if (L->currState->upvalues[i] == v)
  299. return i;
  300. }
  301. /* new one */
  302. if (++(L->currState->nupvalues) > MAXUPVALUES)
  303. luaY_error("too many upvalues in a single function " MES_LIM(SMAXUPVALUES));
  304. L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
  305. return i;
  306. }
  307. static void pushupvalue (TaggedString *n)
  308. {
  309. int i;
  310. if (L->currState == L->mainState)
  311. luaY_error("cannot access upvalue in main");
  312. if (aux_localname(n, L->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. if (lua_debug && line != L->lexstate->lastline) {
  320. code_oparg(SETLINE, 0, line, 0);
  321. L->lexstate->lastline = line;
  322. }
  323. }
  324. static void adjuststack (int n)
  325. {
  326. if (n > 0)
  327. code_oparg(POP, 2, n-1, -n);
  328. else if (n < 0)
  329. code_oparg(PUSHNIL, 1, (-n)-1, -n);
  330. }
  331. static long adjust_functioncall (long exp, int nresults)
  332. {
  333. if (exp <= 0)
  334. return -exp; /* exp is -list length */
  335. else {
  336. int temp = L->currState->f->code[exp];
  337. int nparams = L->currState->f->code[exp-1];
  338. exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
  339. L->currState->f->code[exp] = nparams;
  340. if (nresults != MULT_RET)
  341. deltastack(nresults);
  342. deltastack(-(nparams+1));
  343. return temp+nresults;
  344. }
  345. }
  346. static void adjust_mult_assign (int vars, long exps)
  347. {
  348. if (exps > 0) { /* must correct function call */
  349. int diff = L->currState->f->code[exps] - vars;
  350. if (diff < 0)
  351. adjust_functioncall(exps, -diff);
  352. else {
  353. adjust_functioncall(exps, 0);
  354. adjuststack(diff);
  355. }
  356. }
  357. else adjuststack((-exps)-vars);
  358. }
  359. static void code_args (int nparams, int dots)
  360. {
  361. L->currState->nlocalvar += nparams; /* "self" may already be there */
  362. nparams = L->currState->nlocalvar;
  363. if (!dots) {
  364. L->currState->f->code[1] = nparams; /* fill-in arg information */
  365. deltastack(nparams);
  366. }
  367. else {
  368. L->currState->f->code[1] = nparams+ZEROVARARG;
  369. deltastack(nparams+1);
  370. add_localvar(luaS_new("arg"));
  371. }
  372. }
  373. static void lua_pushvar (vardesc var)
  374. {
  375. if (isglobal(var))
  376. code_oparg(GETGLOBAL, 8, globalindex(var), 1);
  377. else if (islocal(var))
  378. code_oparg(PUSHLOCAL, 8, localindex(var), 1);
  379. else if (isdot(var))
  380. code_oparg(GETDOTTED, 8, dotindex(var), 0);
  381. else
  382. code_pop(GETTABLE);
  383. }
  384. static void storevar (vardesc var)
  385. {
  386. if (var == 0) /* indexed var */
  387. code_opcode(SETTABLE0, -3);
  388. else if (isglobal(var))
  389. code_oparg(SETGLOBAL, 8, globalindex(var), -1);
  390. else /* local var */
  391. code_oparg(SETLOCAL, 8, localindex(var), -1);
  392. }
  393. /* returns how many elements are left as 'garbage' on the stack */
  394. static int lua_codestore (int i, int left)
  395. {
  396. if (L->currState->varbuffer[i] != 0 || /* global or local var or */
  397. left+i == 0) { /* indexed var without values in between */
  398. storevar(L->currState->varbuffer[i]);
  399. return left;
  400. }
  401. else { /* indexed var with values in between*/
  402. code_oparg(SETTABLE, 0, left+i, -1);
  403. return left+2; /* table/index are not popped, since they are not on top */
  404. }
  405. }
  406. static int fix_jump (int pc, OpCode op, int n)
  407. {
  408. /* jump is relative to position following jump instruction */
  409. return fix_opcode(pc, op, 0, n-(pc+JMPSIZE));
  410. }
  411. static void fix_upjmp (OpCode op, int pos)
  412. {
  413. int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
  414. if (delta > 255) delta++;
  415. code_oparg(op, 0, delta, 0);
  416. }
  417. static void codeIf (int thenAdd, int elseAdd)
  418. {
  419. int elseinit = elseAdd+JMPSIZE;
  420. if (L->currState->pc == elseinit) { /* no else part */
  421. L->currState->pc -= JMPSIZE;
  422. elseinit = L->currState->pc;
  423. }
  424. else
  425. elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
  426. fix_jump(thenAdd, IFFJMP, elseinit);
  427. }
  428. static void code_shortcircuit (int pc, OpCode op)
  429. {
  430. fix_jump(pc, op, L->currState->pc);
  431. }
  432. static void codereturn (void)
  433. {
  434. code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
  435. L->currState->stacksize = L->currState->nlocalvar;
  436. }
  437. static void func_onstack (TProtoFunc *f)
  438. {
  439. int i;
  440. int nupvalues = (L->currState+1)->nupvalues;
  441. int c = next_constant(L->currState);
  442. ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
  443. L->currState->f->consts[c].value.tf = (L->currState+1)->f;
  444. if (nupvalues == 0)
  445. code_constant(c);
  446. else {
  447. for (i=0; i<nupvalues; i++)
  448. lua_pushvar((L->currState+1)->upvalues[i]);
  449. code_constant(c);
  450. code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
  451. }
  452. }
  453. static void init_state (TaggedString *filename)
  454. {
  455. TProtoFunc *f = luaF_newproto();
  456. FuncState *fs = L->currState;
  457. fs->stacksize = 0;
  458. fs->maxstacksize = 0;
  459. fs->nlocalvar = 0;
  460. fs->nupvalues = 0;
  461. fs->f = f;
  462. f->fileName = filename;
  463. fs->pc = 0;
  464. fs->maxcode = 0;
  465. f->code = NULL;
  466. fs->maxconsts = 0;
  467. if (lua_debug) {
  468. fs->nvars = 0;
  469. fs->maxvars = 0;
  470. }
  471. else
  472. fs->maxvars = -1; /* flag no debug information */
  473. code_byte(0); /* to be filled with stacksize */
  474. code_byte(0); /* to be filled with arg information */
  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)
  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(zname(z)));
  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, sexp /* if != 0, points to function return
  538. counter */
  539. %type <vInt> varlist1, funcParams, funcvalue
  540. %type <vInt> fieldlist, localnamelist, decinit
  541. %type <vInt> ffieldlist1, lfieldlist1, ffieldlist, lfieldlist, part
  542. %type <vLong> var, varname, funcname /* vardesc */
  543. %left AND OR
  544. %left EQ NE '>' '<' LE GE
  545. %left CONC
  546. %left '+' '-'
  547. %left '*' '/'
  548. %left UNARY NOT
  549. %right '^'
  550. %% /* beginning of rules section */
  551. chunk : statlist ret ;
  552. statlist : /* empty */
  553. | statlist stat sc
  554. ;
  555. sc : /* empty */ | ';' ;
  556. stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
  557. | DO block END
  558. | WHILE GetPC cond DO block END
  559. {{
  560. FuncState *fs = L->currState;
  561. int expsize = $3-$2;
  562. int newpos = $2+JMPSIZE;
  563. check_pc(expsize);
  564. memcpy(fs->f->code+fs->pc, fs->f->code+$2, expsize);
  565. luaO_memdown(fs->f->code+$2, fs->f->code+$3, fs->pc-$2);
  566. newpos += fix_jump($2, JMP, fs->pc-expsize);
  567. fix_upjmp(IFTUPJMP, newpos);
  568. }}
  569. | REPEAT GetPC block UNTIL expr1
  570. {
  571. fix_upjmp(IFFUPJMP, $2);
  572. deltastack(-1); /* pops condition */
  573. }
  574. | varlist1 '=' exprlist1
  575. {{
  576. int i;
  577. int left = 0;
  578. adjust_mult_assign($1, $3);
  579. for (i=$1-1; i>=0; i--)
  580. left = lua_codestore(i, left);
  581. adjuststack(left); /* remove eventual 'garbage' left on stack */
  582. }}
  583. | functioncall { adjust_functioncall($1, 0); }
  584. | LOCAL localnamelist decinit
  585. {
  586. L->currState->nlocalvar += $2;
  587. adjust_mult_assign($2, $3);
  588. }
  589. | FUNCTION funcname body { storevar($2); }
  590. ;
  591. block : {$<vInt>$ = L->currState->nlocalvar;} chunk
  592. {
  593. adjuststack(L->currState->nlocalvar - $<vInt>1);
  594. for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
  595. luaI_unregisterlocalvar(L->lexstate->linenumber);
  596. }
  597. ;
  598. funcname : varname { $$ = $1; init_func(); }
  599. | fvarname '.' fname
  600. {
  601. $$ = 0; /* flag indexed variable */
  602. init_func();
  603. }
  604. | fvarname ':' fname
  605. {
  606. $$ = 0; /* flag indexed variable */
  607. init_func();
  608. add_localvar(luaS_new("self"));
  609. }
  610. ;
  611. fvarname : varname { lua_pushvar($1); } ;
  612. fname : NAME { code_string($1); } ;
  613. body : '(' parlist ')' chunk END { func_onstack(close_func()); } ;
  614. elsepart : /* empty */
  615. | ELSE block
  616. | ELSEIF cond THEN block SaveWord elsepart { codeIf($2, $5); }
  617. ;
  618. ret : /* empty */
  619. | RETURN exprlist sc
  620. {
  621. adjust_functioncall($2, MULT_RET);
  622. codereturn();
  623. }
  624. ;
  625. GetPC : /* empty */ { $$ = L->currState->pc; } ;
  626. SaveWord : /* empty */
  627. { $$ = L->currState->pc;
  628. check_pc(JMPSIZE);
  629. L->currState->pc += JMPSIZE; /* open space */
  630. }
  631. ;
  632. SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ } ;
  633. SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ } ;
  634. cond : expr1 SaveWordPop { $$ = $2; } ;
  635. expr1 : expr { adjust_functioncall($1, 1); } ;
  636. expr : '(' expr ')' { $$ = $2; }
  637. | expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
  638. | expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
  639. | expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
  640. | expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
  641. | expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
  642. | expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
  643. | expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
  644. | expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
  645. | expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
  646. | expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
  647. | expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
  648. | expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
  649. | '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
  650. | NOT expr1 { code_unop(NOTOP); $$ = 0;}
  651. | sexp { $$ = $1; /* simple expressions */ }
  652. | table { $$ = 0; }
  653. | NUMBER { code_number($1); $$ = 0; }
  654. | STRING { code_string($1); $$ = 0; }
  655. | NIL { adjuststack(-1); $$ = 0; }
  656. | FUNCTION { init_func(); } body { $$ = 0; }
  657. | expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
  658. | expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
  659. ;
  660. sexp1 : sexp { adjust_functioncall($1, 1); } ;
  661. sexp : var { lua_pushvar($1); $$ = 0; }
  662. | '%' NAME { pushupvalue($2); $$ = 0; }
  663. | functioncall { $$ = $1; }
  664. ;
  665. var : varname { $$ = $1; }
  666. | sexp1 '[' expr1 ']' { $$ = 0; } /* indexed variable */
  667. | sexp1 '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
  668. ;
  669. varname : NAME { $$ = singlevar($1, L->currState); } ;
  670. table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); } ;
  671. functioncall : funcvalue funcParams
  672. {
  673. code_byte(0); /* save space for opcode */
  674. code_byte($1+$2); /* number of parameters */
  675. $$ = L->currState->pc;
  676. code_byte(0); /* must be adjusted by other rules */
  677. }
  678. ;
  679. funcvalue : sexp1 { $$ = 0; }
  680. | sexp1 ':' NAME
  681. {
  682. code_oparg(PUSHSELF, 8, string_constant($3, L->currState), 1);
  683. $$ = 1;
  684. }
  685. ;
  686. funcParams : '(' exprlist ')' { $$ = adjust_functioncall($2, 1); }
  687. | table { $$ = 1; }
  688. | STRING { code_string($1); $$ = 1; }
  689. ;
  690. exprlist : /* empty */ { $$ = 0; }
  691. | exprlist1 { $$ = $1; }
  692. ;
  693. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  694. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  695. {
  696. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  697. else {
  698. L->currState->f->code[$4] = $<vLong>3; /* store list length */
  699. $$ = $4;
  700. }
  701. }
  702. ;
  703. parlist : /* empty */ { code_args(0, 0); }
  704. | DOTS { code_args(0, 1); }
  705. | localnamelist { code_args($1, 0); }
  706. | localnamelist ',' DOTS { code_args($1, 1); }
  707. ;
  708. fieldlist : part { $$ = abs($1); }
  709. | part ';' part
  710. {
  711. if ($1*$3 > 0) /* repeated parts? */
  712. luaY_error("invalid constructor syntax");
  713. $$ = abs($1)+abs($3);
  714. }
  715. ;
  716. part : /* empty */ { $$ = 0; }
  717. | ffieldlist { $$ = $1; }
  718. | lfieldlist { $$ = $1; }
  719. ;
  720. lastcomma : /* empty */ | ',' ;
  721. ffieldlist : ffieldlist1 lastcomma
  722. {
  723. flush_record($1%RFIELDS_PER_FLUSH);
  724. $$ = -$1; /* negative signals a "record" part */
  725. }
  726. ;
  727. lfieldlist : lfieldlist1 lastcomma
  728. {
  729. flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH);
  730. $$ = $1;
  731. }
  732. ;
  733. ffieldlist1 : ffield {$$=1;}
  734. | ffieldlist1 ',' ffield
  735. {
  736. $$=$1+1;
  737. if ($$%RFIELDS_PER_FLUSH == 0)
  738. flush_record(RFIELDS_PER_FLUSH);
  739. }
  740. ;
  741. ffield : ffieldkey '=' expr1 ;
  742. ffieldkey : '[' expr1 ']'
  743. | fname
  744. ;
  745. lfieldlist1 : expr1 {$$=1;}
  746. | lfieldlist1 ',' expr1
  747. {
  748. $$=$1+1;
  749. if ($$%LFIELDS_PER_FLUSH == 0)
  750. flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
  751. }
  752. ;
  753. varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
  754. | varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
  755. ;
  756. localnamelist : NAME {store_localvar($1, 0); $$ = 1;}
  757. | localnamelist ',' NAME { store_localvar($3, $1); $$ = $1+1; }
  758. ;
  759. decinit : /* empty */ { $$ = 0; }
  760. | '=' exprlist1 { $$ = $2; }
  761. ;
  762. %%