Skip to content

Commit 29fbd92

Browse files
committed
Closes #90: Add inspect predicate match, as extension of value match
1 parent 2f37277 commit 29fbd92

13 files changed

+234
-91
lines changed

include/cpp2util.h

Lines changed: 164 additions & 77 deletions
Large diffs are not rendered by default.

regression-tests/pure2-inspect-values.cpp2 renamed to regression-tests/mixed-inspect-values.cpp2

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
auto in(int min, int max) {
2+
return [=](int x){ return min <= x && x <= max; };
3+
}
4+
5+
in_2_3: (x: int) -> bool = 2 <= x && x <= 3;
6+
17
main: ()->int = {
28
v: std::variant<double, std::string, double> = ();
3-
test(v);
49
v = "rev dodgson";
510
test(v);
611

@@ -15,6 +20,9 @@ main: ()->int = {
1520
test(a);
1621

1722
test(0);
23+
test(1);
24+
test(2);
25+
test(3);
1826
test(-42);
1927
test("xyzzy" as std::string);
2028
test(3.14);
@@ -24,6 +32,8 @@ test: (x:_) = {
2432
forty_two := 42;
2533
std::cout << inspect x -> std::string {
2634
is 0 = "zero";
35+
is (in(1,2)) = "1 or 2";
36+
is (in_2_3) = "3";
2737
is (forty_two) = "the answer";
2838
is int = "integer " + cpp2::to_string(x);
2939
is std::string = x as std::string;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
rev dodgson
2+
(no match)
3+
the answer
4+
zero
5+
plugh
6+
zero
7+
1 or 2
8+
1 or 2
9+
3
10+
integer -42
11+
xyzzy
12+
3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
rev dodgson
2+
(no match)
3+
the answer
4+
zero
5+
plugh
6+
zero
7+
1 or 2
8+
1 or 2
9+
3
10+
integer -42
11+
xyzzy
12+
3

regression-tests/test-results/pure2-inspect-values.cpp renamed to regression-tests/test-results/mixed-inspect-values.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
// ----- Cpp2 support -----
2-
#define CPP2_USE_MODULES Yes
32
#include "cpp2util.h"
43

4+
#line 1 "mixed-inspect-values.cpp2"
5+
auto in(int min, int max) {
6+
return [=](int x){ return min <= x && x <= max; };
7+
}
58

6-
#line 1 "pure2-inspect-values.cpp2"
9+
[[nodiscard]] auto in_2_3(cpp2::in<int> x) -> bool;
10+
#line 7 "mixed-inspect-values.cpp2"
711
[[nodiscard]] auto main() -> int;
8-
#line 23 "pure2-inspect-values.cpp2"
12+
#line 31 "mixed-inspect-values.cpp2"
913
auto test(auto const& x) -> void;
1014

1115
//=== Cpp2 definitions ==========================================================
1216

13-
#line 1 "pure2-inspect-values.cpp2"
17+
#line 4 "mixed-inspect-values.cpp2"
18+
19+
[[nodiscard]] auto in_2_3(cpp2::in<int> x) -> bool { return 2 <= x && x <= 3; }
20+
1421
[[nodiscard]] auto main() -> int{
1522
std::variant<double,std::string,double> v { };
16-
test(v);
1723
v = "rev dodgson";
1824
test(std::move(v));
1925

@@ -28,6 +34,9 @@ auto test(auto const& x) -> void;
2834
test(std::move(a));
2935

3036
test(0);
37+
test(1);
38+
test(2);
39+
test(3);
3140
test(-42);
3241
test(cpp2::as< std::string>("xyzzy"));
3342
test(3.14);
@@ -37,7 +46,9 @@ auto test(auto const& x) -> void{
3746
auto forty_two { 42 };
3847
std::cout << [&] () -> std::string { auto&& __expr = x;
3948
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{}; }
40-
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{}; }
49+
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{}; }
4152
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{}; }
4253
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{}; }
4354
else return "(no match)"; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-inspect-values.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
rev dodgson
2+
(no match)
3+
the answer
4+
zero
5+
plugh
6+
zero
7+
1 or 2
8+
1 or 2
9+
3
10+
integer -42
11+
xyzzy
12+
3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mixed-inspect-values.cpp

regression-tests/test-results/msvc-2022/pure2-inspect-values.cpp.output

Lines changed: 0 additions & 2 deletions
This file was deleted.

regression-tests/test-results/pure2-inspect-values.cpp2.output

Lines changed: 0 additions & 2 deletions
This file was deleted.

source/parse.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ struct function_type_node
867867
enum active { empty = 0, id, list };
868868
std::variant<
869869
std::monostate,
870-
std::unique_ptr<id_expression_node>,
870+
std::unique_ptr<type_id_node>,
871871
std::unique_ptr<parameter_declaration_list_node>
872872
> returns;
873873

@@ -2824,7 +2824,7 @@ class parser
28242824
//G throws
28252825
//G
28262826
//G return-list:
2827-
//G -> id-expression
2827+
//G -> type-id
28282828
//G -> parameter_declaration_list
28292829
//G
28302830
//G contract-seq:
@@ -2853,7 +2853,7 @@ class parser
28532853
{
28542854
next();
28552855

2856-
if (auto t = id_expression()) {
2856+
if (auto t = type_id()) {
28572857
n->returns = std::move(t);
28582858
}
28592859
else if (auto returns_list = parameter_declaration_list(true)) {

0 commit comments

Comments
 (0)