ArkScript
A small, fast, functional and scripting language for video games
NameResolutionPass.hpp
Go to the documentation of this file.
1/**
2 * @file NameResolutionPass.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Resolves names and fully qualify them in the AST (prefixing them with the package they are from)
5 * @date 2024-07-22
6 *
7 * @copyright Copyright (c) 2024-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_NAMERESOLUTIONPASS_HPP
12#define ARK_COMPILER_NAMERESOLUTIONPASS_HPP
13
14#include <vector>
15#include <string>
16#include <unordered_set>
17
18#include <Ark/Platform.hpp>
19#include <Ark/Compiler/Pass.hpp>
21
22namespace Ark::internal
23{
24 class ARK_API NameResolutionPass final : public Pass
25 {
26 public:
27 /**
28 * @brief Create a NameResolutionPass
29 * @param debug debug level
30 */
31 explicit NameResolutionPass(unsigned debug);
32
33 /**
34 * @brief Start visiting the given AST, checking for mutability violation and unbound variables
35 * @param ast AST to analyze
36 */
37 void process(const Node& ast) override;
38
39 /**
40 * @brief Unused overload that return the input AST (untouched as this pass only generates errors)
41 * @return const Node& ast
42 */
43 [[nodiscard]] const Node& ast() const noexcept override;
44
45 /**
46 * @brief Register a symbol as defined, so that later we can throw errors on undefined symbols
47 *
48 * @param sym
49 * @param is_mutable true if the symbol is inside mut/set, false otherwise (let)
50 */
51 std::string addDefinedSymbol(const std::string& sym, bool is_mutable);
52
53 private:
55 std::unordered_set<std::string> m_language_symbols; ///< Precomputed set of language symbols that can't be used to define variables
56 std::vector<Node> m_symbol_nodes;
57 std::unordered_set<std::string> m_defined_symbols;
58 std::vector<std::string> m_plugin_names;
60
61 /**
62 * @brief Recursively visit nodes
63 * @param node node to visit
64 * @param register_declarations whether or not the visit should register declarations
65 */
66 void visit(Node& node, bool register_declarations);
67
68 /**
69 *
70 * @param node a list node whose first child is a keyword
71 * @param keyword
72 * @param register_declarations whether or not the visit should register declarations
73 */
74 void visitKeyword(Node& node, Keyword keyword, bool register_declarations);
75
76 /**
77 * @brief Register a given node in the symbol table
78 *
79 * @param symbol
80 * @param old_name old name for symbol, to replace it with the new one if it was renamed
81 */
82 void addSymbolNode(const Node& symbol, const std::string& old_name = "");
83
84 /**
85 * @brief Checking if a symbol may be coming from a plugin
86 *
87 * @param name symbol name
88 * @return true the symbol may be from a plugin, loaded at runtime
89 * @return false
90 */
91 [[nodiscard]] bool mayBeFromPlugin(const std::string& name) const noexcept;
92
93 std::string updateSymbolWithFullyQualifiedName(Node& symbol);
94
95 /**
96 * @brief Checks for undefined symbols, not present in the defined symbols table
97 *
98 */
99 void checkForUndefinedSymbol() const;
100
101 /**
102 * @brief Suggest a symbol of what the user may have meant to input
103 *
104 * @param str the string
105 * @return std::string
106 */
107 [[nodiscard]] std::string offerSuggestion(const std::string& str) const;
108 };
109}
110
111#endif // ARK_COMPILER_NAMERESOLUTIONPASS_HPP
#define ARK_API
Definition Module.hpp:28
Interface for a compiler pass (take in an AST, output an AST)
ArkScript configuration macros.
Handle scope resolution at compile time.
std::vector< std::string > m_plugin_names
std::unordered_set< std::string > m_language_symbols
Precomputed set of language symbols that can't be used to define variables.
std::unordered_set< std::string > m_defined_symbols
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:30
An interface to describe compiler passes.
Definition Pass.hpp:23
Keyword
The different keywords available.
Definition Common.hpp:75