mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
* refactor(generators): Migrate python_generator.js to TypeScript * refactor(generators): Migrate generators/python/* to TypeScript First pass doing very mechanistic migration, not attempting to fix all the resulting type errors. * fix(generators): Fix type errors in generator functions This consists almost entirely of adding casts, so the code output by tsc should be as similar as possible to the pre-migration .js source files. * refactor(generators): Migrate generators/python.js to TypeScript The way the generator functions are added to pythonGenerator.forBlock has been modified so that incorrect generator function signatures will cause tsc to generate a type error. * chore(generator): Format One block protected with // prettier-ignore to preserve careful comment formatting. Where there are repeated concatenations prettier has made a pretty mess of things, but the correct fix is probably to use template literals instead (rather than just locally disabling prettier). This is one of the items in the to-do list in #7600.
This commit is contained in:
committed by
GitHub
parent
a2b895f7a9
commit
b418907e41
148
generators/python/logic.ts
Normal file
148
generators/python/logic.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2012 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for logic blocks.
|
||||
*/
|
||||
|
||||
// Former goog.module ID: Blockly.Python.logic
|
||||
|
||||
import type {Block} from '../../core/block.js';
|
||||
import type {PythonGenerator} from './python_generator.js';
|
||||
import {Order} from './python_generator.js';
|
||||
|
||||
export function controls_if(block: Block, generator: PythonGenerator) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
export const controls_ifelse = controls_if;
|
||||
|
||||
export function logic_compare(
|
||||
block: Block,
|
||||
generator: PythonGenerator,
|
||||
): [string, Order] {
|
||||
// Comparison operator.
|
||||
const OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '!=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>=',
|
||||
};
|
||||
type OperatorOption = keyof typeof OPERATORS;
|
||||
const operator = OPERATORS[block.getFieldValue('OP') as OperatorOption];
|
||||
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];
|
||||
}
|
||||
|
||||
export function logic_operation(
|
||||
block: Block,
|
||||
generator: PythonGenerator,
|
||||
): [string, Order] {
|
||||
// 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];
|
||||
}
|
||||
|
||||
export function logic_negate(
|
||||
block: Block,
|
||||
generator: PythonGenerator,
|
||||
): [string, Order] {
|
||||
// Negation.
|
||||
const argument0 =
|
||||
generator.valueToCode(block, 'BOOL', Order.LOGICAL_NOT) || 'True';
|
||||
const code = 'not ' + argument0;
|
||||
return [code, Order.LOGICAL_NOT];
|
||||
}
|
||||
|
||||
export function logic_boolean(
|
||||
block: Block,
|
||||
generator: PythonGenerator,
|
||||
): [string, Order] {
|
||||
// Boolean values true and false.
|
||||
const code = block.getFieldValue('BOOL') === 'TRUE' ? 'True' : 'False';
|
||||
return [code, Order.ATOMIC];
|
||||
}
|
||||
|
||||
export function logic_null(
|
||||
block: Block,
|
||||
generator: PythonGenerator,
|
||||
): [string, Order] {
|
||||
// Null data type.
|
||||
return ['None', Order.ATOMIC];
|
||||
}
|
||||
|
||||
export function logic_ternary(
|
||||
block: Block,
|
||||
generator: PythonGenerator,
|
||||
): [string, Order] {
|
||||
// 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];
|
||||
}
|
||||
Reference in New Issue
Block a user