Use Object.values rather than loop

Only for JS, Lua, and PHP.  Dart and Python need extra logic to split out import statements.

Also use ‘this’ instead of fully qualified names in generators.
This commit is contained in:
Neil Fraser
2021-05-21 12:48:03 -07:00
committed by Neil Fraser
parent d919b0af21
commit ecca5bf274
6 changed files with 118 additions and 133 deletions

View File

@@ -505,8 +505,8 @@ Blockly.Generator.prototype.scrub_ = function(_block, code, _opt_thisOnly) {
/** /**
* Hook for code to run at end of code generation. * Hook for code to run at end of code generation.
* Subclasses may override this, e.g. to prepend the generated code with the * Subclasses may override this, e.g. to prepend the generated code with import
* variable definitions. * statements or variable definitions.
* @param {string} code Generated code. * @param {string} code Generated code.
* @return {string} Completed code. * @return {string} Completed code.
*/ */

View File

@@ -86,54 +86,54 @@ Blockly.Dart.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 (!Blockly.Dart.nameDB_) { if (!this.nameDB_) {
Blockly.Dart.nameDB_ = new Blockly.Names(Blockly.Dart.RESERVED_WORDS_); this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
} else { } else {
Blockly.Dart.nameDB_.reset(); this.nameDB_.reset();
} }
Blockly.Dart.nameDB_.setVariableMap(workspace.getVariableMap()); this.nameDB_.setVariableMap(workspace.getVariableMap());
var defvars = []; var defvars = [];
// Add developer variables (not created or named by the user). // Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace); var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) { for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.Dart.nameDB_.getName(devVarList[i], defvars.push(this.nameDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE)); Blockly.Names.DEVELOPER_VARIABLE_TYPE));
} }
// Add user variables, but only ones that are being used. // Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace); var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) { for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.Dart.nameDB_.getName(variables[i].getId(), defvars.push(this.nameDB_.getName(variables[i].getId(),
Blockly.VARIABLE_CATEGORY_NAME)); Blockly.VARIABLE_CATEGORY_NAME));
} }
// Declare all of the variables. // Declare all of the variables.
if (defvars.length) { if (defvars.length) {
Blockly.Dart.definitions_['variables'] = this.definitions_['variables'] =
'var ' + defvars.join(', ') + ';'; 'var ' + defvars.join(', ') + ';';
} }
this.isInitialized = true; this.isInitialized = true;
}; };
/** /**
* Prepend the generated code with the variable definitions. * Prepend the generated code with import statements and variable definitions.
* @param {string} code Generated code. * @param {string} code Generated code.
* @return {string} Completed code. * @return {string} Completed code.
*/ */
Blockly.Dart.finish = function(code) { Blockly.Dart.finish = function(code) {
// Indent every line. // Indent every line.
if (code) { if (code) {
code = Blockly.Dart.prefixLines(code, Blockly.Dart.INDENT); code = this.prefixLines(code, this.INDENT);
} }
code = 'main() {\n' + code + '}'; code = 'main() {\n' + code + '}';
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
var imports = []; var imports = [];
var definitions = []; var definitions = [];
for (var name in Blockly.Dart.definitions_) { for (var name in this.definitions_) {
var def = Blockly.Dart.definitions_[name]; var def = this.definitions_[name];
if (def.match(/^import\s/)) { if (def.match(/^import\s/)) {
imports.push(def); imports.push(def);
} else { } else {
@@ -144,7 +144,7 @@ Blockly.Dart.finish = function(code) {
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;
Blockly.Dart.nameDB_.reset(); this.nameDB_.reset();
var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n'); var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code; return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
}; };
@@ -182,7 +182,7 @@ Blockly.Dart.quote_ = function(string) {
* @protected * @protected
*/ */
Blockly.Dart.multiline_quote_ = function (string) { Blockly.Dart.multiline_quote_ = function (string) {
var lines = string.split(/\n/g).map(Blockly.Dart.quote_); var lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline: // Join with the following, plus a newline:
// + '\n' + // + '\n' +
return lines.join(' + \'\\n\' + \n'); return lines.join(' + \'\\n\' + \n');
@@ -205,13 +205,12 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {
// Collect comment for this block. // Collect comment for this block.
var comment = block.getCommentText(); var comment = block.getCommentText();
if (comment) { if (comment) {
comment = Blockly.utils.string.wrap(comment, comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
Blockly.Dart.COMMENT_WRAP - 3);
if (block.getProcedureDef) { if (block.getProcedureDef) {
// Use documentation comment for function comments. // Use documentation comment for function comments.
commentCode += Blockly.Dart.prefixLines(comment + '\n', '/// '); commentCode += this.prefixLines(comment + '\n', '/// ');
} else { } else {
commentCode += Blockly.Dart.prefixLines(comment + '\n', '// '); commentCode += this.prefixLines(comment + '\n', '// ');
} }
} }
// Collect comments for all value arguments. // Collect comments for all value arguments.
@@ -220,16 +219,16 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {
if (block.inputList[i].type == Blockly.inputTypes.VALUE) { if (block.inputList[i].type == Blockly.inputTypes.VALUE) {
var childBlock = block.inputList[i].connection.targetBlock(); var childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) { if (childBlock) {
comment = Blockly.Dart.allNestedComments(childBlock); comment = this.allNestedComments(childBlock);
if (comment) { if (comment) {
commentCode += Blockly.Dart.prefixLines(comment, '// '); commentCode += this.prefixLines(comment, '// ');
} }
} }
} }
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = opt_thisOnly ? '' : Blockly.Dart.blockToCode(nextBlock); var nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };
@@ -245,19 +244,19 @@ Blockly.Dart.scrub_ = function(block, code, opt_thisOnly) {
Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate, Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
opt_order) { opt_order) {
var delta = opt_delta || 0; var delta = opt_delta || 0;
var order = opt_order || Blockly.Dart.ORDER_NONE; var order = opt_order || this.ORDER_NONE;
if (block.workspace.options.oneBasedIndex) { if (block.workspace.options.oneBasedIndex) {
delta--; delta--;
} }
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0'; var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
if (delta) { if (delta) {
var at = Blockly.Dart.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.Dart.ORDER_ADDITIVE) || defaultAtIndex; this.ORDER_ADDITIVE) || defaultAtIndex;
} else if (opt_negate) { } else if (opt_negate) {
var at = Blockly.Dart.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.Dart.ORDER_UNARY_PREFIX) || defaultAtIndex; this.ORDER_UNARY_PREFIX) || defaultAtIndex;
} else { } else {
var at = Blockly.Dart.valueToCode(block, atId, order) || var at = this.valueToCode(block, atId, order) ||
defaultAtIndex; defaultAtIndex;
} }
@@ -271,10 +270,10 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
// If the index is dynamic, adjust it in code. // If the index is dynamic, adjust it in code.
if (delta > 0) { if (delta > 0) {
at = at + ' + ' + delta; at = at + ' + ' + delta;
var innerOrder = Blockly.Dart.ORDER_ADDITIVE; var innerOrder = this.ORDER_ADDITIVE;
} else if (delta < 0) { } else if (delta < 0) {
at = at + ' - ' + -delta; at = at + ' - ' + -delta;
var innerOrder = Blockly.Dart.ORDER_ADDITIVE; var innerOrder = this.ORDER_ADDITIVE;
} }
if (opt_negate) { if (opt_negate) {
if (delta) { if (delta) {
@@ -282,7 +281,7 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
} else { } else {
at = '-' + at; at = '-' + at;
} }
var innerOrder = Blockly.Dart.ORDER_UNARY_PREFIX; var innerOrder = this.ORDER_UNARY_PREFIX;
} }
innerOrder = Math.floor(innerOrder); innerOrder = Math.floor(innerOrder);
order = Math.floor(order); order = Math.floor(order);

View File

@@ -15,6 +15,7 @@ goog.provide('Blockly.JavaScript');
goog.require('Blockly.Generator'); goog.require('Blockly.Generator');
goog.require('Blockly.inputTypes'); goog.require('Blockly.inputTypes');
goog.require('Blockly.utils.global'); goog.require('Blockly.utils.global');
goog.require('Blockly.utils.object');
goog.require('Blockly.utils.string'); goog.require('Blockly.utils.string');
@@ -128,34 +129,32 @@ Blockly.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 (!Blockly.JavaScript.nameDB_) { if (!this.nameDB_) {
Blockly.JavaScript.nameDB_ = this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
new Blockly.Names(Blockly.JavaScript.RESERVED_WORDS_);
} else { } else {
Blockly.JavaScript.nameDB_.reset(); this.nameDB_.reset();
} }
Blockly.JavaScript.nameDB_.setVariableMap(workspace.getVariableMap()); this.nameDB_.setVariableMap(workspace.getVariableMap());
var defvars = []; var defvars = [];
// Add developer variables (not created or named by the user). // Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace); var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) { for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.JavaScript.nameDB_.getName(devVarList[i], defvars.push(this.nameDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE)); Blockly.Names.DEVELOPER_VARIABLE_TYPE));
} }
// Add user variables, but only ones that are being used. // Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace); var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) { for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.JavaScript.nameDB_.getName(variables[i].getId(), defvars.push(this.nameDB_.getName(variables[i].getId(),
Blockly.VARIABLE_CATEGORY_NAME)); Blockly.VARIABLE_CATEGORY_NAME));
} }
// Declare all of the variables. // Declare all of the variables.
if (defvars.length) { if (defvars.length) {
Blockly.JavaScript.definitions_['variables'] = this.definitions_['variables'] = 'var ' + defvars.join(', ') + ';';
'var ' + defvars.join(', ') + ';';
} }
this.isInitialized = true; this.isInitialized = true;
}; };
@@ -167,15 +166,12 @@ Blockly.JavaScript.init = function(workspace) {
*/ */
Blockly.JavaScript.finish = function(code) { Blockly.JavaScript.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
var definitions = []; var definitions = Blockly.utils.object.values(this.definitions_);
for (var name in Blockly.JavaScript.definitions_) {
definitions.push(Blockly.JavaScript.definitions_[name]);
}
// 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;
Blockly.JavaScript.nameDB_.reset(); this.nameDB_.reset();
return definitions.join('\n\n') + '\n\n\n' + code; return definitions.join('\n\n') + '\n\n\n' + code;
}; };
@@ -215,7 +211,7 @@ Blockly.JavaScript.quote_ = function(string) {
Blockly.JavaScript.multiline_quote_ = function(string) { Blockly.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.
var lines = string.split(/\n/g).map(Blockly.JavaScript.quote_); var lines = string.split(/\n/g).map(this.quote_);
return lines.join(' + \'\\n\' +\n'); return lines.join(' + \'\\n\' +\n');
}; };
@@ -236,9 +232,8 @@ Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) {
// Collect comment for this block. // Collect comment for this block.
var comment = block.getCommentText(); var comment = block.getCommentText();
if (comment) { if (comment) {
comment = Blockly.utils.string.wrap(comment, comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
Blockly.JavaScript.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment + '\n', '// ');
commentCode += Blockly.JavaScript.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.
@@ -246,16 +241,16 @@ Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) {
if (block.inputList[i].type == Blockly.inputTypes.VALUE) { if (block.inputList[i].type == Blockly.inputTypes.VALUE) {
var childBlock = block.inputList[i].connection.targetBlock(); var childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) { if (childBlock) {
comment = Blockly.JavaScript.allNestedComments(childBlock); comment = this.allNestedComments(childBlock);
if (comment) { if (comment) {
commentCode += Blockly.JavaScript.prefixLines(comment, '// '); commentCode += this.prefixLines(comment, '// ');
} }
} }
} }
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = opt_thisOnly ? '' : Blockly.JavaScript.blockToCode(nextBlock); var nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };
@@ -271,23 +266,22 @@ Blockly.JavaScript.scrub_ = function(block, code, opt_thisOnly) {
Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate, Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
opt_order) { opt_order) {
var delta = opt_delta || 0; var delta = opt_delta || 0;
var order = opt_order || Blockly.JavaScript.ORDER_NONE; var order = opt_order || this.ORDER_NONE;
if (block.workspace.options.oneBasedIndex) { if (block.workspace.options.oneBasedIndex) {
delta--; delta--;
} }
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0'; var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
if (delta > 0) { if (delta > 0) {
var at = Blockly.JavaScript.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.JavaScript.ORDER_ADDITION) || defaultAtIndex; this.ORDER_ADDITION) || defaultAtIndex;
} else if (delta < 0) { } else if (delta < 0) {
var at = Blockly.JavaScript.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.JavaScript.ORDER_SUBTRACTION) || defaultAtIndex; this.ORDER_SUBTRACTION) || defaultAtIndex;
} else if (opt_negate) { } else if (opt_negate) {
var at = Blockly.JavaScript.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.JavaScript.ORDER_UNARY_NEGATION) || defaultAtIndex; this.ORDER_UNARY_NEGATION) || defaultAtIndex;
} else { } else {
var at = Blockly.JavaScript.valueToCode(block, atId, order) || var at = this.valueToCode(block, atId, order) || defaultAtIndex;
defaultAtIndex;
} }
if (Blockly.isNumber(at)) { if (Blockly.isNumber(at)) {
@@ -300,10 +294,10 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
// If the index is dynamic, adjust it in code. // If the index is dynamic, adjust it in code.
if (delta > 0) { if (delta > 0) {
at = at + ' + ' + delta; at = at + ' + ' + delta;
var innerOrder = Blockly.JavaScript.ORDER_ADDITION; var innerOrder = this.ORDER_ADDITION;
} else if (delta < 0) { } else if (delta < 0) {
at = at + ' - ' + -delta; at = at + ' - ' + -delta;
var innerOrder = Blockly.JavaScript.ORDER_SUBTRACTION; var innerOrder = this.ORDER_SUBTRACTION;
} }
if (opt_negate) { if (opt_negate) {
if (delta) { if (delta) {
@@ -311,7 +305,7 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
} else { } else {
at = '-' + at; at = '-' + at;
} }
var innerOrder = Blockly.JavaScript.ORDER_UNARY_NEGATION; var innerOrder = this.ORDER_UNARY_NEGATION;
} }
innerOrder = Math.floor(innerOrder); innerOrder = Math.floor(innerOrder);
order = Math.floor(order); order = Math.floor(order);

View File

@@ -15,6 +15,7 @@ goog.provide('Blockly.Lua');
goog.require('Blockly.Generator'); goog.require('Blockly.Generator');
goog.require('Blockly.inputTypes'); goog.require('Blockly.inputTypes');
goog.require('Blockly.utils.object');
goog.require('Blockly.utils.string'); goog.require('Blockly.utils.string');
@@ -94,12 +95,12 @@ Blockly.Lua.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 (!Blockly.Lua.nameDB_) { if (!this.nameDB_) {
Blockly.Lua.nameDB_ = new Blockly.Names(Blockly.Lua.RESERVED_WORDS_); this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
} else { } else {
Blockly.Lua.nameDB_.reset(); this.nameDB_.reset();
} }
Blockly.Lua.nameDB_.setVariableMap(workspace.getVariableMap()); this.nameDB_.setVariableMap(workspace.getVariableMap());
this.isInitialized = true; this.isInitialized = true;
}; };
@@ -110,15 +111,12 @@ Blockly.Lua.init = function(workspace) {
*/ */
Blockly.Lua.finish = function(code) { Blockly.Lua.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
var definitions = []; var definitions = Blockly.utils.object.values(this.definitions_);
for (var name in Blockly.Lua.definitions_) {
definitions.push(Blockly.Lua.definitions_[name]);
}
// 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;
Blockly.Lua.nameDB_.reset(); this.nameDB_.reset();
return definitions.join('\n\n') + '\n\n\n' + code; return definitions.join('\n\n') + '\n\n\n' + code;
}; };
@@ -156,7 +154,7 @@ Blockly.Lua.quote_ = function(string) {
* @protected * @protected
*/ */
Blockly.Lua.multiline_quote_ = function(string) { Blockly.Lua.multiline_quote_ = function(string) {
var lines = string.split(/\n/g).map(Blockly.Lua.quote_); var lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline: // Join with the following, plus a newline:
// .. '\n' .. // .. '\n' ..
return lines.join(' .. \'\\n\' ..\n'); return lines.join(' .. \'\\n\' ..\n');
@@ -179,9 +177,8 @@ Blockly.Lua.scrub_ = function(block, code, opt_thisOnly) {
// Collect comment for this block. // Collect comment for this block.
var comment = block.getCommentText(); var comment = block.getCommentText();
if (comment) { if (comment) {
comment = Blockly.utils.string.wrap(comment, comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
Blockly.Lua.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment, '-- ') + '\n';
commentCode += Blockly.Lua.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.
@@ -189,15 +186,15 @@ Blockly.Lua.scrub_ = function(block, code, opt_thisOnly) {
if (block.inputList[i].type == Blockly.inputTypes.VALUE) { if (block.inputList[i].type == Blockly.inputTypes.VALUE) {
var childBlock = block.inputList[i].connection.targetBlock(); var childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) { if (childBlock) {
comment = Blockly.Lua.allNestedComments(childBlock); comment = this.allNestedComments(childBlock);
if (comment) { if (comment) {
commentCode += Blockly.Lua.prefixLines(comment, '-- '); commentCode += this.prefixLines(comment, '-- ');
} }
} }
} }
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = opt_thisOnly ? '' : Blockly.Lua.blockToCode(nextBlock); var nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };

View File

@@ -14,6 +14,7 @@ goog.provide('Blockly.PHP');
goog.require('Blockly.Generator'); goog.require('Blockly.Generator');
goog.require('Blockly.inputTypes'); goog.require('Blockly.inputTypes');
goog.require('Blockly.utils.object');
goog.require('Blockly.utils.string'); goog.require('Blockly.utils.string');
@@ -132,31 +133,31 @@ Blockly.PHP.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 (!Blockly.PHP.nameDB_) { if (!this.nameDB_) {
Blockly.PHP.nameDB_ = new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, '$'); this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_, '$');
} else { } else {
Blockly.PHP.nameDB_.reset(); this.nameDB_.reset();
} }
Blockly.PHP.nameDB_.setVariableMap(workspace.getVariableMap()); this.nameDB_.setVariableMap(workspace.getVariableMap());
var defvars = []; var defvars = [];
// Add developer variables (not created or named by the user). // Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace); var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) { for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.PHP.nameDB_.getName(devVarList[i], defvars.push(this.nameDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ';'); Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ';');
} }
// Add user variables, but only ones that are being used. // Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace); var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0, variable; variable = variables[i]; i++) { for (var i = 0, variable; (variable = variables[i]); i++) {
defvars.push(Blockly.PHP.nameDB_.getName(variable.getId(), defvars.push(this.nameDB_.getName(variable.getId(),
Blockly.VARIABLE_CATEGORY_NAME) + ';'); Blockly.VARIABLE_CATEGORY_NAME) + ';');
} }
// Declare all of the variables. // Declare all of the variables.
Blockly.PHP.definitions_['variables'] = defvars.join('\n'); this.definitions_['variables'] = defvars.join('\n');
this.isInitialized = true; this.isInitialized = true;
}; };
@@ -167,15 +168,12 @@ Blockly.PHP.init = function(workspace) {
*/ */
Blockly.PHP.finish = function(code) { Blockly.PHP.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
var definitions = []; var definitions = Blockly.utils.object.values(this.definitions_);
for (var name in Blockly.PHP.definitions_) {
definitions.push(Blockly.PHP.definitions_[name]);
}
// 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;
Blockly.PHP.nameDB_.reset(); this.nameDB_.reset();
return definitions.join('\n\n') + '\n\n\n' + code; return definitions.join('\n\n') + '\n\n\n' + code;
}; };
@@ -211,7 +209,7 @@ Blockly.PHP.quote_ = function(string) {
* @protected * @protected
*/ */
Blockly.PHP.multiline_quote_ = function (string) { Blockly.PHP.multiline_quote_ = function (string) {
var lines = string.split(/\n/g).map(Blockly.PHP.quote_); var lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline: // Join with the following, plus a newline:
// . "\n" . // . "\n" .
// Newline escaping only works in double-quoted strings. // Newline escaping only works in double-quoted strings.
@@ -235,9 +233,8 @@ Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) {
// Collect comment for this block. // Collect comment for this block.
var comment = block.getCommentText(); var comment = block.getCommentText();
if (comment) { if (comment) {
comment = Blockly.utils.string.wrap(comment, comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
Blockly.PHP.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment, '// ') + '\n';
commentCode += Blockly.PHP.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.
@@ -245,16 +242,16 @@ Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) {
if (block.inputList[i].type == Blockly.inputTypes.VALUE) { if (block.inputList[i].type == Blockly.inputTypes.VALUE) {
var childBlock = block.inputList[i].connection.targetBlock(); var childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) { if (childBlock) {
comment = Blockly.PHP.allNestedComments(childBlock); comment = this.allNestedComments(childBlock);
if (comment) { if (comment) {
commentCode += Blockly.PHP.prefixLines(comment, '// '); commentCode += this.prefixLines(comment, '// ');
} }
} }
} }
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = opt_thisOnly ? '' : Blockly.PHP.blockToCode(nextBlock); var nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };
@@ -270,22 +267,22 @@ Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) {
Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate, Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
opt_order) { opt_order) {
var delta = opt_delta || 0; var delta = opt_delta || 0;
var order = opt_order || Blockly.PHP.ORDER_NONE; var order = opt_order || this.ORDER_NONE;
if (block.workspace.options.oneBasedIndex) { if (block.workspace.options.oneBasedIndex) {
delta--; delta--;
} }
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0'; var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
if (delta > 0) { if (delta > 0) {
var at = Blockly.PHP.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.PHP.ORDER_ADDITION) || defaultAtIndex; this.ORDER_ADDITION) || defaultAtIndex;
} else if (delta < 0) { } else if (delta < 0) {
var at = Blockly.PHP.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.PHP.ORDER_SUBTRACTION) || defaultAtIndex; this.ORDER_SUBTRACTION) || defaultAtIndex;
} else if (opt_negate) { } else if (opt_negate) {
var at = Blockly.PHP.valueToCode(block, atId, var at = this.valueToCode(block, atId,
Blockly.PHP.ORDER_UNARY_NEGATION) || defaultAtIndex; this.ORDER_UNARY_NEGATION) || defaultAtIndex;
} else { } else {
var at = Blockly.PHP.valueToCode(block, atId, order) || var at = this.valueToCode(block, atId, order) ||
defaultAtIndex; defaultAtIndex;
} }
@@ -299,10 +296,10 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
// If the index is dynamic, adjust it in code. // If the index is dynamic, adjust it in code.
if (delta > 0) { if (delta > 0) {
at = at + ' + ' + delta; at = at + ' + ' + delta;
var innerOrder = Blockly.PHP.ORDER_ADDITION; var innerOrder = this.ORDER_ADDITION;
} else if (delta < 0) { } else if (delta < 0) {
at = at + ' - ' + -delta; at = at + ' - ' + -delta;
var innerOrder = Blockly.PHP.ORDER_SUBTRACTION; var innerOrder = this.ORDER_SUBTRACTION;
} }
if (opt_negate) { if (opt_negate) {
if (delta) { if (delta) {
@@ -310,7 +307,7 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
} else { } else {
at = '-' + at; at = '-' + at;
} }
var innerOrder = Blockly.PHP.ORDER_UNARY_NEGATION; var innerOrder = this.ORDER_UNARY_NEGATION;
} }
innerOrder = Math.floor(innerOrder); innerOrder = Math.floor(innerOrder);
order = Math.floor(order); order = Math.floor(order);

