Skip to content

Commit

Permalink
fix(generators): Fix an operator precedence issue in the math_number_…
Browse files Browse the repository at this point in the history
…property generators to remove extra parentheses (google#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>
  • Loading branch information
3 people authored Jan 27, 2022
1 parent f68043d commit a31003f
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 257 deletions.
105 changes: 55 additions & 50 deletions generators/dart/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
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;
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;
}
return [code, Dart.ORDER_EQUALITY];
return [code, outputOrder];
};

Dart['math_change'] = function(block) {
Expand Down
59 changes: 29 additions & 30 deletions generators/javascript/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -176,33 +193,15 @@ JavaScript['math_number_property'] = function(block) {
' }',
' return true;',
'}']);
code = functionName + '(' + number_to_check + ')';
return [code, JavaScript.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 = JavaScript.valueToCode(block, 'DIVISOR',
JavaScript.ORDER_MODULUS) || '0';
code = number_to_check + ' % ' + divisor + ' === 0';
break;
}
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;
}
return [code, JavaScript.ORDER_EQUALITY];
return [code, outputOrder];
};

JavaScript['math_change'] = function(block) {
Expand Down
107 changes: 56 additions & 51 deletions generators/lua/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit a31003f

Please sign in to comment.