assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

break: Add support for labeled statements, breaks, and continues

Open CountBleck opened this issue 1 year ago • 5 comments

Fixes #2889.

Changes proposed in this pull request: ⯈ Support labeled statements in the parser/AST ⯈ Support labeled break/continue.

This change is breaking solely because it modifies the Node.createXXX APIs, which are used by transforms.

  • [x] I've read the contributing guidelines
  • [x] I've added my name and email to the NOTICE file

CountBleck avatar Dec 16 '24 06:12 CountBleck

I just need to add some tests; then, this will be ready to merge.

@JairusSW Heads up: this change definitely breaks transforms. Do you have anything to comment on this PR?

CountBleck avatar Dec 16 '24 23:12 CountBleck

It looks like I forgot about if...

CountBleck avatar Dec 17 '24 00:12 CountBleck

Okay, it appears that my code breaks something related to Flow flags, so more work on this PR is needed.

One issue is that breaking out of a labeled block sets the Breaks flag on the outer flow and messes with the function not appearing to always terminate.

Another issue is that a continue (and likely break as well) from inside an inner loop to an outer loop won't set the Continues/ConditionallyContinues flow flag on the outer loop body's flow. This is a much more significant issue (since nested loops are the most common use of labels) and seems to be more difficult to fix...

CountBleck avatar Dec 17 '24 05:12 CountBleck

In TS, there are new ASTNode LabeledStatement to identifier this labeled statement, could we use the same concept?

HerrCai0907 avatar Dec 18 '24 15:12 HerrCai0907

@HerrCai0907 I considered that before, and it might be a pretty good idea. It would be better for transform users for sure.

Still, there's the issue of setting the proper FlowFlags for a continue to an outer loop...maybe the class I use to keep track of labels should contain the flow, and I can use something like do flow.set(...) while ((flow = flow.parent) != targetFlow). (Of course, the actual code would look nicer than that.)

CountBleck avatar Dec 18 '24 18:12 CountBleck