-
Notifications
You must be signed in to change notification settings - Fork 2
/
Expr.cpp
247 lines (227 loc) · 6.19 KB
/
Expr.cpp
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
/**
* @file Expr.cpp
* 描述Stmt及其子类转换为Json格式的方式
**/
#include "Expr.h"
#include <string>
using namespace AST;
/**
* @brief 取得双目运算符对应的字符串
* @param op 该运算符的枚举类型
* @return std::string
*/
static std::string getBinOpcode(BinaryOperatorKind op)
{
switch (op)
{
case BinaryOperatorKind::BO_Mul:
return "*";
case BinaryOperatorKind::BO_Div:
return "/";
case BinaryOperatorKind::BO_Rem:
return "%";
case BinaryOperatorKind::BO_Add:
return "+";
case BinaryOperatorKind::BO_Sub:
return "-";
case BinaryOperatorKind::BO_Shl:
return "<<";
case BinaryOperatorKind::BO_Shr:
return ">>";
case BinaryOperatorKind::BO_LT:
return "<";
case BinaryOperatorKind::BO_GT:
return ">";
case BinaryOperatorKind::BO_LE:
return "<=";
case BinaryOperatorKind::BO_GE:
return ">=";
case BinaryOperatorKind::BO_EQ:
return "==";
case BinaryOperatorKind::BO_NE:
return "!=";
case BinaryOperatorKind::BO_And:
return "&";
case BinaryOperatorKind::BO_Xor:
return "^";
case BinaryOperatorKind::BO_Or:
return "|";
case BinaryOperatorKind::BO_LAnd:
return "&&";
case BinaryOperatorKind::BO_LOr:
return "||";
case BinaryOperatorKind::BO_Assign:
return "=";
case BinaryOperatorKind::BO_MulAssign:
return "*=";
case BinaryOperatorKind::BO_DivAssign:
return "/=";
case BinaryOperatorKind::BO_RemAssign:
return "%=";
case BinaryOperatorKind::BO_AddAssign:
return "+=";
case BinaryOperatorKind::BO_SubAssign:
return "-=";
case BinaryOperatorKind::BO_ShlAssign:
return "<<=";
case BinaryOperatorKind::BO_ShrAssign:
return ">>=";
case BinaryOperatorKind::BO_AndAssign:
return "&=";
case BinaryOperatorKind::BO_XorAssign:
return "^=";
case BinaryOperatorKind::BO_OrAssign:
return "|=";
default:
return "";
}
}
static std::string getUnaryOpcode(UnaryOperatorKind op)
{
switch (op)
{
case AST::UnaryOperatorKind::UO_PostInc:
case AST::UnaryOperatorKind::UO_PreInc:
return "++";
case AST::UnaryOperatorKind::UO_PostDec:
case AST::UnaryOperatorKind::UO_PreDec:
return "--";
case AST::UnaryOperatorKind::UO_Plus:
return "+";
case AST::UnaryOperatorKind::UO_Minus:
return "-";
case AST::UnaryOperatorKind::UO_Not:
return "~";
case AST::UnaryOperatorKind::UO_LNot:
return "!";
default:
return "";
}
}
AST::BinaryOperator::BinaryOperator(BinaryOperatorKind op, unique_ptr<Expr> LHS, unique_ptr<Expr> RHS) : op(op)
{
if (!isAssignment() && LHS->getIsLvalue())
{
LHS = std::make_unique<ImplicitCastExpr>(std::move(LHS), "LValueToRValue");
}
if (RHS->getIsLvalue())
{
RHS = std::make_unique<ImplicitCastExpr>(std::move(RHS), "LValueToRValue");
}
this->LHS = std::move(LHS);
this->RHS = std::move(RHS);
}
bool AST::BinaryOperator::isAssignment() const
{
switch (op)
{
case AST::BinaryOperatorKind::BO_Assign:
case AST::BinaryOperatorKind::BO_MulAssign:
case AST::BinaryOperatorKind::BO_DivAssign:
case AST::BinaryOperatorKind::BO_RemAssign:
case AST::BinaryOperatorKind::BO_AddAssign:
case AST::BinaryOperatorKind::BO_SubAssign:
case AST::BinaryOperatorKind::BO_ShlAssign:
case AST::BinaryOperatorKind::BO_ShrAssign:
case AST::BinaryOperatorKind::BO_AndAssign:
case AST::BinaryOperatorKind::BO_XorAssign:
case AST::BinaryOperatorKind::BO_OrAssign:
return true;
default:
return false;
}
}
json AST::BinaryOperator::toJson() const
{
json res = {
{"kind", "BinaryOperator"},
{"opcode", getBinOpcode(op)}
};
res["inner"] = json::array({ LHS->toJson(), RHS->toJson() });
return res;
}
Value* AST::BinaryOperator::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json IntegerLiteral::toJson() const
{
return {
{"kind", "IntegerLiteral"},
{"type", "int"},
{"value", std::to_string(value)}
};
}
Value* AST::IntegerLiteral::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json DeclRefExpr::toJson() const
{
return {
{"kind", "DeclRefExpr"},
{"name", name}
};
}
Value* AST::DeclRefExpr::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
Function* AST::DeclRefExpr::genFunc(CodeGeneratorBase& generator) const
{
return nullptr;
}
json ParenExpr::toJson() const
{
return {
{"kind", "ParenExpr"},
{"inner", json::array({subExpr->toJson()})}
};
}
Value* AST::ParenExpr::genCode(CodeGeneratorBase& generator) const
{
return subExpr->genCode(generator);
}
json CallExpr::toJson() const
{
json res = {
{"kind", "CallExpr"},
{"inner", json::array({function->toJson()})}
};
for (const auto& u : paras)
{
res["inner"].emplace_back(u->toJson());
}
return res;
}
Value* AST::CallExpr::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json AST::UnaryOperator::toJson() const
{
json res = {
{"kind", "UnaryOperator"},
{"opcode", getUnaryOpcode(op)}
};
res["inner"] = json::array({ body->toJson()});
return res;
}
Value* AST::UnaryOperator::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json AST::ImplicitCastExpr::toJson() const
{
json res = {
{"kind", "ImplicitCastExpr"},
{"valueCategory", "rvalue"},
{"castKind", castKind}
};
res["inner"] = json::array({ subExpr->toJson() });
return res;
}
Value* AST::ImplicitCastExpr::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}