• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vla5924-practice / compiler-project / 13884489619

16 Mar 2025 02:44PM UTC coverage: 77.883% (+1.2%) from 76.638%
13884489619

Pull #212

github

web-flow
Merge 37571da47 into 9f356f9ca
Pull Request #212: Implement fold control flow optimization

25 of 37 new or added lines in 1 file covered. (67.57%)

323 existing lines in 11 files now uncovered.

4437 of 5697 relevant lines covered (77.88%)

250.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.92
/compiler/lib/ast/node.cpp
1
#include "node.hpp"
2

3
#include <cstddef>
4
#include <cstdint>
5
#include <iostream>
6
#include <iterator>
7
#include <sstream>
8
#include <string>
9

10
#include "node_type.hpp"
11
#include "types.hpp"
12
#include "variables_table.hpp"
13

14
using namespace ast;
15

16
namespace {
17

18
const char *binaryOperationToString(BinaryOperation binOp) {
162✔
19
    switch (binOp) {
162✔
20
    case BinaryOperation::Add:
37✔
21
        return "Add";
37✔
22
    case BinaryOperation::And:
4✔
23
        return "And";
4✔
24
    case BinaryOperation::Assign:
75✔
25
        return "Assign";
75✔
26
    case BinaryOperation::Div:
4✔
27
        return "Div";
4✔
28
    case BinaryOperation::Equal:
8✔
29
        return "Equal";
8✔
30
    case BinaryOperation::FAdd:
10✔
31
        return "FAdd";
10✔
32
    case BinaryOperation::FDiv:
×
UNCOV
33
        return "FDiv";
×
UNCOV
34
    case BinaryOperation::FMult:
×
35
        return "FMult";
×
36
    case BinaryOperation::FSub:
×
37
        return "FSub";
×
38
    case BinaryOperation::Greater:
1✔
39
        return "Greater";
1✔
40
    case BinaryOperation::GreaterEqual:
×
UNCOV
41
        return "GreaterEqual";
×
UNCOV
42
    case BinaryOperation::Less:
×
43
        return "Less";
×
44
    case BinaryOperation::LessEqual:
×
UNCOV
45
        return "LessEqual";
×
46
    case BinaryOperation::Mult:
16✔
47
        return "Mult";
16✔
UNCOV
48
    case BinaryOperation::NotEqual:
×
UNCOV
49
        return "NotEqual";
×
50
    case BinaryOperation::Or:
2✔
51
        return "Or";
2✔
52
    case BinaryOperation::Sub:
1✔
53
        return "Sub";
1✔
54
    case BinaryOperation::FGreater:
3✔
55
        return "FGreater";
3✔
56
    case BinaryOperation::FGreaterEqual:
×
57
        return "FGreaterEqual";
×
58
    case BinaryOperation::FLess:
1✔
59
        return "FLess";
1✔
60
    case BinaryOperation::FLessEqual:
×
61
        return "FLessEqual";
×
62
    case BinaryOperation::FNotEqual:
×
63
        return "FNotEqual";
×
64
    case BinaryOperation::FOr:
×
UNCOV
65
        return "FOr";
×
UNCOV
66
    case BinaryOperation::Unknown:
×
UNCOV
67
        return "Unknown";
×
UNCOV
68
    default:
×
UNCOV
69
        return "";
×
70
    }
71
}
72

73
const char *unaryOperationToString(UnaryOperation unOp) {
43✔
74
    switch (unOp) {
43✔
75
    case UnaryOperation::Not:
9✔
76
        return "Not";
9✔
77
    case UnaryOperation::Negative:
34✔
78
        return "Negative";
34✔
UNCOV
79
    case UnaryOperation::Unknown:
×
UNCOV
80
        return "Unknown";
×
81
    }
UNCOV
82
    return "";
×
83
}
84

85
const char *typeIdToString(TypeId typeId) {
458✔
86
    switch (typeId) {
458✔
87
    case IntType:
213✔
88
        return "IntType";
213✔
89
    case FloatType:
136✔
90
        return "FloatType";
136✔
91
    case BoolType:
4✔
92
        return "BoolType";
4✔
UNCOV
93
    case StrType:
×
UNCOV
94
        return "StrType";
×
95
    case ListType:
6✔
96
        return "ListType";
6✔
97
    case NoneType:
99✔
98
        return "NoneType";
99✔
99
    }
UNCOV
100
    return "";
×
101
}
102

103
void dumpVariablesTable(std::ostream &stream, const VariablesTable &table) {
78✔
104
    for (const auto &[name, variable] : table)
170✔
105
        stream << " " << name << ":" << typeIdToString(variable.type);
92✔
106
}
78✔
107

108
} // namespace
109

110
Node::Node(const NodeType &type, const Ptr &parent) : type(type), parent(parent) {
3,591✔
111
}
3,591✔
112

