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

ArkScript-lang / Ark / 20065764942

09 Dec 2025 01:49PM UTC coverage: 90.564% (+0.008%) from 90.556%
20065764942

push

github

SuperFola
chore(tests): adding IR generation tests for arg attributes

8043 of 8881 relevant lines covered (90.56%)

180911.91 hits per line

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

99.25
/src/arkreactor/Compiler/NameResolution/NameResolutionPass.cpp
1
#include <Ark/Compiler/NameResolution/NameResolutionPass.hpp>
2

3
#include <Ark/Error/Exceptions.hpp>
4
#include <Ark/Utils/Utils.hpp>
5
#include <Ark/Builtins/Builtins.hpp>
6

7
namespace Ark::internal
8
{
9
    NameResolutionPass::NameResolutionPass(const unsigned debug) :
690✔
10
        Pass("NameResolution", debug)
345✔
11
    {
690✔
12
        for (const auto& builtin : Builtins::builtins)
23,460✔
13
            m_language_symbols.emplace(builtin.first);
23,115✔
14
        for (auto ope : Language::operators)
8,625✔
15
            m_language_symbols.emplace(ope);
8,280✔
16
        for (auto inst : Language::listInstructions)
3,450✔
17
            m_language_symbols.emplace(inst);
3,105✔
18

19
        m_language_symbols.emplace(Language::And);
345✔
20
        m_language_symbols.emplace(Language::Or);
345✔
21
        m_language_symbols.emplace(Language::SysArgs);
345✔
22
        m_language_symbols.emplace(Language::SysProgramName);
345✔
23
    }
345✔
24

25
    void NameResolutionPass::process(const Node& ast)
262✔
26
    {
262✔
27
        m_logger.traceStart("process");
262✔
28

29
        m_ast = ast;
262✔
30
        visit(m_ast, /* register_declarations= */ true);
262✔
31

32
        m_logger.traceEnd();
262✔
33

34
        m_logger.trace("AST after name resolution");
262✔
35
        if (m_logger.shouldTrace())
262✔
36
            m_ast.debugPrint(std::cout) << '\n';
1✔
37

38
        m_logger.traceStart("checkForUndefinedSymbol");
262✔
39
        checkForUndefinedSymbol();
262✔
40
        m_logger.traceEnd();
262✔
41
    }
262✔
42

43
    const Node& NameResolutionPass::ast() const noexcept
259✔
44
    {
259✔
45
        return m_ast;
259✔
46
    }
47

48
    std::string NameResolutionPass::addDefinedSymbol(const std::string& sym, const bool is_mutable)
33,310✔
49
    {
33,310✔
50
        const std::string fully_qualified_name = m_scope_resolver.registerInCurrent(sym, is_mutable);
33,310✔
51
        m_defined_symbols.emplace(fully_qualified_name);
33,310✔
52
        return fully_qualified_name;
33,310✔
53
    }
33,310✔
54

55
    void NameResolutionPass::visit(Node& node, const bool register_declarations)
300,258✔
56
    {
300,258✔
57
        switch (node.nodeType())
300,258✔
58
        {
91,666✔
59
            case NodeType::Symbol:
60
            {
61
                const std::string old_name = node.string();
91,666✔
62
                updateSymbolWithFullyQualifiedName(node);
91,666✔
63
                addSymbolNode(node, old_name);
91,664✔
64
                break;
65
            }
96,364✔
66

67
            case NodeType::Field:
68
                for (std::size_t i = 0, end = node.list().size(); i < end; ++i)
14,083✔
69
                {
70
                    Node& child = node.list()[i];
9,393✔
71

72
                    if (i == 0)
9,393✔
73
                    {
74
                        const std::string old_name = child.string();
4,690✔
75
                        // in case of field, no need to check if we can fully qualify names
76
                        child.setString(m_scope_resolver.getFullyQualifiedNameInNearestScope(old_name));
4,690✔
77
                        addSymbolNode(child, old_name);
4,690✔
78
                    }
4,690✔
79
                    else
80
                        addSymbolNode(child);
4,703✔
81
                }
9,393✔
82
                break;
99,829✔
83

84
            case NodeType::List:
85
                if (!node.constList().empty())
95,139✔
86
                {
87
                    if (node.constList()[0].nodeType() == NodeType::Keyword)
95,112✔
88
                        visitKeyword(node, node.constList()[0].keyword(), register_declarations);
45,701✔
89
                    else
90
                    {
91
                        // function calls
92
                        // the UpdateRef function calls kind get a special treatment, like let/mut/set,
93
                        // because we need to check for mutability errors
94
                        if (node.constList().size() > 1 && node.constList()[0].nodeType() == NodeType::Symbol &&
73,992✔
95
                            node.constList()[1].nodeType() == NodeType::Symbol && register_declarations)
43,437✔
96
                        {
97
                            const auto funcname = node.constList()[0].string();
12,521✔
98
                            const auto arg = node.constList()[1].string();
12,521✔
99

100
                            if (std::ranges::find(Language::UpdateRef, funcname) != Language::UpdateRef.end() && m_scope_resolver.isImmutable(arg).value_or(false))
12,521✔
101
                                throw CodeError(
10✔
102
                                    fmt::format("MutabilityError: Can not modify the constant list `{}' using `{}'", arg, funcname),
5✔
103
                                    CodeErrorContext(node.filename(), node.constList()[1].position()));
5✔
104

105
                            // check that we aren't doing a (append! a a) nor a (concat! a a)
106
                            if (funcname == Language::AppendInPlace || funcname == Language::ConcatInPlace)
12,516✔
107
                            {
108
                                for (std::size_t i = 2, end = node.constList().size(); i < end; ++i)
1,362✔
109
                                {
110
                                    if (node.constList()[i].nodeType() == NodeType::Symbol && node.constList()[i].string() == arg)
695✔
111
                                        throw CodeError(
4✔
112
                                            fmt::format("MutabilityError: Can not {} the list `{}' to itself", funcname, arg),
2✔
113
                                            CodeErrorContext(node.filename(), node.constList()[1].position()));
2✔
114
                                }
693✔
115
                            }
665✔
116
                        }
12,521✔
117

118
                        for (auto& child : node.list())
252,437✔
119
                            visit(child, register_declarations);
203,033✔
120
                    }
121
                }
95,105✔
122
                break;
95,322✔
123

124
            case NodeType::Namespace:
125
            {
126
                auto& namespace_ = node.arkNamespace();
190✔
127
                // no need to guard createNewNamespace with an if (register_declarations), we want to keep the namespace node
128
                // (which will get ignored by the compiler, that only uses its AST), so that we can (re)construct the
129
                // scopes correctly
130
                m_scope_resolver.createNewNamespace(namespace_.name, namespace_.with_prefix, namespace_.is_glob, namespace_.symbols);
190✔
131
                StaticScope* scope = m_scope_resolver.currentScope();
190✔
132

133
                visit(*namespace_.ast, /* register_declarations= */ true);
190✔
134
                // dual visit so that we can handle forward references
135
                visit(*namespace_.ast, /* register_declarations= */ false);
190✔
136

137
                // if we had specific symbols to import, check that those exist
138
                if (!namespace_.symbols.empty())
190✔
139
                {
140
                    const auto it = std::ranges::find_if(
270✔
141
                        namespace_.symbols,
90✔
142
                        [&scope, &namespace_](const std::string& sym) -> bool {
287✔
143
                            return !scope->get(sym, namespace_.name, true).has_value();
197✔
144
                        });
145

146
                    if (it != namespace_.symbols.end())
90✔
147
                        throw CodeError(
2✔
148
                            fmt::format("ImportError: Can not import symbol {} from {}, as it isn't in the package", *it, namespace_.name),
1✔
149
                            CodeErrorContext(namespace_.ast->filename(), namespace_.ast->position()));
1✔
150
                }
90✔
151

152
                m_scope_resolver.saveNamespaceAndRemove();
189✔
153
                break;
154
            }
108,763✔
155

156
            default:
157
                break;
108,573✔
158
        }
300,194✔
159
    }
300,204✔
160

161
    void NameResolutionPass::visitKeyword(Node& node, const Keyword keyword, const bool register_declarations)
45,701✔
162
    {
45,701✔
163
        switch (keyword)
45,701✔
164
        {
22,185✔
165
            case Keyword::Set:
166
                [[fallthrough]];
167
            case Keyword::Let:
168
                [[fallthrough]];
169
            case Keyword::Mut:
170
                // first, visit the value, then register the symbol
171
                // this allows us to detect things like (let foo (fun (&foo) ()))
172
                if (node.constList().size() > 2)
22,185✔
173
                    visit(node.list()[2], register_declarations);
22,184✔
174
                if (node.constList().size() > 1 && node.constList()[1].nodeType() == NodeType::Symbol)
22,185✔
175
                {
176
                    const std::string& name = node.constList()[1].string();
22,183✔
177
                    if (m_language_symbols.contains(name) && register_declarations)
22,183✔
178
                        throw CodeError(
6✔
179
                            fmt::format("Can not use a reserved identifier ('{}') as a {} name.", name, keyword == Keyword::Let ? "constant" : "variable"),
3✔
180
                            CodeErrorContext(node.filename(), node.constList()[1].position()));
3✔
181

182
                    if (m_scope_resolver.isInScope(name) && keyword == Keyword::Let && register_declarations)
22,180✔
183
                        throw CodeError(
2✔
184
                            fmt::format("MutabilityError: Can not use 'let' to redefine variable `{}'", name),
1✔
185
                            CodeErrorContext(node.filename(), node.constList()[1].position()));
1✔
186
                    if (keyword == Keyword::Set && m_scope_resolver.isRegistered(name))
22,179✔
187
                    {
188
                        if (m_scope_resolver.isImmutable(name).value_or(false) && register_declarations)
4,483✔
189
                            throw CodeError(
2✔
190
                                fmt::format("MutabilityError: Can not set the constant `{}' to {}", name, node.constList()[2].repr()),
1✔
191
                                CodeErrorContext(node.filename(), node.constList()[1].position()));
1✔
192

193
                        updateSymbolWithFullyQualifiedName(node.list()[1]);
4,482✔
194
                    }
4,482✔
195
                    else if (keyword != Keyword::Set)
17,696✔
196
                    {
197
                        // update the declared variable name to use the fully qualified name
198
                        // this will prevent name conflicts, and handle scope resolution
199
                        const std::string fully_qualified_name = addDefinedSymbol(name, keyword != Keyword::Let);
17,693✔
200
                        if (register_declarations)
17,693✔
201
                            node.list()[1].setString(fully_qualified_name);
9,023✔
202
                    }
17,693✔
203
                }
22,185✔
204
                break;
22,183✔
205

206
            case Keyword::Import:
207
                if (!node.constList().empty())
3✔
208
                    m_plugin_names.push_back(node.constList()[1].constList().back().string());
3✔
209
                break;
2,392✔
210

211
            case Keyword::While:
212
                // create a new scope to track variables
213
                m_scope_resolver.createNew();
2,389✔
214
                for (auto& child : node.list())
9,555✔
215
                    visit(child, register_declarations);
7,166✔
216
                // remove the scope once the loop has been compiled, only we were registering declarations
217
                m_scope_resolver.removeLastScope();
2,389✔
218
                break;
11,949✔
219

220
            case Keyword::Fun:
221
                // create a new scope to track variables
222
                m_scope_resolver.createNew();
9,560✔
223

224
                if (node.constList()[1].nodeType() == NodeType::List)
9,560✔
225
                {
226
                    for (auto& child : node.list()[1].list())
24,544✔
227
                    {
228
                        if (child.nodeType() == NodeType::Capture)
14,985✔
229
                        {
230
                            if (!m_scope_resolver.isRegistered(child.string()) && register_declarations)
684✔
231
                                throw CodeError(
4✔
232
                                    fmt::format("Can not capture `{}' because it is referencing a variable defined in an unreachable scope.", child.string()),
2✔
233
                                    CodeErrorContext(child.filename(), child.position()));
2✔
234

235
                            // save the old unqualified name of the capture, so that we can use it in the
236
                            // ASTLowerer later one
237
                            if (!child.getUnqualifiedName())
682✔
238
                            {
239
                                child.setUnqualifiedName(child.string());
227✔
240
                                m_defined_symbols.emplace(child.string());
227✔
241
                            }
227✔
242
                            // update the declared variable name to use the fully qualified name
243
                            // this will prevent name conflicts, and handle scope resolution
244
                            std::string old_name = child.string();
682✔
245
                            updateSymbolWithFullyQualifiedName(child);
682✔
246
                            addDefinedSymbol(old_name, true);
682✔
247
                        }
682✔
248
                        else if (child.nodeType() == NodeType::Symbol || child.nodeType() == NodeType::RefArg)
14,301✔
249
                            addDefinedSymbol(child.string(), /* is_mutable= */ false);
14,161✔
250
                        else if (child.nodeType() == NodeType::MutArg)
140✔
251
                            addDefinedSymbol(child.string(), /* is_mutable= */ true);
140✔
252
                    }
14,985✔
253
                }
9,557✔
254
                if (node.constList().size() > 2)
9,558✔
255
                    visit(node.list()[2], register_declarations);
9,557✔
256

257
                // remove the scope once the function has been compiled, only we were registering declarations
258
                m_scope_resolver.removeLastScope();
9,558✔
259
                break;
21,122✔
260

261
            default:
262
                for (auto& child : node.list())
69,200✔
263
                    visit(child, register_declarations);
57,636✔
264
                break;
11,564✔
265
        }
45,694✔
266
    }
45,701✔
267

268
    void NameResolutionPass::addSymbolNode(const Node& symbol, const std::string& old_name)
101,057✔
269
    {
101,057✔
270
        const std::string& name = symbol.string();
101,057✔
271

272
        // we don't accept builtins/operators as a user symbol
273
        if (m_language_symbols.contains(name))
101,057✔
274
            return;
40,122✔
275

276
        // remove the old name node, to avoid false positive when looking for unbound symbols
277
        if (!old_name.empty())
60,935✔
278
        {
279
            auto it = std::ranges::find_if(m_symbol_nodes, [&old_name, &symbol](const Node& sym_node) -> bool {
6,816,312✔
280
                return sym_node.string() == old_name &&
6,811,600✔
281
                    sym_node.position().start == symbol.position().start &&
64,963✔
282
                    sym_node.filename() == symbol.filename();
13,443✔
283
            });
284
            if (it != m_symbol_nodes.end())
56,232✔
285
            {
286
                it->setString(name);
13,443✔
287
                return;
13,443✔
288
            }
289
        }
56,232✔
290

291
        const auto it = std::ranges::find_if(m_symbol_nodes, [&name](const Node& sym_node) -> bool {
3,134,769✔
292
            return sym_node.string() == name;
3,087,277✔
293
        });
294
        if (it == m_symbol_nodes.end())
47,492✔
295
            m_symbol_nodes.push_back(symbol);
3,974✔
296
    }
101,057✔
297

298
    bool NameResolutionPass::mayBeFromPlugin(const std::string& name) const noexcept
3,970✔
299
    {
3,970✔
300
        std::string splitted = Utils::splitString(name, ':')[0];
3,970✔
301
        const auto it = std::ranges::find_if(
3,970✔
302
            m_plugin_names,
3,970✔
303
            [&splitted](const std::string& plugin) -> bool {
4,438✔
304
                return plugin == splitted;
468✔
305
            });
306
        return it != m_plugin_names.end();
3,970✔
307
    }
3,970✔
308

309
    std::string NameResolutionPass::updateSymbolWithFullyQualifiedName(Node& symbol)
96,830✔
310
    {
96,830✔
311
        auto [allowed, fqn] = m_scope_resolver.canFullyQualifyName(symbol.string());
427,443✔
312

313
        if (m_language_symbols.contains(fqn) && symbol.string() != fqn)
96,830✔
314
        {
315
            throw CodeError(
2✔
316
                fmt::format(
1✔
317
                    "Symbol `{}' was resolved to `{}', which is also a builtin name. Either the symbol or the package it's in needs to be renamed to avoid conflicting with the builtin.",
1✔
318
                    symbol.string(), fqn),
1✔
319
                CodeErrorContext(symbol.filename(), symbol.position()));
1✔
320
        }
321
        if (!allowed)
96,829✔
322
        {
323
            std::string message;
1✔
324
            if (fqn.ends_with("#hidden"))
1✔
325
                message = fmt::format(
2✔
326
                    R"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to add it to the symbol list while importing?)",
1✔
327
                    symbol.string(),
1✔
328
                    fqn.substr(0, fqn.find_first_of('#')));
2✔
329
            else
330
                message = fmt::format(R"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to prefix it with its namespace?)", symbol.string(), fqn);
×
331

332
            if (m_logger.shouldTrace())
1✔
333
                m_ast.debugPrint(std::cout) << '\n';
×
334

335
            throw CodeError(message, CodeErrorContext(symbol.filename(), symbol.position()));
1✔
336
        }
1✔
337

338
        symbol.setString(fqn);
96,828✔
339
        return fqn;
96,828✔
340
    }
96,832✔
341

342
    void NameResolutionPass::checkForUndefinedSymbol() const
262✔
343
    {
262✔
344
        for (const auto& sym : m_symbol_nodes)
4,232✔
345
        {
346
            const auto& str = sym.string();
3,970✔
347
            const bool is_plugin = mayBeFromPlugin(str);
3,970✔
348

349
            if (!m_defined_symbols.contains(str) && !is_plugin)
3,970✔
350
            {
351
                std::string message;
3✔
352

353
                const std::string suggestion = offerSuggestion(str);
3✔
354
                if (suggestion.empty())
3✔
355
                    message = fmt::format(R"(Unbound variable error "{}" (variable is used but not defined))", str);
1✔
356
                else
357
                {
358
                    const std::string prefix = suggestion.substr(0, suggestion.find_first_of(':'));
2✔
359
                    const std::string note_about_prefix = fmt::format(
4✔
360
                        " You either forgot to import it in the symbol list (eg `(import {} :{})') or need to fully qualify it by adding the namespace",
2✔
361
                        prefix,
362
                        str);
2✔
363
                    const bool add_note = suggestion.ends_with(":" + str);
2✔
364
                    message = fmt::format(R"(Unbound variable error "{}" (did you mean "{}"?{}))", str, suggestion, add_note ? note_about_prefix : "");
2✔
365
                }
2✔
366

367
                throw CodeError(message, CodeErrorContext(sym.filename(), sym.position()));
3✔
368
            }
3✔
369
        }
3,970✔
370
    }
262✔
371

372
    std::string NameResolutionPass::offerSuggestion(const std::string& str) const
3✔
373
    {
3✔
374
        auto iterate = [](const std::string& word, const std::unordered_set<std::string>& dict) -> std::string {
8✔
375
            std::string suggestion;
5✔
376
            // our suggestion shouldn't require more than half the string to change
377
            std::size_t suggestion_distance = word.size() / 2;
5✔
378
            for (const std::string& symbol : dict)
223✔
379
            {
380
                const std::size_t current_distance = Utils::levenshteinDistance(word, symbol);
218✔
381
                if (current_distance <= suggestion_distance)
218✔
382
                {
383
                    suggestion_distance = current_distance;
1✔
384
                    suggestion = symbol;
1✔
385
                }
1✔
386
            }
218✔
387
            return suggestion;
5✔
388
        };
5✔
389

390
        std::string suggestion = iterate(str, m_defined_symbols);
3✔
391
        // look for a suggestion related to language builtins
392
        if (suggestion.empty())
3✔
393
            suggestion = iterate(str, m_language_symbols);
2✔
394
        // look for a suggestion related to a namespace change
395
        if (suggestion.empty())
3✔
396
        {
397
            if (const auto it = std::ranges::find_if(m_defined_symbols, [&str](const std::string& symbol) {
8✔
398
                    return symbol.ends_with(":" + str);
4✔
399
                });
400
                it != m_defined_symbols.end())
3✔
401
                suggestion = *it;
1✔
402
        }
2✔
403

404
        return suggestion;
3✔
405
    }
3✔
406
}
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