ArkScript
A small, fast, functional and scripting language for video games
StaticScope.hpp
Go to the documentation of this file.
1/**
2 * @file StaticScope.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Static scopes (for functions, loops) and namespace scopes (for packages) definitions, used at compile time
5 * @date 2024-11-30
6 *
7 * @copyright Copyright (c) 2024-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_NAMERESOLUTION_STATICSCOPE_HPP
12#define ARK_COMPILER_NAMERESOLUTION_STATICSCOPE_HPP
13
14#include <string>
15#include <optional>
16#include <memory>
17#include <vector>
18#include <ranges>
19#include <unordered_set>
20
21#include <Ark/Platform.hpp>
22
23namespace Ark::internal
24{
26 {
27 std::string name; ///< End name, can be modified to be hidden
28 std::string original_name; ///< Original name, with the prefix, without hidden namespaces
30
31 bool operator==(const Declaration& other) const = default;
32 };
33}
34
35template <>
36struct std::hash<Ark::internal::Declaration>
37{
38 inline size_t operator()(const Ark::internal::Declaration& x) const noexcept
39 {
40 return std::hash<std::string> {}(x.original_name);
41 }
42};
43
44namespace Ark::internal
45{
47 {
48 public:
49 virtual ~StaticScope() = default;
50
51 StaticScope() = default;
52
53 StaticScope(const StaticScope&) = delete;
57
58 /**
59 * @brief Add a Declaration to the scope, given a mutability status
60 * @param name
61 * @param is_mutable
62 */
63 virtual std::string add(const std::string& name, bool is_mutable);
64
65 /**
66 * @brief Try to return a Declaration from this scope with a given name.
67 * @param name
68 * @param extensive_lookup unused in StaticScope
69 * @return std::optional<Declaration> std::nullopt if the Declaration isn't in scope
70 */
71 [[nodiscard]] virtual std::optional<Declaration> get(const std::string& name, bool extensive_lookup);
72
73 /**
74 * @brief Given a Declaration name, compute its fully qualified name
75 * @param name
76 * @return std::string fully qualified name in the scope
77 */
78 [[nodiscard]] virtual std::string fullyQualifiedName(const std::string& name) const;
79
80 /**
81 * @brief Save a namespace scope to help with lookup
82 *
83 * @return true if the scope was saved, on NamespaceScope
84 * @return false on StaticScope
85 */
86 virtual bool saveNamespace(std::unique_ptr<StaticScope>&);
87
88 [[nodiscard]] virtual bool isNamespace() const;
89 [[nodiscard]] inline virtual bool withPrefix() const { return false; }
90 [[nodiscard]] inline virtual bool isGlob() const { return false; }
91 [[nodiscard]] inline virtual std::string prefix() const { return ""; }
92 [[nodiscard]] inline virtual bool hasSymbol(const std::string&) const { return false; }
93 [[nodiscard]] inline virtual bool recursiveHasSymbol(const std::string&) { return false; }
94
95 private:
96 std::unordered_set<Declaration> m_vars {};
97 };
98
99 class ARK_API NamespaceScope final : public StaticScope
100 {
101 public:
102 NamespaceScope(std::string name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols);
103
108
109 /**
110 * @brief Add a Declaration to the scope, given a mutability status
111 * @param name
112 * @param is_mutable
113 */
114 std::string add(const std::string& name, bool is_mutable) override;
115
116 /**
117 * @brief Try to return a Declaration from this scope with a given name.
118 * @param name
119 * @param extensive_lookup if true, use the additional saved namespaces
120 * @return std::optional<Declaration> std::nullopt if the Declaration isn't in scope
121 */
122 [[nodiscard]] std::optional<Declaration> get(const std::string& name, bool extensive_lookup) override;
123
124 /**
125 * @brief Given a Declaration name, compute its fully qualified name
126 * @param name
127 * @return std::string fully qualified name in the namespace
128 */
129 [[nodiscard]] std::string fullyQualifiedName(const std::string& name) const override;
130
131 /**
132 * @brief Save a namespace scope to help with lookup
133 *
134 * @return true if the scope was saved, on NamespaceScope
135 * @return false on StaticScope
136 */
137 bool saveNamespace(std::unique_ptr<StaticScope>&) override;
138
139 [[nodiscard]] bool isNamespace() const override;
140 [[nodiscard]] inline bool withPrefix() const override { return m_with_prefix; }
141 [[nodiscard]] inline bool isGlob() const override { return m_is_glob; }
142 [[nodiscard]] inline std::string prefix() const override { return m_namespace; }
143 [[nodiscard]] inline bool hasSymbol(const std::string& symbol) const override { return std::ranges::find(m_symbols, symbol) != m_symbols.end(); }
144 [[nodiscard]] bool recursiveHasSymbol(const std::string& symbol) override;
145
146 private:
147 std::string m_namespace;
150 std::vector<std::string> m_symbols;
151 std::unordered_set<Declaration> m_vars {};
152 std::vector<std::unique_ptr<StaticScope>> m_additional_namespaces;
153 };
154}
155
156#endif // ARK_COMPILER_NAMERESOLUTION_STATICSCOPE_HPP
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
bool isGlob() const override
bool hasSymbol(const std::string &symbol) const override
NamespaceScope(const NamespaceScope &)=delete
NamespaceScope & operator=(const NamespaceScope &)=delete
bool withPrefix() const override
NamespaceScope(NamespaceScope &&)=default
std::string prefix() const override
std::vector< std::string > m_symbols
NamespaceScope & operator=(NamespaceScope &&)=default
std::vector< std::unique_ptr< StaticScope > > m_additional_namespaces
virtual bool withPrefix() const
StaticScope(const StaticScope &)=delete
virtual bool recursiveHasSymbol(const std::string &)
StaticScope & operator=(const StaticScope &)=delete
virtual std::string prefix() const
virtual bool hasSymbol(const std::string &) const
StaticScope(StaticScope &&)=default
StaticScope & operator=(StaticScope &&)=default
virtual ~StaticScope()=default
virtual bool isGlob() const
std::string name
End name, can be modified to be hidden.
std::string original_name
Original name, with the prefix, without hidden namespaces.
bool operator==(const Declaration &other) const =default
size_t operator()(const Ark::internal::Declaration &x) const noexcept