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