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

ArkScript-lang / Ark / 13344738465

15 Feb 2025 11:50AM UTC coverage: 77.022% (-1.9%) from 78.929%
13344738465

Pull #510

github

web-flow
Merge 70d135e0e into dd2f0e5fc
Pull Request #510: wip

5695 of 7394 relevant lines covered (77.02%)

44192.48 hits per line

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

83.73
/src/arkreactor/Compiler/Macros/Processor.cpp
1
#include <Ark/Compiler/Macros/Processor.hpp>
2

3
#include <utility>
4
#include <algorithm>
5
#include <cassert>
6
#include <ranges>
7
#include <sstream>
8
#include <fmt/core.h>
9

10
#include <Ark/Constants.hpp>
11
#include <Ark/Exceptions.hpp>
12
#include <Ark/Builtins/Builtins.hpp>
13
#include <Ark/Compiler/Macros/Executor.hpp>
14
#include <Ark/Compiler/Macros/Executors/Symbol.hpp>
15
#include <Ark/Compiler/Macros/Executors/Function.hpp>
16
#include <Ark/Compiler/Macros/Executors/Conditional.hpp>
17

18
namespace Ark::internal
19
{
20
    MacroProcessor::MacroProcessor(const unsigned debug) noexcept :
340✔
21
        Pass("MacroProcessor", debug)
170✔
22
    {
340✔
23
        // create executors pipeline
24
        m_conditional_executor = std::make_shared<ConditionalExecutor>(this);
170✔
25
        m_executors.emplace_back(std::make_shared<SymbolExecutor>(this));
170✔
26
        m_executors.emplace_back(m_conditional_executor);
170✔
27
        m_executors.emplace_back(std::make_shared<FunctionExecutor>(this));
170✔
28
    }
170✔
29

30
    void MacroProcessor::process(const Node& ast)
140✔
31
    {
140✔
32
        m_logger.debug("Processing macros...");
140✔
33
        m_logger.traceStart("process");
140✔
34

35
        // to be able to modify it
36
        m_ast = ast;
140✔
37
        processNode(m_ast, 0);
140✔
38

39
        m_logger.traceEnd();
140✔
40
        m_logger.trace("AST after processing macros");
140✔
41
        if (m_logger.shouldTrace())
140✔
42
            m_ast.debugPrint(std::cout) << '\n';
×
43
    }
140✔
44

45
    const Node& MacroProcessor::ast() const noexcept
119✔
46
    {
119✔
47
        return m_ast;
119✔
48
    }
49

50
    void MacroProcessor::handleMacroNode(Node& node)
131✔
51
    {
131✔
52
        // a macro needs at least 2 nodes, name + value is the minimal form
53
        // this is guaranted by the parser
54
        assert(node.constList().size() >= 2 && "Invalid macro, missing value");
131✔
55

56
        const Node& first_node = node.list()[0];
131✔
57

58
        // ($ name value)
59
        if (node.constList().size() == 2)
131✔
60
        {
61
            assert(first_node.nodeType() == NodeType::Symbol && "Can not define a macro without a symbol");
39✔
62
            applyMacro(node.list()[1], 0);
19✔
63
            node.list()[1] = evaluate(node.list()[1], 0, true);
19✔
64
            m_macros.back().add(first_node.string(), node);
19✔
65
        }
19✔
66
        // ($ name (args) body)
67
        else if (node.constList().size() == 3 && first_node.nodeType() == NodeType::Symbol)
92✔
68
        {
69
            assert(node.list()[1].nodeType() == NodeType::List && "Invalid macro argument's list");
34✔
70
            m_macros.back().add(first_node.string(), node);
34✔
71
        }
34✔
72
        // in case we had a conditional, we need to evaluate and expand it
73
        else if (m_conditional_executor->canHandle(node))
58✔
74
            m_conditional_executor->applyMacro(node, 0);
56✔
75
    }
107✔
76

77
    // todo find a better way to do this
78
    void MacroProcessor::registerFuncDef(const Node& node)
17,487✔
79
    {
17,487✔
80
        if (node.nodeType() == NodeType::List && node.constList().size() == 3 && node.constList()[0].nodeType() == NodeType::Keyword)
17,487✔
81
        {
82
            Keyword kw = node.constList()[0].keyword();
3,046✔
83
            // checking for function definition, which can occur only inside an assignment node
84
            if (kw != Keyword::Let && kw != Keyword::Mut && kw != Keyword::Set)
3,046✔
85
                return;
1,238✔
86

87
            const Node inner = node.constList()[2];
1,808✔
88
            if (inner.nodeType() != NodeType::List)
1,808✔
89
                return;
462✔
90

91
            if (!inner.constList().empty() && inner.constList()[0].nodeType() == NodeType::Keyword && inner.constList()[0].keyword() == Keyword::Fun)
1,346✔
92
            {
93
                const Node symbol = node.constList()[1];
489✔
94
                if (symbol.nodeType() == NodeType::Symbol)
489✔
95
                    m_defined_functions.emplace(symbol.string(), inner.constList()[1]);
488✔
96
                else
97
                    throwMacroProcessingError(fmt::format("Can not use a {} to define a variable", typeToString(symbol)), symbol);
1✔
98
            }
489✔
99
        }
3,046✔
100
    }
17,487✔
101

102
    void MacroProcessor::processNode(Node& node, unsigned depth, bool is_processing_namespace)
118,563✔
103
    {
118,563✔
104
        if (depth >= MaxMacroProcessingDepth)
118,563✔
105
            throwMacroProcessingError(
1✔
106
                fmt::format(
2✔
107
                    "Max recursion depth reached ({}). You most likely have a badly defined recursive macro calling itself without a proper exit condition",
1✔
108
                    MaxMacroProcessingDepth),
109
                node);
1✔
110

111
        if (node.nodeType() == NodeType::List)
118,562✔
112
        {
113
            bool has_created = false;
17,667✔
114
            // recursive call
115
            std::size_t i = 0;
17,667✔
116
            while (i < node.list().size())
136,414✔
117
            {
118
                const std::size_t pos = i;
118,747✔
119
                Node& child = node.list()[pos];
118,747✔
120
                const bool had_begin = isBeginNode(child);
118,747✔
121
                bool added_begin = false;
118,747✔
122

123
                if (child.nodeType() == NodeType::Macro)
118,747✔
124
                {
125
                    // create a scope only if needed
126
                    if ((!m_macros.empty() && !m_macros.back().empty() && m_macros.back().depth() < depth && !is_processing_namespace) ||
178✔
127
                        (!has_created && !is_processing_namespace) ||
75✔
128
                        (m_macros.empty() && is_processing_namespace))
20✔
129
                    {
130
                        has_created = true;
83✔
131
                        m_macros.emplace_back(depth);
83✔
132
                    }
83✔
133

134
                    handleMacroNode(child);
101✔
135
                    added_begin = isBeginNode(child) && !had_begin;
101✔
136
                }
101✔
137
                else  // running on non-macros
138
                {
139
                    applyMacro(child, 0);
118,646✔
140
                    added_begin = isBeginNode(child) && !had_begin;
118,646✔
141

142
                    if (child.nodeType() == NodeType::Unused)
118,646✔
143
                        node.list().erase(node.constList().begin() + static_cast<std::vector<Node>::difference_type>(pos));
4✔
144
                    else if (!added_begin)
118,642✔
145
                        // Go forward only if it isn't a macro, because we delete macros
146
                        // while running on the AST. Also, applying a macro can result in
147
                        // nodes being marked unused, and delete them immediately. When
148
                        // that happens, we can't increment i, otherwise we delete a node,
149
                        // advance, resulting in a node being skipped!
150
                        ++i;
118,219✔
151

152
                    // process subnodes if any
153
                    if (node.nodeType() == NodeType::List && pos < node.constList().size())
118,646✔
154
                    {
155
                        processNode(child, depth + 1);
118,382✔
156
                        // needed if we created a function node from a macro
157
                        registerFuncDef(child);
118,382✔
158
                    }
118,382✔
159
                }
160

161
                if (pos < node.constList().size())
118,747✔
162
                {
163
                    // if we now have a surrounding (begin ...) and didn't have one before, remove it
164
                    if (added_begin)
118,482✔
165
                        removeBegin(node, pos);
424✔
166
                    // if there is an unused node or a leftover macro need, we need to get rid of it in the final ast
167
                    else if (child.nodeType() == NodeType::Macro || child.nodeType() == NodeType::Unused)
118,058✔
168
                        node.list().erase(node.constList().begin() + static_cast<std::vector<Node>::difference_type>(pos));
63✔
169
                }
118,482✔
170
            }
118,747✔
171

172
            // delete a scope only if needed
173
            if (!m_macros.empty() && m_macros.back().depth() == depth && !is_processing_namespace)
17,667✔
174
                m_macros.pop_back();
62✔
175
        }
17,667✔
176
        else if (node.nodeType() == NodeType::Namespace)
100,895✔
177
        {
178
            Node& namespace_ast = *node.arkNamespace().ast;
61✔
179
            // We have to use depth - 1 because it was incremented previously, as a namespace node
180
            // must be in a list node. Then depth - 1 is safe as depth is at least 1.
181
            // Using a decreased value of depth ensures that macros are stored in the correct scope,
182
            // and not deleted when the namespace traversal ends.
183
            processNode(namespace_ast, depth - 1, /* is_processing_namespace= */ true);
61✔
184
        }
61✔
185
    }
118,563✔
186

187
    bool MacroProcessor::applyMacro(Node& node, const unsigned depth)
119,540✔
188
    {
119,540✔
189
        if (depth > MaxMacroProcessingDepth)
119,540✔
190
            throwMacroProcessingError(
1✔
191
                fmt::format(
2✔
192
                    "Max macro processing depth reached ({}). You may have a macro trying to evaluate itself, try splitting your code in multiple nodes.",
1✔
193
                    MaxMacroProcessingDepth),
194
                node);
1✔
195

196
        for (const auto& executor : m_executors)
477,591✔
197
        {
198
            if (executor->canHandle(node))
358,244✔
199
            {
200
                if (executor->applyMacro(node, depth))
31,960✔
201
                    return true;
586✔
202
            }
31,182✔
203
        }
358,052✔
204
        return false;
118,857✔
205
    }
119,348✔
206

207
    void MacroProcessor::checkMacroArgCountEq(const Node& node, std::size_t expected, const std::string& name, const std::string& kind)
78✔
208
    {
78✔
209
        const std::size_t argcount = node.constList().size();
78✔
210
        if (argcount != expected + 1)
78✔
211
            throwMacroProcessingError(
×
212
                fmt::format(
×
213
                    "Interpreting a `{}'{} with {} arguments, expected {}.",
×
214
                    name,
×
215
                    kind.empty() ? kind : " " + kind,
×
216
                    argcount,
217
                    expected),
218
                node);
×
219
    }
78✔
220

221
    void MacroProcessor::checkMacroArgCountGe(const Node& node, std::size_t expected, const std::string& name, const std::string& kind)
42✔
222
    {
42✔
223
        const std::size_t argcount = node.constList().size();
42✔
224
        if (argcount < expected + 1)
42✔
225
            throwMacroProcessingError(
×
226
                fmt::format(
×
227
                    "Interpreting a `{}'{} with {} arguments, expected at least {}.",
×
228
                    name,
×
229
                    kind.empty() ? kind : " " + kind,
×
230
                    argcount,
231
                    expected),
232
                node);
×
233
    }
42✔
234

235
    Node MacroProcessor::evaluate(Node& node, const unsigned depth, const bool is_not_body)
9,836✔
236
    {
9,836✔
237
        if (node.nodeType() == NodeType::Symbol)
9,836✔
238
        {
239
            const Node* macro = findNearestMacro(node.string());
2,592✔
240
            if (macro != nullptr && macro->constList().size() == 2)
2,592✔
241
                return macro->constList()[1];
131✔
242
            return node;
2,461✔
243
        }
2,592✔
244
        if (node.nodeType() == NodeType::List && !node.constList().empty() && node.list()[0].nodeType() == NodeType::Symbol)
7,244✔
245
        {
246
            const std::string& name = node.list()[0].string();
4,138✔
247
            const std::size_t argcount = node.list().size() - 1;
4,138✔
248

249
            if (const Node* macro = findNearestMacro(name); macro != nullptr)
8,276✔
250
            {
251
                applyMacro(node.list()[0], depth + 1);
189✔
252
                if (node.list()[0].nodeType() == NodeType::Unused)
189✔
253
                    node.list().erase(node.constList().begin());
×
254
            }
189✔
255
            else if (name == "=" && is_not_body)
3,949✔
256
            {
257
                checkMacroArgCountEq(node, 2, "=", "condition");
15✔
258
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
15✔
259
                const Node two = evaluate(node.list()[2], depth + 1, is_not_body);
15✔
260
                return (one == two) ? getTrueNode() : getFalseNode();
15✔
261
            }
15✔
262
            else if (name == "!=" && is_not_body)
3,934✔
263
            {
264
                checkMacroArgCountEq(node, 2, "!=", "condition");
×
265
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
×
266
                const Node two = evaluate(node.list()[2], depth + 1, is_not_body);
×
267
                return (one != two) ? getTrueNode() : getFalseNode();
×
268
            }
×
269
            else if (name == "<" && is_not_body)
3,934✔
270
            {
271
                checkMacroArgCountEq(node, 2, "<", "condition");
×
272
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
×
273
                const Node two = evaluate(node.list()[2], depth + 1, is_not_body);
×
274
                return (one < two) ? getTrueNode() : getFalseNode();
×
275
            }
×
276
            else if (name == ">" && is_not_body)
3,934✔
277
            {
278
                checkMacroArgCountEq(node, 2, ">", "condition");
28✔
279
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
28✔
280
                const Node two = evaluate(node.list()[2], depth + 1, is_not_body);
28✔
281
                return !(one < two) && (one != two) ? getTrueNode() : getFalseNode();
28✔
282
            }
28✔
283
            else if (name == "<=" && is_not_body)
3,906✔
284
            {
285
                checkMacroArgCountEq(node, 2, "<=", "condition");
×
286
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
×
287
                const Node two = evaluate(node.list()[2], depth + 1, is_not_body);
×
288
                return one < two || one == two ? getTrueNode() : getFalseNode();
×
289
            }
×
290
            else if (name == ">=" && is_not_body)
3,906✔
291
            {
292
                checkMacroArgCountEq(node, 2, ">=", "condition");
7✔
293
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
7✔
294
                const Node two = evaluate(node.list()[2], depth + 1, is_not_body);
7✔
295
                return !(one < two) ? getTrueNode() : getFalseNode();
7✔
296
            }
7✔
297
            else if (name == "+" && is_not_body)
3,899✔
298
            {
299
                checkMacroArgCountGe(node, 2, "+", "operator");
2✔
300
                double v = 0.0;
2✔
301
                for (auto& child : node.list() | std::ranges::views::drop(1))
7✔
302
                {
303
                    Node ev = evaluate(child, depth + 1, is_not_body);
5✔
304
                    if (ev.nodeType() != NodeType::Number)
5✔
305
                        return node;
×
306
                    v += ev.number();
5✔
307
                }
5✔
308
                return Node(v);
2✔
309
            }
2✔
310
            else if (name == "-" && is_not_body)
3,897✔
311
            {
312
                checkMacroArgCountGe(node, 2, "-", "operator");
38✔
313
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
38✔
314
                if (one.nodeType() != NodeType::Number)
38✔
315
                    return node;
×
316

317
                double v = one.number();
38✔
318
                for (auto& child : node.list() | std::ranges::views::drop(2))
77✔
319
                {
320
                    Node ev = evaluate(child, depth + 1, is_not_body);
39✔
321
                    if (ev.nodeType() != NodeType::Number)
39✔
322
                        return node;
×
323
                    v -= ev.number();
39✔
324
                }
39✔
325
                return Node(v);
38✔
326
            }
38✔
327
            else if (name == "*" && is_not_body)
3,859✔
328
            {
329
                checkMacroArgCountGe(node, 2, "*", "operator");
×
330
                double v = 1.0;
×
331
                for (auto& child : node.list() | std::ranges::views::drop(1))
×
332
                {
333
                    Node ev = evaluate(child, depth + 1, is_not_body);
×
334
                    if (ev.nodeType() != NodeType::Number)
×
335
                        return node;
×
336
                    v *= ev.number();
×
337
                }
×
338
                return Node(v);
×
339
            }
×
340
            else if (name == "/" && is_not_body)
3,859✔
341
            {
342
                checkMacroArgCountGe(node, 2, "/", "operator");
1✔
343
                const Node one = evaluate(node.list()[1], depth + 1, is_not_body);
1✔
344
                if (one.nodeType() != NodeType::Number)
1✔
345
                    return node;
1✔
346

347
                double v = one.number();
×
348
                for (auto& child : node.list() | std::ranges::views::drop(2))
×
349
                {
350
                    Node ev = evaluate(child, depth + 1, is_not_body);
×
351
                    if (ev.nodeType() != NodeType::Number)
×
352
                        return node;
×
353
                    v /= ev.number();
×
354
                }
×
355
                return Node(v);
×
356
            }
1✔
357
            else if (name == "not" && is_not_body)
3,858✔
358
            {
359
                checkMacroArgCountEq(node, 1, "not", "condition");
×
360
                return (!isTruthy(evaluate(node.list()[1], depth + 1, is_not_body))) ? getTrueNode() : getFalseNode();
×
361
            }
362
            else if (name == Language::And && is_not_body)
3,858✔
363
            {
364
                if (node.list().size() < 3)
2✔
365
                    throwMacroProcessingError(fmt::format("Interpreting a `{}' chain with {} arguments, expected at least 2.", Language::And, argcount), node);
1✔
366

367
                for (std::size_t i = 1, end = node.list().size(); i < end; ++i)
3✔
368
                {
369
                    if (!isTruthy(evaluate(node.list()[i], depth + 1, is_not_body)))
2✔
370
                        return getFalseNode();
×
371
                }
2✔
372
                return getTrueNode();
1✔
373
            }
374
            else if (name == Language::Or && is_not_body)
3,856✔
375
            {
376
                if (node.list().size() < 3)
1✔
377
                    throwMacroProcessingError(fmt::format("Interpreting an `{}' chain with {} arguments, expected at least 2.", Language::Or, argcount), node);
1✔
378

379
                for (std::size_t i = 1, end = node.list().size(); i < end; ++i)
×
380
                {
381
                    if (isTruthy(evaluate(node.list()[i], depth + 1, is_not_body)))
×
382
                        return getTrueNode();
×
383
                }
×
384
                return getFalseNode();
×
385
            }
386
            else if (name == "len")
3,855✔
387
            {
388
                if (node.list().size() > 2)
51✔
389
                    throwMacroProcessingError(fmt::format("When expanding `len' inside a macro, got {} arguments, expected 1", argcount), node);
1✔
390
                if (Node& lst = node.list()[1]; lst.nodeType() == NodeType::List)  // only apply len at compile time if we can
97✔
391
                {
392
                    if (isConstEval(lst))
47✔
393
                    {
394
                        if (!lst.list().empty() && lst.list()[0] == getListNode())
32✔
395
                            node.updateValueAndType(Node(static_cast<long>(lst.list().size()) - 1));
32✔
396
                        else
397
                            node.updateValueAndType(Node(static_cast<long>(lst.list().size())));
×
398
                    }
32✔
399
                }
47✔
400
            }
50✔
401
            else if (name == "empty?")
3,804✔
402
            {
403
                if (node.list().size() > 2)
9✔
404
                    throwMacroProcessingError(fmt::format("When expanding `empty?' inside a macro, got {} arguments, expected 1", argcount), node);
1✔
405
                if (Node& lst = node.list()[1]; lst.nodeType() == NodeType::List && isConstEval(lst))
16✔
406
                {
407
                    // only apply len at compile time if we can
408
                    if (!lst.list().empty() && lst.list()[0] == getListNode())
5✔
409
                        node.updateValueAndType(lst.list().size() - 1 == 0 ? getTrueNode() : getFalseNode());
5✔
410
                    else
411
                        node.updateValueAndType(lst.list().empty() ? getTrueNode() : getFalseNode());
×
412
                }
5✔
413
                else if (lst == getNilNode())
3✔
414
                    node.updateValueAndType(getTrueNode());
×
415
            }
8✔
416
            else if (name == "@")
3,795✔
417
            {
418
                checkMacroArgCountEq(node, 2, "@");
28✔
419

420
                Node sublist = evaluate(node.list()[1], depth + 1, is_not_body);
28✔
421
                const Node idx = evaluate(node.list()[2], depth + 1, is_not_body);
28✔
422

423
                if (sublist.nodeType() == NodeType::List && idx.nodeType() == NodeType::Number)
28✔
424
                {
425
                    const std::size_t size = sublist.list().size();
10✔
426
                    std::size_t real_size = size;
10✔
427
                    long num_idx = static_cast<long>(idx.number());
10✔
428

429
                    // if the first node is the function call to "list", don't count it
430
                    if (size > 0 && sublist.list()[0] == getListNode())
10✔
431
                    {
432
                        real_size--;
10✔
433
                        if (num_idx >= 0)
10✔
434
                            ++num_idx;
4✔
435
                    }
10✔
436

437
                    Node output;
10✔
438
                    if (num_idx >= 0 && std::cmp_less(num_idx, size))
10✔
439
                        output = sublist.list()[static_cast<std::size_t>(num_idx)];
4✔
440
                    else if (const auto c = static_cast<long>(size) + num_idx; num_idx < 0 && std::cmp_less(c, size) && c >= 0)
12✔
441
                        output = sublist.list()[static_cast<std::size_t>(c)];
5✔
442
                    else
443
                        throwMacroProcessingError(fmt::format("Index ({}) out of range (list size: {})", num_idx, real_size), node);
1✔
444

445
                    output.setFilename(node.filename());
9✔
446
                    output.setPos(node.line(), node.col());
9✔
447
                    return output;
9✔
448
                }
10✔
449
            }
28✔
450
            else if (name == "head")
3,767✔
451
            {
452
                if (node.list().size() > 2)
9✔
453
                    throwMacroProcessingError(fmt::format("When expanding `head' inside a macro, got {} arguments, expected 1", argcount), node);
1✔
454
                if (node.list()[1].nodeType() == NodeType::List)
8✔
455
                {
456
                    Node& sublist = node.list()[1];
3✔
457
                    if (!sublist.constList().empty() && sublist.constList()[0] == getListNode())
3✔
458
                    {
459
                        if (sublist.constList().size() > 1)
3✔
460
                        {
461
                            const Node sublistCopy = sublist.constList()[1];
2✔
462
                            node.updateValueAndType(sublistCopy);
2✔
463
                        }
2✔
464
                        else
465
                            node.updateValueAndType(getNilNode());
1✔
466
                    }
3✔
467
                    else if (!sublist.list().empty())
×
468
                        node.updateValueAndType(sublist.constList()[0]);
×
469
                    else
470
                        node.updateValueAndType(getNilNode());
×
471
                }
3✔
472
            }
8✔
473
            else if (name == "tail")
3,758✔
474
            {
475
                if (node.list().size() > 2)
9✔
476
                    throwMacroProcessingError(fmt::format("When expanding `tail' inside a macro, got {} arguments, expected 1", argcount), node);
1✔
477
                if (node.list()[1].nodeType() == NodeType::List)
8✔
478
                {
479
                    Node sublist = node.list()[1];
3✔
480
                    if (!sublist.list().empty() && sublist.list()[0] == getListNode())
3✔
481
                    {
482
                        if (sublist.list().size() > 1)
3✔
483
                        {
484
                            sublist.list().erase(sublist.constList().begin() + 1);
2✔
485
                            node.updateValueAndType(sublist);
2✔
486
                        }
2✔
487
                        else
488
                        {
489
                            node.updateValueAndType(Node(NodeType::List));
1✔
490
                            node.push_back(getListNode());
1✔
491
                        }
492
                    }
3✔
493
                    else if (!sublist.list().empty())
×
494
                    {
495
                        sublist.list().erase(sublist.constList().begin());
×
496
                        sublist.list().insert(sublist.list().begin(), getListNode());
×
497
                        node.updateValueAndType(sublist);
×
498
                    }
×
499
                    else
500
                    {
501
                        node.updateValueAndType(Node(NodeType::List));
×
502
                        node.push_back(getListNode());
×
503
                    }
504
                }
3✔
505
            }
8✔
506
            else if (name == Language::Symcat)
3,749✔
507
            {
508
                if (node.list().size() <= 2)
54✔
509
                    throwMacroProcessingError(fmt::format("When expanding `{}', expected at least 2 arguments, got {} arguments", Language::Symcat, argcount), node);
1✔
510
                if (node.list()[1].nodeType() != NodeType::Symbol)
53✔
511
                    throwMacroProcessingError(
1✔
512
                        fmt::format(
2✔
513
                            "When expanding `{}', expected the first argument to be a Symbol, got a {}: {}",
1✔
514
                            Language::Symcat,
515
                            typeToString(node.list()[1]),
1✔
516
                            node.list()[1].repr()),
1✔
517
                        node);
1✔
518

519
                std::string sym = node.list()[1].string();
52✔
520

521
                for (std::size_t i = 2, end = node.list().size(); i < end; ++i)
104✔
522
                {
523
                    const Node ev = evaluate(node.list()[i], depth + 1, /* is_not_body */ true);
52✔
524

525
                    switch (ev.nodeType())
51✔
526
                    {
11✔
527
                        case NodeType::Number:
528
                            // we don't want '.' in identifiers
529
                            sym += std::to_string(static_cast<long int>(ev.number()));
11✔
530
                            break;
50✔
531

532
                        case NodeType::String:
533
                        case NodeType::Symbol:
534
                            sym += ev.string();
39✔
535
                            break;
40✔
536

537
                        default:
538
                            throwMacroProcessingError(
1✔
539
                                fmt::format(
2✔
540
                                    "When expanding `{}', expected either a Number, String or Symbol, got a {}: {}",
1✔
541
                                    Language::Symcat,
542
                                    typeToString(ev),
1✔
543
                                    ev.repr()),
1✔
544
                                ev);
545
                    }
50✔
546
                }
52✔
547

548
                node.setNodeType(NodeType::Symbol);
50✔
549
                node.setString(sym);
50✔
550
            }
52✔
551
            else if (name == Language::Argcount)
3,695✔
552
            {
553
                const Node sym = node.constList()[1];
32✔
554
                if (sym.nodeType() == NodeType::Symbol)
32✔
555
                {
556
                    if (const auto maybe_func = lookupDefinedFunction(sym.string()); maybe_func.has_value())
64✔
557
                        node.updateValueAndType(Node(static_cast<long>(maybe_func->constList().size())));
31✔
558
                    else
559
                        throwMacroProcessingError(fmt::format("When expanding `{}', expected a known function name, got unbound variable {}", Language::Argcount, sym.string()), sym);
1✔
560
                }
31✔
561
                else if (sym.nodeType() == NodeType::List && sym.constList().size() == 3 && sym.constList()[0].nodeType() == NodeType::Keyword && sym.constList()[0].keyword() == Keyword::Fun)
×
562
                    node.updateValueAndType(Node(static_cast<long>(sym.constList()[1].constList().size())));
×
563
                else
564
                    throwMacroProcessingError(fmt::format("When trying to apply `{}', got a {} instead of a Symbol or Function", Language::Argcount, typeToString(sym)), sym);
×
565
            }
32✔
566
            else if (name == Language::Repr)
3,663✔
567
            {
568
                const Node ast = node.constList()[1];
653✔
569
                node.updateValueAndType(Node(NodeType::String, ast.repr()));
653✔
570
            }
653✔
571
            else if (name == Language::Paste)
3,010✔
572
            {
573
                if (node.list().size() != 2)
1,240✔
574
                    throwMacroProcessingError(fmt::format("When expanding `{}', expected one argument, got {} arguments", Language::Paste, argcount), node);
1✔
575
                return node.constList()[1];
1,239✔
576
            }
577
            else if (name == Language::Undef)
1,770✔
578
            {
579
                if (node.list().size() != 2)
6✔
580
                    throwMacroProcessingError(fmt::format("When expanding `{}', expected one argument, got {} arguments", Language::Undef, argcount), node);
×
581

582
                const Node sym = node.constList()[1];
6✔
583
                if (sym.nodeType() == NodeType::Symbol)
6✔
584
                {
585
                    deleteNearestMacro(sym.string());
5✔
586
                    node.setNodeType(NodeType::Unused);
5✔
587
                    return node;
5✔
588
                }
589

590
                throwMacroProcessingError(
1✔
591
                    fmt::format(
2✔
592
                        "When expanding `{}', got a {}. Can not un-define a macro without a valid name",
1✔
593
                        Language::Undef, typeToString(sym)),
1✔
594
                    sym);
595
            }
6✔
596
        }
4,139✔
597

598
        if (node.nodeType() == NodeType::List && !node.constList().empty())
5,885✔
599
        {
600
            for (auto& child : node.list())
11,995✔
601
                child.updateValueAndType(evaluate(child, depth + 1, is_not_body));
8,962✔
602
        }
3,033✔
603

604
        if (node.nodeType() == NodeType::Spread)
5,885✔
605
            throwMacroProcessingError(fmt::format("Found an unevaluated spread: `{}'", node.string()), node);
1✔
606

607
        return node;
5,884✔
608
    }
9,836✔
609

610
    bool MacroProcessor::isTruthy(const Node& node)
59✔
611
    {
59✔
612
        if (node.nodeType() == NodeType::Symbol)
59✔
613
        {
614
            if (node.string() == "true")
57✔
615
                return true;
29✔
616
            if (node.string() == "false" || node.string() == "nil")
28✔
617
                return false;
28✔
618
        }
×
619
        else if ((node.nodeType() == NodeType::Number && node.number() != 0.0) || (node.nodeType() == NodeType::String && !node.string().empty()))
2✔
620
            return true;
×
621
        else if (node.nodeType() == NodeType::Spread)
2✔
622
            throwMacroProcessingError("Can not determine the truth value of a spreaded symbol", node);
×
623
        return false;
2✔
624
    }
59✔
625

626
    std::optional<Node> MacroProcessor::lookupDefinedFunction(const std::string& name) const
32✔
627
    {
32✔
628
        if (m_defined_functions.contains(name))
32✔
629
            return m_defined_functions.at(name);
31✔
630
        return std::nullopt;
1✔
631
    }
32✔
632

633
    const Node* MacroProcessor::findNearestMacro(const std::string& name) const
38,773✔
634
    {
38,773✔
635
        if (m_macros.empty())
38,773✔
636
            return nullptr;
8,685✔
637

638
        for (const auto& m_macro : std::ranges::reverse_view(m_macros))
67,456✔
639
        {
640
            if (const auto res = m_macro.has(name); res != nullptr)
37,368✔
641
                return res;
1,481✔
642
        }
37,368✔
643
        return nullptr;
28,607✔
644
    }
38,773✔
645

646
    void MacroProcessor::deleteNearestMacro(const std::string& name)
5✔
647
    {
5✔
648
        if (m_macros.empty())
5✔
649
            return;
×
650

651
        for (auto& m_macro : std::ranges::reverse_view(m_macros))
10✔
652
        {
653
            if (m_macro.remove(name))
5✔
654
            {
655
                // stop right here because we found one matching macro
656
                return;
×
657
            }
658
        }
5✔
659
    }
5✔
660

661
    bool MacroProcessor::isBeginNode(const Node& node)
237,513✔
662
    {
237,513✔
663
        return node.nodeType() == NodeType::List &&
272,915✔
664
            !node.constList().empty() &&
35,402✔
665
            node.constList()[0].nodeType() == NodeType::Keyword &&
45,593✔
666
            node.constList()[0].keyword() == Keyword::Begin;
10,331✔
667
    }
668

669
    void MacroProcessor::removeBegin(Node& node, std::size_t i)
424✔
670
    {
424✔
671
        if (node.isListLike() && node.list()[i].nodeType() == NodeType::List && !node.list()[i].list().empty())
424✔
672
        {
673
            Node lst = node.constList()[i];
424✔
674
            Node first = lst.constList()[0];
424✔
675

676
            if (first.nodeType() == NodeType::Keyword && first.keyword() == Keyword::Begin)
424✔
677
            {
678
                const std::size_t previous = i;
424✔
679

680
                for (std::size_t block_idx = 1, end = lst.constList().size(); block_idx < end; ++block_idx)
992✔
681
                    node.list().insert(
1,136✔
682
                        node.constList().begin() + static_cast<std::vector<Node>::difference_type>(i + block_idx),
568✔
683
                        lst.list()[block_idx]);
568✔
684

685
                node.list().erase(node.constList().begin() + static_cast<std::vector<Node>::difference_type>(previous));
424✔
686
            }
424✔
687
        }
424✔
688
    }
424✔
689

690
    bool MacroProcessor::isConstEval(const Node& node) const
245✔
691
    {
245✔
692
        switch (node.nodeType())
245✔
693
        {
125✔
694
            case NodeType::Symbol:
695
            {
696
                const auto it = std::ranges::find(Language::operators, node.string());
125✔
697
                const auto it2 = std::ranges::find_if(Builtins::builtins,
125✔
698
                                                      [&node](const std::pair<std::string, Value>& element) -> bool {
5,545✔
699
                                                          return node.string() == element.first;
5,420✔
700
                                                      });
701

702
                return it != Language::operators.end() ||
249✔
703
                    it2 != Builtins::builtins.end() ||
124✔
704
                    findNearestMacro(node.string()) != nullptr ||
95✔
705
                    node.string() == "list" ||
91✔
706
                    node.string() == "nil";
17✔
707
            }
204✔
708

709
            case NodeType::List:
710
                return std::ranges::all_of(node.constList(), [this](const Node& child) {
270✔
711
                    return isConstEval(child);
191✔
712
                });
713

714
            case NodeType::Capture:
715
            case NodeType::Field:
716
                return false;
41✔
717

718
            case NodeType::Keyword:
719
            case NodeType::String:
720
            case NodeType::Number:
721
            case NodeType::Macro:
722
            case NodeType::Spread:
723
            case NodeType::Namespace:
724
            case NodeType::Unused:
725
                return true;
41✔
726
        }
×
727

728
        return false;
×
729
    }
245✔
730

731
    void MacroProcessor::throwMacroProcessingError(const std::string& message, const Node& node)
21✔
732
    {
21✔
733
        throw CodeError(message, node.filename(), node.line(), node.col(), node.repr());
21✔
734
    }
21✔
735
}
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