Skip to content

Commit 6553e5f

Browse files
committed
Add support for try/catch
Including regression test cases TODO: write docs Basic usage is try { ... } catch( i: int ) { use(i); } // catch an int catch( _ ) { } // catch everything
1 parent c638b16 commit 6553e5f

17 files changed

+542
-39
lines changed

regression-tests/pure2-print.cpp2

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ outer: @print type = {
102102

103103
y: (_: type_of(0)) = { }
104104

105+
ff: (_: int) = { }
106+
107+
try_catch: () =
108+
{
109+
fptr := ff&;
110+
try {
111+
fptr(-666);
112+
} catch (i: int) {
113+
std::cout << "oh look, a thrown int: " << i << std::endl;
114+
}
115+
try {
116+
ff(-42);
117+
} catch (_) {
118+
std::cout << "oh look, another exception" << std::endl;
119+
}
120+
}
121+
105122
}
106123

107124
main: () = {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
throwing_pred: (_: int) -> bool
2+
= {
3+
throw(42);
4+
return false;
5+
}
6+
7+
f: (x: int)
8+
pre( throwing_pred(x), "a throwing predicate failed, oh my" )
9+
= { }
10+
11+
main: () =
12+
{
13+
fptr := f&;
14+
try {
15+
fptr(-666);
16+
} catch (i: int) {
17+
std::cout << "oh look, a thrown int: " << i << std::endl;
18+
}
19+
try {
20+
f(-42);
21+
} catch (_) {
22+
std::cout << "oh look, another exception" << std::endl;
23+
}
24+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
oh look, a thrown int: 42
2+
oh look, another exception
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
oh look, a thrown int: 42
2+
oh look, another exception
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
oh look, a thrown int: 42
2+
oh look, another exception
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
diff(x + y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
2+
r = 5.000000
3+
d1 = 3.000000
4+
d2 = 0.000000
5+
d3 = 0.000000
6+
d4 = 0.000000
7+
d5 = 0.000000
8+
d6 = 0.000000
9+
diff(x + y + x) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
10+
r = 7.000000
11+
d1 = 4.000000
12+
d2 = 0.000000
13+
d3 = 0.000000
14+
d4 = 0.000000
15+
d5 = 0.000000
16+
d6 = 0.000000
17+
diff(x - y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
18+
r = -1.000000
19+
d1 = -1.000000
20+
d2 = 0.000000
21+
d3 = 0.000000
22+
d4 = 0.000000
23+
d5 = 0.000000
24+
d6 = 0.000000
25+
diff(x - y - x) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
26+
r = -3.000000
27+
d1 = -2.000000
28+
d2 = 0.000000
29+
d3 = 0.000000
30+
d4 = 0.000000
31+
d5 = 0.000000
32+
d6 = 0.000000
33+
diff(x + y - x) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
34+
r = 3.000000
35+
d1 = 2.000000
36+
d2 = 0.000000
37+
d3 = 0.000000
38+
d4 = 0.000000
39+
d5 = 0.000000
40+
d6 = 0.000000
41+
diff(x * y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
42+
r = 6.000000
43+
d1 = 7.000000
44+
d2 = 4.000000
45+
d3 = 0.000000
46+
d4 = 0.000000
47+
d5 = 0.000000
48+
d6 = 0.000000
49+
diff(x * y * x) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
50+
r = 12.000000
51+
d1 = 20.000000
52+
d2 = 22.000000
53+
d3 = 12.000000
54+
d4 = 0.000000
55+
d5 = 0.000000
56+
d6 = 0.000000
57+
diff(x / y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
58+
r = 0.666667
59+
d1 = -0.111111
60+
d2 = 0.148148
61+
d3 = -0.296296
62+
d4 = 0.790123
63+
d5 = -2.633745
64+
d6 = 10.534979
65+
diff(x / y / y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
66+
r = 0.222222
67+
d1 = -0.185185
68+
d2 = 0.296296
69+
d3 = -0.691358
70+
d4 = 2.106996
71+
d5 = -7.901235
72+
d6 = 35.116598
73+
diff(x * y / x) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
74+
r = 3.000000
75+
d1 = 2.000000
76+
d2 = 0.000000
77+
d3 = 0.000000
78+
d4 = 0.000000
79+
d5 = 0.000000
80+
d6 = 0.000000
81+
diff(x * (x + y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
82+
r = 10.000000
83+
d1 = 11.000000
84+
d2 = 6.000000
85+
d3 = 0.000000
86+
d4 = 0.000000
87+
d5 = 0.000000
88+
d6 = 0.000000
89+
diff(x + x * y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
90+
r = 8.000000
91+
d1 = 8.000000
92+
d2 = 4.000000
93+
d3 = 0.000000
94+
d4 = 0.000000
95+
d5 = 0.000000
96+
d6 = 0.000000
97+
diff(+x + y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
98+
r = 5.000000
99+
d1 = 3.000000
100+
d2 = 0.000000
101+
d3 = 0.000000
102+
d4 = 0.000000
103+
d5 = 0.000000
104+
d6 = 0.000000
105+
diff(-x + y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
106+
r = 1.000000
107+
d1 = 1.000000
108+
d2 = 0.000000
109+
d3 = 0.000000
110+
d4 = 0.000000
111+
d5 = 0.000000
112+
d6 = 0.000000
113+
diff(x * func(x, y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
114+
r = 10.000000
115+
d1 = 11.000000
116+
d2 = 6.000000
117+
d3 = 0.000000
118+
d4 = 0.000000
119+
d5 = 0.000000
120+
d6 = 0.000000
121+
diff(x * func_outer(x, y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
122+
r = 10.000000
123+
d1 = 11.000000
124+
d2 = 6.000000
125+
d3 = 0.000000
126+
d4 = 0.000000
127+
d5 = 0.000000
128+
d6 = 0.000000
129+
diff(sin(x - y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
130+
r = -0.841471
131+
d1 = -0.540302
132+
d2 = 0.841471
133+
d3 = 0.540302
134+
d4 = -0.841471
135+
d5 = -0.540302
136+
d6 = 0.841471
137+
diff(if branch) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
138+
r = 2.000000
139+
d1 = 1.000000
140+
d2 = 0.000000
141+
d3 = 0.000000
142+
d4 = 0.000000
143+
d5 = 0.000000
144+
d6 = 0.000000
145+
diff(if else branch) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
146+
r = 2.000000
147+
d1 = 1.000000
148+
d2 = 0.000000
149+
d3 = 0.000000
150+
d4 = 0.000000
151+
d5 = 0.000000
152+
d6 = 0.000000
153+
diff(direct return) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
154+
r = 5.000000
155+
d1 = 3.000000
156+
d2 = 0.000000
157+
d3 = 0.000000
158+
d4 = 0.000000
159+
d5 = 0.000000
160+
d6 = 0.000000
161+
diff(intermediate var) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
162+
r = 5.000000
163+
d1 = 3.000000
164+
d2 = 0.000000
165+
d3 = 0.000000
166+
d4 = 0.000000
167+
d5 = 0.000000
168+
d6 = 0.000000
169+
diff(intermediate passive var) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
170+
r = 5.000000
171+
d1 = 3.000000
172+
d2 = 0.000000
173+
d3 = 0.000000
174+
d4 = 0.000000
175+
d5 = 0.000000
176+
d6 = 0.000000
177+
diff(intermediate untyped) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
178+
r = 5.000000
179+
d1 = 3.000000
180+
d2 = 0.000000
181+
d3 = 0.000000
182+
d4 = 0.000000
183+
d5 = 0.000000
184+
d6 = 0.000000
185+
diff(intermediate default init) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
186+
r = 5.000000
187+
d1 = 3.000000
188+
d2 = 0.000000
189+
d3 = 0.000000
190+
d4 = 0.000000
191+
d5 = 0.000000
192+
d6 = 0.000000
193+
diff(intermediate no init) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
194+
r = 5.000000
195+
d1 = 3.000000
196+
d2 = 0.000000
197+
d3 = 0.000000
198+
d4 = 0.000000
199+
d5 = 0.000000
200+
d6 = 0.000000
201+
diff(while loop) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
202+
r = 8.000000
203+
d1 = 5.000000
204+
d2 = 0.000000
205+
d3 = 0.000000
206+
d4 = 0.000000
207+
d5 = 0.000000
208+
d6 = 0.000000
209+
diff(do while loop) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
210+
r = 8.000000
211+
d1 = 5.000000
212+
d2 = 0.000000
213+
d3 = 0.000000
214+
d4 = 0.000000
215+
d5 = 0.000000
216+
d6 = 0.000000
217+
diff(for loop) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
218+
r = 5.000000
219+
d1 = 3.000000
220+
d2 = 0.000000
221+
d3 = 0.000000
222+
d4 = 0.000000
223+
d5 = 0.000000
224+
d6 = 0.000000
225+
diff(tye_outer.a + y) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
226+
r = 5.000000
227+
d1 = 3.000000
228+
d2 = 0.000000
229+
d3 = 0.000000
230+
d4 = 0.000000
231+
d5 = 0.000000
232+
d6 = 0.000000
233+
diff(type_outer.add(y)) at (x = 2.000000, x_d = ( 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ), y = 3.000000, y_d = ( 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 )):
234+
r = 5.000000
235+
d1 = 3.000000
236+
d2 = 0.000000
237+
d3 = 0.000000
238+
d4 = 0.000000
239+
d5 = 0.000000
240+
d6 = 0.000000
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
11
pure2-autodiff-higher-order.cpp
2-
C:\github\cppfront\include\cpp2util.h(1250): error C7595: 'std::source_location::current': call to immediate function is not a constant expression
3-
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include\source_location(37): note: failure was caused by attempting to access a member on an object of dynamic type 'std::source_location' in which the member is not defined
4-
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include\source_location(37): note: see usage of 'std::source_location::source_location'
5-
C:\github\cppfront\include\cpp2util.h(1250): note: the call stack of the evaluation (the oldest call first) is
6-
C:\github\cppfront\include\cpp2util.h(1250): note: while evaluating function 'std::source_location std::source_location::current(const uint_least32_t,const uint_least32_t,const char *const ,const char *const ) noexcept'
7-
C:\github\cppfront\include\cpp2util.h(1250): note: the template instantiation context (the oldest one first) is
8-
pure2-autodiff-higher-order.cpp2(14): note: see reference to class template instantiation 'cpp2::taylor<double,6>' being compiled
9-
cpp2taylor.h2(16): note: while compiling class template member function 'cpp2::taylor<double,6>::taylor(const std::initializer_list<R> &)'
10-
with
11-
[
12-
R=double
13-
]
14-
pure2-autodiff-higher-order.cpp2(25): note: see the first reference to 'cpp2::taylor<double,6>::taylor' in 'ad_name::func_outer_d'
15-
cpp2taylor.h2(35): note: while compiling class template member function 'void cpp2::taylor<double,6>::set(const int,const double) &'
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
11
pure2-autodiff.cpp
2-
C:\github\cppfront\include\cpp2util.h(1250): error C7595: 'std::source_location::current': call to immediate function is not a constant expression
3-
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include\source_location(37): note: failure was caused by attempting to access a member on an object of dynamic type 'std::source_location' in which the member is not defined
4-
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include\source_location(37): note: see usage of 'std::source_location::source_location'
5-
C:\github\cppfront\include\cpp2util.h(1250): note: the call stack of the evaluation (the oldest call first) is
6-
C:\github\cppfront\include\cpp2util.h(1250): note: while evaluating function 'std::source_location std::source_location::current(const uint_least32_t,const uint_least32_t,const char *const ,const char *const ) noexcept'
7-
C:\github\cppfront\include\cpp2util.h(1250): note: the template instantiation context (the oldest one first) is
8-
pure2-autodiff.cpp2(282): note: see reference to class template instantiation 'cpp2::taylor<double,2>' being compiled
9-
cpp2taylor.h2(16): note: while compiling class template member function 'cpp2::taylor<double,2>::taylor(const std::initializer_list<R> &)'
10-
with
11-
[
12-
R=double
13-
]
14-
pure2-autodiff.cpp2(298): note: see the first reference to 'cpp2::taylor<double,2>::taylor' in 'ad_test_2::f_d'
15-
cpp2taylor.h2(35): note: while compiling class template member function 'void cpp2::taylor<double,2>::set(const int,const double) &'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
oh look, a thrown int: 42
2+
oh look, another exception
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-try-catch.cpp

0 commit comments

Comments
 (0)