Cardinal
Cardinal is an esoteric programming language by User:Madk, named for the directions that the instruction pointers can travel. Execution takes place on a 2D grid. For anything significantly complex, many instruction pointers must be operating and communicating simultaneously; each pointer has only 2 bytes of memory.
Commands
4 pointers go out in all cardinal directions from all instances of a % at program start. When a pointer encounters a # the pointer is copied into every direction except the one it came from ^ v < and > change the direction of a pointer When a pointer encounters an x the pointer is removed. If it encounters a @, the entire program is ended. If no pointers exist, the program end automatically. ? and ! remove a pointer if it's =0 and !=0 respectively. / and \ are buffers that reflect a pointer's direction from the diagonal surface. Every time a buffer is hit by a pointer, it flips to the opposite state. Every pointer has 2 values to operate upon. ~ switches the active one with the inactive. + and - increment and decrement the active value of the pointer t d * and ' multiply, divide, add, and subtract (respectively) the active value by the inactive one. The result is placed in the active value. M performs (active value) modulo (inactive value) and puts the result in the place of the active value. O rotates the direction of incoming pointers clockwise. o rotates anticlockwise. : reads a value from console input and places it as the pointer's active value. . outputs the pointer's active value to the console as a value, ; starts a new line, and , outputs the pointer's value as an ASCII character. & | and X perform logical AND, OR, and XOR on the pointer's active and inactive values and places the result in the active value. = copies the pointer's active value into its inactive value. nu() drops the pointer's active value into the direction the curve points and picks it up from the character on the opposite side. ` toggles whether the pointer gets removed on doing one of these commands. By default they do not. A V { } remove pointers coming from the north, south, left, right, respectively. 0 sets the pointer's active value to zero " toggles console output mode. A pointer with console output mode on doesn't follow any instructions (except the next ") and instead outputs them to the console. N reflects pointers travelling left/right but does not disrupt pointers going up/down. Z reflects up/down and allows left/right. I reflects everything. U D L R sets the pointer in the corresponding up, down, left or right direction if the active value of the pointer is >0 8 traps the pointer for three extra steps. $ sets a pointer's X,Y position to (active value),(inactive) and it maintains its current travel direction. J,j jumps ahead and skips the next instruction if the active value is >0 or =0
Examples
Hello, world!
%"Hello, world!";x
Cat
%:.x
Fibonacci sequence
x x x%++++++=*>- # !@ x ^ < x%.;+.; >~*v x ^;.<
Mid-execution visualization:
99 bottles of beer
%++++++v xv=++++<v < >t- >." bottles of beer on the wall, "v v"eht no re"<v"ekaT";".reeb fo selttob ". < >" wall, 1"v">" one down, pass it around" v v" elttob "<ev"no reeb fo selttob ".-" ." < >"of beer."v">" the wall."--;v v"ti ekaT";<^"b fo elttob 1"!#?++ ^ >" down, pass it around. No more bottles o"v v"fo selttob erom oN";".llaw eht no reeb f"< >" beer on the wall, no more bottles of be"v v".erom emos yub dna erots eht ot oG";".re"< >" 99 bottles of beer on the wall."x
Mid-execution visualization:
Collatz sequence
%"What number would you like to determine a Collatz sequence of?";v x v" >" < >:.;>~++~ #M#?0+v >!0v+ v #x x#ux ++ . + ; ^ ~0~d u ~ < - ~ ! 0 >ux @ ~ ^ +t u ~ <
Mid-execution visualization:
Multiplication
%>"Multiply X by Y."v ;v.t:"> Y"~:"> X"<;< >"Again? [0/1] ":;v ^ #!@
External resources
A Python interpreter can be found here. You can Try It Online here.
Download original Cardinal interpreter Wayback download mirror for original interpreter (from the Wayback Machine; retrieved on 3 April 2013)
The original interpreter contains a handful of wrongly implemented instructions, and some instructions are missing.
A corrected and recompiled version that works according to the language specification is available at User:Albedo’s Cardinal repository on GitHub