mirror of
https://github.com/google/blockly.git
synced 2025-12-15 13:50:08 +01:00
fix(generators): Fix an operator precedence issue in the math_number_property generators to remove extra parentheses (#5685)
* Fixed issue where the mathIsPrime function inserted extra parenthesis around certain blocks. * Added tests to generated JS * Updated generated code in Lua * Updated Generated code for tests in Dart * Updated generated code for tests in PHP * Updated generated code for tests in Python * Also changed var to const and let. Co-authored-by: jeremyjacob123 <43049656+jeremyjacob123@users.noreply.github.com> Co-authored-by: LouisCatala <86700310+LouisCatala@users.noreply.github.com> Co-authored-by: jeremyjacob123 <43049656+jeremyjacob123@users.noreply.github.com>
This commit is contained in:
@@ -164,59 +164,64 @@ Dart['math_constant'] = function(block) {
|
||||
Dart['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const number_to_check =
|
||||
Dart.valueToCode(block, 'NUMBER_TO_CHECK', Dart.ORDER_MULTIPLICATIVE);
|
||||
if (!number_to_check) {
|
||||
return ['false', Dart.ORDER_ATOMIC];
|
||||
}
|
||||
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||
const PROPERTIES = {
|
||||
'EVEN': [' % 2 == 0', Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'ODD': [' % 2 == 1', Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'WHOLE': [' % 1 == 0', Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'POSITIVE': [' > 0', Dart.ORDER_RELATIONAL,
|
||||
Dart.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', Dart.ORDER_RELATIONAL,
|
||||
Dart.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'PRIME': [null, Dart.ORDER_NONE,
|
||||
Dart.ORDER_UNARY_POSTFIX]
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = Dart.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
inputOrder) || '0';
|
||||
let code;
|
||||
if (dropdown_property === 'PRIME') {
|
||||
if (dropdownProperty === 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_('math_isPrime', [
|
||||
'bool ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if (n == 2 || n == 3) {', ' return true;', ' }',
|
||||
' // False if n is null, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||
' n % 3 == 0) {',
|
||||
' return false;', ' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {', ' return false;',
|
||||
' }', ' }', ' return true;', '}'
|
||||
]);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||
Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_(
|
||||
'math_isPrime',
|
||||
['bool ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if (n == 2 || n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is null, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||
' n % 3 == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = Dart.valueToCode(block, 'DIVISOR',
|
||||
Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||
if (divisor === '0') {
|
||||
return ['false', Dart.ORDER_ATOMIC];
|
||||
}
|
||||
code = numberToCheck + ' % ' + divisor + ' == 0';
|
||||
} else {
|
||||
code = numberToCheck + suffix;
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 == 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 == 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
code = number_to_check + ' % 1 == 0';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY':
|
||||
const divisor =
|
||||
Dart.valueToCode(block, 'DIVISOR', Dart.ORDER_MULTIPLICATIVE);
|
||||
if (!divisor) {
|
||||
return ['false', Dart.ORDER_ATOMIC];
|
||||
}
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
}
|
||||
return [code, Dart.ORDER_EQUALITY];
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
Dart['math_change'] = function(block) {
|
||||
|
||||
@@ -149,11 +149,28 @@ JavaScript['math_constant'] = function(block) {
|
||||
JavaScript['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const number_to_check = JavaScript.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
JavaScript.ORDER_MODULUS) || '0';
|
||||
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||
const PROPERTIES = {
|
||||
'EVEN': [' % 2 === 0', JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'ODD': [' % 2 === 1', JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'WHOLE': [' % 1 === 0', JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'POSITIVE': [' > 0', JavaScript.ORDER_RELATIONAL,
|
||||
JavaScript.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', JavaScript.ORDER_RELATIONAL,
|
||||
JavaScript.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'PRIME': [null, JavaScript.ORDER_NONE,
|
||||
JavaScript.ORDER_FUNCTION_CALL]
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = JavaScript.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
inputOrder) || '0';
|
||||
let code;
|
||||
if (dropdown_property === 'PRIME') {
|
||||
if (dropdownProperty === 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
const functionName = JavaScript.provideFunction_(
|
||||
'mathIsPrime',
|
||||
@@ -176,33 +193,15 @@ JavaScript['math_number_property'] = function(block) {
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, JavaScript.ORDER_FUNCTION_CALL];
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = JavaScript.valueToCode(block, 'DIVISOR',
|
||||
JavaScript.ORDER_MODULUS) || '0';
|
||||
code = numberToCheck + ' % ' + divisor + ' === 0';
|
||||
} else {
|
||||
code = numberToCheck + suffix;
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 === 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 === 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
code = number_to_check + ' % 1 === 0';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY': {
|
||||
const divisor = JavaScript.valueToCode(block, 'DIVISOR',
|
||||
JavaScript.ORDER_MODULUS) || '0';
|
||||
code = number_to_check + ' % ' + divisor + ' === 0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return [code, JavaScript.ORDER_EQUALITY];
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
JavaScript['math_change'] = function(block) {
|
||||
|
||||
@@ -126,61 +126,66 @@ Lua['math_constant'] = function(block) {
|
||||
Lua['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const number_to_check =
|
||||
Lua.valueToCode(block, 'NUMBER_TO_CHECK', Lua.ORDER_MULTIPLICATIVE) ||
|
||||
'0';
|
||||
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||
const PROPERTIES = {
|
||||
'EVEN': [' % 2 == 0', Lua.ORDER_MULTIPLICATIVE,
|
||||
Lua.ORDER_RELATIONAL],
|
||||
'ODD': [' % 2 == 1', Lua.ORDER_MULTIPLICATIVE,
|
||||
Lua.ORDER_RELATIONAL],
|
||||
'WHOLE': [' % 1 == 0', Lua.ORDER_MULTIPLICATIVE,
|
||||
Lua.ORDER_RELATIONAL],
|
||||
'POSITIVE': [' > 0', Lua.ORDER_RELATIONAL,
|
||||
Lua.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', Lua.ORDER_RELATIONAL,
|
||||
Lua.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, Lua.ORDER_MULTIPLICATIVE,
|
||||
Lua.ORDER_RELATIONAL],
|
||||
'PRIME': [null, Lua.ORDER_NONE,
|
||||
Lua.ORDER_HIGH]
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = Lua.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
inputOrder) || '0';
|
||||
let code;
|
||||
if (dropdown_property === 'PRIME') {
|
||||
if (dropdownProperty === 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
const functionName = Lua.provideFunction_('math_isPrime', [
|
||||
'function ' + Lua.FUNCTION_NAME_PLACEHOLDER_ + '(n)',
|
||||
' -- https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if n == 2 or n == 3 then', ' return true', ' end',
|
||||
' -- False if n is NaN, negative, is 1, or not whole.',
|
||||
' -- And false if n is divisible by 2 or 3.',
|
||||
' if not(n > 1) or n % 1 ~= 0 or n % 2 == 0 or n % 3 == 0 then',
|
||||
' return false', ' end',
|
||||
' -- Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for x = 6, math.sqrt(n) + 1.5, 6 do',
|
||||
' if n % (x - 1) == 0 or n % (x + 1) == 0 then', ' return false',
|
||||
' end', ' end', ' return true', 'end'
|
||||
]);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Lua.ORDER_HIGH];
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 == 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 == 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
code = number_to_check + ' % 1 == 0';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY': {
|
||||
const divisor =
|
||||
Lua.valueToCode(block, 'DIVISOR', Lua.ORDER_MULTIPLICATIVE);
|
||||
// If 'divisor' is some code that evals to 0, Lua will produce a nan.
|
||||
// Let's produce nil if we can determine this at compile-time.
|
||||
if (!divisor || divisor === '0') {
|
||||
return ['nil', Lua.ORDER_ATOMIC];
|
||||
}
|
||||
// The normal trick to implement ?: with and/or doesn't work here:
|
||||
// divisor == 0 and nil or number_to_check % divisor == 0
|
||||
// because nil is false, so allow a runtime failure. :-(
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
const functionName = Lua.provideFunction_(
|
||||
'math_isPrime',
|
||||
['function ' + Lua.FUNCTION_NAME_PLACEHOLDER_ + '(n)',
|
||||
' -- https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if n == 2 or n == 3 then',
|
||||
' return true',
|
||||
' end',
|
||||
' -- False if n is NaN, negative, is 1, or not whole.',
|
||||
' -- And false if n is divisible by 2 or 3.',
|
||||
' if not(n > 1) or n % 1 ~= 0 or n % 2 == 0 or n % 3 == 0 then',
|
||||
' return false',
|
||||
' end',
|
||||
' -- Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for x = 6, math.sqrt(n) + 1.5, 6 do',
|
||||
' if n % (x - 1) == 0 or n % (x + 1) == 0 then',
|
||||
' return false',
|
||||
' end',
|
||||
' end',
|
||||
' return true',
|
||||
'end']);
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = Lua.valueToCode(block, 'DIVISOR',
|
||||
Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||
// If 'divisor' is some code that evals to 0, Lua will produce a nan.
|
||||
// Let's produce nil if we can determine this at compile-time.
|
||||
if (divisor === '0') {
|
||||
return ['nil', Lua.ORDER_ATOMIC];
|
||||
}
|
||||
// The normal trick to implement ?: with and/or doesn't work here:
|
||||
// divisor == 0 and nil or number_to_check % divisor == 0
|
||||
// because nil is false, so allow a runtime failure. :-(
|
||||
code = numberToCheck + ' % ' + divisor + ' == 0';
|
||||
} else {
|
||||
code = numberToCheck + suffix;
|
||||
}
|
||||
return [code, Lua.ORDER_RELATIONAL];
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
Lua['math_change'] = function(block) {
|
||||
|
||||
@@ -142,53 +142,63 @@ PHP['math_constant'] = function(block) {
|
||||
PHP['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const number_to_check =
|
||||
PHP.valueToCode(block, 'NUMBER_TO_CHECK', PHP.ORDER_MODULUS) || '0';
|
||||
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||
const PROPERTIES = {
|
||||
'EVEN': ['', ' % 2 == 0', PHP.ORDER_MODULUS,
|
||||
PHP.ORDER_EQUALITY],
|
||||
'ODD': ['', ' % 2 == 1', PHP.ORDER_MODULUS,
|
||||
PHP.ORDER_EQUALITY],
|
||||
'WHOLE': ['is_int(', ')', PHP.ORDER_NONE,
|
||||
PHP.ORDER_FUNCTION_CALL],
|
||||
'POSITIVE': ['', ' > 0', PHP.ORDER_RELATIONAL,
|
||||
PHP.ORDER_RELATIONAL],
|
||||
'NEGATIVE': ['', ' < 0', PHP.ORDER_RELATIONAL,
|
||||
PHP.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, null, PHP.ORDER_MODULUS,
|
||||
PHP.ORDER_EQUALITY],
|
||||
'PRIME': [null, null, PHP.ORDER_NONE,
|
||||
PHP.ORDER_FUNCTION_CALL]
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [prefix, suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = PHP.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
inputOrder) || '0';
|
||||
let code;
|
||||
if (dropdown_property === 'PRIME') {
|
||||
if (dropdownProperty === 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
const functionName = PHP.provideFunction_('math_isPrime', [
|
||||
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if ($n == 2 || $n == 3) {', ' return true;', ' }',
|
||||
' // False if n is NaN, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (!is_numeric($n) || $n <= 1 || $n % 1 != 0 || $n % 2 == 0 ||' +
|
||||
' $n % 3 == 0) {',
|
||||
' return false;', ' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for ($x = 6; $x <= sqrt($n) + 1; $x += 6) {',
|
||||
' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {',
|
||||
' return false;', ' }', ' }', ' return true;', '}'
|
||||
]);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 == 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 == 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
code = 'is_int(' + number_to_check + ')';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY': {
|
||||
const divisor =
|
||||
PHP.valueToCode(block, 'DIVISOR', PHP.ORDER_MODULUS) || '0';
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
const functionName = PHP.provideFunction_(
|
||||
'math_isPrime',
|
||||
['function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if ($n == 2 || $n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is NaN, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (!is_numeric($n) || $n <= 1 || $n % 1 != 0 || $n % 2 == 0 ||' +
|
||||
' $n % 3 == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for ($x = 6; $x <= sqrt($n) + 1; $x += 6) {',
|
||||
' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = PHP.valueToCode(block, 'DIVISOR',
|
||||
PHP.ORDER_MODULUS) || '0';
|
||||
if (divisor === '0') {
|
||||
return ['false', PHP.ORDER_ATOMIC];
|
||||
|
||||
}
|
||||
code = numberToCheck + ' % ' + divisor + ' == 0';
|
||||
} else {
|
||||
code = prefix + numberToCheck + suffix;
|
||||
}
|
||||
return [code, PHP.ORDER_EQUALITY];
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
PHP['math_change'] = function(block) {
|
||||
|
||||
@@ -152,65 +152,68 @@ Python['math_constant'] = function(block) {
|
||||
};
|
||||
|
||||
Python['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const number_to_check =
|
||||
Python.valueToCode(
|
||||
block, 'NUMBER_TO_CHECK', Python.ORDER_MULTIPLICATIVE) ||
|
||||
'0';
|
||||
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const PROPERTIES = {
|
||||
'EVEN': [' % 2 == 0', Python.ORDER_MULTIPLICATIVE,
|
||||
Python.ORDER_RELATIONAL],
|
||||
'ODD': [' % 2 == 1', Python.ORDER_MULTIPLICATIVE,
|
||||
Python.ORDER_RELATIONAL],
|
||||
'WHOLE': [' % 1 == 0', Python.ORDER_MULTIPLICATIVE,
|
||||
Python.ORDER_RELATIONAL],
|
||||
'POSITIVE': [' > 0', Python.ORDER_RELATIONAL,
|
||||
Python.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', Python.ORDER_RELATIONAL,
|
||||
Python.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, Python.ORDER_MULTIPLICATIVE,
|
||||
Python.ORDER_RELATIONAL],
|
||||
'PRIME': [null, Python.ORDER_NONE,
|
||||
Python.ORDER_FUNCTION_CALL]
|
||||
}
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = Python.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
inputOrder) || '0';
|
||||
let code;
|
||||
if (dropdown_property === 'PRIME') {
|
||||
if (dropdownProperty === 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
Python.definitions_['import_math'] = 'import math';
|
||||
Python.definitions_['from_numbers_import_Number'] =
|
||||
'from numbers import Number';
|
||||
const functionName = Python.provideFunction_('math_isPrime', [
|
||||
'def ' + Python.FUNCTION_NAME_PLACEHOLDER_ + '(n):',
|
||||
' # https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' # If n is not a number but a string, try parsing it.',
|
||||
' if not isinstance(n, Number):', ' try:', ' n = float(n)',
|
||||
' except:', ' return False',
|
||||
' if n == 2 or n == 3:', ' return True',
|
||||
' # False if n is negative, is 1, or not whole,' +
|
||||
' or if n is divisible by 2 or 3.',
|
||||
' if n <= 1 or n % 1 != 0 or n % 2 == 0 or n % 3 == 0:',
|
||||
' return False',
|
||||
' # Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for x in range(6, int(math.sqrt(n)) + 2, 6):',
|
||||
' if n % (x - 1) == 0 or n % (x + 1) == 0:', ' return False',
|
||||
' return True'
|
||||
]);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 == 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 == 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
code = number_to_check + ' % 1 == 0';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY': {
|
||||
const divisor =
|
||||
Python.valueToCode(block, 'DIVISOR', Python.ORDER_MULTIPLICATIVE);
|
||||
// If 'divisor' is some code that evals to 0, Python will raise an error.
|
||||
if (!divisor || divisor === '0') {
|
||||
return ['False', Python.ORDER_ATOMIC];
|
||||
}
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
const functionName = Python.provideFunction_(
|
||||
'math_isPrime',
|
||||
['def ' + Python.FUNCTION_NAME_PLACEHOLDER_ + '(n):',
|
||||
' # https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' # If n is not a number but a string, try parsing it.',
|
||||
' if not isinstance(n, Number):',
|
||||
' try:',
|
||||
' n = float(n)',
|
||||
' except:',
|
||||
' return False',
|
||||
' if n == 2 or n == 3:',
|
||||
' return True',
|
||||
' # False if n is negative, is 1, or not whole,' +
|
||||
' or if n is divisible by 2 or 3.',
|
||||
' if n <= 1 or n % 1 != 0 or n % 2 == 0 or n % 3 == 0:',
|
||||
' return False',
|
||||
' # Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for x in range(6, int(math.sqrt(n)) + 2, 6):',
|
||||
' if n % (x - 1) == 0 or n % (x + 1) == 0:',
|
||||
' return False',
|
||||
' return True']);
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = Python.valueToCode(block, 'DIVISOR',
|
||||
Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
// If 'divisor' is some code that evals to 0, Python will raise an error.
|
||||
if (divisor === '0') {
|
||||
return ['False', Python.ORDER_ATOMIC];
|
||||
}
|
||||
}
|
||||
return [code, Python.ORDER_RELATIONAL];
|
||||
code = numberToCheck + ' % ' + divisor + ' == 0';
|
||||
} else {
|
||||
code = numberToCheck + suffix;
|
||||
};
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
Python['math_change'] = function(block) {
|
||||
|
||||
Reference in New Issue
Block a user