Ndim
Befunge in as many dimensions as you ask for, with every command placed by its coordinate.
Schlafhase · Turing complete: unknown · ●●● hard · esolangs.org
Tested with 7 known programs · Test method
Run it
The idea
Schlafhase made Ndim after Befunge. The first line says how many dimensions the program has, and every other line places one command at a coordinate, like print <2, 4>;. The order of the lines does not matter; a program sorted alphabetically still runs.
Read the story
A pointer starts at the origin and moves along the first axis, cell by cell, through mostly empty space. It runs each command it meets. The stack holds only two values, a and b, so every number is built a step at a time.
The machine
| Command | Does |
|---|---|
k -k | Point along axis k, forward or back |
? jump | Point a random way; skip the next cell |
#d | Push a digit |
pop swap duplicate | Drop a; swap; copy a into b |
+ - * / ^ | Pop a and b, push a+b, b-a, a*b, b/a or b to the power a |
< > & \| ! | Push 1 when the test holds; ! pushes 0 when it does not |
if v | Pop a value and turn right if it equals v, else left |
assign assignHere | Store a popped value to the right, or here |
toggleEat | Switch eat mode, in which every cell the pointer meets is deleted |
input print printChar end | Read a number, print, stop |
A push moves a into b, and a pop moves b back into a and leaves 0 behind. A stored value is pushed when the pointer meets it. Right and left lie along axis |direction| mod n, counted from 0, as the formula has it, so moving along the first axis of two, right is the second axis.
Example
Three cells in a row multiply 6 by 7 on the two-value stack.
Three cells in a row multiply 6 by 7 on the two-value stack.
The two-way program. After a 0 it turns right, up the second axis, reads a number and doubles it. After anything else it turns left and triples it.
The Hello, World! runs along one row building each character, stores it one cell above with assign, then turns around and walks the row above, printing as it goes. Its factorial uses three dimensions and loops by storing the counter and the product where its own path crosses them. Both run, and 5 gives 120.
Patterns it teaches
- Two-dimensional control flow: in any number of dimensions, with the program as a list of coordinates.
Further
See also
Multi-dimensional languages
Stack-based
Write your own in Run Convert a program from Ndim
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: ndim.
Command line
shib run ndim -e '2dim; //except this line because it has to be at the top
#2 <2, 2>;
#3 <2, -2>;
* <2, 3>;
* <2, -3>;
end <2, -5>;
end <2, 5>;
if 0 <2, 0>;
input <0, 0>;
input <2, -1>;
input <2, 1>;
print <2, -4>;
print <2, 4>;' --input '0
21'shib manual ndimSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/ndim.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org