Commit d9fd9c29 authored by carlosperate's avatar carlosperate

Fix issue with math_number_property block and updated with addFunction calls.

Fix issue with math_number_property generator created invalid code.
Fix issue with math_number_property block definition return type.
parent a13ac7a6
...@@ -283,7 +283,7 @@ Blockly.Blocks['math_number_property'] = { ...@@ -283,7 +283,7 @@ Blockly.Blocks['math_number_property'] = {
* Assigns a type to the block, all these operations return booleans. * Assigns a type to the block, all these operations return booleans.
*/ */
getType: function() { getType: function() {
Blockly.StaticTyping.blocklyType.BOOLEAN; return Blockly.StaticTyping.blocklyType.BOOLEAN;
} }
}; };
......
...@@ -175,37 +175,35 @@ Blockly.Arduino['math_constant'] = function(block) { ...@@ -175,37 +175,35 @@ Blockly.Arduino['math_constant'] = function(block) {
* @return {array} Completed code with order of operation. * @return {array} Completed code with order of operation.
*/ */
Blockly.Arduino['math_number_property'] = function(block) { Blockly.Arduino['math_number_property'] = function(block) {
//
var number_to_check = Blockly.Arduino.valueToCode(block, 'NUMBER_TO_CHECK', var number_to_check = Blockly.Arduino.valueToCode(block, 'NUMBER_TO_CHECK',
Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0'; Blockly.Arduino.ORDER_MULTIPLICATIVE) || '0';
var dropdown_property = block.getFieldValue('PROPERTY'); var dropdown_property = block.getFieldValue('PROPERTY');
var code; var code;
if (dropdown_property == 'PRIME') { if (dropdown_property == 'PRIME') {
//FIXME: this doesn't work yet var func = [
// Prime is a special case as it is not a one-liner test. 'boolean ' + Blockly.Arduino.DEF_FUNC_NAME + '(int n) {',
var functionName = Blockly.Arduino.provideFunction_( ' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
'math_isPrime', ' if (n == 2 || n == 3) {',
[ 'function ' + Blockly.Arduino.FUNCTION_NAME_PLACEHOLDER_ + '(n) {', ' return true;',
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods', ' }',
' if (n == 2 || n == 3) {', ' // False if n is NaN, negative, is 1.',
' return true;', ' // And false if n is divisible by 2 or 3.',
' }', ' if (isnan(n) || (n <= 1) || (n == 1) || (n % 2 == 0) || ' +
' // False if n is NaN, negative, is 1, or not whole.', '(n % 3 == 0)) {',
' // And false if n is divisible by 2 or 3.', ' return false;',
' if (isnan(n) || (n <= 1) || (n % 1 != 0) || (n % 2 == 0) ||' + ' }',
' (n % 3 == 0)) {', ' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
' return false;', ' for (int x = 6; x <= sqrt(n) + 1; x += 6) {',
' }', ' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).', ' return false;',
' for (int x = 6; x <= sqrt(n) + 1; x += 6) {', ' }',
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {', ' }',
' return false;', ' return true;',
' }', '}'];
' }', var funcName = Blockly.Arduino.addFunction('mathIsPrime', func.join('\n'));
' return true;', Blockly.Arduino.addInclude('math', '#include <math.h>');
'}']); code = funcName + '(' + number_to_check + ')';
code = functionName + '(' + number_to_check + ')'; return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
return [code, Blockly.Arduino.ORDER_FUNCTION_CALL];
} }
switch (dropdown_property) { switch (dropdown_property) {
case 'EVEN': case 'EVEN':
...@@ -215,7 +213,8 @@ Blockly.Arduino['math_number_property'] = function(block) { ...@@ -215,7 +213,8 @@ Blockly.Arduino['math_number_property'] = function(block) {
code = number_to_check + ' % 2 == 1'; code = number_to_check + ' % 2 == 1';
break; break;
case 'WHOLE': case 'WHOLE':
code = number_to_check + ' % 1 == 0'; Blockly.Arduino.addInclude('math', '#include <math.h>');
code = '(floor(' + number_to_check + ') == ' + number_to_check +')';
break; break;
case 'POSITIVE': case 'POSITIVE':
code = number_to_check + ' > 0'; code = number_to_check + ' > 0';
...@@ -248,20 +247,17 @@ Blockly.Arduino['math_change'] = function(block) { ...@@ -248,20 +247,17 @@ Blockly.Arduino['math_change'] = function(block) {
return varName + ' += ' + argument0 + ';\n'; return varName + ' += ' + argument0 + ';\n';
}; };
/** /** Rounding functions have a single operand. */
* Rounding functions have a single operand.
*/
Blockly.Arduino['math_round'] = Blockly.Arduino['math_single']; Blockly.Arduino['math_round'] = Blockly.Arduino['math_single'];
/** /** Trigonometry functions have a single operand. */
* Trigonometry functions have a single operand.
*/
Blockly.Arduino['math_trig'] = Blockly.Arduino['math_single']; Blockly.Arduino['math_trig'] = Blockly.Arduino['math_single'];
/** /**
* Generator for the math function to a list. * Generator for the math function to a list.
* Arduino code: ??? * Arduino code: ???
* TODO: List have to be implemented first. Removed from toolbox for now. * TODO: List have to be implemented first. Removed from toolbox for now.
* The first case has been done as an example, the other are still left.
* @param {!Blockly.Block} block Block to generate the code from. * @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation. * @return {array} Completed code with order of operation.
*/ */
...@@ -272,19 +268,15 @@ Blockly.Arduino['math_on_list'] = function(block) { ...@@ -272,19 +268,15 @@ Blockly.Arduino['math_on_list'] = function(block) {
var code; var code;
switch (func) { switch (func) {
case 'SUM': case 'SUM':
if (!Blockly.Arduino.definitions_['math_sum']) { var func = [
var functionName = Blockly.Arduino.variableDB_.getDistinctName( 'num ' + Blockly.Arduino.DEF_FUNC_NAME + '(List myList) {',
'math_sum', Blockly.Generator.NAME_TYPE); ' num sumVal = 0;',
Blockly.Arduino.math_on_list.math_sum = functionName; ' myList.forEach((num entry) {sumVal += entry;});',
var func = []; ' return sumVal;',
func.push('num ' + functionName + '(List myList) {'); '}'];
func.push(' num sumVal = 0;'); var functionName = Blockly.Arduino.addFunction(
func.push(' myList.forEach((num entry) {sumVal += entry;});'); 'mathListSum', func.join('\n'));
func.push(' return sumVal;'); code = functionName + '(' + list + ')';
func.push('}');
Blockly.Arduino.definitions_['math_sum'] = func.join('\n');
}
code = Blockly.Arduino.math_on_list.math_sum + '(' + list + ')';
break; break;
case 'MIN': case 'MIN':
if (!Blockly.Arduino.definitions_['math_min']) { if (!Blockly.Arduino.definitions_['math_min']) {
...@@ -425,7 +417,8 @@ Blockly.Arduino['math_on_list'] = function(block) { ...@@ -425,7 +417,8 @@ Blockly.Arduino['math_on_list'] = function(block) {
'Math.pow(x - mean, 2));'); 'Math.pow(x - mean, 2));');
func.push(' return Math.sqrt(sumSquare / n);'); func.push(' return Math.sqrt(sumSquare / n);');
func.push('}'); func.push('}');
Blockly.Arduino.definitions_['math_standard_deviation'] = func.join('\n'); Blockly.Arduino.definitions_['math_standard_deviation'] =
func.join('\n');
} }
code = Blockly.Arduino.math_on_list.math_standard_deviation + code = Blockly.Arduino.math_on_list.math_standard_deviation +
'(' + list + ')'; '(' + list + ')';
...@@ -481,7 +474,7 @@ Blockly.Arduino['math_constrain'] = function(block) { ...@@ -481,7 +474,7 @@ Blockly.Arduino['math_constrain'] = function(block) {
Blockly.Arduino.ORDER_NONE) || '0'; Blockly.Arduino.ORDER_NONE) || '0';
var code = '(' + argument0 + ' < ' + argument1 + ' ? ' + argument1 + var code = '(' + argument0 + ' < ' + argument1 + ' ? ' + argument1 +
' : ( ' + argument0 + ' > ' + argument2 + ' ? ' + argument2 + ' : ' + ' : ( ' + argument0 + ' > ' + argument2 + ' ? ' + argument2 + ' : ' +
argument0 + '))' argument0 + '))';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX]; return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
}; };
...@@ -497,24 +490,21 @@ Blockly.Arduino['math_random_int'] = function(block) { ...@@ -497,24 +490,21 @@ Blockly.Arduino['math_random_int'] = function(block) {
Blockly.Arduino.ORDER_NONE) || '0'; Blockly.Arduino.ORDER_NONE) || '0';
var argument1 = Blockly.Arduino.valueToCode(block, 'TO', var argument1 = Blockly.Arduino.valueToCode(block, 'TO',
Blockly.Arduino.ORDER_NONE) || '0'; Blockly.Arduino.ORDER_NONE) || '0';
if (!Blockly.Arduino.definitions_['math_random_int']) { var functionName = Blockly.Arduino.variableDB_.getDistinctName(
var functionName = Blockly.Arduino.variableDB_.getDistinctName( 'math_random_int', Blockly.Generator.NAME_TYPE);
'math_random_int', Blockly.Generator.NAME_TYPE); Blockly.Arduino.math_random_int.random_function = functionName;
Blockly.Arduino.math_random_int.random_function = functionName; var func = [
var func = []; 'int ' + Blockly.Arduino.DEF_FUNC_NAME + '(int min, int max) {',
func.push('int ' + functionName + '(int min, int max) {'); ' if (min > max) {',
func.push(' if (min > max) {'); ' // Swap min and max to ensure min is smaller.',
func.push(' // Swap min and max to ensure min is smaller.'); ' int temp = min;',
func.push(' int temp = min;'); ' min = max;',
func.push(' min = max;'); ' max = temp;',
func.push(' max = temp;'); ' }',
func.push(' }'); ' return min + (rand() % (max - min + 1));',
func.push(' return min + (rand() % (max - min + 1));'); '}'];
func.push('}'); var funcName = Blockly.Arduino.addFunction('mathRandomInt', func.join('\n'));
Blockly.Arduino.definitions_['math_random_int'] = func.join('\n'); var code = funcName + '(' + argument0 + ', ' + argument1 + ')';
}
var code = Blockly.Arduino.math_random_int.random_function +
'(' + argument0 + ', ' + argument1 + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX]; return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
}; };
......
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