View File

@@ -144,37 +144,37 @@ Blockly.Python.init = function(workspace) {
/** /**
* Empty loops or conditionals are not allowed in Python. * Empty loops or conditionals are not allowed in Python.
*/ */
Blockly.Python.PASS = this.INDENT + 'pass\n'; this.PASS = this.INDENT + 'pass\n';
if (!Blockly.Python.nameDB_) { if (!this.nameDB_) {
Blockly.Python.nameDB_ = new Blockly.Names(Blockly.Python.RESERVED_WORDS_); this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_);
} else { } else {
Blockly.Python.nameDB_.reset(); this.nameDB_.reset();
} }
Blockly.Python.nameDB_.setVariableMap(workspace.getVariableMap()); this.nameDB_.setVariableMap(workspace.getVariableMap());
var defvars = []; var defvars = [];
// Add developer variables (not created or named by the user). // Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace); var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) { for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.Python.nameDB_.getName(devVarList[i], defvars.push(this.nameDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None'); Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None');
} }
// Add user variables, but only ones that are being used. // Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace); var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) { for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.Python.nameDB_.getName(variables[i].getId(), defvars.push(this.nameDB_.getName(variables[i].getId(),
Blockly.VARIABLE_CATEGORY_NAME) + ' = None'); Blockly.VARIABLE_CATEGORY_NAME) + ' = None');
} }
Blockly.Python.definitions_['variables'] = defvars.join('\n'); this.definitions_['variables'] = defvars.join('\n');
this.isInitialized = true; this.isInitialized = true;
}; };
/** /**
* Prepend the generated code with the variable definitions. * Prepend the generated code with import statements and variable definitions.
* @param {string} code Generated code. * @param {string} code Generated code.
* @return {string} Completed code. * @return {string} Completed code.
*/ */
@@ -182,8 +182,8 @@ Blockly.Python.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
var imports = []; var imports = [];
var definitions = []; var definitions = [];
for (var name in Blockly.Python.definitions_) { for (var name in this.definitions_) {
var def = Blockly.Python.definitions_[name]; var def = this.definitions_[name];
if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) { if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
imports.push(def); imports.push(def);
} else { } else {
@@ -194,7 +194,7 @@ Blockly.Python.finish = function(code) {
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;
Blockly.Python.nameDB_.reset(); this.nameDB_.reset();
var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n'); var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code; return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
}; };
@@ -240,7 +240,7 @@ Blockly.Python.quote_ = function(string) {
* @protected * @protected
*/ */
Blockly.Python.multiline_quote_ = function(string) { Blockly.Python.multiline_quote_ = function(string) {
var lines = string.split(/\n/g).map(Blockly.Python.quote_); var lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline: // Join with the following, plus a newline:
// + '\n' + // + '\n' +
return lines.join(' + \'\\n\' + \n'); return lines.join(' + \'\\n\' + \n');
@@ -263,9 +263,8 @@ Blockly.Python.scrub_ = function(block, code, opt_thisOnly) {
// Collect comment for this block. // Collect comment for this block.
var comment = block.getCommentText(); var comment = block.getCommentText();
if (comment) { if (comment) {
comment = Blockly.utils.string.wrap(comment, comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
Blockly.Python.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment + '\n', '# ');
commentCode += Blockly.Python.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.
@@ -273,16 +272,16 @@ Blockly.Python.scrub_ = function(block, code, opt_thisOnly) {
if (block.inputList[i].type == Blockly.inputTypes.VALUE) { if (block.inputList[i].type == Blockly.inputTypes.VALUE) {
var childBlock = block.inputList[i].connection.targetBlock(); var childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) { if (childBlock) {
comment = Blockly.Python.allNestedComments(childBlock); comment = this.allNestedComments(childBlock);
if (comment) { if (comment) {
commentCode += Blockly.Python.prefixLines(comment, '# '); commentCode += this.prefixLines(comment, '# ');
} }
} }
} }
} }
} }
var nextBlock = block.nextConnection && block.nextConnection.targetBlock(); var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = opt_thisOnly ? '' : Blockly.Python.blockToCode(nextBlock); var nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
return commentCode + code + nextCode; return commentCode + code + nextCode;
}; };
@@ -301,9 +300,8 @@ Blockly.Python.getAdjustedInt = function(block, atId, opt_delta, opt_negate) {
delta--; delta--;
} }
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0'; var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
var atOrder = delta ? Blockly.Python.ORDER_ADDITIVE : var atOrder = delta ? this.ORDER_ADDITIVE : this.ORDER_NONE;
Blockly.Python.ORDER_NONE; var at = this.valueToCode(block, atId, atOrder) || defaultAtIndex;
var at = Blockly.Python.valueToCode(block, atId, atOrder) || defaultAtIndex;
if (Blockly.isNumber(at)) { if (Blockly.isNumber(at)) {
// If the index is a naked number, adjust it right now. // If the index is a naked number, adjust it right now.