lparser.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp $
  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 "lzio.h"
  11. /*
  12. ** Expression and variable descriptor.
  13. ** Code generation for variables and expressions can be delayed to allow
  14. ** optimizations; An 'expdesc' structure describes a potentially-delayed
  15. ** variable/expression. It has a description of its "main" value plus a
  16. ** list of conditional jumps that can also produce its value (generated
  17. ** by short-circuit operators 'and'/'or').
  18. */
  19. /* kinds of variables/expressions */
  20. typedef enum {
  21. VVOID, /* when 'expdesc' describes the last expression a list,
  22. this kind means an empty list (so, no expression) */
  23. VNIL, /* constant nil */
  24. VTRUE, /* constant true */
  25. VFALSE, /* constant false */
  26. VK, /* constant in 'k'; info = index of constant in 'k' */
  27. VKFLT, /* floating constant; nval = numerical float value */
  28. VKINT, /* integer constant; nval = numerical integer value */
  29. VNONRELOC, /* expression has its value in a fixed register;
  30. info = result register */
  31. VLOCAL, /* local variable; info = local register */
  32. VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
  33. VINDEXED, /* indexed variable;
  34. ind.vt = whether 't' is register or upvalue;
  35. ind.t = table register or upvalue;
  36. ind.idx = key's R/K index */
  37. VJMP, /* expression is a test/comparison;
  38. info = pc of corresponding jump instruction */
  39. VRELOCABLE, /* expression can put result in any register;
  40. info = instruction pc */
  41. VCALL, /* expression is a function call; info = instruction pc */
  42. VVARARG /* vararg expression; info = instruction pc */
  43. } expkind;
  44. #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED)
  45. #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL)
  46. typedef struct expdesc {
  47. expkind k;
  48. union {
  49. lua_Integer ival; /* for VKINT */
  50. lua_Number nval; /* for VKFLT */
  51. int info; /* for generic use */
  52. struct { /* for indexed variables (VINDEXED) */
  53. short idx; /* index (R/K) */
  54. lu_byte t; /* table (register or upvalue) */
  55. lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
  56. } ind;
  57. } u;
  58. int t; /* patch list of 'exit when true' */
  59. int f; /* patch list of 'exit when false' */
  60. } expdesc;
  61. /* description of active local variable */
  62. typedef struct Vardesc {
  63. short idx; /* variable index in stack */
  64. } Vardesc;
  65. /* description of pending goto statements and label statements */
  66. typedef struct Labeldesc {
  67. TString *name; /* label identifier */
  68. int pc; /* position in code */
  69. int line; /* line where it appeared */
  70. lu_byte nactvar; /* local level where it appears in current block */
  71. } Labeldesc;
  72. /* list of labels or gotos */
  73. typedef struct Labellist {
  74. Labeldesc *arr; /* array */
  75. int n; /* number of entries in use */
  76. int size; /* array size */
  77. } Labellist;
  78. /* dynamic structures used by the parser */
  79. typedef struct Dyndata {
  80. struct { /* list of active local variables */
  81. Vardesc *arr;
  82. int n;
  83. int size;
  84. } actvar;
  85. Labellist gt; /* list of pending gotos */
  86. Labellist label; /* list of active labels */
  87. } Dyndata;
  88. /* control of blocks */
  89. struct BlockCnt; /* defined in lparser.c */
  90. /* state needed to generate code for a given function */
  91. typedef struct FuncState {
  92. Proto *f; /* current function header */
  93. struct FuncState *prev; /* enclosing function */
  94. struct LexState *ls; /* lexical state */
  95. struct BlockCnt *bl; /* chain of current blocks */
  96. int pc; /* next position to code (equivalent to 'ncode') */
  97. int lasttarget; /* 'label' of last 'jump label' */
  98. int jpc; /* list of pending jumps to 'pc' */
  99. int nk; /* number of elements in 'k' */
  100. int np; /* number of elements in 'p' */
  101. int firstlocal; /* index of first local var (in Dyndata array) */
  102. short nlocvars; /* number of elements in 'f->locvars' */
  103. lu_byte nactvar; /* number of active local variables */
  104. lu_byte nups; /* number of upvalues */
  105. lu_byte freereg; /* first free register */
  106. } FuncState;
  107. LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
  108. Dyndata *dyd, const char *name, int firstchar);
  109. #endif