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

ArkScript-lang / Ark / 15292297599

27 May 2025 03:31PM UTC coverage: 86.63% (+0.004%) from 86.626%
15292297599

push

github

SuperFola
fix(name resolver): couldn't import two different files with the same symbol names inside, even though both were hidden and prefixed by their file

5 of 5 new or added lines in 2 files covered. (100.0%)

61 existing lines in 5 files now uncovered.

7231 of 8347 relevant lines covered (86.63%)

91165.94 hits per line

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

92.75
/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) :
95,368✔
11
        m_type(node_type), m_value(value)
47,684✔
12
    {}
47,684✔
13

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

21
    Node::Node(double value) :
69,920✔
22
        m_type(NodeType::Number), m_value(value)
69,920✔
23
    {}
139,840✔
24

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

29
    Node::Node(Keyword value) :
9,330✔
30
        m_type(NodeType::Keyword), m_value(value)
9,330✔
31
    {}
18,660✔
32

33
    Node::Node(const Namespace& namespace_) :
198✔
34
        m_type(NodeType::Namespace), m_value(namespace_)
99✔
35
    {}
99✔
36

37
    const std::string& Node::string() const noexcept
3,927,393✔
38
    {
3,927,393✔
39
        return std::get<std::string>(m_value);
3,927,393✔
40
    }
41

42
    double Node::number() const noexcept
71,892✔
43
    {
71,892✔
44
        return std::get<double>(m_value);
71,892✔
45
    }
46

47
    Keyword Node::keyword() const noexcept
76,671✔
48
    {
76,671✔
49
        return std::get<Keyword>(m_value);
76,671✔
50
    }
51

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

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

62
    void Node::push_back(const Node& node) noexcept
148,144✔
63
    {
148,144✔
64
        list().push_back(node);
148,144✔
65
    }
148,144✔
66

67
    std::vector<Node>& Node::list() noexcept
904,286✔
68
    {
904,286✔
69
        return std::get<std::vector<Node>>(m_value);
904,286✔
70
    }
71

72
    const std::vector<Node>& Node::constList() const noexcept
1,581,224✔
73
    {
1,581,224✔
74
        return std::get<std::vector<Node>>(m_value);
1,581,224✔
75
    }
76

77
    NodeType Node::nodeType() const noexcept
3,221,046✔
78
    {
3,221,046✔
79
        return m_type;
3,221,046✔
80
    }
81

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

87
    bool Node::isStringLike() const noexcept
×
88
    {
×
89
        return m_type == NodeType::Symbol || m_type == NodeType::String || m_type == NodeType::Spread;
×
90
    }
91

92
    void Node::updateValueAndType(const Node& source) noexcept
17,107✔
93
    {
17,107✔
94
        m_type = source.m_type;
17,107✔
95
        m_value = source.m_value;
17,107✔
96
    }
17,107✔
97

98
    void Node::setNodeType(const NodeType type) noexcept
278✔
99
    {
278✔
100
        m_type = type;
278✔
101
    }
278✔
102

103
    void Node::setString(const std::string& value) noexcept
77,771✔
104
    {
77,771✔
105
        m_value = value;
77,771✔
106
    }
77,771✔
107

108
    void Node::setPos(const std::size_t line, const std::size_t col) noexcept
278,811✔
109
    {
278,811✔
110
        m_line = line;
278,811✔
111
        m_col = col;
278,811✔
112
    }
278,811✔
113

114
    void Node::setFilename(const std::string& filename) noexcept
278,811✔
115
    {
278,811✔
116
        m_filename = filename;
278,811✔
117
    }
278,811✔
118

119
    Node& Node::attachNearestCommentBefore(const std::string& comment)
138,823✔
120
    {
138,823✔
121
        m_comment = comment;
138,823✔
122
        return *this;
138,823✔
123
    }
124

125
    Node& Node::attachCommentAfter(const std::string& comment)
11,486✔
126
    {
11,486✔
127
        if (!m_after_comment.empty())
11,486✔
128
            m_after_comment += "\n";
2✔
129
        m_after_comment += comment;
11,486✔
130
        if (!m_after_comment.empty() && m_after_comment.back() == '\n')
11,486✔
131
            m_after_comment.pop_back();
69✔
132
        return *this;
11,486✔
133
    }
134

135
    void Node::setAltSyntax(const bool toggle)
2,391✔
136
    {
2,391✔
137
        m_alt_syntax = toggle;
2,391✔
138
    }
2,391✔
139

