Commit 9eac6619 authored by carlosperate's avatar carlosperate

Added static typing to the drop down variables in the math and text blocks.

parent 8f2ff5eb
......@@ -339,11 +339,50 @@ Blockly.Blocks['math_change'] = {
}
},
/**
* Assigns a type to the block, all these operations are floats.
* Finds the type of the variable selected in the drop down. Sets it to an
* an integer if it has not been defined before.
* @this Blockly.Block
* @param {Array<string>} existingVars List of variables already defined.
* @return {string} String to indicate the type if it has not been defined
* before.
*/
getType: function() {
return 'boolean';
getVarType: function(existingVars) {
var varName = this.getFieldValue('VAR');
var varType = null;
// Check if variable has been defined already, add type if it has been.
for (var name in existingVars) {
if (name === varName) {
varType = existingVars[varName];
this.varType = varType;
break;
}
}
if (varType == null) {
// not defined, so set it to an int
varType = 'int';
this.varType = 'int';
this.setWarningText(null);
} else if ((varType != 'int') && (varType != 'float')) {
this.setWarningText('This variable type has been previously set to a ' +
existingVars[varName] + ' and it needs to be a number!');
} else {
this.setWarningText(null);
}
return varType;
},
/**
* Contains the type of the variable selected from the drop down.
*/
varType: 'nonono',
/**
* Retrieves the type of the selected variable, defined at getVarType.
* @this Blockly.Block
*/
getType: function(existingVars) {
return this.varType;
}
};
......
......@@ -277,11 +277,39 @@ Blockly.Blocks['text_append'] = {
}
},
/**
* Assigns a type to the block, append always returns a string.
* @this Blockly.Blocks
* Finds the type of the variable selected in the drop down. Sets it to an
* a string if it has not been defined before.
* @this Blockly.Block
* @param {Array<string>} existingVars List of variables already defined.
* @return {string} String to indicate the type if it has not been defined
* before.
*/
getVarType: function() {
return 'String';
getVarType: function(existingVars) {
var varName = this.getFieldValue('VAR');
var varType = null;
// Check if variable has been defined already, add type if it has been.
for (var name in existingVars) {
if (name === varName) {
varType = existingVars[varName];
this.varType = varType;
break;
}
}
if (varType == null) {
// not defined, so set it to an int
varType = 'String';
this.varType = 'String';
this.setWarningText(null);
} else if (varType != 'String') {
this.setWarningText('This variable type has been previously set to a ' +
existingVars[varName] + ' and it needs to be a String!');
} else {
this.setWarningText(null);
}
return varType;
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment