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

ArkScript-lang / Ark / 22157739607

18 Feb 2026 09:10PM UTC coverage: 93.312% (-0.2%) from 93.464%
22157739607

Pull #641

github

web-flow
Merge 832fcd44e into 672fb743f
Pull Request #641: Feat/various improvements

138 of 167 new or added lines in 7 files covered. (82.63%)

3 existing lines in 2 files now uncovered.

9223 of 9884 relevant lines covered (93.31%)

265167.15 hits per line

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

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

4
#include <Ark/Error/Exceptions.hpp>
5

6
#include <cassert>
7
#include <fmt/core.h>
8

9
namespace Ark::internal
10
{
11
    Node::Node(const NodeType node_type, const std::string& value) :
224,702✔
12
        m_type(node_type), m_value(value), m_pos()
112,351✔
13
    {}
112,351✔
14

15
    Node::Node(const NodeType node_type) :
429,074✔
16
        m_type(node_type), m_pos()
858,148✔
17
    {
429,074✔
18
        if (m_type == NodeType::List || m_type == NodeType::Macro || m_type == NodeType::Field)
429,074✔
19
            m_value = std::vector<Node>();
427,656✔
20
    }
429,074✔
21

22
    Node::Node(double value) :
78,196✔
23
        m_type(NodeType::Number), m_value(value), m_pos()
156,392✔
24
    {}
156,392✔
25

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

30
    Node::Node(Keyword value) :
21,664✔
31
        m_type(NodeType::Keyword), m_value(value), m_pos()
43,328✔
32
    {}
43,328✔
33

34
    Node::Node(const Namespace& namespace_) :
284✔
35
        m_type(NodeType::Namespace), m_value(namespace_), m_pos()
142✔
36
    {}
142✔
37

38
    const std::string& Node::string() const noexcept
18,980,863✔
39
    {
18,980,863✔
40
        return std::get<std::string>(m_value);
18,980,863✔
41
    }
42

43
    double Node::number() const noexcept
14,646✔
44
    {
14,646✔
45
        return std::get<double>(m_value);
14,646✔
46
    }
47

48
    Keyword Node::keyword() const noexcept
230,928✔
49
    {
230,928✔
50
        return std::get<Keyword>(m_value);
230,928✔
51
    }
52

53
    Namespace& Node::arkNamespace() noexcept
482✔
54
    {
482✔
55
        return std::get<Namespace>(m_value);
482✔
56
    }
57

58
    const Namespace& Node::constArkNamespace() const noexcept
135✔
59
    {
135✔
60
        return std::get<Namespace>(m_value);
135✔
61
    }
62

63
    void Node::push_back(const Node& node) noexcept
261,784✔
64
    {
261,784✔
65
        list().push_back(node);
261,784✔
66
    }
261,784✔
67

68
    std::vector<Node>& Node::list() noexcept
1,931,141✔
69
    {
1,931,141✔
70
        return std::get<std::vector<Node>>(m_value);
1,931,141✔
71
    }
72

73
    const std::vector<Node>& Node::constList() const noexcept
3,462,754✔
74
    {
3,462,754✔
75
        return std::get<std::vector<Node>>(m_value);
3,462,754✔
76
    }
77

78
    NodeType Node::nodeType() const noexcept
6,987,120✔
79
    {
6,987,120✔
80
        return m_type;
6,987,120✔
81
    }
82

83
    bool Node::isListLike() const noexcept
27,087✔
84
    {
27,087✔
85
        return m_type == NodeType::List || m_type == NodeType::Macro;
27,087✔
86
    }
87

88
    bool Node::isFunction() const noexcept
12,337✔
89
    {
12,337✔
90
        return m_type == NodeType::List &&
21,051✔
91
            !constList().empty() &&
8,714✔
92
            constList()[0].nodeType() == NodeType::Keyword &&
12,136✔
93
            constList()[0].keyword() == Keyword::Fun;
3,422✔
94
    }
95

96
    const std::optional<std::string>& Node::getUnqualifiedName() const noexcept
909✔
97
    {
909✔
98
        return m_unqualified_name;
909✔
99
    }
100

101
    void Node::updateValueAndType(const Node& source) noexcept
50,477✔
102
    {
50,477✔
103
        m_type = source.m_type;
50,477✔
104
        m_value = source.m_value;
50,477✔
105
    }
50,477✔
106

107
    void Node::setNodeType(const NodeType type) noexcept
263✔
108
    {
263✔
109
        m_type = type;
263✔
110
    }
263✔
111

112
    void Node::setUnqualifiedName(const std::string& name) noexcept
227✔
113
    {
227✔
114
        m_unqualified_name = name;
227✔
115
    }
227✔
116

117
    void Node::setString(const std::string& value) noexcept
180,435✔
118
    {
180,435✔
119
        m_value = value;
180,435✔
120
    }
180,435✔
121

122
    void Node::setPositionFrom(const Node& source) noexcept
156✔
123
    {
156✔
124
        m_filename = source.m_filename;
156✔
125
        m_pos = source.m_pos;
156✔
126
    }
156✔
127

128
    Node& Node::attachNearestCommentBefore(const std::string& comment)
233,689✔
129
    {
233,689✔
130
        m_comment = comment;
233,689✔
131
        return *this;
233,689✔
132
    }
133

134
    Node& Node::attachCommentAfter(const std::string& comment)
68,283✔
135
    {
68,283✔
136
        if (!m_after_comment.empty())
68,283✔
137
            m_after_comment += "\n";
37✔
138
        m_after_comment += comment;
68,283✔
139
        if (!m_after_comment.empty() && m_after_comment.back() == '\n')
68,283✔
140
            m_after_comment.pop_back();
110✔
141
        return *this;
68,283✔
142
    }
143

144
    void Node::setAltSyntax(const bool toggle)
5,046✔
145
    {
5,046✔
146
        m_alt_syntax = toggle;
5,046✔
147
    }
5,046✔
148

149
    void Node::setFunctionKind(const bool anonymous)
3,360✔
150
    {
3,360✔
151
        m_is_anonymous_function = anonymous;
3,360✔
152
    }
3,360✔
153

154
    bool Node::isAnonymousFunction() const noexcept
7,517✔
155
    {
7,517✔
156
        return m_is_anonymous_function;
7,517✔
157
    }
158

159
    FileSpan Node::position() const noexcept
246,717✔
160
    {
246,717✔
161
        return m_pos;
246,717✔
162
    }
163

164
    const std::string& Node::filename() const noexcept
134,961✔
165
    {
134,961✔
166
        return m_filename;
134,961✔
167
    }
168

169
    const std::string& Node::comment() const noexcept
25,722✔
170
    {
25,722✔
171
        return m_comment;
25,722✔
172
    }
173

174
    const std::string& Node::commentAfter() const noexcept
4,024✔
175
    {
4,024✔
176
        return m_after_comment;
4,024✔
177
    }
178

179
    std::string Node::repr() const noexcept
28,891✔
180
    {
28,891✔
181
        std::string data;
28,891✔
182
        switch (m_type)
28,891✔
183
        {
23,862✔
184
            case NodeType::Symbol:
185
                data += string();
23,862✔
186
                break;
23,862✔
187

188
            case NodeType::MutArg:
189
                data += "(mut " + string() + ")";
×
190
                break;
×
191

192
            case NodeType::RefArg:
193
                data += "(ref " + string() + ")";
×
194
                break;
×
195

196
            case NodeType::Capture:
197
                data += "&" + string();
×
198
                break;
102✔
199

200
            case NodeType::Keyword:
201
                data += keywords[static_cast<std::size_t>(keyword())];
102✔
202
                break;
874✔
203

204
            case NodeType::String:
205
                data += "\"" + string() + "\"";
772✔
206
                break;
2,694✔
207

208
            case NodeType::Number:
209
                data += fmt::format("{}", number());
1,922✔
210
                break;
4,001✔
211

212
            case NodeType::List:
213
                if (m_alt_syntax)
2,079✔
214
                {
215
                    const auto first = constList().front();
605✔
216
                    char open = 0;
605✔
217
                    if (first.nodeType() == NodeType::Keyword && first.keyword() == Keyword::Begin)
605✔
218
                        open = '{';
5✔
219
                    else if (first.nodeType() == NodeType::Symbol && first.string() == "list")
600✔
220
                        open = '[';
600✔
221
                    else
222
                        assert(false && "Alt syntax nodes can only be begin or list");
×
223

224
                    data += open;
605✔
225
                    for (std::size_t i = 1, end = constList().size(); i < end; ++i)
1,804✔
226
                    {
227
                        data += constList()[i].repr();
1,199✔
228
                        if (i < end - 1)
1,199✔
229
                            data += " ";
698✔
230
                    }
1,199✔
231

232
                    if (open == '{')
605✔
233
                        data += "}";
5✔
234
                    else if (open == '[')
600✔
235
                        data += "]";
600✔
236
                }
605✔
237
                else
238
                {
239
                    data += "(";
1,474✔
240
                    for (std::size_t i = 0, end = constList().size(); i < end; ++i)
5,326✔
241
                    {
242
                        data += constList()[i].repr();
3,852✔
243
                        if (i < end - 1)
3,852✔
244
                            data += " ";
643,902✔
245
                    }
3,852✔
246
                    data += ")";
1,474✔
247
                }
248
                break;
2,216✔
249

250
            case NodeType::Field:
641,499✔
251
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
641,924✔
252
                {
253
                    data += constList()[i].repr();
288✔
254
                    if (i < end - 1)
288✔
255
                        data += ".";
151✔
256
                }
288✔
257
                break;
153✔
258

259
            case NodeType::Macro:
260
                data += "(macro ";
16✔
261
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
76✔
262
                {
263
                    data += constList()[i].repr();
60✔
264
                    if (i < end - 1)
60✔
265
                        data += " ";
44✔
266
                }
60✔
267
                data += ")";
16✔
268
                break;
17✔
269

270
            case NodeType::Spread:
271
                data += "..." + string();
1✔
272
                break;
1✔
273

274
            // namespace node should not have a representation as it is purely internal,
275
            // and it can't be exploited by macros (unless you try passing an import node
276
            // to a macro, which should not happen?)
277
            case NodeType::Namespace:
278
                data += constArkNamespace().ast->repr();
×
279
                break;
×
280

281
            case NodeType::Unused:
282
                break;
×
283
        }
28,891✔
284
        return data;
28,891✔
285
    }
28,891✔
286

287
    std::ostream& Node::debugPrint(std::ostream& os) const noexcept
537✔
288
    {
537✔
289
        switch (m_type)
537✔
290
        {
155✔
291
            case NodeType::Symbol:
292
                os << "Symbol:" << string();
155✔
293
                break;
161✔
294

295
            case NodeType::MutArg:
296
                os << "MutArg:" << string();
6✔
297
                break;
12✔
298

299
            case NodeType::RefArg:
300
                os << "RefArg:" << string();
6✔
301
                break;
16✔
302

303
            case NodeType::Capture:
304
                os << "Capture:" << string();
10✔
305
                break;
102✔
306

307
            case NodeType::Keyword:
308
                os << "Keyword:";
92✔
309
                switch (keyword())
92✔
310
                {
24✔
311
                    case Keyword::Fun: os << "Fun"; break;
44✔
312
                    case Keyword::Let: os << "Let"; break;
23✔
313
                    case Keyword::Mut: os << "Mut"; break;
4✔
314
                    case Keyword::Set: os << "Set"; break;
16✔
315
                    case Keyword::If: os << "If"; break;
21✔
316
                    case Keyword::While: os << "While"; break;
17✔
317
                    case Keyword::Begin: os << "Begin"; break;
19✔
318
                    case Keyword::Import: os << "Import"; break;
12✔
319
                    case Keyword::Del: os << "Del"; break;
4✔
320
                }
92✔
321
                break;
100✔
322

323
            case NodeType::String:
324
                os << "String:" << string();
8✔
325
                break;
85✔
326

327
            case NodeType::Number:
328
                os << "Number:" << number();
77✔
329
                break;
241✔
330

331
            case NodeType::List:
332
                os << "( ";
164✔
333
                for (const auto& i : constList())
564✔
334
                    i.debugPrint(os) << " ";
400✔
335
                os << ")";
164✔
336
                break;
173✔
337

338
            case NodeType::Field:
339
                os << "( Field ";
9✔
340
                for (const auto& i : constList())
30✔
341
                    i.debugPrint(os) << " ";
21✔
342
                os << ")";
9✔
343
                break;
17✔
344

345
            case NodeType::Macro:
346
                os << "( Macro ";
8✔
347
                for (const auto& i : constList())
31✔
348
                    i.debugPrint(os) << " ";
23✔
349
                os << ")";
8✔
350
                break;
10✔
351

352
            case NodeType::Spread:
353
                os << "Spread:" << string();
2✔
354
                break;
2✔
355

356
            case NodeType::Namespace:
357
            {
358
                const auto details = constArkNamespace();
×
359
                os << "( Namespace:" << details.name << " ";
×
360
                details.ast->debugPrint(os) << " )";
×
361
                break;
362
            }
×
363

364
            case NodeType::Unused:
365
                break;
×
366
        }
537✔
367
        return os;
537✔
368
    }
369

370
    const Node& getTrueNode()
61✔
371
    {
61✔
372
        static const Node TrueNode(NodeType::Symbol, "true");
61✔
373
        return TrueNode;
61✔
374
    }
×
375

376
    const Node& getFalseNode()
63✔
377
    {
63✔
378
        static const Node FalseNode(NodeType::Symbol, "false");
63✔
379
        return FalseNode;
63✔
380
    }
×
381

382
    const Node& getNilNode()
45✔
383
    {
45✔
384
        static const Node NilNode(NodeType::Symbol, "nil");
45✔
385
        return NilNode;
45✔
386
    }
×
387

388
    const Node& getListNode()
1,437✔
389
    {
1,437✔
390
        static const Node ListNode(NodeType::Symbol, "list");
1,437✔
391
        return ListNode;
1,437✔
392
    }
×
393

394
    bool operator==(const Node& A, const Node& B)
319✔
395
    {
319✔
396
        if (A.m_type != B.m_type)  // should have the same types
319✔
397
            return false;
5✔
398

399
        if (A.m_type != NodeType::List)
314✔
400
            return A.m_value == B.m_value;
314✔
401
        return false;
×
402
    }
319✔
403

404
    bool operator<(const Node& A, const Node& B)
53✔
405
    {
53✔
406
        if (A.nodeType() != B.nodeType())
53✔
UNCOV
407
            return (static_cast<int>(A.nodeType()) - static_cast<int>(B.nodeType())) < 0;
×
408

409
        switch (A.nodeType())
53✔
410
        {
53✔
411
            case NodeType::Number:
412
                [[fallthrough]];
413
            case NodeType::Symbol:
414
                [[fallthrough]];
415
            case NodeType::String:
416
                return A.m_value < B.m_value;
53✔
417

418
            default:
419
                return false;
×
420
        }
421
    }
53✔
422
}
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