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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* Source code for RCC-Legacy.
Copyright (C) 2026 Richard Qin
This file is part of RCC.
RCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
RCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with RCC; see the file LICENSE. If not see
<http://www.gnu.org/licenses/>. */
#pragma once
/// @file ast.h
/// @brief SysY AST Node Definitions
#include "rcc/sysy_type.h"
#include <memory>
#include <string>
#include <vector>
namespace rcc {
// Forward declarations of all AST nodes
struct ASTNode;
struct CompUnitNode;
struct FuncDefNode;
struct FuncFParamNode;
struct VarDeclNode;
struct VarDefNode;
struct BlockNode;
struct AssignStmtNode;
struct ExprStmtNode;
struct IfStmtNode;
struct WhileStmtNode;
struct BreakStmtNode;
struct ContinueStmtNode;
struct ReturnStmtNode;
struct IntLiteralNode;
struct FloatLiteralNode;
struct LValNode;
struct FuncCallNode;
struct BinaryExprNode;
struct UnaryExprNode;
struct ImplicitCastNode;
struct InitListNode;
using ASTPtr = std::unique_ptr<ASTNode>;
using ASTList = std::vector<ASTPtr>;
// operators
enum class BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Lt,
Gt,
Le,
Ge,
Eq,
Ne,
And,
Or,
};
enum class UnaryOp { Pos, Neg, Not };
// AST Node Kind
enum class NodeKind {
CompUnit,
FuncDef,
FuncFParam,
VarDecl,
VarDef,
Block,
AssignStmt,
ExprStmt,
IfStmt,
WhileStmt,
BreakStmt,
ContinueStmt,
ReturnStmt,
IntLiteral,
FloatLiteral,
LVal,
FuncCall,
UnaryExpr,
BinaryExpr,
ImplicitCast,
InitList,
};
// AST Node Base Class
struct ASTNode {
NodeKind kind;
BType eval_type = BType::Int;
int lineno = 0;
explicit ASTNode(NodeKind k) : kind(k) {}
virtual ~ASTNode() = default;
// no copy
ASTNode(const ASTNode&) = delete;
ASTNode& operator=(const ASTNode&) = delete;
ASTNode(ASTNode&&) = default;
ASTNode& operator=(ASTNode&&) = default;
};
// Top-level Nodes
struct CompUnitNode : ASTNode {
ASTList decls_and_funcs;
CompUnitNode() : ASTNode(NodeKind::CompUnit) {}
};
struct FuncDefNode : ASTNode {
BType ret_type = BType::Void;
std::string name;
ASTList params; // FuncFParamNode
ASTPtr body; // BlockNode
FuncDefNode() : ASTNode(NodeKind::FuncDef) {}
};
struct FuncFParamNode : ASTNode {
BType btype = BType::Int;
std::string name;
bool is_array = false;
ASTList dims; // first dim is empty, others are ConstExp
FuncFParamNode() : ASTNode(NodeKind::FuncFParam) {}
};
// declarations
struct VarDeclNode : ASTNode {
bool is_const = false;
BType btype = BType::Int;
ASTList defs; // VarDefNode
VarDeclNode() : ASTNode(NodeKind::VarDecl) {}
};
struct VarDefNode : ASTNode {
std::string name;
ASTList dims; // each dim size (ConstExp), scalar has empty dims list
ASTPtr init_val; // InitListNode | Expr | nullptr
VarDefNode() : ASTNode(NodeKind::VarDef) {}
};
struct InitListNode : ASTNode {
ASTList items; // Expr or InitListNode
InitListNode() : ASTNode(NodeKind::InitList) {}
};
// statements
struct BlockNode : ASTNode {
ASTList items; // Decl | Stmt
BlockNode() : ASTNode(NodeKind::Block) {}
};
struct AssignStmtNode : ASTNode {
ASTPtr lval; // LValNode
ASTPtr expr;
AssignStmtNode() : ASTNode(NodeKind::AssignStmt) {}
};
struct ExprStmtNode : ASTNode {
ASTPtr expr; // nullable (empty statement)
ExprStmtNode() : ASTNode(NodeKind::ExprStmt) {}
};
struct IfStmtNode : ASTNode {
ASTPtr cond;
ASTPtr then_stmt;
ASTPtr else_stmt; // nullable
IfStmtNode() : ASTNode(NodeKind::IfStmt) {}
};
struct WhileStmtNode : ASTNode {
ASTPtr cond;
ASTPtr body;
WhileStmtNode() : ASTNode(NodeKind::WhileStmt) {}
};
struct BreakStmtNode : ASTNode {
BreakStmtNode() : ASTNode(NodeKind::BreakStmt) {}
};
struct ContinueStmtNode : ASTNode {
ContinueStmtNode() : ASTNode(NodeKind::ContinueStmt) {}
};
struct ReturnStmtNode : ASTNode {
ASTPtr expr; // nullable for void return
ReturnStmtNode() : ASTNode(NodeKind::ReturnStmt) {}
};
// expressions
struct IntLiteralNode : ASTNode {
int32_t value = 0;
IntLiteralNode() : ASTNode(NodeKind::IntLiteral) { eval_type = BType::Int; }
};
struct FloatLiteralNode : ASTNode {
float value = 0.0f;
FloatLiteralNode() : ASTNode(NodeKind::FloatLiteral) { eval_type = BType::Float; }
};
struct LValNode : ASTNode {
std::string name;
ASTList indices; // index expressions
LValNode() : ASTNode(NodeKind::LVal) {}
};
struct FuncCallNode : ASTNode {
std::string name;
ASTList args;
FuncCallNode() : ASTNode(NodeKind::FuncCall) {}
};
struct BinaryExprNode : ASTNode {
BinOp op;
ASTPtr lhs, rhs;
BinaryExprNode() : ASTNode(NodeKind::BinaryExpr) {}
};
struct UnaryExprNode : ASTNode {
UnaryOp op;
ASTPtr operand;
UnaryExprNode() : ASTNode(NodeKind::UnaryExpr) {}
};
struct ImplicitCastNode : ASTNode {
BType target_type;
ASTPtr operand;
ImplicitCastNode() : ASTNode(NodeKind::ImplicitCast) {}
};
// AST utility functions
/// Print AST (for debugging)
void ast_dump(const ASTNode* node, int indent = 0);
} // namespace rcc
|