Commit 4fa6aea8 authored by carlosperate's avatar carlosperate

Refactoring the static typing implementation, phase 1.

For now the implementation is very similar, but a lot of the logic moved into the StaticTyping class, which together with the capitalisation of blocklyType object it causes the largest change in the source code, as all blocks are affected.
parent 2a402af1
......@@ -11,6 +11,7 @@
goog.provide('Blockly.Blocks.Arduino.io');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
Blockly.Blocks.Arduino.io.HUE = 250;
......@@ -27,17 +28,17 @@ Blockly.Blocks['io_digitalwrite'] = {
.appendField('set digital pin#')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'PIN');
this.appendValueInput('STATE', Blockly.StaticTyping.blocklyType.BOOLEAN)
this.appendValueInput('STATE', Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField('to')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN);
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Write digital value to a specific Port.');
},
/** @return {string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
/**
* Updates the content of the the pin related fields.
......@@ -61,12 +62,12 @@ Blockly.Blocks['io_digitalread'] = {
.appendField('read digital pin#')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'PIN');
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.setTooltip('Reads the digital value of a pin.');
},
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
/**
* Updates the content of the the pin related fields.
......@@ -90,18 +91,14 @@ Blockly.Blocks['io_builtin_led'] = {
.appendField('set LED')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.builtinLed), 'BUILT_IN_LED');
this.appendValueInput('STATE', Blockly.StaticTyping.blocklyType.BOOLEAN)
this.appendValueInput('STATE', Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField('to')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN);
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Turn on or off the built in LED.');
},
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
},
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
......@@ -109,7 +106,11 @@ Blockly.Blocks['io_builtin_led'] = {
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'BUILT_IN_LED', 'builtinLed');
}
},
/** @return {!string} The type of input value for the block, an integer. */
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
};
Blockly.Blocks['io_analogwrite'] = {
......@@ -124,9 +125,9 @@ Blockly.Blocks['io_analogwrite'] = {
.appendField('set analogue pin#')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.analogPins), 'PIN');
this.appendValueInput('NUM', Blockly.StaticTyping.blocklyType.NUMBER)
this.appendValueInput('NUM', Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('to')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER);
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER);
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
......@@ -138,7 +139,11 @@ Blockly.Blocks['io_analogwrite'] = {
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(this, 'PIN', 'analogPins');
}
},
/** @return {!string} The type of input value for the block, an integer. */
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
};
Blockly.Blocks['io_analogread'] = {
......@@ -153,12 +158,12 @@ Blockly.Blocks['io_analogread'] = {
.appendField('read analogue pin#')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.analogPins), 'PIN');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
this.setTooltip('Return value between 0 and 1024.');
},
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
/**
* Updates the content of the the pin related fields.
......@@ -181,11 +186,11 @@ Blockly.Blocks['io_highlow'] = {
.appendField(
new Blockly.FieldDropdown([['HIGH', 'HIGH'], ['LOW', 'LOW']]),
'STATE');
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.setTooltip('Set a pin state logic High or Low.');
},
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
}
};
......@@ -13,6 +13,7 @@
goog.provide('Blockly.Blocks.Arduino.map');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
......@@ -26,12 +27,12 @@ Blockly.Blocks['base_map'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/map');
this.setColour(Blockly.Blocks.Arduino.map.HUE);
this.appendValueInput('NUM', Blockly.StaticTyping.blocklyType.NUMBER)
this.appendValueInput('NUM', Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('Map ')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER);
this.appendValueInput('DMAX', Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER);
this.appendValueInput('DMAX', Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('value to [0-')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER);
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER);
this.appendDummyInput('')
.appendField(']');
this.setInputsInline(true);
......@@ -39,7 +40,7 @@ Blockly.Blocks['base_map'] = {
this.setTooltip('Re-maps a number from [0-1024] to another range.');
},
/** @return {string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
}
};
......@@ -34,11 +34,7 @@ Blockly.Blocks['arduino_functions'] = {
this.setColour(Blockly.Blocks.Arduino.procedures.HUE);
this.setTooltip('Defines the Arduino setup() and loop() functions.');
this.setHelpUrl('https://arduino.cc/en/Reference/Loop');
},
/** Removes all the option to disable the block context menu */
customContextMenu: function(options) {
// Remove all options from the original reference (=[] won't work)
options.splice(0, options.length);
this.contextMenu = false;
},
/** @return {!boolean} True if the block instance is in the workspace. */
getArduinoLoopsInstance: function() {
......
......@@ -14,6 +14,7 @@
goog.provide('Blockly.Blocks.Arduino.serial');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
......@@ -72,8 +73,8 @@ Blockly.Blocks['serial_print'] = {
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.serial), 'SERIAL_ID')
.appendField('print');
this.appendValueInput('CONTENT', Blockly.StaticTyping.blocklyType.TEXT)
.setCheck(Blockly.StaticTyping.blocklyType.TEXT);
this.appendValueInput('CONTENT', Blockly.StaticTyping.BlocklyType.TEXT)
.setCheck(Blockly.StaticTyping.BlocklyType.TEXT);
this.appendDummyInput()
.appendField(new Blockly.FieldCheckbox('TRUE'), 'NEW_LINE')
.appendField('add new line');
......
......@@ -13,6 +13,7 @@
goog.provide('Blockly.Blocks.Arduino.servo');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
......@@ -32,8 +33,8 @@ Blockly.Blocks['servo_write'] = {
Blockly.Arduino.Boards.selected.pwmPins), 'SERVO_PIN');
this.setInputsInline(false);
this.appendValueInput(
'SERVO_ANGLE', Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
'SERVO_ANGLE', Blockly.StaticTyping.BlocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('to');
this.appendDummyInput('')
.appendField('Degrees (0-180)');
......@@ -64,12 +65,12 @@ Blockly.Blocks['servo_read'] = {
.appendField('Read SERVO from PIN#')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.pwmPins), 'SERVO_PIN');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
this.setTooltip('Read a Servo angle');
},
/** @return {string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
/**
* Updates the content of the the pin related fields.
......
......@@ -11,6 +11,7 @@
goog.provide('Blockly.Blocks.Arduino.spi');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
......@@ -89,8 +90,8 @@ Blockly.Blocks['spi_transfer'] = {
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.spi), 'SPI_ID');
this.appendValueInput('SPI_DATA', '')
.setCheck([Blockly.StaticTyping.blocklyType.NUMBER,
Blockly.StaticTyping.blocklyType.BOOLEAN])
.setCheck([Blockly.StaticTyping.BlocklyType.NUMBER,
Blockly.StaticTyping.BlocklyType.BOOLEAN])
.appendField('transfer');
this.appendDummyInput('')
.appendField('to slave pin')
......@@ -138,10 +139,9 @@ Blockly.Blocks['spi_transfer'] = {
* Retrieves the type of the selected variable, Arduino code returns a byte,
* for now set it to integer.
* @return {!string} Blockly type.
* @this Blockly.Block
*/
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
},
/**
* Updates the content of the board SPI related fields.
......@@ -201,7 +201,7 @@ Blockly.Blocks['spi_transfer_return'] = {
/** Same as spi_transfer block */
onchange: Blockly.Blocks['spi_transfer'].onchange,
/** Same as spi_transfer block */
getType: Blockly.Blocks['spi_transfer'].getType,
getBlockType: Blockly.Blocks['spi_transfer'].getBlockType,
/** Same as spi_transfer block */
updateFields: Blockly.Blocks['spi_transfer'].updateFields
};
......@@ -20,6 +20,7 @@
goog.provide('Blockly.Blocks.Arduino.stepper');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
goog.require('Blockly.FieldDropdown');
......@@ -106,11 +107,11 @@ Blockly.Blocks['stepper_config'] = {
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'STEPPER_PIN2');
this.appendValueInput('STEPPER_STEPS')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField('how many steps per revolution');
this.appendValueInput('STEPPER_SPEED')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField('set speed (rpm) to');
this.setTooltip('Configures a stepper motor pinout and other settings.');
......@@ -154,7 +155,7 @@ Blockly.Blocks['stepper_step'] = {
.appendField(new Blockly.Blocks.Arduino.stepper.FieldStepperInstance(),
'STEPPER_NAME');
this.appendValueInput('STEPPER_STEPS')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER);
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER);
this.appendDummyInput()
.appendField('steps');
this.setPreviousStatement(true);
......
......@@ -11,6 +11,7 @@
goog.provide('Blockly.Blocks.Arduino.time');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
......@@ -25,8 +26,8 @@ Blockly.Blocks['time_delay'] = {
this.setHelpUrl('http://arduino.cc/en/Reference/Delay');
this.setColour(Blockly.Blocks.Arduino.time.HUE);
this.appendValueInput(
'DELAY_TIME_MILI', Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
'DELAY_TIME_MILI', Blockly.StaticTyping.BlocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('wait');
this.appendDummyInput()
.appendField('milliseconds');
......@@ -46,8 +47,8 @@ Blockly.Blocks['time_delaymicros'] = {
this.setHelpUrl('http://arduino.cc/en/Reference/DelayMicroseconds');
this.setColour(Blockly.Blocks.Arduino.time.HUE);
this.appendValueInput(
'DELAY_TIME_MICRO', Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
'DELAY_TIME_MICRO', Blockly.StaticTyping.BlocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('wait');
this.appendDummyInput()
.appendField('microseconds');
......@@ -68,13 +69,13 @@ Blockly.Blocks['time_millis'] = {
this.setColour(Blockly.Blocks.Arduino.time.HUE);
this.appendDummyInput('')
.appendField('current elapsed Time (milliseconds)');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
this.setTooltip('Returns the number of milliseconds since the Arduino ' +
'board began running the current program.');
},
/** @return {string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
}
};
......@@ -88,7 +89,7 @@ Blockly.Blocks['time_micros'] = {
this.setColour(Blockly.Blocks.Arduino.time.HUE);
this.appendDummyInput('')
.appendField('current elapsed Time (microseconds)');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
this.setTooltip('Returns the number of microseconds since the Arduino ' +
'board began running the current program.');
},
......@@ -96,8 +97,8 @@ Blockly.Blocks['time_micros'] = {
* Should be a long (32bit), but for for now an int.
* @return {string} The type of return value for the block, an integer.
*/
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.INTEGER;
}
};
......
......@@ -13,6 +13,7 @@
goog.provide('Blockly.Blocks.Arduino.variables');
goog.require('Blockly.Arduino');
goog.require('Blockly.StaticTyping');
/** Common HSV hue for all blocks in this category. */
......@@ -30,7 +31,7 @@ Blockly.Blocks['variables_set_type'] = {
this.appendDummyInput('')
.appendField('as')
.appendField(new Blockly.FieldDropdown(
Blockly.StaticTyping.blocklySafeTypeArray()),
Blockly.StaticTyping.blocklyValidTypeArray()),
'VARIABLE_SETTYPE_TYPE');
this.setInputsInline(true);
this.setOutput(true);
......@@ -41,8 +42,8 @@ Blockly.Blocks['variables_set_type'] = {
* @return {!string} Blockly type for this block configuration.
* @this Blockly.Block
*/
getType: function() {
getBlockType: function() {
var blocklyTypeKey = this.getFieldValue('VARIABLE_SETTYPE_TYPE');
return Blockly.StaticTyping.blocklyType[blocklyTypeKey];
return Blockly.StaticTyping.BlocklyType[blocklyTypeKey];
}
};
......@@ -27,6 +27,7 @@
goog.provide('Blockly.Blocks.colour');
goog.require('Blockly.Blocks');
goog.require('Blockly.StaticTyping');
/**
......@@ -73,16 +74,16 @@ Blockly.Blocks['colour_rgb'] = {
this.setHelpUrl(Blockly.Msg.COLOUR_RGB_HELPURL);
this.setColour(Blockly.Blocks.colour.HUE);
this.appendValueInput('RED')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.COLOUR_RGB_TITLE)
.appendField(Blockly.Msg.COLOUR_RGB_RED);
this.appendValueInput('GREEN')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.COLOUR_RGB_GREEN);
this.appendValueInput('BLUE')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.COLOUR_RGB_BLUE);
this.setOutput(true, 'Colour');
......@@ -108,7 +109,7 @@ Blockly.Blocks['colour_blend'] = {
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.COLOUR_BLEND_COLOUR2);
this.appendValueInput('RATIO')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER)
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER)
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(Blockly.Msg.COLOUR_BLEND_RATIO);
this.setOutput(true, 'Colour');
......
......@@ -27,6 +27,7 @@
goog.provide('Blockly.Blocks.lists');
goog.require('Blockly.Blocks');
goog.require('Blockly.StaticTyping');
/**
......@@ -220,7 +221,7 @@ Blockly.Blocks['lists_repeat'] = {
{
"type": "input_value",
"name": "NUM",
"check": Blockly.StaticTyping.blocklyType.NUMBER
"check": Blockly.StaticTyping.BlocklyType.NUMBER
}
],
"output": "Array",
......@@ -243,10 +244,10 @@ Blockly.Blocks['lists_length'] = {
{
"type": "input_value",
"name": "VALUE",
"check": [Blockly.StaticTyping.blocklyType.TEXT, 'Array']
"check": [Blockly.StaticTyping.BlocklyType.TEXT, 'Array']
}
],
"output": Blockly.StaticTyping.blocklyType.NUMBER,
"output": Blockly.StaticTyping.BlocklyType.NUMBER,
"colour": Blockly.Blocks.lists.HUE,
"tooltip": Blockly.Msg.LISTS_LENGTH_TOOLTIP,
"helpUrl": Blockly.Msg.LISTS_LENGTH_HELPURL
......@@ -266,10 +267,10 @@ Blockly.Blocks['lists_isEmpty'] = {
{
"type": "input_value",
"name": "VALUE",
"check": [Blockly.StaticTyping.blocklyType.TEXT, 'Array']
"check": [Blockly.StaticTyping.BlocklyType.TEXT, 'Array']
}
],
"output": Blockly.StaticTyping.blocklyType.BOOLEAN,
"output": Blockly.StaticTyping.BlocklyType.BOOLEAN,
"colour": Blockly.Blocks.lists.HUE,
"tooltip": Blockly.Msg.LISTS_ISEMPTY_TOOLTIP,
"helpUrl": Blockly.Msg.LISTS_ISEMPTY_HELPURL
......@@ -288,7 +289,7 @@ Blockly.Blocks['lists_indexOf'] = {
[Blockly.Msg.LISTS_INDEX_OF_LAST, 'LAST']];
this.setHelpUrl(Blockly.Msg.LISTS_INDEX_OF_HELPURL);
this.setColour(Blockly.Blocks.lists.HUE);
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
this.appendValueInput('VALUE')
.setCheck('Array')
.appendField(Blockly.Msg.LISTS_INDEX_OF_INPUT_IN_LIST);
......@@ -405,7 +406,7 @@ Blockly.Blocks['lists_getIndex'] = {
// Create either a value 'AT' input or a dummy input.
if (isAt) {
this.appendValueInput('AT').setCheck(
Blockly.StaticTyping.blocklyType.NUMBER);
Blockly.StaticTyping.BlocklyType.NUMBER);
if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
this.appendDummyInput('ORDINAL')
.appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
......@@ -506,7 +507,7 @@ Blockly.Blocks['lists_setIndex'] = {
// Create either a value 'AT' input or a dummy input.
if (isAt) {
this.appendValueInput('AT').setCheck(
Blockly.StaticTyping.blocklyType.NUMBER);
Blockly.StaticTyping.BlocklyType.NUMBER);
if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
this.appendDummyInput('ORDINAL')
.appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
......@@ -606,7 +607,7 @@ Blockly.Blocks['lists_getSublist'] = {
// Create either a value 'AT' input or a dummy input.
if (isAt) {
this.appendValueInput('AT' + n).setCheck(
Blockly.StaticTyping.blocklyType.NUMBER);
Blockly.StaticTyping.BlocklyType.NUMBER);
if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
this.appendDummyInput('ORDINAL' + n)
.appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
......@@ -656,20 +657,20 @@ Blockly.Blocks['lists_split'] = {
if (newOp == 'SPLIT') {
thisBlock.outputConnection.setCheck('Array');
thisBlock.getInput('INPUT').setCheck(
Blockly.StaticTyping.blocklyType.TEXT);
Blockly.StaticTyping.BlocklyType.TEXT);
} else {
thisBlock.outputConnection.setCheck(
Blockly.StaticTyping.blocklyType.TEXT);
Blockly.StaticTyping.BlocklyType.TEXT);
thisBlock.getInput('INPUT').setCheck('Array');
}
});
this.setHelpUrl(Blockly.Msg.LISTS_SPLIT_HELPURL);
this.setColour(Blockly.Blocks.lists.HUE);
this.appendValueInput('INPUT')
.setCheck(Blockly.StaticTyping.blocklyType.TEXT)
.setCheck(Blockly.StaticTyping.BlocklyType.TEXT)
.appendField(dropdown, 'MODE');
this.appendValueInput('DELIM')
.setCheck(Blockly.StaticTyping.blocklyType.TEXT)
.setCheck(Blockly.StaticTyping.BlocklyType.TEXT)
.appendField(Blockly.Msg.LISTS_SPLIT_WITH_DELIMITER);
this.setInputsInline(true);
this.setOutput(true, 'Array');
......
......@@ -27,6 +27,7 @@
goog.provide('Blockly.Blocks.logic');
goog.require('Blockly.Blocks');
goog.require('Blockly.StaticTyping');
/**
......@@ -43,7 +44,7 @@ Blockly.Blocks['controls_if'] = {
this.setHelpUrl(Blockly.Msg.CONTROLS_IF_HELPURL);
this.setColour(Blockly.Blocks.logic.HUE);
this.appendValueInput('IF0')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_IF);
this.appendStatementInput('DO0')
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
......@@ -96,7 +97,7 @@ Blockly.Blocks['controls_if'] = {
this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10) || 0;
for (var i = 1; i <= this.elseifCount_; i++) {
this.appendValueInput('IF' + i)
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
this.appendStatementInput('DO' + i)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
......@@ -153,7 +154,7 @@ Blockly.Blocks['controls_if'] = {
case 'controls_if_elseif':
this.elseifCount_++;
var ifInput = this.appendValueInput('IF' + this.elseifCount_)
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
var doInput = this.appendStatementInput('DO' + this.elseifCount_);
doInput.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
......@@ -283,7 +284,7 @@ Blockly.Blocks['logic_compare'] = {
];
this.setHelpUrl(Blockly.Msg.LOGIC_COMPARE_HELPURL);
this.setColour(Blockly.Blocks.logic.HUE);
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.appendValueInput('A');
this.appendValueInput('B')
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
......@@ -327,11 +328,9 @@ Blockly.Blocks['logic_compare'] = {
this.prevBlocks_[0] = blockA;
this.prevBlocks_[1] = blockB;
},
/**
* Assigns a type to the block, comparison operations result in booleans.
*/
getType: function() {
return Blockly.StaticTyping.blocklyType.BOOLEAN;
/** Assigns a type to the block, comparison operations result in booleans. */
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.BOOLEAN;
}
};
......@@ -346,11 +345,11 @@ Blockly.Blocks['logic_operation'] = {
[Blockly.Msg.LOGIC_OPERATION_OR, 'OR']];
this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
this.setColour(Blockly.Blocks.logic.HUE);
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.appendValueInput('A')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN);
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.appendValueInput('B')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
this.setInputsInline(true);
// Assign 'this' to a variable for use in the tooltip closure below.
......@@ -364,11 +363,9 @@ Blockly.Blocks['logic_operation'] = {
return TOOLTIPS[op];
});
},
/**
* Assigns a type to the block, logic comparison operations result in bools.
*/
getType: function() {
Blockly.StaticTyping.blocklyType.BOOLEAN;
/** Assigns a block type, logic comparison operations result in bools. */
getBlockType: function() {
Blockly.StaticTyping.BlocklyType.BOOLEAN;
}
};
......@@ -384,21 +381,18 @@ Blockly.Blocks['logic_negate'] = {
{
"type": "input_value",
"name": "BOOL",
"check": Blockly.StaticTyping.blocklyType.BOOLEAN
"check": Blockly.StaticTyping.BlocklyType.BOOLEAN
}
],
"output": Blockly.StaticTyping.blocklyType.BOOLEAN,
"output": Blockly.StaticTyping.BlocklyType.BOOLEAN,
"colour": Blockly.Blocks.logic.HUE,
"tooltip": Blockly.Msg.LOGIC_NEGATE_TOOLTIP,
"helpUrl": Blockly.Msg.LOGIC_NEGATE_HELPURL
});
},
/**
* Assigns a type to the block, not block input is meant to be a booleans, so
* it should return the same.
*/
getType: function() {
return Blockly.StaticTyping.blocklyType.BOOLEAN;
/** Assigns block type, 'block input' is meant to be a boolean, so same. */
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.BOOLEAN;
}
};
......@@ -413,16 +407,14 @@ Blockly.Blocks['logic_boolean'] = {
[Blockly.Msg.LOGIC_BOOLEAN_FALSE, 'FALSE']];
this.setHelpUrl(Blockly.Msg.LOGIC_BOOLEAN_HELPURL);
this.setColour(Blockly.Blocks.logic.HUE);
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.BOOLEAN);
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown(BOOLEANS), 'BOOL');
this.setTooltip(Blockly.Msg.LOGIC_BOOLEAN_TOOLTIP);
},
/**
* Assigns a type to the boolean block.
*/
getType: function() {
return Blockly.StaticTyping.blocklyType.BOOLEAN;
/** Assigns a type to the boolean block. */
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.BOOLEAN;
}
};
......@@ -438,8 +430,11 @@ Blockly.Blocks['logic_null'] = {
this.appendDummyInput()
.appendField(Blockly.Msg.LOGIC_NULL);
this.setTooltip(Blockly.Msg.LOGIC_NULL_TOOLTIP);
},
/** Assigns a type to the NULL block. */
getBlockType: function() {
return Blockly.StaticTyping.BlocklyType.NULL;
}
// Null does not have a type, so no getType. Might change this in the future.
};
Blockly.Blocks['logic_ternary'] = {
......@@ -451,7 +446,7 @@ Blockly.Blocks['logic_ternary'] = {
this.setHelpUrl(Blockly.Msg.LOGIC_TERNARY_HELPURL);
this.setColour(Blockly.Blocks.logic.HUE);
this.appendValueInput('IF')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(Blockly.Msg.LOGIC_TERNARY_CONDITION);
this.appendValueInput('THEN')
.appendField(Blockly.Msg.LOGIC_TERNARY_IF_TRUE);
......@@ -487,5 +482,5 @@ Blockly.Blocks['logic_ternary'] = {
}
this.prevParentConnection_ = parentConnection;
}
//TODO: getType that uses the type of the given inputs
//TODO: getBlockType that uses the type of the given inputs
};
......@@ -27,6 +27,7 @@
goog.provide('Blockly.Blocks.loops');
goog.require('Blockly.Blocks');
goog.require('Blockly.StaticTyping');
/**
......@@ -74,7 +75,7 @@ Blockly.Blocks['controls_repeat_ext'] = {
{
"type": "input_value",
"name": "TIMES",
"check": Blockly.StaticTyping.blocklyType.NUMBER
"check": Blockly.StaticTyping.BlocklyType.NUMBER
}
],
"previousStatement": null,
......@@ -100,7 +101,7 @@ Blockly.Blocks['controls_whileUntil'] = {
this.setHelpUrl(Blockly.Msg.CONTROLS_WHILEUNTIL_HELPURL);
this.setColour(Blockly.Blocks.loops.HUE);
this.appendValueInput('BOOL')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
this.appendStatementInput('DO')
.appendField(Blockly.Msg.CONTROLS_WHILEUNTIL_INPUT_DO);
......@@ -136,19 +137,19 @@ Blockly.Blocks['controls_for'] = {
{
"type": "input_value",
"name": "FROM",
"check": Blockly.StaticTyping.blocklyType.NUMBER,
"check": Blockly.StaticTyping.BlocklyType.NUMBER,
"align": "RIGHT"
},
{
"type": "input_value",
"name": "TO",
"check": Blockly.StaticTyping.blocklyType.NUMBER,
"check": Blockly.StaticTyping.BlocklyType.NUMBER,
"align": "RIGHT"
},
{
"type": "input_value",
"name": "BY",
"check": Blockly.StaticTyping.blocklyType.NUMBER,
"check": Blockly.StaticTyping.BlocklyType.NUMBER,
"align": "RIGHT"
}
],
......@@ -206,36 +207,12 @@ Blockly.Blocks['controls_for'] = {
}
},
/**
* 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 Associative array of variables already
* defined. Variable name as the key,
* type as their value.
* Defines the type of the variable selected in the drop down, an integer.
* @return {string} String to indicate the type if it has not been defined
* before.
*/
getVarType: function(existingVars) {
var varName = this.getFieldValue('VAR');
// Check if variable has been defined already
var varType = Blockly.StaticTyping.findListVarType(varName, existingVars);
if (varType != null) {
if ((varType != Blockly.StaticTyping.blocklyType.INTEGER) &&
(varType != Blockly.StaticTyping.blocklyType.DECIMAL)) {
this.setWarningText('This variable type has been previously set to a ' +
existingVars[varName] + ' and it needs to be a number!')
} else {
this.setWarningText(null);
}
} else {
// not defined, so set it to an integer
//TODO: The number input could be set to a decirmal, so check input
varType = Blockly.StaticTyping.blocklyType.INTEGER;
this.setWarningText(null);
}
return varType;
getVarType: function(varName) {
return Blockly.StaticTyping.BlocklyType.INTEGER;
}
};
......@@ -293,7 +270,11 @@ Blockly.Blocks['controls_forEach'] = {
this.setFieldValue(newName, 'VAR');
}
},
customContextMenu: Blockly.Blocks['controls_for'].customContextMenu
customContextMenu: Blockly.Blocks['controls_for'].customContextMenu,
/** @returns {!string} The type of the variable used in this block */
getVarType: function(varName) {
return Blockly.StaticTyping.BlocklyType.INTEGER;
}
};
Blockly.Blocks['controls_flow_statements'] = {
......
This diff is collapsed.
......@@ -27,6 +27,7 @@
goog.provide('Blockly.Blocks.procedures');
goog.require('Blockly.Blocks');
goog.require('Blockly.StaticTyping');
/**
......@@ -327,32 +328,35 @@ Blockly.Blocks['procedures_defnoreturn'] = {
}
},
callType_: 'procedures_callnoreturn',
/** @return {!string} This block does not define type, so 'undefined' */
getVarType: function(varName) {
return Blockly.StaticTyping.BlocklyType.UNDEF;
},
/** Contains the type of the arguments added with mutators. */
argsTypes: {},
/**
* Searches through a list of variables with type to assign the type of the
* arguments.
* @this Blockly.Block
* @param {Array<string>} existingVars Associative array variable already
* defined, names as key, type as value.
* @param {Array<string>} existingVars Associative array variable already
* defined, names as key, type as value.
*/
setArgsType: function(existingVars) {
var varNames = this.arguments_;
// Check if variable has been defined already and save type
for (var name in existingVars) {
for (var i = 0; i < varNames.length; i++) {
if (name == varNames[i]) {
for (var i = 0, length_ = varNames.length; i < length_; i++) {
if (name === varNames[i]) {
this.argsTypes[name] = existingVars[name];
}
}
}
},
/**
* Contains the type of the arguments added with mutators.
*/
argsTypes: {},
/**
* Retrieves the type of the arguments, types defined at setArgsType.
* @this Blockly.Block
* @return {string} Type of the argument indicated in the input.
*/
getArgType: function(varName) {
for (var name in this.argsTypes) {
......@@ -412,22 +416,23 @@ Blockly.Blocks['procedures_defreturn'] = {
renameVar: Blockly.Blocks['procedures_defnoreturn'].renameVar,
customContextMenu: Blockly.Blocks['procedures_defnoreturn'].customContextMenu,
callType_: 'procedures_callreturn',
setArgsType: Blockly.Blocks['procedures_defnoreturn'].setArgsType,
varType: {},
getVarType: Blockly.Blocks['procedures_defnoreturn'].getVarType,
argsTypes: {},
setArgsType: Blockly.Blocks['procedures_defnoreturn'].setArgsType,
getArgType: Blockly.Blocks['procedures_defnoreturn'].getArgType,
/**
* Searches through the nested blocks in the return input to find a variable
* type or return unspecified.
* type or returns NULL.
* @this Blockly.Block
* @return {string} String to indicate the type or unspecified.
* @return {string} String to indicate the type or NULL.
*/
getReturnType: function() {
var returnType = Blockly.StaticTyping.blocklyType.UNSPECIFIED;
var returnType = Blockly.StaticTyping.BlocklyType.NULL;
var returnBlock = this.getInputTargetBlock('RETURN');
if (returnBlock) {
// First check if the block itself has a type already
if (returnBlock.getType) {
returnType = returnBlock.getType();
if (returnBlock.getBlockType) {
returnType = returnBlock.getBlockType();
} else {
returnType = Blockly.StaticTyping.getChildBlockType(returnBlock);
}
......@@ -750,7 +755,7 @@ Blockly.Blocks['procedures_ifreturn'] = {
this.setHelpUrl('http://c2.com/cgi/wiki?GuardClause');
this.setColour(Blockly.Blocks.procedures.HUE);
this.appendValueInput('CONDITION')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN)
.setCheck(Blockly.StaticTyping.BlocklyType.BOOLEAN)
.appendField(Blockly.Msg.CONTROLS_IF_MSG_IF);
this.appendValueInput('VALUE')
.appendField(Blockly.Msg.PROCEDURES_DEFRETURN_RETURN);
......
This diff is collapsed.
......@@ -88,37 +88,35 @@ Blockly.Blocks['variables_get'] = {
options.push(option);
},
/**
* Finds the type of the selected variable.
* Contains the type of the variable selected from the block.
* @type {!string} Type from the Static Typing class in string format.
*/
varType: Blockly.StaticTyping.BlocklyType.UNDEF,
/**
* Set this block variable to a type.
* @this Blockly.Block
* @param {Array<string>} existingVars Associative array of variables already
* defined. Var names as key and type as value.
* @return {string} String to indicate the type if not defined before.
* @param {!string} varType Type that this block is to be set to.
*/
getVarType: function(existingVars) {
var varName = this.getFieldValue('VAR');
// Check if variable has been defined already add if it has been.
var varType = Blockly.StaticTyping.findListVarType(varName, existingVars);
if (varType != null) {
this.varType = varType;
this.setWarningText(null);
} else {
// This block needs the variable to be define before use, so warn user.
this.setWarningText(
'This variable needs to be set to something before it can be used!');
}
return varType;
setBlockType: function(varType) {
this.varType = varType;
},
/**
* Contains the type of the variable selected from the first set block.
* @return {!string} Retrieves the type (stored in varType) of this block.
* @this Blockly.Block
*/
varType: Blockly.StaticTyping.blocklyType.UNDEF,
getBlockType: function() {
return this.varType;
},
/**
* Retrieves the type of the selected variable, defined at getVarType.
* @this Blockly.Block
* Gets the stored type of the variable indicated in the argument. As only one
* variable is stored in this block, no need to check input
* @this Blockly.
* @param {!string} varName Name of this block variable to check type.
* @return {!string} String to indicate the type of this block.
*/
getType: function() {
getVarType: function(varName) {
return this.varType;
}
},
};
Blockly.Blocks['variables_set'] = {
......@@ -173,36 +171,9 @@ Blockly.Blocks['variables_set'] = {
/**
* Searches through the nested blocks to find a variable type.
* @this Blockly.Block
* @param {Array<string>} existingVars Associative array of variables already
* defined. Var name as the key, type as
* the value.
* @return {string} String to indicate the type if it has not been defined
* before.
* @return {string} String to indicate the type of this block.
*/
getVarType: function(existingVars) {
var varName = this.getFieldValue('VAR');
// Check what this block type should be
var thisBlockType = Blockly.StaticTyping.getChildBlockType(this);
// Check if variable has been defined already
var varType = Blockly.StaticTyping.findListVarType(varName, existingVars);
if (varType === null) {
// This block var has not been encountered before, so return type
this.setWarningText(null);
return thisBlockType;
} else if ((existingVars[varName] !== thisBlockType) &&
(this.getChildren().length > 0)) {
// Variable name defined before, but only set warning if there are child
// blocks
this.setWarningText('This block is using a different type than what ' +
'was set on the first use of this variable.\nFirst use type: ' +
existingVars[varName] + '\nThis block type: ' + thisBlockType);
return null;
} else {
// Variable defined before, but it is the same type, or block is empty
this.setWarningText(null);
return null;
}
getVarType: function() {
return Blockly.StaticTyping.getChildBlockType(this);
}
};
This diff is collapsed.
......@@ -55,10 +55,10 @@ Blockly.Blocks['factory_base'] = {
.appendField('colour');
/*
this.appendValueInput('TOOLTIP')
.setCheck(Blockly.StaticTyping.blocklyType.TEXT)
.setCheck(Blockly.StaticTyping.BlocklyType.TEXT)
.appendField('tooltip');
this.appendValueInput('HELP')
.setCheck(Blockly.StaticTyping.blocklyType.TEXT)
.setCheck(Blockly.StaticTyping.BlocklyType.TEXT)
.appendField('help url');
*/
this.setTooltip('Build a custom block by plugging\n' +
......@@ -611,7 +611,7 @@ Blockly.Blocks['type_group_item'] = {
Blockly.Blocks['type_null'] = {
// Null type.
valueType: Blockly.StaticTyping.blocklyType.NULL,
valueType: Blockly.StaticTyping.BlocklyType.NULL,
init: function() {
this.setColour(230);
this.appendDummyInput()
......@@ -624,7 +624,7 @@ Blockly.Blocks['type_null'] = {
Blockly.Blocks['type_boolean'] = {
// Boolean type.
valueType: Blockly.StaticTyping.blocklyType.BOOLEAN,
valueType: Blockly.StaticTyping.BlocklyType.BOOLEAN,
init: function() {
this.setColour(230);
this.appendDummyInput()
......@@ -637,7 +637,7 @@ Blockly.Blocks['type_boolean'] = {
Blockly.Blocks['type_number'] = {
// Number type.
valueType: Blockly.StaticTyping.blocklyType.NUMBER,
valueType: Blockly.StaticTyping.BlocklyType.NUMBER,
init: function() {
this.setColour(230);
this.appendDummyInput()
......@@ -650,7 +650,7 @@ Blockly.Blocks['type_number'] = {
Blockly.Blocks['type_string'] = {
// String type.
valueType: Blockly.StaticTyping.blocklyType.TEXT,
valueType: Blockly.StaticTyping.BlocklyType.TEXT,
init: function() {
this.setColour(230);
this.appendDummyInput()
......
......@@ -140,7 +140,7 @@ Blockly.Blocks['graph_get_x'] = {
this.setColour(330);
this.appendDummyInput()
.appendField('x');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
}
};
......@@ -157,7 +157,7 @@ Blockly.Blocks['graph_set_y'] = {
this.setColour(330);
this.appendValueInput('VALUE')
.appendField('y =')
.setCheck(Blockly.StaticTyping.blocklyType.NUMBER);
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER);
this.setTooltip(Blockly.Msg.VARIABLES_SET_TOOLTIP);
}
};
......
......@@ -49,7 +49,7 @@ Blockly.Blocks['plane_get_rows'] = {
this.setColour(330);
this.appendDummyInput()
.appendField(Plane.getMsg('Plane_getRows'), 'title');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
},
customUpdate: function() {
this.setFieldValue(
......@@ -69,7 +69,7 @@ Blockly.Blocks['plane_get_rows1st'] = {
this.setColour(330);
this.appendDummyInput()
.appendField(Plane.getMsg('Plane_getRows1'), 'title');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
},
customUpdate: function() {
this.setFieldValue(
......@@ -89,7 +89,7 @@ Blockly.Blocks['plane_get_rows2nd'] = {
this.setColour(330);
this.appendDummyInput()
.appendField(Plane.getMsg('Plane_getRows2'), 'title');
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setOutput(true, Blockly.StaticTyping.BlocklyType.NUMBER);
},
customUpdate: function() {
this.setFieldValue(
......
......@@ -109,32 +109,24 @@ Blockly.Arduino.init = function(workspace) {
Blockly.Arduino.variableDB_.reset();
}
// Iterate through the blocks to capture variables with first instance type
var variableTypes = Blockly.StaticTyping.getAllVarsWithTypes(workspace);
// Iterate through to capture all blocks types and set the function arguments
var varsWithTypes = Blockly.StaticTyping.getAllVarsWithTypes(workspace);
Blockly.StaticTyping.setProcedureArgs(workspace, varsWithTypes);
// The procedure arguments need to have all the variables collected first
var blocks = workspace.getAllBlocks();
for (var x = 0; x < blocks.length; x++) {
var setArgsType = blocks[x].setArgsType;
if (setArgsType) {
setArgsType.call(blocks[x], variableTypes);
}
}
// Set variable declarations
// Set variable declarations with their Arduino type in the defines dictionary
var variableDeclarations = [];
for (var varName in variableTypes) {
for (var varName in varsWithTypes) {
variableDeclarations.push(
Blockly.Arduino.getArduinoType_(variableTypes[varName]) + ' ' +
Blockly.Arduino.getArduinoType_(varsWithTypes[varName]) + ' ' +
varName + ';');
}
Blockly.Arduino.definitions_['variables'] = variableDeclarations.join('\n');
};
/**
* Prepend the generated code with the variable definitions.
* @param {string} code Generated code.
* @return {string} Completed code.
* Prepare all generated code to be placed in the sketch specific locations.
* @param {string} code Generated main program (loop function) code.
* @return {string} Completed sketch code.
*/
Blockly.Arduino.finish = function(code) {
// Convert the includes, definitions, and functions dictionaries into lists
......@@ -331,7 +323,7 @@ Blockly.Arduino.scrub_ = function(block, code) {
/**
* Generates Arduino Types from a Blockly Type.
* @param {!Blockly.StaticTyping.blocklyType} typeBlockly The Blockly type to be
* @param {!Blockly.StaticTyping.BlocklyType} typeBlockly The Blockly type to be
* converted.
* @return {string} Arduino type for the respective Blockly input type, in a
* string format.
......@@ -340,28 +332,28 @@ Blockly.Arduino.scrub_ = function(block, code) {
*/
Blockly.Arduino.getArduinoType_ = function(typeBlockly) {
switch (typeBlockly) {
case Blockly.StaticTyping.blocklyType.UNDEF:
case Blockly.StaticTyping.BlocklyType.UNDEF:
return 'undefined';
case Blockly.StaticTyping.blocklyType.UNSPECIFIED:
case Blockly.StaticTyping.BlocklyType.UNSPECIFIED:
return 'unspecified';
case Blockly.StaticTyping.BlocklyType.NULL:
return 'void';
case Blockly.StaticTyping.blocklyType.NULL:
return 'NULL';
case Blockly.StaticTyping.blocklyType.NUMBER:
case Blockly.StaticTyping.BlocklyType.NUMBER:
return 'int';
case Blockly.StaticTyping.blocklyType.INTEGER:
case Blockly.StaticTyping.BlocklyType.INTEGER:
return 'int';
case Blockly.StaticTyping.blocklyType.DECIMAL:
case Blockly.StaticTyping.BlocklyType.DECIMAL:
return 'float';
case Blockly.StaticTyping.blocklyType.TEXT:
case Blockly.StaticTyping.BlocklyType.TEXT:
return 'String';
case Blockly.StaticTyping.blocklyType.CHARACTER:
case Blockly.StaticTyping.BlocklyType.CHARACTER:
return 'char';
case Blockly.StaticTyping.blocklyType.BOOLEAN:
case Blockly.StaticTyping.BlocklyType.BOOLEAN:
return 'boolean';
case Blockly.StaticTyping.blocklyType.ERROR:
case Blockly.StaticTyping.BlocklyType.ERROR:
return 'ErrorArdu';
case Blockly.StaticTyping.blocklyType.CHILD_TYPE_MISSING:
return 'ChildBlockTypeMissingArdu';
case Blockly.StaticTyping.BlocklyType.CHILD_BLOCK_MISSING:
return 'ChildBlockMissing';
default:
return 'Invalid Blockly Type';
}
......
......@@ -43,13 +43,15 @@ Blockly.Arduino['procedures_defreturn'] = function(block) {
// Get arguments with type
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = block.getArgType(block.arguments_[x]) + ' ' +
args[x] =
Blockly.Arduino.getArduinoType_(block.getArgType(block.arguments_[x])) +
' ' +
Blockly.Arduino.variableDB_.getName(block.arguments_[x],
Blockly.Variables.NAME_TYPE);
}
// Get return type
var returnType = Blockly.StaticTyping.blocklyType.UNSPECIFIED;
var returnType = Blockly.StaticTyping.BlocklyType.NULL;
if (block.getReturnType) {
returnType = block.getReturnType();
}
......
......@@ -192,7 +192,7 @@ Blockly.Arduino['text_prompt_ext'] = function(block) {
// The function code changes based on reading a number or string
var func = [];
var toNumber = returnType == Blockly.StaticTyping.blocklyType.NUMBER;
var toNumber = returnType == Blockly.StaticTyping.BlocklyType.NUMBER;
if (toNumber) {
func.push('int ' + Blockly.Arduino.DEF_FUNC_NAME + '(String msg) {');
} else {
......
......@@ -48,7 +48,7 @@ Blockly.Arduino['variables_set_type'] = function(block) {
var argument0 = Blockly.Arduino.valueToCode(block, 'VARIABLE_SETTYPE_INPUT',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var varType = Blockly.Arduino.getArduinoType_(
Blockly.StaticTyping.blocklyType[block.getFieldValue(
Blockly.StaticTyping.BlocklyType[block.getFieldValue(
'VARIABLE_SETTYPE_TYPE')]);
var code = '(' + varType + ')(' + argument0 + ')';
return [code, Blockly.Arduino.ORDER_ATOMIC];
......
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