https://github.com/tfc/cpp_template_meta_brainfuck_interpreter
A Brainfuck Interpreter, which is completely implemented from scratch as a C++ Template Meta Program
https://github.com/tfc/cpp_template_meta_brainfuck_interpreter
Last synced: 8 months ago
JSON representation
A Brainfuck Interpreter, which is completely implemented from scratch as a C++ Template Meta Program
- Host: GitHub
- URL: https://github.com/tfc/cpp_template_meta_brainfuck_interpreter
- Owner: tfc
- Created: 2016-05-02T07:28:58.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-03-22T12:38:42.000Z (over 4 years ago)
- Last Synced: 2025-04-10T04:38:52.011Z (about 1 year ago)
- Language: C++
- Size: 19.5 KB
- Stars: 24
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
``` bash
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:
``` bash
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 >'
debug_t::list> t;
^
main.cpp:6:29: note: template is declared here
template class debug_t;
^
1 error generated.
Makefile:3: recipe for target 'default' failed
make: *** [default] Error 1
```
The "Hello World" example, printed at runtime:
``` bash
Jacek.Galowicz ~/src/tmp_brainfuck $ g++ -o main main.cpp -std=c++14 -DPROGRAM_STR='"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.+++."'
Jacek.Galowicz ~/src/tmp_brainfuck $ ./main
Hello World!
```