Prefix and suffix edge cases for flow statements.

Call suffix code on break/continue before executing the break/continue.
Call prefix code for enclosing loop before executing continue.
This commit is contained in:
Neil Fraser
2019-05-13 15:25:52 -07:00
committed by Neil Fraser
parent 0259f8bb48
commit 25adb40e66
16 changed files with 131 additions and 42 deletions

View File

@@ -197,11 +197,27 @@ Blockly.Python['controls_forEach'] = function(block) {
Blockly.Python['controls_flow_statements'] = function(block) {
// Flow statements: continue, break.
switch (block.getFieldValue('FLOW')) {
var flowType = block.getFieldValue('FLOW');
var xfix = '';
if (Blockly.Python.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the break/continue is triggered.
xfix += Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block);
}
if (Blockly.Python.STATEMENT_PREFIX && flowType == 'CONTINUE') {
var loop = Blockly.Constants.Loops
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop) {
// Inject loop's statement prefix here since the regular one at the end
// of the loop will not get executed if the continue is triggered.
xfix += Blockly.Python.injectId(Blockly.Python.STATEMENT_PREFIX, loop);
}
}
switch (flowType) {
case 'BREAK':
return 'break\n';
return xfix + 'break\n';
case 'CONTINUE':
return 'continue\n';
return xfix + 'continue\n';
}
throw Error('Unknown flow statement.');
};