esolang

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

Expected: 42

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

CommandDoes
k -kPoint along axis k, forward or back
? jumpPoint a random way; skip the next cell
#dPush a digit
pop swap duplicateDrop 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 vPop a value and turn right if it equals v, else left
assign assignHereStore a popped value to the right, or here
toggleEatSwitch eat mode, in which every cell the pointer meets is deleted
input print printChar endRead 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.

Expected: 15

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

Further

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 ndim

Source

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