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

vla5924-practice / compiler-project / 13883629245

16 Mar 2025 01:01PM UTC coverage: 77.233% (-0.04%) from 77.273%
13883629245

Pull #223

github

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

89 of 98 new or added lines in 7 files covered. (90.82%)

10 existing lines in 2 files 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 <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:
×
33
        return "FDiv";
×
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:
×
41
        return "GreaterEqual";
×
42
    case BinaryOperation::Less:
×
43
        return "Less";
×
44
    case BinaryOperation::LessEqual:
×
45
        return "LessEqual";
×
46
    case BinaryOperation::Mult:
16✔
47
        return "Mult";
16✔
48
    case BinaryOperation::NotEqual:
×
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:
×
65
        return "FOr";
×
66
    case BinaryOperation::Unknown:
×
67
        return "Unknown";
×
68
    default:
×
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✔
79
    case UnaryOperation::Unknown:
×
80
        return "Unknown";
×
81
    }
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✔
93
    case StrType:
×
94
        return "StrType";
×
95
    case ListType:
6✔
96
        return "ListType";
6✔
97
    case NoneType:
99✔
98
        return "NoneType";
99✔
99
    }
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

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

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

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

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

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

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

124
const int64_t &Node::intNum() const {
278✔
125
    return std::get<int64_t>(value);
278✔
126
}
127

128
const double &Node::fpNum() const {
73✔
129
    return std::get<double>(value);
73✔
130
}
131

132
const bool &Node::boolean() const {
4✔
133
    return std::get<bool>(value);
4✔
134
}
135

136
const std::string &Node::str() const {
1,520✔
137
    return std::get<std::string>(value);
1,520✔
138
}
139

140
const TypeId &Node::typeId() const {
1,124✔
141
    return std::get<TypeId>(value);
1,124✔
142
}
143

144
const BinaryOperation &Node::binOp() const {
537✔
145
    return std::get<BinaryOperation>(value);
537✔
146
}
147

148
const UnaryOperation &Node::unOp() const {
44✔
149
    return std::get<UnaryOperation>(value);
44✔
150
}
151

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

156
VariablesTable &Node::variables() {
264✔
157
    return std::get<VariablesTable>(value);
264✔
158
}
159

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

171
bool Node::operator!=(const Node &other) const {
4✔
172
    return !(*this == other);
4✔
173
}
174

175
Node::Ptr &Node::firstChild() {
909✔
176
    return children.front();
909✔
177
}
178

179
const Node::Ptr &Node::firstChild() const {
9✔
180
    return children.front();
9✔
181
}
182

183
Node::Ptr &Node::secondChild() {
165✔
184
    return *std::next(children.begin());
165✔
185
}
186

NEW
187
const Node::Ptr &Node::secondChild() const {
×
NEW
188
    return *std::next(children.begin());
×
189
}
190

191
Node::Ptr &Node::lastChild() {
231✔
192
    return children.back();
231✔
193
}
194

NEW
195
const Node::Ptr &Node::lastChild() const {
×
NEW
196
    return children.back();
×
197
}
198

199
size_t Node::numChildren() const {
1,797✔
200
    return children.size();
1,797✔
201
}
202

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

329
std::string Node::dump(int depth) const {
3✔
330
    std::stringstream str;
3✔
331
    dump(str, depth);
3✔
332
    return str.str();
6✔
333
}
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