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

ArkScript-lang / Ark / 11445793250

21 Oct 2024 05:51PM UTC coverage: 76.877% (+0.4%) from 76.499%
11445793250

push

github

SuperFola
refactor(macro): removing useless constructor

5130 of 6673 relevant lines covered (76.88%)

9379.81 hits per line

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

95.35
/src/arkreactor/Compiler/AST/Node.cpp
1
#include <Ark/Compiler/Common.hpp>
2
#include <Ark/Compiler/AST/Node.hpp>
3

4
#include <Ark/Exceptions.hpp>
5

6
#include <fmt/core.h>
7

8
namespace Ark::internal
9
{
10
    Node::Node(const NodeType node_type, const std::string& value) :
14,488✔
11
        m_type(node_type), m_value(value)
7,244✔
12
    {}
7,244✔
13

14
    Node::Node(const NodeType node_type) :
28,410✔
15
        m_type(node_type)
28,410✔
16
    {
28,410✔
17
        if (m_type == NodeType::List || m_type == NodeType::Macro || m_type == NodeType::Field)
28,410✔
18
            m_value = std::vector<Node>();
28,322✔
19
    }
28,410✔
20

21
    Node::Node(double value) :
1,010✔
22
        m_type(NodeType::Number), m_value(value)
1,010✔
23
    {}
2,020✔
24

25
    Node::Node(const long value) :
36✔
26
        m_type(NodeType::Number), m_value(static_cast<double>(value))
36✔
27
    {}
72✔
28

29
    Node::Node(Keyword value) :
1,235✔
30
        m_type(NodeType::Keyword), m_value(value)
1,235✔
31
    {}
2,470✔
32

33
    const std::string& Node::string() const noexcept
178,730✔
34
    {
178,730✔
35
        return std::get<std::string>(m_value);
178,730✔
36
    }
37

38
    double Node::number() const noexcept
1,891✔
39
    {
1,891✔
40
        return std::get<double>(m_value);
1,891✔
41
    }
42

43
    Keyword Node::keyword() const noexcept
8,483✔
44
    {
8,483✔
45
        return std::get<Keyword>(m_value);
8,483✔
46
    }
47

48
    void Node::push_back(const Node& node) noexcept
12,481✔
49
    {
12,481✔
50
        list().push_back(node);
12,481✔
51
    }
12,481✔
52

53
    std::vector<Node>& Node::list() noexcept
297,453✔
54
    {
297,453✔
55
        return std::get<std::vector<Node>>(m_value);
297,453✔
56
    }
57

58
    const std::vector<Node>& Node::constList() const noexcept
193,950✔
59
    {
193,950✔
60
        return std::get<std::vector<Node>>(m_value);
193,950✔
61
    }
62

63
    NodeType Node::nodeType() const noexcept
452,535✔
64
    {
452,535✔
65
        return m_type;
452,535✔
66
    }
67

68
    bool Node::isListLike() const noexcept
6,652✔
69
    {
6,652✔
70
        return m_type == NodeType::List || m_type == NodeType::Macro;
6,652✔
71
    }
72

73
    void Node::setNodeType(const NodeType type) noexcept
148✔
74
    {
148✔
75
        m_type = type;
148✔
76
    }
148✔
77

78
    void Node::setString(const std::string& value) noexcept
38✔
79
    {
38✔
80
        m_value = value;
38✔
81
    }
38✔
82

83
    void Node::setPos(const std::size_t line, const std::size_t col) noexcept
39,351✔
84
    {
39,351✔
85
        m_line = line;
39,351✔
86
        m_col = col;
39,351✔
87
    }
39,351✔
88

89
    void Node::setFilename(const std::string& filename) noexcept
39,351✔
90
    {
39,351✔
91
        m_filename = filename;
39,351✔
92
    }
39,351✔
93

94
    Node& Node::attachNearestCommentBefore(const std::string& comment)
10,795✔
95
    {
10,795✔
96
        m_comment = comment;
10,795✔
97
        return *this;
10,795✔
98
    }
99

100
    Node& Node::attachCommentAfter(const std::string& comment)
1,810✔
101
    {
1,810✔
102
        if (!m_after_comment.empty())
1,810✔
103
            m_after_comment += "\n";
2✔
104
        m_after_comment += comment;
1,810✔
105
        if (!m_after_comment.empty() && m_after_comment.back() == '\n')
1,810✔
106
            m_after_comment.pop_back();
55✔
107
        return *this;
1,810✔
108
    }
109

110
    std::size_t Node::line() const noexcept
45,863✔
111
    {
45,863✔
112
        return m_line;
45,863✔
113
    }
114

115
    std::size_t Node::col() const noexcept
39,778✔
116
    {
39,778✔
117
        return m_col;
39,778✔
118
    }
119

120
    const std::string& Node::filename() const noexcept
8,922✔
121
    {
8,922✔
122
        return m_filename;
8,922✔
123
    }
124

125
    const std::string& Node::comment() const noexcept
3,124✔
126
    {
3,124✔
127
        return m_comment;
3,124✔
128
    }
129

130
    const std::string& Node::commentAfter() const noexcept
2,048✔
131
    {
2,048✔
132
        return m_after_comment;
2,048✔
133
    }
134

135
    std::string Node::repr() const noexcept
1,919✔
136
    {
1,919✔
137
        std::string data;
1,919✔
138
        switch (m_type)
1,919✔
139
        {
809✔
140
            case NodeType::Symbol:
141
                data += string();
809✔
142
                break;
811✔
143

144
            case NodeType::Capture:
145
                data += "&" + string();
2✔
146
                break;
32✔
147

148
            case NodeType::Keyword:
149
                data += keywords[static_cast<std::size_t>(keyword())];
30✔
150
                break;
181✔
151

152
            case NodeType::String:
153
                data += "\"" + string() + "\"";
151✔
154
                break;
606✔
155

156
            case NodeType::Number:
157
                data += fmt::format("{}", number());
455✔
158
                break;
899✔
159

160
            case NodeType::List:
161
                data += "(";
444✔
162
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
1,675✔
163
                {
164
                    data += constList()[i].repr();
1,231✔
165
                    if (i < end - 1)
1,231✔
166
                        data += " ";
787✔
167
                }
1,231✔
168
                data += ")";
444✔
169
                break;
453✔
170

171
            case NodeType::Field:
172
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
29✔
173
                {
174
                    data += constList()[i].repr();
20✔
175
                    if (i < end - 1)
20✔
176
                        data += ".";
11✔
177
                }
20✔
178
                break;
25✔
179

180
            case NodeType::Macro:
181
                data += "($ ";
16✔
182
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
77✔
183
                {
184
                    data += constList()[i].repr();
61✔
185
                    if (i < end - 1)
61✔
186
                        data += " ";
45✔
187
                }
61✔
188
                data += ")";
16✔
189
                break;
19✔
190

191
            case NodeType::Spread:
192
                data += "..." + string();
3✔
193
                break;
3✔
194

195
            case NodeType::Unused:
37,935✔
196
                break;
×
197
        }
1,919✔
198
        return data;
1,919✔
199
    }
1,919✔
200

201
    std::ostream& Node::debugPrint(std::ostream& os) const noexcept
469✔
202
    {
469✔
203
        switch (m_type)
469✔
204
        {
143✔
205
            case NodeType::Symbol:
206
                os << "Symbol:" << string();
143✔
207
                break;
149✔
208

209
            case NodeType::Capture:
210
                os << "Capture:" << string();
6✔
211
                break;
84✔
212

213
            case NodeType::Keyword:
214
                os << "Keyword:";
78✔
215
                switch (keyword())
78✔
216
                {
14✔
217
                    case Keyword::Fun: os << "Fun"; break;
32✔
218
                    case Keyword::Let: os << "Let"; break;
21✔
219
                    case Keyword::Mut: os << "Mut"; break;
4✔
220
                    case Keyword::Set: os << "Set"; break;
16✔
221
                    case Keyword::If: os << "If"; break;
21✔
222
                    case Keyword::While: os << "While"; break;
15✔
223
                    case Keyword::Begin: os << "Begin"; break;
17✔
224
                    case Keyword::Import: os << "Import"; break;
12✔
225
                    case Keyword::Del: os << "Del"; break;
4✔
226
                }
78✔
227
                break;
86✔
228

229
            case NodeType::String:
230
                os << "String:" << string();
8✔
231
                break;
83✔
232

233
            case NodeType::Number:
234
                os << "Number:" << number();
75✔
235
                break;
215✔
236

237
            case NodeType::List:
238
                os << "( ";
140✔
239
                for (const auto& i : constList())
482✔
240
                    i.debugPrint(os) << " ";
342✔
241
                os << ")";
140✔
242
                break;
149✔
243

244
            case NodeType::Field:
245
                os << "( Field ";
9✔
246
                for (const auto& i : constList())
30✔
247
                    i.debugPrint(os) << " ";
21✔
248
                os << ")";
9✔
249
                break;
17✔
250

251
            case NodeType::Macro:
252
                os << "( Macro ";
8✔
253
                for (const auto& i : constList())
31✔
254
                    i.debugPrint(os) << " ";
23✔
255
                os << ")";
8✔
256
                break;
10✔
257

258
            case NodeType::Spread:
259
                os << "Spread:" << string();
2✔
260
                break;
2✔
261

262
            case NodeType::Unused:
263
                os << "Unused:" << string();
×
264
                break;
×
265
        }
469✔
266
        return os;
469✔
267
    }
268

269
    const Node& getTrueNode()
29✔
270
    {
29✔
271
        static const Node TrueNode(NodeType::Symbol, "true");
29✔
272
        return TrueNode;
29✔
273
    }
×
274

275
    const Node& getFalseNode()
19✔
276
    {
19✔
277
        static const Node FalseNode(NodeType::Symbol, "false");
19✔
278
        return FalseNode;
19✔
279
    }
×
280

281
    const Node& getNilNode()
21✔
282
    {
21✔
283
        static const Node NilNode(NodeType::Symbol, "nil");
21✔
284
        return NilNode;
21✔
285
    }
×
286

287
    const Node& getListNode()
388✔
288
    {
388✔
289
        static const Node ListNode(NodeType::Symbol, "list");
388✔
290
        return ListNode;
388✔
291
    }
×
292

293
    bool operator==(const Node& A, const Node& B)
99✔
294
    {
99✔
295
        if (A.m_type != B.m_type)  // should have the same types
99✔
296
            return false;
9✔
297

298
        if (A.m_type != NodeType::List)
90✔
299
            return A.m_value == B.m_value;
90✔
300
        return false;
×
301
    }
99✔
302

303
    bool operator<(const Node& A, const Node& B)
21✔
304
    {
21✔
305
        if (A.nodeType() != B.nodeType())
21✔
306
            return (static_cast<int>(A.nodeType()) - static_cast<int>(B.nodeType())) < 0;
×
307

308
        switch (A.nodeType())
21✔
309
        {
21✔
310
            case NodeType::Number:
311
                [[fallthrough]];
312
            case NodeType::Symbol:
313
                [[fallthrough]];
314
            case NodeType::String:
315
                return A.m_value < B.m_value;
21✔
316

317
            default:
318
                return false;
×
319
        }
320
    }
21✔
321
}
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