Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b7d4117
refactor(generators): Migrate generators/javascript.js to goog.module
cpcallen Nov 16, 2021
371ffee
chore: migrate generators/javascript.js to named requires
rachel-fenichel Nov 17, 2021
7028bc0
chore: migrate generators/javascript/colour.js to named requires
rachel-fenichel Nov 17, 2021
b2312f3
chore: migrate generators/javascript/lists.js to named requires
rachel-fenichel Nov 17, 2021
e9cec6f
chore: migrate generators/javascript/logic.js to named requires
rachel-fenichel Nov 17, 2021
fe39649
chore: migrate generators/javascript/loops.js to named requires
rachel-fenichel Nov 17, 2021
796eaa4
chore: migrate generators/javascript/math.js to named requires
rachel-fenichel Nov 17, 2021
52e3e6f
chore: migrate generators/javascript/procedures.js to named requires
rachel-fenichel Nov 17, 2021
0926758
chore: migrate generators/javascript/text.js to named requires
rachel-fenichel Nov 17, 2021
2f549a1
chore: migrate generators/javascript/variables and variables_dynamic …
rachel-fenichel Nov 17, 2021
e2c7c4e
chore: migrate dart block generators to goog.module
rachel-fenichel Nov 17, 2021
4ca5700
chore: migrate dart generator and block generators to named imports
rachel-fenichel Nov 17, 2021
169ef06
chore: migrate lua generator and block generators to goog.module
rachel-fenichel Nov 17, 2021
d29846b
chore: migrate lua generator and block generators to named imports
rachel-fenichel Nov 17, 2021
f58a957
chore: migrate lua generator and block generators to goog.module
rachel-fenichel Nov 17, 2021
8625c36
chore: migrate php generator and block generators to named imports
rachel-fenichel Nov 17, 2021
ee96698
chore: migrate python generator and block generators to goog.module
rachel-fenichel Nov 17, 2021
546cc27
chore: migrate python generator and block generators to named imports
rachel-fenichel Nov 17, 2021
a537fa1
chore: add some externs for generator use
rachel-fenichel Nov 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions externs/generator-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ goog.provide('Blockly.inputTypes');
goog.provide('Blockly.utils.global');
goog.provide('Blockly.utils.object');
goog.provide('Blockly.utils.string');
goog.provide('Blockly.Names');
goog.provide('Blockly.Variables');
goog.provide('Blockly.Workspace');
goog.provide('Blockly.Block');
goog.provide('Blockly.Constants.Loops');

var Blockly;
92 changes: 50 additions & 42 deletions generators/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
*/
'use strict';

goog.provide('Blockly.Dart');
goog.module('Blockly.Dart');
goog.module.declareLegacyNamespace();

goog.require('Blockly.Generator');
goog.require('Blockly.inputTypes');
goog.require('Blockly.utils.string');
const Blockly = goog.require('Blockly');
const {Generator} = goog.require('Blockly.Generator');
const {inputTypes} = goog.require('Blockly.inputTypes');
const stringUtils = goog.require('Blockly.utils.string');
const {Names} = goog.require('Blockly.Names');
const Variables = goog.require('Blockly.Variables');
const {Workspace} = goog.requireType('Blockly.Workspace');
const {Block} = goog.requireType('Blockly.Block');


/**
* Dart code generator.
* @type {!Blockly.Generator}
* @type {!Generator}
*/
Blockly.Dart = new Blockly.Generator('Dart');
const Dart = new Generator('Dart');

