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

vla5924-practice / compiler-project / 13883569707

16 Mar 2025 12:54PM UTC coverage: 77.233% (-0.04%) from 77.273%
13883569707

Pull #223

github

web-flow
Merge 28bde1312 into 21e8f12a8
Pull Request #223: Refactor ast::Node (add numChildren method, use in64_t)

79 of 85 new or added lines in 7 files covered. (92.94%)

1 existing line in 1 file now uncovered.

4366 of 5653 relevant lines covered (77.23%)

250.61 hits per line

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

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

3
#include <iostream>
4
#include <sstream>
5

6
#include "node_type.hpp"
7
#include "types.hpp"
8

9
using namespace ast;
10

11
namespace {
12

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

68
const char *unaryOperationToString(UnaryOperation unOp) {
43✔
69
    switch (unOp) {
43✔
70
    case UnaryOperation::Not:
9✔
71
        return "Not";
9✔
72
    case UnaryOperation::Negative:
34✔
73
        return "Negative";
34✔
74
    case UnaryOperation::Unknown:
×
75
        return "Unknown";
×
76
    }
77
    return "";
×
78
}
79

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

98
void dumpVariablesTable(std::ostream &stream, const VariablesTable &table) {
78✔
99
    for (const auto &[name, variable] : table)
170✔
100
        stream << " " << name << ":" << typeIdToString(variable.type);
92✔
101
}
78✔
102

103
} // namespace
104

105
Node::Node(const NodeType &type, const Ptr &parent) : type(type), parent(parent){};
3,591✔
106

107
Node::Node(int64_t intNum, const Ptr &parent) : type(NodeType::IntegerLiteralValue), value(intNum), parent(parent){};
3✔
108

109
Node::Node(double fpNum, const Ptr &parent) : type(NodeType::FloatingPointLiteralValue), value(fpNum), parent(parent){};
1✔
110

111
Node::Node(const NodeType &type, const std::string &str, const Ptr &parent) : type(type), value(str), parent(parent){};
1✔
112

113
Node::Node(TypeId typeId, const Ptr &parent) : type(NodeType::TypeName), value(typeId), parent(parent){};
41✔
114

115
Node::Node(BinaryOperation binOp, const Ptr &parent) : type(NodeType::BinaryOperation), value(binOp), parent(parent){};
5✔
116

117
Node::Node(UnaryOperation unOp, const Ptr &parent) : type(NodeType::UnaryOperation), value(unOp), parent(parent){};
1✔
118

119
const int64_t &Node::intNum() const {
278✔
120
    return std::get<int64_t>(value);
278✔
121
}
122

123
const double &Node::fpNum() const {
73✔
124
    return std::get<double>(value);
73✔
125
}
126

127
const bool &Node::boolean() const {
4✔
128
    return std::get<bool>(value);
4✔
129
}
130

131
const std::string &Node::str() const {
1,520✔
132
    return std::get<std::string>(value);
1,520✔
133
}
134

135
const TypeId &Node::typeId() const {
1,124✔
136
    return std::get<TypeId>(value);
1,124✔
137
}
138

139
const BinaryOperation &Node::binOp() const {
537✔
140
    return std::get<BinaryOperation>(value);
537✔
141
}
142

143
const UnaryOperation &Node::unOp() const {
44✔
144
    return std::get<UnaryOperation>(value);
44✔
145
}
146

147
const VariablesTable &Node::variables() const {
78✔
148
    return std::get<VariablesTable>(value);
78✔
149
}
150

151
VariablesTable &Node::variables() {
264✔
152
    return std::get<VariablesTable>(value);
264✔
153
}
154

155
bool Node::operator==(const Node &other) const {
8✔
156
    if (numChildren() != other.numChildren())
8✔
NEW
157
        return false;
×
158
    if (numChildren() > 0) {
8✔
159
        for (auto i = children.begin(), j = other.children.begin(); i != children.end(); i++, j++)
6✔
160
            if (**i != **j)
4✔
161
                return false;
2✔
162
    }
163
    return type == other.type && value == other.value;
6✔
164
}
165

166
bool Node::operator!=(const Node &other) const {
4✔
167
    return !(*this == other);
4✔
168
}
169

170
Node::Ptr &Node::firstChild() {
909✔
171
    return children.front();
909✔
172
}
173

174
const Node::Ptr &Node::firstChild() const {
9✔
175
    return children.front();
9✔
176
}
177

178
Node::Ptr &Node::secondChild() {
165✔
179
    return *std::next(children.begin());
165✔
180
}
181

NEW
182
const Node::Ptr &Node::secondChild() const {
×
NEW
183
    return *std::next(children.begin());
×
184
}
185

186
Node::Ptr &Node::lastChild() {
231✔
187
    return children.back();
231✔
188
}
189

NEW
190
const Node::Ptr &Node::lastChild() const {
×
NEW
191
    return children.back();
×
192
}
193

194
size_t Node::numChildren() const {
1,797✔
195
    return children.size();
1,797✔
196
}
197

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

324
std::string Node::dump(int depth) const {
3✔
325
    std::stringstream str;
3✔
326
    dump(str, depth);
3✔
327
    return str.str();
6✔
328
}
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

© 2026 Coveralls, Inc