interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lparser.h

197 lines · 7118 bytes Raw Blame History
1/*
2** $Id: lparser.h $
3** Lua Parser
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lparser_h
8#define lparser_h
9
10#include "llimits.h"
11#include "lobject.h"
12#include "lzio.h"
13
14
15/*
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').
22*/
23
24/* kinds of variables/expressions */
25typedef enum {
26 VVOID, /* when 'expdesc' describes the last expression of 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; ival = numerical integer value */
34 VKSTR, /* string constant; strval = TString address;
35 (string is fixed by the scanner) */
36 VNONRELOC, /* expression has its value in a fixed register;
37 info = result register */
38 VLOCAL, /* local variable; var.ridx = register index;
39 var.vidx = relative index in 'actvar.arr' */
40 VVARGVAR, /* vararg parameter; var.ridx = register index;
41 var.vidx = relative index in 'actvar.arr' */
42 VGLOBAL, /* global variable;
43 info = relative index in 'actvar.arr' (or -1 for
44 implicit declaration) */
45 VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
46 VCONST, /* compile-time <const> variable;
47 info = absolute index in 'actvar.arr' */
48 VINDEXED, /* indexed variable;
49 ind.t = table register;
50 ind.idx = key's R index;
51 ind.ro = true if it represents a read-only global;
52 ind.keystr = if key is a string, index in 'k' of that string;
53 -1 if key is not a string */
54 VVARGIND, /* indexed vararg parameter;
55 ind.* as in VINDEXED */
56 VINDEXUP, /* indexed upvalue;
57 ind.idx = key's K index;
58 ind.* as in VINDEXED */
59 VINDEXI, /* indexed variable with constant integer;
60 ind.t = table register;
61 ind.idx = key's value */
62 VINDEXSTR, /* indexed variable with literal string;
63 ind.idx = key's K index;
64 ind.* as in VINDEXED */
65 VJMP, /* expression is a test/comparison;
66 info = pc of corresponding jump instruction */
67 VRELOC, /* expression can put result in any register;
68 info = instruction pc */
69 VCALL, /* expression is a function call; info = instruction pc */
70 VVARARG /* vararg expression; info = instruction pc */
71} expkind;
72
73
74#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)
75#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)
76
77
78typedef struct expdesc {
79 expkind k;
80 union {
81 lua_Integer ival; /* for VKINT */
82 lua_Number nval; /* for VKFLT */
83 TString *strval; /* for VKSTR */
84 int info; /* for generic use */
85 struct { /* for indexed variables */
86 short idx; /* index (R or "long" K) */
87 lu_byte t; /* table (register or upvalue) */
88 lu_byte ro; /* true if variable is read-only */
89 int keystr; /* index in 'k' of string key, or -1 if not a string */
90 } ind;
91 struct { /* for local variables */
92 lu_byte ridx; /* register holding the variable */
93 short vidx; /* index in 'actvar.arr' */
94 } var;
95 } u;
96 int t; /* patch list of 'exit when true' */
97 int f; /* patch list of 'exit when false' */
98} expdesc;
99
100
101/* kinds of variables */
102#define VDKREG 0 /* regular local */
103#define RDKCONST 1 /* local constant */
104#define RDKVAVAR 2 /* vararg parameter */
105#define RDKTOCLOSE 3 /* to-be-closed */
106#define RDKCTC 4 /* local compile-time constant */
107#define GDKREG 5 /* regular global */
108#define GDKCONST 6 /* global constant */
109
110/* variables that live in registers */
111#define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE)
112
113/* test for global variables */
114#define varglobal(v) ((v)->vd.kind >= GDKREG)
115
116
117/* description of an active variable */
118typedef union Vardesc {
119 struct {
120 TValuefields; /* constant value (if it is a compile-time constant) */
121 lu_byte kind;
122 lu_byte ridx; /* register holding the variable */
123 short pidx; /* index of the variable in the Proto's 'locvars' array */
124 TString *name; /* variable name */
125 } vd;
126 TValue k; /* constant value (if any) */
127} Vardesc;
128
129
130
131/* description of pending goto statements and label statements */
132typedef struct Labeldesc {
133 TString *name; /* label identifier */
134 int pc; /* position in code */
135 int line; /* line where it appeared */
136 short nactvar; /* number of active variables in that position */
137 lu_byte close; /* true for goto that escapes upvalues */
138} Labeldesc;
139
140
141/* list of labels or gotos */
142typedef struct Labellist {
143 Labeldesc *arr; /* array */
144 int n; /* number of entries in use */
145 int size; /* array size */
146} Labellist;
147
148
149/* dynamic structures used by the parser */
150typedef struct Dyndata {
151 struct { /* list of all active local variables */
152 Vardesc *arr;
153 int n;
154 int size;
155 } actvar;
156 Labellist gt; /* list of pending gotos */
157 Labellist label; /* list of active labels */
158} Dyndata;
159
160
161/* control of blocks */
162struct BlockCnt; /* defined in lparser.c */
163
164
165/* state needed to generate code for a given function */
166typedef struct FuncState {
167 Proto *f; /* current function header */
168 struct FuncState *prev; /* enclosing function */
169 struct LexState *ls; /* lexical state */
170 struct BlockCnt *bl; /* chain of current blocks */
171 Table *kcache; /* cache for reusing constants */
172 int pc; /* next position to code (equivalent to 'ncode') */
173 int lasttarget; /* 'label' of last 'jump label' */
174 int previousline; /* last line that was saved in 'lineinfo' */
175 int nk; /* number of elements in 'k' */
176 int np; /* number of elements in 'p' */
177 int nabslineinfo; /* number of elements in 'abslineinfo' */
178 int firstlocal; /* index of first local var (in Dyndata array) */
179 int firstlabel; /* index of first label (in 'dyd->label->arr') */
180 short ndebugvars; /* number of elements in 'f->locvars' */
181 short nactvar; /* number of active variable declarations */
182 lu_byte nups; /* number of upvalues */
183 lu_byte freereg; /* first free register */
184 lu_byte iwthabs; /* instructions issued since last absolute line info */
185 lu_byte needclose; /* function needs to close upvalues when returning */
186} FuncState;
187
188
189LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs);
190LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l,
191 const char *what);
192LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Table *anchor,
193 Mbuffer *buff, Dyndata *dyd,
194 const char *name, int firstchar);
195
196
197#endif