refactor(generators): Introduce PythonGenerator class, Order enum (#7163)

* refactor(generators): Introduce PhpGenerator class, Order enum

* refactor(generators): Use Order.* instead of .ORDER_*

* refactor(generators): Don't rename pythonGenerator
This commit is contained in:
Christopher Allen
2023-06-14 16:51:15 +01:00
committed by GitHub
parent 26901654ea
commit 2f89c0dd09
10 changed files with 725 additions and 671 deletions

View File

@@ -13,10 +13,10 @@ goog.declareModuleId('Blockly.Python.procedures');
import * as Variables from '../../core/variables.js';
import {NameType} from '../../core/names.js';
import {pythonGenerator as Python} from '../python.js';
import {pythonGenerator, Order} from '../python.js';
Python.forBlock['procedures_defreturn'] = function(block) {
pythonGenerator.forBlock['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.
@@ -26,104 +26,111 @@ Python.forBlock['procedures_defreturn'] = function(block) {
for (let i = 0, variable; (variable = usedVariables[i]); i++) {
const varName = variable.name;
if (block.getVars().indexOf(varName) === -1) {
globals.push(Python.nameDB_.getName(varName, NameType.VARIABLE));
globals.push(pythonGenerator.nameDB_.getName(varName, NameType.VARIABLE));
}
}
// Add developer variables.
const devVarList = Variables.allDeveloperVariables(workspace);
for (let i = 0; i < devVarList.length; i++) {
globals.push(
Python.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE));
pythonGenerator.nameDB_.getName(
devVarList[i], NameType.DEVELOPER_VARIABLE));
}
const globalString = globals.length ?
Python.INDENT + 'global ' + globals.join(', ') + '\n' :
pythonGenerator.INDENT + 'global ' + globals.join(', ') + '\n' :
'';
const funcName =
Python.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
pythonGenerator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
let xfix1 = '';
if (Python.STATEMENT_PREFIX) {
xfix1 += Python.injectId(Python.STATEMENT_PREFIX, block);
if (pythonGenerator.STATEMENT_PREFIX) {
xfix1 += pythonGenerator.injectId(pythonGenerator.STATEMENT_PREFIX, block);
}
if (Python.STATEMENT_SUFFIX) {
xfix1 += Python.injectId(Python.STATEMENT_SUFFIX, block);
if (pythonGenerator.STATEMENT_SUFFIX) {
xfix1 += pythonGenerator.injectId(pythonGenerator.STATEMENT_SUFFIX, block);
}
if (xfix1) {
xfix1 = Python.prefixLines(xfix1, Python.INDENT);
xfix1 = pythonGenerator.prefixLines(xfix1, pythonGenerator.INDENT);
}
let loopTrap = '';
if (Python.INFINITE_LOOP_TRAP) {
loopTrap = Python.prefixLines(
Python.injectId(Python.INFINITE_LOOP_TRAP, block), Python.INDENT);
if (pythonGenerator.INFINITE_LOOP_TRAP) {
loopTrap = pythonGenerator.prefixLines(
pythonGenerator.injectId(pythonGenerator.INFINITE_LOOP_TRAP, block),
pythonGenerator.INDENT);
}
let branch = Python.statementToCode(block, 'STACK');
let branch = pythonGenerator.statementToCode(block, 'STACK');
let returnValue =
Python.valueToCode(block, 'RETURN', Python.ORDER_NONE) || '';
pythonGenerator.valueToCode(block, 'RETURN', Order.NONE) || '';
let xfix2 = '';
if (branch && returnValue) {
// After executing the function body, revisit this block for the return.
xfix2 = xfix1;
}
if (returnValue) {
returnValue = Python.INDENT + 'return ' + returnValue + '\n';
returnValue = pythonGenerator.INDENT + 'return ' + returnValue + '\n';
} else if (!branch) {
branch = Python.PASS;
branch = pythonGenerator.PASS;
}
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = Python.nameDB_.getName(variables[i], NameType.VARIABLE);
args[i] = pythonGenerator.nameDB_.getName(variables[i], NameType.VARIABLE);
}
let code = 'def ' + funcName + '(' + args.join(', ') + '):\n' + globalString +
xfix1 + loopTrap + branch + xfix2 + returnValue;
code = Python.scrub_(block, code);
code = pythonGenerator.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
Python.definitions_['%' + funcName] = code;
pythonGenerator.definitions_['%' + funcName] = code;
return null;
};
// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
Python.forBlock['procedures_defnoreturn'] = Python.forBlock['procedures_defreturn'];
pythonGenerator.forBlock['procedures_defnoreturn'] =
pythonGenerator.forBlock['procedures_defreturn'];
Python.forBlock['procedures_callreturn'] = function(block) {
pythonGenerator.forBlock['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
const funcName =
Python.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
pythonGenerator.nameDB_.getName(
block.getFieldValue('NAME'), NameType.PROCEDURE);
const args = [];
const variables = block.getVars();
for (let i = 0; i < variables.length; i++) {
args[i] = Python.valueToCode(block, 'ARG' + i, Python.ORDER_NONE) || 'None';
args[i] =
pythonGenerator.valueToCode(block, 'ARG' + i, Order.NONE) || 'None';
}
const code = funcName + '(' + args.join(', ') + ')';
return [code, Python.ORDER_FUNCTION_CALL];
return [code, Order.FUNCTION_CALL];
};
Python.forBlock['procedures_callnoreturn'] = function(block) {
pythonGenerator.forBlock['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 = Python.forBlock['procedures_callreturn'](block);
const tuple = pythonGenerator.forBlock['procedures_callreturn'](block);
return tuple[0] + '\n';
};
Python.forBlock['procedures_ifreturn'] = function(block) {
pythonGenerator.forBlock['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
const condition =
Python.valueToCode(block, 'CONDITION', Python.ORDER_NONE) || 'False';
pythonGenerator.valueToCode(block, 'CONDITION', Order.NONE) || 'False';
let code = 'if ' + condition + ':\n';
if (Python.STATEMENT_SUFFIX) {
if (pythonGenerator.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the return is triggered.
code += Python.prefixLines(
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT);
code += pythonGenerator.prefixLines(
pythonGenerator.injectId(
pythonGenerator.STATEMENT_SUFFIX, block), pythonGenerator.INDENT);
}
if (block.hasReturnValue_) {
const value =
Python.valueToCode(block, 'VALUE', Python.ORDER_NONE) || 'None';
code += Python.INDENT + 'return ' + value + '\n';
pythonGenerator.valueToCode(block, 'VALUE', Order.NONE) || 'None';
code += pythonGenerator.INDENT + 'return ' + value + '\n';
} else {
code += Python.INDENT + 'return\n';
code += pythonGenerator.INDENT + 'return\n';
}
return code;
};