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
5 * @version 1.0
6 * @date 2024-07-22
7 *
8 * @copyright Copyright (c) 2024
9 *
10 */
11
12#ifndef ARK_COMPILER_NAMERESOLUTIONPASS_HPP
13#define ARK_COMPILER_NAMERESOLUTIONPASS_HPP
14
15#include <vector>
16#include <string>
17#include <unordered_set>
18
19#include <Ark/Compiler/Pass.hpp>
21
22namespace Ark::internal
23{
24 class 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
Interface for a compiler pass (take in an AST, output an AST)
Handle scope resolution at compile time.
std::vector< std::string > m_plugin_names
void visit(Node &node, bool register_declarations)
Recursively visit nodes.
void visitKeyword(Node &node, Keyword keyword, bool register_declarations)
const Node & ast() const noexcept override
Unused overload that return the input AST (untouched as this pass only generates errors)
void checkForUndefinedSymbol() const
Checks for undefined symbols, not present in the defined symbols table.
std::string offerSuggestion(const std::string &str) const
Suggest a symbol of what the user may have meant to input.
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
bool mayBeFromPlugin(const std::string &name) const noexcept
Checking if a symbol may be coming from a plugin.
void process(const Node &ast) override
Start visiting the given AST, checking for mutability violation and unbound variables.
std::string addDefinedSymbol(const std::string &sym, bool is_mutable)
Register a symbol as defined, so that later we can throw errors on undefined symbols.
std::string updateSymbolWithFullyQualifiedName(Node &symbol)
void addSymbolNode(const Node &symbol, const std::string &old_name="")
Register a given node in the symbol table.
NameResolutionPass(unsigned debug)
Create a NameResolutionPass.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:31
An interface to describe compiler passes.
Definition Pass.hpp:23
Keyword
The different keywords available.
Definition Common.hpp:60