Minkolang

Current version: 0.8.

Introduction
The Minkowskian (2+1)D semi-golfing language!

This language was inspired by Befunge and ><>, both of which are stack-based 2D programming languages. It is a (2+1)D Minkowskian language, meaning that it has two dimensions of space and one dimension of time. When the program counter is not moving through space, it is moving through time (the layers of the program). The program space is toroidal in all directions, meaning that if the program counter moves outside of the program's boundary in spacetime, then it will relocate to the opposite side.

Minkolang is stack-based like many esolangs, which has an infinite well of s, though it only has one stack, like Befunge and unlike ><>. It also has an array where data can be stored, or data can be stored by altering the program's code. Yes, Minkolang too is capable of self-modification.

How To
Let's say that  is stored in   (and that it and   are in the same folder). Then you would run this with an input of  like this:

python minkolang_0.7.py collatz.mkl 13

Movement
  Lets the program counter move through time (fall to the next layer).  Net; stops the program counter from moving through time (it's a no-op).  Changes the direction of the program counter.  Mirrors. They act like you would expect.  Trampolines. jump one character ( jumps if the top of stack is non-zero);   pop   from top of stack and jump   characters (  is conditional, like  ).  Boost; enables the program counter to cross any number of spaces.  enables the boost permanently until it encounters another  . </li>  Wormholes; they allow you to jump to any point in the code. pops  off the stack and jumps to   whereas   pops   off the stack and jumps to  .</li> </ul>

Control structures
  Terminates program.</li>  Straight, T branches.  reverses the branch endpoints (swaps where truthy and falsy go).</li></ul> </li>  While loop; takes all of parent's stack.  pops   and puts the top   elements of the parent stack into the loop's stack.</li></ul> </li>  For loop. Pops  and repeats body   times.  Pops   and puts the top   elements of parent stack into the loop's stack.</li></ul> </li>  Recursion.  Starts a new recursion.</li></ul> </li> </ul>

Literals
 <li> Pushes the corresponding digit onto the stack.</li> <li> String literal. Minkolang is smart enough to reverse this before pushing on the stack.</li> <li> Number literal. Does the work of multiplying by 10 and adding the next digit.</li> </ul>

Math and comparisons
<ul> <li> Add, subtract, multiply, integer division , exponentiation , modulus , negation. is popped first, then  so doing   (for example) intuitively gives , as expected. <ul><li> changes these into sum, reverse subtract, product, float division , reverse exponentiation , reverse modulus , and absolute value.</li></ul> </li> <li> Equality, greater-than (pops , , then pushes  , so   will push 1, as expected), and not. <ul><li> changes these into not-equal, , and boolean (like Python's  ).</li></ul> </li> </ul>

Input and output
<ul> <li> Input/output character.</li> <li> Input/output number. ( dumps non-digits from the input until an integer is found. This is how this Befunge interpreter works.)</li> </ul>

Stack manipulation
<ul> <li> Duplicates top element of stack. pops  and duplicates the top of stack   times.</li> <li> Stack index/insert. pops  and gets the stack's  th element and puts it on top of stack. pops,  and inserts   at the  -th position (zero-indexed).</li> <li> Gets loop's counter (0-based) and pushes it onto the stack.</li> <li> Pushes the stack's length onto the stack.</li> <li> Reverses stack.</li> <li> Rotates stack. Pops  and rotates clockwise   times (may be negative). If the stack is, then   results in  .</li> <li> Sorts the stack. <ul><li> pops   and sorts the top   elements of the stack.</li></ul> </li> <li> Removes duplicates. So  will give you. <ul><li> pops   and removes duplicates from the top   elements of the stack.</li></ul> </li> <li> Dump. pops the top of stack and throws it away. pops  and throws away the top   elements of the stack. <ul><li> dumps the whole stack.</li></ul> </li> </ul>

Memory and reflection (self-modification)
<ul> <li> Puts to code. pops  and replaces   with. pops  and replaces   with  .</li> <li> Gets from code. pops  and puts   on top of stack. pops ` and puts   on top of stack.</li> <li> Array get/put. pops  and puts   on top of stack. pops  and writes   to  .</li> </ul>

Special
<ul> <li> Toggles the functionality of many functions. This "toggle flag" only remains active for one step. <ul><li> (that is,  ) turns on the toggle flag permanently until another   is encountered.</li></ul> </li> <li> Separates layers of a program. See the layered Hello world! example.</li> <li> Debug (print) the current stack  and the code and loops (` `).</li> </ul>

To implement
(None yet.)

Unassigned
<ul> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> <li> </li> </ul>

Loops
All loops have their own stack, which may have any number of elements from the parent stack (either the main program or a parent loop). Now, very interestingly, the loop's closing brace can be anywhere. When a closing brace is reached, the program counter is relocated to the opening brace and the direction is reset to what it was when the loop was entered.

<ul> <li> For loop : Pops the top of the stack  and repeats the loop's contents   times. Takes all of the parent stack by default, but pre-pending with  will further pop   and take   elements of the parent stack.</li> <li> While loop (take it all) : The parent's whole stack is used to populate this loop's stack, unless   is used right beforehand, in which case, pops the top of the stack  and takes the top   elements of the parent's stack to populate this loop's stack. Loops until stack is empty or top of stack is 0.</li> </ul>

Branches
Another interesting thing about Minkolang is its branches. There are two kinds of branches: straight branches and T branches. Both kinds pop the top of the stack off and check to see if it's zero.

<ul> <li> Straight branch : a non-zero input will continue on in the same direction whereas a zero input will reverse direction.</li> </ul>

v      0         1 &gt; 0b1 b  1b0 &lt;  b       1         0 ^

<ul> <li> T branch : a non-zero input will bend to the right (clockwise) whereas a zero input will bend to the left (counter-clockwise).</li> </ul>

0  v   1 &gt; B 1B0  B &lt;  0B1 1      0     ^

Recursion
As far as I know, this is absolutely the only 2D language to implement recursion directly. In Minkolang,  are the control characters. On the first use of  (or if   was used right before), a recursive function is initialized. This means popping  off the top of the stack, which sets the number of arguments to take. The top  elements of the parent stack are popped off and used to populate the recursion's stack. This applies to child instances too, which are started when the program counter encounters another. Returning is done by using, which simply plops the instance's stack on top of the parent (often, another recursion instance) stack.

To put it another way, there are three steps to doing recursion in Minkolang:

<ol> <li>Initialize. For example, if the program counter encounters  and the stack is , then the recursion's stack will be  , and any further children will also take the top three elements of their parent's stack.</li> <li>Recurse. This is done by using  again. The program counter jumps to the very first  that started the whole recurrence.</li> <li>Return. This is done by using. The program counter jumps to the last  that was countered and moves from there.</li> </ol>

This is certainly a bit complicated, but try working through the Fibonacci example below. Hopefully that'll help.

Hello world!
"Hello world!"(O).

is pushed onto the stack in reverse order, then  repeatedly prints the top of stack until it's empty.

Collatz sequence (one line)
ndN(d2%,7@)Nd+1*3b2:dNd1=?).

takes the input and duplicates it so it can be output.

starts a while loop with  as the stack. It is duplicated and modded by 2. This is inverted and then seven characters are skipped with, landing it squarely on the. In effect, the program counter moves right if the top of stack is odd, left if it's even.

multiplies the top of stack by three, then adds one, and outputs it (duplicated beforehand). The  closes the loop.

divides the top of stack by two and outputs it. Then it is checked to see if it's equal to. If so, then the conditional trampoline  jumps the program counter over the closing   and onto the , where it terminates. Otherwise, it just goes back to the start of the loop.

Collatz sequence (three lines)
ndN(d2%,B ?=1dNd:2&lt;.) )Nd+1*3&lt;

This one works very similarly to the one above. The biggest difference is that the T-branch  is used, which directs the program counter either up or down. The toroidal nature of the program space means that the third line is executed when the top of stack is odd.

Fibonacci sequence (recursion)
This (horribly inefficient) way of calculating the Fibonacci sequence uses its recursive definition: F(n) = F(n-1) + F(n-2) where F(0) = 0 and F(1) = 1.

n1{d1`,9&amp;d1-{r2-{+}N.

takes an integer in input, then the recursive function is initialized with. The  here specifies that the function takes one argument.

` looks to see if the top of stack is &lt;= 1. If it is, then it jumps to the closing, which effectively "returns" from the function. This is the base case - where the input is 0 or 1, so F(0 or 1) is the same.

If the top of stack is not 0 or 1, then the trampoline is not taken. Hence,  is executed, which duplicates the top of stack, subtracts 1, and runs the function on it. This is the F(n-1) part.

Once that returns,  is executed. This reverses the stack so input is now in front. Then two is subtracted and the function is run again with it as input. This is the F(n-2) part.

Once that returns, then finally, the two values are added together and returned with. This is the F(n-1) + F(n-2) part.

At the ultimate conclusion (i.e., when F(n) has been calculated),  outputs F(n) as an integer and exits.

Layered "Hello world!"
!v"Hello world!"!

$$$

$$$

V&lt;       .)O(

This demonstrates how the layers of a program can be separated. The interpreter automatically fills in lines and spaces as needed to make a complete rectangular prism for the code space. The execution is as follows: the first  jumps over the , "Hello world!" is pushed onto the stack, then another  skips the first one, so the program counter is redirected downward. It lands on a space, so it moves through time (falls) until it hits the. The  boosts it across the space to the output loop   (normally written as  ).