esolang

DStack

Two stacks and a register, programmed in the six letters of its own name, read two at a time with every pair overlapping the next.

-Dark-Phantom-, 2015 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 5 known programs · Hello World on Try It Online: identical · Test method

Run it

Expected: Hello, World!

The idea

-Dark-Phantom- made DStack in November 2015. The only letters allowed are d s t a c k, in either case, plus the digits. The cursor reads the pair of characters under it, runs it, and then moves on by one character, not two. So 0kckt is the four instructions 0k, kc, ck and kt, and every character usually belongs to two of them.

Read the story

Writing DStack means choosing letters so that the pairs in between are harmless. A digit followed by a letter does nothing, and a capital second letter swaps the roles of the two stacks, which gives some room to manoeuvre.

Text lives outside the code, in numbered string literals between @ lines. Printing commands take the number of the string from the register.

The machine

Two stacks, each starting with a 0, and a register. Most pairs compute from the tops of the stacks into the register.

PairDoes
x0Register = register × 10 + digit
ds dS dt da dc dkAdd, multiply, subtract, power, divide, remainder
st sc sk saEqual, greater, at least, between
td ts tS taNot, or, and, xor
tc tk tK sdPeek, min, max, zero
dd ss tt cc kkPush the register
cd cs caMove a top to the other stack, pop, swap the tops
ck cKPrint the register as a character, as a number
kc kCRead a character, a number
ks ktPush the cursor's next position; jump there if the register is not 0
ka kARestart, or halt, if the register is not 0
ad as at ac akPrint string register, with # and $ filled in, or push it

The program ends when the cursor reaches its last character.

Example

The truth machine reads as 04, 4K, KK, KC, CK, KT.
Expected: 0
Expected: 120
Expected: 6 3 10 5 16 8 4 2 1

The truth machine reads as 04, 4K, KK, KC, CK, KT. 04 puts 4 in the register and KK pushes it on the second stack. KC reads a number, CK prints it, and KT jumps back to position 4, the CK, as long as the number is not 0. The other two are the factorial and Collatz sequence.

Patterns it teaches

  • Stack machines: two stacks, with a register between them.
  • Encoding tricks: overlapping pairs make every letter do double duty.
  • Quine: the specification has one, which prints its code from a string literal holding the same code.
  • Turing completeness: every brainfuck command has a DStack translation.

Further

Stack-based

Write your own in Run Convert a program from DStack

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: dstack.

Command line

shib run dstack -e '0kckt' --input 'ab'
shib manual dstack

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/dstack.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org