Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NamespaceIndentation: All

SortIncludes: Never

SpaceInEmptyBlock: false
SpaceInEmptyBraces: Never
SpacesBeforeTrailingComments: 2

UseTab: Never
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/clang-format-action@6cd220de46c89139a0365edae93eee8eb30ca8fe # v4.16.0
with:
clang-format-version: '19'
clang-format-version: '23'
check-path: ${{ matrix.path }}
fallback-style: 'Mozilla' # optional

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

- uses: actions/setup-python@v6
with:
python-version: '3.9'
python-version: '3.12'

- run: |
sudo apt install -yq cppcheck
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
### Added

### Changed
- import solver tracks imports per file, to be used later by the name resolution pass
- C++ unittests now keep running even when ArkScript code can't compile, so that we can have a complete picture of the failing tests
- name resolution isn't based on import order any more (GitHub issue #712)

### Removed

Expand Down
2 changes: 1 addition & 1 deletion examples/closures.ark
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# this function will return a closure capturing the number given
# and modifying its value each time we'll call the closure, returning
# the new number
(let countdown-from (fun (number)
(let countdown-from (fun ((mut number))
(fun (&number) {
(set number (- number 1))
number })))
Expand Down
40 changes: 21 additions & 19 deletions include/Ark/Compiler/AST/Import.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ namespace Ark::internal
*/
std::string prefix;

/**
* @brief Package with all the segments
* @details Example: `(import foo.bar)` => `foo.bar`
* `(import foo.bar.egg:*)` => `foo.bar.egg`
* `(import foo :a :b :c)` => `foo`
*/
std::string package;

/**
* @brief Package with all the segments
* @details Example: `(import foo.bar)` => `{foo, bar}`
* `(import foo.bar.egg:*)` => `{foo, bar, egg}`
* `(import foo :a :b :c)` => `{foo}`
*/
std::vector<std::string> package;
std::vector<std::string> segments;

/**
* @brief Import with prefix (import package)
Expand All @@ -47,31 +55,16 @@ namespace Ark::internal
*/
std::vector<std::string> symbols;

/**
*
* @return a package string, eg a.b.c
*/
[[nodiscard]] std::string toPackageString() const
{
return std::accumulate(
package.begin() + 1,
package.end(),
package.front(),
[](const std::string& left, const std::string& right) {
return left + "." + right;
});
}

/**
*
* @return a package as a path, eg a/b/c
*/
[[nodiscard]] std::string packageToPath() const
{
return std::accumulate(
std::next(package.begin()),
package.end(),
package[0],
std::next(segments.begin()),
segments.end(),
segments[0],
[](const std::string& a, const std::string& b) {
return a + "/" + b;
});
Expand All @@ -88,6 +81,15 @@ namespace Ark::internal
return with_prefix && symbols.empty();
}
};

[[nodiscard]] inline bool operator==(const Import& lhs, const Import& rhs)
{
return lhs.prefix == rhs.prefix &&
lhs.package == rhs.package &&
lhs.with_prefix == rhs.with_prefix &&
lhs.is_glob == rhs.is_glob &&
lhs.symbols == rhs.symbols;
}
}

#endif
13 changes: 7 additions & 6 deletions include/Ark/Compiler/AST/Namespace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
#include <vector>
#include <memory>

#include <Ark/Compiler/AST/Import.hpp>

namespace Ark::internal
{
class Node;

struct Namespace
{
std::string name;
bool is_glob; // (import package:*)
bool with_prefix; // (import package)
std::vector<std::string> symbols; // (import package :a :b)
std::string name; ///< Prefix for the namespace, eg egg
std::string package; ///< Complete package name, eg foo.bar.egg
std::vector<std::string> symbols;
std::vector<Import> imports;
std::shared_ptr<Node> ast;
};

inline bool operator==(const Namespace& A, const Namespace& B)
{
return A.name == B.name && A.is_glob == B.is_glob &&
A.with_prefix == B.with_prefix;
return A.name == B.name && A.ast.get() == B.ast.get();
}

inline bool operator<([[maybe_unused]] const Namespace&, [[maybe_unused]] const Namespace&)
Expand Down
2 changes: 0 additions & 2 deletions include/Ark/Compiler/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ namespace Ark::internal
"del"
};

constexpr std::string_view HiddenSymbolSuffix = "#hidden";

namespace Language
{
constexpr std::string_view AppendInPlace = "append!";
Expand Down
28 changes: 16 additions & 12 deletions include/Ark/Compiler/NameResolution/NameResolutionPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ namespace Ark::internal
*/
explicit NameResolutionPass(unsigned debug, Statistics* stats_collector = nullptr);

/**
* @brief Configure the NameResolutionPass with the import list of the root package
*
* @param root_import_list
*/
void setup(const std::vector<Import>& root_import_list);

/**
* @brief Start visiting the given AST, checking for mutability violation and unbound variables
*
* @param ast AST to analyze
* @param ast AST to analyse
*/
void process(const Node& ast);

Expand All @@ -53,7 +60,7 @@ namespace Ark::internal
* @param sym
* @param is_mutable true if the symbol is inside mut/set, false otherwise (let)
*/
std::string addDefinedSymbol(const std::string& sym, bool is_mutable);
std::string qualifyAndRegisterSymbol(const std::string& sym, bool is_mutable);

private:
Node m_ast;
Expand All @@ -64,28 +71,25 @@ namespace Ark::internal
ScopeResolver m_scope_resolver;

/**
* @brief Recursively visit nodes
* @brief Recursively visit nodes and register variable declarations
*
* @param node node to visit
* @param register_declarations whether or not the visit should register declarations
*/
void visit(Node& node, bool register_declarations);
void registerDeclarations(Node& node);

/**
* @brief Recursively visit nodes and updates symbol names
*
* @param node a list node whose first child is a keyword
* @param keyword
* @param register_declarations whether or not the visit should register declarations
* @param node node to visit and update
*/
void visitKeyword(Node& node, Keyword keyword, bool register_declarations);
void qualifyNames(Node& node);

/**
* @brief Register a given node in the symbol table
*
* @param symbol
* @param old_name old name for symbol, to replace it with the new one if it was renamed
*/
void addSymbolNode(const Node& symbol, const std::string& old_name = "");
void savedAsKnownSymbol(const Node& symbol);

/**
* @brief Checking if a symbol may be coming from a plugin
Expand All @@ -96,7 +100,7 @@ namespace Ark::internal
*/
[[nodiscard]] bool mayBeFromPlugin(const std::string& name) const noexcept;

std::string updateSymbolWithFullyQualifiedName(Node& symbol);
void tryQualifyAndUpdateSymbol(Node& symbol);

/**
* @brief Checks for undefined symbols, not present in the defined symbols table
Expand Down
55 changes: 37 additions & 18 deletions include/Ark/Compiler/NameResolution/ScopeResolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,37 @@
#include <vector>
#include <utility>

#include <ankerl/unordered_dense.h>

#include <Ark/Compiler/NameResolution/StaticScope.hpp>

namespace Ark::internal
{
enum class QualifyingFailReason
{
NotInSymbolList,
NeedPrefixing
};

struct QualifiedName
{
struct Match
{
QualifyingFailReason reason;
std::string name;
};

std::optional<std::string> qualified_name;
std::vector<Match> alternatives;
};

class ScopeResolver
{
public:
/**
* @brief Create a ScopeResolver
* @details Kickstart by create a default global scope
*/
ScopeResolver();
ScopeResolver() = default;

ScopeResolver(const ScopeResolver&) = delete;
ScopeResolver& operator=(const ScopeResolver&) = delete;
Expand All @@ -47,12 +66,11 @@ namespace Ark::internal

/**
* @brief Create a new namespace scope
* @param name
* @param with_prefix
* @param is_glob
* @param symbols
* @param prefix
* @param package
* @param imports
*/
void createNewNamespace(const std::string& name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols);
void createNewNamespace(const std::string& prefix, const std::string& package, const std::vector<Import>& imports);

/**
* @brief Register a Declaration in the current (last) scope
Expand Down Expand Up @@ -92,21 +110,13 @@ namespace Ark::internal
*/
[[nodiscard]] bool isInScope(const std::string& name) const;

/**
* @brief Get a FQN from a variable name in the nearest scope it is declared in
*
* @param name
* @return std::string
*/
[[nodiscard]] std::string getFullyQualifiedNameInNearestScope(const std::string& name) const;

/**
* @brief Checks if a name can be fully qualified (allows only unprefixed names to be resolved by glob namespaces or inside their own namespace)
*
* @param name
* @return std::pair<bool, std::string> if the name can be fully qualified, first element is true ; second element is the FQN
* @return std::pair<std::optional<QualifyingFailReason>, std::string> if the name can be fully qualified, first element is nullopt ; second element is the FQN
*/
[[nodiscard]] std::pair<bool, std::string> canFullyQualifyName(const std::string& name);
[[nodiscard]] std::pair<std::optional<QualifyingFailReason>, std::string> tryFullyQualifyName(const std::string& name) const;

/**
* @brief Return a non-owning raw pointer to the current scope
Expand All @@ -118,8 +128,17 @@ namespace Ark::internal

private:
std::vector<std::unique_ptr<StaticScope>> m_scopes;
ankerl::unordered_dense::map<std::string, std::unique_ptr<StaticScope>> m_saved_scopes;

[[nodiscard]] StaticScope* currentNamespace() const;

[[nodiscard]] std::string currentNamespace() const;
/**
* @brief Get a FQN from a variable name in the nearest scope it is declared in
*
* @param name
* @return QualifiedName
*/
[[nodiscard]] QualifiedName getFullyQualifiedNameInNearestScope(const std::string& name) const;
};
}

Expand Down
Loading
Loading