UNCOV
140
    bool Node::isAltSyntax() const
×
UNCOV
141
    {
×
UNCOV
142
        return m_alt_syntax;
×
143
    }
144

145
    std::size_t Node::line() const noexcept
375,079✔
146
    {
375,079✔
147
        return m_line;
375,079✔
148
    }
149

150
    std::size_t Node::col() const noexcept
342,918✔
151
    {
342,918✔
152
        return m_col;
342,918✔
153
    }
154

155
    const std::string& Node::filename() const noexcept
43,463✔
156
    {
43,463✔
157
        return m_filename;
43,463✔
158
    }
159

160
    const std::string& Node::comment() const noexcept
11,091✔
161
    {
11,091✔
162
        return m_comment;
11,091✔
163
    }
164

165
    const std::string& Node::commentAfter() const noexcept
2,048✔
166
    {
2,048✔
167
        return m_after_comment;
2,048✔
168
    }
169

170
    std::string Node::repr() const noexcept
78,232✔
171
    {
78,232✔
172
        std::string data;
78,232✔
173
        switch (m_type)
78,232✔
174
        {
10,271✔
175
            case NodeType::Symbol:
176
                data += string();
10,271✔
177
                break;
10,273✔
178

179
            case NodeType::Capture:
180
                data += "&" + string();
2✔
181
                break;
71✔
182

183
            case NodeType::Keyword:
184
                data += keywords[static_cast<std::size_t>(keyword())];
69✔
185
                break;
358✔
186

187
            case NodeType::String:
188
                data += "\"" + string() + "\"";
289✔
189
                break;
66,828✔
190

191
            case NodeType::Number:
192
                data += fmt::format("{}", number());
66,539✔
193
                break;
67,432✔
194

195
            case NodeType::List:
196
                if (m_alt_syntax)
893✔
197
                {
198
                    const auto first = constList().front();
225✔
199
                    char open = 0;
225✔
200
                    if (first.nodeType() == NodeType::Keyword && first.keyword() == Keyword::Begin)
225✔
201
                        open = '{';
10✔
202
                    else if (first.nodeType() == NodeType::Symbol && first.string() == "list")
215✔
203
                        open = '[';
215✔
204
                    else
UNCOV
205
                        assert(false && "Alt syntax nodes can only be begin or list");
×
206

207
                    data += open;
225✔
208
                    for (std::size_t i = 1, end = constList().size(); i < end; ++i)
686✔
209
                    {
210
                        data += constList()[i].repr();
461✔
211
                        if (i < end - 1)
461✔
212
                            data += " ";
294✔
213
                    }
461✔
214

215
                    if (open == '{')
225✔
216
                        data += "}";
10✔
217
                    else if (open == '[')
215✔
218
                        data += "]";
215✔
219
                }
225✔
220
                else
221
                {
222
                    data += "(";
668✔
223
                    for (std::size_t i = 0, end = constList().size(); i < end; ++i)
67,997✔
224
                    {
225
                        data += constList()[i].repr();
67,329✔
226
                        if (i < end - 1)
67,329✔
227
                            data += " ";
66,665✔
228
                    }
67,329✔
229
                    data += ")";
668✔
230
                }
231
                break;
1,022✔
232

233
            case NodeType::Field:
234
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
401✔
235
                {
236
                    data += constList()[i].repr();
315,893✔
237
                    if (i < end - 1)
272✔
238
                        data += ".";
143✔
239
                }
272✔
240
                break;
315,777✔
241

242
            case NodeType::Macro:
243
                data += "(macro ";
27✔
244
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
117✔
245
                {
246
                    data += constList()[i].repr();
90✔
247
                    if (i < end - 1)
90✔
248
                        data += " ";
63✔
249
                }
90✔
250
                data += ")";
27✔
251
                break;
36✔
252

253
            case NodeType::Spread:
254
                data += "..." + string();
9✔
255
                break;
9✔
256

257
            // namespace node should not have a representation as it is purely internal,
258
            // and it can't be exploited by macros (unless you try passing an import node
259
            // to a macro, which should not happen?)
260
            case NodeType::Namespace:
UNCOV
261
                data += constArkNamespace().ast->repr();
×
262
                break;
4✔
263

264
            case NodeType::Unused:
265
                break;
4✔
266
        }
78,232✔
267
        return data;
78,232✔
268
    }
78,232✔
269

270
    std::ostream& Node::debugPrint(std::ostream& os) const noexcept
