Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] for loop don't allow several assignments in next #448

Closed
realgdman opened this issue May 10, 2023 · 4 comments
Closed

[BUG] for loop don't allow several assignments in next #448

realgdman opened this issue May 10, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@realgdman
Copy link

for loop (and other loops) don't accept several assignments inside next

Reproduce Code
https://cpp2.godbolt.org/z/aqYqr63z5

main: () = {
	(copy a := 0, copy b := 0)
	//for "abc" next a++ do (i) { //ok
	for "abc" next { a++; b++; } do (i) { //error
	//for "abc' next a do (i) { //unused a
		std::cout << i << a << b << '\n';
	}
}

Version
latest (38aec57)

Command lines
cppfront/cppfront $1.cpp2 -p
clang++-15 -Icppfront/include $1.cpp -std=c++20 -o $1

Expected result
Some block syntax for that assignment or diagnostic

Actual result
Compilation error

Additional notes
In grammar only single expr allowed
//G next-clause:
//G 'next' assignment-expression

Additionally, logical-or-expression could be logical.
Which can lead to compiled, but unused logical expression
for "abc' next a do (i) { //unused a

@realgdman realgdman added the bug Something isn't working label May 10, 2023
@realgdman
Copy link
Author

realgdman commented May 11, 2023

Temporary workaround was found

main: () = {
	(copy a := 0, copy b := 0, next :=:()={ a&$*++; b&$*++; })
	for "abc" next next() do (c) {
		std::cout << c << a << b << '\n';
	}	
}
First attempt ``` main: () = { a:=0; b:=0; l := :() = { a&$*++; b&$*++; }; for "abc" next l() do (c) { std::cout << c << a << b << '\n'; } } ```
Thanks for idea from here, https://github.com//issues/432 currently lambda cannot be declared inside `next`

@msadeqhe
Copy link

The example should be written like this:

main: () = {
	(copy a: = 0, copy b: = 0)
	for "abc" next (a++, b++) do (i) {
		std::cout << i << a << b << '\n';
	}
}

Cpp2 generates this:

auto main() -> int{
{
auto a = 0;
auto b = 0;

 { auto const& cpp2_range = "abc"; for ( auto const& i : cpp2_range )  { do {
  std::cout << i << a << b << '\n';
 } while (false); (++a, ++b); }}
}
}

@realgdman
Copy link
Author

Well I should have guessed. Even this works.
for "abc" next ( a++, b++, std::cout << a, foo()) do (c)

@JohelEGP
Copy link
Contributor

See #449 (comment). It may work by accident.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants