kmichel / bf0x

A static c++ brainfuck compiler using c++0x features in 200 nearly readable lines.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bf0x

A static brainfuck compiler in c++0x.

Code is fed to the compiler using a big ugly variadic template.

auto multiply = bf0x::interpreter<bf0x::bstack<bf0x::interpreter>,
     ',','>',',','>','+','+','+','+','+','+','+','+','[','<','-','-',
     '-','-','-','-','<','-','-','-','-','-','-','>','>','-',']','<',
     '<','[','>','[','>','+','>','+','<','<','-',']','>','>','[',
     '<','<','+','>','>','-',']','<','<','<','-',']','>','>','>',
     '+','+','+','+','+','+','[','<','+','+','+','+',
     '+','+','+','+','>','-',']',',','<','.','>','.'>();

bf0x::state st;
multiply.exec(st);

A bunch of variadic templates consume the string char by char, using pattern matching (partial specialization on the first char) and build a big recursive function exec(state& st) which is then inlined and optimized by the c++0x compiler.

Thanks to the c++0x compiler, mix of +, -, < and > are simplified. For example, the code ">++++++++[<------<------>>-]" is assembled into something equivalent to :

data[0] = 8;
while (data[0] != 0) {
    data[1] -= 6;
    data[2] -= 6;
    data[0] --;
}

About

A static c++ brainfuck compiler using c++0x features in 200 nearly readable lines.


Languages

Language:C++ 100.0%