SforzandoCF

random thing

Jan 28th, 2026
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Name: The Stack
  2. // ID: stack
  3. // Description: Push, pull or peek values on any number of stacks!
  4. // By: SforzandoCF <https://scratch.mit.edu/users/CzechFlagTDOC/>
  5. // License: MPL-2.0 AND BSD-3-Clause
  6. // Version: 1.0.0
  7. let stacks = {};
  8.  
  9. const definitions = [
  10.     {
  11.         opcode: "push",
  12.         blockType: BlockType.COMMAND,
  13.         text: "push [VALUE] to [STACK]",
  14.         arguments: {
  15.             VALUE: {
  16.                 type: ArgumentType.STRING,
  17.                 defaultValue: ""
  18.             },
  19.             STACK: {
  20.                 type: ArgumentType.STRING,
  21.                 defaultValue: "my stack"
  22.             }
  23.         },
  24.         def: function ({ VALUE, STACK }) {
  25.             if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
  26.             stacks[STACK].push(VALUE);
  27.         }
  28.     },
  29.         {
  30.             opcode: "popBlock",
  31.             blockType: BlockType.COMMAND,
  32.             text: "pop value from [STACK]",
  33.             arguments: {
  34.                 STACK: {
  35.                     type: ArgumentType.STRING,
  36.                     defaultValue: "my stack"
  37.                 }
  38.             },
  39.             def: function ({ STACK }) {
  40.                 if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
  41.                 stacks[STACK].pop();
  42.             }
  43.         },
  44.         {
  45.             opcode: "popValue",
  46.             blockType: BlockType.REPORTER,
  47.             text: "pop value from [STACK]",
  48.             arguments: {
  49.                 STACK: {
  50.                     type: ArgumentType.STRING,
  51.                     defaultValue: "my stack"
  52.                 }
  53.             },
  54.             def: function ({ STACK }) {
  55.                 if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
  56.                 return stacks[STACK].pop() ?? "";
  57.             }
  58.         },
  59.         {
  60.             opcode: "peekValue",
  61.             blockType: BlockType.REPORTER,
  62.             text: "peek top value from [STACK]",
  63.             arguments: {
  64.                 STACK: {
  65.                     type: ArgumentType.STRING,
  66.                     defaultValue: "my stack"
  67.                 }
  68.             },
  69.             def: function ({ STACK }) {
  70.                 if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
  71.                 return stacks[STACK].peek() ?? "";
  72.             }
  73.         },
  74.     {
  75.         opcode: "sizeOf",
  76.         blockType: BlockType.REPORTER,
  77.         text: "size of [STACK]",
  78.         arguments: {
  79.             STACK: {
  80.                 type: ArgumentType.STRING,
  81.                 defaultValue: "my stack"
  82.             }
  83.         },
  84.         def: function ({ STACK }) {
  85.             if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
  86.             return stacks[STACK].length;
  87.         }
  88.     },
  89.     {
  90.         opcode: "isEmpty",
  91.         blockType: BlockType.BOOLEAN,
  92.         text: "is [STACK] empty?",
  93.         arguments: {
  94.             STACK: {
  95.                 type: ArgumentType.STRING,
  96.                 defaultValue: "my stack"
  97.             }
  98.         },
  99.         def: function ({ STACK }) {
  100.             if (!Object.hasOwn(stacks, STACK)) stacks[STACK] = [];
  101.             return stacks[STACK].length == 0;
  102.         }
  103.     }
  104. ];
  105.  
  106. Scratch.extensions.register({
  107.     getInfo: function () {
  108.         return {
  109.             id: "stack",
  110.             name: "Stack",
  111.             color1: "#D65CB1",
  112.             color2: "#D147A8",
  113.             color3: "#B82E8E",
  114.             blocks: definitions,
  115.         };
  116.     }
  117. });
Advertisement
Add Comment
Please, Sign In to add comment