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

ArkScript-lang / Ark / 11987163431

23 Nov 2024 12:31PM UTC coverage: 77.19% (-0.1%) from 77.285%
11987163431

push

github

SuperFola
refactor(macro processor): removing the need for a double apply macro + recursive apply in MacroProcessor::processNode

7 of 7 new or added lines in 4 files covered. (100.0%)

71 existing lines in 6 files now uncovered.

5191 of 6725 relevant lines covered (77.19%)

9553.21 hits per line

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

95.89
/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) :
15,342✔
11
        m_type(node_type), m_value(value)
7,671✔
12
    {}
7,671✔
13

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

21
    Node::Node(double value) :
1,064✔
22
        m_type(NodeType::Number), m_value(value)
1,064✔
23
    {}
2,128✔
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,344✔
30
        m_type(NodeType::Keyword), m_value(value)
1,344✔
31
    {}
2,688✔
32

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

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

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

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

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

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

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

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

73
    void Node::updateValueAndType(const Node& source) noexcept
8,855✔
74
    {
8,855✔
75
        m_type = source.m_type;
8,855✔
76
        m_value = source.m_value;
8,855✔
77
    }
8,855✔
78

79
    void Node::setNodeType(const NodeType type) noexcept
148✔
80
    {
148✔
81
        m_type = type;
148✔
82
    }
148✔
83

84
    void Node::setString(const std::string& value) noexcept
38✔
85
    {
38✔
86
        m_value = value;
38✔
87
    }
38✔
88

89
    void Node::setPos(const std::size_t line, const std::size_t col) noexcept
32,401✔
90
    {
32,401✔
91
        m_line = line;
32,401✔
92
        m_col = col;
32,401✔
93
    }
32,401✔
94

95
    void Node::setFilename(const std::string& filename) noexcept
32,401✔
96
    {
32,401✔
97
        m_filename = filename;
32,401✔
98
    }
32,401✔
99

100
    Node& Node::attachNearestCommentBefore(const std::string& comment)
11,506✔
101
    {
11,506✔
102
        m_comment = comment;
11,506✔
103
        return *this;
11,506✔
104
    }
105

106
    Node& Node::attachCommentAfter(const std::string& comment)
1,921✔
107
    {
1,921✔
108
        if (!m_after_comment.empty())
1,921✔
109
            m_after_comment += "\n";
2✔
110
        m_after_comment += comment;
1,921✔
111
        if (!m_after_comment.empty() && m_after_comment.back() == '\n')
1,921✔
112
            m_after_comment.pop_back();
61✔
113
        return *this;
1,921✔
114
    }
115

116
    std::size_t Node::line() const noexcept
39,314✔
117
    {
39,314✔
118
        return m_line;
39,314✔
119
    }
120

121
    std::size_t Node::col() const noexcept
32,828✔
122
    {
32,828✔
123
        return m_col;
32,828✔
124
    }
125

126
    const std::string& Node::filename() const noexcept
66✔
127
    {
66✔
128
        return m_filename;
66✔
129
    }
130

131
    const std::string& Node::comment() const noexcept
3,235✔
132
    {
3,235✔
133
        return m_comment;
3,235✔
134
    }
135

136
    const std::string& Node::commentAfter() const noexcept
2,048✔
137
    {
2,048✔
138
        return m_after_comment;
2,048✔
139
    }
140

141
    std::string Node::repr() const noexcept
1,953✔
142
    {
1,953✔
143
        std::string data;
1,953✔
144
        switch (m_type)
1,953✔
145
        {
829✔
146
            case NodeType::Symbol:
147
                data += string();
829✔
148
                break;
831✔
149

150
            case NodeType::Capture:
151
                data += "&" + string();
2✔
152
                break;
32✔
153

154
            case NodeType::Keyword:
155
                data += keywords[static_cast<std::size_t>(keyword())];
30✔
156
                break;
181✔
157

158
            case NodeType::String:
159
                data += "\"" + string() + "\"";
151✔
160
                break;
612✔
161

162
            case NodeType::Number:
163
                data += fmt::format("{}", number());
461✔
164
                break;
913✔
165

166
            case NodeType::List:
167
                data += "(";
452✔
168
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
1,707✔
169
                {
170
                    data += constList()[i].repr();
1,255✔
171
                    if (i < end - 1)
1,255✔
172
                        data += " ";
803✔
173
                }
1,255✔
174
                data += ")";
452✔
175
                break;
461✔
176

177
            case NodeType::Field:
178
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
29✔
179
                {
180
                    data += constList()[i].repr();
20✔
181
                    if (i < end - 1)
20✔
182
                        data += ".";
11✔
183
                }
20✔
184
                break;
25✔
185

186
            case NodeType::Macro:
187
                data += "($ ";
16✔
188
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
77✔
189
                {
190
                    data += constList()[i].repr();
61✔
191
                    if (i < end - 1)
61✔
192
                        data += " ";
45✔
193
                }
61✔
194
                data += ")";
16✔
195
                break;
19✔
196

197
            case NodeType::Spread:
198
                data += "..." + string();
3✔
199
                break;
3✔
200

201
            case NodeType::Unused:
202
                break;
40,317✔
203
        }
1,953✔
204
        return data;
1,953✔
205
    }
1,953✔
206

207
    std::ostream& Node::debugPrint(std::ostream& os) const noexcept
469✔
208
    {
469✔
209
        switch (m_type)
469✔
210
        {
143✔
211
            case NodeType::Symbol:
212
                os << "Symbol:" << string();
143✔
213
                break;
149✔
214

215
            case NodeType::Capture:
216
                os << "Capture:" << string();
6✔
217
                break;
84✔
218

219
            case NodeType::Keyword:
220
                os << "Keyword:";
78✔
221
                switch (keyword())
78✔
222
                {
14✔
223
                    case Keyword::Fun: os << "Fun"; break;
32✔
224
                    case Keyword::Let: os << "Let"; break;
21✔
225
                    case Keyword::Mut: os << "Mut"; break;
4✔
226
                    case Keyword::Set: os << "Set"; break;
16✔
227
                    case Keyword::If: os << "If"; break;
21✔
228
                    case Keyword::While: os << "While"; break;
15✔
229
                    case Keyword::Begin: os << "Begin"; break;
17✔
230
                    case Keyword::Import: os << "Import"; break;
12✔
231
                    case Keyword::Del: os << "Del"; break;
4✔
232
                }
78✔
233
                break;
86✔
234

235
            case NodeType::String:
236
                os << "String:" << string();
8✔
237
                break;
83✔
238

239
            case NodeType::Number:
240
                os << "Number:" << number();
75✔
241
                break;
215✔
242

243
            case NodeType::List:
244
                os << "( ";
140✔
245
                for (const auto& i : constList())
482✔
246
                    i.debugPrint(os) << " ";
342✔
247
                os << ")";
140✔
248
                break;
149✔
249

250
            case NodeType::Field:
251
                os << "( Field ";
9✔
252
                for (const auto& i : constList())
30✔
253
                    i.debugPrint(os) << " ";
21✔
254
                os << ")";
9✔
255
                break;
17✔
256

257
            case NodeType::Macro:
258
                os << "( Macro ";
8✔
259
                for (const auto& i : constList())
31✔
260
                    i.debugPrint(os) << " ";
23✔
261
                os << ")";
8✔
262
                break;
10✔
263

264
            case NodeType::Spread:
265
                os << "Spread:" << string();
2✔
266
                break;
2✔
267

268
            case NodeType::Unused:
UNCOV
269
                os << "Unused:" << string();
×
UNCOV
270
                break;
×
271
        }
469✔
272
        return os;
469✔
273
    }
274

275
    const Node& getTrueNode()
29✔
276
    {
29✔
277
        static const Node TrueNode(NodeType::Symbol, "true");
29✔
278
        return TrueNode;
29✔
279
    }
×
280

281
    const Node& getFalseNode()
19✔
282
    {
19✔
283
        static const Node FalseNode(NodeType::Symbol, "false");
19✔
284
        return FalseNode;
19✔
285
    }
×
286

287
    const Node& getNilNode()
21✔
288
    {
21✔
289
        static const Node NilNode(NodeType::Symbol, "nil");
21✔
290
        return NilNode;
21✔
291
    }
×
292

293
    const Node& getListNode()
388✔
294
    {
388✔
295
        static const Node ListNode(NodeType::Symbol, "list");
388✔
296
        return ListNode;
388✔
UNCOV
297
    }
×
298

299
    bool operator==(const Node& A, const Node& B)
99✔
300
    {
99✔
301
        if (A.m_type != B.m_type)  // should have the same types
99✔
302
            return false;
9✔
303

304
        if (A.m_type != NodeType::List)
90✔
305
            return A.m_value == B.m_value;
90✔
306
        return false;
×
307
    }
99✔
308

309
    bool operator<(const Node& A, const Node& B)
21✔
310
    {
21✔
311
        if (A.nodeType() != B.nodeType())
21✔
UNCOV
312
            return (static_cast<int>(A.nodeType()) - static_cast<int>(B.nodeType())) < 0;
×
313

314
        switch (A.nodeType())
21✔
315
        {
21✔
316
            case NodeType::Number:
317
                [[fallthrough]];
318
            case NodeType::Symbol:
319
                [[fallthrough]];
320
            case NodeType::String:
321
                return A.m_value < B.m_value;
21✔
322

323
            default:
UNCOV
324
                return false;
×
325
        }
326
    }
21✔
327
}
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