113
Node::Node(int64_t intNum, const Ptr &parent) : type(NodeType::IntegerLiteralValue), value(intNum), parent(parent) {
3✔
114
}
3✔
115

116
Node::Node(double fpNum, const Ptr &parent) : type(NodeType::FloatingPointLiteralValue), value(fpNum), parent(parent) {
1✔
117
}
1✔
118

119
Node::Node(const NodeType &type, const std::string &str, const Ptr &parent) : type(type), value(str), parent(parent) {
1✔
120
}
1✔
121

122
Node::Node(TypeId typeId, const Ptr &parent) : type(NodeType::TypeName), value(typeId), parent(parent) {
41✔
123
}
41✔
124

125
Node::Node(BinaryOperation binOp, const Ptr &parent) : type(NodeType::BinaryOperation), value(binOp), parent(parent) {
5✔
126
}
5✔
127

128
Node::Node(UnaryOperation unOp, const Ptr &parent) : type(NodeType::UnaryOperation), value(unOp), parent(parent) {
1✔
129
}
1✔
130

131
const int64_t &Node::intNum() const {
278✔
132
    return std::get<int64_t>(value);
278✔
133
}
134

135
const double &Node::fpNum() const {
73✔
136
    return std::get<double>(value);
73✔
137
}
138

139
const bool &Node::boolean() const {
4✔
140
    return std::get<bool>(value);
4✔
141
}
142

143
const std::string &Node::str() const {
1,520✔
144
    return std::get<std::string>(value);
1,520✔
145
}
146

147
const TypeId &Node::typeId() const {
1,124✔
148
    return std::get<TypeId>(value);
1,124✔
149
}
150

151
const BinaryOperation &Node::binOp() const {
537✔
152
    return std::get<BinaryOperation>(value);
537✔
153
}
154

155
const UnaryOperation &Node::unOp() const {
44✔
156
    return std::get<UnaryOperation>(value);
44✔
157
}
158

159
const VariablesTable &Node::variables() const {
78✔
160
    return std::get<VariablesTable>(value);
78✔
161
}
162

163
VariablesTable &Node::variables() {
264✔
164
    return std::get<VariablesTable>(value);
264✔
165
}
166

167
bool Node::operator==(const Node &other) const {
8✔
168
    if (numChildren() != other.numChildren())
8✔
UNCOV
169
        return false;
×
170
    if (numChildren() > 0) {
8✔
171
        for (auto i = children.begin(), j = other.children.begin(); i != children.end(); i++, j++)
6✔
172
            if (**i != **j)
4✔
173
                return false;
2✔
174
    }
175
    return type == other.type && value == other.value;
6✔
176
}
177

178
bool Node::operator!=(const Node &other) const {
4✔
179
    return !(*this == other);
4✔
180
}
181

182
Node::Ptr &Node::firstChild() {
909✔
183
    return children.front();
909✔
184
}
185

186
const Node::Ptr &Node::firstChild() const {
9✔
187
    return children.front();
9✔
188
}
189

190
Node::Ptr &Node::secondChild() {
165✔
191
    return *std::next(children.begin());
165✔
192
}
193

UNCOV
194
const Node::Ptr &Node::secondChild() const {
×
UNCOV
195
    return *std::next(children.begin());
×
196
}
197

198
Node::Ptr &Node::lastChild() {
231✔
199
    return children.back();
231✔
200
}
201

UNCOV
202
const Node::Ptr &Node::lastChild() const {
×
203
    return children.back();
×
204
}
205

206
size_t Node::numChildren() const {
1,797✔
207
    return children.size();
1,797✔
208
}
209

