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

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