Commit 4ea8699e authored by Susan Rati Lane's avatar Susan Rati Lane Committed by Evan W. Patton

Add bitwise operator blocks

Added bitwise-and. bitwise-or, and bitwise-xor Math blocks.
parent c9c79272
......@@ -312,6 +312,107 @@ Blockly.Blocks['math_power'] = {
typeblock: [{translatedName: Blockly.Msg.LANG_MATH_ARITHMETIC_POWER}]
};
Blockly.Blocks['math_bitwise'] = {
category: 'Math',
helpUrl: function () {
var mode = this.getFieldValue('OP');
return Blockly.Blocks.math_bitwise.HELPURLS()[mode];
},
init: function () {
// Assign 'this' to a variable for use in the closures below.
var thisBlock = this;
this.setColour(Blockly.MATH_CATEGORY_HUE);
this.setOutput(true, Blockly.Blocks.Utilities.YailTypeToBlocklyType("number", Blockly.Blocks.Utilities.OUTPUT));
this.appendValueInput('NUM0')
.setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("number", Blockly.Blocks.Utilities.INPUT))
.appendField(new Blockly.FieldDropdown(this.OPERATORS), 'OP');
this.appendValueInput('NUM1')
.setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("number", Blockly.Blocks.Utilities.INPUT));
this.setInputsInline(false);
this.setTooltip(function () {
var mode = thisBlock.getFieldValue('OP');
return Blockly.Blocks.math_bitwise.TOOLTIPS()[mode];
});
this.setMutator(new Blockly.Mutator(['math_mutator_item']));
this.itemCount_ = 2;
this.valuesToSave = {'OP': null};
this.emptyInputName = 'EMPTY';
this.repeatingInputName = 'NUM';
},
mutationToDom: Blockly.mutationToDom,
domToMutation: Blockly.domToMutation,
decompose: function (workspace) {
return Blockly.decompose(workspace, 'math_mutator_item', this);
},
compose: Blockly.compose,
saveConnections: Blockly.saveConnections,
addEmptyInput: function () {
var input = this.appendDummyInput(this.emptyInputName);
input.appendField(new Blockly.FieldDropdown(this.OPERATORS), 'OP');
this.setFieldValue(this.valuesToSave['OP'], 'OP');
},
addInput: function (inputNum) {
var input = this.appendValueInput(this.repeatingInputName + inputNum)
.setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("number", Blockly.Blocks.Utilities.INPUT));
if (inputNum == 0) {
input.appendField(new Blockly.FieldDropdown(this.OPERATORS), 'OP');
this.setFieldValue(this.valuesToSave['OP'], 'OP');
}
return input;
},
updateContainerBlock: function (containerBlock) {
for (var i = 0; i < Blockly.Blocks.math_bitwise.OPERATORS.length; i++) {
if (Blockly.Blocks.math_bitwise.OPERATORS[i][1] == this.getFieldValue("OP")) {
containerBlock.setFieldValue(Blockly.Blocks.math_bitwise.OPERATORS[i][0], "CONTAINER_TEXT");
}
}
},
typeblock: [{
translatedName: Blockly.Msg.LANG_MATH_BITWISE_AND,
dropDown: {
titleName: 'OP',
value: 'BITAND'
}
}, {
translatedName: Blockly.Msg.LANG_MATH_BITWISE_IOR,
dropDown: {
titleName: 'OP',
value: 'BITIOR'
}
}, {
translatedName: Blockly.Msg.LANG_MATH_BITWISE_XOR,
dropDown: {
titleName: 'OP',
value: 'BITXOR'
}
}]
};
Blockly.Blocks.math_bitwise.OPERATORS = function () {
return [[Blockly.Msg.LANG_MATH_BITWISE_AND, 'BITAND'],
[Blockly.Msg.LANG_MATH_BITWISE_IOR, 'BITIOR'],
[Blockly.Msg.LANG_MATH_BITWISE_XOR, 'BITXOR']]
};
Blockly.Blocks.math_bitwise.TOOLTIPS = function () {
return {
BITAND: Blockly.Msg.LANG_MATH_BITWISE_TOOLTIP_AND,
BITIOR: Blockly.Msg.LANG_MATH_BITWISE_TOOLTIP_IOR,
BITXOR: Blockly.Msg.LANG_MATH_BITWISE_TOOLTIP_XOR
}
};
Blockly.Blocks.math_bitwise.HELPURLS = function () {
return {
BITAND: Blockly.Msg.LANG_MATH_BITWISE_HELPURL_AND,
BITIOR: Blockly.Msg.LANG_MATH_BITWISE_HELPURL_IOR,
BITXOR: Blockly.Msg.LANG_MATH_BITWISE_HELPURL_XOR
}
};
Blockly.Blocks['math_random_int'] = {
// Random integer between [X] and [Y].
category: 'Math',
......
......@@ -126,6 +126,38 @@ Blockly.Yail.math_arithmetic.OPERATORS = {
POWER: ['expt', Blockly.Yail.ORDER_NONE]
};
Blockly.Yail['math_bitwise'] = function() {
// Bitwise and, inclusive or, and exclusive or. All can take variable number of arguments.
var mode = this.getFieldValue('OP');
var tuple = Blockly.Yail.math_bitwise.OPERATORS[mode];
var operator = tuple[0];
var order = tuple[1];
var args = "";
var typeString = "";
for(var i=0;i<this.itemCount_;i++) {
args += (Blockly.Yail.valueToCode(this, 'NUM' + i, order) || 0) + Blockly.Yail.YAIL_SPACER;
typeString += "number" + Blockly.Yail.YAIL_SPACER;
}
var code = Blockly.Yail.YAIL_CALL_YAIL_PRIMITIVE + operator
+ Blockly.Yail.YAIL_SPACER;
code = code + Blockly.Yail.YAIL_OPEN_COMBINATION
+ Blockly.Yail.YAIL_LIST_CONSTRUCTOR + Blockly.Yail.YAIL_SPACER
+ args
+ Blockly.Yail.YAIL_CLOSE_COMBINATION;
code = code + Blockly.Yail.YAIL_SPACER + Blockly.Yail.YAIL_QUOTE
+ Blockly.Yail.YAIL_OPEN_COMBINATION + typeString
+ Blockly.Yail.YAIL_CLOSE_COMBINATION + Blockly.Yail.YAIL_SPACER;
code = code + Blockly.Yail.YAIL_DOUBLE_QUOTE + operator
+ Blockly.Yail.YAIL_DOUBLE_QUOTE + Blockly.Yail.YAIL_CLOSE_COMBINATION;
return [ code, Blockly.Yail.ORDER_ATOMIC ];
};
Blockly.Yail.math_bitwise.OPERATORS = {
BITAND: ['bitwise-and', Blockly.Yail.ORDER_NONE],
BITIOR: ['bitwise-ior', Blockly.Yail.ORDER_NONE],
BITXOR: ['bitwise-xor', Blockly.Yail.ORDER_NONE]
};
Blockly.Yail['math_single'] = function() {
// Basic arithmetic operators.
var mode = this.getFieldValue('OP');
......
......@@ -388,18 +388,28 @@ Blockly.Msg.en.switch_language_to_english = {
Blockly.Msg.LANG_MATH_ARITHMETIC_HELPURL_MULTIPLY = '/reference/blocks/math.html#multiply';
Blockly.Msg.LANG_MATH_ARITHMETIC_HELPURL_DIVIDE = '/reference/blocks/math.html#divide';
Blockly.Msg.LANG_MATH_ARITHMETIC_HELPURL_POWER = '/reference/blocks/math.html#exponent';
Blockly.Msg.LANG_MATH_BITWISE_HELPURL_AND = '/reference/blocks/math.html#bitwise_and';
Blockly.Msg.LANG_MATH_BITWISE_HELPURL_IOR = '/reference/blocks/math.html#bitwise_ior';
Blockly.Msg.LANG_MATH_BITWISE_HELPURL_XOR = '/reference/blocks/math.html#bitwise_xor';
Blockly.Msg.LANG_MATH_ARITHMETIC_TOOLTIP_ADD = 'Return the sum of the two numbers.';
Blockly.Msg.LANG_MATH_ARITHMETIC_TOOLTIP_MINUS = 'Return the difference of the two numbers.';
Blockly.Msg.LANG_MATH_ARITHMETIC_TOOLTIP_MULTIPLY = 'Return the product of the two numbers.';
Blockly.Msg.LANG_MATH_ARITHMETIC_TOOLTIP_DIVIDE = 'Return the quotient of the two numbers.';
Blockly.Msg.LANG_MATH_ARITHMETIC_TOOLTIP_POWER = 'Return the first number raised to\n' +
'the power of the second number.';
Blockly.Msg.LANG_MATH_BITWISE_TOOLTIP_AND = 'Return the bitwise AND of the two numbers.';
Blockly.Msg.LANG_MATH_BITWISE_TOOLTIP_IOR = 'Return the bitwise inclusive OR of the two numbers.';
Blockly.Msg.LANG_MATH_BITWISE_TOOLTIP_XOR = 'Return the bitwise exclusive OR of the two numbers.';
Blockly.Msg.LANG_MATH_ARITHMETIC_ADD = '+';
Blockly.Msg.LANG_MATH_ARITHMETIC_MINUS = '-';
Blockly.Msg.LANG_MATH_ARITHMETIC_MULTIPLY = '*';
Blockly.Msg.LANG_MATH_ARITHMETIC_DIVIDE = '/';
Blockly.Msg.LANG_MATH_ARITHMETIC_POWER = '^';
Blockly.Msg.LANG_MATH_BITWISE_AND = 'bitwise and';
Blockly.Msg.LANG_MATH_BITWISE_IOR = 'bitwise or';
Blockly.Msg.LANG_MATH_BITWISE_XOR = 'bitwise xor';
/*Blockly.Msg.LANG_MATH_CHANGE_TITLE_CHANGE = 'change';
Blockly.Msg.LANG_MATH_CHANGE_TITLE_ITEM = 'item';
Blockly.Msg.LANG_MATH_CHANGE_INPUT_BY = 'by';
......
......@@ -1768,7 +1768,10 @@ Blockly.Versioning.AllUpgradeMaps =
21: "noUpgrade",
// AI2: Added Break Block
22: "noUpgrade"
22: "noUpgrade",
// AI2: Added Bitwise Blocks
23: "noUpgrade"
}, // End Language upgraders
......
......@@ -433,8 +433,10 @@ public class YaVersion {
// - WEBVIEWER_COMPONENT_VERSION was incremented to 7
// For YOUNG_ANDROID_VERSION 173:
// - FORM_COMPONENT_VERSION was incremented to 24
// For YOUNG_ANDROID_VERSION 174:
// - BLOCKS_LANGUAGE_VERSION was incremented to 23
public static final int YOUNG_ANDROID_VERSION = 173;
public static final int YOUNG_ANDROID_VERSION = 174;
// ............................... Blocks Language Version Number ...............................
......@@ -496,8 +498,12 @@ public class YaVersion {
// - Spelling of "Obsfucate" was corrected to Obfuscate in Text Block
// For BLOCKS_LANGUAGE_VERSION 21:
// - The is-text block was added.
// For BLOCKS_LANGUAGE_VERSION 22:
// - Break block was added.
// For BLOCKS_LANGUAGE_VERSION 23:
// - Bitwise and, ior, and xor blocks were added.
public static final int BLOCKS_LANGUAGE_VERSION = 22;
public static final int BLOCKS_LANGUAGE_VERSION = 23;
// ................................. Component Version Numbers ..................................
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment