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 Dart generators to TypeScript #7646

Merged
merged 8 commits into from
Nov 14, 2023
12 changes: 9 additions & 3 deletions generators/dart/colour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function colour_picker(block: Block, generator: DartGenerator): [string,

export function colour_random(block: Block, generator: DartGenerator): [string, Order] {
// Generate a random colour.
generator.definitions_['import_dart_math'] =
// TODO(#7600): find better approach than casting to any to override
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
// CodeGenerator declaring .definitions protected.
(generator as AnyDuringMigration).definitions_['import_dart_math'] =
"import 'dart:math' as Math;";
const functionName = generator.provideFunction_('colour_random', `
String ${generator.FUNCTION_NAME_PLACEHOLDER_}() {
Expand All @@ -46,7 +48,9 @@ export function colour_rgb(block: Block, generator: DartGenerator): [string, Ord
const green = generator.valueToCode(block, 'GREEN', Order.NONE) || 0;
const blue = generator.valueToCode(block, 'BLUE', Order.NONE) || 0;

generator.definitions_['import_dart_math'] =
// TODO(#7600): find better approach than casting to any to override
// CodeGenerator declaring .definitions protected.
(generator as AnyDuringMigration).definitions_['import_dart_math'] =
"import 'dart:math' as Math;";
const functionName = generator.provideFunction_('colour_rgb', `
String ${generator.FUNCTION_NAME_PLACEHOLDER_}(num r, num g, num b) {
Expand Down Expand Up @@ -78,7 +82,9 @@ export function colour_blend(block: Block, generator: DartGenerator): [string, O
const ratio =
generator.valueToCode(block, 'RATIO', Order.NONE) || 0.5;

generator.definitions_['import_dart_math'] =
// TODO(#7600): find better approach than casting to any to override
// CodeGenerator declaring .definitions protected.
(generator as AnyDuringMigration).definitions_['import_dart_math'] =
"import 'dart:math' as Math;";
const functionName = generator.provideFunction_('colour_blend', `
String ${generator.FUNCTION_NAME_PLACEHOLDER_}(String c1, String c2, num ratio) {
Expand Down
28 changes: 18 additions & 10 deletions generators/dart/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// Former goog.module ID: Blockly.Dart.lists

import type {Block} from '../../core/block.js';
import type {CreateWithBlock} from '../../blocks/lists.js';
import type {DartGenerator} from './dart_generator.js';
import {NameType} from '../../core/names.js';
import {Order} from './dart_generator.js';
Expand All @@ -25,8 +26,9 @@ export function lists_create_empty(block: Block, generator: DartGenerator): [str

export function lists_create_with(block: Block, generator: DartGenerator): [string, Order] {
// Create a list with any number of elements of any type.
const elements = new Array(block.itemCount_);
for (let i = 0; i < block.itemCount_; i++) {
const createWithBlock = block as CreateWithBlock;
const elements = new Array(createWithBlock.itemCount_);
for (let i = 0; i < createWithBlock.itemCount_; i++) {
elements[i] =
generator.valueToCode(block, 'ADD' + i, Order.NONE) || 'null';
}
Expand Down Expand Up @@ -85,7 +87,7 @@ export function lists_getIndex(block: Block, generator: DartGenerator): [string,
// Closure, which accesses and modifies 'list'.
function cacheList() {
const listVar =
generator.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('tmp_list', NameType.VARIABLE);
const code = 'List ' + listVar + ' = ' + list + ';\n';
list = listVar;
return code;
Expand All @@ -97,12 +99,14 @@ export function lists_getIndex(block: Block, generator: DartGenerator): [string,
!list.match(/^\w+$/)) {
// `list` is an expression, so we may not evaluate it more than once.
if (where === 'RANDOM') {
generator.definitions_['import_dart_math'] =
// TODO(#7600): find better approach than casting to any to override
// CodeGenerator declaring .definitions protected.
(generator as AnyDuringMigration).definitions_['import_dart_math'] =
'import \'dart:math\' as Math;';
// We can use multiple statements.
let code = cacheList();
const xVar =
generator.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('tmp_x', NameType.VARIABLE);
code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
'.length);\n';
code += list + '.removeAt(' + xVar + ');\n';
Expand Down Expand Up @@ -198,12 +202,14 @@ dynamic ${generator.FUNCTION_NAME_PLACEHOLDER_}(List my_list, num x) {
break;
}
case 'RANDOM':
generator.definitions_['import_dart_math'] =
// TODO(#7600): find better approach than casting to any to override
// CodeGenerator declaring .definitions protected.
(generator as AnyDuringMigration).definitions_['import_dart_math'] =
'import \'dart:math\' as Math;';
if (mode === 'REMOVE') {
// We can use multiple statements.
const xVar =
generator.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('tmp_x', NameType.VARIABLE);
let code = 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
'.length);\n';
code += list + '.removeAt(' + xVar + ');\n';
Expand Down Expand Up @@ -251,7 +257,7 @@ export function lists_setIndex(block: Block, generator: DartGenerator) {
return '';
}
const listVar =
generator.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('tmp_list', NameType.VARIABLE);
const code = 'List ' + listVar + ' = ' + list + ';\n';
list = listVar;
return code;
Expand Down Expand Up @@ -297,11 +303,13 @@ export function lists_setIndex(block: Block, generator: DartGenerator) {
break;
}
case 'RANDOM': {
generator.definitions_['import_dart_math'] =
// TODO(#7600): find better approach than casting to any to override
// CodeGenerator declaring .definitions protected.
(generator as AnyDuringMigration).definitions_['import_dart_math'] =
'import \'dart:math\' as Math;';
let code = cacheList();
const xVar =
generator.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('tmp_x', NameType.VARIABLE);
code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
'.length);\n';
if (mode === 'SET') {
Expand Down
3 changes: 2 additions & 1 deletion generators/dart/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export function logic_compare(block: Block, generator: DartGenerator): [string,
// Comparison operator.
const OPERATORS =
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
const operator = OPERATORS[block.getFieldValue('OP')];
type OperatorOption = keyof typeof OPERATORS;
const operator = OPERATORS[block.getFieldValue('OP') as OperatorOption];
const order = (operator === '==' || operator === '!=') ?
Order.EQUALITY :
Order.RELATIONAL;
Expand Down
15 changes: 8 additions & 7 deletions generators/dart/loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import * as stringUtils from '../../core/utils/string.js';
import type {Block} from '../../core/block.js';
import type {ControlFlowInLoopBlock} from '../../blocks/loops.js';
import type {DartGenerator} from './dart_generator.js';
import {NameType} from '../../core/names.js';
import {Order} from './dart_generator.js';
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -32,11 +33,11 @@ export function controls_repeat_ext(block: Block, generator: DartGenerator) {
branch = generator.addLoopTrap(branch, block);
let code = '';
const loopVar =
generator.nameDB_.getDistinctName('count', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('count', NameType.VARIABLE);
let endVar = repeats;
if (!repeats.match(/^\w+$/) && !stringUtils.isNumber(repeats)) {
endVar =
generator.nameDB_.getDistinctName('repeat_end', NameType.VARIABLE);
generator.nameDB_!.getDistinctName('repeat_end', NameType.VARIABLE);
code += 'var ' + endVar + ' = ' + repeats + ';\n';
}
code += 'for (int ' + loopVar + ' = 0; ' + loopVar + ' < ' + endVar + '; ' +
Expand Down Expand Up @@ -93,25 +94,25 @@ export function controls_for(block: Block, generator: DartGenerator) {
let startVar = argument0;
if (!argument0.match(/^\w+$/) && !stringUtils.isNumber(argument0)) {
startVar =
generator.nameDB_.getDistinctName(
generator.nameDB_!.getDistinctName(
variable0 + '_start', NameType.VARIABLE);
code += 'var ' + startVar + ' = ' + argument0 + ';\n';
}
let endVar = argument1;
if (!argument1.match(/^\w+$/) && !stringUtils.isNumber(argument1)) {
endVar =
generator.nameDB_.getDistinctName(
generator.nameDB_!.getDistinctName(
variable0 + '_end', NameType.VARIABLE);
code += 'var ' + endVar + ' = ' + argument1 + ';\n';
}
// Determine loop direction at start, in case one of the bounds
// changes during loop execution.
const incVar =
generator.nameDB_.getDistinctName(
generator.nameDB_!.getDistinctName(
variable0 + '_inc', NameType.VARIABLE);
code += 'num ' + incVar + ' = ';
if (stringUtils.isNumber(increment)) {
code += Math.abs(increment) + ';\n';
code += Math.abs(Number(increment)) + ';\n';
} else {
code += '(' + increment + ').abs();\n';
}
Expand Down Expand Up @@ -152,7 +153,7 @@ export function controls_flow_statements(block: Block, generator: DartGenerator)
xfix += generator.injectId(generator.STATEMENT_SUFFIX, block);
}
if (generator.STATEMENT_PREFIX) {
const loop = block.getSurroundLoop();
const loop = (block as ControlFlowInLoopBlock).getSurroundLoop();
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
// of the loop will not get executed if 'continue' is triggered.
Expand Down
Loading