lua.stx 20 KB

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