BFshort

From Esolang
Jump to navigation Jump to search

BFshort is a Brainfuck derivative where scripts are usually 2x shorter.

Commands

  +-<>[].,
+`12345678
-90-=qwert
<yuiop[]\a
>sdfghjkl;
['zxcvbnm,
]./~!@#$%^
.&*()_+QWE
,RTYUIOP{}

Cat

OQ

Counter

5*.

Converter to and from brainfuck

Written in JavaScript.

const matrix = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}";
const bfChars = "+-<>[].,";

function bfshortToBrainfuck(program)
{
    let result = "";
    for(const char of program)
    {
        const index = matrix.indexOf(char);
        if(index >= 0)
        {
            result += bfChars.charAt(Math.floor(index / 9));
            if(index % 9 > 0)
                result += bfChars.charAt(index % 9 - 1);
        }
    }
    return result;
}

function brainfuckToBFshort(program)
{
    program = program.replace(/[^-+<>\[\].,]/g, "");
    
    let result = "";
    for(let i = 0; i < program.length; i += 2)
    {
        const first = program.charAt(i);
        const second = program.charAt(i + 1) || " ";
        const firstIndex = bfChars.indexOf(first);
        const secondIndex = " +-<>[].,".indexOf(second);
        result += matrix[firstIndex * 9 + secondIndex];
    }
    return result;
}