-
Notifications
You must be signed in to change notification settings - Fork 695
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
merging loop constructs #261
Comments
Afterthought: even in the current situations, it might be useful to allow 'continue' to jump to the beginning of a 'block'. Right now, a goto statement to an earlier line would have to be translated to something like:
|
The reason for having
(There is also a question of whether |
Thinking about this a little more, if we want minimalism, one could argue that this hypothetical
look like
and
look like
Note that without a
It's worth considering. |
The problem with just having a loop construct is it would make encoding a loop with continue in the body much more difficult. Continue would skip past the condition check and go back to the start of the loop. |
Yeah, we'd have to do an extra nesting level:
(Note that I'm not presently proposing we do this. I'm just exploring the idea.) It's a little visually cluttered, but the translation to branching is in relief. |
Taking this idea further, we could do the same thing for forward branches. I think this path tends toward the following control opcode set:
Then this:
might look like this:
and this:
might look like this:
Unconditional break/continue could just be conditional break/continue with a constant, but since unconditional branches is typically a different instruction, it's nice to make the distinction. Also, Advantages of this approach over the status quo include:
Disadvantages include:
|
As I mentioned at the meeting, FWIW this has some resemblance to the 4 core operations ANS Forth defines its other structured control flow in terms of: |
With #427 merged, there is now only one |
AstSemantics.md mentions that WebAssembly has two loop constructs: 'do_while', and 'forever'.
I note that there is no equivalent to 'while() { ... }'.
This may be because it is functionally equivalent [1] to
Considering that there is also a 'break' expression, both can however also be expressed with just 'forever':
I would suggest either removing 'do_while', or adding a 'while', depending on whether the design criteria favour either frugality and elegance, or ease of reading of the textual form. In the latter case, maybe there should be a construct for 'for' loops as well.
Either way, having 'do_while', but not 'while' seems inconsistent.
In case 'do_while' is eliminated, I would also suggest renaming 'forever' to just 'loop'; it is shorter, and in the presence of 'break', 'forever' is not really forever.
One could go even further, and combine 'block' and 'forever'.
Either by substituting 'block' by:
Or, by substituting 'forever' by:
The latter gets rid of another symmetry: right now 'break' can be used to jump to the end of a loop or block, while 'continue' can only be used to jump to the beginning of a loop (and not a block).
[1] Note however that the guard expression is duplicated here.
The text was updated successfully, but these errors were encountered: