Commit a13ac7a6 authored by carlosperate's avatar carlosperate

Update addFunction to create unique function names.

parent bbb0aec6
...@@ -34,7 +34,7 @@ There was an error manipulating the sketch data!! ...@@ -34,7 +34,7 @@ There was an error manipulating the sketch data!!
## Static typing ## Static typing
- [ ] logic_ternary block getType to defines type as that of its inputs - [ ] logic_ternary block getType to defines type as that of its inputs
- [ ] logic_null block right now does not return a type, this might change - [ ] logic_null block right now does not return a type, this might change
- [ ] math_number block 'errornumber' type used for debugging, remove - [x] math_number block 'errornumber' type used for debugging, remove
- [ ] math_arithmetic getType to check types of given inputs to decide between int or float . Right now first block within sets the type. - [ ] math_arithmetic getType to check types of given inputs to decide between int or float . Right now first block within sets the type.
- [ ] math_constrain getType to check types of given inputs to decide between int or float . Right now first block within sets the type. - [ ] math_constrain getType to check types of given inputs to decide between int or float . Right now first block within sets the type.
- [ ] math_number getType to use regular expressions more efficiently - [ ] math_number getType to use regular expressions more efficiently
......
...@@ -16,7 +16,7 @@ goog.require('Blockly.Generator'); ...@@ -16,7 +16,7 @@ goog.require('Blockly.Generator');
/** /**
* Arduino code generator. * Arduino code generator.
* @type !Blockly.Generator * @type {!Blockly.Generator}
*/ */
Blockly.Arduino = new Blockly.Generator('Arduino'); Blockly.Arduino = new Blockly.Generator('Arduino');
...@@ -47,7 +47,7 @@ Blockly.Arduino.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr ...@@ -47,7 +47,7 @@ Blockly.Arduino.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
Blockly.Arduino.ORDER_MULTIPLICATIVE = 3; // * / % ~/ Blockly.Arduino.ORDER_MULTIPLICATIVE = 3; // * / % ~/
Blockly.Arduino.ORDER_ADDITIVE = 4; // + - Blockly.Arduino.ORDER_ADDITIVE = 4; // + -
Blockly.Arduino.ORDER_SHIFT = 5; // << >> Blockly.Arduino.ORDER_SHIFT = 5; // << >>
Blockly.Arduino.ORDER_RELATIONAL = 6; // is is! >= > <= < Blockly.Arduino.ORDER_RELATIONAL = 6; // >= > <= <
Blockly.Arduino.ORDER_EQUALITY = 7; // == != === !== Blockly.Arduino.ORDER_EQUALITY = 7; // == != === !==
Blockly.Arduino.ORDER_BITWISE_AND = 8; // & Blockly.Arduino.ORDER_BITWISE_AND = 8; // &
Blockly.Arduino.ORDER_BITWISE_XOR = 9; // ^ Blockly.Arduino.ORDER_BITWISE_XOR = 9; // ^
...@@ -58,6 +58,13 @@ Blockly.Arduino.ORDER_CONDITIONAL = 13; // expr ? expr : expr ...@@ -58,6 +58,13 @@ 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 generator short name for
* Blockly.Generator.prototype.FUNCTION_NAME_PLACEHOLDER_
* @type {!string}
*/
Blockly.Arduino.DEF_FUNC_NAME = Blockly.Arduino.FUNCTION_NAME_PLACEHOLDER_;
/** /**
* Initialises the database of global definitions, the setup function, function * Initialises the database of global definitions, the setup function, function
* names, and variable names. * names, and variable names.
...@@ -69,7 +76,7 @@ Blockly.Arduino.init = function(workspace) { ...@@ -69,7 +76,7 @@ Blockly.Arduino.init = function(workspace) {
// Create a dictionary of definitions to be printed after variable definitions // Create a dictionary of definitions to be printed after variable definitions
Blockly.Arduino.definitions_ = Object.create(null); Blockly.Arduino.definitions_ = Object.create(null);
// Create a dictionary of functions from the code generator // Create a dictionary of functions from the code generator
Blockly.Arduino.codefunctions_ = Object.create(null); Blockly.Arduino.codeFunctions_ = Object.create(null);
// Create a dictionary of functions created by the user // Create a dictionary of functions created by the user
Blockly.Arduino.userFunctions_ = Object.create(null); Blockly.Arduino.userFunctions_ = Object.create(null);
// Create a dictionary mapping desired function names in definitions_ // Create a dictionary mapping desired function names in definitions_
...@@ -136,24 +143,23 @@ Blockly.Arduino.finish = function(code) { ...@@ -136,24 +143,23 @@ Blockly.Arduino.finish = function(code) {
} }
// Convert the includes, functions, and setup dictionaries into lists // Convert the includes, functions, and setup dictionaries into lists
var includes = [], functions = [], userFunctions = [], setups = []; var includes = [], functions = [], setups = [];
for (var name in Blockly.Arduino.includes_) { for (var name in Blockly.Arduino.includes_) {
includes.push(Blockly.Arduino.includes_[name]); includes.push(Blockly.Arduino.includes_[name]);
} }
for (var name in Blockly.Arduino.codefunctions_) { for (var name in Blockly.Arduino.codeFunctions_) {
functions.push(Blockly.Arduino.codefunctions_[name]); functions.push(Blockly.Arduino.codeFunctions_[name]);
} }
for (var name in Blockly.Arduino.userFunctions_) { for (var name in Blockly.Arduino.userFunctions_) {
userFunctions.push(Blockly.Arduino.userFunctions_[name]); functions.push(Blockly.Arduino.userFunctions_[name]);
} }
for (var name in Blockly.Arduino.setups_) { for (var name in Blockly.Arduino.setups_) {
setups.push(Blockly.Arduino.setups_[name]); setups.push(Blockly.Arduino.setups_[name]);
} }
var allDefs = includes.join('\n') + imports.join('\n') + var allDefs = includes.join('\n') + '\n' + imports.join('\n') + '\n' +
definitions.join('\n') + functions.join('\n\n') + definitions.join('\n') + '\n' + functions.join('\n\n') +
userFunctions.join('\n\n') + '\n\nvoid setup() {\n ' + '\n\nvoid setup() {\n ' + setups.join('\n ') + '\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;
}; };
...@@ -188,17 +194,23 @@ Blockly.Arduino.addSetup = function(setupTag, code, overwrite) { ...@@ -188,17 +194,23 @@ Blockly.Arduino.addSetup = function(setupTag, code, overwrite) {
/** /**
* Adds a string of code as a function. It takes an identifier (meant to be the * Adds a string of code as a function. It takes an identifier (meant to be the
* function name) to only keep a single copy even multiple blocks might request * function name) to only keep a single copy even if multiple blocks might
* this function to appear. * request this function to be created.
* Once a function is added it will not get overwritten with new code. * A function (and its code) will only be added on first request.
* @param {!string} functionName Identifier for the function. * @param {!string} functionName Identifier for the function.
* @param {!string} code Code to be included in the setup() function. * @param {!string} code Code to be included in the setup() function.
* @param {boolean=} overwrite Description. * @param {boolean=} overwrite Description.
* @return {!string} A unique function name based on input name.
*/ */
Blockly.Arduino.addFunction = function(functionName, code) { Blockly.Arduino.addFunction = function(preferedName, code) {
if (Blockly.Arduino.codefunctions_[functionName] === undefined) { if (Blockly.Arduino.codeFunctions_[preferedName] === undefined) {
Blockly.Arduino.codefunctions_[functionName] = code; var uniqueName = Blockly.Arduino.variableDB_.getDistinctName(
preferedName, Blockly.Generator.NAME_TYPE);
Blockly.Arduino.codeFunctions_[preferedName] =
code.replace(Blockly.Arduino.DEF_FUNC_NAME, uniqueName);
Blockly.Arduino.functionNames_[preferedName] = uniqueName;
} }
return Blockly.Arduino.functionNames_[preferedName];
}; };
/** /**
...@@ -237,10 +249,8 @@ Blockly.Arduino.quote_ = function(string) { ...@@ -237,10 +249,8 @@ Blockly.Arduino.quote_ = function(string) {
* @private * @private
*/ */
Blockly.Arduino.scrub_ = function(block, code) { Blockly.Arduino.scrub_ = function(block, code) {
if (code === null) { if (code === null) { return ''; } // Block has handled code generation itself
// Block has handled code generation itself
return '';
}
var commentCode = ''; 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) { if (!block.outputConnection || !block.outputConnection.targetConnection) {
......
...@@ -110,14 +110,14 @@ Blockly.Arduino['text_length'] = function(block) { ...@@ -110,14 +110,14 @@ Blockly.Arduino['text_length'] = function(block) {
*/ */
Blockly.Arduino['text_isEmpty'] = function(block) { Blockly.Arduino['text_isEmpty'] = function(block) {
var func = []; var func = [];
func.push('boolean isStringEmpty(String msg) {'); func.push('boolean ' + Blockly.Arduino.DEF_FUNC_NAME + '(String msg) {');
func.push(' if (msg.length() == 0) {'); func.push(' if (msg.length() == 0) {');
func.push(' return true;'); func.push(' return true;');
func.push(' } else {'); func.push(' } else {');
func.push(' return false;'); func.push(' return false;');
func.push(' }'); func.push(' }');
func.push('}'); func.push('}');
Blockly.Arduino.addFunction('is_string_empty', func.join('\n')); var funcName = Blockly.Arduino.addFunction('isStringEmpty', func.join('\n'));
var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE', var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE',
Blockly.Arduino.ORDER_UNARY_POSTFIX); Blockly.Arduino.ORDER_UNARY_POSTFIX);
if (argument0 == '') { if (argument0 == '') {
...@@ -125,7 +125,7 @@ Blockly.Arduino['text_isEmpty'] = function(block) { ...@@ -125,7 +125,7 @@ Blockly.Arduino['text_isEmpty'] = function(block) {
} else { } else {
argument0 = 'String(' + argument0 + ')'; argument0 = 'String(' + argument0 + ')';
} }
var code = 'isStringEmpty(' + argument0 + ')'; var code = funcName + '(' + argument0 + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX]; return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
}; };
...@@ -189,15 +189,14 @@ Blockly.Arduino['text_prompt_ext'] = function(block) { ...@@ -189,15 +189,14 @@ Blockly.Arduino['text_prompt_ext'] = function(block) {
// Get the first Serial peripheral of arduino board // Get the first Serial peripheral of arduino board
var serialId = Blockly.Arduino.Boards.selected.serial[0][1]; var serialId = Blockly.Arduino.Boards.selected.serial[0][1];
var returnType = block.getFieldValue('TYPE'); var returnType = block.getFieldValue('TYPE');
var funcName = 'getUserInputPrompt' + returnType;
// The function code changes based on reading a number or string // The function code changes based on reading a number or string
var func = []; var func = [];
var toNumber = returnType == Blockly.StaticTyping.blocklyType.NUMBER; var toNumber = returnType == Blockly.StaticTyping.blocklyType.NUMBER;
if (toNumber) { if (toNumber) {
func.push('int ' + funcName + '(String msg) {'); func.push('int ' + Blockly.Arduino.DEF_FUNC_NAME + '(String msg) {');
} else { } else {
func.push('String ' + funcName + '(String msg) {'); func.push('String ' + Blockly.Arduino.DEF_FUNC_NAME + '(String msg) {');
} }
func.push(' ' + serialId + '.println(msg);'); func.push(' ' + serialId + '.println(msg);');
func.push(' boolean stringComplete = false;'); func.push(' boolean stringComplete = false;');
...@@ -225,7 +224,8 @@ Blockly.Arduino['text_prompt_ext'] = function(block) { ...@@ -225,7 +224,8 @@ Blockly.Arduino['text_prompt_ext'] = function(block) {
func.push(' while(Serial.available()) { Serial.read(); };'); func.push(' while(Serial.available()) { Serial.read(); };');
func.push(' return content;'); func.push(' return content;');
func.push('}'); func.push('}');
Blockly.Arduino.addFunction(funcName, func.join('\n')); var funcName = Blockly.Arduino.addFunction(
'getUserInputPrompt' + returnType, func.join('\n'));
// Only overwrite the serial set up if not present already // Only overwrite the serial set up if not present already
var setupCode = serialId + '.begin(9600);'; var setupCode = serialId + '.begin(9600);';
......
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