/**
* List of illegal variable names.
Expand All @@ -29,7 +35,7 @@ Blockly.Dart = new Blockly.Generator('Dart');
* accidentally clobbering a built-in object or function.
* @private
*/
Blockly.Dart.addReservedWords(
Dart.addReservedWords(
// https://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf
// Section 16.1.1
'assert,break,case,catch,class,const,continue,default,do,else,enum,' +
Expand All @@ -52,41 +58,41 @@ Blockly.Dart.addReservedWords(
* Order of operation ENUMs.
* https://dart.dev/guides/language/language-tour#operators
*/
Blockly.Dart.ORDER_ATOMIC = 0; // 0 "" ...
Blockly.Dart.ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] . ?.
Blockly.Dart.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
Blockly.Dart.ORDER_MULTIPLICATIVE = 3; // * / % ~/
Blockly.Dart.ORDER_ADDITIVE = 4; // + -
Blockly.Dart.ORDER_SHIFT = 5; // << >>
Blockly.Dart.ORDER_BITWISE_AND = 6; // &
Blockly.Dart.ORDER_BITWISE_XOR = 7; // ^
Blockly.Dart.ORDER_BITWISE_OR = 8; // |
Blockly.Dart.ORDER_RELATIONAL = 9; // >= > <= < as is is!
Blockly.Dart.ORDER_EQUALITY = 10; // == !=
Blockly.Dart.ORDER_LOGICAL_AND = 11; // &&
Blockly.Dart.ORDER_LOGICAL_OR = 12; // ||
Blockly.Dart.ORDER_IF_NULL = 13; // ??
Blockly.Dart.ORDER_CONDITIONAL = 14; // expr ? expr : expr
Blockly.Dart.ORDER_CASCADE = 15; // ..
Blockly.Dart.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
Blockly.Dart.ORDER_NONE = 99; // (...)
Dart.ORDER_ATOMIC = 0; // 0 "" ...
Dart.ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] . ?.
Dart.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
Dart.ORDER_MULTIPLICATIVE = 3; // * / % ~/
Dart.ORDER_ADDITIVE = 4; // + -
Dart.ORDER_SHIFT = 5; // << >>
Dart.ORDER_BITWISE_AND = 6; // &
Dart.ORDER_BITWISE_XOR = 7; // ^
Dart.ORDER_BITWISE_OR = 8; // |
Dart.ORDER_RELATIONAL = 9; // >= > <= < as is is!
Dart.ORDER_EQUALITY = 10; // == !=
Dart.ORDER_LOGICAL_AND = 11; // &&
Dart.ORDER_LOGICAL_OR = 12; // ||
Dart.ORDER_IF_NULL = 13; // ??
Dart.ORDER_CONDITIONAL = 14; // expr ? expr : expr
Dart.ORDER_CASCADE = 15; // ..
Dart.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
Dart.ORDER_NONE = 99; // (...)

/**
* Whether the init method has been called.
* @type {?boolean}
*/
Blockly.Dart.isInitialized = false;
Dart.isInitialized = false;

/**
* Initialise the database of variable names.
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
* @param {!Workspace} workspace Workspace to generate code from.
*/
Blockly.Dart.init = function(workspace) {
Dart.init = function(workspace) {
// Call Blockly.Generator's init.
Object.getPrototypeOf(this).init.call(this);

if (!this.nameDB_) {
this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
this.nameDB_ = new Names(this.RESERVED_WORDS_);
} else {
this.nameDB_.reset();
}
Expand All @@ -97,14 +103,14 @@ Blockly.Dart.init = function(workspace) {

const defvars = [];
// 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++) {
defvars.push(this.nameDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
Names.DEVELOPER_VARIABLE_TYPE));
}

// 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++) {
defvars.push(this.nameDB_.getName(variables[i].getId(),
Blockly.VARIABLE_CATEGORY_NAME));
Expand All @@ -123,7 +129,7 @@ Blockly.Dart.init = function(workspace) {
* @param {string} code Generated code.
* @return {string} Completed code.
*/
Blockly.Dart.finish = function(code) {
Dart.finish = function(code) {
// Indent every line.
if (code) {
code = this.prefixLines(code, this.INDENT);
Expand Down Expand Up @@ -156,7 +162,7 @@ Blockly.Dart.finish = function(code) {
* @param {string} line Line of generated code.
* @return {string} Legal line of code.
*/
Blockly.Dart.scrubNakedValue = function(line) {
Dart.scrubNakedValue = function(line) {
return line + ';\n';
};

Expand All @@ -166,7 +172,7 @@ Blockly.Dart.scrubNakedValue = function(line) {
* @return {string} Dart string.
* @protected
*/
Blockly.Dart.quote_ = function(string) {
Dart.quote_ = function(string) {
// Can't use goog.string.quote since $ must also be escaped.
string = string.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\\n')
Expand All @@ -182,7 +188,7 @@ Blockly.Dart.quote_ = function(string) {
* @return {string} Dart string.
* @protected
*/
Blockly.Dart.multiline_quote_ = function (string) {
Dart.multiline_quote_ = function (string) {
const lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline:
// + '\n' +
Expand All @@ -193,20 +199,20 @@ Blockly.Dart.multiline_quote_ = function (string) {
* Common tasks for generating Dart from blocks.
* Handles comments for the specified block and any connected value blocks.
* Calls any statements following this block.
* @param {!Blockly.Block} block The current block.
* @param {!Block} block The current block.
* @param {string} code The Dart code created for this block.
* @param {boolean=} opt_thisOnly True to generate code for only this statement.
* @return {string} Dart code with comments and subsequent blocks added.
* @protected
*/
Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {
Dart.scrub_ = function(block, code, opt_thisOnly) {
let commentCode = '';
// Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) {
// Collect comment for this block.
let comment = block.getCommentText();
if (comment) {
comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3);
if (block.getProcedureDef) {
// Use documentation comment for function comments.
commentCode += this.prefixLines(comment + '\n', '/// ');
Expand All @@ -217,7 +223,7 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {
// Collect comments for all value arguments.
// Don't collect comments for nested statements.
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();
if (childBlock) {
comment = this.allNestedComments(childBlock);
Expand All @@ -235,14 +241,14 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {

/**
* 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 {number=} opt_delta Value to add.
* @param {boolean=} opt_negate Whether to negate the value.
* @param {number=} opt_order The highest order acting on this value.
* @return {string|number}
*/
Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
opt_order) {
let delta = opt_delta || 0;
let order = opt_order || this.ORDER_NONE;
Expand Down Expand Up @@ -295,3 +301,5 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
}
return at;
};

exports = Dart;
66 changes: 33 additions & 33 deletions generators/dart/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,50 @@
*/
'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.
const code = Blockly.Dart.quote_(block.getFieldValue('COLOUR'));
return [code, Blockly.Dart.ORDER_ATOMIC];
const code = Dart.quote_(block.getFieldValue('COLOUR'));
return [code, Dart.ORDER_ATOMIC];
};

Blockly.Dart['colour_random'] = function(block) {
Dart['colour_random'] = function(block) {
// Generate a random colour.
Blockly.Dart.definitions_['import_dart_math'] =
Dart.definitions_['import_dart_math'] =
'import \'dart:math\' as Math;';
const functionName = Blockly.Dart.provideFunction_(
const functionName = Dart.provideFunction_(
'colour_random',
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
' String hex = \'0123456789abcdef\';',
' var rnd = new Math.Random();',
' return \'#${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\';',
'}']);
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.
const red = Blockly.Dart.valueToCode(block, 'RED',
Blockly.Dart.ORDER_NONE) || 0;
const green = Blockly.Dart.valueToCode(block, 'GREEN',
Blockly.Dart.ORDER_NONE) || 0;
const blue = Blockly.Dart.valueToCode(block, 'BLUE',
Blockly.Dart.ORDER_NONE) || 0;
const red = Dart.valueToCode(block, 'RED',
Dart.ORDER_NONE) || 0;
const green = Dart.valueToCode(block, 'GREEN',
Dart.ORDER_NONE) || 0;
const blue = Dart.valueToCode(block, 'BLUE',
Dart.ORDER_NONE) || 0;

Blockly.Dart.definitions_['import_dart_math'] =
Dart.definitions_['import_dart_math'] =
'import \'dart:math\' as Math;';
const functionName = Blockly.Dart.provideFunction_(
const functionName = Dart.provideFunction_(
'colour_rgb',
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
'(num r, num g, num b) {',
' num rn = (Math.max(Math.min(r, 100), 0) * 2.55).round();',
' String rs = rn.toInt().toRadixString(16);',
Expand All @@ -69,23 +69,23 @@ Blockly.Dart['colour_rgb'] = function(block) {
' return \'#$rs$gs$bs\';',
'}']);
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.
const c1 = Blockly.Dart.valueToCode(block, 'COLOUR1',
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
const c2 = Blockly.Dart.valueToCode(block, 'COLOUR2',
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
const ratio = Blockly.Dart.valueToCode(block, 'RATIO',
Blockly.Dart.ORDER_NONE) || 0.5;
const c1 = Dart.valueToCode(block, 'COLOUR1',
Dart.ORDER_NONE) || '\'#000000\'';
const c2 = Dart.valueToCode(block, 'COLOUR2',
Dart.ORDER_NONE) || '\'#000000\'';
const ratio = Dart.valueToCode(block, 'RATIO',
Dart.ORDER_NONE) || 0.5;

Blockly.Dart.definitions_['import_dart_math'] =
Dart.definitions_['import_dart_math'] =
'import \'dart:math\' as Math;';
const functionName = Blockly.Dart.provideFunction_(
const functionName = Dart.provideFunction_(
'colour_blend',
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
['String ' + Dart.FUNCTION_NAME_PLACEHOLDER_ +
'(String c1, String c2, num ratio) {',
' ratio = Math.max(Math.min(ratio, 1), 0);',
' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');',
Expand All @@ -109,5 +109,5 @@ Blockly.Dart['colour_blend'] = function(block) {
' return \'#$rs$gs$bs\';',
'}']);
const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
return [code, Dart.ORDER_UNARY_POSTFIX];
};
Loading