lparser.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. ** $Id: lparser.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Lua Parser
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lparser_h
  7. #define lparser_h
  8. #include "llimits.h"
  9. #include "lobject.h"
  10. #include "ltable.h"
  11. #include "lzio.h"
  12. /* small implementation of bit arrays */
  13. #define BPW (CHAR_BIT*sizeof(unsigned int)) /* bits per word */
  14. #define words2bits(b) (((b)-1)/BPW + 1)
  15. #define setbit(a, b) ((a)[(b)/BPW] |= (1 << (b)%BPW))
  16. #define resetbit(a, b) ((a)[(b)/BPW] &= ~((1 << (b)%BPW)))
  17. #define testbit(a, b) ((a)[(b)/BPW] & (1 << (b)%BPW))
  18. /*
  19. ** Expression descriptor
  20. */
  21. typedef enum {
  22. VVOID, /* no value */
  23. VNIL,
  24. VTRUE,
  25. VFALSE,
  26. VNUMBER, /* n = value */
  27. VK, /* info = index of constant in `k' */
  28. VLOCAL, /* info = local register */
  29. VUPVAL, /* info = index of upvalue in `upvalues' */
  30. VGLOBAL, /* info = index of global name in `k' */
  31. VINDEXED, /* info = table register; aux = index register (or `k') */
  32. VRELOCABLE, /* info = instruction pc */
  33. VNONRELOC, /* info = result register */
  34. VJMP, /* info = result register */
  35. VCALL /* info = result register */
  36. } expkind;
  37. typedef struct expdesc {
  38. expkind k;
  39. union {
  40. struct {
  41. int info, aux;
  42. } i;
  43. lua_Number n;
  44. } u;
  45. int t; /* patch list of `exit when true' */
  46. int f; /* patch list of `exit when false' */
  47. } expdesc;
  48. /* state needed to generate code for a given function */
  49. typedef struct FuncState {
  50. Proto *f; /* current function header */
  51. struct FuncState *prev; /* enclosing function */
  52. struct LexState *ls; /* lexical state */
  53. struct lua_State *L; /* copy of the Lua state */
  54. int pc; /* next position to code (equivalent to `ncode') */
  55. int lasttarget; /* `pc' of last `jump target' */
  56. int jlt; /* list of jumps to `lasttarget' */
  57. int freereg; /* first free register */
  58. int nk; /* number of elements in `k' */
  59. Table *h; /* table to find (and reuse) elements in `k' */
  60. int np; /* number of elements in `p' */
  61. int nlineinfo; /* number of elements in `lineinfo' */
  62. int nlocvars; /* number of elements in `locvars' */
  63. int nactloc; /* number of active local variables */
  64. int lastline; /* line where last `lineinfo' was generated */
  65. struct Breaklabel *bl; /* chain of breakable blocks */
  66. expdesc upvalues[MAXUPVALUES]; /* upvalues */
  67. int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
  68. unsigned int wasup[words2bits(MAXLOCALS)]; /* bit array to mark whether a
  69. local variable was used as upvalue at some level */
  70. } FuncState;
  71. Proto *luaY_parser (lua_State *L, ZIO *z);
  72. #endif