12#ifndef ARK_COMPILER_NAMERESOLUTIONPASS_HPP
13#define ARK_COMPILER_NAMERESOLUTIONPASS_HPP
18#include <unordered_set>
34struct std::hash<
Ark::internal::Variable>
38 return std::hash<std::string> {}(x.name);
77 [[nodiscard]] std::optional<bool>
isImmutable(
const std::string& name)
const;
84 [[nodiscard]]
bool isRegistered(
const std::string& name)
const;
92 [[nodiscard]]
bool isInScope(
const std::string& name)
const;
102 void add(
const std::string& name,
bool is_mutable);
109 [[nodiscard]] std::optional<Variable>
get(
const std::string& name)
const;
111 [[nodiscard]]
bool has(
const std::string& name)
const;
140 [[nodiscard]]
const Node&
ast()
const noexcept override;
185 [[nodiscard]]
bool mayBeFromPlugin(
const std::string& name)
const noexcept;
199 [[nodiscard]] std::string
offerSuggestion(
const std::string& str)
const;
Interface for a compiler pass (take in an AST, output an AST)
std::vector< std::string > m_plugin_names
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.
void addSymbolNode(const Node &symbol)
Register a given node in the symbol table.
std::string offerSuggestion(const std::string &str) const
Suggest a symbol of what the user may have meant to input.
void visit(const Node &node)
Recursively visit nodes.
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.
void addDefinedSymbol(const std::string &sym, bool is_mutable)
Register a symbol as defined, so that later we can throw errors on undefined symbols.
void visitKeyword(const Node &node, Keyword keyword)
NameResolutionPass(unsigned debug)
Create a NameResolutionPass.
ScopeResolver m_scope_resolver
std::vector< Node > m_symbol_nodes
A node of an Abstract Syntax Tree for ArkScript.
An interface to describe compiler passes.
bool has(const std::string &name) const
std::unordered_set< Variable > m_vars
void add(const std::string &name, bool is_mutable)
Add a variable to the scope, given a mutability status.
std::optional< Variable > get(const std::string &name) const
Try to return a variable from this scope with a given name.
std::vector< Scope > m_scopes
void removeLocalScope()
Remove the last scope.
ScopeResolver()
Create a ScopeResolver.
bool isRegistered(const std::string &name) const
Checks if any scope has 'name', in reverse order.
void createNew()
Create a new scope.
void registerInCurrent(const std::string &name, bool is_mutable)
Register a variable in the current (last) scope.
bool isInScope(const std::string &name) const
Checks if 'name' is in the current scope.
std::optional< bool > isImmutable(const std::string &name) const
Checks the scopes in reverse order for 'name' and returns its mutability status.
Keyword
The different keywords available.
bool operator==(const Variable &other) const =default
size_t operator()(const Ark::internal::Variable &x) const noexcept