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

ArkScript-lang / Ark / 12206460859

06 Dec 2024 09:32PM UTC coverage: 77.864% (+0.05%) from 77.815%
12206460859

push

github

SuperFola
refactor(name resolution): removing dead code and refactoring code when assigning names to symbols in namespaces

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

24 existing lines in 5 files now uncovered.

5438 of 6984 relevant lines covered (77.86%)

9338.42 hits per line

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

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

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

7
namespace Ark::internal
8
{
9
    NameResolutionPass::NameResolutionPass(const unsigned debug) :
232✔
10
        Pass("NameResolution", debug),
116✔
11
        m_ast()
116✔
12
    {
232✔
13
        for (const auto& builtin : Builtins::builtins)
6,496✔
14
            m_language_symbols.emplace(builtin.first);
6,380✔
15
        for (auto ope : Language::operators)
2,784✔
16
            m_language_symbols.emplace(ope);
2,668✔
17
        for (auto inst : Language::listInstructions)
928✔
18
            m_language_symbols.emplace(inst);
812✔
19

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

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

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

32
        m_logger.traceEnd();
54✔
33

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

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

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

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

55
    void NameResolutionPass::visit(Node& node, const bool register_declarations)
21,824✔
56
    {
21,824✔
57
        switch (node.nodeType())
21,824✔
58
        {
7,397✔
59
            case NodeType::Symbol:
60
            {
61
                const std::string old_name = node.string();
7,397✔
62
                updateSymbolWithFullyQualifiedName(node);
7,397✔
63
                addSymbolNode(node, old_name);
7,395✔
64
                break;
65
            }
8,059✔
66

67
            case NodeType::Field:
68
                for (auto& child : node.list())
1,984✔
69
                {
70
                    const std::string old_name = child.string();
1,327✔
71
                    // in case of field, no need to check if we can fully qualify names
72
                    child.setString(m_scope_resolver.getFullyQualifiedNameInNearestScope(old_name));
1,327✔
73
                    addSymbolNode(child, old_name);
1,327✔
74
                }
1,327✔
75
                break;
8,832✔
76

77
            case NodeType::List:
78
                if (!node.constList().empty())
8,175✔
79
                {
80
                    if (node.constList()[0].nodeType() == NodeType::Keyword)
8,154✔
81
                        visitKeyword(node, node.constList()[0].keyword(), register_declarations);
2,977✔
82
                    else
83
                    {
84
                        // function calls
85
                        // the UpdateRef function calls kind get a special treatment, like let/mut/set,
86
                        // because we need to check for mutability errors
87
                        if (node.constList().size() > 1 && node.constList()[0].nodeType() == NodeType::Symbol &&
6,590✔
88
                            node.constList()[1].nodeType() == NodeType::Symbol && register_declarations)
3,692✔
89
                        {
90
                            const auto funcname = node.constList()[0].string();
732✔
91
                            const auto arg = node.constList()[1].string();
732✔
92

93
                            if (std::ranges::find(Language::UpdateRef, funcname) != Language::UpdateRef.end() && m_scope_resolver.isImmutable(arg).value_or(false))
732✔
94
                                throw CodeError(
6✔
95
                                    fmt::format("MutabilityError: Can not modify the constant list `{}' using `{}'", arg, funcname),
3✔
96
                                    node.filename(),
3✔
97
                                    node.constList()[1].line(),
3✔
98
                                    node.constList()[1].col(),
3✔
99
                                    arg);
3✔
100

101
                            // check that we aren't doing a (append! a a) nor a (concat! a a)
102
                            if (funcname == Language::AppendInPlace || funcname == Language::ConcatInPlace)
729✔
103
                            {
104
                                for (std::size_t i = 2, end = node.constList().size(); i < end; ++i)
16✔
105
                                {
106
                                    if (node.constList()[i].nodeType() == NodeType::Symbol && node.constList()[i].string() == arg)
8✔
107
                                        throw CodeError(
4✔
108
                                            fmt::format("MutabilityError: Can not {} the list `{}' to itself", funcname, arg),
2✔
109
                                            node.filename(),
2✔
110
                                            node.constList()[1].line(),
2✔
111
                                            node.constList()[1].col(),
2✔
112
                                            arg);
2✔
113
                                }
6✔
114
                            }
6✔
115
                        }
732✔
116

117
                        for (auto& child : node.list())
19,217✔
118
                            visit(child, register_declarations);
14,045✔
119
                    }
120
                }
8,149✔
121
                break;
8,197✔
122

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

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

136
                // if we had specific symbols to import, check that those exist
137
                if (!namespace_.symbols.empty())
27✔
138
                {
139
                    for (const auto& sym : namespace_.symbols)
16✔
140
                    {
141
                        if (!scope->get(sym, true).has_value())
10✔
142
                            throw CodeError(
×
143
                                fmt::format("ImportError: Can not import symbol {} from {}, as it isn't in the package", sym, namespace_.name),
×
144
                                node.filename(),
×
145
                                namespace_.ast->line(),
×
146
                                namespace_.ast->col(),
×
147
                                sym);
×
148
                    }
10✔
149
                }
6✔
150

151
                m_scope_resolver.saveNamespaceAndRemove();
27✔
152
                break;
153
            }
5,595✔
154

155
            default:
156
                break;
5,568✔
157
        }
21,775✔
158
    }
21,782✔
159

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

184
                    if (m_scope_resolver.isInScope(name) && keyword == Keyword::Let && register_declarations)
1,206✔
185
                        throw CodeError(
2✔
186
                            fmt::format("MutabilityError: Can not use 'let' to redefine variable `{}'", name),
1✔
187
                            node.filename(),
1✔
188
                            node.constList()[1].line(),
1✔
189
                            node.constList()[1].col(),
1✔
190
                            name);
1✔
191
                    if (keyword == Keyword::Set && m_scope_resolver.isRegistered(name))
1,205✔
192
                    {
193
                        if (m_scope_resolver.isImmutable(name).value_or(false) && register_declarations)
235✔
194
                            throw CodeError(
2✔
195
                                fmt::format("MutabilityError: Can not set the constant `{}' to {}", name, node.constList()[2].repr()),
1✔
196
                                node.filename(),
1✔
197
                                node.constList()[1].line(),
1✔
198
                                node.constList()[1].col(),
1✔
199
                                name);
1✔
200

201
                        updateSymbolWithFullyQualifiedName(node.list()[1]);
234✔
202
                    }
234✔
203
                    else if (keyword != Keyword::Set)
970✔
204
                    {
205
                        // update the declared variable name to use the fully qualified name
206
                        // this will prevent name conflicts, and handle scope resolution
207
                        const std::string fully_qualified_name = addDefinedSymbol(name, keyword != Keyword::Let);
967✔
208
                        if (register_declarations)
967✔
209
                            node.list()[1].setString(fully_qualified_name);
527✔
210
                    }
967✔
211
                }