210
void Node::dump(std::ostream &stream, int depth) const {
2,161✔
211
    for (int i = 0; i < depth; i++)
11,688✔
212
        stream << "  ";
9,527✔
213
    // stream << "0x" << this << " ";
214
    switch (type) {
2,161✔
215
    case NodeType::BinaryOperation:
162✔
216
        stream << "BinaryOperation: " << binaryOperationToString(binOp()) << "\n";
162✔
217
        break;
162✔
218
    case NodeType::BooleanLiteralValue:
4✔
219
        stream << "BooleanLiteralValue: " << (boolean() ? "True" : "False") << "\n";
4✔
220
        break;
4✔
221
    case NodeType::BranchRoot:
137✔
222
        stream << "BranchRoot";
137✔
223
        if (std::holds_alternative<VariablesTable>(value)) {
137✔
224
            stream << ':';
78✔
225
            dumpVariablesTable(stream, variables());
78✔
226
        }
227
        stream << '\n';
137✔
228
        break;
137✔
229
    case NodeType::ElifStatement:
2✔
230
        stream << "ElifStatement\n";
2✔
231
        break;
2✔
232
    case NodeType::ElseStatement:
1✔
233
        stream << "ElseStatement\n";
1✔
234
        break;
1✔
235
    case NodeType::Expression:
297✔
236
        stream << "Expression";
297✔
237
        if (std::holds_alternative<TypeId>(value)) {
297✔
238
            stream << ": " << typeIdToString(typeId());
101✔
239
        }
240
        stream << '\n';
297✔
241
        break;
297✔
242
    case NodeType::FloatingPointLiteralValue:
33✔
243
        stream << "FloatingPointLiteralValue: " << fpNum() << "\n";
33✔
244
        break;
33✔
245
    case NodeType::FunctionArgument:
25✔
246
        stream << "FunctionArgument\n";
25✔
247
        break;
25✔
248
    case NodeType::FunctionArguments:
133✔
249
        stream << "FunctionArguments\n";
133✔
250
        break;
133✔
251
    case NodeType::FunctionCall:
38✔
252
        stream << "FunctionCall\n";
38✔
253
        break;
38✔
254
    case NodeType::FunctionDefinition:
104✔
255
        stream << "FunctionDefinition\n";
104✔
256
        break;
104✔
257
    case NodeType::FunctionName:
142✔
258
        stream << "FunctionName: " << str() << "\n";
142✔
259
        break;
142✔
260
    case NodeType::FunctionReturnType:
105✔
261
        if (typeId() >= BuiltInTypesCount)
105✔
UNCOV
262
            stream << "FunctionReturnType: user-defined(" << typeId() << ")\n";
×
263
        else
264
            stream << "FunctionReturnType: " << typeIdToString(typeId()) << "\n";
105✔
265
        break;
105✔
266
    case NodeType::IfStatement:
11✔
267
        stream << "IfStatement\n";
11✔
268
        break;
11✔
269
    case NodeType::IntegerLiteralValue:
148✔
270
        stream << "IntegerLiteralValue: " << intNum() << "\n";
148✔
271
        break;
148✔
272
    case NodeType::ProgramRoot:
91✔
273
        stream << "ProgramRoot\n";
91✔
274
        break;
91✔
275
    case NodeType::ReturnStatement:
15✔
276
        stream << "ReturnStatement\n";
15✔
277
        break;
15✔
UNCOV
278
    case NodeType::StringLiteralValue:
×
UNCOV
279
        stream << "StringLiteralValue: " << str() << "\n";
×
UNCOV
280
        break;
×
281
    case NodeType::TypeConversion:
26✔
282
        stream << "TypeConversion\n";
26✔
283
        break;
26✔
284
    case NodeType::TypeName:
160✔
285
        if (typeId() >= BuiltInTypesCount)
160✔
UNCOV
286
            stream << "TypeName: user-defined(" << typeId() << ")\n";
×
287
        else
288
            stream << "TypeName: " << typeIdToString(typeId()) << "\n";
160✔
289
        break;
160✔
290
    case NodeType::UnaryOperation:
43✔
291
        stream << "UnaryOperation: " << unaryOperationToString(unOp()) << "\n";
43✔
292
        break;
43✔
293
    case NodeType::VariableDeclaration:
103✔
294
        stream << "VariableDeclaration\n";
103✔
295
        break;
103✔
296
    case NodeType::VariableName:
327✔
297
        stream << "VariableName: " << str() << "\n";
327✔
298
        break;
327✔
299
    case NodeType::WhileStatement:
3✔
300
        stream << "WhileStatement\n";
3✔
301
        break;
3✔
302
    case NodeType::ListStatement:
3✔
303
        stream << "ListStatement\n";
3✔
304
        break;
3✔
305
    case NodeType::ListAccessor:
19✔
306
        stream << "ListAccessor\n";
19✔
307
        break;
19✔
UNCOV
308
    case NodeType::ListDynamicSize:
×
UNCOV
309
        stream << "ListDynamicSize\n";
×
UNCOV
310
        break;
×
311
    case NodeType::ForStatement:
8✔
312
        stream << "ForStatement\n";
8✔
313
        break;
8✔
314
    case NodeType::ForIterable:
8✔
315
        stream << "ForIterable\n";
8✔
316
        break;
8✔
317
    case NodeType::ForTargets:
8✔
318
        stream << "ForTargets\n";
8✔
319
        break;
8✔
320
    case NodeType::BreakStatement:
1✔
321
        stream << "BreakStatement\n";
1✔
322
        break;
1✔
323
    case NodeType::ContinueStatement:
1✔
324
        stream << "ContinueStatement\n";
1✔
325
        break;
1✔
326
    case NodeType::PassStatement:
3✔
327
        stream << "PassStatement\n";
3✔
328
        break;
3✔
UNCOV
329
    default:
×
UNCOV
330
        stream << "Unknown\n";
×
331
    }
332
    for (const auto &child : children)
4,230✔
333
        child->dump(stream, depth + 1);
2,069✔
334
}
2,161✔
335

336
std::string Node::dump(int depth) const {
3✔
337
    std::stringstream str;
3✔
338
    dump(str, depth);
3✔
339
    return str.str();
6✔
340
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc