Skip to content

Commit 928186e

Browse files
committed
Add statement (including block) scope in/inout parameters, and apply that syntax to for (remove : and =), closes #386
1 parent 1886ed2 commit 928186e

26 files changed

+241
-97
lines changed

regression-tests/mixed-bounds-safety-with-assert-2.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ main: () -> int = {
33
v: std::vector<int> = (1, 2, 3, 4, 5);
44
add_42_to_subrange(v, 1, 3);
55

6-
for v do :(i) =
6+
for v do (i)
77
std::cout << i << "\n";
88
}
99

@@ -15,7 +15,7 @@ add_42_to_subrange: (inout rng:_, start:int, end:int)
1515
count := 0;
1616
for rng
1717
next count++
18-
do :(inout i) =
18+
do (inout i)
1919
if start <= count <= end {
2020
i += 42;
2121
}

regression-tests/mixed-bounds-safety-with-assert.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ print_subrange: (rng:_, start:int, end:int) = {
1111
count := 0;
1212
for rng
1313
next count++
14-
do: (i:_) =
14+
do (i)
1515
if start <= count && count <= end {
1616
std::cout << i << "\n";
1717
}

regression-tests/mixed-fixed-type-aliases.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ main: (args) -> int = {
1919
z: u16 = 42;
2020
test(z);
2121

22-
for args do :(arg) =
22+
for args do (arg)
2323
std::cout << arg << "\n";
2424
}

regression-tests/mixed-function-expression-and-std-for-each.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ main: () -> int = {
2424
callback
2525
);
2626

27-
for view do :(str:_) = {
27+
for view do (str) {
2828
std::cout << str << "\n";
2929
}
3030
}

regression-tests/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <iostream>
77

88
main: () -> int = {
9-
vec: std::vector<std::string>
9+
vec: std::vector<std::string>
1010
= ("hello", "2022");
1111
view: std::span = vec;
1212

@@ -17,6 +17,6 @@ main: () -> int = {
1717
callback := :(inout x:_) = x += "-ish";
1818
std::ranges::for_each( view, callback );
1919

20-
for view do :(str:_) =
20+
for view do (str)
2121
std::cout << str << "\n";
2222
}

regression-tests/mixed-function-expression-and-std-ranges-for-each.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <iostream>
77

88
main: () -> int = {
9-
vec: std::vector<std::string>
9+
vec: std::vector<std::string>
1010
= ("hello", "2022");
1111
view: std::span = vec;
1212

@@ -16,6 +16,6 @@ main: () -> int = {
1616
callback := :(inout x:_) = x += "-ish";
1717
std::ranges::for_each( view, callback );
1818

19-
for view do :(str:_) =
19+
for view do (str)
2020
std::cout << str << "\n";
2121
}

regression-tests/mixed-function-expression-with-pointer-capture.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <iostream>
77

88
main: () -> int = {
9-
vec: std::vector<std::string>
9+
vec: std::vector<std::string>
1010
= ("hello", "2023");
1111
view: std::span = vec;
1212

@@ -18,6 +18,6 @@ main: () -> int = {
1818
callback := :(inout x:_) = x += "-ish";
1919
std::ranges::for_each( view, callback );
2020

21-
for view do :(str:_) =
21+
for view do (str)
2222
std::cout << str << "\n";
2323
}

regression-tests/mixed-function-expression-with-repeated-capture.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <iostream>
77

88
main: () -> int = {
9-
vec: std::vector<std::string>
9+
vec: std::vector<std::string>
1010
= ("hello", "2022");
1111
view: std::span = vec;
1212

@@ -17,6 +17,6 @@ main: () -> int = {
1717
callback := :(inout x:_) = x += "-ish";
1818
std::ranges::for_each( view, callback );
1919

20-
for view do :(str:_) =
20+
for view do (str)
2121
std::cout << str << "\n";
2222
}

regression-tests/mixed-intro-for-with-counter-include-last.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ main: () -> int
33
= {
44
v: std::vector<int> = (1, 2, 3, 4, 5);
55
counter := 42;
6-
for v next counter *= 2 do: (i:_) = {
6+
for v next counter *= 2 do (i) {
77
std::cout << i << " " << counter << "\n";
88
}
99
}

regression-tests/pure2-break-continue.cpp2

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ for_continue_inner: () =
161161
{
162162
vi: std::vector = ( 0, 1, 2 );
163163
counter := 0;
164-
for vi next counter++ do :(i) = {
164+
for vi next counter++ do (i) {
165165
vj: std::vector = ( 0, 1, 2 );
166-
inner: for vj do :(j) = {
166+
inner: for vj do (j) {
167167
std::cout << i << j << " ";
168168
if j == 1 {
169169
continue inner;
@@ -179,9 +179,9 @@ for_continue_outer: () =
179179
{
180180
vi: std::vector = ( 0, 1, 2 );
181181
counter := 0;
182-
outer: for vi next counter++ do :(i) = {
182+
outer: for vi next counter++ do (i) {
183183
vj: std::vector = ( 0, 1, 2 );
184-
for vj do :(j) = {
184+
for vj do (j) {
185185
std::cout << i << j << " ";
186186
if j == 1 {
187187
continue outer;
@@ -197,9 +197,9 @@ for_break_inner: () =
197197
{
198198
vi: std::vector = ( 0, 1, 2 );
199199
counter := 0;
200-
for vi next counter++ do :(i) = {
200+
for vi next counter++ do (i) {
201201
vj: std::vector = ( 0, 1, 2 );
202-
inner: for vj do :(j) = {
202+
inner: for vj do (j) {
203203
std::cout << i << j << " ";
204204
if j == 1 {
205205
break inner;
@@ -215,9 +215,9 @@ for_break_outer: () =
215215
{
216216
vi: std::vector = ( 0, 1, 2 );
217217
counter := 0;
218-
outer: for vi next counter++ do :(i) = {
218+
outer: for vi next counter++ do (i) {
219219
vj: std::vector = ( 0, 1, 2 );
220-
for vj do :(j) = {
220+
for vj do (j) {
221221
std::cout << i << j << " ";
222222
if j == 1 {
223223
break outer;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
main: () -> int = {
2-
vec: std::vector<std::string>
2+
vec: std::vector<std::string>
33
= ("hello", "2022");
44
view: std::span = vec;
55

6-
for view do :(inout str:_) = {
6+
for view do (inout str) {
77
len := decorate(str);
88
println(str, len);
99
}
1010
}
1111

12-
decorate: (inout thing: _ ) -> int = {
12+
decorate: (inout thing: _ ) -> int = {
1313
thing = "[" + thing + "]";
1414
return thing.ssize();
1515
}
1616

1717
println: (x: _, len: _) =
1818
std::cout
19-
<< ">> " << x
20-
<< " - length "
19+
<< ">> " << x
20+
<< " - length "
2121
<< len << "\n";

regression-tests/pure2-intro-example-three-loops.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ main: () -> int = {
2222
} while i* > 1 next i*--;
2323

2424
std::cout << "\n";
25-
for words do: (inout word:_) =
25+
for words do (inout word)
2626
decorate_and_print(word);
2727

2828
print( : std::string = "end of program" );
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
main: (args) = {
3+
local_int := 42;
4+
5+
// 'in' (read-only) statement scope variable
6+
(i := local_int) for args do (arg) {
7+
std::cout << i << "\n"; // prints 42
8+
}
9+
10+
// 'inout' (read-write) statement scope variable
11+
(inout i := local_int) {
12+
i++;
13+
}
14+
std::cout << local_int << "\n"; // prints 43
15+
}

regression-tests/pure2-type-and-namespace-aliases.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ myfunc: () = {
1919

2020
v2 :== v;
2121

22-
for v2 do :(s) =
22+
for v2 do (s)
2323
std::cout << "(s)$\n";
2424
}
2525

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
42
2+
43

regression-tests/test-results/clang-12/pure2-statement-scope-parameters.cpp.output

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
42
2+
43

regression-tests/test-results/gcc-10/pure2-statement-scope-parameters.cpp.output

Whitespace-only changes.

regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> vo
4343
auto count {0};
4444
for ( auto&& cpp2_range = rng;
4545

46-
auto& i : cpp2_range ) { do
46+
auto& i : cpp2_range ) { do
4747
if ([_0 = start, _1 = count, _2 = end]{ return cpp2::cmp_less_eq(_0,_1) && cpp2::cmp_less_eq(_1,_2); }()) {
4848
i += 42;
4949
} while (false); ++count; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
42
2+
43
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-statement-scope-parameters.cpp
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
14+
#line 2 "pure2-statement-scope-parameters.cpp2"
15+
auto main(int const argc_, char const* const* const argv_) -> int;
16+
17+
18+
//=== Cpp2 function definitions =================================================
19+
20+
21+
#line 2 "pure2-statement-scope-parameters.cpp2"
22+
auto main(int const argc_, char const* const* const argv_) -> int{
23+
auto args = cpp2::make_args(argc_, argv_);
24+
#line 3 "pure2-statement-scope-parameters.cpp2"
25+
auto local_int {42};
26+
{
27+
auto const& i = local_int;
28+
29+
// 'in' (read-only) statement scope variable
30+
#line 6 "pure2-statement-scope-parameters.cpp2"
31+
for ( auto const& cpp2_range = args; auto const& arg : cpp2_range ) {
32+
std::cout << i << "\n"; // prints 42
33+
}
34+
}
35+
{
36+
auto& i = local_int;
37+
38+
// 'inout' (read-write) statement scope variable
39+
#line 11 "pure2-statement-scope-parameters.cpp2"
40+
{
41+
++i;
42+
}
43+
}
44+
#line 14 "pure2-statement-scope-parameters.cpp2"
45+
std::cout << std::move(local_int) << "\n";// prints 43
46+
}
47+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-statement-scope-parameters.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)