-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add statement (including block) scope in/inout parameters, and apply …
…that syntax to `for` (remove `:` and `=`), closes #386
- Loading branch information
Showing
26 changed files
with
241 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ main: () -> int = { | |
| callback | ||
| ); | ||
|
|
||
| for view do :(str:_) = { | ||
| for view do (str) { | ||
| std::cout << str << "\n"; | ||
| } | ||
| } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,21 @@ | ||
| main: () -> int = { | ||
| vec: std::vector<std::string> | ||
| vec: std::vector<std::string> | ||
| = ("hello", "2022"); | ||
| view: std::span = vec; | ||
|
|
||
| for view do :(inout str:_) = { | ||
| for view do (inout str) { | ||
| len := decorate(str); | ||
| println(str, len); | ||
| } | ||
| } | ||
|
|
||
| decorate: (inout thing: _ ) -> int = { | ||
| decorate: (inout thing: _ ) -> int = { | ||
| thing = "[" + thing + "]"; | ||
| return thing.ssize(); | ||
| } | ||
|
|
||
| println: (x: _, len: _) = | ||
| std::cout | ||
| << ">> " << x | ||
| << " - length " | ||
| << ">> " << x | ||
| << " - length " | ||
| << len << "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| main: (args) = { | ||
| local_int := 42; | ||
|
|
||
| // 'in' (read-only) statement scope variable | ||
| (i := local_int) for args do (arg) { | ||
| std::cout << i << "\n"; // prints 42 | ||
| } | ||
|
|
||
| // 'inout' (read-write) statement scope variable | ||
| (inout i := local_int) { | ||
| i++; | ||
| } | ||
| std::cout << local_int << "\n"; // prints 43 | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ myfunc: () = { | |
|
|
||
| v2 :== v; | ||
|
|
||
| for v2 do :(s) = | ||
| for v2 do (s) | ||
| std::cout << "(s)$\n"; | ||
| } | ||
|
|
||
|
|
||
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/clang-12/pure2-statement-scope-parameters.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 42 | ||
| 43 |
Empty file.
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/gcc-10/pure2-statement-scope-parameters.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 42 | ||
| 43 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/msvc-2022/pure2-statement-scope-parameters.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 42 | ||
| 43 |
1 change: 1 addition & 0 deletions
1
regression-tests/test-results/msvc-2022/pure2-statement-scope-parameters.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pure2-statement-scope-parameters.cpp |
47 changes: 47 additions & 0 deletions
47
regression-tests/test-results/pure2-statement-scope-parameters.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
|
|
||
| #define CPP2_USE_MODULES Yes | ||
|
|
||
| //=== Cpp2 type declarations ==================================================== | ||
|
|
||
|
|
||
| #include "cpp2util.h" | ||
|
|
||
|
|
||
|
|
||
| //=== Cpp2 type definitions and function declarations =========================== | ||
|
|
||
|
|
||
| #line 2 "pure2-statement-scope-parameters.cpp2" | ||
| auto main(int const argc_, char const* const* const argv_) -> int; | ||
|
|
||
|
|
||
| //=== Cpp2 function definitions ================================================= | ||
|
|
||
|
|
||
| #line 2 "pure2-statement-scope-parameters.cpp2" | ||
| auto main(int const argc_, char const* const* const argv_) -> int{ | ||
| auto args = cpp2::make_args(argc_, argv_); | ||
| #line 3 "pure2-statement-scope-parameters.cpp2" | ||
| auto local_int {42}; | ||
| { | ||
| auto const& i = local_int; | ||
|
|
||
| // 'in' (read-only) statement scope variable | ||
| #line 6 "pure2-statement-scope-parameters.cpp2" | ||
| for ( auto const& cpp2_range = args; auto const& arg : cpp2_range ) { | ||
| std::cout << i << "\n"; // prints 42 | ||
| } | ||
| } | ||
| { | ||
| auto& i = local_int; | ||
|
|
||
| // 'inout' (read-write) statement scope variable | ||
| #line 11 "pure2-statement-scope-parameters.cpp2" | ||
| { | ||
| ++i; | ||
| } | ||
| } | ||
| #line 14 "pure2-statement-scope-parameters.cpp2" | ||
| std::cout << std::move(local_int) << "\n";// prints 43 | ||
| } | ||
|
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-statement-scope-parameters.cpp2.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pure2-statement-scope-parameters.cpp2... ok (all Cpp2, passes safety checks) | ||
|
|
Oops, something went wrong.
928186eThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is an empty statement parameter list intentional?
I hope to take advantage of it in a metafunction.
See https://cpp2.godbolt.org/z/EqPsT68dz:
928186eThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JohelEGP Sorry for the delay -- I just usually allow empty paren-lists in general, and I don't think I thought about that case either way here in particular because I just didn't think about examples like that. If you have a use for it I'm happy to let it stand and be cited as a curiosity in 'obfuscated code' contests 😁
928186eThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's some more detail on the use case.
I was generating statement parameters in the for loop of
@array.I noticed that, sometimes, the parameter list was empty.
So I started to wonder whether it was something I could rely on.
I have since refactored
@arrayto not have that parameter list.928186eThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll just leave it alone and let it stand for now. I don't see a reason to remove it... I don't think there's a surprising parse the way things are parsed right now (if there is one then we should reconsider this of course).