Skip to content

Commit

Permalink
Merge branch 'develop_math_number_property_operator_precedence' into …
Browse files Browse the repository at this point in the history
…develop_math_number_property_operator_precedence_lua
  • Loading branch information
jeremyjacob123 authored Nov 6, 2021
2 parents f839071 + fa2d761 commit e305021
Show file tree
Hide file tree
Showing 73 changed files with 2,472 additions and 2,456 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/check_clang_format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: DoozyX/clang-format-lint-action@v0.12
- uses: DoozyX/clang-format-lint-action@v0.13
with:
source: 'core'
extensions: 'js'
clangFormatVersion: 12
style: Google
# This should be as close as possible to the version that the npm
# package supports. This can be found by running:
# npx clang-format --version.
clangFormatVersion: 13

# The Report clang format workflow (report_clang_format.yml) will
# run (if required) after this one to post a comment to the PR.
Expand Down
2 changes: 1 addition & 1 deletion core/toolbox/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ Toolbox.prototype.selectItemByPosition = function(position) {
* @protected
*/
Toolbox.prototype.updateFlyout_ = function(oldItem, newItem) {
if ((oldItem === newItem && !newItem.isCollapsible()) || !newItem ||
if (!newItem || (oldItem === newItem && !newItem.isCollapsible()) ||
!newItem.getContents().length) {
this.flyout_.hide();
} else {
Expand Down
64 changes: 33 additions & 31 deletions generators/javascript/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,37 @@ Blockly.JavaScript['math_constant'] = function(block) {
Blockly.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 = Blockly.JavaScript.valueToCode(block, 'NUMBER_TO_CHECK',
Blockly.JavaScript.ORDER_MODULUS) || '0';
const dropdown_property = block.getFieldValue('PROPERTY');
const PROPERTIES = {
'EVEN': [' % 2 === 0', Blockly.JavaScript.ORDER_MODULUS,
Blockly.JavaScript.ORDER_EQUALITY],
'ODD': [' % 2 === 1', Blockly.JavaScript.ORDER_MODULUS,
Blockly.JavaScript.ORDER_EQUALITY],
'WHOLE': [' % 1 === 0', Blockly.JavaScript.ORDER_MODULUS,
Blockly.JavaScript.ORDER_EQUALITY],
'POSITIVE': [' > 0', Blockly.JavaScript.ORDER_RELATIONAL,
Blockly.JavaScript.ORDER_RELATIONAL],
'NEGATIVE': [' < 0', Blockly.JavaScript.ORDER_RELATIONAL,
Blockly.JavaScript.ORDER_RELATIONAL],
'DIVISIBLE_BY': [null, Blockly.JavaScript.ORDER_MODULUS,
Blockly.JavaScript.ORDER_EQUALITY],
'PRIME': [null, Blockly.JavaScript.ORDER_NONE,
Blockly.JavaScript.ORDER_FUNCTION_CALL]
};
const dropdownProperty = block.getFieldValue('PROPERTY');
const tuple = PROPERTIES[dropdownProperty];
const suffix = tuple[0];
const inputOrder = tuple[1];
const outputOrder = tuple[2];
const numberToCheck = Blockly.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 = Blockly.JavaScript.provideFunction_(
'mathIsPrime',
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
' if (n === 2 || n === 3) {',
' if (n == 2 || n == 3) {',
' return true;',
' }',
' // False if n is NaN, negative, is 1, or not whole.',
Expand All @@ -174,33 +194,15 @@ Blockly.JavaScript['math_number_property'] = function(block) {
' }',
' return true;',
'}']);
code = functionName + '(' + number_to_check + ')';
return [code, Blockly.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 = Blockly.JavaScript.valueToCode(block, 'DIVISOR',
Blockly.JavaScript.ORDER_MODULUS) || '0';
code = number_to_check + ' % ' + divisor + ' === 0';
break;
}
code = functionName + '(' + numberToCheck + ')';
} else if (dropdownProperty === 'DIVISIBLE_BY') {
const divisor = Blockly.JavaScript.valueToCode(block, 'DIVISOR',
Blockly.JavaScript.ORDER_MODULUS) || '0';
code = numberToCheck + ' % ' + divisor + ' === 0';
} else {
code = numberToCheck + suffix;
}
return [code, Blockly.JavaScript.ORDER_EQUALITY];
return [code, outputOrder];
};

Blockly.JavaScript['math_change'] = function(block) {
Expand Down
24 changes: 12 additions & 12 deletions generators/lua/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ goog.require('Blockly.Lua');

Blockly.Lua['colour_picker'] = function(block) {
// Colour picker.
var code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
const code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
return [code, Blockly.Lua.ORDER_ATOMIC];
};

Blockly.Lua['colour_random'] = function(block) {
// Generate a random colour.
var code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
const code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'colour_rgb',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
' g = math.floor(math.min(100, math.max(0, g)) * 2.55 + .5)',
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
' return string.format("#%02x%02x%02x", r, g, b)',
'end']);
var r = Blockly.Lua.valueToCode(block, 'RED',
const r = Blockly.Lua.valueToCode(block, 'RED',
Blockly.Lua.ORDER_NONE) || 0;
var g = Blockly.Lua.valueToCode(block, 'GREEN',
const g = Blockly.Lua.valueToCode(block, 'GREEN',
Blockly.Lua.ORDER_NONE) || 0;
var b = Blockly.Lua.valueToCode(block, 'BLUE',
const b = Blockly.Lua.valueToCode(block, 'BLUE',
Blockly.Lua.ORDER_NONE) || 0;
var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};

Blockly.Lua['colour_blend'] = function(block) {
// Blend two colours together.
var functionName = Blockly.Lua.provideFunction_(
const functionName = Blockly.Lua.provideFunction_(
'colour_blend',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(colour1, colour2, ratio)',
Expand All @@ -64,12 +64,12 @@ Blockly.Lua['colour_blend'] = function(block) {
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
' return string.format("#%02x%02x%02x", r, g, b)',
'end']);
var colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
const colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
var colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
const colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
var ratio = Blockly.Lua.valueToCode(block, 'RATIO',
const ratio = Blockly.Lua.valueToCode(block, 'RATIO',
Blockly.Lua.ORDER_NONE) || 0;
var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
const code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
Loading

0 comments on commit e305021

Please sign in to comment.