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:
Christopher Allen
2023-06-14 16:51:15 +01:00
committed by GitHub
parent 26901654ea
commit 2f89c0dd09
10 changed files with 725 additions and 671 deletions

View File

@@ -11,42 +11,42 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Python.colour');
import {pythonGenerator as Python} from '../python.js';
import {pythonGenerator, Order} from '../python.js';
Python.forBlock['colour_picker'] = function(block) {
pythonGenerator.forBlock['colour_picker'] = function(block) {
// Colour picker.
const code = Python.quote_(block.getFieldValue('COLOUR'));
return [code, Python.ORDER_ATOMIC];
const code = pythonGenerator.quote_(block.getFieldValue('COLOUR'));
return [code, Order.ATOMIC];
};
Python.forBlock['colour_random'] = function(block) {
pythonGenerator.forBlock['colour_random'] = function(block) {
// Generate a random colour.
Python.definitions_['import_random'] = 'import random';
pythonGenerator.definitions_['import_random'] = 'import random';
const code = '\'#%06x\' % random.randint(0, 2**24 - 1)';
return [code, Python.ORDER_FUNCTION_CALL];
return [code, Order.FUNCTION_CALL];
};
Python.forBlock['colour_rgb'] = function(block) {
pythonGenerator.forBlock['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
const functionName = Python.provideFunction_('colour_rgb', `
def ${Python.FUNCTION_NAME_PLACEHOLDER_}(r, g, b):
const functionName = pythonGenerator.provideFunction_('colour_rgb', `
def ${pythonGenerator.FUNCTION_NAME_PLACEHOLDER_}(r, g, b):
r = round(min(100, max(0, r)) * 2.55)
g = round(min(100, max(0, g)) * 2.55)
b = round(min(100, max(0, b)) * 2.55)
return '#%02x%02x%02x' % (r, g, b)
`);
const r = Python.valueToCode(block, 'RED', Python.ORDER_NONE) || 0;
const g = Python.valueToCode(block, 'GREEN', Python.ORDER_NONE) || 0;
const b = Python.valueToCode(block, 'BLUE', Python.ORDER_NONE) || 0;
const r = pythonGenerator.valueToCode(block, 'RED', Order.NONE) || 0;
const g = pythonGenerator.valueToCode(block, 'GREEN', Order.NONE) || 0;
const b = pythonGenerator.valueToCode(block, 'BLUE', Order.NONE) || 0;
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
return [code, Python.ORDER_FUNCTION_CALL];
return [code, Order.FUNCTION_CALL];
};
Python.forBlock['colour_blend'] = function(block) {
pythonGenerator.forBlock['colour_blend'] = function(block) {
// Blend two colours together.
const functionName = Python.provideFunction_('colour_blend', `
def ${Python.FUNCTION_NAME_PLACEHOLDER_}(colour1, colour2, ratio):
const functionName = pythonGenerator.provideFunction_('colour_blend', `
def ${pythonGenerator.FUNCTION_NAME_PLACEHOLDER_}(colour1, colour2, ratio):
r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)
g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)
b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)
@@ -57,11 +57,13 @@ def ${Python.FUNCTION_NAME_PLACEHOLDER_}(colour1, colour2, ratio):
return '#%02x%02x%02x' % (r, g, b)
`);
const colour1 =
Python.valueToCode(block, 'COLOUR1', Python.ORDER_NONE) || '\'#000000\'';
pythonGenerator.valueToCode(block, 'COLOUR1', Order.NONE)
|| '\'#000000\'';
const colour2 =
Python.valueToCode(block, 'COLOUR2', Python.ORDER_NONE) || '\'#000000\'';
const ratio = Python.valueToCode(block, 'RATIO', Python.ORDER_NONE) || 0;
pythonGenerator.valueToCode(block, 'COLOUR2', Order.NONE)
|| '\'#000000\'';
const ratio = pythonGenerator.valueToCode(block, 'RATIO', Order.NONE) || 0;
const code =
functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Python.ORDER_FUNCTION_CALL];
return [code, Order.FUNCTION_CALL];
};