469✔
271
    {
469✔
272
        switch (m_type)
469✔
273
        {
143✔
274
            case NodeType::Symbol:
275
                os << "Symbol:" << string();
143✔
276
                break;
149✔
277

278
            case NodeType::Capture:
279
                os << "Capture:" << string();
6✔
280
                break;
84✔
281

282
            case NodeType::Keyword:
283
                os << "Keyword:";
78✔
284
                switch (keyword())
78✔
285
                {
14✔
286
                    case Keyword::Fun: os << "Fun"; break;
32✔
287
                    case Keyword::Let: os << "Let"; break;
21✔
288
                    case Keyword::Mut: os << "Mut"; break;
4✔
289
                    case Keyword::Set: os << "Set"; break;
16✔
290
                    case Keyword::If: os << "If"; break;
21✔
291
                    case Keyword::While: os << "While"; break;
15✔
292
                    case Keyword::Begin: os << "Begin"; break;
17✔
293
                    case Keyword::Import: os << "Import"; break;
12✔
294
                    case Keyword::Del: os << "Del"; break;
4✔
295
                }
78✔
296
                break;
86✔
297

298
            case NodeType::String:
299
                os << "String:" << string();
8✔
300
                break;
83✔
301

302
            case NodeType::Number:
303
                os << "Number:" << number();
75✔
304
                break;
215✔
305

306
            case NodeType::List:
307
                os << "( ";
140✔
308
                for (const auto& i : constList())
482✔
309
                    i.debugPrint(os) << " ";
342✔
310
                os << ")";
140✔
311
                break;
149✔
312

313
            case NodeType::Field:
314
                os << "( Field ";
9✔
315
                for (const auto& i : constList())
30✔
316
                    i.debugPrint(os) << " ";
21✔
317
                os << ")";
9✔
318
                break;
17✔
319

320
            case NodeType::Macro:
321
                os << "( Macro ";
8✔
322
                for (const auto& i : constList())
31✔
323
                    i.debugPrint(os) << " ";
23✔
324
                os << ")";
8✔
325
                break;
10✔
326

327
            case NodeType::Spread:
328
                os << "Spread:" << string();
2✔
329
                break;
2✔
330

331
            case NodeType::Namespace:
332
            {
UNCOV
333
                const auto details = constArkNamespace();
×
UNCOV
334
                os << "( Namespace:" << details.name << " ";
×
UNCOV
335
                details.ast->debugPrint(os) << " )";
×
336
                break;
UNCOV
337
            }
×
338

339
            case NodeType::Unused:
UNCOV
340
                break;
×
341
        }
469✔
342
        return os;
469✔
343
    }
344

345
    const Node& getTrueNode()
77✔
346
    {
77✔
347
        static const Node TrueNode(NodeType::Symbol, "true");
77✔
348
        return TrueNode;
77✔
UNCOV
349
    }
×
350

351
    const Node& getFalseNode()
59✔
352
    {
59✔
353
        static const Node FalseNode(NodeType::Symbol, "false");
59✔
354
        return FalseNode;
59✔
UNCOV
355
    }
×
356

357
    const Node& getNilNode()
51✔
358
    {
51✔
359
        static const Node NilNode(NodeType::Symbol, "nil");
51✔
360
        return NilNode;
51✔
UNCOV
361
    }
×
362

363
    const Node& getListNode()
940✔
364
    {
940✔
365
        static const Node ListNode(NodeType::Symbol, "list");
940✔
366
        return ListNode;
940✔
UNCOV
367
    }
×
368

369
    bool operator==(const Node& A, const Node& B)
414✔
370
    {
414✔
371
        if (A.m_type != B.m_type)  // should have the same types
414✔
372
            return false;
30✔
373

374
        if (A.m_type != NodeType::List)
384✔
375
            return A.m_value == B.m_value;
384✔
UNCOV
376
        return false;
×
377
    }
414✔
378

379
    bool operator<(const Node& A, const Node& B)
58✔
380
    {
58✔
381
        if (A.nodeType() != B.nodeType())
58✔
382
            return (static_cast<int>(A.nodeType()) - static_cast<int>(B.nodeType())) < 0;
18✔
383

384
        switch (A.nodeType())
40✔
385
        {
40✔
386
            case NodeType::Number:
387
                [[fallthrough]];
388
            case NodeType::Symbol:
389
                [[fallthrough]];
390
            case NodeType::String:
391
                return A.m_value < B.m_value;
40✔
392

393
            default:
UNCOV
394
                return false;
×
395
        }
396
    }
58✔
397
}
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