1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ 3*eda14cbcSMatt Macy ** Lua Parser 4*eda14cbcSMatt Macy ** See Copyright Notice in lua.h 5*eda14cbcSMatt Macy */ 6*eda14cbcSMatt Macy 7*eda14cbcSMatt Macy #ifndef lparser_h 8*eda14cbcSMatt Macy #define lparser_h 9*eda14cbcSMatt Macy 10*eda14cbcSMatt Macy #include "llimits.h" 11*eda14cbcSMatt Macy #include "lobject.h" 12*eda14cbcSMatt Macy #include "lzio.h" 13*eda14cbcSMatt Macy 14*eda14cbcSMatt Macy 15*eda14cbcSMatt Macy /* 16*eda14cbcSMatt Macy ** Expression descriptor 17*eda14cbcSMatt Macy */ 18*eda14cbcSMatt Macy 19*eda14cbcSMatt Macy typedef enum { 20*eda14cbcSMatt Macy VVOID, /* no value */ 21*eda14cbcSMatt Macy VNIL, 22*eda14cbcSMatt Macy VTRUE, 23*eda14cbcSMatt Macy VFALSE, 24*eda14cbcSMatt Macy VK, /* info = index of constant in `k' */ 25*eda14cbcSMatt Macy VKNUM, /* nval = numerical value */ 26*eda14cbcSMatt Macy VNONRELOC, /* info = result register */ 27*eda14cbcSMatt Macy VLOCAL, /* info = local register */ 28*eda14cbcSMatt Macy VUPVAL, /* info = index of upvalue in 'upvalues' */ 29*eda14cbcSMatt Macy VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30*eda14cbcSMatt Macy VJMP, /* info = instruction pc */ 31*eda14cbcSMatt Macy VRELOCABLE, /* info = instruction pc */ 32*eda14cbcSMatt Macy VCALL, /* info = instruction pc */ 33*eda14cbcSMatt Macy VVARARG /* info = instruction pc */ 34*eda14cbcSMatt Macy } expkind; 35*eda14cbcSMatt Macy 36*eda14cbcSMatt Macy 37*eda14cbcSMatt Macy #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38*eda14cbcSMatt Macy #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39*eda14cbcSMatt Macy 40*eda14cbcSMatt Macy typedef struct expdesc { 41*eda14cbcSMatt Macy expkind k; 42*eda14cbcSMatt Macy union { 43*eda14cbcSMatt Macy struct { /* for indexed variables (VINDEXED) */ 44*eda14cbcSMatt Macy short idx; /* index (R/K) */ 45*eda14cbcSMatt Macy lu_byte t; /* table (register or upvalue) */ 46*eda14cbcSMatt Macy lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47*eda14cbcSMatt Macy } ind; 48*eda14cbcSMatt Macy int info; /* for generic use */ 49*eda14cbcSMatt Macy lua_Number nval; /* for VKNUM */ 50*eda14cbcSMatt Macy } u; 51*eda14cbcSMatt Macy int t; /* patch list of `exit when true' */ 52*eda14cbcSMatt Macy int f; /* patch list of `exit when false' */ 53*eda14cbcSMatt Macy } expdesc; 54*eda14cbcSMatt Macy 55*eda14cbcSMatt Macy 56*eda14cbcSMatt Macy /* description of active local variable */ 57*eda14cbcSMatt Macy typedef struct Vardesc { 58*eda14cbcSMatt Macy short idx; /* variable index in stack */ 59*eda14cbcSMatt Macy } Vardesc; 60*eda14cbcSMatt Macy 61*eda14cbcSMatt Macy 62*eda14cbcSMatt Macy /* description of pending goto statements and label statements */ 63*eda14cbcSMatt Macy typedef struct Labeldesc { 64*eda14cbcSMatt Macy TString *name; /* label identifier */ 65*eda14cbcSMatt Macy int pc; /* position in code */ 66*eda14cbcSMatt Macy int line; /* line where it appeared */ 67*eda14cbcSMatt Macy lu_byte nactvar; /* local level where it appears in current block */ 68*eda14cbcSMatt Macy } Labeldesc; 69*eda14cbcSMatt Macy 70*eda14cbcSMatt Macy 71*eda14cbcSMatt Macy /* list of labels or gotos */ 72*eda14cbcSMatt Macy typedef struct Labellist { 73*eda14cbcSMatt Macy Labeldesc *arr; /* array */ 74*eda14cbcSMatt Macy int n; /* number of entries in use */ 75*eda14cbcSMatt Macy int size; /* array size */ 76*eda14cbcSMatt Macy } Labellist; 77*eda14cbcSMatt Macy 78*eda14cbcSMatt Macy 79*eda14cbcSMatt Macy /* dynamic structures used by the parser */ 80*eda14cbcSMatt Macy typedef struct Dyndata { 81*eda14cbcSMatt Macy struct { /* list of active local variables */ 82*eda14cbcSMatt Macy Vardesc *arr; 83*eda14cbcSMatt Macy int n; 84*eda14cbcSMatt Macy int size; 85*eda14cbcSMatt Macy } actvar; 86*eda14cbcSMatt Macy Labellist gt; /* list of pending gotos */ 87*eda14cbcSMatt Macy Labellist label; /* list of active labels */ 88*eda14cbcSMatt Macy } Dyndata; 89*eda14cbcSMatt Macy 90*eda14cbcSMatt Macy 91*eda14cbcSMatt Macy /* control of blocks */ 92*eda14cbcSMatt Macy struct BlockCnt; /* defined in lparser.c */ 93*eda14cbcSMatt Macy 94*eda14cbcSMatt Macy 95*eda14cbcSMatt Macy /* state needed to generate code for a given function */ 96*eda14cbcSMatt Macy typedef struct FuncState { 97*eda14cbcSMatt Macy Proto *f; /* current function header */ 98*eda14cbcSMatt Macy Table *h; /* table to find (and reuse) elements in `k' */ 99*eda14cbcSMatt Macy struct FuncState *prev; /* enclosing function */ 100*eda14cbcSMatt Macy struct LexState *ls; /* lexical state */ 101*eda14cbcSMatt Macy struct BlockCnt *bl; /* chain of current blocks */ 102*eda14cbcSMatt Macy int pc; /* next position to code (equivalent to `ncode') */ 103*eda14cbcSMatt Macy int lasttarget; /* 'label' of last 'jump label' */ 104*eda14cbcSMatt Macy int jpc; /* list of pending jumps to `pc' */ 105*eda14cbcSMatt Macy int nk; /* number of elements in `k' */ 106*eda14cbcSMatt Macy int np; /* number of elements in `p' */ 107*eda14cbcSMatt Macy int firstlocal; /* index of first local var (in Dyndata array) */ 108*eda14cbcSMatt Macy short nlocvars; /* number of elements in 'f->locvars' */ 109*eda14cbcSMatt Macy lu_byte nactvar; /* number of active local variables */ 110*eda14cbcSMatt Macy lu_byte nups; /* number of upvalues */ 111*eda14cbcSMatt Macy lu_byte freereg; /* first free register */ 112*eda14cbcSMatt Macy } FuncState; 113*eda14cbcSMatt Macy 114*eda14cbcSMatt Macy 115*eda14cbcSMatt Macy LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116*eda14cbcSMatt Macy Dyndata *dyd, const char *name, int firstchar); 117*eda14cbcSMatt Macy 118*eda14cbcSMatt Macy 119*eda14cbcSMatt Macy #endif 120