2ds
A Forth-like stack, but the stack is a plane, and every push and pop says which way it goes.
Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 5 known programs · Test method
Run it
The idea
The name stands for 2-Dimensional Stack, and the words are after Forth. The stack is a grid of byte cells with one head, the two-dimensional version of a stack pointer. A push writes where the head is and moves it one cell along x or along y. A pop moves it back one cell along x or y and reads what is there. So a value pushed along y has to be popped along y, or the head lands on another cell.
Read the story
All the work happens in two registers, A and B. Values move between the plane and the registers, and every number wraps within 0 to 255.
The machine
| Word | Does |
|---|---|
72x 72y | Push a number along x or y |
'Hx 'Hy | Push a character |
ax ay bx by | Push A or B |
xa ya xb yb | Pop into A or B |
x. y. | Pop and print as a character |
.x .y | Read a character and push it |
+ - * / | A plus, minus, times or divided by B, into A |
== != > < >= <= | 1 into A if A compares so with B, else 0 |
! && || | Not, and, or, which count only values above 1 as true |
swap | Swap A and B |
if ... fi | Skip to fi if A is 0 |
while ... elihw | Loop while A is not 0 |
Words are separated by white space and may be in any case. # starts a comment.
Example
A Hello World pushes the letters backwards along x and pops all 14.
A Hello World pushes the letters backwards along x and pops all 14.
Two directions. 48x writes cell 0,0 and moves the head to 1,0. 58y writes cell 1,0 and moves it to 1,1. Popping along y comes back to 1,0 and the 58 of the colon, then along x to 0,0 and the 48 of the digit.
The truth machine puts the character 0 in B and the input in A. On 0, != makes A 0 and while skips the loop, so it prints B. On 1 it prints 1 forever.
A 99 bottles of beer program prints every number with two digits, from 99 down to 01. It takes 30,441 steps. It checks for the end by comparing the count with B, and B still holds the 48 it used to print a digit. So after 48 bottles it also prints No bottles of beer on the wall.
Patterns it teaches
- Stack machines: one stack, read and written along two axes.
Further
See also
Also in Functional and concatenative
Write your own in Run Convert a program from 2ds
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: 2ds.
Command line
shib run 2ds -e ''\''0x xb .x xa != while '\''1x x. elihw bx x.' --input '0'shib manual 2dsSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/2ds.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org