All Projects → tfc → cpp_template_meta_brainfuck_interpreter

tfc / cpp_template_meta_brainfuck_interpreter

Licence: other
A Brainfuck Interpreter, which is completely implemented from scratch as a C++ Template Meta Program

Programming Languages

C++
36643 projects - #6 most used programming language
Nix
1067 projects
Makefile
30231 projects

A C++ Template Meta Brainfuck Interpreter

For exercise reasons, i implemented this Brainfuck Interpreter on a long train ride. It works completely at compile time, as it is implemented in a purely functional way, expressed in C++ Template Language.

The program and user input is provided via type lists, and the output is a type list again. The output string, as well as the Brainfuck Machine state can be printed as a compiler error message, or on the terminal when launching the program after compilation.

Usage

Program and user input are provided via preprocessor macros on the command line:

Jacek.Galowicz ~/src/tmp_brainfuck $ g++ -o main main.cpp -std=c++14 -DINPUT_STR=\"z\" -DPROGRAM_STR=\",[.-]\"
Jacek.Galowicz ~/src/tmp_brainfuck $ ./main
zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!

The brainfuck program output can also be printed at compile time, by producing an error message with the helper type debug_t:

"Hello World" example, printed at compile time:

Jacek.Galowicz ~/src/tmp_brainfuck $ g++ -o main main.cpp -std=c++14 -DPROGRAM_STR='"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.+++."'
main.cpp:31:51: error: implicit instantiation of undefined template 
'debug_t<char_tl<'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\n', '\x0D'> >'
    debug_t<typename tl_to_varlist<output>::list> t;
                                                  ^
main.cpp:6:29: note: template is declared here
template <typename T> class debug_t;
                            ^
1 error generated.
Makefile:3: recipe for target 'default' failed
make: *** [default] Error 1

The "Hello World" example, printed at runtime:

Jacek.Galowicz ~/src/tmp_brainfuck $ g++ -o main main.cpp -std=c++14 -DPROGRAM_STR='"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.+++."'
Jacek.Galowicz ~/src/tmp_brainfuck $ ./main
Hello World!
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].