1,210✔
212
                break;
1,206✔
213

214
            case Keyword::Import:
215
                if (!node.constList().empty())
×
216
                    m_plugin_names.push_back(node.constList()[1].constList().back().string());
×
217
                break;
439✔
218

219
            case Keyword::Fun:
220
                // create a new scope to track variables
221
                m_scope_resolver.createNew();
439✔
222

223
                if (node.constList()[1].nodeType() == NodeType::List)
439✔
224
                {
225
                    for (auto& child : node.list()[1].list())
1,170✔
226
                    {
227
                        if (child.nodeType() == NodeType::Capture)
732✔
228
                        {
229
                            if (!m_scope_resolver.isRegistered(child.string()) && register_declarations)
143✔
230
                                throw CodeError(
4✔
231
                                    fmt::format("Can not capture {} because it is referencing a variable defined in an unreachable scope.", child.string()),
2✔
232
                                    child.filename(),
2✔
233
                                    child.line(),
2✔
234
                                    child.col(),
2✔
235
                                    child.repr());
2✔
236

237
                            // update the declared variable name to use the fully qualified name
238
                            // this will prevent name conflicts, and handle scope resolution
239
                            std::string fqn = updateSymbolWithFullyQualifiedName(child);
141✔
240
                            addDefinedSymbol(fqn, true);
141✔
241
                        }
141✔
242
                        else if (child.nodeType() == NodeType::Symbol)
589✔
243
                            addDefinedSymbol(child.string(), /* is_mutable= */ true);
589✔
244
                    }
732✔
245
                }
436✔
246
                if (node.constList().size() > 2)
437✔
247
                    visit(node.list()[2], register_declarations);
436✔
248

249
                // remove the scope once the function has been compiled, only we were registering declarations
250
                m_scope_resolver.removeLastScope();
437✔
251
                break;
1,765✔
252

253
            default:
254
                for (auto& child : node.list())
7,323✔
255
                    visit(child, register_declarations);
5,995✔
256
                break;
1,328✔
257
        }
2,971✔
258
    }
2,977✔
259

260
    void NameResolutionPass::addSymbolNode(const Node& symbol, const std::string& old_name)
8,722✔
261
    {
8,722✔
262
        const std::string& name = symbol.string();
8,722✔
263

264
        // we don't accept builtins/operators as a user symbol
265
        if (m_language_symbols.contains(name))
8,722✔
266
            return;
4,123✔
267

268
        // remove the old name node, to avoid false positive when looking for unbound symbols
269
        if (!old_name.empty())
4,599✔
270
        {
271
            auto it = std::ranges::find_if(m_symbol_nodes, [&old_name, &symbol](const Node& sym_node) -> bool {
378,519✔
272
                return sym_node.string() == old_name &&
378,124✔
273
                    sym_node.col() == symbol.col() &&
4,204✔
274
                    sym_node.line() == symbol.line() &&
5,130✔
275
                    sym_node.filename() == symbol.filename();
2,345✔
276
            });
277
            if (it != m_symbol_nodes.end())
4,599✔
278
            {
279
                it->setString(name);
2,345✔
280
                return;
2,345✔
281
            }
282
        }
4,599✔
283

284
        const auto it = std::ranges::find_if(m_symbol_nodes, [&name](const Node& sym_node) -> bool {
172,774✔
285
            return sym_node.string() == name;
170,520✔
286
        });
287
        if (it == m_symbol_nodes.end())
2,254✔
288
            m_symbol_nodes.push_back(symbol);
272✔
289
    }
