Skip to content

Language: add labelled break and continue statements#484

Open
chqrlie wants to merge 1 commit intoc2lang:masterfrom
chqrlie:break-label
Open

Language: add labelled break and continue statements#484
chqrlie wants to merge 1 commit intoc2lang:masterfrom
chqrlie:break-label

Conversation

@chqrlie
Copy link
Copy Markdown
Contributor

@chqrlie chqrlie commented Apr 1, 2026

  • break and continue can take the label of an enclosing statement to break or continue directly, bypassing the nested code flow.
  • simplified flow analysis
  • add tests

@chqrlie chqrlie force-pushed the break-label branch 2 times, most recently from 2020018 to fa40579 Compare April 1, 2026 12:11
@bvdberg
Copy link
Copy Markdown
Member

bvdberg commented Apr 1, 2026

Why would you use 'continue X' instead of 'goto X' (which is also what we generate)? When I read the title of the PR, I expected something like:

for(..) {
  while (..) {
     switch (..) {
       case 1:
        break while;
      case 2:
        break for;
      case 3:
        break;
     }
  }
}

So that we insert labels etc.

@chqrlie
Copy link
Copy Markdown
Contributor Author

chqrlie commented Apr 2, 2026 via email

@chqrlie
Copy link
Copy Markdown
Contributor Author

chqrlie commented Apr 2, 2026

continue X expands to a goto but the label is not the same : the target label is inserted just before the } at the end of the block whereas the label X points to the for statement itself.
Your suggestion to accept for, while and switch in break statements is interesting. it is less general and has no prior art but would be especially useful for a switch statement inside a for loop. Supporting that requires some extra work as we cannot use the existing label list.

@chqrlie chqrlie force-pushed the break-label branch 5 times, most recently from cbfca24 to d1f8486 Compare April 9, 2026 07:28
* `break` and `continue` can take the label of an enclosing statement
  to break or continue directly, bypassing the nested code flow.
* this feature is popular in many derived languages and will be added
  in C in the next version of the C Standard.
* simplified flow analysis
* add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants