Skip to content

Commit

Permalink
fix: once import not accounting for different aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
jumanji144 committed Feb 3, 2024
1 parent e8e3177 commit 45c4509
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ add_library(libpl ${LIBRARY_TYPE}
source/pl/lib/std/hash.cpp
source/pl/lib/std/random.cpp
source/pl/core/resolvers.cpp
source/pl/core/api.cpp
)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
Expand Down
12 changes: 12 additions & 0 deletions lib/include/pl/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,25 @@ namespace pl::api {
struct Source {
std::string content;
std::string source;
u32 id = 0;

static u32 idCounter;

Source(std::string content, std::string source) :
content(std::move(content)), source(std::move(source)), id(idCounter++) { }

Source() = default;

static constexpr auto DefaultSource = "<Source Code>";
static constexpr Source* NoSource = nullptr;
static constexpr Source Empty() {
return { "", "" };
}

constexpr auto operator<=>(const Source& other) const {
return this->id <=> other.id;
}

};

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/include/pl/core/parser_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ namespace pl::core {
}

private:
struct OnceIncludePair {
api::Source* source;
std::string alias;

auto operator<=>(const OnceIncludePair& other) const {
return std::tie(*this->source, this->alias) <=> std::tie(*other.source, other.alias);
}
};

std::set<OnceIncludePair> m_onceIncluded {};
api::Resolver m_resolver = nullptr;
PatternLanguage* m_patternLanguage = nullptr;
std::set<api::Source*> m_onceIncluded;
};

}
3 changes: 3 additions & 0 deletions lib/source/pl/core/api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <pl/api.hpp>

pl::u32 pl::api::Source::idCounter;
4 changes: 2 additions & 2 deletions lib/source/pl/core/parser_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pl::hlp::CompileResult<std::vector<std::shared_ptr<ast::ASTNode>>>
ParserManager::parse(api::Source *source, const std::string &namespacePrefix) {
using result_t = hlp::CompileResult<std::vector<std::shared_ptr<ast::ASTNode>>>;

if(m_onceIncluded.contains(source))
if(m_onceIncluded.contains( { source, namespacePrefix } ))
return result_t::good({});

auto parser = Parser();
Expand Down Expand Up @@ -44,7 +44,7 @@ ParserManager::parse(api::Source *source, const std::string &namespacePrefix) {
source->content = preprocessedCode.value();

if(preprocessor->shouldOnlyIncludeOnce())
m_onceIncluded.insert(source);
m_onceIncluded.insert( { source, namespacePrefix } );

auto [tokens, lexerErrors] = lexer->lex(source);

Expand Down
7 changes: 7 additions & 0 deletions tests/include/test_patterns/test_pattern_import.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ namespace pl::test {
[[nodiscard]] std::string getSourceCode() const override {
return R"(
import IA as A;
// once include tests
import IC as C; // should do nothing, IC as C was transitively imported from A
import IC; // should work as expected (import IC without any alias) [ c ]
import IC; // should do nothing, IC was already imported
import IC as C2; // should work as expected (import IC with alias C2) [ C2::c ]
fn main() {
A::a();
B::b();
C::c();
c();
C2::c();
};
)";
}
Expand Down

0 comments on commit 45c4509

Please sign in to comment.