Skip to content

Commit 61dc376

Browse files
committed
Add reflection for binary expressions, and first working autodiff example
The test @autodiff function can now handle the "r = a + b;" case. This code: test: @autodiff @print type = { // A simple first function would be: // func: (a: double, b: double) -> double = { return a + b; } func: (a: double, b: double) -> (r: double) = { r = a + b; } // The generated autodiff forward transformation would be: // func_diff: (a: double, a_d: double, b: double, b_d: double) -> (r: double, r_d: double) = { // r_d = a * b_d + b * a_d; // r = a * b; // } } Generates the following, as reported by @print in the cppfront compile output: test:/* @autodiff @print */ type = { func:( in a: double, in b: double, ) -> (out r: double, ) = { r = a + b; return; } func_diff:( in a: double, in a_d: double, in b: double, in b_d: double, ) -> ( out r: double = 1, out r_d: double = 1, ) = { r_d = a * b_d + b * a_d; r = a * b; return; } } And a bug fix: When a file that contains Cpp2 code begins with Cpp1 #includes, preserve their position at the top of the generated file too. This allows a Cpp2 type's template parameter list to use types a from Cpp1 header file.
1 parent 8a99d2e commit 61dc376

34 files changed

+1570
-677
lines changed

build_h2.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@echo off
22
cd source
3-
cppfront reflect.h2 -verb
3+
cppfront reflect.h2 -verb %1
44
cd ..\include
5-
cppfront cpp2regex.h2 -verb
5+
cppfront cpp2regex.h2 -verb %1
66
cd..

regression-tests/test-results/mixed-bounds-check.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#line 2 "mixed-bounds-check.cpp2"
2+
#include <vector>
13

24

35
//=== Cpp2 type declarations ====================================================
@@ -12,8 +14,6 @@
1214

1315
#line 1 "mixed-bounds-check.cpp2"
1416

15-
#include <vector>
16-
1717
#line 4 "mixed-bounds-check.cpp2"
1818
[[nodiscard]] auto main() -> int;
1919

regression-tests/test-results/mixed-bugfix-for-literal-as-nttp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <chrono>
12

23

34
//=== Cpp2 type declarations ====================================================
@@ -11,7 +12,8 @@
1112
//=== Cpp2 type definitions and function declarations ===========================
1213

1314
#line 1 "mixed-bugfix-for-literal-as-nttp.cpp2"
14-
#include <chrono>
15+
16+
#line 2 "mixed-bugfix-for-literal-as-nttp.cpp2"
1517
auto main() -> int;
1618

1719
//=== Cpp2 function definitions =================================================

regression-tests/test-results/mixed-captures-in-expressions-and-postconditions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#line 2 "mixed-captures-in-expressions-and-postconditions.cpp2"
2+
#include <algorithm>
3+
#include <vector>
14

25

36
//=== Cpp2 type declarations ====================================================
@@ -12,9 +15,6 @@
1215

1316
#line 1 "mixed-captures-in-expressions-and-postconditions.cpp2"
1417

15-
#include <algorithm>
16-
#include <vector>
17-
1818
#line 5 "mixed-captures-in-expressions-and-postconditions.cpp2"
1919
[[nodiscard]] auto main() -> int;
2020

regression-tests/test-results/mixed-fixed-type-aliases.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <filesystem>
2+
#include <iostream>
3+
#include <typeinfo>
14

25

36
//=== Cpp2 type declarations ====================================================
@@ -14,9 +17,7 @@ template<typename T> class mytype;
1417
//=== Cpp2 type definitions and function declarations ===========================
1518

1619
#line 1 "mixed-fixed-type-aliases.cpp2"
17-
#include <filesystem>
18-
#include <iostream>
19-
#include <typeinfo>
20+
#line 4 "mixed-fixed-type-aliases.cpp2"
2021

2122
namespace my {
2223
using u16 = float;

regression-tests/test-results/mixed-forwarding.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <iostream>
2+
#include <utility>
13

24

35
//=== Cpp2 type declarations ====================================================
@@ -11,8 +13,7 @@
1113
//=== Cpp2 type definitions and function declarations ===========================
1214

1315
#line 1 "mixed-forwarding.cpp2"
14-
#include <iostream>
15-
#include <utility>
16+
#line 3 "mixed-forwarding.cpp2"
1617

1718
struct X {
1819
int i;

regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#include <vector>
2+
#include <string>
3+
#include <span>
4+
#include <algorithm>
5+
#include <iostream>
16

27

38
//=== Cpp2 type declarations ====================================================
@@ -11,11 +16,6 @@
1116
//=== Cpp2 type definitions and function declarations ===========================
1217

1318
#line 1 "mixed-function-expression-and-std-for-each.cpp2"
14-
#include <vector>
15-
#include <string>
16-
#include <span>
17-
#include <algorithm>
18-
#include <iostream>
1919

2020
#line 7 "mixed-function-expression-and-std-for-each.cpp2"
2121
[[nodiscard]] auto main() -> int;

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#include <vector>
2+
#include <ranges>
3+
#include <string>
4+
#include <span>
5+
#include <algorithm>
6+
#include <iostream>
17

28

39
//=== Cpp2 type declarations ====================================================
@@ -11,12 +17,6 @@
1117
//=== Cpp2 type definitions and function declarations ===========================
1218

1319
#line 1 "mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2"
14-
#include <vector>
15-
#include <ranges>
16-
#include <string>
17-
#include <span>
18-
#include <algorithm>
19-
#include <iostream>
2020

2121
#line 8 "mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2"
2222
[[nodiscard]] auto main() -> int;

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#include <vector>
2+
#include <ranges>
3+
#include <string>
4+
#include <span>
5+
#include <algorithm>
6+
#include <iostream>
17

28

39
//=== Cpp2 type declarations ====================================================
@@ -11,12 +17,6 @@
1117
//=== Cpp2 type definitions and function declarations ===========================
1218

1319
#line 1 "mixed-function-expression-and-std-ranges-for-each.cpp2"
14-
#include <vector>
15-
#include <ranges>
16-
#include <string>
17-
#include <span>
18-
#include <algorithm>
19-
#include <iostream>
2020

2121
#line 8 "mixed-function-expression-and-std-ranges-for-each.cpp2"
2222
[[nodiscard]] auto main() -> int;

regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#include <vector>
2+
#include <ranges>
3+
#include <string>
4+
#include <span>
5+
#include <algorithm>
6+
#include <iostream>
17

28

39
//=== Cpp2 type declarations ====================================================
@@ -11,12 +17,6 @@
1117
//=== Cpp2 type definitions and function declarations ===========================
1218

1319
#line 1 "mixed-function-expression-with-pointer-capture.cpp2"
14-
#include <vector>
15-
#include <ranges>
16-
#include <string>
17-
#include <span>
18-
#include <algorithm>
19-
#include <iostream>
2020

2121
#line 8 "mixed-function-expression-with-pointer-capture.cpp2"
2222
[[nodiscard]] auto main() -> int;

0 commit comments

Comments
 (0)