mirror of
https://github.com/google/blockly.git
synced 2025-12-16 14:20:10 +01:00
refactor: convert some block generators to goog.module (#5762)
* refactor: convert generators/javascript.js to goog.module * refactor: convert generators/javascript.js to named requires * chore: run clang-format * refactor: convert generators/dart/variables.js to goog.module * refactor: convert generators/dart/variables.js to named requires * chore: run clang-format * refactor: convert generators/dart/variables_dynamic.js to goog.module * refactor: convert generators/dart/variables_dynamic.js to named requires * refactor: convert generators/dart/text.js to goog.module * refactor: convert generators/dart/text.js to named requires * chore: run clang-format * refactor: convert generators/dart/procedures.js to goog.module * refactor: convert generators/dart/procedures.js to named requires * chore: run clang-format * refactor: convert generators/dart/math.js to goog.module * refactor: convert generators/dart/math.js to named requires * chore: run clang-format * refactor: convert generators/dart/loops.js to goog.module * refactor: convert generators/dart/loops.js to named requires * chore: run clang-format * refactor: convert generators/dart/logic.js to goog.module * refactor: convert generators/dart/logic.js to named requires * chore: run clang-format * refactor: convert generators/dart/lists.js to goog.module * refactor: convert generators/dart/lists.js to named requires * chore: run clang-format * refactor: convert generators/dart/colour.js to goog.module * refactor: convert generators/dart/colour.js to named requires * chore: rebuild deps.js * chore: lint
This commit is contained in:
@@ -9,26 +9,26 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.colour');
|
goog.module('Blockly.Dart.colour');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart.addReservedWords('Math');
|
Dart.addReservedWords('Math');
|
||||||
|
|
||||||
Blockly.Dart['colour_picker'] = function(block) {
|
Dart['colour_picker'] = function(block) {
|
||||||
// Colour picker.
|
// Colour picker.
|
||||||
const code = Blockly.Dart.quote_(block.getFieldValue('COLOUR'));
|
const code = Dart.quote_(block.getFieldValue('COLOUR'));
|
||||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
return [code, Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['colour_random'] = function(block) {
|
Dart['colour_random'] = function(block) {
|
||||||
// Generate a random colour.
|
// Generate a random colour.
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] =
|
||||||
'import \'dart:math\' as Math;';
|
'import \'dart:math\' as Math;';
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_(
|
||||||
'colour_random',
|
'colour_random',
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
|
['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
|
||||||
' String hex = \'0123456789abcdef\';',
|
' String hex = \'0123456789abcdef\';',
|
||||||
' var rnd = new Math.Random();',
|
' var rnd = new Math.Random();',
|
||||||
' return \'#${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
|
' return \'#${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
|
||||||
@@ -36,23 +36,23 @@ Blockly.Dart['colour_random'] = function(block) {
|
|||||||
' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\';',
|
' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\';',
|
||||||
'}']);
|
'}']);
|
||||||
const code = functionName + '()';
|
const code = functionName + '()';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['colour_rgb'] = function(block) {
|
Dart['colour_rgb'] = function(block) {
|
||||||
// Compose a colour from RGB components expressed as percentages.
|
// Compose a colour from RGB components expressed as percentages.
|
||||||
const red = Blockly.Dart.valueToCode(block, 'RED',
|
const red = Dart.valueToCode(block, 'RED',
|
||||||
Blockly.Dart.ORDER_NONE) || 0;
|
Dart.ORDER_NONE) || 0;
|
||||||
const green = Blockly.Dart.valueToCode(block, 'GREEN',
|
const green = Dart.valueToCode(block, 'GREEN',
|
||||||
Blockly.Dart.ORDER_NONE) || 0;
|
Dart.ORDER_NONE) || 0;
|
||||||
const blue = Blockly.Dart.valueToCode(block, 'BLUE',
|
const blue = Dart.valueToCode(block, 'BLUE',
|
||||||
Blockly.Dart.ORDER_NONE) || 0;
|
Dart.ORDER_NONE) || 0;
|
||||||
|
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] =
|
||||||
'import \'dart:math\' as Math;';
|
'import \'dart:math\' as Math;';
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_(
|
||||||
'colour_rgb',
|
'colour_rgb',
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(num r, num g, num b) {',
|
'(num r, num g, num b) {',
|
||||||
' num rn = (Math.max(Math.min(r, 100), 0) * 2.55).round();',
|
' num rn = (Math.max(Math.min(r, 100), 0) * 2.55).round();',
|
||||||
' String rs = rn.toInt().toRadixString(16);',
|
' String rs = rn.toInt().toRadixString(16);',
|
||||||
@@ -69,23 +69,23 @@ Blockly.Dart['colour_rgb'] = function(block) {
|
|||||||
' return \'#$rs$gs$bs\';',
|
' return \'#$rs$gs$bs\';',
|
||||||
'}']);
|
'}']);
|
||||||
const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
|
const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['colour_blend'] = function(block) {
|
Dart['colour_blend'] = function(block) {
|
||||||
// Blend two colours together.
|
// Blend two colours together.
|
||||||
const c1 = Blockly.Dart.valueToCode(block, 'COLOUR1',
|
const c1 = Dart.valueToCode(block, 'COLOUR1',
|
||||||
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
|
Dart.ORDER_NONE) || '\'#000000\'';
|
||||||
const c2 = Blockly.Dart.valueToCode(block, 'COLOUR2',
|
const c2 = Dart.valueToCode(block, 'COLOUR2',
|
||||||
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
|
Dart.ORDER_NONE) || '\'#000000\'';
|
||||||
const ratio = Blockly.Dart.valueToCode(block, 'RATIO',
|
const ratio = Dart.valueToCode(block, 'RATIO',
|
||||||
Blockly.Dart.ORDER_NONE) || 0.5;
|
Dart.ORDER_NONE) || 0.5;
|
||||||
|
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] =
|
||||||
'import \'dart:math\' as Math;';
|
'import \'dart:math\' as Math;';
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_(
|
||||||
'colour_blend',
|
'colour_blend',
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(String c1, String c2, num ratio) {',
|
'(String c1, String c2, num ratio) {',
|
||||||
' ratio = Math.max(Math.min(ratio, 1), 0);',
|
' ratio = Math.max(Math.min(ratio, 1), 0);',
|
||||||
' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');',
|
' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');',
|
||||||
@@ -109,5 +109,5 @@ Blockly.Dart['colour_blend'] = function(block) {
|
|||||||
' return \'#$rs$gs$bs\';',
|
' return \'#$rs$gs$bs\';',
|
||||||
'}']);
|
'}']);
|
||||||
const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
|
const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,85 +6,81 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for list blocks.
|
* @fileoverview Generating Dart for list blocks.
|
||||||
* @suppress {missingRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.lists');
|
goog.module('Blockly.Dart.lists');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
const {NameType} = goog.require('Blockly.Names');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart.addReservedWords('Math');
|
Dart.addReservedWords('Math');
|
||||||
|
|
||||||
Blockly.Dart['lists_create_empty'] = function(block) {
|
Dart['lists_create_empty'] = function(block) {
|
||||||
// Create an empty list.
|
// Create an empty list.
|
||||||
return ['[]', Blockly.Dart.ORDER_ATOMIC];
|
return ['[]', Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_create_with'] = function(block) {
|
Dart['lists_create_with'] = function(block) {
|
||||||
// Create a list with any number of elements of any type.
|
// Create a list with any number of elements of any type.
|
||||||
const elements = new Array(block.itemCount_);
|
const elements = new Array(block.itemCount_);
|
||||||
for (let i = 0; i < block.itemCount_; i++) {
|
for (let i = 0; i < block.itemCount_; i++) {
|
||||||
elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i,
|
elements[i] = Dart.valueToCode(block, 'ADD' + i, Dart.ORDER_NONE) || 'null';
|
||||||
Blockly.Dart.ORDER_NONE) || 'null';
|
|
||||||
}
|
}
|
||||||
const code = '[' + elements.join(', ') + ']';
|
const code = '[' + elements.join(', ') + ']';
|
||||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
return [code, Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_repeat'] = function(block) {
|
Dart['lists_repeat'] = function(block) {
|
||||||
// Create a list with one element repeated.
|
// Create a list with one element repeated.
|
||||||
const element = Blockly.Dart.valueToCode(block, 'ITEM',
|
const element = Dart.valueToCode(block, 'ITEM', Dart.ORDER_NONE) || 'null';
|
||||||
Blockly.Dart.ORDER_NONE) || 'null';
|
const repeatCount = Dart.valueToCode(block, 'NUM', Dart.ORDER_NONE) || '0';
|
||||||
const repeatCount = Blockly.Dart.valueToCode(block, 'NUM',
|
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
|
||||||
const code = 'new List.filled(' + repeatCount + ', ' + element + ')';
|
const code = 'new List.filled(' + repeatCount + ', ' + element + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_length'] = function(block) {
|
Dart['lists_length'] = function(block) {
|
||||||
// String or array length.
|
// String or array length.
|
||||||
const list = Blockly.Dart.valueToCode(block, 'VALUE',
|
const list =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||||
return [list + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [list + '.length', Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_isEmpty'] = function(block) {
|
Dart['lists_isEmpty'] = function(block) {
|
||||||
// Is the string null or array empty?
|
// Is the string null or array empty?
|
||||||
const list = Blockly.Dart.valueToCode(block, 'VALUE',
|
const list =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||||
return [list + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [list + '.isEmpty', Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_indexOf'] = function(block) {
|
Dart['lists_indexOf'] = function(block) {
|
||||||
// Find an item in the list.
|
// Find an item in the list.
|
||||||
const operator = block.getFieldValue('END') === 'FIRST' ?
|
const operator =
|
||||||
'indexOf' : 'lastIndexOf';
|
block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf';
|
||||||
const item = Blockly.Dart.valueToCode(block, 'FIND',
|
const item = Dart.valueToCode(block, 'FIND', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
const list =
|
||||||
const list = Blockly.Dart.valueToCode(block, 'VALUE',
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
|
||||||
const code = list + '.' + operator + '(' + item + ')';
|
const code = list + '.' + operator + '(' + item + ')';
|
||||||
if (block.workspace.options.oneBasedIndex) {
|
if (block.workspace.options.oneBasedIndex) {
|
||||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
return [code + ' + 1', Dart.ORDER_ADDITIVE];
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_getIndex'] = function(block) {
|
Dart['lists_getIndex'] = function(block) {
|
||||||
// Get element at index.
|
// Get element at index.
|
||||||
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
||||||
const mode = block.getFieldValue('MODE') || 'GET';
|
const mode = block.getFieldValue('MODE') || 'GET';
|
||||||
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||||
const listOrder = (where === 'RANDOM' || where === 'FROM_END') ?
|
const listOrder = (where === 'RANDOM' || where === 'FROM_END') ?
|
||||||
Blockly.Dart.ORDER_NONE : Blockly.Dart.ORDER_UNARY_POSTFIX;
|
Dart.ORDER_NONE :
|
||||||
let list = Blockly.Dart.valueToCode(block, 'VALUE', listOrder) || '[]';
|
Dart.ORDER_UNARY_POSTFIX;
|
||||||
|
let list = Dart.valueToCode(block, 'VALUE', listOrder) || '[]';
|
||||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||||
// Closure, which accesses and modifies 'list'.
|
// Closure, which accesses and modifies 'list'.
|
||||||
function cacheList() {
|
function cacheList() {
|
||||||
const listVar = Blockly.Dart.nameDB_.getDistinctName(
|
const listVar = Dart.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE);
|
||||||
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
const code = 'List ' + listVar + ' = ' + list + ';\n';
|
const code = 'List ' + listVar + ' = ' + list + ';\n';
|
||||||
list = listVar;
|
list = listVar;
|
||||||
return code;
|
return code;
|
||||||
@@ -96,12 +92,10 @@ Blockly.Dart['lists_getIndex'] = function(block) {
|
|||||||
!list.match(/^\w+$/)) {
|
!list.match(/^\w+$/)) {
|
||||||
// `list` is an expression, so we may not evaluate it more than once.
|
// `list` is an expression, so we may not evaluate it more than once.
|
||||||
if (where === 'RANDOM') {
|
if (where === 'RANDOM') {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
// We can use multiple statements.
|
// We can use multiple statements.
|
||||||
let code = cacheList();
|
let code = cacheList();
|
||||||
const xVar = Blockly.Dart.nameDB_.getDistinctName(
|
const xVar = Dart.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE);
|
||||||
'tmp_x', Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
||||||
'.length);\n';
|
'.length);\n';
|
||||||
code += list + '.removeAt(' + xVar + ');\n';
|
code += list + '.removeAt(' + xVar + ');\n';
|
||||||
@@ -109,36 +103,32 @@ Blockly.Dart['lists_getIndex'] = function(block) {
|
|||||||
} else { // where === 'FROM_END'
|
} else { // where === 'FROM_END'
|
||||||
if (mode === 'REMOVE') {
|
if (mode === 'REMOVE') {
|
||||||
// We can use multiple statements.
|
// We can use multiple statements.
|
||||||
const at = Blockly.Dart.getAdjusted(block, 'AT', 1, false,
|
const at = Dart.getAdjusted(block, 'AT', 1, false, Dart.ORDER_ADDITIVE);
|
||||||
Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
let code = cacheList();
|
let code = cacheList();
|
||||||
code += list + '.removeAt(' + list + '.length' + ' - ' + at + ');\n';
|
code += list + '.removeAt(' + list + '.length' +
|
||||||
|
' - ' + at + ');\n';
|
||||||
return code;
|
return code;
|
||||||
|
|
||||||
} else if (mode === 'GET') {
|
} else if (mode === 'GET') {
|
||||||
const at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
const at = Dart.getAdjusted(block, 'AT', 1);
|
||||||
// We need to create a procedure to avoid reevaluating values.
|
// We need to create a procedure to avoid reevaluating values.
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('lists_get_from_end', [
|
||||||
'lists_get_from_end',
|
'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
'(List my_list, num x) {',
|
||||||
'(List my_list, num x) {',
|
' x = my_list.length - x;', ' return my_list[x];', '}'
|
||||||
' x = my_list.length - x;',
|
]);
|
||||||
' return my_list[x];',
|
|
||||||
'}']);
|
|
||||||
const code = functionName + '(' + list + ', ' + at + ')';
|
const code = functionName + '(' + list + ', ' + at + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'GET_REMOVE') {
|
} else if (mode === 'GET_REMOVE') {
|
||||||
const at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
const at = Dart.getAdjusted(block, 'AT', 1);
|
||||||
// We need to create a procedure to avoid reevaluating values.
|
// We need to create a procedure to avoid reevaluating values.
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('lists_remove_from_end', [
|
||||||
'lists_remove_from_end',
|
'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
'(List my_list, num x) {',
|
||||||
'(List my_list, num x) {',
|
' x = my_list.length - x;', ' return my_list.removeAt(x);', '}'
|
||||||
' x = my_list.length - x;',
|
]);
|
||||||
' return my_list.removeAt(x);',
|
|
||||||
'}']);
|
|
||||||
const code = functionName + '(' + list + ', ' + at + ')';
|
const code = functionName + '(' + list + ', ' + at + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -148,10 +138,10 @@ Blockly.Dart['lists_getIndex'] = function(block) {
|
|||||||
case 'FIRST':
|
case 'FIRST':
|
||||||
if (mode === 'GET') {
|
if (mode === 'GET') {
|
||||||
const code = list + '.first';
|
const code = list + '.first';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'GET_REMOVE') {
|
} else if (mode === 'GET_REMOVE') {
|
||||||
const code = list + '.removeAt(0)';
|
const code = list + '.removeAt(0)';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'REMOVE') {
|
} else if (mode === 'REMOVE') {
|
||||||
return list + '.removeAt(0);\n';
|
return list + '.removeAt(0);\n';
|
||||||
}
|
}
|
||||||
@@ -159,37 +149,36 @@ Blockly.Dart['lists_getIndex'] = function(block) {
|
|||||||
case 'LAST':
|
case 'LAST':
|
||||||
if (mode === 'GET') {
|
if (mode === 'GET') {
|
||||||
const code = list + '.last';
|
const code = list + '.last';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'GET_REMOVE') {
|
} else if (mode === 'GET_REMOVE') {
|
||||||
const code = list + '.removeLast()';
|
const code = list + '.removeLast()';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'REMOVE') {
|
} else if (mode === 'REMOVE') {
|
||||||
return list + '.removeLast();\n';
|
return list + '.removeLast();\n';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'FROM_START': {
|
case 'FROM_START': {
|
||||||
const at = Blockly.Dart.getAdjusted(block, 'AT');
|
const at = Dart.getAdjusted(block, 'AT');
|
||||||
if (mode === 'GET') {
|
if (mode === 'GET') {
|
||||||
const code = list + '[' + at + ']';
|
const code = list + '[' + at + ']';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'GET_REMOVE') {
|
} else if (mode === 'GET_REMOVE') {
|
||||||
const code = list + '.removeAt(' + at + ')';
|
const code = list + '.removeAt(' + at + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'REMOVE') {
|
} else if (mode === 'REMOVE') {
|
||||||
return list + '.removeAt(' + at + ');\n';
|
return list + '.removeAt(' + at + ');\n';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'FROM_END': {
|
case 'FROM_END': {
|
||||||
const at = Blockly.Dart.getAdjusted(block, 'AT', 1, false,
|
const at = Dart.getAdjusted(block, 'AT', 1, false, Dart.ORDER_ADDITIVE);
|
||||||
Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
if (mode === 'GET') {
|
if (mode === 'GET') {
|
||||||
const code = list + '[' + list + '.length - ' + at + ']';
|
const code = list + '[' + list + '.length - ' + at + ']';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'GET_REMOVE' || mode === 'REMOVE') {
|
} else if (mode === 'GET_REMOVE' || mode === 'REMOVE') {
|
||||||
const code = list + '.removeAt(' + list + '.length - ' + at + ')';
|
const code = list + '.removeAt(' + list + '.length - ' + at + ')';
|
||||||
if (mode === 'GET_REMOVE') {
|
if (mode === 'GET_REMOVE') {
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'REMOVE') {
|
} else if (mode === 'REMOVE') {
|
||||||
return code + ';\n';
|
return code + ';\n';
|
||||||
}
|
}
|
||||||
@@ -197,36 +186,32 @@ Blockly.Dart['lists_getIndex'] = function(block) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'RANDOM':
|
case 'RANDOM':
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
if (mode === 'REMOVE') {
|
if (mode === 'REMOVE') {
|
||||||
// We can use multiple statements.
|
// We can use multiple statements.
|
||||||
const xVar = Blockly.Dart.nameDB_.getDistinctName(
|
const xVar = Dart.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE);
|
||||||
'tmp_x', Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
let code = 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
let code = 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
||||||
'.length);\n';
|
'.length);\n';
|
||||||
code += list + '.removeAt(' + xVar + ');\n';
|
code += list + '.removeAt(' + xVar + ');\n';
|
||||||
return code;
|
return code;
|
||||||
} else if (mode === 'GET') {
|
} else if (mode === 'GET') {
|
||||||
const functionName =
|
const functionName = Dart.provideFunction_('lists_get_random_item', [
|
||||||
Blockly.Dart.provideFunction_('lists_get_random_item', [
|
'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List my_list) {',
|
||||||
'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' int x = new Math.Random().nextInt(my_list.length);',
|
||||||
'(List my_list) {',
|
' return my_list[x];', '}'
|
||||||
' int x = new Math.Random().nextInt(my_list.length);',
|
]);
|
||||||
' return my_list[x];', '}'
|
|
||||||
]);
|
|
||||||
const code = functionName + '(' + list + ')';
|
const code = functionName + '(' + list + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
} else if (mode === 'GET_REMOVE') {
|
} else if (mode === 'GET_REMOVE') {
|
||||||
const functionName =
|
const functionName =
|
||||||
Blockly.Dart.provideFunction_('lists_remove_random_item', [
|
Dart.provideFunction_('lists_remove_random_item', [
|
||||||
'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(List my_list) {',
|
'(List my_list) {',
|
||||||
' int x = new Math.Random().nextInt(my_list.length);',
|
' int x = new Math.Random().nextInt(my_list.length);',
|
||||||
' return my_list.removeAt(x);', '}'
|
' return my_list.removeAt(x);', '}'
|
||||||
]);
|
]);
|
||||||
const code = functionName + '(' + list + ')';
|
const code = functionName + '(' + list + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -234,23 +219,20 @@ Blockly.Dart['lists_getIndex'] = function(block) {
|
|||||||
throw Error('Unhandled combination (lists_getIndex).');
|
throw Error('Unhandled combination (lists_getIndex).');
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_setIndex'] = function(block) {
|
Dart['lists_setIndex'] = function(block) {
|
||||||
// Set element at index.
|
// Set element at index.
|
||||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||||
const mode = block.getFieldValue('MODE') || 'GET';
|
const mode = block.getFieldValue('MODE') || 'GET';
|
||||||
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||||
let list = Blockly.Dart.valueToCode(block, 'LIST',
|
let list = Dart.valueToCode(block, 'LIST', Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
const value = Dart.valueToCode(block, 'TO', Dart.ORDER_ASSIGNMENT) || 'null';
|
||||||
const value = Blockly.Dart.valueToCode(block, 'TO',
|
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || 'null';
|
|
||||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||||
// Closure, which accesses and modifies 'list'.
|
// Closure, which accesses and modifies 'list'.
|
||||||
function cacheList() {
|
function cacheList() {
|
||||||
if (list.match(/^\w+$/)) {
|
if (list.match(/^\w+$/)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
const listVar = Blockly.Dart.nameDB_.getDistinctName(
|
const listVar = Dart.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE);
|
||||||
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
const code = 'List ' + listVar + ' = ' + list + ';\n';
|
const code = 'List ' + listVar + ' = ' + list + ';\n';
|
||||||
list = listVar;
|
list = listVar;
|
||||||
return code;
|
return code;
|
||||||
@@ -273,7 +255,7 @@ Blockly.Dart['lists_setIndex'] = function(block) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'FROM_START': {
|
case 'FROM_START': {
|
||||||
const at = Blockly.Dart.getAdjusted(block, 'AT');
|
const at = Dart.getAdjusted(block, 'AT');
|
||||||
if (mode === 'SET') {
|
if (mode === 'SET') {
|
||||||
return list + '[' + at + '] = ' + value + ';\n';
|
return list + '[' + at + '] = ' + value + ';\n';
|
||||||
} else if (mode === 'INSERT') {
|
} else if (mode === 'INSERT') {
|
||||||
@@ -282,28 +264,24 @@ Blockly.Dart['lists_setIndex'] = function(block) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'FROM_END': {
|
case 'FROM_END': {
|
||||||
const at = Blockly.Dart.getAdjusted(
|
const at = Dart.getAdjusted(block, 'AT', 1, false, Dart.ORDER_ADDITIVE);
|
||||||
block, 'AT', 1, false, Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
let code = cacheList();
|
let code = cacheList();
|
||||||
if (mode === 'SET') {
|
if (mode === 'SET') {
|
||||||
code += list + '[' + list + '.length - ' + at + '] = ' + value +
|
code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n';
|
||||||
';\n';
|
|
||||||
return code;
|
return code;
|
||||||
} else if (mode === 'INSERT') {
|
} else if (mode === 'INSERT') {
|
||||||
code += list + '.insert(' + list + '.length - ' + at + ', ' +
|
code += list + '.insert(' + list + '.length - ' + at + ', ' + value +
|
||||||
value + ');\n';
|
');\n';
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'RANDOM': {
|
case 'RANDOM': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
let code = cacheList();
|
let code = cacheList();
|
||||||
const xVar = Blockly.Dart.nameDB_.getDistinctName(
|
const xVar = Dart.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE);
|
||||||
'tmp_x', Blockly.VARIABLE_CATEGORY_NAME);
|
code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
||||||
code += 'int ' + xVar +
|
'.length);\n';
|
||||||
' = new Math.Random().nextInt(' + list + '.length);\n';
|
|
||||||
if (mode === 'SET') {
|
if (mode === 'SET') {
|
||||||
code += list + '[' + xVar + '] = ' + value + ';\n';
|
code += list + '[' + xVar + '] = ' + value + ';\n';
|
||||||
return code;
|
return code;
|
||||||
@@ -317,24 +295,24 @@ Blockly.Dart['lists_setIndex'] = function(block) {
|
|||||||
throw Error('Unhandled combination (lists_setIndex).');
|
throw Error('Unhandled combination (lists_setIndex).');
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_getSublist'] = function(block) {
|
Dart['lists_getSublist'] = function(block) {
|
||||||
// Get sublist.
|
// Get sublist.
|
||||||
const list = Blockly.Dart.valueToCode(block, 'LIST',
|
const list =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
Dart.valueToCode(block, 'LIST', Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||||
const where1 = block.getFieldValue('WHERE1');
|
const where1 = block.getFieldValue('WHERE1');
|
||||||
const where2 = block.getFieldValue('WHERE2');
|
const where2 = block.getFieldValue('WHERE2');
|
||||||
let code;
|
let code;
|
||||||
if (list.match(/^\w+$/) || (where1 !== 'FROM_END' && where2 === 'FROM_START')) {
|
if (list.match(/^\w+$/) ||
|
||||||
|
(where1 !== 'FROM_END' && where2 === 'FROM_START')) {
|
||||||
// If the list is a is a variable or doesn't require a call for length,
|
// If the list is a is a variable or doesn't require a call for length,
|
||||||
// don't generate a helper function.
|
// don't generate a helper function.
|
||||||
let at1;
|
let at1;
|
||||||
switch (where1) {
|
switch (where1) {
|
||||||
case 'FROM_START':
|
case 'FROM_START':
|
||||||
at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
at1 = Dart.getAdjusted(block, 'AT1');
|
||||||
break;
|
break;
|
||||||
case 'FROM_END':
|
case 'FROM_END':
|
||||||
at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
|
at1 = Dart.getAdjusted(block, 'AT1', 1, false, Dart.ORDER_ADDITIVE);
|
||||||
Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
at1 = list + '.length - ' + at1;
|
at1 = list + '.length - ' + at1;
|
||||||
break;
|
break;
|
||||||
case 'FIRST':
|
case 'FIRST':
|
||||||
@@ -346,11 +324,10 @@ Blockly.Dart['lists_getSublist'] = function(block) {
|
|||||||
let at2;
|
let at2;
|
||||||
switch (where2) {
|
switch (where2) {
|
||||||
case 'FROM_START':
|
case 'FROM_START':
|
||||||
at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
|
at2 = Dart.getAdjusted(block, 'AT2', 1);
|
||||||
break;
|
break;
|
||||||
case 'FROM_END':
|
case 'FROM_END':
|
||||||
at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
|
at2 = Dart.getAdjusted(block, 'AT2', 0, false, Dart.ORDER_ADDITIVE);
|
||||||
Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
at2 = list + '.length - ' + at2;
|
at2 = list + '.length - ' + at2;
|
||||||
break;
|
break;
|
||||||
case 'LAST':
|
case 'LAST':
|
||||||
@@ -365,68 +342,56 @@ Blockly.Dart['lists_getSublist'] = function(block) {
|
|||||||
code = list + '.sublist(' + at1 + ', ' + at2 + ')';
|
code = list + '.sublist(' + at1 + ', ' + at2 + ')';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
const at1 = Dart.getAdjusted(block, 'AT1');
|
||||||
const at2 = Blockly.Dart.getAdjusted(block, 'AT2');
|
const at2 = Dart.getAdjusted(block, 'AT2');
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('lists_get_sublist', [
|
||||||
'lists_get_sublist',
|
'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
'(List list, String where1, num at1, String where2, num at2) {',
|
||||||
'(List list, String where1, num at1, String where2, num at2) {',
|
' int getAt(String where, num at) {', ' if (where == \'FROM_END\') {',
|
||||||
' int getAt(String where, num at) {',
|
' at = list.length - 1 - at;',
|
||||||
' if (where == \'FROM_END\') {',
|
' } else if (where == \'FIRST\') {', ' at = 0;',
|
||||||
' at = list.length - 1 - at;',
|
' } else if (where == \'LAST\') {', ' at = list.length - 1;',
|
||||||
' } else if (where == \'FIRST\') {',
|
' } else if (where != \'FROM_START\') {',
|
||||||
' at = 0;',
|
' throw \'Unhandled option (lists_getSublist).\';', ' }',
|
||||||
' } else if (where == \'LAST\') {',
|
' return at;', ' }', ' at1 = getAt(where1, at1);',
|
||||||
' at = list.length - 1;',
|
' at2 = getAt(where2, at2) + 1;', ' return list.sublist(at1, at2);', '}'
|
||||||
' } else if (where != \'FROM_START\') {',
|
]);
|
||||||
' throw \'Unhandled option (lists_getSublist).\';',
|
code = functionName + '(' + list + ', \'' + where1 + '\', ' + at1 + ', \'' +
|
||||||
' }',
|
where2 + '\', ' + at2 + ')';
|
||||||
' return at;',
|
|
||||||
' }',
|
|
||||||
' at1 = getAt(where1, at1);',
|
|
||||||
' at2 = getAt(where2, at2) + 1;',
|
|
||||||
' return list.sublist(at1, at2);',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ', \'' + where1 + '\', ' + at1 +
|
|
||||||
', \'' + where2 + '\', ' + at2 + ')';
|
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_sort'] = function(block) {
|
Dart['lists_sort'] = function(block) {
|
||||||
// Block for sorting a list.
|
// Block for sorting a list.
|
||||||
const list = Blockly.Dart.valueToCode(block, 'LIST',
|
const list = Dart.valueToCode(block, 'LIST', Dart.ORDER_NONE) || '[]';
|
||||||
Blockly.Dart.ORDER_NONE) || '[]';
|
|
||||||
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||||
const type = block.getFieldValue('TYPE');
|
const type = block.getFieldValue('TYPE');
|
||||||
const sortFunctionName = Blockly.Dart.provideFunction_(
|
const sortFunctionName = Dart.provideFunction_('lists_sort', [
|
||||||
'lists_sort',
|
'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
'(List list, String type, int direction) {',
|
||||||
'(List list, String type, int direction) {',
|
' var compareFuncs = {',
|
||||||
' var compareFuncs = {',
|
' "NUMERIC": (a, b) => (direction * a.compareTo(b)).toInt(),',
|
||||||
' "NUMERIC": (a, b) => (direction * a.compareTo(b)).toInt(),',
|
' "TEXT": (a, b) => direction * ' +
|
||||||
' "TEXT": (a, b) => direction * ' +
|
'a.toString().compareTo(b.toString()),',
|
||||||
'a.toString().compareTo(b.toString()),',
|
' "IGNORE_CASE": ', ' (a, b) => direction * ',
|
||||||
' "IGNORE_CASE": ',
|
' a.toString().toLowerCase().compareTo(b.toString().toLowerCase())',
|
||||||
' (a, b) => direction * ',
|
' };',
|
||||||
' a.toString().toLowerCase().compareTo(b.toString().toLowerCase())',
|
' list = new List.from(list);', // Clone the list.
|
||||||
' };',
|
' var compare = compareFuncs[type];', ' list.sort(compare);',
|
||||||
' list = new List.from(list);', // Clone the list.
|
' return list;', '}'
|
||||||
' var compare = compareFuncs[type];',
|
]);
|
||||||
' list.sort(compare);',
|
return [
|
||||||
' return list;',
|
sortFunctionName + '(' + list + ', ' +
|
||||||
'}']);
|
'"' + type + '", ' + direction + ')',
|
||||||
return [sortFunctionName + '(' + list + ', ' +
|
Dart.ORDER_UNARY_POSTFIX
|
||||||
'"' + type + '", ' + direction + ')',
|
];
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_split'] = function(block) {
|
Dart['lists_split'] = function(block) {
|
||||||
// Block for splitting text into a list, or joining a list into text.
|
// Block for splitting text into a list, or joining a list into text.
|
||||||
let input = Blockly.Dart.valueToCode(block, 'INPUT',
|
let input = Dart.valueToCode(block, 'INPUT', Dart.ORDER_UNARY_POSTFIX);
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX);
|
const delimiter = Dart.valueToCode(block, 'DELIM', Dart.ORDER_NONE) || '\'\'';
|
||||||
const delimiter = Blockly.Dart.valueToCode(block, 'DELIM',
|
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
|
||||||
const mode = block.getFieldValue('MODE');
|
const mode = block.getFieldValue('MODE');
|
||||||
let functionName;
|
let functionName;
|
||||||
if (mode === 'SPLIT') {
|
if (mode === 'SPLIT') {
|
||||||
@@ -443,14 +408,13 @@ Blockly.Dart['lists_split'] = function(block) {
|
|||||||
throw Error('Unknown mode: ' + mode);
|
throw Error('Unknown mode: ' + mode);
|
||||||
}
|
}
|
||||||
const code = input + '.' + functionName + '(' + delimiter + ')';
|
const code = input + '.' + functionName + '(' + delimiter + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['lists_reverse'] = function(block) {
|
Dart['lists_reverse'] = function(block) {
|
||||||
// Block for reversing a list.
|
// Block for reversing a list.
|
||||||
const list = Blockly.Dart.valueToCode(block, 'LIST',
|
const list = Dart.valueToCode(block, 'LIST', Dart.ORDER_NONE) || '[]';
|
||||||
Blockly.Dart.ORDER_NONE) || '[]';
|
|
||||||
// XXX What should the operator precedence be for a `new`?
|
// XXX What should the operator precedence be for a `new`?
|
||||||
const code = 'new List.from(' + list + '.reversed)';
|
const code = 'new List.from(' + list + '.reversed)';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,73 +9,70 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.logic');
|
goog.module('Blockly.Dart.logic');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart['controls_if'] = function(block) {
|
Dart['controls_if'] = function(block) {
|
||||||
// If/elseif/else condition.
|
// If/elseif/else condition.
|
||||||
let n = 0;
|
let n = 0;
|
||||||
let code = '', branchCode, conditionCode;
|
let code = '', branchCode, conditionCode;
|
||||||
if (Blockly.Dart.STATEMENT_PREFIX) {
|
if (Dart.STATEMENT_PREFIX) {
|
||||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||||
code += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, block);
|
code += Dart.injectId(Dart.STATEMENT_PREFIX, block);
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
conditionCode = Blockly.Dart.valueToCode(block, 'IF' + n,
|
conditionCode =
|
||||||
Blockly.Dart.ORDER_NONE) || 'false';
|
Dart.valueToCode(block, 'IF' + n, Dart.ORDER_NONE) || 'false';
|
||||||
branchCode = Blockly.Dart.statementToCode(block, 'DO' + n);
|
branchCode = Dart.statementToCode(block, 'DO' + n);
|
||||||
if (Blockly.Dart.STATEMENT_SUFFIX) {
|
if (Dart.STATEMENT_SUFFIX) {
|
||||||
branchCode = Blockly.Dart.prefixLines(
|
branchCode =
|
||||||
Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block),
|
Dart.prefixLines(
|
||||||
Blockly.Dart.INDENT) + branchCode;
|
Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT) +
|
||||||
|
branchCode;
|
||||||
}
|
}
|
||||||
code += (n > 0 ? 'else ' : '') +
|
code += (n > 0 ? 'else ' : '') + 'if (' + conditionCode + ') {\n' +
|
||||||
'if (' + conditionCode + ') {\n' + branchCode + '}';
|
branchCode + '}';
|
||||||
n++;
|
n++;
|
||||||
} while (block.getInput('IF' + n));
|
} while (block.getInput('IF' + n));
|
||||||
|
|
||||||
if (block.getInput('ELSE') || Blockly.Dart.STATEMENT_SUFFIX) {
|
if (block.getInput('ELSE') || Dart.STATEMENT_SUFFIX) {
|
||||||
branchCode = Blockly.Dart.statementToCode(block, 'ELSE');
|
branchCode = Dart.statementToCode(block, 'ELSE');
|
||||||
if (Blockly.Dart.STATEMENT_SUFFIX) {
|
if (Dart.STATEMENT_SUFFIX) {
|
||||||
branchCode = Blockly.Dart.prefixLines(
|
branchCode =
|
||||||
Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block),
|
Dart.prefixLines(
|
||||||
Blockly.Dart.INDENT) + branchCode;
|
Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT) +
|
||||||
|
branchCode;
|
||||||
}
|
}
|
||||||
code += ' else {\n' + branchCode + '}';
|
code += ' else {\n' + branchCode + '}';
|
||||||
}
|
}
|
||||||
return code + '\n';
|
return code + '\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['controls_ifelse'] = Blockly.Dart['controls_if'];
|
Dart['controls_ifelse'] = Dart['controls_if'];
|
||||||
|
|
||||||
Blockly.Dart['logic_compare'] = function(block) {
|
Dart['logic_compare'] = function(block) {
|
||||||
// Comparison operator.
|
// Comparison operator.
|
||||||
const OPERATORS = {
|
const OPERATORS =
|
||||||
'EQ': '==',
|
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
||||||
'NEQ': '!=',
|
|
||||||
'LT': '<',
|
|
||||||
'LTE': '<=',
|
|
||||||
'GT': '>',
|
|
||||||
'GTE': '>='
|
|
||||||
};
|
|
||||||
const operator = OPERATORS[block.getFieldValue('OP')];
|
const operator = OPERATORS[block.getFieldValue('OP')];
|
||||||
const order = (operator === '==' || operator === '!=') ?
|
const order = (operator === '==' || operator === '!=') ?
|
||||||
Blockly.Dart.ORDER_EQUALITY : Blockly.Dart.ORDER_RELATIONAL;
|
Dart.ORDER_EQUALITY :
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'A', order) || '0';
|
Dart.ORDER_RELATIONAL;
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'B', order) || '0';
|
const argument0 = Dart.valueToCode(block, 'A', order) || '0';
|
||||||
|
const argument1 = Dart.valueToCode(block, 'B', order) || '0';
|
||||||
const code = argument0 + ' ' + operator + ' ' + argument1;
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['logic_operation'] = function(block) {
|
Dart['logic_operation'] = function(block) {
|
||||||
// Operations 'and', 'or'.
|
// Operations 'and', 'or'.
|
||||||
const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||';
|
const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||';
|
||||||
const order = (operator === '&&') ? Blockly.Dart.ORDER_LOGICAL_AND :
|
const order =
|
||||||
Blockly.Dart.ORDER_LOGICAL_OR;
|
(operator === '&&') ? Dart.ORDER_LOGICAL_AND : Dart.ORDER_LOGICAL_OR;
|
||||||
let argument0 = Blockly.Dart.valueToCode(block, 'A', order);
|
let argument0 = Dart.valueToCode(block, 'A', order);
|
||||||
let argument1 = Blockly.Dart.valueToCode(block, 'B', order);
|
let argument1 = Dart.valueToCode(block, 'B', order);
|
||||||
if (!argument0 && !argument1) {
|
if (!argument0 && !argument1) {
|
||||||
// If there are no arguments, then the return value is false.
|
// If there are no arguments, then the return value is false.
|
||||||
argument0 = 'false';
|
argument0 = 'false';
|
||||||
@@ -94,33 +91,33 @@ Blockly.Dart['logic_operation'] = function(block) {
|
|||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['logic_negate'] = function(block) {
|
Dart['logic_negate'] = function(block) {
|
||||||
// Negation.
|
// Negation.
|
||||||
const order = Blockly.Dart.ORDER_UNARY_PREFIX;
|
const order = Dart.ORDER_UNARY_PREFIX;
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'BOOL', order) || 'true';
|
const argument0 = Dart.valueToCode(block, 'BOOL', order) || 'true';
|
||||||
const code = '!' + argument0;
|
const code = '!' + argument0;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['logic_boolean'] = function(block) {
|
Dart['logic_boolean'] = function(block) {
|
||||||
// Boolean values true and false.
|
// Boolean values true and false.
|
||||||
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
||||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
return [code, Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['logic_null'] = function(block) {
|
Dart['logic_null'] = function(block) {
|
||||||
// Null data type.
|
// Null data type.
|
||||||
return ['null', Blockly.Dart.ORDER_ATOMIC];
|
return ['null', Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['logic_ternary'] = function(block) {
|
Dart['logic_ternary'] = function(block) {
|
||||||
// Ternary operator.
|
// Ternary operator.
|
||||||
const value_if = Blockly.Dart.valueToCode(block, 'IF',
|
const value_if =
|
||||||
Blockly.Dart.ORDER_CONDITIONAL) || 'false';
|
Dart.valueToCode(block, 'IF', Dart.ORDER_CONDITIONAL) || 'false';
|
||||||
const value_then = Blockly.Dart.valueToCode(block, 'THEN',
|
const value_then =
|
||||||
Blockly.Dart.ORDER_CONDITIONAL) || 'null';
|
Dart.valueToCode(block, 'THEN', Dart.ORDER_CONDITIONAL) || 'null';
|
||||||
const value_else = Blockly.Dart.valueToCode(block, 'ELSE',
|
const value_else =
|
||||||
Blockly.Dart.ORDER_CONDITIONAL) || 'null';
|
Dart.valueToCode(block, 'ELSE', Dart.ORDER_CONDITIONAL) || 'null';
|
||||||
const code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
const code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
||||||
return [code, Blockly.Dart.ORDER_CONDITIONAL];
|
return [code, Dart.ORDER_CONDITIONAL];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,17 +6,17 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for loop blocks.
|
* @fileoverview Generating Dart for loop blocks.
|
||||||
* @suppress {missingRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.loops');
|
goog.module('Blockly.Dart.loops');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
goog.require('Blockly.utils.string');
|
const stringUtils = goog.require('Blockly.utils.string');
|
||||||
|
const {NameType} = goog.require('Blockly.Names');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart['controls_repeat_ext'] = function(block) {
|
Dart['controls_repeat_ext'] = function(block) {
|
||||||
let repeats;
|
let repeats;
|
||||||
// Repeat n times.
|
// Repeat n times.
|
||||||
if (block.getField('TIMES')) {
|
if (block.getField('TIMES')) {
|
||||||
@@ -24,63 +24,56 @@ Blockly.Dart['controls_repeat_ext'] = function(block) {
|
|||||||
repeats = String(Number(block.getFieldValue('TIMES')));
|
repeats = String(Number(block.getFieldValue('TIMES')));
|
||||||
} else {
|
} else {
|
||||||
// External number.
|
// External number.
|
||||||
repeats = Blockly.Dart.valueToCode(block, 'TIMES',
|
repeats = Dart.valueToCode(block, 'TIMES', Dart.ORDER_ASSIGNMENT) || '0';
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
|
||||||
}
|
}
|
||||||
let branch = Blockly.Dart.statementToCode(block, 'DO');
|
let branch = Dart.statementToCode(block, 'DO');
|
||||||
branch = Blockly.Dart.addLoopTrap(branch, block);
|
branch = Dart.addLoopTrap(branch, block);
|
||||||
let code = '';
|
let code = '';
|
||||||
const loopVar = Blockly.Dart.nameDB_.getDistinctName(
|
const loopVar = Dart.nameDB_.getDistinctName('count', NameType.VARIABLE);
|
||||||
'count', Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
let endVar = repeats;
|
let endVar = repeats;
|
||||||
if (!repeats.match(/^\w+$/) && !Blockly.utils.string.isNumber(repeats)) {
|
if (!repeats.match(/^\w+$/) && !stringUtils.isNumber(repeats)) {
|
||||||
endVar = Blockly.Dart.nameDB_.getDistinctName(
|
endVar = Dart.nameDB_.getDistinctName('repeat_end', NameType.VARIABLE);
|
||||||
'repeat_end', Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
code += 'var ' + endVar + ' = ' + repeats + ';\n';
|
code += 'var ' + endVar + ' = ' + repeats + ';\n';
|
||||||
}
|
}
|
||||||
code += 'for (int ' + loopVar + ' = 0; ' +
|
code += 'for (int ' + loopVar + ' = 0; ' + loopVar + ' < ' + endVar + '; ' +
|
||||||
loopVar + ' < ' + endVar + '; ' +
|
loopVar + '++) {\n' + branch + '}\n';
|
||||||
loopVar + '++) {\n' +
|
|
||||||
branch + '}\n';
|
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['controls_repeat'] = Blockly.Dart['controls_repeat_ext'];
|
Dart['controls_repeat'] = Dart['controls_repeat_ext'];
|
||||||
|
|
||||||
Blockly.Dart['controls_whileUntil'] = function(block) {
|
Dart['controls_whileUntil'] = function(block) {
|
||||||
// Do while/until loop.
|
// Do while/until loop.
|
||||||
const until = block.getFieldValue('MODE') === 'UNTIL';
|
const until = block.getFieldValue('MODE') === 'UNTIL';
|
||||||
let argument0 = Blockly.Dart.valueToCode(block, 'BOOL',
|
let argument0 =
|
||||||
until ? Blockly.Dart.ORDER_UNARY_PREFIX :
|
Dart.valueToCode(
|
||||||
Blockly.Dart.ORDER_NONE) || 'false';
|
block, 'BOOL', until ? Dart.ORDER_UNARY_PREFIX : Dart.ORDER_NONE) ||
|
||||||
let branch = Blockly.Dart.statementToCode(block, 'DO');
|
'false';
|
||||||
branch = Blockly.Dart.addLoopTrap(branch, block);
|
let branch = Dart.statementToCode(block, 'DO');
|
||||||
|
branch = Dart.addLoopTrap(branch, block);
|
||||||
if (until) {
|
if (until) {
|
||||||
argument0 = '!' + argument0;
|
argument0 = '!' + argument0;
|
||||||
}
|
}
|
||||||
return 'while (' + argument0 + ') {\n' + branch + '}\n';
|
return 'while (' + argument0 + ') {\n' + branch + '}\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['controls_for'] = function(block) {
|
Dart['controls_for'] = function(block) {
|
||||||
// For loop.
|
// For loop.
|
||||||
const variable0 = Blockly.Dart.nameDB_.getName(
|
const variable0 =
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'FROM',
|
const argument0 =
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
Dart.valueToCode(block, 'FROM', Dart.ORDER_ASSIGNMENT) || '0';
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'TO',
|
const argument1 = Dart.valueToCode(block, 'TO', Dart.ORDER_ASSIGNMENT) || '0';
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
const increment = Dart.valueToCode(block, 'BY', Dart.ORDER_ASSIGNMENT) || '1';
|
||||||
const increment = Blockly.Dart.valueToCode(block, 'BY',
|
let branch = Dart.statementToCode(block, 'DO');
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || '1';
|
branch = Dart.addLoopTrap(branch, block);
|
||||||
let branch = Blockly.Dart.statementToCode(block, 'DO');
|
|
||||||
branch = Blockly.Dart.addLoopTrap(branch, block);
|
|
||||||
let code;
|
let code;
|
||||||
if (Blockly.utils.string.isNumber(argument0) && Blockly.utils.string.isNumber(argument1) &&
|
if (stringUtils.isNumber(argument0) && stringUtils.isNumber(argument1) &&
|
||||||
Blockly.utils.string.isNumber(increment)) {
|
stringUtils.isNumber(increment)) {
|
||||||
// All arguments are simple numbers.
|
// All arguments are simple numbers.
|
||||||
const up = Number(argument0) <= Number(argument1);
|
const up = Number(argument0) <= Number(argument1);
|
||||||
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
|
code = 'for (' + variable0 + ' = ' + argument0 + '; ' + variable0 +
|
||||||
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
|
(up ? ' <= ' : ' >= ') + argument1 + '; ' + variable0;
|
||||||
variable0;
|
|
||||||
const step = Math.abs(Number(increment));
|
const step = Math.abs(Number(increment));
|
||||||
if (step === 1) {
|
if (step === 1) {
|
||||||
code += up ? '++' : '--';
|
code += up ? '++' : '--';
|
||||||
@@ -92,72 +85,70 @@ Blockly.Dart['controls_for'] = function(block) {
|
|||||||
code = '';
|
code = '';
|
||||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||||
let startVar = argument0;
|
let startVar = argument0;
|
||||||
if (!argument0.match(/^\w+$/) && !Blockly.utils.string.isNumber(argument0)) {
|
if (!argument0.match(/^\w+$/) && !stringUtils.isNumber(argument0)) {
|
||||||
startVar = Blockly.Dart.nameDB_.getDistinctName(
|
startVar =
|
||||||
variable0 + '_start', Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getDistinctName(variable0 + '_start', NameType.VARIABLE);
|
||||||
code += 'var ' + startVar + ' = ' + argument0 + ';\n';
|
code += 'var ' + startVar + ' = ' + argument0 + ';\n';
|
||||||
}
|
}
|
||||||
let endVar = argument1;
|
let endVar = argument1;
|
||||||
if (!argument1.match(/^\w+$/) && !Blockly.utils.string.isNumber(argument1)) {
|
if (!argument1.match(/^\w+$/) && !stringUtils.isNumber(argument1)) {
|
||||||
endVar = Blockly.Dart.nameDB_.getDistinctName(
|
endVar =
|
||||||
variable0 + '_end', Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getDistinctName(variable0 + '_end', NameType.VARIABLE);
|
||||||
code += 'var ' + endVar + ' = ' + argument1 + ';\n';
|
code += 'var ' + endVar + ' = ' + argument1 + ';\n';
|
||||||
}
|
}
|
||||||
// Determine loop direction at start, in case one of the bounds
|
// Determine loop direction at start, in case one of the bounds
|
||||||
// changes during loop execution.
|
// changes during loop execution.
|
||||||
const incVar = Blockly.Dart.nameDB_.getDistinctName(
|
const incVar =
|
||||||
variable0 + '_inc', Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getDistinctName(variable0 + '_inc', NameType.VARIABLE);
|
||||||
code += 'num ' + incVar + ' = ';
|
code += 'num ' + incVar + ' = ';
|
||||||
if (Blockly.utils.string.isNumber(increment)) {
|
if (stringUtils.isNumber(increment)) {
|
||||||
code += Math.abs(increment) + ';\n';
|
code += Math.abs(increment) + ';\n';
|
||||||
} else {
|
} else {
|
||||||
code += '(' + increment + ').abs();\n';
|
code += '(' + increment + ').abs();\n';
|
||||||
}
|
}
|
||||||
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
|
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
|
||||||
code += Blockly.Dart.INDENT + incVar + ' = -' + incVar + ';\n';
|
code += Dart.INDENT + incVar + ' = -' + incVar + ';\n';
|
||||||
code += '}\n';
|
code += '}\n';
|
||||||
code += 'for (' + variable0 + ' = ' + startVar + '; ' +
|
code += 'for (' + variable0 + ' = ' + startVar + '; ' + incVar +
|
||||||
incVar + ' >= 0 ? ' +
|
' >= 0 ? ' + variable0 + ' <= ' + endVar + ' : ' + variable0 +
|
||||||
variable0 + ' <= ' + endVar + ' : ' +
|
' >= ' + endVar + '; ' + variable0 + ' += ' + incVar + ') {\n' +
|
||||||
variable0 + ' >= ' + endVar + '; ' +
|
|
||||||
variable0 + ' += ' + incVar + ') {\n' +
|
|
||||||
branch + '}\n';
|
branch + '}\n';
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['controls_forEach'] = function(block) {
|
Dart['controls_forEach'] = function(block) {
|
||||||
// For each loop.
|
// For each loop.
|
||||||
const variable0 = Blockly.Dart.nameDB_.getName(
|
const variable0 =
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'LIST',
|
const argument0 =
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || '[]';
|
Dart.valueToCode(block, 'LIST', Dart.ORDER_ASSIGNMENT) || '[]';
|
||||||
let branch = Blockly.Dart.statementToCode(block, 'DO');
|
let branch = Dart.statementToCode(block, 'DO');
|
||||||
branch = Blockly.Dart.addLoopTrap(branch, block);
|
branch = Dart.addLoopTrap(branch, block);
|
||||||
const code = 'for (var ' + variable0 + ' in ' + argument0 + ') {\n' +
|
const code =
|
||||||
branch + '}\n';
|
'for (var ' + variable0 + ' in ' + argument0 + ') {\n' + branch + '}\n';
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['controls_flow_statements'] = function(block) {
|
Dart['controls_flow_statements'] = function(block) {
|
||||||
// Flow statements: continue, break.
|
// Flow statements: continue, break.
|
||||||
let xfix = '';
|
let xfix = '';
|
||||||
if (Blockly.Dart.STATEMENT_PREFIX) {
|
if (Dart.STATEMENT_PREFIX) {
|
||||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||||
xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, block);
|
xfix += Dart.injectId(Dart.STATEMENT_PREFIX, block);
|
||||||
}
|
}
|
||||||
if (Blockly.Dart.STATEMENT_SUFFIX) {
|
if (Dart.STATEMENT_SUFFIX) {
|
||||||
// Inject any statement suffix here since the regular one at the end
|
// Inject any statement suffix here since the regular one at the end
|
||||||
// will not get executed if the break/continue is triggered.
|
// will not get executed if the break/continue is triggered.
|
||||||
xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block);
|
xfix += Dart.injectId(Dart.STATEMENT_SUFFIX, block);
|
||||||
}
|
}
|
||||||
if (Blockly.Dart.STATEMENT_PREFIX) {
|
if (Dart.STATEMENT_PREFIX) {
|
||||||
const loop = block.getSurroundLoop();
|
const loop = block.getSurroundLoop();
|
||||||
if (loop && !loop.suppressPrefixSuffix) {
|
if (loop && !loop.suppressPrefixSuffix) {
|
||||||
// Inject loop's statement prefix here since the regular one at the end
|
// Inject loop's statement prefix here since the regular one at the end
|
||||||
// of the loop will not get executed if 'continue' is triggered.
|
// of the loop will not get executed if 'continue' is triggered.
|
||||||
// In the case of 'break', a prefix is needed due to the loop's suffix.
|
// In the case of 'break', a prefix is needed due to the loop's suffix.
|
||||||
xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, loop);
|
xfix += Dart.injectId(Dart.STATEMENT_PREFIX, loop);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (block.getFieldValue('FLOW')) {
|
switch (block.getFieldValue('FLOW')) {
|
||||||
|
|||||||
@@ -6,89 +6,82 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for math blocks.
|
* @fileoverview Generating Dart for math blocks.
|
||||||
* @suppress {missingRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.math');
|
goog.module('Blockly.Dart.math');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
const {NameType} = goog.require('Blockly.Names');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart.addReservedWords('Math');
|
Dart.addReservedWords('Math');
|
||||||
|
|
||||||
Blockly.Dart['math_number'] = function(block) {
|
Dart['math_number'] = function(block) {
|
||||||
// Numeric value.
|
// Numeric value.
|
||||||
let code = Number(block.getFieldValue('NUM'));
|
let code = Number(block.getFieldValue('NUM'));
|
||||||
let order;
|
let order;
|
||||||
if (code === Infinity) {
|
if (code === Infinity) {
|
||||||
code = 'double.infinity';
|
code = 'double.infinity';
|
||||||
order = Blockly.Dart.ORDER_UNARY_POSTFIX;
|
order = Dart.ORDER_UNARY_POSTFIX;
|
||||||
} else if (code === -Infinity) {
|
} else if (code === -Infinity) {
|
||||||
code = '-double.infinity';
|
code = '-double.infinity';
|
||||||
order = Blockly.Dart.ORDER_UNARY_PREFIX;
|
order = Dart.ORDER_UNARY_PREFIX;
|
||||||
} else {
|
} else {
|
||||||
// -4.abs() returns -4 in Dart due to strange order of operation choices.
|
// -4.abs() returns -4 in Dart due to strange order of operation choices.
|
||||||
// -4 is actually an operator and a number. Reflect this in the order.
|
// -4 is actually an operator and a number. Reflect this in the order.
|
||||||
order = code < 0 ?
|
order = code < 0 ? Dart.ORDER_UNARY_PREFIX : Dart.ORDER_ATOMIC;
|
||||||
Blockly.Dart.ORDER_UNARY_PREFIX : Blockly.Dart.ORDER_ATOMIC;
|
|
||||||
}
|
}
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_arithmetic'] = function(block) {
|
Dart['math_arithmetic'] = function(block) {
|
||||||
// Basic arithmetic operators, and power.
|
// Basic arithmetic operators, and power.
|
||||||
const OPERATORS = {
|
const OPERATORS = {
|
||||||
'ADD': [' + ', Blockly.Dart.ORDER_ADDITIVE],
|
'ADD': [' + ', Dart.ORDER_ADDITIVE],
|
||||||
'MINUS': [' - ', Blockly.Dart.ORDER_ADDITIVE],
|
'MINUS': [' - ', Dart.ORDER_ADDITIVE],
|
||||||
'MULTIPLY': [' * ', Blockly.Dart.ORDER_MULTIPLICATIVE],
|
'MULTIPLY': [' * ', Dart.ORDER_MULTIPLICATIVE],
|
||||||
'DIVIDE': [' / ', Blockly.Dart.ORDER_MULTIPLICATIVE],
|
'DIVIDE': [' / ', Dart.ORDER_MULTIPLICATIVE],
|
||||||
'POWER': [null, Blockly.Dart.ORDER_NONE] // Handle power separately.
|
'POWER': [null, Dart.ORDER_NONE] // Handle power separately.
|
||||||
};
|
};
|
||||||
const tuple = OPERATORS[block.getFieldValue('OP')];
|
const tuple = OPERATORS[block.getFieldValue('OP')];
|
||||||
const operator = tuple[0];
|
const operator = tuple[0];
|
||||||
const order = tuple[1];
|
const order = tuple[1];
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'A', order) || '0';
|
const argument0 = Dart.valueToCode(block, 'A', order) || '0';
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'B', order) || '0';
|
const argument1 = Dart.valueToCode(block, 'B', order) || '0';
|
||||||
let code;
|
let code;
|
||||||
// Power in Dart requires a special case since it has no operator.
|
// Power in Dart requires a special case since it has no operator.
|
||||||
if (!operator) {
|
if (!operator) {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
code = 'Math.pow(' + argument0 + ', ' + argument1 + ')';
|
code = 'Math.pow(' + argument0 + ', ' + argument1 + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
code = argument0 + operator + argument1;
|
code = argument0 + operator + argument1;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_single'] = function(block) {
|
Dart['math_single'] = function(block) {
|
||||||
// Math operators with single operand.
|
// Math operators with single operand.
|
||||||
const operator = block.getFieldValue('OP');
|
const operator = block.getFieldValue('OP');
|
||||||
let code;
|
let code;
|
||||||
let arg;
|
let arg;
|
||||||
if (operator === 'NEG') {
|
if (operator === 'NEG') {
|
||||||
// Negation is a special case given its different operator precedence.
|
// Negation is a special case given its different operator precedence.
|
||||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_UNARY_PREFIX) || '0';
|
||||||
Blockly.Dart.ORDER_UNARY_PREFIX) || '0';
|
|
||||||
if (arg[0] === '-') {
|
if (arg[0] === '-') {
|
||||||
// --3 is not legal in Dart.
|
// --3 is not legal in Dart.
|
||||||
arg = ' ' + arg;
|
arg = ' ' + arg;
|
||||||
}
|
}
|
||||||
code = '-' + arg;
|
code = '-' + arg;
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_PREFIX];
|
return [code, Dart.ORDER_UNARY_PREFIX];
|
||||||
}
|
}
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
if (operator === 'ABS' || operator.substring(0, 5) === 'ROUND') {
|
if (operator === 'ABS' || operator.substring(0, 5) === 'ROUND') {
|
||||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_UNARY_POSTFIX) || '0';
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '0';
|
|
||||||
} else if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') {
|
} else if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') {
|
||||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
|
||||||
} else {
|
} else {
|
||||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
arg = Dart.valueToCode(block, 'NUM', Dart.ORDER_NONE) || '0';
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
|
||||||
}
|
}
|
||||||
// First, handle cases which generate values that don't need parentheses
|
// First, handle cases which generate values that don't need parentheses
|
||||||
// wrapping the code.
|
// wrapping the code.
|
||||||
@@ -128,7 +121,7 @@ Blockly.Dart['math_single'] = function(block) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (code) {
|
if (code) {
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
// Second, handle cases which generate values that may need parentheses
|
// Second, handle cases which generate values that may need parentheses
|
||||||
// wrapping the code.
|
// wrapping the code.
|
||||||
@@ -148,65 +141,55 @@ Blockly.Dart['math_single'] = function(block) {
|
|||||||
default:
|
default:
|
||||||
throw Error('Unknown math operator: ' + operator);
|
throw Error('Unknown math operator: ' + operator);
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_MULTIPLICATIVE];
|
return [code, Dart.ORDER_MULTIPLICATIVE];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_constant'] = function(block) {
|
Dart['math_constant'] = function(block) {
|
||||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||||
const CONSTANTS = {
|
const CONSTANTS = {
|
||||||
'PI': ['Math.pi', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
'PI': ['Math.pi', Dart.ORDER_UNARY_POSTFIX],
|
||||||
'E': ['Math.e', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
'E': ['Math.e', Dart.ORDER_UNARY_POSTFIX],
|
||||||
'GOLDEN_RATIO':
|
'GOLDEN_RATIO': ['(1 + Math.sqrt(5)) / 2', Dart.ORDER_MULTIPLICATIVE],
|
||||||
['(1 + Math.sqrt(5)) / 2', Blockly.Dart.ORDER_MULTIPLICATIVE],
|
'SQRT2': ['Math.sqrt2', Dart.ORDER_UNARY_POSTFIX],
|
||||||
'SQRT2': ['Math.sqrt2', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
'SQRT1_2': ['Math.sqrt1_2', Dart.ORDER_UNARY_POSTFIX],
|
||||||
'SQRT1_2': ['Math.sqrt1_2', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
'INFINITY': ['double.infinity', Dart.ORDER_ATOMIC]
|
||||||
'INFINITY': ['double.infinity', Blockly.Dart.ORDER_ATOMIC]
|
|
||||||
};
|
};
|
||||||
const constant = block.getFieldValue('CONSTANT');
|
const constant = block.getFieldValue('CONSTANT');
|
||||||
if (constant !== 'INFINITY') {
|
if (constant !== 'INFINITY') {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
}
|
}
|
||||||
return CONSTANTS[constant];
|
return CONSTANTS[constant];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_number_property'] = function(block) {
|
Dart['math_number_property'] = function(block) {
|
||||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||||
// or if it is divisible by certain number. Returns true or false.
|
// or if it is divisible by certain number. Returns true or false.
|
||||||
const number_to_check = Blockly.Dart.valueToCode(block, 'NUMBER_TO_CHECK',
|
const number_to_check =
|
||||||
Blockly.Dart.ORDER_MULTIPLICATIVE);
|
Dart.valueToCode(block, 'NUMBER_TO_CHECK', Dart.ORDER_MULTIPLICATIVE);
|
||||||
if (!number_to_check) {
|
if (!number_to_check) {
|
||||||
return ['false', Blockly.Dart.ORDER_ATOMIC];
|
return ['false', Dart.ORDER_ATOMIC];
|
||||||
}
|
}
|
||||||
const dropdown_property = block.getFieldValue('PROPERTY');
|
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||||
let code;
|
let code;
|
||||||
if (dropdown_property === 'PRIME') {
|
if (dropdown_property === 'PRIME') {
|
||||||
// Prime is a special case as it is not a one-liner test.
|
// Prime is a special case as it is not a one-liner test.
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const functionName = Dart.provideFunction_('math_isPrime', [
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
'bool ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
||||||
'math_isPrime',
|
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||||
['bool ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
' if (n == 2 || n == 3) {', ' return true;', ' }',
|
||||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
' // False if n is null, negative, is 1, or not whole.',
|
||||||
' if (n == 2 || n == 3) {',
|
' // And false if n is divisible by 2 or 3.',
|
||||||
' return true;',
|
' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||||
' }',
|
' n % 3 == 0) {',
|
||||||
' // False if n is null, negative, is 1, or not whole.',
|
' return false;', ' }',
|
||||||
' // And false if n is divisible by 2 or 3.',
|
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||||
' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
||||||
' n % 3 == 0) {',
|
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {', ' return false;',
|
||||||
' return false;',
|
' }', ' }', ' return true;', '}'
|
||||||
' }',
|
]);
|
||||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
|
||||||
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
|
||||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
|
|
||||||
' return false;',
|
|
||||||
' }',
|
|
||||||
' }',
|
|
||||||
' return true;',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + number_to_check + ')';
|
code = functionName + '(' + number_to_check + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
switch (dropdown_property) {
|
switch (dropdown_property) {
|
||||||
case 'EVEN':
|
case 'EVEN':
|
||||||
@@ -225,269 +208,227 @@ Blockly.Dart['math_number_property'] = function(block) {
|
|||||||
code = number_to_check + ' < 0';
|
code = number_to_check + ' < 0';
|
||||||
break;
|
break;
|
||||||
case 'DIVISIBLE_BY':
|
case 'DIVISIBLE_BY':
|
||||||
const divisor = Blockly.Dart.valueToCode(block, 'DIVISOR',
|
const divisor =
|
||||||
Blockly.Dart.ORDER_MULTIPLICATIVE);
|
Dart.valueToCode(block, 'DIVISOR', Dart.ORDER_MULTIPLICATIVE);
|
||||||
if (!divisor) {
|
if (!divisor) {
|
||||||
return ['false', Blockly.Dart.ORDER_ATOMIC];
|
return ['false', Dart.ORDER_ATOMIC];
|
||||||
}
|
}
|
||||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_EQUALITY];
|
return [code, Dart.ORDER_EQUALITY];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_change'] = function(block) {
|
Dart['math_change'] = function(block) {
|
||||||
// Add to a variable in place.
|
// Add to a variable in place.
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'DELTA',
|
const argument0 =
|
||||||
Blockly.Dart.ORDER_ADDITIVE) || '0';
|
Dart.valueToCode(block, 'DELTA', Dart.ORDER_ADDITIVE) || '0';
|
||||||
const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'),
|
const varName =
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||||
return varName + ' = (' + varName + ' is num ? ' + varName + ' : 0) + ' +
|
return varName + ' = (' + varName + ' is num ? ' + varName + ' : 0) + ' +
|
||||||
argument0 + ';\n';
|
argument0 + ';\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Rounding functions have a single operand.
|
// Rounding functions have a single operand.
|
||||||
Blockly.Dart['math_round'] = Blockly.Dart['math_single'];
|
Dart['math_round'] = Dart['math_single'];
|
||||||
// Trigonometry functions have a single operand.
|
// Trigonometry functions have a single operand.
|
||||||
Blockly.Dart['math_trig'] = Blockly.Dart['math_single'];
|
Dart['math_trig'] = Dart['math_single'];
|
||||||
|
|
||||||
Blockly.Dart['math_on_list'] = function(block) {
|
Dart['math_on_list'] = function(block) {
|
||||||
// Math functions for lists.
|
// Math functions for lists.
|
||||||
const func = block.getFieldValue('OP');
|
const func = block.getFieldValue('OP');
|
||||||
const list = Blockly.Dart.valueToCode(block, 'LIST',
|
const list = Dart.valueToCode(block, 'LIST', Dart.ORDER_NONE) || '[]';
|
||||||
Blockly.Dart.ORDER_NONE) || '[]';
|
|
||||||
let code;
|
let code;
|
||||||
switch (func) {
|
switch (func) {
|
||||||
case 'SUM': {
|
case 'SUM': {
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('math_sum', [
|
||||||
'math_sum',
|
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List<num> myList) {',
|
||||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' num sumVal = 0;',
|
||||||
'(List<num> myList) {',
|
' myList.forEach((num entry) {sumVal += entry;});', ' return sumVal;',
|
||||||
' num sumVal = 0;',
|
'}'
|
||||||
' myList.forEach((num entry) {sumVal += entry;});',
|
]);
|
||||||
' return sumVal;',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'MIN': {
|
case 'MIN': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const functionName = Dart.provideFunction_('math_min', [
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List<num> myList) {',
|
||||||
'math_min',
|
' if (myList.isEmpty) return null;', ' num minVal = myList[0];',
|
||||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' myList.forEach((num entry) ' +
|
||||||
'(List<num> myList) {',
|
'{minVal = Math.min(minVal, entry);});',
|
||||||
' if (myList.isEmpty) return null;',
|
' return minVal;', '}'
|
||||||
' num minVal = myList[0];',
|
]);
|
||||||
' myList.forEach((num entry) ' +
|
|
||||||
'{minVal = Math.min(minVal, entry);});',
|
|
||||||
' return minVal;',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'MAX': {
|
case 'MAX': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const functionName = Dart.provideFunction_('math_max', [
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List<num> myList) {',
|
||||||
'math_max',
|
' if (myList.isEmpty) return null;', ' num maxVal = myList[0];',
|
||||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' myList.forEach((num entry) ' +
|
||||||
'(List<num> myList) {',
|
'{maxVal = Math.max(maxVal, entry);});',
|
||||||
' if (myList.isEmpty) return null;',
|
' return maxVal;', '}'
|
||||||
' num maxVal = myList[0];',
|
]);
|
||||||
' myList.forEach((num entry) ' +
|
|
||||||
'{maxVal = Math.max(maxVal, entry);});',
|
|
||||||
' return maxVal;',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'AVERAGE': {
|
case 'AVERAGE': {
|
||||||
// This operation exclude null and values that are not int or float:
|
// This operation exclude null and values that are not int or float:
|
||||||
// math_mean([null,null,"aString",1,9]) -> 5.0
|
// math_mean([null,null,"aString",1,9]) -> 5.0
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('math_mean', [
|
||||||
'math_mean',
|
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' // First filter list for numbers only.',
|
||||||
'(List myList) {',
|
' List localList = new List.from(myList);',
|
||||||
' // First filter list for numbers only.',
|
' localList.removeWhere((a) => a is! num);',
|
||||||
' List localList = new List.from(myList);',
|
' if (localList.isEmpty) return null;', ' num sumVal = 0;',
|
||||||
' localList.removeWhere((a) => a is! num);',
|
' localList.forEach((var entry) {sumVal += entry;});',
|
||||||
' if (localList.isEmpty) return null;',
|
' return sumVal / localList.length;', '}'
|
||||||
' num sumVal = 0;',
|
]);
|
||||||
' localList.forEach((var entry) {sumVal += entry;});',
|
|
||||||
' return sumVal / localList.length;',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'MEDIAN': {
|
case 'MEDIAN': {
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('math_median', [
|
||||||
'math_median',
|
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' // First filter list for numbers only, then sort, ' +
|
||||||
'(List myList) {',
|
'then return middle value',
|
||||||
' // First filter list for numbers only, then sort, ' +
|
' // or the average of two middle values if list has an ' +
|
||||||
'then return middle value',
|
'even number of elements.',
|
||||||
' // or the average of two middle values if list has an ' +
|
' List localList = new List.from(myList);',
|
||||||
'even number of elements.',
|
' localList.removeWhere((a) => a is! num);',
|
||||||
' List localList = new List.from(myList);',
|
' if (localList.isEmpty) return null;',
|
||||||
' localList.removeWhere((a) => a is! num);',
|
' localList.sort((a, b) => (a - b));',
|
||||||
' if (localList.isEmpty) return null;',
|
' int index = localList.length ~/ 2;',
|
||||||
' localList.sort((a, b) => (a - b));',
|
' if (localList.length % 2 == 1) {', ' return localList[index];',
|
||||||
' int index = localList.length ~/ 2;',
|
' } else {',
|
||||||
' if (localList.length % 2 == 1) {',
|
' return (localList[index - 1] + localList[index]) / 2;', ' }', '}'
|
||||||
' return localList[index];',
|
]);
|
||||||
' } else {',
|
|
||||||
' return (localList[index - 1] + localList[index]) / 2;',
|
|
||||||
' }',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'MODE': {
|
case 'MODE': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
// As a list of numbers can contain more than one mode,
|
// As a list of numbers can contain more than one mode,
|
||||||
// the returned result is provided as an array.
|
// the returned result is provided as an array.
|
||||||
// Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1]
|
// Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1]
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('math_modes', [
|
||||||
'math_modes',
|
'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List values) {',
|
||||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' List modes = [];',
|
||||||
'(List values) {',
|
' List counts = [];',
|
||||||
' List modes = [];',
|
' int maxCount = 0;',
|
||||||
' List counts = [];',
|
' for (int i = 0; i < values.length; i++) {',
|
||||||
' int maxCount = 0;',
|
' var value = values[i];',
|
||||||
' for (int i = 0; i < values.length; i++) {',
|
' bool found = false;',
|
||||||
' var value = values[i];',
|
' int thisCount;',
|
||||||
' bool found = false;',
|
' for (int j = 0; j < counts.length; j++) {',
|
||||||
' int thisCount;',
|
' if (counts[j][0] == value) {',
|
||||||
' for (int j = 0; j < counts.length; j++) {',
|
' thisCount = ++counts[j][1];',
|
||||||
' if (counts[j][0] == value) {',
|
' found = true;',
|
||||||
' thisCount = ++counts[j][1];',
|
' break;',
|
||||||
' found = true;',
|
' }',
|
||||||
' break;',
|
' }',
|
||||||
' }',
|
' if (!found) {',
|
||||||
' }',
|
' counts.add([value, 1]);',
|
||||||
' if (!found) {',
|
' thisCount = 1;',
|
||||||
' counts.add([value, 1]);',
|
' }',
|
||||||
' thisCount = 1;',
|
' maxCount = Math.max(thisCount, maxCount);',
|
||||||
' }',
|
' }',
|
||||||
' maxCount = Math.max(thisCount, maxCount);',
|
' for (int j = 0; j < counts.length; j++) {',
|
||||||
' }',
|
' if (counts[j][1] == maxCount) {',
|
||||||
' for (int j = 0; j < counts.length; j++) {',
|
' modes.add(counts[j][0]);',
|
||||||
' if (counts[j][1] == maxCount) {',
|
' }',
|
||||||
' modes.add(counts[j][0]);',
|
' }',
|
||||||
' }',
|
' return modes;',
|
||||||
' }',
|
'}'
|
||||||
' return modes;',
|
]);
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'STD_DEV': {
|
case 'STD_DEV': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const functionName = Dart.provideFunction_('math_standard_deviation', [
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||||
'math_standard_deviation',
|
' // First filter list for numbers only.',
|
||||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' List numbers = new List.from(myList);',
|
||||||
'(List myList) {',
|
' numbers.removeWhere((a) => a is! num);',
|
||||||
' // First filter list for numbers only.',
|
' if (numbers.isEmpty) return null;', ' num n = numbers.length;',
|
||||||
' List numbers = new List.from(myList);',
|
' num sum = 0;', ' numbers.forEach((x) => sum += x);',
|
||||||
' numbers.removeWhere((a) => a is! num);',
|
' num mean = sum / n;', ' num sumSquare = 0;',
|
||||||
' if (numbers.isEmpty) return null;',
|
' numbers.forEach((x) => sumSquare += ' +
|
||||||
' num n = numbers.length;',
|
'Math.pow(x - mean, 2));',
|
||||||
' num sum = 0;',
|
' return Math.sqrt(sumSquare / n);', '}'
|
||||||
' numbers.forEach((x) => sum += x);',
|
]);
|
||||||
' num mean = sum / n;',
|
|
||||||
' num sumSquare = 0;',
|
|
||||||
' numbers.forEach((x) => sumSquare += ' +
|
|
||||||
'Math.pow(x - mean, 2));',
|
|
||||||
' return Math.sqrt(sumSquare / n);',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'RANDOM': {
|
case 'RANDOM': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const functionName = Dart.provideFunction_('math_random_item', [
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||||
'math_random_item',
|
' int x = new Math.Random().nextInt(myList.length);',
|
||||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' return myList[x];', '}'
|
||||||
'(List myList) {',
|
]);
|
||||||
' int x = new Math.Random().nextInt(myList.length);',
|
|
||||||
' return myList[x];',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw Error('Unknown operator: ' + func);
|
throw Error('Unknown operator: ' + func);
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_modulo'] = function(block) {
|
Dart['math_modulo'] = function(block) {
|
||||||
// Remainder computation.
|
// Remainder computation.
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'DIVIDEND',
|
const argument0 =
|
||||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
Dart.valueToCode(block, 'DIVIDEND', Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'DIVISOR',
|
const argument1 =
|
||||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
Dart.valueToCode(block, 'DIVISOR', Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||||
const code = argument0 + ' % ' + argument1;
|
const code = argument0 + ' % ' + argument1;
|
||||||
return [code, Blockly.Dart.ORDER_MULTIPLICATIVE];
|
return [code, Dart.ORDER_MULTIPLICATIVE];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_constrain'] = function(block) {
|
Dart['math_constrain'] = function(block) {
|
||||||
// Constrain a number between two limits.
|
// Constrain a number between two limits.
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const argument0 = Dart.valueToCode(block, 'VALUE', Dart.ORDER_NONE) || '0';
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
const argument1 = Dart.valueToCode(block, 'LOW', Dart.ORDER_NONE) || '0';
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
const argument2 =
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'LOW',
|
Dart.valueToCode(block, 'HIGH', Dart.ORDER_NONE) || 'double.infinity';
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
|
||||||
const argument2 = Blockly.Dart.valueToCode(block, 'HIGH',
|
|
||||||
Blockly.Dart.ORDER_NONE) || 'double.infinity';
|
|
||||||
const code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' +
|
const code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' +
|
||||||
argument2 + ')';
|
argument2 + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_random_int'] = function(block) {
|
Dart['math_random_int'] = function(block) {
|
||||||
// Random integer between [X] and [Y].
|
// Random integer between [X] and [Y].
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const argument0 = Dart.valueToCode(block, 'FROM', Dart.ORDER_NONE) || '0';
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'FROM',
|
const argument1 = Dart.valueToCode(block, 'TO', Dart.ORDER_NONE) || '0';
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
const functionName = Dart.provideFunction_('math_random_int', [
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'TO',
|
'int ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num a, num b) {',
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
' if (a > b) {', ' // Swap a and b to ensure a is smaller.',
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
' num c = a;', ' a = b;', ' b = c;', ' }',
|
||||||
'math_random_int',
|
' return new Math.Random().nextInt(b - a + 1) + a;', '}'
|
||||||
['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num a, num b) {',
|
]);
|
||||||
' if (a > b) {',
|
|
||||||
' // Swap a and b to ensure a is smaller.',
|
|
||||||
' num c = a;',
|
|
||||||
' a = b;',
|
|
||||||
' b = c;',
|
|
||||||
' }',
|
|
||||||
' return new Math.Random().nextInt(b - a + 1) + a;',
|
|
||||||
'}']);
|
|
||||||
const code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
const code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_random_float'] = function(block) {
|
Dart['math_random_float'] = function(block) {
|
||||||
// Random fraction between 0 and 1.
|
// Random fraction between 0 and 1.
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
return ['new Math.Random().nextDouble()', Dart.ORDER_UNARY_POSTFIX];
|
||||||
return ['new Math.Random().nextDouble()', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['math_atan2'] = function(block) {
|
Dart['math_atan2'] = function(block) {
|
||||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const argument0 = Dart.valueToCode(block, 'X', Dart.ORDER_NONE) || '0';
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'X',
|
const argument1 = Dart.valueToCode(block, 'Y', Dart.ORDER_NONE) || '0';
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
return [
|
||||||
const argument1 = Blockly.Dart.valueToCode(block, 'Y',
|
'Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.pi * 180',
|
||||||
Blockly.Dart.ORDER_NONE) || '0';
|
Dart.ORDER_MULTIPLICATIVE
|
||||||
return ['Math.atan2(' + argument1 + ', ' + argument0 + ') / Math.pi * 180',
|
];
|
||||||
Blockly.Dart.ORDER_MULTIPLICATIVE];
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,105 +6,99 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for procedure blocks.
|
* @fileoverview Generating Dart for procedure blocks.
|
||||||
* @suppress {missingRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.procedures');
|
goog.module('Blockly.Dart.procedures');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
const {NameType} = goog.require('Blockly.Names');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart['procedures_defreturn'] = function(block) {
|
Dart['procedures_defreturn'] = function(block) {
|
||||||
// Define a procedure with a return value.
|
// Define a procedure with a return value.
|
||||||
const funcName = Blockly.Dart.nameDB_.getName(block.getFieldValue('NAME'),
|
const funcName =
|
||||||
Blockly.PROCEDURE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||||
let xfix1 = '';
|
let xfix1 = '';
|
||||||
if (Blockly.Dart.STATEMENT_PREFIX) {
|
if (Dart.STATEMENT_PREFIX) {
|
||||||
xfix1 += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_PREFIX, block);
|
xfix1 += Dart.injectId(Dart.STATEMENT_PREFIX, block);
|
||||||
}
|
}
|
||||||
if (Blockly.Dart.STATEMENT_SUFFIX) {
|
if (Dart.STATEMENT_SUFFIX) {
|
||||||
xfix1 += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block);
|
xfix1 += Dart.injectId(Dart.STATEMENT_SUFFIX, block);
|
||||||
}
|
}
|
||||||
if (xfix1) {
|
if (xfix1) {
|
||||||
xfix1 = Blockly.Dart.prefixLines(xfix1, Blockly.Dart.INDENT);
|
xfix1 = Dart.prefixLines(xfix1, Dart.INDENT);
|
||||||
}
|
}
|
||||||
let loopTrap = '';
|
let loopTrap = '';
|
||||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
if (Dart.INFINITE_LOOP_TRAP) {
|
||||||
loopTrap = Blockly.Dart.prefixLines(
|
loopTrap = Dart.prefixLines(
|
||||||
Blockly.Dart.injectId(Blockly.Dart.INFINITE_LOOP_TRAP, block),
|
Dart.injectId(Dart.INFINITE_LOOP_TRAP, block), Dart.INDENT);
|
||||||
Blockly.Dart.INDENT);
|
|
||||||
}
|
}
|
||||||
const branch = Blockly.Dart.statementToCode(block, 'STACK');
|
const branch = Dart.statementToCode(block, 'STACK');
|
||||||
let returnValue = Blockly.Dart.valueToCode(block, 'RETURN',
|
let returnValue = Dart.valueToCode(block, 'RETURN', Dart.ORDER_NONE) || '';
|
||||||
Blockly.Dart.ORDER_NONE) || '';
|
|
||||||
let xfix2 = '';
|
let xfix2 = '';
|
||||||
if (branch && returnValue) {
|
if (branch && returnValue) {
|
||||||
// After executing the function body, revisit this block for the return.
|
// After executing the function body, revisit this block for the return.
|
||||||
xfix2 = xfix1;
|
xfix2 = xfix1;
|
||||||
}
|
}
|
||||||
if (returnValue) {
|
if (returnValue) {
|
||||||
returnValue = Blockly.Dart.INDENT + 'return ' + returnValue + ';\n';
|
returnValue = Dart.INDENT + 'return ' + returnValue + ';\n';
|
||||||
}
|
}
|
||||||
const returnType = returnValue ? 'dynamic' : 'void';
|
const returnType = returnValue ? 'dynamic' : 'void';
|
||||||
const args = [];
|
const args = [];
|
||||||
const variables = block.getVars();
|
const variables = block.getVars();
|
||||||
for (let i = 0; i < variables.length; i++) {
|
for (let i = 0; i < variables.length; i++) {
|
||||||
args[i] = Blockly.Dart.nameDB_.getName(variables[i],
|
args[i] = Dart.nameDB_.getName(variables[i], NameType.VARIABLE);
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
|
||||||
}
|
}
|
||||||
let code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
|
let code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||||
xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
|
xfix1 + loopTrap + branch + xfix2 + returnValue + '}';
|
||||||
code = Blockly.Dart.scrub_(block, code);
|
code = Dart.scrub_(block, code);
|
||||||
// Add % so as not to collide with helper functions in definitions list.
|
// Add % so as not to collide with helper functions in definitions list.
|
||||||
Blockly.Dart.definitions_['%' + funcName] = code;
|
Dart.definitions_['%' + funcName] = code;
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Defining a procedure without a return value uses the same generator as
|
// Defining a procedure without a return value uses the same generator as
|
||||||
// a procedure with a return value.
|
// a procedure with a return value.
|
||||||
Blockly.Dart['procedures_defnoreturn'] = Blockly.Dart['procedures_defreturn'];
|
Dart['procedures_defnoreturn'] = Dart['procedures_defreturn'];
|
||||||
|
|
||||||
Blockly.Dart['procedures_callreturn'] = function(block) {
|
Dart['procedures_callreturn'] = function(block) {
|
||||||
// Call a procedure with a return value.
|
// Call a procedure with a return value.
|
||||||
const funcName = Blockly.Dart.nameDB_.getName(block.getFieldValue('NAME'),
|
const funcName =
|
||||||
Blockly.PROCEDURE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE);
|
||||||
const args = [];
|
const args = [];
|
||||||
const variables = block.getVars();
|
const variables = block.getVars();
|
||||||
for (let i = 0; i < variables.length; i++) {
|
for (let i = 0; i < variables.length; i++) {
|
||||||
args[i] = Blockly.Dart.valueToCode(block, 'ARG' + i,
|
args[i] = Dart.valueToCode(block, 'ARG' + i, Dart.ORDER_NONE) || 'null';
|
||||||
Blockly.Dart.ORDER_NONE) || 'null';
|
|
||||||
}
|
}
|
||||||
let code = funcName + '(' + args.join(', ') + ')';
|
let code = funcName + '(' + args.join(', ') + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['procedures_callnoreturn'] = function(block) {
|
Dart['procedures_callnoreturn'] = function(block) {
|
||||||
// Call a procedure with no return value.
|
// Call a procedure with no return value.
|
||||||
// Generated code is for a function call as a statement is the same as a
|
// Generated code is for a function call as a statement is the same as a
|
||||||
// function call as a value, with the addition of line ending.
|
// function call as a value, with the addition of line ending.
|
||||||
const tuple = Blockly.Dart['procedures_callreturn'](block);
|
const tuple = Dart['procedures_callreturn'](block);
|
||||||
return tuple[0] + ';\n';
|
return tuple[0] + ';\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['procedures_ifreturn'] = function(block) {
|
Dart['procedures_ifreturn'] = function(block) {
|
||||||
// Conditionally return value from a procedure.
|
// Conditionally return value from a procedure.
|
||||||
const condition = Blockly.Dart.valueToCode(block, 'CONDITION',
|
const condition =
|
||||||
Blockly.Dart.ORDER_NONE) || 'false';
|
Dart.valueToCode(block, 'CONDITION', Dart.ORDER_NONE) || 'false';
|
||||||
let code = 'if (' + condition + ') {\n';
|
let code = 'if (' + condition + ') {\n';
|
||||||
if (Blockly.Dart.STATEMENT_SUFFIX) {
|
if (Dart.STATEMENT_SUFFIX) {
|
||||||
// Inject any statement suffix here since the regular one at the end
|
// Inject any statement suffix here since the regular one at the end
|
||||||
// will not get executed if the return is triggered.
|
// will not get executed if the return is triggered.
|
||||||
code += Blockly.Dart.prefixLines(
|
code += Dart.prefixLines(
|
||||||
Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block),
|
Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT);
|
||||||
Blockly.Dart.INDENT);
|
|
||||||
}
|
}
|
||||||
if (block.hasReturnValue_) {
|
if (block.hasReturnValue_) {
|
||||||
const value = Blockly.Dart.valueToCode(block, 'VALUE',
|
const value = Dart.valueToCode(block, 'VALUE', Dart.ORDER_NONE) || 'null';
|
||||||
Blockly.Dart.ORDER_NONE) || 'null';
|
code += Dart.INDENT + 'return ' + value + ';\n';
|
||||||
code += Blockly.Dart.INDENT + 'return ' + value + ';\n';
|
|
||||||
} else {
|
} else {
|
||||||
code += Blockly.Dart.INDENT + 'return;\n';
|
code += Dart.INDENT + 'return;\n';
|
||||||
}
|
}
|
||||||
code += '}\n';
|
code += '}\n';
|
||||||
return code;
|
return code;
|
||||||
|
|||||||
@@ -6,164 +6,157 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for text blocks.
|
* @fileoverview Generating Dart for text blocks.
|
||||||
* @suppress {missingRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.texts');
|
goog.module('Blockly.Dart.texts');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
const {NameType} = goog.require('Blockly.Names');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart.addReservedWords('Html,Math');
|
Dart.addReservedWords('Html,Math');
|
||||||
|
|
||||||
Blockly.Dart['text'] = function(block) {
|
Dart['text'] = function(block) {
|
||||||
// Text value.
|
// Text value.
|
||||||
const code = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
const code = Dart.quote_(block.getFieldValue('TEXT'));
|
||||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
return [code, Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_multiline'] = function(block) {
|
Dart['text_multiline'] = function(block) {
|
||||||
// Text value.
|
// Text value.
|
||||||
const code = Blockly.Dart.multiline_quote_(block.getFieldValue('TEXT'));
|
const code = Dart.multiline_quote_(block.getFieldValue('TEXT'));
|
||||||
const order = code.indexOf('+') !== -1 ? Blockly.Dart.ORDER_ADDITIVE :
|
const order =
|
||||||
Blockly.Dart.ORDER_ATOMIC;
|
code.indexOf('+') !== -1 ? Dart.ORDER_ADDITIVE : Dart.ORDER_ATOMIC;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_join'] = function(block) {
|
Dart['text_join'] = function(block) {
|
||||||
// Create a string made up of any number of elements of any type.
|
// Create a string made up of any number of elements of any type.
|
||||||
switch (block.itemCount_) {
|
switch (block.itemCount_) {
|
||||||
case 0:
|
case 0:
|
||||||
return ['\'\'', Blockly.Dart.ORDER_ATOMIC];
|
return ['\'\'', Dart.ORDER_ATOMIC];
|
||||||
case 1: {
|
case 1: {
|
||||||
const element = Blockly.Dart.valueToCode(block, 'ADD0',
|
const element =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
Dart.valueToCode(block, 'ADD0', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
const code = element + '.toString()';
|
const code = element + '.toString()';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
const elements = new Array(block.itemCount_);
|
const elements = new Array(block.itemCount_);
|
||||||
for (let i = 0; i < block.itemCount_; i++) {
|
for (let i = 0; i < block.itemCount_; i++) {
|
||||||
elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i,
|
elements[i] =
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
Dart.valueToCode(block, 'ADD' + i, Dart.ORDER_NONE) || '\'\'';
|
||||||
}
|
}
|
||||||
const code = '[' + elements.join(',') + '].join()';
|
const code = '[' + elements.join(',') + '].join()';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_append'] = function(block) {
|
Dart['text_append'] = function(block) {
|
||||||
// Append to a variable in place.
|
// Append to a variable in place.
|
||||||
const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'),
|
const varName =
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||||
const value = Blockly.Dart.valueToCode(block, 'TEXT',
|
const value = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
|
||||||
return varName + ' = [' + varName + ', ' + value + '].join();\n';
|
return varName + ' = [' + varName + ', ' + value + '].join();\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_length'] = function(block) {
|
Dart['text_length'] = function(block) {
|
||||||
// String or array length.
|
// String or array length.
|
||||||
const text = Blockly.Dart.valueToCode(block, 'VALUE',
|
const text =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
return [text + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [text + '.length', Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_isEmpty'] = function(block) {
|
Dart['text_isEmpty'] = function(block) {
|
||||||
// Is the string null or array empty?
|
// Is the string null or array empty?
|
||||||
const text = Blockly.Dart.valueToCode(block, 'VALUE',
|
const text =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
return [text + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [text + '.isEmpty', Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_indexOf'] = function(block) {
|
Dart['text_indexOf'] = function(block) {
|
||||||
// Search the text for a substring.
|
// Search the text for a substring.
|
||||||
const operator = block.getFieldValue('END') === 'FIRST' ?
|
const operator =
|
||||||
'indexOf' : 'lastIndexOf';
|
block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf';
|
||||||
const substring = Blockly.Dart.valueToCode(block, 'FIND',
|
const substring = Dart.valueToCode(block, 'FIND', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
const text =
|
||||||
const text = Blockly.Dart.valueToCode(block, 'VALUE',
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
|
||||||
const code = text + '.' + operator + '(' + substring + ')';
|
const code = text + '.' + operator + '(' + substring + ')';
|
||||||
if (block.workspace.options.oneBasedIndex) {
|
if (block.workspace.options.oneBasedIndex) {
|
||||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
return [code + ' + 1', Dart.ORDER_ADDITIVE];
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_charAt'] = function(block) {
|
Dart['text_charAt'] = function(block) {
|
||||||
// Get letter at index.
|
// Get letter at index.
|
||||||
// Note: Until January 2013 this block did not have the WHERE input.
|
// Note: Until January 2013 this block did not have the WHERE input.
|
||||||
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||||
const textOrder = (where === 'FIRST' || where === 'FROM_START') ?
|
const textOrder = (where === 'FIRST' || where === 'FROM_START') ?
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX : Blockly.Dart.ORDER_NONE;
|
Dart.ORDER_UNARY_POSTFIX :
|
||||||
const text = Blockly.Dart.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
Dart.ORDER_NONE;
|
||||||
|
const text = Dart.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
||||||
let at;
|
let at;
|
||||||
switch (where) {
|
switch (where) {
|
||||||
case 'FIRST': {
|
case 'FIRST': {
|
||||||
const code = text + '[0]';
|
const code = text + '[0]';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
case 'FROM_START': {
|
case 'FROM_START': {
|
||||||
at = Blockly.Dart.getAdjusted(block, 'AT');
|
at = Dart.getAdjusted(block, 'AT');
|
||||||
const code = text + '[' + at + ']';
|
const code = text + '[' + at + ']';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
case 'LAST':
|
case 'LAST':
|
||||||
at = 1;
|
at = 1;
|
||||||
// Fall through.
|
// Fall through.
|
||||||
case 'FROM_END': {
|
case 'FROM_END': {
|
||||||
at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
at = Dart.getAdjusted(block, 'AT', 1);
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('text_get_from_end', [
|
||||||
'text_get_from_end',
|
'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String text, num x) {',
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' return text[text.length - x];', '}'
|
||||||
'(String text, num x) {',
|
]);
|
||||||
' return text[text.length - x];',
|
|
||||||
'}']);
|
|
||||||
const code = functionName + '(' + text + ', ' + at + ')';
|
const code = functionName + '(' + text + ', ' + at + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
case 'RANDOM': {
|
case 'RANDOM': {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
const functionName = Dart.provideFunction_('text_random_letter', [
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String text) {',
|
||||||
'text_random_letter',
|
' int x = new Math.Random().nextInt(text.length);',
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' return text[x];', '}'
|
||||||
'(String text) {',
|
]);
|
||||||
' int x = new Math.Random().nextInt(text.length);',
|
|
||||||
' return text[x];',
|
|
||||||
'}']);
|
|
||||||
const code = functionName + '(' + text + ')';
|
const code = functionName + '(' + text + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw Error('Unhandled option (text_charAt).');
|
throw Error('Unhandled option (text_charAt).');
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_getSubstring'] = function(block) {
|
Dart['text_getSubstring'] = function(block) {
|
||||||
// Get substring.
|
// Get substring.
|
||||||
const where1 = block.getFieldValue('WHERE1');
|
const where1 = block.getFieldValue('WHERE1');
|
||||||
const where2 = block.getFieldValue('WHERE2');
|
const where2 = block.getFieldValue('WHERE2');
|
||||||
const requiresLengthCall = (where1 !== 'FROM_END' && where2 === 'FROM_START');
|
const requiresLengthCall = (where1 !== 'FROM_END' && where2 === 'FROM_START');
|
||||||
const textOrder = requiresLengthCall ? Blockly.Dart.ORDER_UNARY_POSTFIX :
|
const textOrder =
|
||||||
Blockly.Dart.ORDER_NONE;
|
requiresLengthCall ? Dart.ORDER_UNARY_POSTFIX : Dart.ORDER_NONE;
|
||||||
const text = Blockly.Dart.valueToCode(block, 'STRING', textOrder) || '\'\'';
|
const text = Dart.valueToCode(block, 'STRING', textOrder) || '\'\'';
|
||||||
let code;
|
let code;
|
||||||
if (where1 === 'FIRST' && where2 === 'LAST') {
|
if (where1 === 'FIRST' && where2 === 'LAST') {
|
||||||
code = text;
|
code = text;
|
||||||
return [code, Blockly.Dart.ORDER_NONE];
|
return [code, Dart.ORDER_NONE];
|
||||||
} else if (text.match(/^'?\w+'?$/) || requiresLengthCall) {
|
} else if (text.match(/^'?\w+'?$/) || requiresLengthCall) {
|
||||||
// If the text is a variable or literal or doesn't require a call for
|
// If the text is a variable or literal or doesn't require a call for
|
||||||
// length, don't generate a helper function.
|
// length, don't generate a helper function.
|
||||||
let at1;
|
let at1;
|
||||||
switch (where1) {
|
switch (where1) {
|
||||||
case 'FROM_START':
|
case 'FROM_START':
|
||||||
at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
at1 = Dart.getAdjusted(block, 'AT1');
|
||||||
break;
|
break;
|
||||||
case 'FROM_END':
|
case 'FROM_END':
|
||||||
at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
|
at1 = Dart.getAdjusted(block, 'AT1', 1, false, Dart.ORDER_ADDITIVE);
|
||||||
Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
at1 = text + '.length - ' + at1;
|
at1 = text + '.length - ' + at1;
|
||||||
break;
|
break;
|
||||||
case 'FIRST':
|
case 'FIRST':
|
||||||
@@ -175,11 +168,10 @@ Blockly.Dart['text_getSubstring'] = function(block) {
|
|||||||
let at2;
|
let at2;
|
||||||
switch (where2) {
|
switch (where2) {
|
||||||
case 'FROM_START':
|
case 'FROM_START':
|
||||||
at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
|
at2 = Dart.getAdjusted(block, 'AT2', 1);
|
||||||
break;
|
break;
|
||||||
case 'FROM_END':
|
case 'FROM_END':
|
||||||
at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
|
at2 = Dart.getAdjusted(block, 'AT2', 0, false, Dart.ORDER_ADDITIVE);
|
||||||
Blockly.Dart.ORDER_ADDITIVE);
|
|
||||||
at2 = text + '.length - ' + at2;
|
at2 = text + '.length - ' + at2;
|
||||||
break;
|
break;
|
||||||
case 'LAST':
|
case 'LAST':
|
||||||
@@ -194,35 +186,28 @@ Blockly.Dart['text_getSubstring'] = function(block) {
|
|||||||
code = text + '.substring(' + at1 + ', ' + at2 + ')';
|
code = text + '.substring(' + at1 + ', ' + at2 + ')';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
const at1 = Dart.getAdjusted(block, 'AT1');
|
||||||
const at2 = Blockly.Dart.getAdjusted(block, 'AT2');
|
const at2 = Dart.getAdjusted(block, 'AT2');
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('text_get_substring', [
|
||||||
'text_get_substring',
|
'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
'(String text, String where1, num at1, String where2, num at2) {',
|
||||||
'(String text, String where1, num at1, String where2, num at2) {',
|
' int getAt(String where, num at) {', ' if (where == \'FROM_END\') {',
|
||||||
' int getAt(String where, num at) {',
|
' at = text.length - 1 - at;',
|
||||||
' if (where == \'FROM_END\') {',
|
' } else if (where == \'FIRST\') {', ' at = 0;',
|
||||||
' at = text.length - 1 - at;',
|
' } else if (where == \'LAST\') {', ' at = text.length - 1;',
|
||||||
' } else if (where == \'FIRST\') {',
|
' } else if (where != \'FROM_START\') {',
|
||||||
' at = 0;',
|
' throw \'Unhandled option (text_getSubstring).\';', ' }',
|
||||||
' } else if (where == \'LAST\') {',
|
' return at;', ' }', ' at1 = getAt(where1, at1);',
|
||||||
' at = text.length - 1;',
|
' at2 = getAt(where2, at2) + 1;', ' return text.substring(at1, at2);',
|
||||||
' } else if (where != \'FROM_START\') {',
|
'}'
|
||||||
' throw \'Unhandled option (text_getSubstring).\';',
|
]);
|
||||||
' }',
|
code = functionName + '(' + text + ', \'' + where1 + '\', ' + at1 + ', \'' +
|
||||||
' return at;',
|
where2 + '\', ' + at2 + ')';
|
||||||
' }',
|
|
||||||
' at1 = getAt(where1, at1);',
|
|
||||||
' at2 = getAt(where2, at2) + 1;',
|
|
||||||
' return text.substring(at1, at2);',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + text + ', \'' +
|
|
||||||
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_changeCase'] = function(block) {
|
Dart['text_changeCase'] = function(block) {
|
||||||
// Change capitalization.
|
// Change capitalization.
|
||||||
const OPERATORS = {
|
const OPERATORS = {
|
||||||
'UPPERCASE': '.toUpperCase()',
|
'UPPERCASE': '.toUpperCase()',
|
||||||
@@ -230,38 +215,30 @@ Blockly.Dart['text_changeCase'] = function(block) {
|
|||||||
'TITLECASE': null
|
'TITLECASE': null
|
||||||
};
|
};
|
||||||
const operator = OPERATORS[block.getFieldValue('CASE')];
|
const operator = OPERATORS[block.getFieldValue('CASE')];
|
||||||
const textOrder = operator ? Blockly.Dart.ORDER_UNARY_POSTFIX :
|
const textOrder = operator ? Dart.ORDER_UNARY_POSTFIX : Dart.ORDER_NONE;
|
||||||
Blockly.Dart.ORDER_NONE;
|
const text = Dart.valueToCode(block, 'TEXT', textOrder) || '\'\'';
|
||||||
const text = Blockly.Dart.valueToCode(block, 'TEXT', textOrder) || '\'\'';
|
|
||||||
let code;
|
let code;
|
||||||
if (operator) {
|
if (operator) {
|
||||||
// Upper and lower case are functions built into Dart.
|
// Upper and lower case are functions built into Dart.
|
||||||
code = text + operator;
|
code = text + operator;
|
||||||
} else {
|
} else {
|
||||||
// Title case is not a native Dart function. Define one.
|
// Title case is not a native Dart function. Define one.
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('text_toTitleCase', [
|
||||||
'text_toTitleCase',
|
'String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(String str) {',
|
||||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
' RegExp exp = new RegExp(r\'\\b\');',
|
||||||
'(String str) {',
|
' List<String> list = str.split(exp);',
|
||||||
' RegExp exp = new RegExp(r\'\\b\');',
|
' final title = new StringBuffer();', ' for (String part in list) {',
|
||||||
' List<String> list = str.split(exp);',
|
' if (part.length > 0) {', ' title.write(part[0].toUpperCase());',
|
||||||
' final title = new StringBuffer();',
|
' if (part.length > 0) {',
|
||||||
' for (String part in list) {',
|
' title.write(part.substring(1).toLowerCase());', ' }',
|
||||||
' if (part.length > 0) {',
|
' }', ' }', ' return title.toString();', '}'
|
||||||
' title.write(part[0].toUpperCase());',
|
]);
|
||||||
' if (part.length > 0) {',
|
|
||||||
' title.write(part.substring(1).toLowerCase());',
|
|
||||||
' }',
|
|
||||||
' }',
|
|
||||||
' }',
|
|
||||||
' return title.toString();',
|
|
||||||
'}']);
|
|
||||||
code = functionName + '(' + text + ')';
|
code = functionName + '(' + text + ')';
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_trim'] = function(block) {
|
Dart['text_trim'] = function(block) {
|
||||||
// Trim spaces.
|
// Trim spaces.
|
||||||
const OPERATORS = {
|
const OPERATORS = {
|
||||||
'LEFT': '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
|
'LEFT': '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
|
||||||
@@ -269,88 +246,71 @@ Blockly.Dart['text_trim'] = function(block) {
|
|||||||
'BOTH': '.trim()'
|
'BOTH': '.trim()'
|
||||||
};
|
};
|
||||||
const operator = OPERATORS[block.getFieldValue('MODE')];
|
const operator = OPERATORS[block.getFieldValue('MODE')];
|
||||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
const text =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
Dart.valueToCode(block, 'TEXT', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
return [text + operator, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [text + operator, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_print'] = function(block) {
|
Dart['text_print'] = function(block) {
|
||||||
// Print statement.
|
// Print statement.
|
||||||
const msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
const msg = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
|
||||||
return 'print(' + msg + ');\n';
|
return 'print(' + msg + ');\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_prompt_ext'] = function(block) {
|
Dart['text_prompt_ext'] = function(block) {
|
||||||
// Prompt function.
|
// Prompt function.
|
||||||
Blockly.Dart.definitions_['import_dart_html'] =
|
Dart.definitions_['import_dart_html'] = 'import \'dart:html\' as Html;';
|
||||||
'import \'dart:html\' as Html;';
|
|
||||||
let msg;
|
let msg;
|
||||||
if (block.getField('TEXT')) {
|
if (block.getField('TEXT')) {
|
||||||
// Internal message.
|
// Internal message.
|
||||||
msg = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
msg = Dart.quote_(block.getFieldValue('TEXT'));
|
||||||
} else {
|
} else {
|
||||||
// External message.
|
// External message.
|
||||||
msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
msg = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
|
||||||
}
|
}
|
||||||
let code = 'Html.window.prompt(' + msg + ', \'\')';
|
let code = 'Html.window.prompt(' + msg + ', \'\')';
|
||||||
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
||||||
if (toNumber) {
|
if (toNumber) {
|
||||||
Blockly.Dart.definitions_['import_dart_math'] =
|
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||||
'import \'dart:math\' as Math;';
|
|
||||||
code = 'Math.parseDouble(' + code + ')';
|
code = 'Math.parseDouble(' + code + ')';
|
||||||
}
|
}
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_prompt'] = Blockly.Dart['text_prompt_ext'];
|
Dart['text_prompt'] = Dart['text_prompt_ext'];
|
||||||
|
|
||||||
Blockly.Dart['text_count'] = function(block) {
|
Dart['text_count'] = function(block) {
|
||||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
const text = Dart.valueToCode(block, 'TEXT', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
const sub = Dart.valueToCode(block, 'SUB', Dart.ORDER_NONE) || '\'\'';
|
||||||
const sub = Blockly.Dart.valueToCode(block, 'SUB',
|
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
|
||||||
// Substring count is not a native Dart function. Define one.
|
// Substring count is not a native Dart function. Define one.
|
||||||
const functionName = Blockly.Dart.provideFunction_(
|
const functionName = Dart.provideFunction_('text_count', [
|
||||||
'text_count',
|
'int ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
|
||||||
'(String haystack, String needle) {',
|
'(String haystack, String needle) {',
|
||||||
' if (needle.length == 0) {',
|
' if (needle.length == 0) {', ' return haystack.length + 1;', ' }',
|
||||||
' return haystack.length + 1;',
|
' int index = 0;', ' int count = 0;', ' while (index != -1) {',
|
||||||
' }',
|
' index = haystack.indexOf(needle, index);', ' if (index != -1) {',
|
||||||
' int index = 0;',
|
' count++;', ' index += needle.length;', ' }', ' }',
|
||||||
' int count = 0;',
|
' return count;', '}'
|
||||||
' while (index != -1) {',
|
]);
|
||||||
' index = haystack.indexOf(needle, index);',
|
|
||||||
' if (index != -1) {',
|
|
||||||
' count++;',
|
|
||||||
' index += needle.length;',
|
|
||||||
' }',
|
|
||||||
' }',
|
|
||||||
' return count;',
|
|
||||||
'}']);
|
|
||||||
const code = functionName + '(' + text + ', ' + sub + ')';
|
const code = functionName + '(' + text + ', ' + sub + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_replace'] = function(block) {
|
Dart['text_replace'] = function(block) {
|
||||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
const text =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
Dart.valueToCode(block, 'TEXT', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
const from = Blockly.Dart.valueToCode(block, 'FROM',
|
const from = Dart.valueToCode(block, 'FROM', Dart.ORDER_NONE) || '\'\'';
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
const to = Dart.valueToCode(block, 'TO', Dart.ORDER_NONE) || '\'\'';
|
||||||
const to = Blockly.Dart.valueToCode(block, 'TO',
|
|
||||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
|
||||||
const code = text + '.replaceAll(' + from + ', ' + to + ')';
|
const code = text + '.replaceAll(' + from + ', ' + to + ')';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['text_reverse'] = function(block) {
|
Dart['text_reverse'] = function(block) {
|
||||||
// There isn't a sensible way to do this in Dart. See:
|
// There isn't a sensible way to do this in Dart. See:
|
||||||
// http://stackoverflow.com/a/21613700/3529104
|
// http://stackoverflow.com/a/21613700/3529104
|
||||||
// Implementing something is possibly better than not implementing anything?
|
// Implementing something is possibly better than not implementing anything?
|
||||||
const text = Blockly.Dart.valueToCode(block, 'TEXT',
|
const text =
|
||||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
Dart.valueToCode(block, 'TEXT', Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||||
const code = 'new String.fromCharCodes(' + text + '.runes.toList().reversed)';
|
const code = 'new String.fromCharCodes(' + text + '.runes.toList().reversed)';
|
||||||
return [code, Blockly.Dart.ORDER_UNARY_PREFIX];
|
return [code, Dart.ORDER_UNARY_PREFIX];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,27 +6,27 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for variable blocks.
|
* @fileoverview Generating Dart for variable blocks.
|
||||||
* @suppress {missingRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.variables');
|
goog.module('Blockly.Dart.variables');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
const {NameType} = goog.require('Blockly.Names');
|
||||||
|
|
||||||
|
|
||||||
Blockly.Dart['variables_get'] = function(block) {
|
Dart['variables_get'] = function(block) {
|
||||||
// Variable getter.
|
// Variable getter.
|
||||||
const code = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'),
|
const code =
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
return [code, Dart.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Dart['variables_set'] = function(block) {
|
Dart['variables_set'] = function(block) {
|
||||||
// Variable setter.
|
// Variable setter.
|
||||||
const argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
const argument0 =
|
||||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
Dart.valueToCode(block, 'VALUE', Dart.ORDER_ASSIGNMENT) || '0';
|
||||||
const varName = Blockly.Dart.nameDB_.getName(block.getFieldValue('VAR'),
|
const varName =
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
Dart.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||||
return varName + ' = ' + argument0 + ';\n';
|
return varName + ' = ' + argument0 + ';\n';
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,16 +6,16 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Generating Dart for dynamic variable blocks.
|
* @fileoverview Generating Dart for dynamic variable blocks.
|
||||||
* @suppress {extraRequire}
|
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.Dart.variablesDynamic');
|
goog.module('Blockly.Dart.variablesDynamic');
|
||||||
|
|
||||||
goog.require('Blockly.Dart');
|
const Dart = goog.require('Blockly.Dart');
|
||||||
|
/** @suppress {extraRequire} */
|
||||||
goog.require('Blockly.Dart.variables');
|
goog.require('Blockly.Dart.variables');
|
||||||
|
|
||||||
|
|
||||||
// Dart is dynamically typed.
|
// Dart is dynamically typed.
|
||||||
Blockly.Dart['variables_get_dynamic'] = Blockly.Dart['variables_get'];
|
Dart['variables_get_dynamic'] = Dart['variables_get'];
|
||||||
Blockly.Dart['variables_set_dynamic'] = Blockly.Dart['variables_set'];
|
Dart['variables_set_dynamic'] = Dart['variables_set'];
|
||||||
|
|||||||
@@ -6,28 +6,29 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview Helper functions for generating JavaScript for blocks.
|
* @fileoverview Helper functions for generating JavaScript for blocks.
|
||||||
* @suppress {missingRequire|checkTypes|globalThis}
|
* @suppress {checkTypes|globalThis}
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
goog.provide('Blockly.JavaScript');
|
goog.module('Blockly.JavaScript');
|
||||||
|
goog.module.declareLegacyNamespace();
|
||||||
|
|
||||||
goog.require('Blockly.Generator');
|
const Variables = goog.require('Blockly.Variables');
|
||||||
goog.require('Blockly.Variables');
|
const objectUtils = goog.require('Blockly.utils.object');
|
||||||
goog.require('Blockly.Names');
|
const stringUtils = goog.require('Blockly.utils.string');
|
||||||
goog.require('Blockly.inputTypes');
|
const {Block} = goog.requireType('Blockly.Block');
|
||||||
goog.require('Blockly.utils.global');
|
const {Generator} = goog.require('Blockly.Generator');
|
||||||
goog.require('Blockly.utils.object');
|
const {globalThis} = goog.require('Blockly.utils.global');
|
||||||
goog.require('Blockly.utils.string');
|
const {inputTypes} = goog.require('Blockly.inputTypes');
|
||||||
goog.requireType('Blockly.Block');
|
const {Names, NameType} = goog.require('Blockly.Names');
|
||||||
goog.requireType('Blockly.Workspace');
|
const {Workspace} = goog.requireType('Blockly.Workspace');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JavaScript code generator.
|
* JavaScript code generator.
|
||||||
* @type {!Blockly.Generator}
|
* @type {!Generator}
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript = new Blockly.Generator('JavaScript');
|
const JavaScript = new Generator('JavaScript');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of illegal variable names.
|
* List of illegal variable names.
|
||||||
@@ -36,7 +37,7 @@ Blockly.JavaScript = new Blockly.Generator('JavaScript');
|
|||||||
* accidentally clobbering a built-in object or function.
|
* accidentally clobbering a built-in object or function.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.addReservedWords(
|
JavaScript.addReservedWords(
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
|
||||||
'break,case,catch,class,const,continue,debugger,default,delete,do,else,export,extends,finally,for,function,if,import,in,instanceof,new,return,super,switch,this,throw,try,typeof,var,void,while,with,yield,' +
|
'break,case,catch,class,const,continue,debugger,default,delete,do,else,export,extends,finally,for,function,if,import,in,instanceof,new,return,super,switch,this,throw,try,typeof,var,void,while,with,yield,' +
|
||||||
'enum,' +
|
'enum,' +
|
||||||
@@ -46,95 +47,95 @@ Blockly.JavaScript.addReservedWords(
|
|||||||
// Magic variable.
|
// Magic variable.
|
||||||
'arguments,' +
|
'arguments,' +
|
||||||
// Everything in the current environment (835 items in Chrome, 104 in Node).
|
// Everything in the current environment (835 items in Chrome, 104 in Node).
|
||||||
Object.getOwnPropertyNames(Blockly.utils.global.globalThis).join(','));
|
Object.getOwnPropertyNames(globalThis).join(','));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Order of operation ENUMs.
|
* Order of operation ENUMs.
|
||||||
* https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
|
* https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.ORDER_ATOMIC = 0; // 0 "" ...
|
JavaScript.ORDER_ATOMIC = 0; // 0 "" ...
|
||||||
Blockly.JavaScript.ORDER_NEW = 1.1; // new
|
JavaScript.ORDER_NEW = 1.1; // new
|
||||||
Blockly.JavaScript.ORDER_MEMBER = 1.2; // . []
|
JavaScript.ORDER_MEMBER = 1.2; // . []
|
||||||
Blockly.JavaScript.ORDER_FUNCTION_CALL = 2; // ()
|
JavaScript.ORDER_FUNCTION_CALL = 2; // ()
|
||||||
Blockly.JavaScript.ORDER_INCREMENT = 3; // ++
|
JavaScript.ORDER_INCREMENT = 3; // ++
|
||||||
Blockly.JavaScript.ORDER_DECREMENT = 3; // --
|
JavaScript.ORDER_DECREMENT = 3; // --
|
||||||
Blockly.JavaScript.ORDER_BITWISE_NOT = 4.1; // ~
|
JavaScript.ORDER_BITWISE_NOT = 4.1; // ~
|
||||||
Blockly.JavaScript.ORDER_UNARY_PLUS = 4.2; // +
|
JavaScript.ORDER_UNARY_PLUS = 4.2; // +
|
||||||
Blockly.JavaScript.ORDER_UNARY_NEGATION = 4.3; // -
|
JavaScript.ORDER_UNARY_NEGATION = 4.3; // -
|
||||||
Blockly.JavaScript.ORDER_LOGICAL_NOT = 4.4; // !
|
JavaScript.ORDER_LOGICAL_NOT = 4.4; // !
|
||||||
Blockly.JavaScript.ORDER_TYPEOF = 4.5; // typeof
|
JavaScript.ORDER_TYPEOF = 4.5; // typeof
|
||||||
Blockly.JavaScript.ORDER_VOID = 4.6; // void
|
JavaScript.ORDER_VOID = 4.6; // void
|
||||||
Blockly.JavaScript.ORDER_DELETE = 4.7; // delete
|
JavaScript.ORDER_DELETE = 4.7; // delete
|
||||||
Blockly.JavaScript.ORDER_AWAIT = 4.8; // await
|
JavaScript.ORDER_AWAIT = 4.8; // await
|
||||||
Blockly.JavaScript.ORDER_EXPONENTIATION = 5.0; // **
|
JavaScript.ORDER_EXPONENTIATION = 5.0; // **
|
||||||
Blockly.JavaScript.ORDER_MULTIPLICATION = 5.1; // *
|
JavaScript.ORDER_MULTIPLICATION = 5.1; // *
|
||||||
Blockly.JavaScript.ORDER_DIVISION = 5.2; // /
|
JavaScript.ORDER_DIVISION = 5.2; // /
|
||||||
Blockly.JavaScript.ORDER_MODULUS = 5.3; // %
|
JavaScript.ORDER_MODULUS = 5.3; // %
|
||||||
Blockly.JavaScript.ORDER_SUBTRACTION = 6.1; // -
|
JavaScript.ORDER_SUBTRACTION = 6.1; // -
|
||||||
Blockly.JavaScript.ORDER_ADDITION = 6.2; // +
|
JavaScript.ORDER_ADDITION = 6.2; // +
|
||||||
Blockly.JavaScript.ORDER_BITWISE_SHIFT = 7; // << >> >>>
|
JavaScript.ORDER_BITWISE_SHIFT = 7; // << >> >>>
|
||||||
Blockly.JavaScript.ORDER_RELATIONAL = 8; // < <= > >=
|
JavaScript.ORDER_RELATIONAL = 8; // < <= > >=
|
||||||
Blockly.JavaScript.ORDER_IN = 8; // in
|
JavaScript.ORDER_IN = 8; // in
|
||||||
Blockly.JavaScript.ORDER_INSTANCEOF = 8; // instanceof
|
JavaScript.ORDER_INSTANCEOF = 8; // instanceof
|
||||||
Blockly.JavaScript.ORDER_EQUALITY = 9; // == != === !==
|
JavaScript.ORDER_EQUALITY = 9; // == != === !==
|
||||||
Blockly.JavaScript.ORDER_BITWISE_AND = 10; // &
|
JavaScript.ORDER_BITWISE_AND = 10; // &
|
||||||
Blockly.JavaScript.ORDER_BITWISE_XOR = 11; // ^
|
JavaScript.ORDER_BITWISE_XOR = 11; // ^
|
||||||
Blockly.JavaScript.ORDER_BITWISE_OR = 12; // |
|
JavaScript.ORDER_BITWISE_OR = 12; // |
|
||||||
Blockly.JavaScript.ORDER_LOGICAL_AND = 13; // &&
|
JavaScript.ORDER_LOGICAL_AND = 13; // &&
|
||||||
Blockly.JavaScript.ORDER_LOGICAL_OR = 14; // ||
|
JavaScript.ORDER_LOGICAL_OR = 14; // ||
|
||||||
Blockly.JavaScript.ORDER_CONDITIONAL = 15; // ?:
|
JavaScript.ORDER_CONDITIONAL = 15; // ?:
|
||||||
Blockly.JavaScript.ORDER_ASSIGNMENT = 16; // = += -= **= *= /= %= <<= >>= ...
|
JavaScript.ORDER_ASSIGNMENT = 16; // = += -= **= *= /= %= <<= >>= ...
|
||||||
Blockly.JavaScript.ORDER_YIELD = 17; // yield
|
JavaScript.ORDER_YIELD = 17; // yield
|
||||||
Blockly.JavaScript.ORDER_COMMA = 18; // ,
|
JavaScript.ORDER_COMMA = 18; // ,
|
||||||
Blockly.JavaScript.ORDER_NONE = 99; // (...)
|
JavaScript.ORDER_NONE = 99; // (...)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of outer-inner pairings that do NOT require parentheses.
|
* List of outer-inner pairings that do NOT require parentheses.
|
||||||
* @type {!Array<!Array<number>>}
|
* @type {!Array<!Array<number>>}
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.ORDER_OVERRIDES = [
|
JavaScript.ORDER_OVERRIDES = [
|
||||||
// (foo()).bar -> foo().bar
|
// (foo()).bar -> foo().bar
|
||||||
// (foo())[0] -> foo()[0]
|
// (foo())[0] -> foo()[0]
|
||||||
[Blockly.JavaScript.ORDER_FUNCTION_CALL, Blockly.JavaScript.ORDER_MEMBER],
|
[JavaScript.ORDER_FUNCTION_CALL, JavaScript.ORDER_MEMBER],
|
||||||
// (foo())() -> foo()()
|
// (foo())() -> foo()()
|
||||||
[Blockly.JavaScript.ORDER_FUNCTION_CALL, Blockly.JavaScript.ORDER_FUNCTION_CALL],
|
[JavaScript.ORDER_FUNCTION_CALL, JavaScript.ORDER_FUNCTION_CALL],
|
||||||
// (foo.bar).baz -> foo.bar.baz
|
// (foo.bar).baz -> foo.bar.baz
|
||||||
// (foo.bar)[0] -> foo.bar[0]
|
// (foo.bar)[0] -> foo.bar[0]
|
||||||
// (foo[0]).bar -> foo[0].bar
|
// (foo[0]).bar -> foo[0].bar
|
||||||
// (foo[0])[1] -> foo[0][1]
|
// (foo[0])[1] -> foo[0][1]
|
||||||
[Blockly.JavaScript.ORDER_MEMBER, Blockly.JavaScript.ORDER_MEMBER],
|
[JavaScript.ORDER_MEMBER, JavaScript.ORDER_MEMBER],
|
||||||
// (foo.bar)() -> foo.bar()
|
// (foo.bar)() -> foo.bar()
|
||||||
// (foo[0])() -> foo[0]()
|
// (foo[0])() -> foo[0]()
|
||||||
[Blockly.JavaScript.ORDER_MEMBER, Blockly.JavaScript.ORDER_FUNCTION_CALL],
|
[JavaScript.ORDER_MEMBER, JavaScript.ORDER_FUNCTION_CALL],
|
||||||
|
|
||||||
// !(!foo) -> !!foo
|
// !(!foo) -> !!foo
|
||||||
[Blockly.JavaScript.ORDER_LOGICAL_NOT, Blockly.JavaScript.ORDER_LOGICAL_NOT],
|
[JavaScript.ORDER_LOGICAL_NOT, JavaScript.ORDER_LOGICAL_NOT],
|
||||||
// a * (b * c) -> a * b * c
|
// a * (b * c) -> a * b * c
|
||||||
[Blockly.JavaScript.ORDER_MULTIPLICATION, Blockly.JavaScript.ORDER_MULTIPLICATION],
|
[JavaScript.ORDER_MULTIPLICATION, JavaScript.ORDER_MULTIPLICATION],
|
||||||
// a + (b + c) -> a + b + c
|
// a + (b + c) -> a + b + c
|
||||||
[Blockly.JavaScript.ORDER_ADDITION, Blockly.JavaScript.ORDER_ADDITION],
|
[JavaScript.ORDER_ADDITION, JavaScript.ORDER_ADDITION],
|
||||||
// a && (b && c) -> a && b && c
|
// a && (b && c) -> a && b && c
|
||||||
[Blockly.JavaScript.ORDER_LOGICAL_AND, Blockly.JavaScript.ORDER_LOGICAL_AND],
|
[JavaScript.ORDER_LOGICAL_AND, JavaScript.ORDER_LOGICAL_AND],
|
||||||
// a || (b || c) -> a || b || c
|
// a || (b || c) -> a || b || c
|
||||||
[Blockly.JavaScript.ORDER_LOGICAL_OR, Blockly.JavaScript.ORDER_LOGICAL_OR]
|
[JavaScript.ORDER_LOGICAL_OR, JavaScript.ORDER_LOGICAL_OR]
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the init method has been called.
|
* Whether the init method has been called.
|
||||||
* @type {?boolean}
|
* @type {?boolean}
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.isInitialized = false;
|
JavaScript.isInitialized = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise the database of variable names.
|
* Initialise the database of variable names.
|
||||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
* @param {!Workspace} workspace Workspace to generate code from.
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.init = function(workspace) {
|
JavaScript.init = function(workspace) {
|
||||||
// Call Blockly.Generator's init.
|
// Call Blockly.Generator's init.
|
||||||
Object.getPrototypeOf(this).init.call(this);
|
Object.getPrototypeOf(this).init.call(this);
|
||||||
|
|
||||||
if (!this.nameDB_) {
|
if (!this.nameDB_) {
|
||||||
this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
|
this.nameDB_ = new Names(this.RESERVED_WORDS_);
|
||||||
} else {
|
} else {
|
||||||
this.nameDB_.reset();
|
this.nameDB_.reset();
|
||||||
}
|
}
|
||||||
@@ -145,17 +146,16 @@ Blockly.JavaScript.init = function(workspace) {
|
|||||||
|
|
||||||
const defvars = [];
|
const defvars = [];
|
||||||
// Add developer variables (not created or named by the user).
|
// Add developer variables (not created or named by the user).
|
||||||
const devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
const devVarList = Variables.allDeveloperVariables(workspace);
|
||||||
for (let i = 0; i < devVarList.length; i++) {
|
for (let i = 0; i < devVarList.length; i++) {
|
||||||
defvars.push(this.nameDB_.getName(devVarList[i],
|
defvars.push(
|
||||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
this.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add user variables, but only ones that are being used.
|
// Add user variables, but only ones that are being used.
|
||||||
const variables = Blockly.Variables.allUsedVarModels(workspace);
|
const variables = Variables.allUsedVarModels(workspace);
|
||||||
for (let i = 0; i < variables.length; i++) {
|
for (let i = 0; i < variables.length; i++) {
|
||||||
defvars.push(this.nameDB_.getName(variables[i].getId(),
|
defvars.push(this.nameDB_.getName(variables[i].getId(), NameType.VARIABLE));
|
||||||
Blockly.VARIABLE_CATEGORY_NAME));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Declare all of the variables.
|
// Declare all of the variables.
|
||||||
@@ -170,9 +170,9 @@ Blockly.JavaScript.init = function(workspace) {
|
|||||||
* @param {string} code Generated code.
|
* @param {string} code Generated code.
|
||||||
* @return {string} Completed code.
|
* @return {string} Completed code.
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.finish = function(code) {
|
JavaScript.finish = function(code) {
|
||||||
// Convert the definitions dictionary into a list.
|
// Convert the definitions dictionary into a list.
|
||||||
const definitions = Blockly.utils.object.values(this.definitions_);
|
const definitions = objectUtils.values(this.definitions_);
|
||||||
// Call Blockly.Generator's finish.
|
// Call Blockly.Generator's finish.
|
||||||
code = Object.getPrototypeOf(this).finish.call(this, code);
|
code = Object.getPrototypeOf(this).finish.call(this, code);
|
||||||
this.isInitialized = false;
|
this.isInitialized = false;
|
||||||
@@ -187,7 +187,7 @@ Blockly.JavaScript.finish = function(code) {
|
|||||||
* @param {string} line Line of generated code.
|
* @param {string} line Line of generated code.
|
||||||
* @return {string} Legal line of code.
|
* @return {string} Legal line of code.
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.scrubNakedValue = function(line) {
|
JavaScript.scrubNakedValue = function(line) {
|
||||||
return line + ';\n';
|
return line + ';\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -198,12 +198,12 @@ Blockly.JavaScript.scrubNakedValue = function(line) {
|
|||||||
* @return {string} JavaScript string.
|
* @return {string} JavaScript string.
|
||||||
* @protected
|
* @protected
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.quote_ = function(string) {
|
JavaScript.quote_ = function(string) {
|
||||||
// Can't use goog.string.quote since Google's style guide recommends
|
// Can't use goog.string.quote since Google's style guide recommends
|
||||||
// JS string literals use single quotes.
|
// JS string literals use single quotes.
|
||||||
string = string.replace(/\\/g, '\\\\')
|
string = string.replace(/\\/g, '\\\\')
|
||||||
.replace(/\n/g, '\\\n')
|
.replace(/\n/g, '\\\n')
|
||||||
.replace(/'/g, '\\\'');
|
.replace(/'/g, '\\\'');
|
||||||
return '\'' + string + '\'';
|
return '\'' + string + '\'';
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -214,7 +214,7 @@ Blockly.JavaScript.quote_ = function(string) {
|
|||||||
* @return {string} JavaScript string.
|
* @return {string} JavaScript string.
|
||||||
* @protected
|
* @protected
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.multiline_quote_ = function(string) {
|
JavaScript.multiline_quote_ = function(string) {
|
||||||
// Can't use goog.string.quote since Google's style guide recommends
|
// Can't use goog.string.quote since Google's style guide recommends
|
||||||
// JS string literals use single quotes.
|
// JS string literals use single quotes.
|
||||||
const lines = string.split(/\n/g).map(this.quote_);
|
const lines = string.split(/\n/g).map(this.quote_);
|
||||||
@@ -225,26 +225,26 @@ Blockly.JavaScript.multiline_quote_ = function(string) {
|
|||||||
* Common tasks for generating JavaScript from blocks.
|
* Common tasks for generating JavaScript from blocks.
|
||||||
* Handles comments for the specified block and any connected value blocks.
|
* Handles comments for the specified block and any connected value blocks.
|
||||||
* Calls any statements following this block.
|
* Calls any statements following this block.
|
||||||
* @param {!Blockly.Block} block The current block.
|
* @param {!Block} block The current block.
|
||||||
* @param {string} code The JavaScript code created for this block.
|
* @param {string} code The JavaScript code created for this block.
|
||||||
* @param {boolean=} opt_thisOnly True to generate code for only this statement.
|
* @param {boolean=} opt_thisOnly True to generate code for only this statement.
|
||||||
* @return {string} JavaScript code with comments and subsequent blocks added.
|
* @return {string} JavaScript code with comments and subsequent blocks added.
|
||||||
* @protected
|
* @protected
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) {
|
JavaScript.scrub_ = function(block, code, opt_thisOnly) {
|
||||||
let commentCode = '';
|
let commentCode = '';
|
||||||
// Only collect comments for blocks that aren't inline.
|
// Only collect comments for blocks that aren't inline.
|
||||||
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
||||||
// Collect comment for this block.
|
// Collect comment for this block.
|
||||||
let comment = block.getCommentText();
|
let comment = block.getCommentText();
|
||||||
if (comment) {
|
if (comment) {
|
||||||
comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
|
comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3);
|
||||||
commentCode += this.prefixLines(comment + '\n', '// ');
|
commentCode += this.prefixLines(comment + '\n', '// ');
|
||||||
}
|
}
|
||||||
// Collect comments for all value arguments.
|
// Collect comments for all value arguments.
|
||||||
// Don't collect comments for nested statements.
|
// Don't collect comments for nested statements.
|
||||||
for (let i = 0; i < block.inputList.length; i++) {
|
for (let i = 0; i < block.inputList.length; i++) {
|
||||||
if (block.inputList[i].type === Blockly.inputTypes.VALUE) {
|
if (block.inputList[i].type === inputTypes.VALUE) {
|
||||||
const childBlock = block.inputList[i].connection.targetBlock();
|
const childBlock = block.inputList[i].connection.targetBlock();
|
||||||
if (childBlock) {
|
if (childBlock) {
|
||||||
comment = this.allNestedComments(childBlock);
|
comment = this.allNestedComments(childBlock);
|
||||||
@@ -262,15 +262,15 @@ Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a property and adjusts the value while taking into account indexing.
|
* Gets a property and adjusts the value while taking into account indexing.
|
||||||
* @param {!Blockly.Block} block The block.
|
* @param {!Block} block The block.
|
||||||
* @param {string} atId The property ID of the element to get.
|
* @param {string} atId The property ID of the element to get.
|
||||||
* @param {number=} opt_delta Value to add.
|
* @param {number=} opt_delta Value to add.
|
||||||
* @param {boolean=} opt_negate Whether to negate the value.
|
* @param {boolean=} opt_negate Whether to negate the value.
|
||||||
* @param {number=} opt_order The highest order acting on this value.
|
* @param {number=} opt_order The highest order acting on this value.
|
||||||
* @return {string|number}
|
* @return {string|number}
|
||||||
*/
|
*/
|
||||||
Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
JavaScript.getAdjusted = function(
|
||||||
opt_order) {
|
block, atId, opt_delta, opt_negate, opt_order) {
|
||||||
let delta = opt_delta || 0;
|
let delta = opt_delta || 0;
|
||||||
let order = opt_order || this.ORDER_NONE;
|
let order = opt_order || this.ORDER_NONE;
|
||||||
if (block.workspace.options.oneBasedIndex) {
|
if (block.workspace.options.oneBasedIndex) {
|
||||||
@@ -293,7 +293,7 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
|||||||
|
|
||||||
let at = this.valueToCode(block, atId, outerOrder) || defaultAtIndex;
|
let at = this.valueToCode(block, atId, outerOrder) || defaultAtIndex;
|
||||||
|
|
||||||
if (Blockly.utils.string.isNumber(at)) {
|
if (stringUtils.isNumber(at)) {
|
||||||
// If the index is a naked number, adjust it right now.
|
// If the index is a naked number, adjust it right now.
|
||||||
at = Number(at) + delta;
|
at = Number(at) + delta;
|
||||||
if (opt_negate) {
|
if (opt_negate) {
|
||||||
@@ -321,3 +321,5 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
|||||||
}
|
}
|
||||||
return at;
|
return at;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports = JavaScript;
|
||||||
|
|||||||
@@ -267,16 +267,16 @@ goog.addDependency('../../core/xml.js', ['Blockly.Xml'], ['Blockly.Events.utils'
|
|||||||
goog.addDependency('../../core/zoom_controls.js', ['Blockly.ZoomControls'], ['Blockly.ComponentManager', 'Blockly.Css', 'Blockly.Events.Click', 'Blockly.Events.utils', 'Blockly.IPositionable', 'Blockly.Touch', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.uiPosition', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'});
|
goog.addDependency('../../core/zoom_controls.js', ['Blockly.ZoomControls'], ['Blockly.ComponentManager', 'Blockly.Css', 'Blockly.Events.Click', 'Blockly.Events.utils', 'Blockly.IPositionable', 'Blockly.Touch', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.uiPosition', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart.js', ['Blockly.Dart'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'});
|
goog.addDependency('../../generators/dart.js', ['Blockly.Dart'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/all.js', ['Blockly.Dart.all'], ['Blockly.Dart.colour', 'Blockly.Dart.lists', 'Blockly.Dart.logic', 'Blockly.Dart.loops', 'Blockly.Dart.math', 'Blockly.Dart.procedures', 'Blockly.Dart.texts', 'Blockly.Dart.variables', 'Blockly.Dart.variablesDynamic'], {'module': 'goog'});
|
goog.addDependency('../../generators/dart/all.js', ['Blockly.Dart.all'], ['Blockly.Dart.colour', 'Blockly.Dart.lists', 'Blockly.Dart.logic', 'Blockly.Dart.loops', 'Blockly.Dart.math', 'Blockly.Dart.procedures', 'Blockly.Dart.texts', 'Blockly.Dart.variables', 'Blockly.Dart.variablesDynamic'], {'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/colour.js', ['Blockly.Dart.colour'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/colour.js', ['Blockly.Dart.colour'], ['Blockly.Dart'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/lists.js', ['Blockly.Dart.lists'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/lists.js', ['Blockly.Dart.lists'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/logic.js', ['Blockly.Dart.logic'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/logic.js', ['Blockly.Dart.logic'], ['Blockly.Dart'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/loops.js', ['Blockly.Dart.loops'], ['Blockly.Dart', 'Blockly.utils.string'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/loops.js', ['Blockly.Dart.loops'], ['Blockly.Dart', 'Blockly.Names', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/math.js', ['Blockly.Dart.math'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/math.js', ['Blockly.Dart.math'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/procedures.js', ['Blockly.Dart.procedures'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/procedures.js', ['Blockly.Dart.procedures'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/text.js', ['Blockly.Dart.texts'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/text.js', ['Blockly.Dart.texts'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/variables.js', ['Blockly.Dart.variables'], ['Blockly.Dart'], {'lang': 'es6'});
|
goog.addDependency('../../generators/dart/variables.js', ['Blockly.Dart.variables'], ['Blockly.Dart', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/dart/variables_dynamic.js', ['Blockly.Dart.variablesDynamic'], ['Blockly.Dart', 'Blockly.Dart.variables']);
|
goog.addDependency('../../generators/dart/variables_dynamic.js', ['Blockly.Dart.variablesDynamic'], ['Blockly.Dart', 'Blockly.Dart.variables'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/javascript.js', ['Blockly.JavaScript'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.global', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'});
|
goog.addDependency('../../generators/javascript.js', ['Blockly.JavaScript'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.global', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/javascript/all.js', ['Blockly.JavaScript.all'], ['Blockly.JavaScript.colour', 'Blockly.JavaScript.lists', 'Blockly.JavaScript.logic', 'Blockly.JavaScript.loops', 'Blockly.JavaScript.math', 'Blockly.JavaScript.procedures', 'Blockly.JavaScript.texts', 'Blockly.JavaScript.variables', 'Blockly.JavaScript.variablesDynamic'], {'module': 'goog'});
|
goog.addDependency('../../generators/javascript/all.js', ['Blockly.JavaScript.all'], ['Blockly.JavaScript.colour', 'Blockly.JavaScript.lists', 'Blockly.JavaScript.logic', 'Blockly.JavaScript.loops', 'Blockly.JavaScript.math', 'Blockly.JavaScript.procedures', 'Blockly.JavaScript.texts', 'Blockly.JavaScript.variables', 'Blockly.JavaScript.variablesDynamic'], {'module': 'goog'});
|
||||||
goog.addDependency('../../generators/javascript/colour.js', ['Blockly.JavaScript.colour'], ['Blockly.JavaScript'], {'lang': 'es6', 'module': 'goog'});
|
goog.addDependency('../../generators/javascript/colour.js', ['Blockly.JavaScript.colour'], ['Blockly.JavaScript'], {'lang': 'es6', 'module': 'goog'});
|
||||||
goog.addDependency('../../generators/javascript/lists.js', ['Blockly.JavaScript.lists'], ['Blockly.JavaScript', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
goog.addDependency('../../generators/javascript/lists.js', ['Blockly.JavaScript.lists'], ['Blockly.JavaScript', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'});
|
||||||
|
|||||||
Reference in New Issue
Block a user