mirror of
https://github.com/google/blockly.git
synced 2026-01-20 07:17:10 +01:00
refactor: convert python block generators to goog.module (#5771)
* refactor: convert generators/python/colour.js to goog.module * refactor: convert generators/python/colour.js to named requires * chore: run clang-format * refactor: convert generators/python/lists.js to goog.module * refactor: convert generators/python/lists.js to named requires * chore: run clang-format * refactor: convert generators/python/logic.js to goog.module * refactor: convert generators/python/logic.js to named requires * chore: run clang-format * refactor: convert generators/python/loops.js to goog.module * refactor: convert generators/python/loops.js to named requires * chore: run clang-format * refactor: convert generators/python/math.js to goog.module * refactor: convert generators/python/math.js to named requires * chore: run clang-format * refactor: convert generators/python/procedures.js to goog.module * refactor: convert generators/python/procedures.js to named requires * chore: run clang-format * refactor: convert generators/python/text.js to goog.module * refactor: convert generators/python/text.js to named requires * chore: run clang-format * refactor: convert generators/python/variables_dynamic.js to named requires * refactor: convert generators/python/variables.js to named requires * chore: run clang-format * refactor: convert generators/python.js to goog.module * refactor: convert generators/python.js to named requires * chore: run clang-format * chore: remove spurious @private annotations * chore: rebuild
This commit is contained in:
@@ -6,129 +6,124 @@
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for procedure blocks.
|
||||
* @suppress {missingRequire}
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.procedures');
|
||||
goog.module('Blockly.Python.procedures');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
goog.require('Blockly.Variables');
|
||||
const Python = goog.require('Blockly.Python');
|
||||
const Variables = goog.require('Blockly.Variables');
|
||||
const {NameType} = goog.require('Blockly.Names');
|
||||
|
||||
|
||||
Blockly.Python['procedures_defreturn'] = function(block) {
|
||||
Python['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
// First, add a 'global' statement for every variable that is not shadowed by
|
||||
// a local parameter.
|
||||
const globals = [];
|
||||
const workspace = block.workspace;
|
||||
const usedVariables = Blockly.Variables.allUsedVarModels(workspace) || [];
|
||||
const usedVariables = Variables.allUsedVarModels(workspace) || [];
|
||||
for (let i = 0, variable; (variable = usedVariables[i]); i++) {
|
||||
const varName = variable.name;
|
||||
if (block.getVars().indexOf(varName) === -1) {
|
||||
globals.push(Blockly.Python.nameDB_.getName(varName,
|
||||
Blockly.VARIABLE_CATEGORY_NAME));
|
||||
globals.push(Python.nameDB_.getName(varName, NameType.VARIABLE));
|
||||
}
|
||||
}
|
||||
// Add developer variables.
|
||||
const devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
const devVarList = Variables.allDeveloperVariables(workspace);
|
||||
for (let i = 0; i < devVarList.length; i++) {
|
||||
globals.push(Blockly.Python.nameDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
globals.push(
|
||||
Python.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE));
|
||||
}
|
||||
|
||||
const globalString = globals.length ?
|
||||
Blockly.Python.INDENT + 'global ' + globals.join(', ') + '\n' : '';
|
||||
const funcName = Blockly.Python.nameDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
|
||||
Python.INDENT + 'global ' + globals.join(', ') + '\n' :
|
||||
'';
|
||||
const funcName =
|
||||
Python.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
let xfix1 = '';
|
||||
if (Blockly.Python.STATEMENT_PREFIX) {
|
||||
xfix1 += Blockly.Python.injectId(Blockly.Python.STATEMENT_PREFIX, block);
|
||||
if (Python.STATEMENT_PREFIX) {
|
||||
xfix1 += Python.injectId(Python.STATEMENT_PREFIX, block);
|
||||
}
|
||||
if (Blockly.Python.STATEMENT_SUFFIX) {
|
||||
xfix1 += Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block);
|
||||
if (Python.STATEMENT_SUFFIX) {
|
||||
xfix1 += Python.injectId(Python.STATEMENT_SUFFIX, block);
|
||||
}
|
||||
if (xfix1) {
|
||||
xfix1 = Blockly.Python.prefixLines(xfix1, Blockly.Python.INDENT);
|
||||
xfix1 = Python.prefixLines(xfix1, Python.INDENT);
|
||||
}
|
||||
let loopTrap = '';
|
||||
if (Blockly.Python.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = Blockly.Python.prefixLines(
|
||||
Blockly.Python.injectId(Blockly.Python.INFINITE_LOOP_TRAP, block),
|
||||
Blockly.Python.INDENT);
|
||||
if (Python.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = Python.prefixLines(
|
||||
Python.injectId(Python.INFINITE_LOOP_TRAP, block), Python.INDENT);
|
||||
}
|
||||
let branch = Blockly.Python.statementToCode(block, 'STACK');
|
||||
let returnValue = Blockly.Python.valueToCode(block, 'RETURN',
|
||||
Blockly.Python.ORDER_NONE) || '';
|
||||
let branch = Python.statementToCode(block, 'STACK');
|
||||
let returnValue =
|
||||
Python.valueToCode(block, 'RETURN', Python.ORDER_NONE) || '';
|
||||
let xfix2 = '';
|
||||
if (branch && returnValue) {
|
||||
// After executing the function body, revisit this block for the return.
|
||||
xfix2 = xfix1;
|
||||
}
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.Python.INDENT + 'return ' + returnValue + '\n';
|
||||
returnValue = Python.INDENT + 'return ' + returnValue + '\n';
|
||||
} else if (!branch) {
|
||||
branch = Blockly.Python.PASS;
|
||||
branch = Python.PASS;
|
||||
}
|
||||
const args = [];
|
||||
const variables = block.getVars();
|
||||
for (let i = 0; i < variables.length; i++) {
|
||||
args[i] = Blockly.Python.nameDB_.getName(variables[i],
|
||||
Blockly.VARIABLE_CATEGORY_NAME);
|
||||
args[i] = Python.nameDB_.getName(variables[i], NameType.VARIABLE);
|
||||
}
|
||||
let code = 'def ' + funcName + '(' + args.join(', ') + '):\n' + globalString +
|
||||
xfix1 + loopTrap + branch + xfix2 + returnValue;
|
||||
code = Blockly.Python.scrub_(block, code);
|
||||
code = Python.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.Python.definitions_['%' + funcName] = code;
|
||||
Python.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Blockly.Python['procedures_defnoreturn'] =
|
||||
Blockly.Python['procedures_defreturn'];
|
||||
Python['procedures_defnoreturn'] = Python['procedures_defreturn'];
|
||||
|
||||
Blockly.Python['procedures_callreturn'] = function(block) {
|
||||
Python['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
const funcName = Blockly.Python.nameDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.PROCEDURE_CATEGORY_NAME);
|
||||
const funcName =
|
||||
Python.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
const args = [];
|
||||
const variables = block.getVars();
|
||||
for (let i = 0; i < variables.length; i++) {
|
||||
args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
args[i] = Python.valueToCode(block, 'ARG' + i, Python.ORDER_NONE) || 'None';
|
||||
}
|
||||
const code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
return [code, Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['procedures_callnoreturn'] = function(block) {
|
||||
Python['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
// Generated code is for a function call as a statement is the same as a
|
||||
// function call as a value, with the addition of line ending.
|
||||
const tuple = Blockly.Python['procedures_callreturn'](block);
|
||||
const tuple = Python['procedures_callreturn'](block);
|
||||
return tuple[0] + '\n';
|
||||
};
|
||||
|
||||
Blockly.Python['procedures_ifreturn'] = function(block) {
|
||||
Python['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
const condition = Blockly.Python.valueToCode(block, 'CONDITION',
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
const condition =
|
||||
Python.valueToCode(block, 'CONDITION', Python.ORDER_NONE) || 'False';
|
||||
let code = 'if ' + condition + ':\n';
|
||||
if (Blockly.Python.STATEMENT_SUFFIX) {
|
||||
if (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);
|
||||
code += Python.prefixLines(
|
||||
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT);
|
||||
}
|
||||
if (block.hasReturnValue_) {
|
||||
const value = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
code += Blockly.Python.INDENT + 'return ' + value + '\n';
|
||||
const value =
|
||||
Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || 'None';
|
||||
code += Python.INDENT + 'return ' + value + '\n';
|
||||
} else {
|
||||
code += Blockly.Python.INDENT + 'return\n';
|
||||
code += Python.INDENT + 'return\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user