• 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

96.39
/src/arkreactor/Compiler/NameResolution/ScopeResolver.cpp
1
#include <Ark/Compiler/NameResolution/ScopeResolver.hpp>
2

3
#include <ranges>
4

5
namespace Ark::internal
6
{
7
    ScopeResolver::ScopeResolver()
116✔
8
    {
116✔
9
        createNewNamespace("", /* with_prefix= */ false, /* is_glob= */ true, /* symbols= */ {});
116✔
10
    }
116✔
11

12
    void ScopeResolver::createNew()
439✔
13
    {
439✔
14
        m_scopes.emplace_back(std::make_unique<StaticScope>());
439✔
15
    }
439✔
16

17
    void ScopeResolver::removeLastScope()
437✔
18
    {
437✔
19
        m_scopes.pop_back();
437✔
20
    }
437✔
21

22
    void ScopeResolver::createNewNamespace(const std::string& name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols)
143✔
23
    {
143✔
24
        m_scopes.emplace_back(std::make_unique<NamespaceScope>(name, with_prefix, is_glob, symbols));
143✔
25
    }
143✔
26

27
    std::string ScopeResolver::registerInCurrent(const std::string& name, const bool is_mutable)
1,701✔
28
    {
1,701✔
29
        return m_scopes.back()->add(name, is_mutable);
1,701✔
30
    }
31

32
    void ScopeResolver::saveNamespaceAndRemove()
27✔
33
    {
27✔
34
        for (auto& m_scope : std::ranges::reverse_view(m_scopes) | std::ranges::views::drop(1))
54✔
35
        {
36
            if (m_scope->saveNamespace(m_scopes.back()))
27✔
37
                break;
27✔
38
        }
27✔
39

40
        m_scopes.pop_back();
27✔
41
    }
27✔
42

43
    std::optional<bool> ScopeResolver::isImmutable(const std::string& name) const
250✔
44
    {
250✔
45
        for (const auto& m_scope : std::ranges::reverse_view(m_scopes))
554✔
46
        {
47
            if (auto maybe = m_scope->get(name, true); maybe.has_value())
554✔
48
                return !maybe.value().is_mutable;
250✔
49
        }
304✔
UNCOV
50
        return std::nullopt;
×
51
    }
250✔
52

53
    bool ScopeResolver::isRegistered(const std::string& name) const
381✔
54
    {
381✔
55
        for (const auto& m_scope : std::ranges::reverse_view(m_scopes))
957✔
56
        {
57
            if (m_scope->get(name, true).has_value())
576✔
58
                return true;
376✔
59
        }
576✔
60
        return false;
5✔
61
    }
381✔
62

63
    bool ScopeResolver::isInScope(const std::string& name) const
1,206✔
64
    {
1,206✔
65
        return m_scopes.back()->get(name, false).has_value();
1,206✔
66
    }
67

68
    std::string ScopeResolver::getFullyQualifiedNameInNearestScope(const std::string& name) const
9,099✔
69
    {
9,099✔
70
        for (const auto& scope : std::ranges::reverse_view(m_scopes))
33,234✔
71
        {
72
            if (auto maybe_fqn = scope->get(name, true); maybe_fqn.has_value())
28,352✔
73
                return maybe_fqn.value().name;
4,217✔
74
        }
24,135✔
75
        return name;
4,882✔
76
    }
9,099✔
77

78
    std::pair<bool, std::string> ScopeResolver::canFullyQualifyName(const std::string& name)
7,772✔
79
    {
7,772✔
80
        // a given name can be fully qualified if
81
        // old == new
82
        // old != new and new has prefix
83
        //     if the prefix namespace is glob
84
        //     if the prefix namespace has name in its symbols
85
        //     if the prefix namespace is with_prefix && it is the top most scope
86
        const std::string maybe_fqn = getFullyQualifiedNameInNearestScope(name);
7,772✔
87

88
        if (maybe_fqn == name)
7,772✔
89
            return std::make_pair(true, maybe_fqn);
7,568✔
90

91
        const std::string prefix = maybe_fqn.substr(0, maybe_fqn.find_first_of(':'));
204✔
92
        auto namespaces =
204✔
93
            std::ranges::reverse_view(m_scopes) | std::ranges::views::filter([](const auto& e) {
590✔
94
                return e->isNamespace();
386✔
95
            });
96
        bool top = true;
204✔
97
        for (auto& scope : namespaces)
408✔
98
        {
99
            if (top && prefix == scope->prefix())
204✔
100
                return std::make_pair(true, maybe_fqn);
201✔
101
            if (!top && prefix == scope->prefix() && (scope->isGlob() || scope->hasSymbol(name)))
3✔
102
                return std::make_pair(true, maybe_fqn);
×
103

104
            for (const auto& saved_scope : scope->savedScopes())
8✔
105
            {
106
                if (prefix == saved_scope->prefix() && saved_scope->hasSymbol(name))
5✔
107
                    return std::make_pair(true, maybe_fqn);
1✔
108
            }
5✔
109

110
            top = false;
2✔
111
        }
204✔
112

113
        return std::make_pair(false, maybe_fqn);
2✔
114
    }
7,772✔
115

116
    StaticScope* ScopeResolver::currentScope() const
27✔
117
    {
27✔
118
        if (!m_scopes.empty()) [[likely]]
27✔
119
            return m_scopes.back().get();
27✔
UNCOV
120
        return nullptr;
×
121
    }
27✔
122
}
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