mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +01:00
* 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
133 lines
4.6 KiB
JavaScript
133 lines
4.6 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2015 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating PHP for procedure blocks.
|
|
*/
|
|
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.PHP.procedures');
|
|
|
|
import * as Variables from '../../core/variables.js';
|
|
import {NameType} from '../../core/names.js';
|
|
import {Order} from './php_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 globalStr =
|
|
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);
|
|
}
|
|
const 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';
|
|
}
|
|
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 = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
|
|
globalStr + 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) || 'null';
|
|
}
|
|
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) || 'null';
|
|
code += generator.INDENT + 'return ' + value + ';\n';
|
|
} else {
|
|
code += generator.INDENT + 'return;\n';
|
|
}
|
|
code += '}\n';
|
|
return code;
|
|
};
|