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

ArkScript-lang / Ark / 13241725262

10 Feb 2025 01:14PM UTC coverage: 78.815% (+0.5%) from 78.314%
13241725262

push

github

SuperFola
fix(name resolution): glob importing was broken and expected names to be fully qualified from the get go

2 of 3 new or added lines in 1 file covered. (66.67%)

5826 of 7392 relevant lines covered (78.81%)

22770.96 hits per line

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

78.57
/include/Ark/Compiler/NameResolution/StaticScope.hpp
1
/**
2
 * @file StaticScope.hpp
3
 * @author Alexandre Plateau (lexplt.dev@gmail.com)
4
 * @brief
5
 * @version 0.1
6
 * @date 2024-11-30
7
 *
8
 * @copyright Copyright (c) 2024
9
 *
10
 */
11

12
#ifndef ARK_COMPILER_NAMERESOLUTION_STATICSCOPE_HPP
13
#define ARK_COMPILER_NAMERESOLUTION_STATICSCOPE_HPP
14

15
#include <string>
16
#include <optional>
17
#include <memory>
18
#include <vector>
19
#include <ranges>
20
#include <unordered_set>
21

22
namespace Ark::internal
23
{
24
    struct Declaration
57,819✔
25
    {
26
        std::string name;           ///< End name, can be modified to be hidden
27
        std::string original_name;  ///< Original name, with the prefix, without hidden namespaces
28
        bool is_mutable;
29

30
        bool operator==(const Declaration& other) const = default;
1,879✔
31
    };
32
}
33

34
template <>
35
struct std::hash<Ark::internal::Declaration>
36
{
37
    inline size_t operator()(const Ark::internal::Declaration& x) const noexcept
8,021✔
38
    {
8,021✔
39
        return std::hash<std::string> {}(x.original_name);
8,021✔
40
    }
41
};
42

43
namespace Ark::internal
44
{
45
    class StaticScope
3,904✔
46
    {
47
    public:
48
        virtual ~StaticScope() = default;
3,672✔
49

50
        /**
51
         * @brief Add a Declaration to the scope, given a mutability status
52
         * @param name
53
         * @param is_mutable
54
         */
55
        virtual std::string add(const std::string& name, bool is_mutable);
56

57
        /**
58
         * @brief Try to return a Declaration from this scope with a given name.
59
         * @param name
60
         * @param extensive_lookup unused in StaticScope
61
         * @return std::optional<Declaration> std::nullopt if the Declaration isn't in scope
62
         */
63
        [[nodiscard]] virtual std::optional<Declaration> get(const std::string& name, bool extensive_lookup);
64

65
        /**
66
         * @brief Given a Declaration name, compute its fully qualified name
67
         * @param name
68
         * @return std::string fully qualified name in the scope
69
         */
70
        [[nodiscard]] virtual std::string fullyQualifiedName(const std::string& name) const;
71

72
        /**
73
         * @brief Save a namespace scope to help with lookup
74
         *
75
         * @return true if the scope was saved, on NamespaceScope
76
         * @return false on StaticScope
77
         */
78
        virtual bool saveNamespace(std::unique_ptr<StaticScope>&);
79

80
        [[nodiscard]] virtual bool isNamespace() const;
81
        [[nodiscard]] inline virtual bool withPrefix() const { return false; }
×
82
        [[nodiscard]] inline virtual bool isGlob() const { return false; }
×
83
        [[nodiscard]] inline virtual std::string prefix() const { return ""; }
×
84
        [[nodiscard]] inline virtual bool hasSymbol(const std::string&) const { return false; }
×
NEW
85
        [[nodiscard]] inline virtual bool recursiveHasSymbol(const std::string&) { return false; }
×
86

87
    private:
88
        std::unordered_set<Declaration> m_vars {};
1,952✔
89
    };
90

91
    class NamespaceScope final : public StaticScope
464✔
92
    {
93
    public:
94
        NamespaceScope(std::string name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols);
95

96
        /**
97
         * @brief Add a Declaration to the scope, given a mutability status
98
         * @param name
99
         * @param is_mutable
100
         */
101
        std::string add(const std::string& name, bool is_mutable) override;
102

103
        /**
104
         * @brief Try to return a Declaration from this scope with a given name.
105
         * @param name
106
         * @param extensive_lookup if true, use the additional saved namespaces
107
         * @return std::optional<Declaration> std::nullopt if the Declaration isn't in scope
108
         */
109
        [[nodiscard]] std::optional<Declaration> get(const std::string& name, bool extensive_lookup) override;
110

111
        /**
112
         * @brief Given a Declaration name, compute its fully qualified name
113
         * @param name
114
         * @return std::string fully qualified name in the namespace
115
         */
116
        [[nodiscard]] std::string fullyQualifiedName(const std::string& name) const override;
117

118
        /**
119
         * @brief Save a namespace scope to help with lookup
120
         *
121
         * @return true if the scope was saved, on NamespaceScope
122
         * @return false on StaticScope
123
         */
124
        bool saveNamespace(std::unique_ptr<StaticScope>&) override;
125

126
        [[nodiscard]] bool isNamespace() const override;
127
        [[nodiscard]] inline bool withPrefix() const override { return m_with_prefix; }
×
128
        [[nodiscard]] inline bool isGlob() const override { return m_is_glob; }
48✔
129
        [[nodiscard]] inline std::string prefix() const override { return m_namespace; }
355✔
130
        [[nodiscard]] inline bool hasSymbol(const std::string& symbol) const override { return std::ranges::find(m_symbols, symbol) != m_symbols.end(); }
260,866✔
131
        [[nodiscard]] inline bool recursiveHasSymbol(const std::string& symbol) override
57✔
132
        {
57✔
133
            if (hasSymbol(symbol) || (isGlob() && get(symbol, false).has_value()))
57✔
134
                return true;
23✔
135
            for (const auto& saved_scope : m_additional_namespaces)
66✔
136
            {
137
                if (saved_scope->recursiveHasSymbol(symbol))
32✔
138
                    return true;
24✔
139
            }
32✔
140
            return false;
10✔
141
        }
57✔
142

143
    private:
144
        std::string m_namespace;
145
        bool m_with_prefix;
146
        bool m_is_glob;
147
        std::vector<std::string> m_symbols;
148
        std::unordered_set<Declaration> m_vars {};
149
        std::vector<std::unique_ptr<StaticScope>> m_additional_namespaces;
150
    };
151
}
152

153
#endif  // ARK_COMPILER_NAMERESOLUTION_STATICSCOPE_HPP
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