Test it in the online interpreter!
Trilangle is a 2-D language inspired by Hexagony. It has its own instruction set, and two major differences when it comes to code/memory layout:
- The bounding box of the code is a triangle rather than a hexagon
- Memory is stored in a stack* rather than a grid.
The memory structure isn't a pure stack, as it's possible to look (but not write) arbitrarily far down the stack. This feature makes it Turing-complete.
When unwrapped into the triangular grid, this code is:
v j .
. ! " /
@ . ) e .
, > - . / .
. _ . . ' . .
The IP starts at the north corner, moving southwest.
For lack of a fancy tool, have a diagram I made in paint:
The IP initially follows the red path, hitting the following instructions:
'0: Push the number
0 to the stack
v: Change the direction of control flow
!: Print the number on top of the stack in decimal
): Increment the top of the stack
': Part of a "push" instruction
After hitting the partial "push" instruction, the IP walks off the edge of the board and continues one diagonal to its left -- on the green path.
0: The rest of the push instruction; pushes another
j: The indexing operator. The stack now contains two copies of the same number (1 more than the value that was printed last).
"e: Push the value of the character 'e' (101 in decimal)
/: Changes the direction of control flow
.: No-op again
-: Subtract the two values on top of the stack. If the last value printed was n, the stack now contains [n+1, n-100].
>: Branch. If the value on top of the stack is positive or zero, it takes the yellow path; if the value is negative, it takes the blue path.
On the yellow path, the next instruction is
@, which terminates the program. Continuing on the blue path, the instructions are:
_: Changes the direction of control flow
,: Pop from the stack
[Run off the edge and continue on the magenta path]
A few more NOPs
/: Changes the direction of control flow again. It runs off the edge and continues at
v, where it merges with the red path.
Is this optimal?
I'm not sure. Given the number of NOPs in the code I'd be unsurprised if this can be reduced, but I don't think I can make the entire triangle smaller (reducing its side length to 6) without substantially restructuring it.