Skip to content

Commit

Permalink
Generalize PHP $ hack for variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilFraser committed Jul 3, 2015
1 parent d819db2 commit d8a72df
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions blockly_compressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions core/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ goog.provide('Blockly.Names');
* Class for a database of entity names (variables, functions, etc).
* @param {string} reservedWords A comma-separated string of words that are
* illegal for use as names in a language (e.g. 'new,if,this,...').
* @param {string=} opt_variablePrefix Some languages need a '$' or a namespace
* before all variable names.
* @constructor
* @param namesPrependDollar boolean indicating whether the languages requires dollar signs ($) before a variable
*/
Blockly.Names = function(reservedWords, namesPrependDollar) {
this.prependDollar = namesPrependDollar || false;
Blockly.Names = function(reservedWords, opt_variablePrefix) {
this.variablePrefix_ = opt_variablePrefix || '';
this.reservedDict_ = Object.create(null);
if (reservedWords) {
var splitWords = reservedWords.split(',');
for (var x = 0; x < splitWords.length; x++) {
this.reservedDict_[splitWords[x]] = true;
for (var i = 0; i < splitWords.length; i++) {
this.reservedDict_[splitWords[i]] = true;
}
}
this.reset();
Expand Down Expand Up @@ -72,12 +73,13 @@ Blockly.Names.prototype.reset = function() {
*/
Blockly.Names.prototype.getName = function(name, type) {
var normalized = name.toLowerCase() + '_' + type;
var prepend = type=='VARIABLE' && this.prependDollar ? '$' : '';
var prefix = (type == Blockly.Variables.NAME_TYPE) ?
this.variablePrefix_ : '';
if (normalized in this.db_) {
return prepend + this.db_[normalized];
return prefix + this.db_[normalized];
}
var safeName = this.getDistinctName(name, type);
this.db_[normalized] = type=='VARIABLE' && this.prependDollar ? safeName.substr(1) : safeName;
this.db_[normalized] = safeName.substr(prefix.length);
return safeName;
};

Expand All @@ -101,8 +103,9 @@ Blockly.Names.prototype.getDistinctName = function(name, type) {
}
safeName += i;
this.dbReverse_[safeName] = true;
var prepend = type=='VARIABLE' && this.prependDollar ? '$' : '';
return prepend + safeName;
var prefix = (type == Blockly.Variables.NAME_TYPE) ?
this.variablePrefix_ : '';
return prefix + safeName;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions generators/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ Blockly.Dart.init = function(workspace) {

var defvars = [];
var variables = Blockly.Variables.allVariables(workspace);
for (var x = 0; x < variables.length; x++) {
defvars[x] = 'var ' +
Blockly.Dart.variableDB_.getName(variables[x],
for (var i = 0; i < variables.length; i++) {
defvars[i] = 'var ' +
Blockly.Dart.variableDB_.getName(variables[i],
Blockly.Variables.NAME_TYPE) + ';';
}
Blockly.Dart.definitions_['variables'] = defvars.join('\n');
Expand Down
6 changes: 3 additions & 3 deletions generators/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ Blockly.JavaScript.init = function(workspace) {

var defvars = [];
var variables = Blockly.Variables.allVariables(workspace);
for (var x = 0; x < variables.length; x++) {
defvars[x] = 'var ' +
Blockly.JavaScript.variableDB_.getName(variables[x],
for (var i = 0; i < variables.length; i++) {
defvars[i] = 'var ' +
Blockly.JavaScript.variableDB_.getName(variables[i],
Blockly.Variables.NAME_TYPE) + ';';
}
Blockly.JavaScript.definitions_['variables'] = defvars.join('\n');
Expand Down
6 changes: 3 additions & 3 deletions generators/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ Blockly.PHP.init = function(workspace) {

if (!Blockly.PHP.variableDB_) {
Blockly.PHP.variableDB_ =
new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, true);
new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, '$');
} else {
Blockly.PHP.variableDB_.reset();
}

var defvars = [];
var variables = Blockly.Variables.allVariables(workspace);
for (var x = 0; x < variables.length; x++) {
defvars[x] = Blockly.PHP.variableDB_.getName(variables[x],
for (var i = 0; i < variables.length; i++) {
defvars[i] = Blockly.PHP.variableDB_.getName(variables[i],
Blockly.Variables.NAME_TYPE) + ';';
}
Blockly.PHP.definitions_['variables'] = defvars.join('\n');
Expand Down
4 changes: 2 additions & 2 deletions generators/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ Blockly.Python.init = function(workspace) {

var defvars = [];
var variables = Blockly.Variables.allVariables(workspace);
for (var x = 0; x < variables.length; x++) {
defvars[x] = Blockly.Python.variableDB_.getName(variables[x],
for (var i = 0; i < variables.length; i++) {
defvars[i] = Blockly.Python.variableDB_.getName(variables[i],
Blockly.Variables.NAME_TYPE) + ' = None';
}
Blockly.Python.definitions_['variables'] = defvars.join('\n');
Expand Down
2 changes: 1 addition & 1 deletion php_compressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d8a72df

Please sign in to comment.