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