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

ArkScript-lang / Ark / 17100280902

20 Aug 2025 01:45PM UTC coverage: 87.348% (-0.08%) from 87.426%
17100280902

push

github

SuperFola
feat(tests): adding parser tests to improve coverage

4 of 4 new or added lines in 1 file covered. (100.0%)

171 existing lines in 10 files now uncovered.

7553 of 8647 relevant lines covered (87.35%)

129750.62 hits per line

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

96.96
/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) :
610✔
10
        Pass("NameResolution", debug)
305✔
11
    {
610✔
12
        for (const auto& builtin : Builtins::builtins)
20,130✔
13
            m_language_symbols.emplace(builtin.first);
19,825✔
14
        for (auto ope : Language::operators)
7,625✔
15
            m_language_symbols.emplace(ope);
7,320✔
16
        for (auto inst : Language::listInstructions)
3,050✔
17
            m_language_symbols.emplace(inst);
2,745✔
18

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

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

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

32
        m_logger.traceEnd();
233✔
33

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

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

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

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

55
    void NameResolutionPass::visit(Node& node, const bool register_declarations)
245,201✔
56
    {
245,201✔
57
        switch (node.nodeType())
245,201✔
58
        {
70,331✔
59
            case NodeType::Symbol:
60
            {
61
                const std::string old_name = node.string();
70,331✔
62
                updateSymbolWithFullyQualifiedName(node);
70,331✔
63
                addSymbolNode(node, old_name);
70,329✔
64
                break;
65
            }
74,491✔
66

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

72
                    if (i == 0)
8,317✔
73
                    {
74
                        const std::string old_name = child.string();
4,152✔
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,152✔
77
                        addSymbolNode(child, old_name);
4,152✔
78
                    }
4,152✔
79
                    else
80
                        addSymbolNode(child);
4,165✔
81
                }
8,317✔
82
                break;
78,246✔
83

84
            case NodeType::List:
85
                if (!node.constList().empty())
74,094✔
86
                {
87
                    if (node.constList()[0].nodeType() == NodeType::Keyword)
74,067✔
88
                        visitKeyword(node, node.constList()[0].keyword(), register_declarations);
35,452✔
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 &&
57,079✔
95
                            node.constList()[1].nodeType() == NodeType::Symbol && register_declarations)
33,587✔
96
                        {
97
                            const auto funcname = node.constList()[0].string();
9,435✔
98
                            const auto arg = node.constList()[1].string();
9,435✔
99

100
                            if (std::ranges::find(Language::UpdateRef, funcname) != Language::UpdateRef.end() && m_scope_resolver.isImmutable(arg).value_or(false))
9,435✔
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)
9,430✔
107
                            {
108
                                for (std::size_t i = 2, end = node.constList().size(); i < end; ++i)
1,081✔
109
                                {
110
                                    if (node.constList()[i].nodeType() == NodeType::Symbol && node.constList()[i].string() == arg)
541✔
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
                                }
539✔
115
                            }
538✔
116
                        }
9,435✔
117

118
                        for (auto& child : node.list())
208,157✔
119
                            visit(child, register_declarations);
169,549✔
120
                    }
121
                }
74,060✔
122
                break;
74,262✔
123

124
            case NodeType::Namespace:
125
            {
126
                auto& namespace_ = node.arkNamespace();
175✔
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);
175✔
131
                StaticScope* scope = m_scope_resolver.currentScope();
175✔
132

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

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

146
                    if (it != namespace_.symbols.end())
84✔
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
                }
84✔
151

152
                m_scope_resolver.saveNamespaceAndRemove();
174✔
153
                break;
154
            }
96,624✔
155

156
            default:
157
                break;
96,449✔
158
        }
245,137✔
159
    }
245,147✔
160

161
    void NameResolutionPass::visitKeyword(Node& node, const Keyword keyword, const bool register_declarations)
35,452✔
162
    {
35,452✔
163
        switch (keyword)
35,452✔
164
        {
16,966✔
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)
16,966✔
173
                    visit(node.list()[2], register_declarations);
16,965✔
174
                if (node.constList().size() > 1 && node.constList()[1].nodeType() == NodeType::Symbol)
16,966✔
175
                {
176
                    const std::string& name = node.constList()[1].string();
16,964✔
177
                    if (m_language_symbols.contains(name) && register_declarations)
16,964✔
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)
16,961✔
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))
16,960✔
187
                    {
188
                        if (m_scope_resolver.isImmutable(name).value_or(false) && register_declarations)
3,268✔
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]);
3,267✔
194
                    }
3,267✔
195
                    else if (keyword != Keyword::Set)
13,692✔
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);
13,689✔
200
                        if (register_declarations)
13,689✔
201
                            node.list()[1].setString(fully_qualified_name);
6,999✔
202
                    }
13,689✔
203
                }
16,966✔
204
                break;
16,964✔
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;
1,748✔
210

211
            case Keyword::While:
212
                // create a new scope to track variables
213
                m_scope_resolver.createNew();
1,745✔
214
                for (auto& child : node.list())
6,979✔
215
                    visit(child, register_declarations);
