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

ArkScript-lang / Ark / 14823373998

04 May 2025 05:03PM UTC coverage: 86.442% (+0.03%) from 86.409%
14823373998

push

github

SuperFola
fix: change the color of the function name inside runtime typechecking errors from blue to cyan to be easier to read inside dark terminals

0 of 1 new or added line in 1 file covered. (0.0%)

195 existing lines in 22 files now uncovered.

6835 of 7907 relevant lines covered (86.44%)

79668.53 hits per line

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

93.22
/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) :
82,158✔
11
        m_type(node_type), m_value(value)
41,079✔
12
    {}
41,079✔
13

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

21
    Node::Node(double value) :
69,379✔
22
        m_type(NodeType::Number), m_value(value)
69,379✔
23
    {}
138,758✔
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) :
8,080✔
30
        m_type(NodeType::Keyword), m_value(value)
8,080✔
31
    {}
16,160✔
32

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

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

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

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

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

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

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

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

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

77
    NodeType Node::nodeType() const noexcept
2,933,475✔
78
    {
2,933,475✔
79
        return m_type;
2,933,475✔
80
    }
81

82
    bool Node::isListLike() const noexcept
16,301✔
83
    {
16,301✔
84
        return m_type == NodeType::List || m_type == NodeType::Macro;
16,301✔
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
15,589✔
93
    {
15,589✔
94
        m_type = source.m_type;
15,589✔
95
        m_value = source.m_value;
15,589✔
96
    }
15,589✔
97

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

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

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

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

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

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

135
    std::size_t Node::line() const noexcept
326,832✔
136
    {
326,832✔
137
        return m_line;
326,832✔
138
    }
139

140
    std::size_t Node::col() const noexcept
297,822✔
141
    {
297,822✔
142
        return m_col;
297,822✔
143
    }
144

145
    const std::string& Node::filename() const noexcept
39,175✔
146
    {
39,175✔
147
        return m_filename;
39,175✔
148
    }
149

150
    const std::string& Node::comment() const noexcept
9,911✔
151
    {
9,911✔
152
        return m_comment;
9,911✔
153
    }
154

155
    const std::string& Node::commentAfter() const noexcept
2,048✔
156
    {
2,048✔
157
        return m_after_comment;
2,048✔
158
    }
159

160
    std::string Node::repr() const noexcept
68,801✔
161
    {
68,801✔
162
        std::string data;
68,801✔
163
        switch (m_type)
68,801✔
164
        {
1,217✔
165
            case NodeType::Symbol:
166
                data += string();
1,217✔
167
                break;
1,219✔
168

169
            case NodeType::Capture:
170
                data += "&" + string();
2✔
171
                break;
62✔
172

173
            case NodeType::Keyword:
174
                data += keywords[static_cast<std::size_t>(keyword())];
60✔
175
                break;
341✔
176

177
            case NodeType::String:
178
                data += "\"" + string() + "\"";
281✔
179
                break;
66,690✔
180

181
            case NodeType::Number:
182
                data += fmt::format("{}", number());
66,409✔
183
                break;
67,179✔
184

185
            case NodeType::List:
186
                data += "(";
770✔
187
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
68,419✔
188
                {
189
                    data += constList()[i].repr();
67,649✔
190
                    if (i < end - 1)
67,649✔
191
                        data += " ";
66,880✔
192
                }
67,649✔
193
                data += ")";
770✔
194
                break;
813✔
195

196
            case NodeType::Field:
197
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
131✔
198
                {
199
                    data += constList()[i].repr();
88✔
200
                    if (i < end - 1)
88✔
201
                        data += ".";
45✔
202
                }
88✔
203
                break;
59✔
204

205
            case NodeType::Macro:
206
                data += "($ ";
16✔
207
                for (std::size_t i = 0, end = constList().size(); i < end; ++i)
77✔
208
                {
209
                    data += constList()[i].repr();
61✔
210
                    if (i < end - 1)
61✔
211
                        data += " ";
45✔
212
                }
61✔
213
                data += ")";
16✔
214
                break;
19✔
215

216
            case NodeType::Spread:
217
                data += "..." + string();
3✔
218
                break;
3✔
219

220
            // namespace node should not have a representation as it is purely internal,
221
            // and it can't be exploited by macros (unless you try passing an import node
222
            // to a macro, which should not happen?)
223
            case NodeType::Namespace:
224
                data += constArkNamespace().ast->repr();
273,583✔
UNCOV
225
                break;
×
226

227
            case NodeType::Unused:
228
                break;
×
229
        }
68,801✔
230
        return data;
68,801✔
231
    }
68,801✔
232

233
    std::ostream& Node::debugPrint(std::ostream& os) const noexcept
469✔
234
    {
469✔
235
        switch (m_type)
469✔
236
        {
143✔
237
            case NodeType::Symbol:
238
                os << "Symbol:" << string();
143✔
239
                break;
149✔
240

241
            case NodeType::Capture:
242
                os << "Capture:" << string();
6✔
243
                break;
84✔
244

245
            case NodeType::Keyword:
246
                os << "Keyword:";
78✔
247
                switch (keyword())
78✔
248
                {
14✔
249
                    case Keyword::Fun: os << "Fun"; break;
32✔
250
                    case Keyword::Let: os << "Let"; break;
21✔
251
                    case Keyword::Mut: os << "Mut"; break;
4✔
252
                    case Keyword::Set: os << "Set"; break;
16✔
253
                    case Keyword::If: os << "If"; break;
21✔
254
                    case Keyword::While: os << "While"; break;
15✔
255
                    case Keyword::Begin: os << "Begin"; break;
17✔
256
                    case Keyword::Import: os << "Import"; break;
12✔
257
                    case Keyword::Del: os << "Del"; break;
4✔
258
                }
78✔
259
                break;
86✔
260

261
            case NodeType::String:
262
                os << "String:" << string();
8✔
263
                break;
83✔
264

265
            case NodeType::Number:
266
                os << "Number:" << number();
75✔
267
                break;
215✔
268

269
            case NodeType::List:
270
                os << "( ";
140✔
271
                for (const auto& i : constList())
482✔
272
                    i.debugPrint(os) << " ";
342✔
273
                os << ")";
140✔
274
                break;
149✔
275

276
            case NodeType::Field:
277
                os << "( Field ";
9✔
278
                for (const auto& i : constList())
30✔
279
                    i.debugPrint(os) << " ";
21✔
280
                os << ")";
9✔
281
                break;
17✔
282

283
            case NodeType::Macro:
284
                os << "( Macro ";
8✔
285
                for (const auto& i : constList())
31✔
286
                    i.debugPrint(os) << " ";
23✔
287
                os << ")";
8✔
288
                break;
10✔
289

290
            case NodeType::Spread:
291
                os << "Spread:" << string();
2✔
292
                break;
2✔
293

294
            case NodeType::Namespace:
295
            {
296
                const auto details = constArkNamespace();
×
297
                os << "( Namespace:" << details.name << " ";
×
298
                details.ast->debugPrint(os) << " )";
×
299
                break;
300
            }
×
301

302
            case NodeType::Unused:
303
                break;
×
304
        }
469✔
305
        return os;
469✔
306
    }
307

308
    const Node& getTrueNode()
55✔
309
    {
55✔
310
        static const Node TrueNode(NodeType::Symbol, "true");
55✔
311
        return TrueNode;
55✔
312
    }
×
313

314
    const Node& getFalseNode()
45✔
315
    {
45✔
316
        static const Node FalseNode(NodeType::Symbol, "false");
45✔
317
        return FalseNode;
45✔
318
    }
×
319

320
    const Node& getNilNode()
43✔
321
    {
43✔
322
        static const Node NilNode(NodeType::Symbol, "nil");
43✔
323
        return NilNode;
43✔
324
    }
×
325

326
    const Node& getListNode()
707✔
327
    {
707✔
328
        static const Node ListNode(NodeType::Symbol, "list");
707✔
329
        return ListNode;
707✔
330
    }
×
331

332
    bool operator==(const Node& A, const Node& B)
194✔
333
    {
194✔
334
        if (A.m_type != B.m_type)  // should have the same types
194✔
335
            return false;
18✔
336

337
        if (A.m_type != NodeType::List)
176✔
338
            return A.m_value == B.m_value;
176✔
339
        return false;
×
340
    }
194✔
341

342
    bool operator<(const Node& A, const Node& B)
52✔
343
    {
52✔
344
        if (A.nodeType() != B.nodeType())
52✔
345
            return (static_cast<int>(A.nodeType()) - static_cast<int>(B.nodeType())) < 0;
14✔
346

347
        switch (A.nodeType())
38✔
348
        {
38✔
349
            case NodeType::Number:
350
                [[fallthrough]];
351
            case NodeType::Symbol:
352
                [[fallthrough]];
353
            case NodeType::String:
354
                return A.m_value < B.m_value;
38✔
355

356
            default:
357
                return false;
×
358
        }
359
    }
52✔
360
}
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