DQ

From Esolang
Jump to navigation Jump to search

DQ is a esolang about duplicate and discard

esolang overview

alphabet

DQ uses a minimal stack-based approach. The only symbols are:

symbols:
symbol description
0 make the stack empty
1 pushes a 1 onto the stack
D duplicates the top element on the stack
Q discards the top element on the stack

execution model

  • Start with an empty stack.
  • Read the DQ program character by character in random order.
  • After processing the entire program, the final stack contents represent the output.

examples

11DQ

execution:

  • 1: Push a 1 onto the stack (stack: 1).
  • 1: Push another 1 onto the stack (stack: 1, 1).
  • D: Duplicate the top 1 (stack: 1, 1, 1, 1).
  • Q: Discard the top element (stack: 1, 1, 1).

Final output: 1, 1, 1 (if it goes in right direction).

Key Features:

  • Duplication: The D instruction effectively doubles the size of the stack (up to the current stack pointer) at a specific point.
  • Discarding: The Q instruction removes the top element, allowing for selective removal and manipulation of stack contents.
  • All operations happen in a random order

Interpreter

Written in JavaScript.

function dq(program)
{
    const stack = [];
    const randomisedProgram = program.split("").sort(() => Math.random() - 0.5);
    for(const character of randomisedProgram)
    {
        switch(character)
        {
            case "0":
                stack.length = 0;
                break;
            case "1":
                stack.push(1);
                break;
            case "D":
                if(stack.length > 0)
                    stack.push(stack[stack.length - 1]);
                break;
            case "Q":
                stack.pop();
                break;
        }
    }
    console.log(...stack);
}

See also