-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyntax_tree_node.h
More file actions
259 lines (189 loc) · 6.29 KB
/
Copy pathsyntax_tree_node.h
File metadata and controls
259 lines (189 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#pragma once
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "types.h"
// Inheritance structure of syntax tree nodes gathered from:
// https://docs.python.org/3/library/ast.html
// ----------------------------------------------------------------------------
// Tags for various syntax tree node subcontexts.
// ----------------------------------------------------------------------------
enum class ExprContextType { LOAD, STORE, DEL };
enum class BooleanOpType { AND, OR };
enum class UnaryOpType {
INVERT,
NOT,
POSITIVE,
NEGATIVE,
};
enum class BinaryOpType {
ADD,
SUBTRACT,
MULTIPLY,
MATMUL,
DIVIDE,
MODULO,
POWER,
LEFT_SHIFT,
RIGHT_SHIFT,
BITWISE_OR,
BITWISE_XOR,
BITWISE_AND,
FLOOR_DIVIDE
};
enum class CompareOpType {
EQUALS,
NOT_EQUALS,
LESS_THAN,
LESS_EQUAL,
GREATER_THAN,
GREATER_EQUAL,
IS,
IS_NOT,
IN,
NOT_IN,
};
// TODO(erik):
// - Comprehension
// - Exception handlers
// - Arguments
// - Arg
// - Keywords
// - Aliases
// - With item
// - Match case
// - Pattern
// - Type ignore
struct SyntaxTreeVisitor;
// ----------------------------------------------------------------------------
// Base syntax tree node.
// ----------------------------------------------------------------------------
struct SyntaxTreeNode {
using Ptr = std::unique_ptr<SyntaxTreeNode>;
virtual void Visit(SyntaxTreeVisitor* visitor) = 0;
};
// ----------------------------------------------------------------------------
// Intermediate nodes (not concrete).
// ----------------------------------------------------------------------------
struct ModuleNode : public SyntaxTreeNode {
using Ptr = std::unique_ptr<ModuleNode>;
};
struct StatementNode : public SyntaxTreeNode {
using Ptr = std::unique_ptr<StatementNode>;
};
struct ExpressionNode : public SyntaxTreeNode {
using Ptr = std::unique_ptr<ExpressionNode>;
};
// ----------------------------------------------------------------------------
// Module nodes.
// ----------------------------------------------------------------------------
struct Module : public ModuleNode {
std::vector<StatementNode::Ptr> body;
void Visit(SyntaxTreeVisitor* visitor) override;
};
struct Interactive : public ModuleNode {
std::vector<StatementNode::Ptr> body;
void Visit(SyntaxTreeVisitor* visitor) override;
};
struct Expression : public ModuleNode {
ExpressionNode::Ptr body;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// TODO(erik): Do we need this? This is for type annotations.
// struct FunctionType : public ModuleNode {};
// ----------------------------------------------------------------------------
// Statement nodes.
// ----------------------------------------------------------------------------
// struct FunctionDef : public StatementNode {};
// struct AsyncFunctionDef : public StatementNode {};
// struct ClassDef : public StatementNode {};
// struct Return : public StatementNode {};
struct Delete : public StatementNode {
std::vector<ExpressionNode::Ptr> targets;
void Visit(SyntaxTreeVisitor* visitor) override;
};
struct Assign : public StatementNode {
std::vector<ExpressionNode::Ptr> targets;
ExpressionNode::Ptr value;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct AugAssign : public StatementNode {};
// struct AnnAssign : public StatementNode {};
// struct For : public StatementNode {};
// struct AsyncFor : public StatementNode {};
// struct While : public StatementNode {};
struct If : public StatementNode {
ExpressionNode::Ptr test;
std::vector<StatementNode::Ptr> then_body;
std::vector<StatementNode::Ptr> else_body;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct With : public StatementNode {};
// struct AsyncWith : public StatementNode {};
// struct Match : public StatementNode {};
// struct Raise : public StatementNode {};
// struct Try : public StatementNode {};
// struct TryStar : public StatementNode {};
// struct Assert : public StatementNode {};
// struct Import : public StatementNode {};
// struct ImportFrom : public StatementNode {};
// struct Global : public StatementNode {};
// struct Nonlocal : public StatementNode {};
struct Expr : public StatementNode {
ExpressionNode::Ptr expr;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct Pass : public StatementNode {};
// struct Break : public StatementNode {};
// struct Continue : public StatementNode {};
// ----------------------------------------------------------------------------
// Expression nodes.
// ----------------------------------------------------------------------------
// struct BooleanOp : public ExpressionNode {};
// struct NamedExpr : public ExpressionNode {};
struct BinaryOp : public ExpressionNode {
ExpressionNode::Ptr lhs, rhs;
BinaryOpType op_type;
void Visit(SyntaxTreeVisitor* visitor) override;
};
struct UnaryOp : public ExpressionNode {
ExpressionNode::Ptr operand;
UnaryOpType op_type;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct Lambda : public ExpressionNode {};
// struct IfExp : public ExpressionNode {};
// struct List : public ExpressionNode {};
// struct Dict : public ExpressionNode {};
// struct Set : public ExpressionNode {};
// struct Tuple : public ExpressionNode {};
// struct ListComp : public ExpressionNode {};
// struct SetComp : public ExpressionNode {};
// struct DictComp : public ExpressionNode {};
// struct Generator : public ExpressionNode {};
// struct Await : public ExpressionNode {};
// struct Yield : public ExpressionNode {};
// struct YieldFrom : public ExpressionNode {};
struct Compare : public ExpressionNode {
ExpressionNode::Ptr lhs;
std::vector<CompareOpType> ops;
std::vector<ExpressionNode::Ptr> comparators;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct Call : public ExpressionNode {};
// struct FormattedValue : public ExpressionNode {};
// struct JoinedStr : public ExpressionNode {};
struct Constant : public ExpressionNode {
ConstantValue value;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct Attribute : public ExpressionNode {};
// struct Subscript : public ExpressionNode {};
// struct Starred : public ExpressionNode {};
struct Name : public ExpressionNode {
Identifier id;
ExprContextType ctx_type;
void Visit(SyntaxTreeVisitor* visitor) override;
};
// struct Slice : public ExpressionNode {};