8,722✔
290

291
    bool NameResolutionPass::mayBeFromPlugin(const std::string& name) const noexcept
269✔
292
    {
269✔
293
        std::string splitted = Utils::splitString(name, ':')[0];
269✔
294
        const auto it = std::ranges::find_if(
269✔
295
            m_plugin_names,
269✔
296
            [&splitted](const std::string& plugin) -> bool {
269✔
297
                return plugin == splitted;
×
298
            });
299
        return it != m_plugin_names.end();
269✔
300
    }
269✔
301

302
    std::string NameResolutionPass::updateSymbolWithFullyQualifiedName(Node& symbol)
7,772✔
303
    {
7,772✔
304
        auto [allowed, fqn] = m_scope_resolver.canFullyQualifyName(symbol.string());
15,546✔
305

306
        if (!allowed)
7,772✔
307
        {
308
            std::string message;
2✔
309
            if (fqn.ends_with("#hidden"))
2✔
310
                message = fmt::format(
2✔
311
                    R"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to add it to the symbol list while importing?)",
1✔
312
                    symbol.string(),
1✔
313
                    fqn.substr(0, fqn.find_first_of('#')));
2✔
314
            else
315
                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✔
316
            throw CodeError(
4✔
317
                message,
318
                symbol.filename(),
2✔
319
                symbol.line(),
2✔
320
                symbol.col(),
2✔
321
                symbol.repr());
2✔
322
        }
2✔
323

324
        symbol.setString(fqn);
7,770✔
325
        return fqn;
7,770✔
326
    }
7,774✔
327

328
    void NameResolutionPass::checkForUndefinedSymbol() const
54✔
329
    {
54✔
330
        for (const auto& sym : m_symbol_nodes)
323✔
331
        {
332
            const auto& str = sym.string();
269✔
333
            const bool is_plugin = mayBeFromPlugin(str);
269✔
334

335
            if (!m_defined_symbols.contains(str) && !is_plugin)
269✔
336
            {
337
                std::string message;
2✔
338

339
                const std::string suggestion = offerSuggestion(str);
2✔
340
                if (suggestion.empty())
2✔
341
                    message = fmt::format(R"(Unbound variable error "{}" (variable is used but not defined))", str);
1✔
342
                else
343
                {
344
                    const std::string prefix = suggestion.substr(0, suggestion.find_first_of(':'));
1✔
345
                    const std::string note_about_prefix = fmt::format(
2✔
346
                        " You either forgot to import it in the symbol list (eg `(import {} :{})') or need to fully qualify it by adding the namespace",
1✔
347
                        prefix,
348
                        str);
1✔
349
                    const bool add_note = suggestion.ends_with(":" + str);
1✔
350
                    message = fmt::format(R"(Unbound variable error "{}" (did you mean "{}"?{}))", str, suggestion, add_note ? note_about_prefix : "");
1✔
351
                }
1✔
352

353
                throw CodeError(message, sym.filename(), sym.line(), sym.col(), sym.repr());
2✔
354
            }
2✔
355
        }
269✔
356
    }
54✔
357

358
    std::string NameResolutionPass::offerSuggestion(const std::string& str) const
2✔
359
    {
2✔
360
        auto iterate = [](const std::string& word, const std::unordered_set<std::string>& dict) -> std::string {
5✔
361
            std::string suggestion;
3✔
362
            // our suggestion shouldn't require more than half the string to change
363
            std::size_t suggestion_distance = word.size() / 2;
3✔
364
            for (const std::string& symbol : dict)
94✔
365
            {
366
                const std::size_t current_distance = Utils::levenshteinDistance(word, symbol);
91✔
367
                if (current_distance <= suggestion_distance)
91✔
368
                {
369
                    suggestion_distance = current_distance;
1✔
370
                    suggestion = symbol;
1✔
371
                }
1✔
372
            }
91✔
373
            return suggestion;
3✔
374
        };
3✔
375

376
        std::string suggestion = iterate(str, m_defined_symbols);
2✔
377
        // look for a suggestion related to language builtins
378
        if (suggestion.empty())
2✔
379
            suggestion = iterate(str, m_language_symbols);
1✔
380
        // look for a suggestion related to a namespace change
381
        if (suggestion.empty())
2✔
382
        {
383
            if (const auto it = std::ranges::find_if(m_defined_symbols, [&str](const std::string& symbol) {
3✔
384
                    return symbol.ends_with(":" + str);
1✔
385
                });
386
                it != m_defined_symbols.end())
1✔
UNCOV
387
                suggestion = *it;
×
388
        }
1✔
389

390
        return suggestion;
2✔
391
    }
2✔
392
}
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

© 2025 Coveralls, Inc