diff --git a/develop/matchit.h b/develop/matchit.h new file mode 100644 index 0000000..e785ebe --- /dev/null +++ b/develop/matchit.h @@ -0,0 +1,6 @@ +// Replace include/matchit.h with this file when developing. + +#include "../develop/matchit/core.h" +#include "../develop/matchit/expression.h" +#include "../develop/matchit/patterns.h" +#include "../develop/matchit/utility.h" diff --git a/develop/matchit/patterns.h b/develop/matchit/patterns.h index 597504e..4a4029c 100644 --- a/develop/matchit/patterns.h +++ b/develop/matchit/patterns.h @@ -214,6 +214,22 @@ namespace matchit static_assert(std::is_same_v, int32_t const *>); static_assert(std::is_same_v, int32_t const &>); + template + struct AddConstToPointer + { + using type = std::conditional_t< + !std::is_pointer_v, T, + std::add_pointer_t>>>; + }; + template + using AddConstToPointerT = typename AddConstToPointer::type; + + static_assert(std::is_same_v, void const *>); + static_assert(std::is_same_v, int32_t>); + + template + using InternalPatternT = std::remove_reference_t>>; + template class PatternTraits; @@ -310,7 +326,7 @@ namespace matchit private: Pattern const mPattern; - Func const mHandler; + std::conditional_t, Func const &, Func const> mHandler; }; template @@ -439,7 +455,7 @@ namespace matchit constexpr auto const &patterns() const { return mPatterns; } private: - std::tuple mPatterns; + std::tuple...> mPatterns; }; template @@ -530,7 +546,7 @@ namespace matchit private: Unary const mUnary; - Pattern const mPattern; + InternalPatternT const mPattern; }; template @@ -602,7 +618,7 @@ namespace matchit constexpr auto const &patterns() const { return mPatterns; } private: - std::tuple mPatterns; + std::tuple...> mPatterns; }; template @@ -681,7 +697,7 @@ namespace matchit auto const &pattern() const { return mPattern; } private: - Pattern mPattern; + InternalPatternT mPattern; }; template @@ -944,22 +960,7 @@ namespace matchit constexpr explicit Ds(Patterns const &...patterns) : mPatterns{patterns...} {} constexpr auto const &patterns() const { return mPatterns; } - private: - template - struct AddConstToPointer - { - using type = std::conditional_t< - !std::is_pointer_v, T, - std::add_pointer_t>>>; - }; - template - using AddConstToPointerT = typename AddConstToPointer::type; - - static_assert(std::is_same_v, void const *>); - static_assert(std::is_same_v, int32_t>); - - public: - using Type = std::tuple>...>; + using Type = std::tuple...>; private: Type mPatterns; diff --git a/develop/matchit/utility.h b/develop/matchit/utility.h index 497c42e..ce98ad6 100644 --- a/develop/matchit/utility.h +++ b/develop/matchit/utility.h @@ -91,17 +91,19 @@ namespace matchit { return false; }); } - constexpr auto dsVia = [](auto &&...members) + constexpr auto dsVia = [](auto ...members) { - return [members...](auto &&...pats) + return [members...](auto ...pats) { return and_(app(members, pats)...); }; }; template - constexpr auto asDsVia = [](auto &&...members) + constexpr auto asDsVia = [](auto ...members) { - return [members...](auto &&...pats) + return [members...](auto ...pats) { + // FIXME, why the following line will cause segfault in at-Bindings.cpp + // return as(dsVia(members...)(pats...)); return as(and_(app(members, pats)...)); }; }; diff --git a/include/matchit.h b/include/matchit.h index a096394..29bcebb 100644 --- a/include/matchit.h +++ b/include/matchit.h @@ -521,6 +521,22 @@ namespace matchit static_assert(std::is_same_v, int32_t const *>); static_assert(std::is_same_v, int32_t const &>); + template + struct AddConstToPointer + { + using type = std::conditional_t< + !std::is_pointer_v, T, + std::add_pointer_t>>>; + }; + template + using AddConstToPointerT = typename AddConstToPointer::type; + + static_assert(std::is_same_v, void const *>); + static_assert(std::is_same_v, int32_t>); + + template + using InternalPatternT = std::remove_reference_t>>; + template class PatternTraits; @@ -617,7 +633,7 @@ namespace matchit private: Pattern const mPattern; - std::conditional_t, Func const&, Func const> mHandler; + std::conditional_t, Func const &, Func const> mHandler; }; template @@ -746,7 +762,7 @@ namespace matchit constexpr auto const &patterns() const { return mPatterns; } private: - std::tuple mPatterns; + std::tuple...> mPatterns; }; template @@ -837,7 +853,7 @@ namespace matchit private: Unary const mUnary; - Pattern const mPattern; + InternalPatternT const mPattern; }; template @@ -909,7 +925,7 @@ namespace matchit constexpr auto const &patterns() const { return mPatterns; } private: - std::tuple mPatterns; + std::tuple...> mPatterns; }; template @@ -988,7 +1004,7 @@ namespace matchit auto const &pattern() const { return mPattern; } private: - Pattern mPattern; + InternalPatternT mPattern; }; template @@ -1251,22 +1267,7 @@ namespace matchit constexpr explicit Ds(Patterns const &...patterns) : mPatterns{patterns...} {} constexpr auto const &patterns() const { return mPatterns; } - private: - template - struct AddConstToPointer - { - using type = std::conditional_t< - !std::is_pointer_v, T, - std::add_pointer_t>>>; - }; - template - using AddConstToPointerT = typename AddConstToPointer::type; - - static_assert(std::is_same_v, void const *>); - static_assert(std::is_same_v, int32_t>); - - public: - using Type = std::tuple>...>; + using Type = std::tuple...>; private: Type mPatterns; @@ -2023,17 +2024,19 @@ namespace matchit { return false; }); } - constexpr auto dsVia = [](auto &&...members) + constexpr auto dsVia = [](auto ...members) { - return [members...](auto &&...pats) + return [members...](auto ...pats) { return and_(app(members, pats)...); }; }; template - constexpr auto asDsVia = [](auto &&...members) + constexpr auto asDsVia = [](auto ...members) { - return [members...](auto &&...pats) + return [members...](auto ...pats) { + // FIXME, why the following line will cause segfault in at-Bindings.cpp + // return as(dsVia(members...)(pats...)); return as(and_(app(members, pats)...)); }; }; diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt index 2ed2e75..3200e13 100644 --- a/sample/CMakeLists.txt +++ b/sample/CMakeLists.txt @@ -49,6 +49,7 @@ Predicate-based-Discriminator Closed-Class-Hierarchy Matcher-within visit +graph ) foreach(sample ${MATCHIT_SAMPLES}) diff --git a/sample/graph.cpp b/sample/graph.cpp new file mode 100644 index 0000000..b91d0a8 --- /dev/null +++ b/sample/graph.cpp @@ -0,0 +1,57 @@ +#include "matchit.h" +#include +#include +#include +#include + +template +struct Node +{ + T value; + std::vector parents; +}; + +/* + A + | + B + | \ + | C + | / + D +*/ + +bool matchGraph() +{ + using StrNode = Node; + auto A = std::make_unique(StrNode{"A", {}}); + auto B = std::make_unique(StrNode{"B", {A.get()}}); + auto C = std::make_unique(StrNode{"C", {B.get()}}); + auto D = std::make_unique(StrNode{"D", {B.get(), C.get()}}); + + using namespace matchit; + // FIXME, moving dsN into someDsN will cause segfault. + constexpr auto dsN = dsVia(&StrNode::value, &StrNode::parents); + constexpr auto someDsN = [dsN](auto... pats) + { + return some(dsN(pats...)); + }; + Id b; + return match(D)( + pattern | someDsN("D", + ds( + b.at( + someDsN("B", + ds(someDsN("A", ds())))), + someDsN("C", ds(b)))) = [] + { + std::cout << "Matched!" << std::endl; + return true; + }); +} + +int main() +{ + assert(matchGraph()); + return 0; +} \ No newline at end of file