ArkScript
A small, lisp-inspired, functional scripting language
ScopeResolver.cpp
Go to the documentation of this file.
2
3#include <ranges>
4#include <algorithm>
5
6namespace Ark::internal
7{
9 {
10 createNewNamespace("", /* with_prefix= */ false, /* is_glob= */ true, /* symbols= */ {});
11 }
12
14 {
15 m_scopes.emplace_back(std::make_unique<StaticScope>());
16 }
17
19 {
20 m_scopes.pop_back();
21 }
22
23 void ScopeResolver::createNewNamespace(const std::string& name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols)
24 {
25 m_scopes.emplace_back(std::make_unique<NamespaceScope>(name, with_prefix, is_glob, symbols));
26 }
27
28 std::string ScopeResolver::registerInCurrent(const std::string& name, const bool is_mutable)
29 {
30 return m_scopes.back()->add(name, is_mutable);
31 }
32
34 {
35 for (auto& m_scope : std::ranges::reverse_view(m_scopes) | std::ranges::views::drop(1))
36 {
37 if (m_scope->saveNamespace(m_scopes.back()))
38 break;
39 }
40
41 m_scopes.pop_back();
42 }
43
44 std::optional<bool> ScopeResolver::isImmutable(const std::string& name) const
45 {
46 for (const auto& m_scope : std::ranges::reverse_view(m_scopes))
47 {
48 if (auto maybe = m_scope->get(name, currentNamespace(), true); maybe.has_value())
49 return !maybe.value().is_mutable;
50 }
51 return std::nullopt;
52 }
53
54 bool ScopeResolver::isRegistered(const std::string& name) const
55 {
56 const std::string origin_namespace = currentNamespace();
57 return std::ranges::any_of(std::ranges::reverse_view(m_scopes), [&name, &origin_namespace](const auto& scope) {
58 return scope->get(name, origin_namespace, true).has_value();
59 });
60 }
61
62 bool ScopeResolver::isInScope(const std::string& name) const
63 {
64 return m_scopes.back()->get(name, currentNamespace(), false).has_value();
65 }
66
67 std::string ScopeResolver::getFullyQualifiedNameInNearestScope(const std::string& name) const
68 {
69 const std::string prefix = currentNamespace();
70 std::optional<std::string> maybe_name;
71 for (const auto& scope : std::ranges::reverse_view(m_scopes))
72 {
73 if (auto maybe_fqn = scope->get(name, prefix, true); maybe_fqn.has_value())
74 {
75 // prioritize non-hidden symbols
76 if ((maybe_name.has_value() &&
77 maybe_name.value().ends_with("#hidden") &&
78 !maybe_fqn.value().name.ends_with("#hidden")) ||
79 !maybe_name.has_value())
80 maybe_name = maybe_fqn.value().name;
81 }
82 }
83 return maybe_name.value_or(name);
84 }
85
86 std::pair<bool, std::string> ScopeResolver::canFullyQualifyName(const std::string& name)
87 {
88 // a given name can be fully qualified if
89 // old == new
90 // old != new and new has prefix
91 // if the prefix namespace is glob
92 // if the prefix namespace has name in its symbols
93 // if the prefix namespace is with_prefix && it is the top most scope
94 const std::string maybe_fqn = getFullyQualifiedNameInNearestScope(name);
95
96 if (maybe_fqn == name)
97 return std::make_pair(true, maybe_fqn);
98
99 const std::string prefix = maybe_fqn.substr(0, maybe_fqn.find_first_of(':'));
100 const std::string unprefixed_name = name.substr(name.find_first_of(':') + 1);
101 auto namespaces =
102 std::ranges::reverse_view(m_scopes) | std::ranges::views::filter([](const auto& e) {
103 return e->isNamespace();
104 });
105 bool top = true;
106 for (auto& scope : namespaces)
107 {
108 if (top && prefix == scope->prefix())
109 return std::make_pair(true, maybe_fqn);
110 if (!top && prefix == scope->prefix() && (scope->isGlob() || scope->hasSymbol(name)))
111 return std::make_pair(true, maybe_fqn);
112
113 // check for the presence of the symbol in symbol imports and glob imports
114 if (scope->recursiveHasSymbol(unprefixed_name))
115 return std::make_pair(true, maybe_fqn);
116
117 top = false;
118 }
119
120 return std::make_pair(false, maybe_fqn);
121 }
122
124 {
125 if (!m_scopes.empty()) [[likely]]
126 return m_scopes.back().get();
127 return nullptr;
128 }
129
131 {
132 for (const auto& scope : std::ranges::reverse_view(m_scopes))
133 {
134 if (scope->isNamespace())
135 return scope->prefix();
136 }
137
138 // no namespace name, thus no prefix ; "" is either the default namespace, a function scope or a while loop
139 return "";
140 }
141}
Handle scope resolution at compile time.
std::string registerInCurrent(const std::string &name, bool is_mutable)
Register a Declaration in the current (last) scope.
ScopeResolver()
Create a ScopeResolver.
void createNewNamespace(const std::string &name, bool with_prefix, bool is_glob, const std::vector< std::string > &symbols)
Create a new namespace scope.
void saveNamespaceAndRemove()
Save the last scope as a namespace, by attaching it to the nearest namespace scope.
std::string getFullyQualifiedNameInNearestScope(const std::string &name) const
Get a FQN from a variable name in the nearest scope it is declared in.
bool isRegistered(const std::string &name) const
Checks if any scope has 'name', in reverse order.
void createNew()
Create a new scope.
std::vector< std::unique_ptr< StaticScope > > m_scopes
StaticScope * currentScope() const
Return a non-owning raw pointer to the current scope.
bool isInScope(const std::string &name) const
Checks if 'name' is in the current scope.
void removeLastScope()
Remove the last scope.
std::string currentNamespace() const
std::optional< bool > isImmutable(const std::string &name) const
Checks the scopes in reverse order for 'name' and returns its mutability status.
std::pair< bool, std::string > canFullyQualifyName(const std::string &name)
Checks if a name can be fully qualified (allows only unprefixed names to be resolved by glob namespac...
virtual std::optional< Declaration > get(const std::string &name, const std::string &origin_namespace, bool extensive_lookup)
Try to return a Declaration from this scope with a given name.