mirror of
https://github.com/google/blockly.git
synced 2026-01-05 08:00:09 +01:00
refactor(generators): Introduce PhpGenerator class, Order enum (#7162)
* refactor(generators): Introduce PhpGenerator class, Order enum * refactor(generators): Use Order.* instead of .ORDER_* * refactor(generators): Don't rename phpGenerator
This commit is contained in:
committed by
GitHub
parent
2f89c0dd09
commit
eeb89194c4
@@ -13,10 +13,10 @@ goog.declareModuleId('Blockly.PHP.procedures');
|
||||
|
||||
import * as Variables from '../../core/variables.js';
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {phpGenerator as PHP} from '../php.js';
|
||||
import {phpGenerator, Order} from '../php.js';
|
||||
|
||||
|
||||
PHP.forBlock['procedures_defreturn'] = function(block) {
|
||||
phpGenerator.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,99 +26,107 @@ PHP.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(PHP.nameDB_.getName(varName, NameType.VARIABLE));
|
||||
globals.push(phpGenerator.nameDB_.getName(varName, NameType.VARIABLE));
|
||||
}
|
||||
}
|
||||
// Add developer variables.
|
||||
const devVarList = Variables.allDeveloperVariables(workspace);
|
||||
for (let i = 0; i < devVarList.length; i++) {
|
||||
globals.push(
|
||||
PHP.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE));
|
||||
phpGenerator.nameDB_.getName(
|
||||
devVarList[i], NameType.DEVELOPER_VARIABLE));
|
||||
}
|
||||
const globalStr =
|
||||
globals.length ? PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
|
||||
globals.length ?
|
||||
phpGenerator.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
|
||||
|
||||
const funcName =
|
||||
PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
phpGenerator.nameDB_.getName(
|
||||
block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
let xfix1 = '';
|
||||
if (PHP.STATEMENT_PREFIX) {
|
||||
xfix1 += PHP.injectId(PHP.STATEMENT_PREFIX, block);
|
||||
if (phpGenerator.STATEMENT_PREFIX) {
|
||||
xfix1 += phpGenerator.injectId(phpGenerator.STATEMENT_PREFIX, block);
|
||||
}
|
||||
if (PHP.STATEMENT_SUFFIX) {
|
||||
xfix1 += PHP.injectId(PHP.STATEMENT_SUFFIX, block);
|
||||
if (phpGenerator.STATEMENT_SUFFIX) {
|
||||
xfix1 += phpGenerator.injectId(phpGenerator.STATEMENT_SUFFIX, block);
|
||||
}
|
||||
if (xfix1) {
|
||||
xfix1 = PHP.prefixLines(xfix1, PHP.INDENT);
|
||||
xfix1 = phpGenerator.prefixLines(xfix1, phpGenerator.INDENT);
|
||||
}
|
||||
let loopTrap = '';
|
||||
if (PHP.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = PHP.prefixLines(
|
||||
PHP.injectId(PHP.INFINITE_LOOP_TRAP, block), PHP.INDENT);
|
||||
if (phpGenerator.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = phpGenerator.prefixLines(
|
||||
phpGenerator.injectId(phpGenerator.INFINITE_LOOP_TRAP, block),
|
||||
phpGenerator.INDENT);
|
||||
}
|
||||
const branch = PHP.statementToCode(block, 'STACK');
|
||||
let returnValue = PHP.valueToCode(block, 'RETURN', PHP.ORDER_NONE) || '';
|
||||
const branch = phpGenerator.statementToCode(block, 'STACK');
|
||||
let returnValue = phpGenerator.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 = PHP.INDENT + 'return ' + returnValue + ';\n';
|
||||
returnValue = phpGenerator.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
const args = [];
|
||||
const variables = block.getVars();
|
||||
for (let i = 0; i < variables.length; i++) {
|
||||
args[i] = PHP.nameDB_.getName(variables[i], NameType.VARIABLE);
|
||||
args[i] = phpGenerator.nameDB_.getName(variables[i], NameType.VARIABLE);
|
||||
}
|
||||
let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
globalStr + xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
|
||||
code = PHP.scrub_(block, code);
|
||||
code = phpGenerator.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
PHP.definitions_['%' + funcName] = code;
|
||||
phpGenerator.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
PHP.forBlock['procedures_defnoreturn'] = PHP.forBlock['procedures_defreturn'];
|
||||
phpGenerator.forBlock['procedures_defnoreturn'] =
|
||||
phpGenerator.forBlock['procedures_defreturn'];
|
||||
|
||||
PHP.forBlock['procedures_callreturn'] = function(block) {
|
||||
phpGenerator.forBlock['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
const funcName =
|
||||
PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
phpGenerator.nameDB_.getName(
|
||||
block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
const args = [];
|
||||
const variables = block.getVars();
|
||||
for (let i = 0; i < variables.length; i++) {
|
||||
args[i] = PHP.valueToCode(block, 'ARG' + i, PHP.ORDER_NONE) || 'null';
|
||||
args[i] = phpGenerator.valueToCode(block, 'ARG' + i, Order.NONE) || 'null';
|
||||
}
|
||||
const code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['procedures_callnoreturn'] = function(block) {
|
||||
phpGenerator.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 = PHP.forBlock['procedures_callreturn'](block);
|
||||
const tuple = phpGenerator.forBlock['procedures_callreturn'](block);
|
||||
return tuple[0] + ';\n';
|
||||
};
|
||||
|
||||
PHP.forBlock['procedures_ifreturn'] = function(block) {
|
||||
phpGenerator.forBlock['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
const condition =
|
||||
PHP.valueToCode(block, 'CONDITION', PHP.ORDER_NONE) || 'false';
|
||||
phpGenerator.valueToCode(block, 'CONDITION', Order.NONE) || 'false';
|
||||
let code = 'if (' + condition + ') {\n';
|
||||
if (PHP.STATEMENT_SUFFIX) {
|
||||
if (phpGenerator.STATEMENT_SUFFIX) {
|
||||
// Inject any statement suffix here since the regular one at the end
|
||||
// will not get executed if the return is triggered.
|
||||
code +=
|
||||
PHP.prefixLines(PHP.injectId(PHP.STATEMENT_SUFFIX, block), PHP.INDENT);
|
||||
phpGenerator.prefixLines(
|
||||
phpGenerator.injectId(phpGenerator.STATEMENT_SUFFIX, block),
|
||||
phpGenerator.INDENT);
|
||||
}
|
||||
if (block.hasReturnValue_) {
|
||||
const value = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'null';
|
||||
code += PHP.INDENT + 'return ' + value + ';\n';
|
||||
const value = phpGenerator.valueToCode(block, 'VALUE', Order.NONE) || 'null';
|
||||
code += phpGenerator.INDENT + 'return ' + value + ';\n';
|
||||
} else {
|
||||
code += PHP.INDENT + 'return;\n';
|
||||
code += phpGenerator.INDENT + 'return;\n';
|
||||
}
|
||||
code += '}\n';
|
||||
return code;
|
||||
|
||||
Reference in New Issue
Block a user