Files
blockly/generators/python/logic.js
Christopher Allen a3458871db refactor(generators)!: Pass this CodeGenerator to individual generator functions (#7168)
* feat(generators): Pass this CodeGenerator to generator functions

  This implements option 1A of proposal 1 of #7086.

  This commit is not by itself a breaking change, except in the unlikely event that
  developers' custom generator functions take an (optional) second argument of a
  dfferent type.

* feat(generators): Accept generator argument in block functions

  Accept a CodeGenerator instance as parameter two of every
  per-block-type generator function.

* fix(generators): Pass generator when calling other generator functions

  Make sure to pass generator to any other block functions that are
  called recursively.

* refactor(generators)!: Use generator argument in generator functions

  Refactor per-block-type generator functions to use the provided
  generator argument to make recursive calls, rather than depending
  on the closed-over <lang>Generator instance.

  This allows generator functions to be moved between CodeGenerator
  instances (of the same language, at least).

  This commit was created by search-and-replace and addresses most
  but not all recursive references; remaining uses will require
  manual attention and will be dealt with in a following commit.

  BREAKING CHANGE: This commit makes the generator functions we provide
  dependent on the new generator parameter.  Although
  CodeGenerator.prototype.blockToCode has been modified to supply this,
  so this change will not affect most developers, this change will be a
  breaking change where developers make direct calls to these generator
  functions without supplying the generator parameter.  See previous
  commit for an example of the update required.

* refactor(generators): Manual fix for remaining uses of langGenerator

  Manually replace remaining uses of <lang>Generator in block
  generator functions.

* fix(generators): Delete duplicate procedures_callnoreturn generator

  For some reason the generator function for procedures_callnoreturn
  appears twice in generators/javascript/procedures.js.  Delete the
  first copy (since the second one overwrote it anyway).

* chore(generators): Format
2023-06-14 23:25:36 +01:00

128 lines
4.1 KiB
JavaScript

/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Generating Python for logic blocks.
*/
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Python.logic');
import {pythonGenerator, Order} from '../python.js';
pythonGenerator.forBlock['controls_if'] = function(block, generator) {
// If/elseif/else condition.
let n = 0;
let code = '', branchCode, conditionCode;
if (generator.STATEMENT_PREFIX) {
// Automatic prefix insertion is switched off for this block. Add manually.
code += generator.injectId(generator.STATEMENT_PREFIX, block);
}
do {
conditionCode =
generator.valueToCode(block, 'IF' + n, Order.NONE) || 'False';
branchCode =
generator.statementToCode(block, 'DO' + n) ||
generator.PASS;
if (generator.STATEMENT_SUFFIX) {
branchCode =
generator.prefixLines(
generator.injectId(generator.STATEMENT_SUFFIX, block),
generator.INDENT) +
branchCode;
}
code += (n === 0 ? 'if ' : 'elif ') + conditionCode + ':\n' + branchCode;
n++;
} while (block.getInput('IF' + n));
if (block.getInput('ELSE') || generator.STATEMENT_SUFFIX) {
branchCode =
generator.statementToCode(block, 'ELSE') || generator.PASS;
if (generator.STATEMENT_SUFFIX) {
branchCode =
generator.prefixLines(
generator.injectId(
generator.STATEMENT_SUFFIX, block),
generator.INDENT) +
branchCode;
}
code += 'else:\n' + branchCode;
}
return code;
};
pythonGenerator.forBlock['controls_ifelse'] =
pythonGenerator.forBlock['controls_if'];
pythonGenerator.forBlock['logic_compare'] = function(block, generator) {
// Comparison operator.
const OPERATORS =
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
const operator = OPERATORS[block.getFieldValue('OP')];
const order = Order.RELATIONAL;
const argument0 = generator.valueToCode(block, 'A', order) || '0';
const argument1 = generator.valueToCode(block, 'B', order) || '0';
const code = argument0 + ' ' + operator + ' ' + argument1;
return [code, order];
};
pythonGenerator.forBlock['logic_operation'] = function(block, generator) {
// Operations 'and', 'or'.
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
const order =
(operator === 'and') ? Order.LOGICAL_AND : Order.LOGICAL_OR;
let argument0 = generator.valueToCode(block, 'A', order);
let argument1 = generator.valueToCode(block, 'B', order);
if (!argument0 && !argument1) {
// If there are no arguments, then the return value is false.
argument0 = 'False';
argument1 = 'False';
} else {
// Single missing arguments have no effect on the return value.
const defaultArgument = (operator === 'and') ? 'True' : 'False';
if (!argument0) {
argument0 = defaultArgument;
}
if (!argument1) {
argument1 = defaultArgument;
}
}
const code = argument0 + ' ' + operator + ' ' + argument1;
return [code, order];
};
pythonGenerator.forBlock['logic_negate'] = function(block, generator) {
// Negation.
const argument0 =
generator.valueToCode(block, 'BOOL', Order.LOGICAL_NOT) || 'True';
const code = 'not ' + argument0;
return [code, Order.LOGICAL_NOT];
};
pythonGenerator.forBlock['logic_boolean'] = function(block, generator) {
// Boolean values true and false.
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'True' : 'False';
return [code, Order.ATOMIC];
};
pythonGenerator.forBlock['logic_null'] = function(block, generator) {
// Null data type.
return ['None', Order.ATOMIC];
};
pythonGenerator.forBlock['logic_ternary'] = function(block, generator) {
// Ternary operator.
const value_if =
generator.valueToCode(block, 'IF', Order.CONDITIONAL) || 'False';
const value_then =
generator.valueToCode(block, 'THEN', Order.CONDITIONAL) || 'None';
const value_else =
generator.valueToCode(block, 'ELSE', Order.CONDITIONAL) || 'None';
const code = value_then + ' if ' + value_if + ' else ' + value_else;
return [code, Order.CONDITIONAL];
};