diff options
Diffstat (limited to 'docs/tutorial/advanced_control_flow.md')
| -rw-r--r-- | docs/tutorial/advanced_control_flow.md | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/docs/tutorial/advanced_control_flow.md b/docs/tutorial/advanced_control_flow.md index ee6c13aa7..76d673b35 100644 --- a/docs/tutorial/advanced_control_flow.md +++ b/docs/tutorial/advanced_control_flow.md @@ -220,8 +220,7 @@ which is long, messy, and contains some rather nasty-looking control flow, inclu } ``` -If you read the OoT tutorial, you'll know these nested negated ifs all using the same variable are a good indicator that there's a switch. The problem is working out how to write it. - +If you read the OoT tutorial, you'll know these nested negated ifs all using the same variable are a good indicator that there's a switch. The problem is working out how to write it. ## Goto-only mode @@ -303,6 +302,7 @@ block_17: ``` which in many ways looks worse: you can see why the use of gotos in code is strongly discouraged. However, if you throw this in `diff.py`, you'll find it's rather closer than you'd have thought. Goto-only mode has the advantages that + - code is always in the right order: mips2c has not had to reorder anything to get the ifs to work out - it is often possible to get quite close with gotos, then start removing them, checking the matching status at each point. This is usually easier than trying to puzzle out the way it's trying to jump out of an `if ( || )` or similar. - if you're trying to keep track of where you are in the code, the gotos mean that it is closer to the assembly in the first place. @@ -404,7 +404,6 @@ block_17: We can't apply this rule any more, so we need to move on to the next: `block_17` just contains a `return`. So we can replace it by `return` everywhere it appears. - ```C void func_809527F8(EnMs* this, PlayState* play) { u8 temp_v0; @@ -486,6 +485,7 @@ Now let's start thinking about switches. A good indicator of a switch in goto-on ``` because + - there are multiple ifs that are simple numeric comparisons of the same argument - the goto blocks are in the same order as the ifs - there is one last goto at the end that triggers if none of the ifs does: this sounds an awful lot like a `default`! @@ -523,7 +523,8 @@ So let us rewrite the entire second half as a switch: } ``` -There's a couple of other obvious things here: +There's a couple of other obvious things here: + - the last `return` in `case 0` is unnecessary since there is no other code after the switch, so breaking is equivalent to the return` - a common pattern everywhere, a sequence of ifs with returns as the last thing inside is the same as an if-else chain, so we can rewrite these as @@ -622,6 +623,7 @@ block_7: ``` Now, the top of the function also looks like a switch: + ```C temp_v0 = Message_GetState(&play->msgCtx); if (temp_v0 == 4) { @@ -742,6 +744,6 @@ void func_809527F8(EnMs* this, PlayState* play) { } ``` -And this matches! +And this matches! We will not document this now, although even with so few function named it seems pretty clear that it's to do with buying beans (and indeed, Magic Beans cost 10 Rupees and have Get Item ID `0x35`) You might like to try to match this function without using goto-only mode, to compare. It is also an interesting exercise to see what each elimination does to the diff: sometimes it will stray surprisingly far for a small change. |
