Commit d9b3c6b1 authored by carlosperate's avatar carlosperate

Initial version of static typing. Basic building blocks in place for some of...

Initial version of static typing. Basic building blocks in place for some of the most significant blocks for this feature.
Moved the old regular expressions trick out of the arduino.js, and partially used within the math getType function.
parent 422ae35e
......@@ -44,6 +44,22 @@ Blockly.Blocks['math_number'] = {
Blockly.FieldTextInput.numberValidator), 'NUM');
this.setOutput(true, 'Number');
this.setTooltip(Blockly.Msg.MATH_NUMBER_TOOLTIP);
},
/**
* Reads the numerical value from the block and assigns a type.
* @this Blockly.Block
*/
getType: function() {
var numString = this.getFieldValue('NUM');
var regExpInt = new RegExp(/^\d+$/);
var regExpFloat = new RegExp(/^[0-9]*[.][0-9]+$/);
if (regExpInt.test(numString)) {
return 'int';
} else if (regExpFloat.test(numString)) {
return 'float';
}
//TODO: This is just a temporary value for easy bug catching.
return 'errornumber';
}
};
......@@ -82,6 +98,7 @@ Blockly.Blocks['math_arithmetic'] = {
return TOOLTIPS[mode];
});
}
//TODO: a setType
};
Blockly.Blocks['math_single'] = {
......@@ -120,6 +137,13 @@ Blockly.Blocks['math_single'] = {
};
return TOOLTIPS[mode];
});
},
/**
* Assigns a type to the block, all these operations are floats.
* @this Blockly.Block
*/
getType: function() {
return 'float';
}
};
......@@ -156,6 +180,13 @@ Blockly.Blocks['math_trig'] = {
};
return TOOLTIPS[mode];
});
},
/**
* Assigns a type to the block, all these operations are floats.
* @this Blockly.Block
*/
getType: function() {
return 'float';
}
};
......@@ -178,6 +209,13 @@ Blockly.Blocks['math_constant'] = {
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown(CONSTANTS), 'CONSTANT');
this.setTooltip(Blockly.Msg.MATH_CONSTANT_TOOLTIP);
},
/**
* Assigns a type to the block, all these operations are floats.
* @this Blockly.Block
*/
getType: function() {
return 'float';
}
};
......@@ -246,6 +284,13 @@ Blockly.Blocks['math_number_property'] = {
} else if (inputExists) {
this.removeInput('DIVISOR');
}
},
/**
* Assigns a type to the block, all these operations return booleans.
* @this Blockly.Block
*/
getType: function() {
return 'boolean';
}
};
......@@ -292,6 +337,13 @@ Blockly.Blocks['math_change'] = {
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
this.setFieldValue(newName, 'VAR');
}
},
/**
* Assigns a type to the block, all these operations are floats.
* @this Blockly.Block
*/
getType: function() {
return 'boolean';
}
};
......@@ -312,6 +364,13 @@ Blockly.Blocks['math_round'] = {
.setCheck('Number')
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
this.setTooltip(Blockly.Msg.MATH_ROUND_TOOLTIP);
},
/**
* Assigns a type to the block, round always returns a float.
* @this Blockly.Block
*/
getType: function() {
return 'float';
}
};
......@@ -361,6 +420,7 @@ Blockly.Blocks['math_on_list'] = {
return TOOLTIPS[mode];
});
}
//TODO: a getType once the list code is finished.
};
Blockly.Blocks['math_modulo'] = {
......@@ -378,6 +438,13 @@ Blockly.Blocks['math_modulo'] = {
Blockly.ALIGN_RIGHT);
this.setInputsInline(true);
this.setTooltip(Blockly.Msg.MATH_MODULO_TOOLTIP);
},
/**
* Assigns a type to the block, modulus only works on integers.
* @this Blockly.Block
*/
getType: function() {
return 'int';
}
};
......@@ -398,6 +465,7 @@ Blockly.Blocks['math_constrain'] = {
this.setInputsInline(true);
this.setTooltip(Blockly.Msg.MATH_CONSTRAIN_TOOLTIP);
}
//TODO: a getType of the same type as the inputs.
};
Blockly.Blocks['math_random_int'] = {
......@@ -415,6 +483,13 @@ Blockly.Blocks['math_random_int'] = {
Blockly.ALIGN_RIGHT);
this.setInputsInline(true);
this.setTooltip(Blockly.Msg.MATH_RANDOM_INT_TOOLTIP);
},
/**
* Assigns a type to the block, always an int.
* @this Blockly.Block
*/
getType: function() {
return 'int';
}
};
......@@ -430,5 +505,12 @@ Blockly.Blocks['math_random_float'] = {
this.appendDummyInput()
.appendField(Blockly.Msg.MATH_RANDOM_FLOAT_TITLE_RANDOM);
this.setTooltip(Blockly.Msg.MATH_RANDOM_FLOAT_TOOLTIP);
},
/**
* Assigns a type to the block, always a float.
* @this Blockly.Block
*/
getType: function() {
return 'float';
}
};
......@@ -59,6 +59,13 @@ Blockly.Blocks['text'] = {
var file = 'quote0.png';
}
return new Blockly.FieldImage(Blockly.pathToMedia + file, 12, 12, '"');
},
/**
*
* @this Blockly.Block
*/
getType: function() {
return 'String';
}
};
......
......@@ -128,5 +128,26 @@ Blockly.Blocks['variables_set'] = {
this.setFieldValue(newName, 'VAR');
}
},
customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
customContextMenu: Blockly.Blocks['variables_get'].customContextMenu,
/**
* Searches through the nested blocks to find a variable type.
* @this Blockly.Blocks
*/
getVarType: function() {
var myType = 'nonono';
var nextBlock = [this];
while ((nextBlock[0].getType == null) &&
(nextBlock[0].getChildren().length > 0)) {
nextBlock = nextBlock[0].getChildren();
}
if (nextBlock[0] === this) {
myType = 'defineme';
} else {
var func = nextBlock[0].getType;
if (func) {
myType = nextBlock[0].getType();
}
}
return myType;
}
};
......@@ -146,9 +146,9 @@ Blockly.Arduino.init = function(opt_workspace) {
var variableWithType = Object.create(null);
var blocks = Blockly.mainWorkspace.getAllBlocks();
for (var x = 0; x < blocks.length; x++) {
var func = blocks[x].getVars;
if (func) {
var blockVariables = func.call(blocks[x]);
var getVars = blocks[x].getVars;
if (getVars) {
var blockVariables = getVars.call(blocks[x]);
for (var y = 0; y < blockVariables.length; y++) {
// Check if it's the first instance of the new variable name
var unique = true;
......@@ -160,57 +160,27 @@ Blockly.Arduino.init = function(opt_workspace) {
}
// If it is the first instance log the variable type
if (unique == true) {
variableWithType[blockVariables[y]] = Blockly.Arduino.evaluateType(
Blockly.Arduino.valueToCode(blocks[x], 'VALUE',
Blockly.Arduino.ORDER_ASSIGNMENT));
var getType = blocks[x].getVarType;
if (getType) {
variableWithType[blockVariables[y]] = blocks[x].getVarType();
} else {
variableWithType[blockVariables[y]] = 'notdefined';
}
}
}
}
}
// Set variable declarations
var variableDeclarations = [];
var variableDeclarations = [];
for (var name in variableWithType) {
variableDeclarations.push(variableWithType[name] + ' ' + name + ';');
}
Blockly.Arduino.definitions_['variables'] = variableDeclarations.join('\n') +
'\n';
Blockly.Arduino.definitions_['variables'] =
variableDeclarations.join('\n') + '\n';
};
/**
* Evaluates the type of the data contained in the input string and returns
* a string containing the C++ type.
* @param {string} inputString string containing the input value to evaluate
* @return {string} A String containing the C++ type
*/
Blockly.Arduino.regExpInt = new RegExp(/^\d+$/);
Blockly.Arduino.regExpFloat = new RegExp(/^[0-9]*[.][0-9]+$/);
Blockly.Arduino.evaluateType = function(inputString) {
var firstCharacter = inputString.charAt(0);
if (firstCharacter == '"') {
return 'String';
}
else if (firstCharacter == "'") {
return 'char';
}
else if (!inputString || !inputString.length) {
return 'int';
}
else if (Blockly.Arduino.regExpInt.test(firstCharacter)) {
if( Blockly.Arduino.regExpInt.test(inputString)) {
return 'int';
} else if ( Blockly.Arduino.regExpFloat.test(inputString)) {
return 'float';
}
return 'int';
}
// For now the default is integer, will have to figure out how to
// evaluate other blocks as inputs
return 'int';
}
/**
* Prepend the generated code with the variable definitions.
* @param {string} code Generated code.
......
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