Files
blockly/generators/python/procedures.js
Christopher Allen 130989763c refactor(generators): Restructure generator modules to contain side effects (#7173)
* refactor(generators): Move lang.js -> lang/lang_gernator.js

  Move the LangGenerator definitions into their respective
  subdirectories and add a _generator suffix to their filenames,
  i.e. generators/javascript.js  becomes
  generators/javascript/javascript_generator.js.

  This is to keep related code together and allow the `lang/all.js`
  entrypoints to be moved to the top level generators/ directory.

  No goog module IDs were changed, so playground and test code
  that accesses this modules by filename does not need to be modified.

* refactor(generators) Move lang/all.js -> lang.js

  - Move the entrypoints in generators/*/all.js to correspondingly-named
    files in generators/ instead—i.e., generators/javascript/all.js
    becomes generators/javascript.js.

  - Update build_tasks.js accordingly.

* fix(generators): Add missing exports for LuaGenerator, PhpGenerator

  These were inadvertently omitted from #7161 and #7162, respectively.

* refactor(generators): Make block generator modules side-effect free

  - Move declaration of <lang>Generator instance from
    generators/<lang>/<lang>_generator.js to generators/<lang>.js.
  - Move .addReservedWords() calls from generators/<lang>/*.js to
    generators/<lang>.js
  - Modify generators/<lang>/*.js to export block generator functions
    individually, rather than installing on <lang>Generator instance.
  - Modify generators/<lang>.js to import and install block generator
    functions on <lang>Generator instance.

* fix(tests): Fix tests broken by restructuring of generators

  Where these tests needed block generator functions preinstalled
  they should have been importing the Blockly.<Lang>.all module.

  Where they do not need the provided block generator functions
  they can now create their own empty <Lang>Generator instances.

* chore: Update renamings file

  - Fix a malformation in previous entries that was not detected by
    the renaming file validator test.
  - Add entries describing the work done in this and related recent
    PRs.

* fix: Correct minor errors in PR #7173

  - Fix a search-and-replace error in renamings.json5
  - Fix an incorrect-but-usable import in generator_test.js
2023-06-20 23:22:44 +01:00

136 lines
4.6 KiB
JavaScript

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