mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
refactor(generators): Introduce DartGenerator class, Order enum (#7160)
* refactor(generators): Introduce DartGenerator class, Order enum * refactor(generators): Use Order.* instead of .ORDER_* * refactor(generators): Don't rename dartGenerator
This commit is contained in:
committed by
GitHub
parent
aeee278767
commit
26901654ea
@@ -12,93 +12,99 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Dart.procedures');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {dartGenerator as Dart} from '../dart.js';
|
||||
import {dartGenerator, Order} from '../dart.js';
|
||||
|
||||
|
||||
Dart.forBlock['procedures_defreturn'] = function(block) {
|
||||
dartGenerator.forBlock['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
const funcName =
|
||||
Dart.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
dartGenerator.nameDB_.getName(
|
||||
block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
let xfix1 = '';
|
||||
if (Dart.STATEMENT_PREFIX) {
|
||||
xfix1 += Dart.injectId(Dart.STATEMENT_PREFIX, block);
|
||||
if (dartGenerator.STATEMENT_PREFIX) {
|
||||
xfix1 += dartGenerator.injectId(dartGenerator.STATEMENT_PREFIX, block);
|
||||
}
|
||||
if (Dart.STATEMENT_SUFFIX) {
|
||||
xfix1 += Dart.injectId(Dart.STATEMENT_SUFFIX, block);
|
||||
if (dartGenerator.STATEMENT_SUFFIX) {
|
||||
xfix1 += dartGenerator.injectId(dartGenerator.STATEMENT_SUFFIX, block);
|
||||
}
|
||||
if (xfix1) {
|
||||
xfix1 = Dart.prefixLines(xfix1, Dart.INDENT);
|
||||
xfix1 = dartGenerator.prefixLines(xfix1, dartGenerator.INDENT);
|
||||
}
|
||||
let loopTrap = '';
|
||||
if (Dart.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = Dart.prefixLines(
|
||||
Dart.injectId(Dart.INFINITE_LOOP_TRAP, block), Dart.INDENT);
|
||||
if (dartGenerator.INFINITE_LOOP_TRAP) {
|
||||
loopTrap = dartGenerator.prefixLines(
|
||||
dartGenerator.injectId(dartGenerator.INFINITE_LOOP_TRAP, block),
|
||||
dartGenerator.INDENT);
|
||||
}
|
||||
const branch = Dart.statementToCode(block, 'STACK');
|
||||
let returnValue = Dart.valueToCode(block, 'RETURN', Dart.ORDER_NONE) || '';
|
||||
const branch = dartGenerator.statementToCode(block, 'STACK');
|
||||
let returnValue =
|
||||
dartGenerator.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 = Dart.INDENT + 'return ' + returnValue + ';\n';
|
||||
returnValue = dartGenerator.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
const returnType = returnValue ? 'dynamic' : 'void';
|
||||
const args = [];
|
||||
const variables = block.getVars();
|
||||
for (let i = 0; i < variables.length; i++) {
|
||||
args[i] = Dart.nameDB_.getName(variables[i], NameType.VARIABLE);
|
||||
args[i] = dartGenerator.nameDB_.getName(variables[i], NameType.VARIABLE);
|
||||
}
|
||||
let code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
|
||||
code = Dart.scrub_(block, code);
|
||||
code = dartGenerator.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Dart.definitions_['%' + funcName] = code;
|
||||
dartGenerator.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Dart.forBlock['procedures_defnoreturn'] = Dart.forBlock['procedures_defreturn'];
|
||||
dartGenerator.forBlock['procedures_defnoreturn'] = dartGenerator.forBlock['procedures_defreturn'];
|
||||
|
||||
Dart.forBlock['procedures_callreturn'] = function(block) {
|
||||
dartGenerator.forBlock['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
const funcName =
|
||||
Dart.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||
dartGenerator.nameDB_.getName(
|
||||
block.getFieldValue('NAME'),NameType.PROCEDURE);
|
||||
const args = [];
|
||||
const variables = block.getVars();
|
||||
for (let i = 0; i < variables.length; i++) {
|
||||
args[i] = Dart.valueToCode(block, 'ARG' + i, Dart.ORDER_NONE) || 'null';
|
||||
args[i] = dartGenerator.valueToCode(block, 'ARG' + i, Order.NONE) || 'null';
|
||||
}
|
||||
let code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||
return [code, Order.UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Dart.forBlock['procedures_callnoreturn'] = function(block) {
|
||||
dartGenerator.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 = Dart.forBlock['procedures_callreturn'](block);
|
||||
const tuple = dartGenerator.forBlock['procedures_callreturn'](block);
|
||||
return tuple[0] + ';\n';
|
||||
};
|
||||
|
||||
Dart.forBlock['procedures_ifreturn'] = function(block) {
|
||||
dartGenerator.forBlock['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
const condition =
|
||||
Dart.valueToCode(block, 'CONDITION', Dart.ORDER_NONE) || 'false';
|
||||
dartGenerator.valueToCode(block, 'CONDITION', Order.NONE) || 'false';
|
||||
let code = 'if (' + condition + ') {\n';
|
||||
if (Dart.STATEMENT_SUFFIX) {
|
||||
if (dartGenerator.STATEMENT_SUFFIX) {
|
||||
// Inject any statement suffix here since the regular one at the end
|
||||
// will not get executed if the return is triggered.
|
||||
code += Dart.prefixLines(
|
||||
Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT);
|
||||
code += dartGenerator.prefixLines(
|
||||
dartGenerator.injectId(
|
||||
dartGenerator.STATEMENT_SUFFIX, block), dartGenerator.INDENT);
|
||||
}
|
||||
if (block.hasReturnValue_) {
|
||||
const value = Dart.valueToCode(block, 'VALUE', Dart.ORDER_NONE) || 'null';
|
||||
code += Dart.INDENT + 'return ' + value + ';\n';
|
||||
const value =
|
||||
dartGenerator.valueToCode(block, 'VALUE', Order.NONE) || 'null';
|
||||
code += dartGenerator.INDENT + 'return ' + value + ';\n';
|
||||
} else {
|
||||
code += Dart.INDENT + 'return;\n';
|
||||
code += dartGenerator.INDENT + 'return;\n';
|
||||
}
|
||||
code += '}\n';
|
||||
return code;
|
||||
|
||||
Reference in New Issue
Block a user