5,234✔
216
                // remove the scope once the loop has been compiled, only we were registering declarations
217
                m_scope_resolver.removeLastScope();
1,745✔
218
                break;
9,414✔
219

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

224
                if (node.constList()[1].nodeType() == NodeType::List)
7,669✔
225
                {
226
                    for (auto& child : node.list()[1].list())
19,596✔
227
                    {
228
                        if (child.nodeType() == NodeType::Capture)
11,928✔
229
                        {
230
                            if (!m_scope_resolver.isRegistered(child.string()) && register_declarations)
658✔
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())
656✔
238
                            {
239
                                child.setUnqualifiedName(child.string());
214✔
240
                                m_defined_symbols.emplace(child.string());
214✔
241
                            }
214✔
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();
656✔
245
                            updateSymbolWithFullyQualifiedName(child);
656✔
246
                            // FIXME: addDefinedSymbol(fqn, true); ?
247
                            addDefinedSymbol(old_name, true);
656✔
248
                        }
656✔
249
                        else if (child.nodeType() == NodeType::Symbol)
11,270✔
250
                            addDefinedSymbol(child.string(), /* is_mutable= */ true);
11,270✔
251
                    }
11,928✔
252
                }
7,666✔
253
                if (node.constList().size() > 2)
7,667✔
254
                    visit(node.list()[2], register_declarations);
7,666✔
255

256
                // remove the scope once the function has been compiled, only we were registering declarations
257
                m_scope_resolver.removeLastScope();
7,667✔
258
                break;
16,736✔
259

260
            default:
261
                for (auto& child : node.list())
54,232✔
262
                    visit(child, register_declarations);
45,163✔
263
                break;
9,069✔
264
        }
35,445✔
265
    }
35,452✔
266

267
    void NameResolutionPass::addSymbolNode(const Node& symbol, const std::string& old_name)
78,646✔
268
    {
78,646✔
269
        const std::string& name = symbol.string();
78,646✔
270

271
        // we don't accept builtins/operators as a user symbol
272
        if (m_language_symbols.contains(name))
78,646✔
273
            return;
31,367✔
274

275
        // remove the old name node, to avoid false positive when looking for unbound symbols
276
        if (!old_name.empty())
47,279✔
277
        {
278
            auto it = std::ranges::find_if(m_symbol_nodes, [&old_name, &symbol](const Node& sym_node) -> bool {
4,602,121✔
279
                return sym_node.string() == old_name &&
4,598,708✔
280
                    sym_node.position().start == symbol.position().start &&
50,975✔
281
                    sym_node.filename() == symbol.filename();
11,274✔
282
            });
283
            if (it != m_symbol_nodes.end())
43,114✔
284
            {
285
                it->setString(name);
11,274✔
286
                return;
11,274✔
287
            }
288
        }
43,114✔
289

290
        const auto it = std::ranges::find_if(m_symbol_nodes, [&name](const Node& sym_node) -> bool {
2,101,501✔
291
            return sym_node.string() == name;
2,065,496✔
292
        });
293
        if (it == m_symbol_nodes.end())
36,005✔
294
            m_symbol_nodes.push_back(symbol);
2,929✔
295
    }
78,646✔
296

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

308
    std::string NameResolutionPass::updateSymbolWithFullyQualifiedName(Node& symbol)
74,254✔
309
    {
74,254✔
310
        auto [allowed, fqn] = m_scope_resolver.canFullyQualifyName(symbol.string());
328,385✔
311

312
        if (m_language_symbols.contains(fqn) && symbol.string() != fqn)
74,254✔
313
        {
UNCOV
314
            throw CodeError(
×
UNCOV
315
                fmt::format(
×
UNCOV
316
                    "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.",
×
UNCOV
317
                    symbol.string(), fqn),
×
UNCOV
318
                CodeErrorContext(symbol.filename(), symbol.position()));
×
319
        }
320
        if (!allowed)
74,254✔
321
        {
322
            std::string message;
2✔
323
            if (fqn.ends_with("#hidden"))
2✔
324
                message = fmt::format(
2✔
325
                    R"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to add it to the symbol list while importing?)",
1✔
326
                    symbol.string(),
1✔
327
                    fqn.substr(0, fqn.find_first_of('#')));
2✔
328
            else
329
                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);
1✔
330

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

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

337
        symbol.setString(fqn);
74,252✔
338
        return fqn;
74,252✔
339
    }
74,256✔
340

341
    void NameResolutionPass::checkForUndefinedSymbol() const
233✔
342
    {
233✔
343
        for (const auto& sym : m_symbol_nodes)
3,158✔
344
        {
345
            const auto& str = sym.string();
2,925✔
346
            const bool is_plugin = mayBeFromPlugin(str);
2,925✔
347

348
            if (!m_defined_symbols.contains(str) && !is_plugin)
2,925✔
349
            {
350
                std::string message;
2✔
351

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

366
                throw CodeError(message, CodeErrorContext(sym.filename(), sym.position()));
2✔
367
            }
2✔
368
        }
2,925✔
369
    }
233✔
370

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

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

403
        return suggestion;
2✔
404
    }
2✔
405
}
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