Commit 73f49407 authored by Carlos Pereira Atencio's avatar Carlos Pereira Atencio Committed by carlospamg

Added simple type recognition for Arduino code generation for variable declarations.

This still needs work and testing for blocks that are not simple numbers or strings.
parent 3e5a1ef2
...@@ -64,9 +64,8 @@ Blockly.Arduino.ORDER_CONDITIONAL = 13; // expr ? expr : expr ...@@ -64,9 +64,8 @@ Blockly.Arduino.ORDER_CONDITIONAL = 13; // expr ? expr : expr
Blockly.Arduino.ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |= Blockly.Arduino.ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
Blockly.Arduino.ORDER_NONE = 99; // (...) Blockly.Arduino.ORDER_NONE = 99; // (...)
/* /**
* Arduino Board profiles * Arduino Board profiles
*
*/ */
var profile = { var profile = {
arduino: { arduino: {
...@@ -92,7 +91,7 @@ var profile = { ...@@ -92,7 +91,7 @@ var profile = {
} }
} }
//set default profile to arduino standard-compatible board // Set default profile to arduino standard-compatible board
profile["default"] = profile["arduino"]; profile["default"] = profile["arduino"];
/** /**
...@@ -106,23 +105,78 @@ Blockly.Arduino.init = function() { ...@@ -106,23 +105,78 @@ Blockly.Arduino.init = function() {
if (Blockly.Variables) { if (Blockly.Variables) {
if (!Blockly.Arduino.variableDB_) { if (!Blockly.Arduino.variableDB_) {
Blockly.Arduino.variableDB_ = Blockly.Arduino.variableDB_ = new Blockly.Names(Blockly.Arduino.RESERVED_WORDS_);
new Blockly.Names(Blockly.Arduino.RESERVED_WORDS_);
} else { } else {
Blockly.Arduino.variableDB_.reset(); Blockly.Arduino.variableDB_.reset();
} }
var defvars = []; // Iterate through the blocks to capture variables with first value
var variables = Blockly.Variables.allVariables(); var variableWithType = Object.create(null);
for (var x = 0; x < variables.length; x++) { var blocks = Blockly.mainWorkspace.getAllBlocks();
defvars[x] = 'int ' + for (var x = 0; x < blocks.length; x++) {
Blockly.Arduino.variableDB_.getName(variables[x], var func = blocks[x].getVars;
Blockly.Variables.NAME_TYPE); if (func) {
var blockVariables = func.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;
for (var name in variableWithType) {
if (name == blockVariables[y]) {
unique = false;
break;
}
}
// 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));
}
}
}
}
// Set variable declarations
var variableDeclarations = [];
for (var name in variableWithType) {
variableDeclarations.push(variableWithType[name] + ' ' + name + ';');
} }
Blockly.Arduino.definitions_['variables'] = defvars.join('\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 'defineme';
}
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 'dontknowInt';
}
// For now the default is integer, will have to figure out how to
// evaluate other blocks as inputs
return 'integer';
}
/** /**
* Prepend the generated code with the variable definitions. * Prepend the generated code with the variable definitions.
* @param {string} code Generated code. * @param {string} code Generated code.
...@@ -132,7 +186,7 @@ Blockly.Arduino.finish = function(code) { ...@@ -132,7 +186,7 @@ Blockly.Arduino.finish = function(code) {
// Indent every line. // Indent every line.
code = ' ' + code.replace(/\n/g, '\n '); code = ' ' + code.replace(/\n/g, '\n ');
code = code.replace(/\n\s+$/, '\n'); code = code.replace(/\n\s+$/, '\n');
code = 'void loop() {\n' + code + '}'; code = 'void loop() {\n' + code + '\n}';
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
var imports = []; var imports = [];
...@@ -152,7 +206,8 @@ Blockly.Arduino.finish = function(code) { ...@@ -152,7 +206,8 @@ Blockly.Arduino.finish = function(code) {
setups.push(Blockly.Arduino.setups_[name]); setups.push(Blockly.Arduino.setups_[name]);
} }
var allDefs = imports.join('\n') + definitions.join('\n') + '\nvoid setup() {\n '+setups.join('\n ') + '\n}'; var allDefs = imports.join('\n') + definitions.join('\n') +
'\nvoid setup() {\n '+ setups.join('\n ') + '\n}';
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code; return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
}; };
......
...@@ -27,89 +27,6 @@ goog.provide('Blockly.Arduino.variables'); ...@@ -27,89 +27,6 @@ goog.provide('Blockly.Arduino.variables');
goog.require('Blockly.Arduino'); goog.require('Blockly.Arduino');
//if (!Blockly.Language) Blockly.Language = {};
//Blockly.Language.variables_get = {
// // Variable getter.
// category: null, // Variables are handled specially.
// helpUrl: Blockly.LANG_VARIABLES_GET_HELPURL,
// init: function() {
// this.setColour(330);
// this.appendDummyInput("")
// .appendTitle(Blockly.LANG_VARIABLES_GET_TITLE_1)
// .appendTitle(new Blockly.FieldVariable(
// Blockly.LANG_VARIABLES_GET_ITEM), 'VAR');
// this.setOutput(true, null);
// this.setTooltip(Blockly.LANG_VARIABLES_GET_TOOLTIP_1);
// },
// getVars: function() {
// return [this.getTitleValue('VAR')];
// },
// renameVar: function(oldName, newName) {
// if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) {
// this.setTitleValue(newName, 'VAR');
// }
// }
//};
//Blockly.Language.variables_declare = {
// // Variable setter.
// category: null, // Variables are handled specially.
// helpUrl: Blockly.LANG_VARIABLES_SET_HELPURL,
// init: function() {
// this.setColour(330);
// this.appendValueInput('VALUE', null)
// .appendTitle('Declare')
// .appendTitle(new Blockly.FieldVariable(
// Blockly.LANG_VARIABLES_SET_ITEM), 'VAR')
// .appendTitle("as")
// .appendTitle(new Blockly.FieldDropdown([["Number", "int"]]), "TYPE")
// .appendTitle("value");
// this.setPreviousStatement(true);
// this.setNextStatement(true);
// this.setTooltip(Blockly.LANG_VARIABLES_SET_TOOLTIP_1);
// },
// getVars: function() {
// return [this.getTitleValue('VAR')];
// },
// renameVar: function(oldName, newName) {
// if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) {
// this.setTitleValue(newName, 'VAR');
// }
// }
//};
//Blockly.Language.variables_set = {
// // Variable setter.
// category: null, // Variables are handled specially.
// helpUrl: Blockly.LANG_VARIABLES_SET_HELPURL,
// init: function() {
// this.setColour(330);
// this.appendValueInput('VALUE')
// .appendTitle(Blockly.LANG_VARIABLES_SET_TITLE_1)
// .appendTitle(new Blockly.FieldVariable(
// Blockly.LANG_VARIABLES_SET_ITEM), 'VAR');
// this.setPreviousStatement(true);
// this.setNextStatement(true);
// this.setTooltip(Blockly.LANG_VARIABLES_SET_TOOLTIP_1);
// },
// getVars: function() {
// return [this.getTitleValue('VAR')];
// },
// renameVar: function(oldName, newName) {
// if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) {
// this.setTitleValue(newName, 'VAR');
// }
// }
//};
/**
* @fileoverview Variable blocks for Arduino.
* @author gasolin@gmail.com (Fred Lin)
*/
//Blockly.Arduino = Blockly.Generator.get('Arduino');
Blockly.Arduino['variables_get'] = function(block) { Blockly.Arduino['variables_get'] = function(block) {
// Variable getter. // Variable getter.
...@@ -118,17 +35,6 @@ Blockly.Arduino['variables_get'] = function(block) { ...@@ -118,17 +35,6 @@ Blockly.Arduino['variables_get'] = function(block) {
return [code, Blockly.Arduino.ORDER_ATOMIC]; return [code, Blockly.Arduino.ORDER_ATOMIC];
}; };
//Blockly.Arduino.variables_declare = function() {
// // Variable setter.
// var dropdown_type = this.getTitleValue('TYPE');
// //TODO: settype to variable
// var argument0 = Blockly.Arduino.valueToCode(this, 'VALUE',
// Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
// var varName = Blockly.Arduino.variableDB_.getName(this.getTitleValue('VAR'),
// Blockly.Variables.NAME_TYPE);
// Blockly.Arduino.setups_['setup_var'+varName] = varName + ' = ' + argument0 + ';\n';
// return '';
//};
Blockly.Arduino['variables_set'] = function(block) { Blockly.Arduino['variables_set'] = function(block) {
// Variable setter. // Variable setter.
......
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