Commit b5e81450 authored by carlosperate's avatar carlosperate

Fix variable casting to work with blockly/core/static_typing.js.

parent 6da9eeea
......@@ -107,9 +107,7 @@
<block type="variables_set"></block>
<block type="variables_set">
<value name="VALUE">
<block type="variables_set_type">
<field name="VARIABLE_SETTYPE_TYPE">int</field>
</block>
<block type="variables_set_type"></block>
</value>
</block>
<block type="variables_set_type"></block>
......
......@@ -28,7 +28,8 @@ Blockly.Blocks['variables_set_type'] = {
this.appendValueInput('VARIABLE_SETTYPE_INPUT', '');
this.appendDummyInput('')
.appendField('as')
.appendField(new Blockly.FieldDropdown(profile.default.types),
.appendField(new Blockly.FieldDropdown(
Blockly.StaticTyping.blocklySafeTypeArray()),
'VARIABLE_SETTYPE_TYPE');
this.setInputsInline(true);
this.setOutput(true);
......@@ -39,6 +40,7 @@ Blockly.Blocks['variables_set_type'] = {
* @this Blockly.Block
*/
getType: function() {
return this.getFieldValue('VARIABLE_SETTYPE_TYPE');
var blocklyTypeKey = this.getFieldValue('VARIABLE_SETTYPE_TYPE');
return Blockly.StaticTyping.blocklyType[blocklyTypeKey];
}
};
......@@ -153,3 +153,18 @@ Blockly.StaticTyping.identifyNumber = function(numberString) {
//TODO: This is just a temporary value for easy bug catching.
return Blockly.StaticTyping.blocklyType.ERROR;
};
/**
* Converts the static types dictionary in to a an array with 2-item arrays.
* @return {!array} Blockly types in the format described above.
*/
Blockly.StaticTyping.blocklySafeTypeArray = function() {
var typesArray = [];
for (var key in Blockly.StaticTyping.blocklyType) {
if ((key !== 'UNDEF') && (key !== 'UNSPECIFIED') && (key !== 'ERROR') &&
(key !== 'NULL') && (key !== 'CHILD_TYPE_MISSING')) {
typesArray.push([Blockly.StaticTyping.blocklyType[key], key]);
}
}
return typesArray;
};
......@@ -4,7 +4,8 @@
*
* Based on work of Fred Lin (gasolin@gmail.com) for Blocklyduino.
*
* @fileoverview Helper functions for generating Arduino for blocks.
* @fileoverview Helper functions for generating Arduino language (C++) for
* blocks.
*
*/
'use strict';
......@@ -95,12 +96,12 @@ var profile = {
builtin_led: [['BUILTIN_1', '13']],
pin_types: { INPUT: 'INPUT', OUTPUT: 'OUTPUT', PWM: 'PWM', SERVO: 'SERVO',
STEPPER: 'STEPPER', SPI: 'SPI' },
types : [['void', 'void'], ['Boolean', 'boolean'], ['Character', 'char'],
/*types : [['void', 'void'], ['Boolean', 'boolean'], ['Character', 'char'],
['Unsigned Character', 'unsigned char'], ['Byte', 'byte'],
['Integer', 'int'], ['Unsigned Integer', 'unsigned int'],
['Word', 'word'], ['Long', 'long'],
['Unsigned Long', 'unsigned long'], ['Short', 'short'],
['Float', 'float'], ['Double', 'double'], ['String', 'String']],
['Float', 'float'], ['Double', 'double'], ['String', 'String']],*/
spi_clock_divide: [['2 (8MHz)', 'SPI_CLOCK_DIV2'],
['4 (4MHz)', 'SPI_CLOCK_DIV4'],
['8 (2MHz)', 'SPI_CLOCK_DIV8'],
......@@ -138,14 +139,14 @@ profile['default'] = profile['arduino'];
* @param {Blockly.Workspace} workspace Workspace to generate code from.
*/
Blockly.Arduino.init = function(workspace) {
// Create a dictionary of definitions to be printed before setups.
// Create a dictionary of definitions to be printed before setups
Blockly.Arduino.definitions_ = Object.create(null);
// Create a dictionary of setups to be printed before the code.
// Create a dictionary of setups to be printed before the code
Blockly.Arduino.setups_ = Object.create(null);
// Create a dictionary of pins to check if their use conflicts
Blockly.Arduino.pins_ = Object.create(null);
// Create a dictionary mapping desired function names in definitions_
// to actual function names (to avoid collisions with user functions).
// to actual function names (to avoid collisions with user functions)
Blockly.Arduino.functionNames_ = Object.create(null);
if (!Blockly.Arduino.variableDB_) {
......@@ -189,7 +190,7 @@ Blockly.Arduino.finish = function(code) {
code = code.replace(/\n\s+$/, '\n');
code = 'void loop() {\n' + code + '\n}';
// Convert the definitions dictionary into a list.
// Convert the definitions dictionary into a list
var imports = [];
var definitions = [];
for (var name in Blockly.Arduino.definitions_) {
......@@ -201,7 +202,7 @@ Blockly.Arduino.finish = function(code) {
}
}
// Convert the setups dictionary into a list.
// Convert the setups dictionary into a list
var setups = [];
for (var name in Blockly.Arduino.setups_) {
setups.push(Blockly.Arduino.setups_[name]);
......@@ -249,19 +250,19 @@ Blockly.Arduino.quote_ = function(string) {
*/
Blockly.Arduino.scrub_ = function(block, code) {
if (code === null) {
// Block has handled code generation itself.
// Block has handled code generation itself
return '';
}
var commentCode = '';
// Only collect comments for blocks that aren't inline.
// Only collect comments for blocks that aren't inline
if (!block.outputConnection || !block.outputConnection.targetConnection) {
// Collect comment for this block.
var comment = block.getCommentText();
if (comment) {
commentCode += this.prefixLines(comment, '// ') + '\n';
}
// Collect comments for all value arguments.
// Don't collect comments for nested statements.
// Collect comments for all value arguments
// Don't collect comments for nested statements
for (var x = 0; x < block.inputList.length; x++) {
if (block.inputList[x].type == Blockly.INPUT_VALUE) {
var childBlock = block.inputList[x].connection.targetBlock();
......@@ -294,8 +295,10 @@ Blockly.Arduino.getArduinoType_ = function(typeBlockly) {
return 'undefined';
case Blockly.StaticTyping.blocklyType.UNSPECIFIED:
return 'void';
case Blockly.StaticTyping.blocklyType.NULL:
return 'NULL';
case Blockly.StaticTyping.blocklyType.NUMBER:
return 'number';
return 'int';
case Blockly.StaticTyping.blocklyType.INTEGER:
return 'int';
case Blockly.StaticTyping.blocklyType.DECIMAL:
......
......@@ -47,7 +47,9 @@ Blockly.Arduino['variables_set'] = function(block) {
Blockly.Arduino['variables_set_type'] = function(block) {
var argument0 = Blockly.Arduino.valueToCode(block, 'VARIABLE_SETTYPE_INPUT',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var varType = block.getFieldValue('VARIABLE_SETTYPE_TYPE');
var varType = Blockly.Arduino.getArduinoType_(
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