-
Notifications
You must be signed in to change notification settings - Fork 2
/
Stmt.cpp
181 lines (159 loc) · 3.36 KB
/
Stmt.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
/**
* @file Stmt.cpp
* 描述Stmt及其子类转换为Json格式的方式
**/
#include "Stmt.h"
using namespace AST;
json IfStmt::toJson() const
{
json res = {
{"kind", "IfStmt"},
{"hasElse", !elseBody}
};
auto inner = json::array({ cond->toJson() , body->toJson() });
if (elseBody)
{
inner.emplace_back(elseBody->toJson());
}
res["inner"] = std::move(inner);;
return res;
}
Value* AST::IfStmt::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json DeclStmt::toJson() const
{
json res = {
{"kind", "DeclStmt"}
};
if (!decls.empty())
{
auto inner = json::array();
for (const auto& u : decls)
{
inner.emplace_back(u->toJson());
}
res["inner"] = std::move(inner);
}
return res;
}
Value* AST::DeclStmt::genCode(CodeGeneratorBase& generator) const
{
for (const auto& decl : decls)
{
decl->genCode(generator);
}
return nullptr;
}
json CompoundStmt::toJson() const
{
json res = {
{"kind", "CompoundStmt"},
};
if (!body.empty())
{
auto inner = json::array();
for (const auto& u : body)
{
inner.emplace_back(u->toJson());
}
res["inner"] = std::move(inner);
}
return res;
}
Value* AST::CompoundStmt::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json FunctionDecl::toJson() const
{
std::string type = returnType + "(";
for (auto it = paras.begin(); it != paras.end(); ++it)
{
if (it != paras.begin())
{
type += ", ";
}
type += (*it)->type;
}
type += ")";
json res = {
{"kind", "FunctionDecl"},
{"name", name},
{"type", type}
};
if (body || !paras.empty())
{
auto inner = json::array();
if (!paras.empty())
{
for (const auto& u : paras)
{
inner.emplace_back(u->toJson());
}
}
if (body)
{
inner.emplace_back(body->toJson());
}
res["inner"] = std::move(inner);
}
return res;
}
Function* AST::FunctionDecl::genFunc(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
Value* AST::FunctionDecl::genCode(CodeGeneratorBase& generator) const
{
generator.gen(*this);
return nullptr;
}
json ReturnStmt::toJson() const
{
json res = {
{"kind", "ReturnStmt"}
};
if (returnValue)
{
res["inner"] = json::array({ returnValue->toJson() });
}
return res;
}
Value* AST::ReturnStmt::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json ValueStmt::toJson() const
{
return {
{"kind", "ValueStmt"},
{"inner", json::array({expr->toJson()})}
};
}
Value* AST::ValueStmt::genCode(CodeGeneratorBase& generator) const
{
return expr->genCode(generator);
}
json WhileStmt::toJson() const
{
return {
{"kind", "WhileStmt"},
{"inner", json::array({cond->toJson(), body->toJson()})}
};
}
Value* AST::WhileStmt::genCode(CodeGeneratorBase& generator) const
{
return generator.gen(*this);
}
json NullStmt::toJson() const
{
return {
{ "kind", "NullStmt" }
};
}
Value* AST::NullStmt::genCode(CodeGeneratorBase& generator) const
{
return nullptr;
}