mirror of
https://github.com/google/blockly.git
synced 2026-01-04 07:30:08 +01:00
refactor: convert python block generators to goog.module (#5771)
* refactor: convert generators/python/colour.js to goog.module * refactor: convert generators/python/colour.js to named requires * chore: run clang-format * refactor: convert generators/python/lists.js to goog.module * refactor: convert generators/python/lists.js to named requires * chore: run clang-format * refactor: convert generators/python/logic.js to goog.module * refactor: convert generators/python/logic.js to named requires * chore: run clang-format * refactor: convert generators/python/loops.js to goog.module * refactor: convert generators/python/loops.js to named requires * chore: run clang-format * refactor: convert generators/python/math.js to goog.module * refactor: convert generators/python/math.js to named requires * chore: run clang-format * refactor: convert generators/python/procedures.js to goog.module * refactor: convert generators/python/procedures.js to named requires * chore: run clang-format * refactor: convert generators/python/text.js to goog.module * refactor: convert generators/python/text.js to named requires * chore: run clang-format * refactor: convert generators/python/variables_dynamic.js to named requires * refactor: convert generators/python/variables.js to named requires * chore: run clang-format * refactor: convert generators/python.js to goog.module * refactor: convert generators/python.js to named requires * chore: run clang-format * chore: remove spurious @private annotations * chore: rebuild
This commit is contained in:
@@ -9,73 +9,67 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.logic');
|
||||
goog.module('Blockly.Python.logic');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
const Python = goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['controls_if'] = function(block) {
|
||||
Python['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
let n = 0;
|
||||
let code = '', branchCode, conditionCode;
|
||||
if (Blockly.Python.STATEMENT_PREFIX) {
|
||||
if (Python.STATEMENT_PREFIX) {
|
||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||
code += Blockly.Python.injectId(Blockly.Python.STATEMENT_PREFIX, block);
|
||||
code += Python.injectId(Python.STATEMENT_PREFIX, block);
|
||||
}
|
||||
do {
|
||||
conditionCode = Blockly.Python.valueToCode(block, 'IF' + n,
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
branchCode = Blockly.Python.statementToCode(block, 'DO' + n) ||
|
||||
Blockly.Python.PASS;
|
||||
if (Blockly.Python.STATEMENT_SUFFIX) {
|
||||
branchCode = Blockly.Python.prefixLines(
|
||||
Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block),
|
||||
Blockly.Python.INDENT) + branchCode;
|
||||
conditionCode =
|
||||
Python.valueToCode(block, 'IF' + n, Python.ORDER_NONE) || 'False';
|
||||
branchCode = Python.statementToCode(block, 'DO' + n) || Python.PASS;
|
||||
if (Python.STATEMENT_SUFFIX) {
|
||||
branchCode =
|
||||
Python.prefixLines(
|
||||
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT) +
|
||||
branchCode;
|
||||
}
|
||||
code += (n === 0 ? 'if ' : 'elif ') + conditionCode + ':\n' + branchCode;
|
||||
n++;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE') || Blockly.Python.STATEMENT_SUFFIX) {
|
||||
branchCode = Blockly.Python.statementToCode(block, 'ELSE') ||
|
||||
Blockly.Python.PASS;
|
||||
if (Blockly.Python.STATEMENT_SUFFIX) {
|
||||
branchCode = Blockly.Python.prefixLines(
|
||||
Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block),
|
||||
Blockly.Python.INDENT) + branchCode;
|
||||
if (block.getInput('ELSE') || Python.STATEMENT_SUFFIX) {
|
||||
branchCode = Python.statementToCode(block, 'ELSE') || Python.PASS;
|
||||
if (Python.STATEMENT_SUFFIX) {
|
||||
branchCode =
|
||||
Python.prefixLines(
|
||||
Python.injectId(Python.STATEMENT_SUFFIX, block), Python.INDENT) +
|
||||
branchCode;
|
||||
}
|
||||
code += 'else:\n' + branchCode;
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['controls_ifelse'] = Blockly.Python['controls_if'];
|
||||
Python['controls_ifelse'] = Python['controls_if'];
|
||||
|
||||
Blockly.Python['logic_compare'] = function(block) {
|
||||
Python['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
const OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '!=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
const OPERATORS =
|
||||
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
||||
const operator = OPERATORS[block.getFieldValue('OP')];
|
||||
const order = Blockly.Python.ORDER_RELATIONAL;
|
||||
const argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
|
||||
const argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
|
||||
const order = Python.ORDER_RELATIONAL;
|
||||
const argument0 = Python.valueToCode(block, 'A', order) || '0';
|
||||
const argument1 = Python.valueToCode(block, 'B', order) || '0';
|
||||
const code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_operation'] = function(block) {
|
||||
Python['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
||||
const order = (operator === 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
|
||||
Blockly.Python.ORDER_LOGICAL_OR;
|
||||
let argument0 = Blockly.Python.valueToCode(block, 'A', order);
|
||||
let argument1 = Blockly.Python.valueToCode(block, 'B', order);
|
||||
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);
|
||||
if (!argument0 && !argument1) {
|
||||
// If there are no arguments, then the return value is false.
|
||||
argument0 = 'False';
|
||||
@@ -94,33 +88,33 @@ Blockly.Python['logic_operation'] = function(block) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_negate'] = function(block) {
|
||||
Python['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
const argument0 = Blockly.Python.valueToCode(block, 'BOOL',
|
||||
Blockly.Python.ORDER_LOGICAL_NOT) || 'True';
|
||||
const argument0 =
|
||||
Python.valueToCode(block, 'BOOL', Python.ORDER_LOGICAL_NOT) || 'True';
|
||||
const code = 'not ' + argument0;
|
||||
return [code, Blockly.Python.ORDER_LOGICAL_NOT];
|
||||
return [code, Python.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_boolean'] = function(block) {
|
||||
Python['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'True' : 'False';
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
return [code, Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_null'] = function(block) {
|
||||
Python['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['None', Blockly.Python.ORDER_ATOMIC];
|
||||
return ['None', Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_ternary'] = function(block) {
|
||||
Python['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
const value_if = Blockly.Python.valueToCode(block, 'IF',
|
||||
Blockly.Python.ORDER_CONDITIONAL) || 'False';
|
||||
const value_then = Blockly.Python.valueToCode(block, 'THEN',
|
||||
Blockly.Python.ORDER_CONDITIONAL) || 'None';
|
||||
const value_else = Blockly.Python.valueToCode(block, 'ELSE',
|
||||
Blockly.Python.ORDER_CONDITIONAL) || 'None';
|
||||
const value_if =
|
||||
Python.valueToCode(block, 'IF', Python.ORDER_CONDITIONAL) || 'False';
|
||||
const value_then =
|
||||
Python.valueToCode(block, 'THEN', Python.ORDER_CONDITIONAL) || 'None';
|
||||
const value_else =
|
||||
Python.valueToCode(block, 'ELSE', Python.ORDER_CONDITIONAL) || 'None';
|
||||
const code = value_then + ' if ' + value_if + ' else ' + value_else;
|
||||
return [code, Blockly.Python.ORDER_CONDITIONAL];
|
||||
return [code, Python.ORDER_CONDITIONAL];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user