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:
Richard Doherty
2022-01-27 10:54:05 -05:00
committed by GitHub
parent f68043d3eb
commit a31003fab9
11 changed files with 427 additions and 258 deletions

View File

@@ -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) {