FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add predicate support for `is` outside `inspect`, see #90 · iopenstack/cppfront@327ceeb · GitHub

Repository navigation

Commit 327ceeb

Browse files
committed
Add predicate support for is outside inspect, see hsutter#90
1 parent 68e465a commit 327ceeb

17 files changed

Lines changed: 180 additions & 26 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
#include <iostream>
3+
#include <vector>
4+
5+
constexpr auto less_then = [](int value) {
6+
return [=](auto x) { return x < value;};
7+
};
8+
9+
constexpr auto in = [](auto min, auto max) {
10+
return [=](auto x) {
11+
return min <= x && x <= max;
12+
};
13+
};
14+
15+
constexpr auto empty = [](auto&& x){
16+
return std::empty(x);
17+
};
18+
19+
main: () -> int = {
20+
i := 15;
21+
22+
std::cout << inspect i -> std::string {
23+
is (less_then(10)) = "i less than 10";
24+
is (in(11,20)) = "i is between 11 and 20";
25+
is _ = "i is out of our interest";
26+
} << std::endl;
27+
28+
if i is less_then(20) {
29+
std::cout << "less than 20" << std::endl;
30+
}
31+
32+
if i is in(10,30) {
33+
std::cout << "i is between 10 and 30" << std::endl;
34+
}
35+
36+
v : std::vector<int> = ();
37+
38+
if empty(v) {
39+
std::cout << "v is empty" << std::endl;
40+
}
41+
42+
if v is (empty) {
43+
std::cout << "v is empty" << std::endl;
44+
}
45+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
i is between 11 and 20
2+
less than 20
3+
i is between 10 and 30
4+
v is empty
5+
v is empty

‎regression-tests/test-results/clang-12/mixed-inspect-values-2.cpp.output‎

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
i is between 11 and 20
2+
less than 20
3+
i is between 10 and 30
4+
v is empty
5+
v is empty

‎regression-tests/test-results/gcc-10/mixed-inspect-values-2.cpp.output‎

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// ----- Cpp2 support -----
2+
#include "cpp2util.h"
3+
4+
#line 1 "mixed-inspect-values-2.cpp2"
5+
6+
#include <iostream>
7+
#include <vector>
8+
9+
constexpr auto less_then = [](int value) {
10+
return [=](auto x) { return x < value;};
11+
};
12+
13+
constexpr auto in = [](auto min, auto max) {
14+
return [=](auto x) {
15+
return min <= x && x <= max;
16+
};
17+
};
18+
19+
constexpr auto empty = [](auto&& x){
20+
return std::empty(x);
21+
};
22+
23+
[[nodiscard]] auto main() -> int;
24+
25+
//=== Cpp2 definitions ==========================================================
26+
27+
#line 18 "mixed-inspect-values-2.cpp2"
28+
29+
[[nodiscard]] auto main() -> int{
30+
auto i { 15 };
31+
32+
std::cout << [&] () -> std::string { auto&& __expr = i;
33+
if (cpp2::is(__expr, (less_then(10)))) { if constexpr( requires{"i less than 10";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("i less than 10")),std::string> ) return "i less than 10"; else return std::string{}; else return std::string{}; }
34+
else if (cpp2::is(__expr, in(11, 20))) { if constexpr( requires{"i is between 11 and 20";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("i is between 11 and 20")),std::string> ) return "i is between 11 and 20"; else return std::string{}; else return std::string{}; }
35+
else return "i is out of our interest"; }
36+
() << std::endl;
37+
38+
if (cpp2::is(i, less_then(20))) {
39+
std::cout << "less than 20" << std::endl;
40+
}
41+
42+
if (cpp2::is(std::move(i), in(10, 30))) {
43+
std::cout << "i is between 10 and 30" << std::endl;
44+
}
45+
46+
std::vector<int> v { };
47+
48+
if (empty(v)) {
49+
std::cout << "v is empty" << std::endl;
50+
}
51+
52+
if (cpp2::is(std::move(v), (empty))) {
53+
std::cout << "v is empty" << std::endl;
54+
}
55+
}
56+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-inspect-values-2.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+

‎regression-tests/test-results/mixed-inspect-values.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ auto test(auto const& x) -> void{
4747
std::cout << [&] () -> std::string { auto&& __expr = x;
4848
if (cpp2::is(__expr, 0)) { if constexpr( requires{"zero";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("zero")),std::string> ) return "zero"; else return std::string{}; else return std::string{}; }
4949
else if (cpp2::is(__expr, (in(1, 2)))) { if constexpr( requires{"1 or 2";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("1 or 2")),std::string> ) return "1 or 2"; else return std::string{}; else return std::string{}; }
50-
else if (cpp2::is(__expr, (in_2_3))) { if constexpr( requires{"3";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("3")),std::string> ) return "3"; else return std::string{}; else return std::string{}; }
51-
else if (cpp2::is(__expr, (std::move(forty_two)))) { if constexpr( requires{"the answer";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("the answer")),std::string> ) return "the answer"; else return std::string{}; else return std::string{}; }
50+
else if (cpp2::is(__expr, in_2_3)) { if constexpr( requires{"3";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("3")),std::string> ) return "3"; else return std::string{}; else return std::string{}; }
51+
else if (cpp2::is(__expr, std::move(forty_two))) { if constexpr( requires{"the answer";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("the answer")),std::string> ) return "the answer"; else return std::string{}; else return std::string{}; }
5252
else if (cpp2::is<int>(__expr)) { if constexpr( requires{"integer " + cpp2::to_string(x);} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("integer " + cpp2::to_string(x))),std::string> ) return "integer " + cpp2::to_string(x); else return std::string{}; else return std::string{}; }
5353
else if (cpp2::is<std::string>(__expr)) { if constexpr( requires{cpp2::as<std::string>(x);} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((cpp2::as<std::string>(x))),std::string> ) return cpp2::as<std::string>(x); else return std::string{}; else return std::string{}; }
5454
else return "(no match)"; }
55-
()
56-
<< "\n";}
55+
() << "\n";
56+
}
5757

‎regression-tests/test-results/mixed-inspect-with-typeof-of-template-arg-list.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ auto calc() {
2020
return [&] () -> int { auto&& __expr = v;
2121
if (cpp2::is<int>(__expr)) { if constexpr( requires{calc<1,2>();} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((calc<1,2>())),int> ) return calc<1,2>(); else return int{}; else return int{}; }
2222
else return 0; }
23-
()
24-
; }
23+
();
24+
}
2525

2626
[[nodiscard]] auto main() -> int{
2727
return fun(42);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
i is between 11 and 20
2+
less than 20
3+
i is between 10 and 30
4+
v is empty
5+
v is empty

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL