diff --git a/extensions/Cheddarphanie/logic.js b/extensions/Cheddarphanie/logic.js new file mode 100644 index 0000000000..0501701364 --- /dev/null +++ b/extensions/Cheddarphanie/logic.js @@ -0,0 +1,141 @@ +// Name: Logic +// ID: lemonLogic +// Description: A pack of handy logic operators. +// By: Cheddarphanie +// License: Apache-2.0 + +(function (Scratch) { + "use strict"; + + if (!Scratch.extensions.unsandboxed) { + throw new Error("Logic must run unsandboxed."); + } + + const Cast = Scratch.Cast; + + class LogicExt { + getInfo() { + return { + id: "lemonLogic", + name: Scratch.translate("Logic"), + color1: "#59C059", + color2: "#50ad50", + color3: "#479a47", + blocks: [ + /* eslint-disable extension/should-translate */ + { + opcode: "Logic_NAND", + blockType: Scratch.BlockType.BOOLEAN, + text: "[A] NAND [B]", + arguments: { + A: { + type: Scratch.ArgumentType.BOOLEAN, + }, + B: { + type: Scratch.ArgumentType.BOOLEAN, + }, + }, + extensions: ["colours_operators"], + }, + { + opcode: "Logic_NOR", + blockType: Scratch.BlockType.BOOLEAN, + text: "[A] NOR [B]", + arguments: { + A: { + type: Scratch.ArgumentType.BOOLEAN, + }, + B: { + type: Scratch.ArgumentType.BOOLEAN, + }, + }, + extensions: ["colours_operators"], + }, + { + opcode: "Logic_XOR", + blockType: Scratch.BlockType.BOOLEAN, + text: "[A] XOR [B]", + arguments: { + A: { + type: Scratch.ArgumentType.BOOLEAN, + }, + B: { + type: Scratch.ArgumentType.BOOLEAN, + }, + }, + extensions: ["colours_operators"], + }, + { + opcode: "Logic_XNOR", + blockType: Scratch.BlockType.BOOLEAN, + text: "[A] XNOR [B]", + arguments: { + A: { + type: Scratch.ArgumentType.BOOLEAN, + }, + B: { + type: Scratch.ArgumentType.BOOLEAN, + }, + }, + extensions: ["colours_operators"], + }, + { + opcode: "Logic_IMPLY", + blockType: Scratch.BlockType.BOOLEAN, + text: "[A] IMPLY [B]", + arguments: { + A: { + type: Scratch.ArgumentType.BOOLEAN, + }, + B: { + type: Scratch.ArgumentType.BOOLEAN, + }, + }, + extensions: ["colours_operators"], + }, + { + opcode: "Logic_NIMPLY", + blockType: Scratch.BlockType.BOOLEAN, + text: "[A] NIMPLY [B]", + arguments: { + A: { + type: Scratch.ArgumentType.BOOLEAN, + }, + B: { + type: Scratch.ArgumentType.BOOLEAN, + }, + }, + extensions: ["colours_operators"], + }, + ], + }; + } + + Logic_NAND({ A, B }) { + [A, B] = [A, B].map((arg) => Cast.toBoolean(arg)); + return !(A && B); + } + Logic_NOR({ A, B }) { + [A, B] = [A, B].map((arg) => Cast.toBoolean(arg)); + return !(A || B); + } + Logic_XOR({ A, B }) { + [A, B] = [A, B].map((arg) => Cast.toBoolean(arg)); + return A !== B; + } + Logic_XNOR({ A, B }) { + [A, B] = [A, B].map((arg) => Cast.toBoolean(arg)); + return A === B; + } + Logic_IMPLY({ A, B }) { + [A, B] = [A, B].map((arg) => Cast.toBoolean(arg)); + return !A || B; + } + Logic_NIMPLY({ A, B }) { + [A, B] = [A, B].map((arg) => Cast.toBoolean(arg)); + return A && !B; + } + } + + Scratch.extensions.register(new LogicExt()); +})(Scratch); diff --git a/extensions/extensions.json b/extensions/extensions.json index 9027c00ac9..f33493dc42 100644 --- a/extensions/extensions.json +++ b/extensions/extensions.json @@ -13,6 +13,7 @@ "local-storage", "true-fantom/base", "bitwise", + "Cheddarphanie/logic", "Skyhigh173/bigint", "utilities", "sound",