ArkScript
A small, fast, functional and scripting language for video games
ScopeResolver.hpp
Go to the documentation of this file.
1/**
2 * @file ScopeResolver.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Handle scope resolution at compile time
5 * @version 0.1
6 * @date 2024-11-30
7 *
8 * @copyright Copyright (c) 2024
9 *
10 */
11
12#ifndef ARK_COMPILER_NAMERESOLUTION_SCOPERESOLVER_HPP
13#define ARK_COMPILER_NAMERESOLUTION_SCOPERESOLVER_HPP
14
15#include <string>
16#include <optional>
17#include <memory>
18#include <vector>
19#include <utility>
20
22
23namespace Ark::internal
24{
26 {
27 public:
28 /**
29 * @brief Create a ScopeResolver
30 * @details Kickstart by create a default global scope
31 */
33
34 ScopeResolver(const ScopeResolver&) = delete;
38
39 /**
40 * @brief Create a new scope
41 */
42 void createNew();
43
44 /**
45 * @brief Remove the last scope
46 */
47 void removeLastScope();
48
49 /**
50 * @brief Create a new namespace scope
51 * @param name
52 * @param with_prefix
53 * @param is_glob
54 * @param symbols
55 */
56 void createNewNamespace(const std::string& name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols);
57
58 /**
59 * @brief Register a Declaration in the current (last) scope
60 * @param name
61 * @param is_mutable
62 * @return std::string the fully qualified name assigned by the scope
63 */
64 std::string registerInCurrent(const std::string& name, bool is_mutable);
65
66 /**
67 * @brief Save the last scope as a namespace, by attaching it to the nearest namespace scope
68 * @details Also handle removing the scope from the scope pile.
69 */
71
72 /**
73 * @brief Checks the scopes in reverse order for 'name' and returns its mutability status
74 * @param name
75 * @return std::nullopt if the Declaration could not be found
76 * @return true if immutable
77 * @return false if mutable
78 */
79 [[nodiscard]] std::optional<bool> isImmutable(const std::string& name) const;
80
81 /**
82 * @brief Checks if any scope has 'name', in reverse order
83 * @param name
84 * @return
85 */
86 [[nodiscard]] bool isRegistered(const std::string& name) const;
87
88 /**
89 * @brief Checks if 'name' is in the current scope
90 *
91 * @param name
92 * @return
93 */
94 [[nodiscard]] bool isInScope(const std::string& name) const;
95
96 /**
97 * @brief Get a FQN from a variable name in the nearest scope it is declared in
98 *
99 * @param name
100 * @return std::string
101 */
102 [[nodiscard]] std::string getFullyQualifiedNameInNearestScope(const std::string& name) const;
103
104 /**
105 * @brief Checks if a name can be fully qualified (allows only unprefixed names to be resolved by glob namespaces or inside their own namespace)
106 *
107 * @param name
108 * @return std::pair<bool, std::string> if the name can be fully qualified, first element is true ; second element is the FQN
109 */
110 [[nodiscard]] std::pair<bool, std::string> canFullyQualifyName(const std::string& name);
111
112 /**
113 * @brief Return a non-owning raw pointer to the current scope
114 *
115 * @return StaticScope* non-owning pointer to the current scope
116 * @return nullptr if there are no scope
117 */
118 [[nodiscard]] StaticScope* currentScope() const;
119
120 private:
121 std::vector<std::unique_ptr<StaticScope>> m_scopes;
122 };
123}
124
125#endif // ARK_COMPILER_NAMERESOLUTION_SCOPERESOLVER_HPP
ScopeResolver & operator=(ScopeResolver &&)=default
ScopeResolver & operator=(const ScopeResolver &)=delete
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
ScopeResolver(ScopeResolver &&)=default
ScopeResolver(const ScopeResolver &)=delete
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::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...