BrainCube

From Esolang
Jump to: navigation, search

BrainCube is an esoteric programming language inspired by brainfuck, that uses a 3D memory cube instead of brainfuck's memory tape. In addition to the memory cube, BrainCube also adds user-declared pointers, and control flow structures.

Syntax

Pointers

To declare a new pointer, write the pointer's name, surrounded by white space. Example: ~my_ptr~

To execute commands using a pointer, write the pointer's name followed by a sequence of pointer commands in parentheses. Example: my_ptr(+>)

Pointer commands

Command Description
> Move pointer to the right
< Move pointer to the left
^ Move pointer up
v Move pointer down
X Move pointer forward (into the cube)
O Move pointer backward (out of the cube)
+ Increment the target cell
- Decrement the target cell
. Write the value of the target cell to the console as an ASCII character
' Write the value of the target cell to the console as a binary number, followed by a newline
: Write the value of the target cell to the console as a base10 number, followed by a newline
, Set the value of the target cell to the ASCII value of a character from the console
" Set the value of the target cell to the value of a binary number from the console (User types binary number, then hits enter)
; Set the value of the target cell to the value of a base10 number from the console (User types base10 number, then hits enter)

Control flow

If statements will execute if the target of the given pointer is a non-zero value. Syntax:

(pointer_name){More BrainCube code}


While statements will repeatedly execute while the target of the given pointer has a non-zero value. Syntax:

(pointer_name)[More BrainCube code]


Repeat statements will execute n times, with n being the initial value of the pointer's target cell, or a number literal written in place of the given pointer. Syntax:

(pointer_name OR number_literal)(More BrainCube code)

System commands

Command Description
! Stops program execution
? Triggers a garbage collection
# Debugging breakpoint (Exact behavior depends upon implementation)

Example programs

Hello, World!

/** Initialization **/
~ch~
ch(XXXXXXX) /*Move to [7][0][0]. The first 256 cells of the tape at [7][0] will hold ASCII values.*/

/** Set cells [7][0][0]..[7][0][255] to 0..255 **/
ch(>+)          /*Set [7][0][1] to 1*/
(ch)[
    (ch)(
        ch(>+<) /*Increase the cell to the left of the current cell by one.*/
    )           /*The result of this repeat loop is setting the cell to the left of ch to the value of ch*/
    ch(>+)      /*Move ch to the left, and increment that cell's value, setting the value of [7][0][n] to n*/
]               /*This loop exits after the value of ch overflows back to zero.*/
ch(<)
(ch)[ch(<)]     /*Go back to [7][0][0]*/

/** Print "Hello, World!" followed by null terminator and newline **/
/*
    Each line moves ch to [7][0][the number in the parentheses],
        prints the value as a character,
        and moves back to [7][0][0]
*/
(072)(ch(>))ch(.)(ch)[ch(<)] /*  H */
(101)(ch(>))ch(.)(ch)[ch(<)] /*  e */
(108)(ch(>))ch(.)(ch)[ch(<)] /*  l */
(108)(ch(>))ch(.)(ch)[ch(<)] /*  l */
(111)(ch(>))ch(.)(ch)[ch(<)] /*  o */
(044)(ch(>))ch(.)(ch)[ch(<)] /*  , */
(032)(ch(>))ch(.)(ch)[ch(<)] /*    */
(087)(ch(>))ch(.)(ch)[ch(<)] /*  W */
(111)(ch(>))ch(.)(ch)[ch(<)] /*  o */
(114)(ch(>))ch(.)(ch)[ch(<)] /*  r */
(108)(ch(>))ch(.)(ch)[ch(<)] /*  l */
(100)(ch(>))ch(.)(ch)[ch(<)] /*  d */
(033)(ch(>))ch(.)(ch)[ch(<)] /*  ! */
(000)(ch(>))ch(.)(ch)[ch(<)] /* \0 */
(010)(ch(>))ch(.)(ch)[ch(<)] /* \n */

External Resources