Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(generators): Migrate PHP generators to TypeScript #7647

Merged
merged 9 commits into from
Nov 28, 2023
6 changes: 2 additions & 4 deletions generators/javascript/javascript_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ export enum Order {
* JavaScript code generator class.
*/
export class JavascriptGenerator extends CodeGenerator {
/**
* List of outer-inner pairings that do NOT require parentheses.
*/
ORDER_OVERRIDES: number[][] = [
/** List of outer-inner pairings that do NOT require parentheses. */
ORDER_OVERRIDES: [Order, Order][] = [
// (foo()).bar -> foo().bar
// (foo())[0] -> foo()[0]
[Order.FUNCTION_CALL, Order.MEMBER],
Expand Down
2 changes: 1 addition & 1 deletion generators/javascript/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ export function lists_getSublist(
block: Block,
generator: JavascriptGenerator,
): [string, Order] {
// Get sublist.
cpcallen marked this conversation as resolved.
Show resolved Hide resolved
// Dictionary of WHEREn field choices and their CamelCase equivalents.
const wherePascalCase = {
'FIRST': 'First',
Expand All @@ -311,7 +312,6 @@ export function lists_getSublist(
'FROM_END': 'FromEnd',
};
type WhereOption = keyof typeof wherePascalCase;
// Get sublist.
const list = generator.valueToCode(block, 'LIST', Order.MEMBER) || '[]';
const where1 = block.getFieldValue('WHERE1') as WhereOption;
const where2 = block.getFieldValue('WHERE2') as WhereOption;
Expand Down
25 changes: 17 additions & 8 deletions generators/php.js → generators/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

/**
* @fileoverview Complete helper functions for generating PHP for
* blocks. This is the entrypoint for php_compressed.js.
* @suppress {extraRequire}
* @file Instantiate a PhpGenerator and populate it with the complete
* set of block generator functions for PHP. This is the entrypoint
* for php_compressed.js.
*/

// Former goog.module ID: Blockly.PHP.all
Expand All @@ -32,8 +32,17 @@ export * from './php/php_generator.js';
export const phpGenerator = new PhpGenerator();

// Install per-block-type generator functions:
Object.assign(
phpGenerator.forBlock,
colour, lists, logic, loops, math, procedures,
text, variables, variablesDynamic
);
const generators: typeof phpGenerator.forBlock = {
...colour,
...lists,
...logic,
...loops,
...math,
...procedures,
...text,
...variables,
...variablesDynamic,
};
for (const name in generators) {
phpGenerator.forBlock[name] = generators[name];
}
60 changes: 40 additions & 20 deletions generators/php/colour.js → generators/php/colour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,52 @@
*/

/**
* @fileoverview Generating PHP for colour blocks.
* @file Generating PHP for colour blocks.
*/

// Former goog.module ID: Blockly.PHP.colour

import type {Block} from '../../core/block.js';
import {Order} from './php_generator.js';
import type {PhpGenerator} from './php_generator.js';


export function colour_picker(block, generator) {
export function colour_picker(
block: Block,
generator: PhpGenerator,
): [string, Order] {
// Colour picker.
const code = generator.quote_(block.getFieldValue('COLOUR'));
return [code, Order.ATOMIC];
};
}

export function colour_random(block, generator) {
export function colour_random(
block: Block,
generator: PhpGenerator,
): [string, Order] {
// Generate a random colour.
const functionName = generator.provideFunction_('colour_random', `
const functionName = generator.provideFunction_(
'colour_random',
`
function ${generator.FUNCTION_NAME_PLACEHOLDER_}() {
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
`);
`,
);
const code = functionName + '()';
return [code, Order.FUNCTION_CALL];
};
}

export function colour_rgb(block, generator) {
export function colour_rgb(
block: Block,
generator: PhpGenerator,
): [string, Order] {
// Compose a colour from RGB components expressed as percentages.
const red = generator.valueToCode(block, 'RED', Order.NONE) || 0;
const green = generator.valueToCode(block, 'GREEN', Order.NONE) || 0;
const blue = generator.valueToCode(block, 'BLUE', Order.NONE) || 0;
const functionName = generator.provideFunction_('colour_rgb', `
const functionName = generator.provideFunction_(
'colour_rgb',
`
function ${generator.FUNCTION_NAME_PLACEHOLDER_}($r, $g, $b) {
$r = round(max(min($r, 100), 0) * 2.55);
$g = round(max(min($g, 100), 0) * 2.55);
Expand All @@ -46,19 +61,23 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($r, $g, $b) {
$hex .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
return $hex;
}
`);
`,
);
const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
return [code, Order.FUNCTION_CALL];
};
}

export function colour_blend(block, generator) {
export function colour_blend(
block: Block,
generator: PhpGenerator,
): [string, Order] {
// Blend two colours together.
const c1 =
generator.valueToCode(block, 'COLOUR1', Order.NONE) || "'#000000'";
const c2 =
generator.valueToCode(block, 'COLOUR2', Order.NONE) || "'#000000'";
const c1 = generator.valueToCode(block, 'COLOUR1', Order.NONE) || "'#000000'";
const c2 = generator.valueToCode(block, 'COLOUR2', Order.NONE) || "'#000000'";
const ratio = generator.valueToCode(block, 'RATIO', Order.NONE) || 0.5;
const functionName = generator.provideFunction_('colour_blend', `
const functionName = generator.provideFunction_(
'colour_blend',
`
function ${generator.FUNCTION_NAME_PLACEHOLDER_}($c1, $c2, $ratio) {
$ratio = max(min($ratio, 1), 0);
$r1 = hexdec(substr($c1, 1, 2));
Expand All @@ -76,7 +95,8 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($c1, $c2, $ratio) {
$hex .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
return $hex;
}
`);
`,
);
const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
return [code, Order.FUNCTION_CALL];
};
}
Loading