10 Pass(
"NameResolution", debug)
53 return fully_qualified_name;
62 const std::string old_name = node.
string();
69 for (std::size_t i = 0, end = node.
list().size(); i < end; ++i)
75 const std::string old_name = child.
string();
98 const auto funcname = node.
constList()[0].string();
99 const auto arg = node.
constList()[1].string();
103 fmt::format(
"MutabilityError: Can not modify the constant list `{}' using `{}'", arg, funcname),
109 for (std::size_t i = 2, end = node.
constList().size(); i < end; ++i)
113 fmt::format(
"MutabilityError: Can not {} the list `{}' to itself", funcname, arg),
119 for (
auto& child : node.
list())
120 visit(child, register_declarations);
134 visit(*namespace_.ast,
true);
136 visit(*namespace_.ast,
false);
139 if (!namespace_.symbols.empty())
141 const auto it = std::ranges::find_if(
143 [&scope, &namespace_](
const std::string& sym) ->
bool {
144 return !scope->get(sym, namespace_.name, true).has_value();
147 if (it != namespace_.symbols.end())
149 fmt::format(
"ImportError: Can not import symbol {} from {}, as it isn't in the package", *it, namespace_.name),
174 visit(node.
list()[2], register_declarations);
177 const std::string& name = node.
constList()[1].string();
180 fmt::format(
"Can not use a reserved identifier ('{}') as a {} name.", name, keyword ==
Keyword::Let ?
"constant" :
"variable"),
185 fmt::format(
"MutabilityError: Can not use 'let' to redefine variable `{}'", name),
191 fmt::format(
"MutabilityError: Can not set the constant `{}' to {}", name, node.
constList()[2].repr()),
201 if (register_declarations)
202 node.
list()[1].setString(fully_qualified_name);
215 for (
auto& child : node.
list())
216 visit(child, register_declarations);
227 for (
auto& child : node.
list()[1].list())
233 fmt::format(
"Can not capture `{}' because it is referencing a variable defined in an unreachable scope.", child.string()),
238 if (!child.getUnqualifiedName())
240 child.setUnqualifiedName(child.string());
245 std::string old_name = child.string();
256 visit(node.
list()[2], register_declarations);
263 for (
auto& child : node.
list())
264 visit(child, register_declarations);
271 const std::string& name = symbol.
string();
278 if (!old_name.empty())
280 auto it = std::ranges::find_if(
m_symbol_nodes, [&old_name, &symbol](
const Node& sym_node) ->
bool {
281 return sym_node.
string() == old_name &&
292 const auto it = std::ranges::find_if(
m_symbol_nodes, [&name](
const Node& sym_node) ->
bool {
293 return sym_node.
string() == name;
302 const auto it = std::ranges::find_if(
304 [&splitted](
const std::string& plugin) ->
bool {
305 return plugin == splitted;
307 return it != m_plugin_names.end();
318 "Symbol `{}' was resolved to `{}', which is also a builtin name. Either the symbol or the package it's in needs to be renamed to avoid conflicting with the builtin.",
325 if (fqn.ends_with(
"#hidden"))
326 message = fmt::format(
327 R
"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to add it to the symbol list while importing?)",
329 fqn.substr(0, fqn.find_first_of('#')));
331 message = fmt::format(R
"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to prefix it with its namespace?)", symbol.string(), fqn);
347 const auto& str = sym.string();
355 if (suggestion.empty())
356 message = fmt::format(R
"(Unbound variable error "{}" (variable is used but not defined))", str);
359 const std::string prefix = suggestion.substr(0, suggestion.find_first_of(
':'));
360 const std::string note_about_prefix = fmt::format(
361 " You either forgot to import it in the symbol list (eg `(import {} :{})') or need to fully qualify it by adding the namespace",
364 const bool add_note = suggestion.ends_with(
":" + str);
365 message = fmt::format(R
"(Unbound variable error "{}" (did you mean "{}"?{}))", str, suggestion, add_note ? note_about_prefix : "");
375 auto iterate = [](
const std::string& word,
const std::unordered_set<std::string>& dict) -> std::string {
376 std::string suggestion;
378 std::size_t suggestion_distance = word.size() / 2;
379 for (
const std::string& symbol : dict)
382 if (current_distance <= suggestion_distance)
384 suggestion_distance = current_distance;
393 if (suggestion.empty())
396 if (suggestion.empty())
398 if (
const auto it = std::ranges::find_if(
m_defined_symbols, [&str](
const std::string& symbol) {
399 return symbol.ends_with(
":" + str);
Lots of utilities about string, filesystem and more.
Host the declaration of all the ArkScript builtins.
ArkScript homemade exceptions.
Resolves names and fully qualify them in the AST (prefixing them with the package they are from)
void trace(const char *fmt, Args &&... args)
Write a trace level log using fmtlib.
void traceStart(std::string &&trace_name)
std::vector< std::string > m_plugin_names
const Node & ast() const noexcept
Unused overload that return the input AST (untouched as this pass only generates errors)
void visit(Node &node, bool register_declarations)
Recursively visit nodes.
void visitKeyword(Node &node, Keyword keyword, bool register_declarations)
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.
void process(const Node &ast)
Start visiting the given AST, checking for mutability violation and unbound 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.
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.
ScopeResolver m_scope_resolver
std::vector< Node > m_symbol_nodes
A node of an Abstract Syntax Tree for ArkScript.
NodeType nodeType() const noexcept
Return the node type.
const std::string & filename() const noexcept
Return the filename in which this node was created.
const std::string & string() const noexcept
Return the string held by the value (if the node type allows it)
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Namespace & arkNamespace() noexcept
Return the namespace held by the value (if the node type allows it)
std::ostream & debugPrint(std::ostream &os) const noexcept
Print a node to an output stream with added type annotations.
FileSpan position() const noexcept
Get the span of the node (start and end)
void setString(const std::string &value) noexcept
Set the String object.
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
An interface to describe compiler passes.
std::string registerInCurrent(const std::string &name, bool is_mutable)
Register a Declaration in the current (last) scope.
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.
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...
std::vector< std::string > splitString(const std::string &source, const char sep)
Cut a string into pieces, given a character separator.
ARK_API std::size_t levenshteinDistance(const std::string &str1, const std::string &str2)
Calculate the Levenshtein distance between two strings.
ARK_API const std::vector< std::pair< std::string, Value > > builtins
constexpr std::array< std::string_view, 9 > listInstructions
constexpr std::string_view AppendInPlace
constexpr std::string_view Apply
constexpr std::array< std::string_view, 24 > operators
constexpr std::string_view ConcatInPlace
constexpr std::string_view SysArgs
constexpr std::string_view And
constexpr std::array UpdateRef
All the builtins that modify in place a variable.
constexpr std::string_view Or
constexpr std::string_view SysProgramName
Keyword
The different keywords available.
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)