Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: The Stack
- // ID: stack
- // Description: Push, pull or peek values on any number of stacks!
- // By: SforzandoCF <https://scratch.mit.edu/users/CzechFlagTDOC/>
- // License: MPL-2.0 AND BSD-3-Clause
- // Version: 1.0.0
- let stacks = {};
- const definitions = [
- {
- opcode: "push",
- blockType: BlockType.COMMAND,
- text: "push [VALUE] to [STACK]",
- arguments: {
- VALUE: {
- type: ArgumentType.STRING,
- defaultValue: ""
- },
- STACK: {
- type: ArgumentType.STRING,
- defaultValue: "my stack"
- }
- },
- def: function ({ VALUE, STACK }) {
- if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
- stacks[STACK].push(VALUE);
- }
- },
- {
- opcode: "popBlock",
- blockType: BlockType.COMMAND,
- text: "pop value from [STACK]",
- arguments: {
- STACK: {
- type: ArgumentType.STRING,
- defaultValue: "my stack"
- }
- },
- def: function ({ STACK }) {
- if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
- stacks[STACK].pop();
- }
- },
- {
- opcode: "popValue",
- blockType: BlockType.REPORTER,
- text: "pop value from [STACK]",
- arguments: {
- STACK: {
- type: ArgumentType.STRING,
- defaultValue: "my stack"
- }
- },
- def: function ({ STACK }) {
- if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
- return stacks[STACK].pop() ?? "";
- }
- },
- {
- opcode: "peekValue",
- blockType: BlockType.REPORTER,
- text: "peek top value from [STACK]",
- arguments: {
- STACK: {
- type: ArgumentType.STRING,
- defaultValue: "my stack"
- }
- },
- def: function ({ STACK }) {
- if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
- return stacks[STACK].peek() ?? "";
- }
- },
- {
- opcode: "sizeOf",
- blockType: BlockType.REPORTER,
- text: "size of [STACK]",
- arguments: {
- STACK: {
- type: ArgumentType.STRING,
- defaultValue: "my stack"
- }
- },
- def: function ({ STACK }) {
- if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
- return stacks[STACK].length;
- }
- },
- {
- opcode: "isEmpty",
- blockType: BlockType.BOOLEAN,
- text: "is [STACK] empty?",
- arguments: {
- STACK: {
- type: ArgumentType.STRING,
- defaultValue: "my stack"
- }
- },
- def: function ({ STACK }) {
- if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
- return stacks[STACK].length == 0;
- }
- }
- ];
- Scratch.extensions.register({
- getInfo: function () {
- return {
- id: "stack",
- name: "Stack",
- color1: "#D65CB1",
- color2: "#D147A8",
- color3: "#B82E8E",
- blocks: definitions,
- };
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment