mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +01:00
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:
committed by
GitHub
parent
26901654ea
commit
2f89c0dd09
@@ -11,37 +11,43 @@
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Python.logic');
|
||||
|
||||
import {pythonGenerator as Python} from '../python.js';
|
||||
import {pythonGenerator, Order} from '../python.js';
|
||||
|
||||
|
||||
Python.forBlock['controls_if'] = function(block) {
|
||||
pythonGenerator.forBlock['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
let n = 0;
|
||||
let code = '', branchCode, conditionCode;
|
||||
if (Python.STATEMENT_PREFIX) {
|
||||
if (pythonGenerator.STATEMENT_PREFIX) {
|
||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||
code += Python.injectId(Python.STATEMENT_PREFIX, block);
|
||||
code += pythonGenerator.injectId(pythonGenerator.STATEMENT_PREFIX, block);
|
||||
}
|
||||
do {
|
||||
conditionCode =
|
||||
Python.valueToCode(block, 'IF' + n, Python.ORDER_NONE) || 'False';
|
||||
branchCode = Python.statementToCode(block, 'DO' + n) || Python.PASS;
|
||||
if (Python.STATEMENT_SUFFIX) {
|
||||
pythonGenerator.valueToCode(block, 'IF' + n, Order.NONE) || 'False';
|
||||
branchCode =
|
||||
pythonGenerator.statementToCode(block, 'DO' + n) ||
|
||||
pythonGenerator.PASS;
|
||||
if (pythonGenerator.STATEMENT_SUFFIX) {
|
||||
branchCode =
|
||||
Python.prefixLines(
|
||||
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT) +
|
||||
pythonGenerator.prefixLines(
|
||||
pythonGenerator.injectId(pythonGenerator.STATEMENT_SUFFIX, block),
|
||||
pythonGenerator.INDENT) +
|
||||
branchCode;
|
||||
}
|
||||
code += (n === 0 ? 'if ' : 'elif ') + conditionCode + ':\n' + branchCode;
|
||||
n++;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE') || Python.STATEMENT_SUFFIX) {
|
||||
branchCode = Python.statementToCode(block, 'ELSE') || Python.PASS;
|
||||
if (Python.STATEMENT_SUFFIX) {
|
||||
if (block.getInput('ELSE') || pythonGenerator.STATEMENT_SUFFIX) {
|
||||
branchCode =
|
||||
pythonGenerator.statementToCode(block, 'ELSE') || pythonGenerator.PASS;
|
||||
if (pythonGenerator.STATEMENT_SUFFIX) {
|
||||
branchCode =
|
||||
Python.prefixLines(
|
||||
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT) +
|
||||
pythonGenerator.prefixLines(
|
||||
pythonGenerator.injectId(
|
||||
pythonGenerator.STATEMENT_SUFFIX, block),
|
||||
pythonGenerator.INDENT) +
|
||||
branchCode;
|
||||
}
|
||||
code += 'else:\n' + branchCode;
|
||||
@@ -49,27 +55,28 @@ Python.forBlock['controls_if'] = function(block) {
|
||||
return code;
|
||||
};
|
||||
|
||||
Python.forBlock['controls_ifelse'] = Python.forBlock['controls_if'];
|
||||
pythonGenerator.forBlock['controls_ifelse'] =
|
||||
pythonGenerator.forBlock['controls_if'];
|
||||
|
||||
Python.forBlock['logic_compare'] = function(block) {
|
||||
pythonGenerator.forBlock['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
const OPERATORS =
|
||||
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
||||
const operator = OPERATORS[block.getFieldValue('OP')];
|
||||
const order = Python.ORDER_RELATIONAL;
|
||||
const argument0 = Python.valueToCode(block, 'A', order) || '0';
|
||||
const argument1 = Python.valueToCode(block, 'B', order) || '0';
|
||||
const order = Order.RELATIONAL;
|
||||
const argument0 = pythonGenerator.valueToCode(block, 'A', order) || '0';
|
||||
const argument1 = pythonGenerator.valueToCode(block, 'B', order) || '0';
|
||||
const code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Python.forBlock['logic_operation'] = function(block) {
|
||||
pythonGenerator.forBlock['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
||||
const order =
|
||||
(operator === 'and') ? Python.ORDER_LOGICAL_AND : Python.ORDER_LOGICAL_OR;
|
||||
let argument0 = Python.valueToCode(block, 'A', order);
|
||||
let argument1 = Python.valueToCode(block, 'B', order);
|
||||
(operator === 'and') ? Order.LOGICAL_AND : Order.LOGICAL_OR;
|
||||
let argument0 = pythonGenerator.valueToCode(block, 'A', order);
|
||||
let argument1 = pythonGenerator.valueToCode(block, 'B', order);
|
||||
if (!argument0 && !argument1) {
|
||||
// If there are no arguments, then the return value is false.
|
||||
argument0 = 'False';
|
||||
@@ -88,33 +95,33 @@ Python.forBlock['logic_operation'] = function(block) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Python.forBlock['logic_negate'] = function(block) {
|
||||
pythonGenerator.forBlock['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
const argument0 =
|
||||
Python.valueToCode(block, 'BOOL', Python.ORDER_LOGICAL_NOT) || 'True';
|
||||
pythonGenerator.valueToCode(block, 'BOOL', Order.LOGICAL_NOT) || 'True';
|
||||
const code = 'not ' + argument0;
|
||||
return [code, Python.ORDER_LOGICAL_NOT];
|
||||
return [code, Order.LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Python.forBlock['logic_boolean'] = function(block) {
|
||||
pythonGenerator.forBlock['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'True' : 'False';
|
||||
return [code, Python.ORDER_ATOMIC];
|
||||
return [code, Order.ATOMIC];
|
||||
};
|
||||
|
||||
Python.forBlock['logic_null'] = function(block) {
|
||||
pythonGenerator.forBlock['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['None', Python.ORDER_ATOMIC];
|
||||
return ['None', Order.ATOMIC];
|
||||
};
|
||||
|
||||
Python.forBlock['logic_ternary'] = function(block) {
|
||||
pythonGenerator.forBlock['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
const value_if =
|
||||
Python.valueToCode(block, 'IF', Python.ORDER_CONDITIONAL) || 'False';
|
||||
pythonGenerator.valueToCode(block, 'IF', Order.CONDITIONAL) || 'False';
|
||||
const value_then =
|
||||
Python.valueToCode(block, 'THEN', Python.ORDER_CONDITIONAL) || 'None';
|
||||
pythonGenerator.valueToCode(block, 'THEN', Order.CONDITIONAL) || 'None';
|
||||
const value_else =
|
||||
Python.valueToCode(block, 'ELSE', Python.ORDER_CONDITIONAL) || 'None';
|
||||
pythonGenerator.valueToCode(block, 'ELSE', Order.CONDITIONAL) || 'None';
|
||||
const code = value_then + ' if ' + value_if + ' else ' + value_else;
|
||||
return [code, Python.ORDER_CONDITIONAL];
|
||||
return [code, Order.CONDITIONAL];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user