From de165d8928997a76c0207bb1bed84b2a93c04fab Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Tue, 30 Apr 2019 11:05:08 +0100 Subject: [PATCH 1/2] Initial proposal for language feature `Continue For id` and `Exit For id`. --- ...-Continue-Exit-For-With-Loop-Identifier.md | 259 ++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md diff --git a/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md b/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md new file mode 100644 index 0000000..b100ce5 --- /dev/null +++ b/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md @@ -0,0 +1,259 @@ +# Continue Exit For Identifier + +* [x] [Proposed](https://github.com/dotnet/vblang/issues/186) +* [*] Prototype: [Prototype/ContinueForIdentifier](https://github.com/AdamSpeight2008/roslyn-AdamSpeight2008/tree/Prototpye/ContinueForID) +* [ ] Implementation: [Not Started](https://github.com/dotnet/roslyn/BRANCH_NAME) +* [ ] Specification: [Not Started](pr/1) + +## Summary +[summary]: #summary + +Extend the capibility of `Contine For` and `Exit For` to enable continuing or exitting a loop nested higher. +Eg. `Continue For x` and `Continue For x` + +------- + +## Motivation +[motivation]: #motivation + +* Why are we doing this? + * Seems like natural extension to the language, give the `Next` can also have an identifer. eg `Next x` +* What use cases does it support? + * It can difficult to implement, resorting to using `label` and `GoTo` eg `GoTo start_of_loop_x` +* What is the expected outcome? + * Without an identifier eg `Continue For` or `Exit For` will perform exactly as it does now. + * With an identifier + * `Continue For x` will continue with the next iterator of the loop named with the identifier `x`. + * `Exit For x` will exit the loop named with the identifier `x`. Resuming execution at the next statement after it. + +------ + +## Detailed design +[design]: #detailed-design + +Add an optional child node to `ExitStatementSyntax` +```xml + + + An exit statement. The kind of block being exited can be found by examining the Kind. + + + 10.11 + ExitStatement + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The "Exit" keyword. + + + + + The keyword describing the block to exit. + + + + + + + + + + + + + + +``` + +Add an optional child node to `ContinueStatementSyntax` +```xml + + + Represents a "Continue (block)" statement. THe kind of block referenced can be determined by examining the Kind. + + + 10.11 + ContinueStatement + + + + + + + + + + + + + + + The "Continue" keyword. + + + + + The "Do", "For" or "While" keyword that identifies the kind of loop being continued. + + + + + + + + +``` + +Parse the optional identifier only if the statements are of the correct kind. +`ContinueForStatement` and `ExitForStatement` + +Validate that the identifer is a loop identifer, within this method. +And only if the identifier is a nesting higher or equal to the current nesting level. IE You can continue or exit an loop that is lower eg an inner loop. + +This is the bulk of the proposal. Explain the design in enough detail for somebody familiar +with the language to understand, and for somebody familiar with the compiler to implement, and include examples of how the feature is used. This section can start out light before the prototyping phase but should get into specifics and corner-cases as the feature is iteratively designed and implemented. + +Example code: + +```vbnet +Imports System +Module M1 + Sub Main() + dim continueLoop as Boolean = true + For i = 0 To 2 + Console.WriteLine($"Loop i Block Start ({i})") + For j = 0 To 2 + Console.WriteLine($"Loop j Block Start ({i})") + If continueLoop Then + Console.WriteLine("Continuing") + continueLoop = false + Continue For i + End If + Console.WriteLine("Exiting") + Exit For i + Console.WriteLine($"Loop j Block End ({i})") + Next j + Console.WriteLine("After Loop j") + Console.WriteLine($"Loop i Block End ({i})") + Next + Console.WriteLine("After Loop i") + End Sub +End Module +``` +Output +``` +Loop i Block Start (0) +Loop j Block Start (0) +Continuing +Loop i Block Start (1) +Loop j Block Start (0) +Exiting +After Loop i +``` + +```vbnet +Imports System +Module M1 + Sub Main() + dim continueLoop as Boolean = true + Dim xs = {0, 1, 2} + Dim ys = {0, 1, 2} + For Each x In xs + Console.WriteLine($"Loop x Block Start ({x})") + For Each y In ys + Console.WriteLine($"Loop y Block Start ({x})") + If continueLoop Then + Console.WriteLine("Continuing") + continueLoop = false + Continue For i + End If + Console.WriteLine("Exiting") + Exit For i + Console.WriteLine($"Loop y Block End ({y})") + Next j + Console.WriteLine("After Loop y") + Console.WriteLine($"Loop x Block End ({x})") + Next + Console.WriteLine("After Loop x") + End Sub +End Module +``` +Output +``` +Loop x Block Start (0) +Loop y Block Start (0) +Continuing +Loop x Block Start (1) +Loop y Block Start (0) +Exiting +After Loop x +``` + +This is implement in this proposal by modifying how the lowering is done, especially the labels use. +The labels used will now include the loop's identifier, which allows it to jump to correct location in the lowered code. + +----- + +## Drawbacks +[drawbacks]: #drawbacks + +Why should we *not* do this? + +------ + +## Alternatives +[alternatives]: #alternatives + +What other designs have been considered? What is the impact of not doing this? + +---- + +## Unresolved questions +[unresolved]: #unresolved-questions +What parts of the design are still TBD? + +* **TIED TO VB15** need to be updated, if accepted. +* Checking of the validity of the identifer used in the `Continue` and `Exit` +* Additional error messages +* Additional use cases. *Not covered in this proposal, has it require provide a name for the loop constuct.* + * `Do ... Loop` + * 'While .. End While` + From cfb9dae7b8e12eea012e6de976d5d684792d305e Mon Sep 17 00:00:00 2001 From: Adam Speight Date: Mon, 10 Jun 2019 06:28:19 +0100 Subject: [PATCH 2/2] Update proposal-Continue-Exit-For-With-Loop-Identifier.md --- ...-Continue-Exit-For-With-Loop-Identifier.md | 90 ++++++++----------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md b/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md index b100ce5..e75bb89 100644 --- a/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md +++ b/proposals/proposal-Continue-Exit-For-With-Loop-Identifier.md @@ -1,7 +1,7 @@ # Continue Exit For Identifier * [x] [Proposed](https://github.com/dotnet/vblang/issues/186) -* [*] Prototype: [Prototype/ContinueForIdentifier](https://github.com/AdamSpeight2008/roslyn-AdamSpeight2008/tree/Prototpye/ContinueForID) +* [x] Prototype: [Prototype/ContinueForIdentifier](https://github.com/AdamSpeight2008/roslyn-AdamSpeight2008/tree/Prototpye/ContinueForID) * [ ] Implementation: [Not Started](https://github.com/dotnet/roslyn/BRANCH_NAME) * [ ] Specification: [Not Started](pr/1) @@ -9,7 +9,7 @@ [summary]: #summary Extend the capibility of `Contine For` and `Exit For` to enable continuing or exitting a loop nested higher. -Eg. `Continue For x` and `Continue For x` +Eg. `Continue For x` and `Exit For x` ------- @@ -17,8 +17,13 @@ Eg. `Continue For x` and `Continue For x` [motivation]: #motivation * Why are we doing this? - * Seems like natural extension to the language, give the `Next` can also have an identifer. eg `Next x` + * Seems like natural extension to the language, given that the `Next` can also have an identifer. eg `Next x` + * It can be difficult to implement workarounds, or require used different loop constructs at different nestings. + * Often implemented via `label`s and `GoTo`s. eg `GoTo start_of_loop_x`. * What use cases does it support? + * This proposal is tightly focused on two cases; + * `Continue For loop_identifier` + * `Exit For loop_identifier` * It can difficult to implement, resorting to using `label` and `GoTo` eg `GoTo start_of_loop_x` * What is the expected outcome? * Without an identifier eg `Continue For` or `Exit For` will perform exactly as it does now. @@ -31,7 +36,15 @@ Eg. `Continue For x` and `Continue For x` ## Detailed design [design]: #detailed-design -Add an optional child node to `ExitStatementSyntax` +### Syntax Node Changes + An additional optional child node `` will added to + * `ExitStatementSyntax` + * `ContinueStatementSyntax` + This additional node is used to identify which loop identifier to the statement refers to. + If the identifier is absent, the statement refers to current loop. + +
ExitStatementSyntax change

+ ```xml