forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDereference-Pattern.cpp
More file actions
41 lines (35 loc) · 837 Bytes
/
Dereference-Pattern.cpp
File metadata and controls
41 lines (35 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "matchit.h"
#include <iostream>
#include <memory>
struct Node
{
int value;
std::unique_ptr<Node> lhs, rhs;
};
bool operator==(Node const &lhs, Node const &rhs)
{
return lhs.value == rhs.value && lhs.lhs == rhs.lhs && lhs.rhs == rhs.rhs;
}
void print_leftmost(const Node &node)
{
auto deref = [](auto &&e) -> decltype(auto)
{ return *e; };
using namespace matchit;
Id<int> v;
Id<Node> l;
using N = Node;
match(node)(
pattern | and_(app(&N::value, v),
app(&N::lhs, nullptr)) = [&]
{ std::cout << *v << '\n'; },
pattern | app(&N::lhs, app(deref, l)) = [&]
{ print_leftmost(*l); }
// ˆˆˆˆˆˆˆˆˆˆˆˆˆ dereference pattern
);
}
int32_t main()
{
const auto n = Node{4, {}, {}};
print_leftmost(n);
return 0;
}