ArkScript
A small, lisp-inspired, functional scripting language
NameResolutionPass.hpp
Go to the documentation of this file.
1/**
2 * @file NameResolutionPass.hpp
3 * @author Lexy Plateau (lexplt.dev@gmail.com)
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-2026
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
19#include <Ark/Compiler/Pass.hpp>
22
23namespace Ark::internal
24{
25 class ARK_API NameResolutionPass final : public Pass
26 {
27 public:
28 /**
29 * @brief Create a NameResolutionPass
30 * @param debug debug level
31 */
32 explicit NameResolutionPass(unsigned debug);
33
34 /**
35 * @brief Start visiting the given AST, checking for mutability violation and unbound variables
36 * @param ast AST to analyze
37 */
38 void process(const Node& ast);
39
40 /**
41 * @brief Unused overload that return the input AST (untouched as this pass only generates errors)
42 * @return const Node& ast
43 */
44 [[nodiscard]] const Node& ast() const noexcept;
45
46 /**
47 * @brief Register a symbol as defined, so that later we can throw errors on undefined symbols
48 *
49 * @param sym
50 * @param is_mutable true if the symbol is inside mut/set, false otherwise (let)
51 */
52 std::string addDefinedSymbol(const std::string& sym, bool is_mutable);
53
54 private:
56 std::unordered_set<std::string> m_language_symbols; ///< Precomputed set of language symbols that can't be used to define variables
57 std::vector<Node> m_symbol_nodes;
58 std::unordered_set<std::string> m_defined_symbols;
59 std::vector<std::string> m_plugin_names;
61
62 /**
63 * @brief Recursively visit nodes
64 * @param node node to visit
65 * @param register_declarations whether or not the visit should register declarations
66 */
67 void visit(Node& node, bool register_declarations);
68
69 /**
70 *
71 * @param node a list node whose first child is a keyword
72 * @param keyword
73 * @param register_declarations whether or not the visit should register declarations
74 */
75 void visitKeyword(Node& node, Keyword keyword, bool register_declarations);
76
77 /**
78 * @brief Register a given node in the symbol table
79 *
80 * @param symbol
81 * @param old_name old name for symbol, to replace it with the new one if it was renamed
82 */
83 void addSymbolNode(const Node& symbol, const std::string& old_name = "");
84
85 /**
86 * @brief Checking if a symbol may be coming from a plugin
87 *
88 * @param name symbol name
89 * @return true the symbol may be from a plugin, loaded at runtime
90 * @return false
91 */
92 [[nodiscard]] bool mayBeFromPlugin(const std::string& name) const noexcept;
93
94 std::string updateSymbolWithFullyQualifiedName(Node& symbol);
95
96 /**
97 * @brief Checks for undefined symbols, not present in the defined symbols table
98 *
99 */
100 void checkForUndefinedSymbol() const;
101
102 /**
103 * @brief Suggest a symbol of what the user may have meant to input
104 *
105 * @param str the string
106 * @return std::string
107 */
108 [[nodiscard]] std::string offerSuggestion(const std::string& str) const;
109 };
110}
111
112#endif // ARK_COMPILER_NAMERESOLUTIONPASS_HPP
#define ARK_API
Definition Module.hpp:22
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass.
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:32
An interface to describe compiler passes.
Definition Pass.hpp:24
Keyword
The different keywords available.
Definition Common.hpp:79