lua.stx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. %{
  2. /*
  3. ** $Id: lua.stx,v 1.30 1997/12/29 17:35:46 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. Word i;
  212. if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)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;
  362. if (!dots)
  363. code_oparg(ARGS, 4, L->currState->nlocalvar, L->currState->nlocalvar);
  364. else {
  365. code_oparg(VARARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar+1);
  366. add_localvar(luaS_new("arg"));
  367. }
  368. }
  369. static void lua_pushvar (vardesc var)
  370. {
  371. if (isglobal(var))
  372. code_oparg(GETGLOBAL, 8, globalindex(var), 1);
  373. else if (islocal(var))
  374. code_oparg(PUSHLOCAL, 8, localindex(var), 1);
  375. else if (isdot(var))
  376. code_oparg(GETDOTTED, 8, dotindex(var), 0);
  377. else
  378. code_pop(GETTABLE);
  379. }
  380. static void storevar (vardesc var)
  381. {
  382. if (var == 0) /* indexed var */
  383. code_opcode(SETTABLE0, -3);
  384. else if (isglobal(var))
  385. code_oparg(SETGLOBAL, 8, globalindex(var), -1);
  386. else /* local var */
  387. code_oparg(SETLOCAL, 8, localindex(var), -1);
  388. }
  389. /* returns how many elements are left as 'garbage' on the stack */
  390. static int lua_codestore (int i, int left)
  391. {
  392. if (L->currState->varbuffer[i] != 0 || /* global or local var or */
  393. left+i == 0) { /* indexed var without values in between */
  394. storevar(L->currState->varbuffer[i]);
  395. return left;
  396. }
  397. else { /* indexed var with values in between*/
  398. code_oparg(SETTABLE, 0, left+i, -1);
  399. return left+2; /* table/index are not popped, since they are not on top */
  400. }
  401. }
  402. static int fix_jump (int pc, OpCode op, int n)
  403. {
  404. /* jump is relative to position following jump instruction */
  405. return fix_opcode(pc, op, 0, n-(pc+JMPSIZE));
  406. }
  407. static void fix_upjmp (OpCode op, int pos)
  408. {
  409. int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
  410. if (delta > 255) delta++;
  411. code_oparg(op, 0, delta, 0);
  412. }
  413. static void codeIf (int thenAdd, int elseAdd)
  414. {
  415. int elseinit = elseAdd+JMPSIZE;
  416. if (L->currState->pc == elseinit) { /* no else part */
  417. L->currState->pc -= JMPSIZE;
  418. elseinit = L->currState->pc;
  419. }
  420. else
  421. elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
  422. fix_jump(thenAdd, IFFJMP, elseinit);
  423. }
  424. static void code_shortcircuit (int pc, OpCode op)
  425. {
  426. fix_jump(pc, op, L->currState->pc);
  427. }
  428. static void codereturn (void)
  429. {
  430. code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
  431. L->currState->stacksize = L->currState->nlocalvar;
  432. }
  433. static void func_onstack (TProtoFunc *f)
  434. {
  435. int i;
  436. int nupvalues = (L->currState+1)->nupvalues;
  437. int c = next_constant(L->currState);
  438. ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
  439. L->currState->f->consts[c].value.tf = (L->currState+1)->f;
  440. if (nupvalues == 0)
  441. code_constant(c);
  442. else {
  443. for (i=0; i<nupvalues; i++)
  444. lua_pushvar((L->currState+1)->upvalues[i]);
  445. code_constant(c);
  446. code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
  447. }
  448. }
  449. static void init_state (TaggedString *filename)
  450. {
  451. TProtoFunc *f = luaF_newproto();
  452. FuncState *fs = L->currState;
  453. fs->stacksize = 0;
  454. fs->maxstacksize = 0;
  455. fs->nlocalvar = 0;
  456. fs->nupvalues = 0;
  457. fs->f = f;
  458. f->fileName = filename;
  459. fs->pc = 0;
  460. fs->maxcode = 0;
  461. f->code = NULL;
  462. fs->maxconsts = 0;
  463. if (lua_debug) {
  464. fs->nvars = 0;
  465. fs->maxvars = 0;
  466. }
  467. else
  468. fs->maxvars = -1; /* flag no debug information */
  469. code_byte(0); /* to be filled with stacksize */
  470. L->lexstate->lastline = 0; /* invalidate it */
  471. }
  472. static void init_func (void)
  473. {
  474. if (L->currState-L->mainState >= MAXSTATES-1)
  475. luaY_error("too many nested functions " MES_LIM(SMAXSTATES));
  476. L->currState++;
  477. init_state(L->mainState->f->fileName);
  478. luaY_codedebugline(L->lexstate->linenumber);
  479. L->currState->f->lineDefined = L->lexstate->linenumber;
  480. }
  481. static TProtoFunc *close_func (void)
  482. {
  483. TProtoFunc *f = L->currState->f;
  484. code_neutralop(ENDCODE);
  485. f->code[0] = L->currState->maxstacksize;
  486. f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
  487. f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
  488. if (L->currState->maxvars != -1) { /* debug information? */
  489. luaI_registerlocalvar(NULL, -1); /* flag end of vector */
  490. f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
  491. }
  492. L->currState--;
  493. return f;
  494. }
  495. /*
  496. ** Parse Lua code.
  497. */
  498. TProtoFunc *luaY_parser (ZIO *z)
  499. {
  500. struct LexState lexstate;
  501. FuncState state[MAXSTATES];
  502. L->currState = L->mainState = &state[0];
  503. L->lexstate = &lexstate;
  504. luaX_setinput(z);
  505. init_state(luaS_new(zname(z)));
  506. if (luaY_parse()) lua_error("parse error");
  507. return close_func();
  508. }
  509. %}
  510. %union
  511. {
  512. int vInt;
  513. real vReal;
  514. char *pChar;
  515. long vLong;
  516. TaggedString *pTStr;
  517. TProtoFunc *pFunc;
  518. }
  519. %start chunk
  520. %token WRONGTOKEN
  521. %token NIL
  522. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  523. %token RETURN
  524. %token LOCAL
  525. %token FUNCTION
  526. %token DOTS
  527. %token <vReal> NUMBER
  528. %token <pTStr> NAME STRING
  529. %type <vInt> SaveWord, cond, GetPC, SaveWordPop, SaveWordPush
  530. %type <vLong> exprlist, exprlist1 /* if > 0, points to function return
  531. counter (which has list length); if <= 0, -list lenght */
  532. %type <vLong> functioncall, expr, sexp /* if != 0, points to function return
  533. counter */
  534. %type <vInt> varlist1, funcParams, funcvalue
  535. %type <vInt> fieldlist, localnamelist, decinit
  536. %type <vInt> ffieldlist1, lfieldlist1, ffieldlist, lfieldlist, part
  537. %type <vLong> var, varname, funcname /* vardesc */
  538. %left AND OR
  539. %left EQ NE '>' '<' LE GE
  540. %left CONC
  541. %left '+' '-'
  542. %left '*' '/'
  543. %left UNARY NOT
  544. %right '^'
  545. %% /* beginning of rules section */
  546. chunk : statlist ret ;
  547. statlist : /* empty */
  548. | statlist stat sc
  549. ;
  550. sc : /* empty */ | ';' ;
  551. stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
  552. | DO block END
  553. | WHILE GetPC cond DO block END
  554. {{
  555. FuncState *fs = L->currState;
  556. int expsize = $3-$2;
  557. int newpos = $2+JMPSIZE;
  558. check_pc(expsize);
  559. memcpy(fs->f->code+fs->pc, fs->f->code+$2, expsize);
  560. luaO_memdown(fs->f->code+$2, fs->f->code+$3, fs->pc-$2);
  561. newpos += fix_jump($2, JMP, fs->pc-expsize);
  562. fix_upjmp(IFTUPJMP, newpos);
  563. }}
  564. | REPEAT GetPC block UNTIL expr1
  565. {
  566. fix_upjmp(IFFUPJMP, $2);
  567. deltastack(-1); /* pops condition */
  568. }
  569. | varlist1 '=' exprlist1
  570. {{
  571. int i;
  572. int left = 0;
  573. adjust_mult_assign($1, $3);
  574. for (i=$1-1; i>=0; i--)
  575. left = lua_codestore(i, left);
  576. adjuststack(left); /* remove eventual 'garbage' left on stack */
  577. }}
  578. | functioncall { adjust_functioncall($1, 0); }
  579. | LOCAL localnamelist decinit
  580. {
  581. L->currState->nlocalvar += $2;
  582. adjust_mult_assign($2, $3);
  583. }
  584. | FUNCTION funcname body { storevar($2); }
  585. ;
  586. block : {$<vInt>$ = L->currState->nlocalvar;} chunk
  587. {
  588. adjuststack(L->currState->nlocalvar - $<vInt>1);
  589. for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
  590. luaI_unregisterlocalvar(L->lexstate->linenumber);
  591. }
  592. ;
  593. funcname : varname { $$ = $1; init_func(); }
  594. | fvarname '.' fname
  595. {
  596. $$ = 0; /* flag indexed variable */
  597. init_func();
  598. }
  599. | fvarname ':' fname
  600. {
  601. $$ = 0; /* flag indexed variable */
  602. init_func();
  603. add_localvar(luaS_new("self"));
  604. }
  605. ;
  606. fvarname : varname { lua_pushvar($1); } ;
  607. fname : NAME { code_string($1); } ;
  608. body : '(' parlist ')' chunk END { func_onstack(close_func()); } ;
  609. elsepart : /* empty */
  610. | ELSE block
  611. | ELSEIF cond THEN block SaveWord elsepart { codeIf($2, $5); }
  612. ;
  613. ret : /* empty */
  614. | RETURN exprlist sc
  615. {
  616. adjust_functioncall($2, MULT_RET);
  617. codereturn();
  618. }
  619. ;
  620. GetPC : /* empty */ { $$ = L->currState->pc; } ;
  621. SaveWord : /* empty */
  622. { $$ = L->currState->pc;
  623. check_pc(JMPSIZE);
  624. L->currState->pc += JMPSIZE; /* open space */
  625. }
  626. ;
  627. SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ } ;
  628. SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ } ;
  629. cond : expr1 SaveWordPop { $$ = $2; } ;
  630. expr1 : expr { adjust_functioncall($1, 1); } ;
  631. expr : '(' expr ')' { $$ = $2; }
  632. | expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
  633. | expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
  634. | expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
  635. | expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
  636. | expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
  637. | expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
  638. | expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
  639. | expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
  640. | expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
  641. | expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
  642. | expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
  643. | expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
  644. | '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
  645. | NOT expr1 { code_unop(NOTOP); $$ = 0;}
  646. | sexp { $$ = $1; /* simple expressions */ }
  647. | table { $$ = 0; }
  648. | NUMBER { code_number($1); $$ = 0; }
  649. | STRING { code_string($1); $$ = 0; }
  650. | NIL { adjuststack(-1); $$ = 0; }
  651. | FUNCTION { init_func(); } body { $$ = 0; }
  652. | expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
  653. | expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
  654. ;
  655. sexp1 : sexp { adjust_functioncall($1, 1); } ;
  656. sexp : var { lua_pushvar($1); $$ = 0; }
  657. | '%' NAME { pushupvalue($2); $$ = 0; }
  658. | functioncall { $$ = $1; }
  659. ;
  660. var : varname { $$ = $1; }
  661. | sexp1 '[' expr1 ']' { $$ = 0; } /* indexed variable */
  662. | sexp1 '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
  663. ;
  664. varname : NAME { $$ = singlevar($1, L->currState); } ;
  665. table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); } ;
  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 : sexp1 { $$ = 0; }
  675. | sexp1 ':' NAME
  676. {
  677. code_oparg(PUSHSELF, 8, string_constant($3, L->currState), 1);
  678. $$ = 1;
  679. }
  680. ;
  681. funcParams : '(' exprlist ')' { $$ = adjust_functioncall($2, 1); }
  682. | table { $$ = 1; }
  683. | STRING { code_string($1); $$ = 1; }
  684. ;
  685. exprlist : /* empty */ { $$ = 0; }
  686. | exprlist1 { $$ = $1; }
  687. ;
  688. exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
  689. | exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
  690. {
  691. if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
  692. else {
  693. L->currState->f->code[$4] = $<vLong>3; /* store list length */
  694. $$ = $4;
  695. }
  696. }
  697. ;
  698. parlist : /* empty */ { code_args(0, 0); }
  699. | DOTS { code_args(0, 1); }
  700. | localnamelist { code_args($1, 0); }
  701. | localnamelist ',' DOTS { code_args($1, 1); }
  702. ;
  703. fieldlist : part { $$ = abs($1); }
  704. | part ';' part
  705. {
  706. if ($1*$3 > 0) /* repeated parts? */
  707. luaY_error("invalid constructor syntax");
  708. $$ = abs($1)+abs($3);
  709. }
  710. ;
  711. part : /* empty */ { $$ = 0; }
  712. | ffieldlist { $$ = $1; }
  713. | lfieldlist { $$ = $1; }
  714. ;
  715. lastcomma : /* empty */ | ',' ;
  716. ffieldlist : ffieldlist1 lastcomma
  717. {
  718. flush_record($1%RFIELDS_PER_FLUSH);
  719. $$ = -$1; /* negative signals a "record" part */
  720. }
  721. ;
  722. lfieldlist : lfieldlist1 lastcomma
  723. {
  724. flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH);
  725. $$ = $1;
  726. }
  727. ;
  728. ffieldlist1 : ffield {$$=1;}
  729. | ffieldlist1 ',' ffield
  730. {
  731. $$=$1+1;
  732. if ($$%RFIELDS_PER_FLUSH == 0)
  733. flush_record(RFIELDS_PER_FLUSH);
  734. }
  735. ;
  736. ffield : ffieldkey '=' expr1 ;
  737. ffieldkey : '[' expr1 ']'
  738. | fname
  739. ;
  740. lfieldlist1 : expr1 {$$=1;}
  741. | lfieldlist1 ',' expr1
  742. {
  743. $$=$1+1;
  744. if ($$%LFIELDS_PER_FLUSH == 0)
  745. flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
  746. }
  747. ;
  748. varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
  749. | varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
  750. ;
  751. localnamelist : NAME {store_localvar($1, 0); $$ = 1;}
  752. | localnamelist ',' NAME { store_localvar($3, $1); $$ = $1+1; }
  753. ;
  754. decinit : /* empty */ { $$ = 0; }
  755. | '=' exprlist1 { $$ = $2; }
  756. ;
  757. %%