esolang

Staq

Arithmetic that never pops, loops that can cross each other, and any character can be redefined.

Madk, 2011 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: 1 1 2 3 5 8 13 21

The idea

Madk made Staq in 2011. It looks like a plain stack language at first, but three things are odd. First, arithmetic keeps its inputs: + pushes the sum of the top two values and leaves both of them there. Programs are full of x and X to throw away what they no longer need.

Read the story

Second, the two loop kinds pair up separately. [ matches the next free ], ( matches the next free ), and the two pairs can overlap, as in [ ( ] ). Third, {u code} makes every later u run that code. You can even redefine built-in commands, and _u still reaches the original.

That last feature makes the Turing-completeness proof easy. Six definitions turn > < + - , . into Staq code, using the two stacks as the left and right halves of a brainfuck tape. After that, a brainfuck program runs as it is.

The machine

Two double-ended stacks of integers, one active.

CharDoes
& x XPush 0; drop the top; drop the second
i d q , . < ~Change the top: +1, −1, square, shift left, shift right, rotate left, not
@ $ sSwap; top to bottom; bottom to top
+ - * / % > =Push top op second as a new value, keeping both
[ … ]Skip if the top is 0; jump back while it is above 0
( … )Skip if the top is above 0; jump back while it is 0
? !Run the next command only if the top is above 0, or is 0
: ; "…"Print the top as a number or a character; print text
' ` | Read a number or a character
n \| tPush the size; swap the stacks; copy the top to the other stack
{c …} _cRedefine c; run the built-in c

Example

This is the Fibonacci program from the specification with a smaller count.

This is the Fibonacci program from the specification with a smaller count. {\&iiiqi;x} makes \ print a newline (0, +3, squared, +1 is 10). Two 1s go on the stack and the first is printed. &iiiqdd pushes 7, the loop counter. Each round counts down, tucks the counter under the pair, prints, adds, and rotates the stack back into shape with $Xss.

Patterns it teaches

Try this

  1. Print the numbers 10 down to 1.
  2. Make D a command that duplicates the top, and use it.
  3. Hard: read a number and print its square without leaving anything else on the stack.

Further

Stack-based

Write your own in Run Convert a program from Staq

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run staq -e '"Hello, world!"'
shib manual staq

Source

Module
langs/stack/ports.js
Interpreter
langs/stack/staq.js

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

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