BreakContinueTransformer( wrapper_root )
Rewrite ‘break’ and ‘continue’ key words in a if-else python way to make it equivalent to original control flow
The main idea of this class is:
Map the ‘break/continue’ stmt with an unique boolean variable V.
2. Find the first ancestor block containing this ‘break/continue’, a block can be a node containing stmt list. We should remove all stmts after the ‘break/continue’ and set the V to True here.
3. Add ‘if V’ for stmts in ancestor blocks between the first one (exclusive) and the ancestor loop (inclusive)
4. For ‘break’ add break into condition of the loop. For ‘continue’, set continue to False at the beginning of each loop
TODO: more details should be summarized as design document
- Note: The class is inherited from BaseNodeVisitor instead of NodeTransformer,
because ancestor nodes will be modified inplace for Break/Continue here. In general, we recommend to inheriting NodeTransformer to modify node!