Add extra suffix when generating if/return block.

Also fix loops in Lua.
This commit is contained in:
Neil Fraser
2019-05-13 14:26:38 -07:00
committed by Neil Fraser
parent ba18ae2159
commit 0259f8bb48
11 changed files with 117 additions and 79 deletions

View File

@@ -45,8 +45,7 @@ Blockly.Python['controls_repeat_ext'] = function(block) {
repeats = 'int(' + repeats + ')';
}
var branch = Blockly.Python.statementToCode(block, 'DO');
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
Blockly.Python.PASS;
branch = Blockly.Python.addLoopTrap(branch, block) || Blockly.Python.PASS;
var loopVar = Blockly.Python.variableDB_.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var code = 'for ' + loopVar + ' in range(' + repeats + '):\n' + branch;
@@ -62,8 +61,7 @@ Blockly.Python['controls_whileUntil'] = function(block) {
until ? Blockly.Python.ORDER_LOGICAL_NOT :
Blockly.Python.ORDER_NONE) || 'False';
var branch = Blockly.Python.statementToCode(block, 'DO');
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
Blockly.Python.PASS;
branch = Blockly.Python.addLoopTrap(branch, block) || Blockly.Python.PASS;
if (until) {
argument0 = 'not ' + argument0;
}
@@ -81,8 +79,7 @@ Blockly.Python['controls_for'] = function(block) {
var increment = Blockly.Python.valueToCode(block, 'BY',
Blockly.Python.ORDER_NONE) || '1';
var branch = Blockly.Python.statementToCode(block, 'DO');
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
Blockly.Python.PASS;
branch = Blockly.Python.addLoopTrap(branch, block) || Blockly.Python.PASS;
var code = '';
var range;
@@ -193,8 +190,7 @@ Blockly.Python['controls_forEach'] = function(block) {
var argument0 = Blockly.Python.valueToCode(block, 'LIST',
Blockly.Python.ORDER_RELATIONAL) || '[]';
var branch = Blockly.Python.statementToCode(block, 'DO');
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
Blockly.Python.PASS;
branch = Blockly.Python.addLoopTrap(branch, block) || Blockly.Python.PASS;
var code = 'for ' + variable0 + ' in ' + argument0 + ':\n' + branch;
return code;
};

View File

@@ -56,20 +56,19 @@ Blockly.Python['procedures_defreturn'] = function(block) {
var funcName = Blockly.Python.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var branch = Blockly.Python.statementToCode(block, 'STACK');
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
if (Blockly.Python.STATEMENT_SUFFIX) {
branch = Blockly.Python.prefixLines(
Blockly.Python.STATEMENT_SUFFIX.replace( /%1/g, '\'' + id + '\''),
Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block),
Blockly.Python.INDENT) + branch;
}
if (Blockly.Python.INFINITE_LOOP_TRAP) {
branch = Blockly.Python.prefixLines(
Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g, '\'' + id + '\''),
Blockly.Python.injectId(Blockly.Python.INFINITE_LOOP_TRAP, block),
Blockly.Python.INDENT) + branch;
}
if (Blockly.Python.STATEMENT_PREFIX) {
branch = Blockly.Python.prefixLines(
Blockly.Python.STATEMENT_PREFIX.replace( /%1/g, '\'' + id + '\''),
Blockly.Python.injectId(Blockly.Python.STATEMENT_PREFIX, block),
Blockly.Python.INDENT) + branch;
}
var returnValue = Blockly.Python.valueToCode(block, 'RETURN',
@@ -128,6 +127,13 @@ Blockly.Python['procedures_ifreturn'] = function(block) {
var condition = Blockly.Python.valueToCode(block, 'CONDITION',
Blockly.Python.ORDER_NONE) || 'False';
var code = 'if ' + condition + ':\n';
if (Blockly.Python.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the return is triggered.
code += Blockly.Python.prefixLines(
Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block),
Blockly.Python.INDENT);
}
if (block.hasReturnValue_) {
var value = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_NONE) || 'None';