[This page is auto-generated; please prefer to read the article’s PDF Form.]
At the beginning of each iteration, we will read the current frame’s res_pt and then will need to execute the logic according to that. For this, we can organize the complete logic into multiple code-blocks, each for one resume-point. As said earlier, for a fresh call (not resuming) also, we can use a special resume-point of 0 and have its own code-block. Based on the res_pt value read, we will jump to that particular code-block and execute it. For convenience, a switch-case can be used, with res_pt as the switch value, and each code-block within its own case block for that resume-point.
As the method’s logic will now get distributed across code-blocks, there can be a code-block B which is not only linked to a resume-point, but is also to be executed after another code-block A in continuity. To handle this, code-block A will simply modify the res_pt of the current frame to point it to B and, without a push/pop on the stack, continue to the new iteration. B will get executed in this new iteration. This technique will be used in the example below, where a recursive call is made only under an if block, but its resuming code-block is needed otherwise also.
Copyright © 2020 Nitin Verma. All rights reserved.