/*1** $Id: lparser.h $2** Lua Parser3** See Copyright Notice in lua.h4*/56#ifndef lparser_h7#define lparser_h89#include "llimits.h"10#include "lobject.h"11#include "lzio.h"121314/*15** Expression and variable descriptor.16** Code generation for variables and expressions can be delayed to allow17** optimizations; An 'expdesc' structure describes a potentially-delayed18** variable/expression. It has a description of its "main" value plus a19** list of conditional jumps that can also produce its value (generated20** by short-circuit operators 'and'/'or').21*/2223/* kinds of variables/expressions */24typedef enum {25VVOID, /* when 'expdesc' describes the last expression of a list,26this kind means an empty list (so, no expression) */27VNIL, /* constant nil */28VTRUE, /* constant true */29VFALSE, /* constant false */30VK, /* constant in 'k'; info = index of constant in 'k' */31VKFLT, /* floating constant; nval = numerical float value */32VKINT, /* integer constant; ival = numerical integer value */33VKSTR, /* string constant; strval = TString address;34(string is fixed by the lexer) */35VNONRELOC, /* expression has its value in a fixed register;36info = result register */37VLOCAL, /* local variable; var.ridx = register index;38var.vidx = relative index in 'actvar.arr' */39VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */40VCONST, /* compile-time <const> variable;41info = absolute index in 'actvar.arr' */42VINDEXED, /* indexed variable;43ind.t = table register;44ind.idx = key's R index */45VINDEXUP, /* indexed upvalue;46ind.t = table upvalue;47ind.idx = key's K index */48VINDEXI, /* indexed variable with constant integer;49ind.t = table register;50ind.idx = key's value */51VINDEXSTR, /* indexed variable with literal string;52ind.t = table register;53ind.idx = key's K index */54VJMP, /* expression is a test/comparison;55info = pc of corresponding jump instruction */56VRELOC, /* expression can put result in any register;57info = instruction pc */58VCALL, /* expression is a function call; info = instruction pc */59VVARARG /* vararg expression; info = instruction pc */60} expkind;616263#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)64#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)656667typedef struct expdesc {68expkind k;69union {70lua_Integer ival; /* for VKINT */71lua_Number nval; /* for VKFLT */72TString *strval; /* for VKSTR */73int info; /* for generic use */74struct { /* for indexed variables */75short idx; /* index (R or "long" K) */76lu_byte t; /* table (register or upvalue) */77} ind;78struct { /* for local variables */79lu_byte ridx; /* register holding the variable */80unsigned short vidx; /* compiler index (in 'actvar.arr') */81} var;82} u;83int t; /* patch list of 'exit when true' */84int f; /* patch list of 'exit when false' */85} expdesc;868788/* kinds of variables */89#define VDKREG 0 /* regular */90#define RDKCONST 1 /* constant */91#define RDKTOCLOSE 2 /* to-be-closed */92#define RDKCTC 3 /* compile-time constant */9394/* description of an active local variable */95typedef union Vardesc {96struct {97TValuefields; /* constant value (if it is a compile-time constant) */98lu_byte kind;99lu_byte ridx; /* register holding the variable */100short pidx; /* index of the variable in the Proto's 'locvars' array */101TString *name; /* variable name */102} vd;103TValue k; /* constant value (if any) */104} Vardesc;105106107108/* description of pending goto statements and label statements */109typedef struct Labeldesc {110TString *name; /* label identifier */111int pc; /* position in code */112int line; /* line where it appeared */113lu_byte nactvar; /* number of active variables in that position */114lu_byte close; /* goto that escapes upvalues */115} Labeldesc;116117118/* list of labels or gotos */119typedef struct Labellist {120Labeldesc *arr; /* array */121int n; /* number of entries in use */122int size; /* array size */123} Labellist;124125126/* dynamic structures used by the parser */127typedef struct Dyndata {128struct { /* list of all active local variables */129Vardesc *arr;130int n;131int size;132} actvar;133Labellist gt; /* list of pending gotos */134Labellist label; /* list of active labels */135} Dyndata;136137138/* control of blocks */139struct BlockCnt; /* defined in lparser.c */140141142/* state needed to generate code for a given function */143typedef struct FuncState {144Proto *f; /* current function header */145struct FuncState *prev; /* enclosing function */146struct LexState *ls; /* lexical state */147struct BlockCnt *bl; /* chain of current blocks */148int pc; /* next position to code (equivalent to 'ncode') */149int lasttarget; /* 'label' of last 'jump label' */150int previousline; /* last line that was saved in 'lineinfo' */151int nk; /* number of elements in 'k' */152int np; /* number of elements in 'p' */153int nabslineinfo; /* number of elements in 'abslineinfo' */154int firstlocal; /* index of first local var (in Dyndata array) */155int firstlabel; /* index of first label (in 'dyd->label->arr') */156short ndebugvars; /* number of elements in 'f->locvars' */157lu_byte nactvar; /* number of active local variables */158lu_byte nups; /* number of upvalues */159lu_byte freereg; /* first free register */160lu_byte iwthabs; /* instructions issued since last absolute line info */161lu_byte needclose; /* function needs to close upvalues when returning */162} FuncState;163164165LUAI_FUNC int luaY_nvarstack (FuncState *fs);166LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,167Dyndata *dyd, const char *name, int firstchar);168169170#endif171172173