The following code transforms multi-line input into the the Brainfuck equivalent. One line turns into one program. The resulting programs are split by newlines for readability.
Brainf.cpp
#include "Brainf.hpp"
int main()
{
std::vector<std::string> userInput;
std::string currentLine = " "; // do not start empty
while (currentLine.length() != 0) // escape input by an empty line
{
std::getline(std::cin, currentLine);
userInput.push_back(currentLine);
}
std::vector<std::string>::size_type userInputSize = userInput.size();
for (unsigned i = 0; i < userInputSize; i++)
{
std::cout << modifyString(userInput[i]) << std::endl << std::endl;
}
}
Brainf.hpp
#ifndef BRAINF_HPP
#define BRAINF_HPP
#include <iostream>
#include <map>
#include <string>
#include <vector>
std::string modifyChar(char input, int carry)
{
std::string output;
int next = input - carry;
if (next > 0)
{
for (unsigned i = 0; i < next; ++i)
{
output += "+";
}
}
if (next < 0)
{
for (unsigned i = 0; i < abs(next); ++i)
{
output += "-";
}
}
output += ".";
return output;
}
std::string modifyString(std::string input)
{
std::string output;
int traveller = 0;
for (unsigned i = 0; i < input.length(); ++i)
{
output += modifyChar(input[i], traveller);
traveller = (int)input[i];
}
return output;
}
#endif
Explanation:
The print statement in Brainfuck is .
. The resulting Brainfuck prints the original string character-by-character, so there will be as much .
as there are characters to print. Between those .
there will be either +
if the ASCII is higher than the previous character and -
if it's lower.
The Brainfuck doesn't think. It increments, decrements or prints.
Example usage:
Input:
Hello World!
Output:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++
++++++++++++++++++++++.+++++++..+++.--------------------------------------------
-----------------------------------.++++++++++++++++++++++++++++++++++++++++++++
+++++++++++.++++++++++++++++++++++++.+++.------.--------.-----------------------
--------------------------------------------.
Input:
FizzBuzz
Foo!Bar!
Output:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++
++++++++++++++++++++++++++.+++++++++++++++++..----------------------------------
----------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++.+++++
..
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++
++++++++++++++++++++++++++++++++..----------------------------------------------
--------------------------------.+++++++++++++++++++++++++++++++++.+++++++++++++
++++++++++++++++++.+++++++++++++++++.-------------------------------------------
--------------------------------------.
Points of concern:
- I'm bad at naming things.
- My user-input handling feels bad.
- There's a cast in my code which feels like a design flaw.
- I may be needlessly repeating myself in one case while not splitting responsibilities far enough in another.
- It's not modular.
- It may not be idiomatic C++11.
- It may not be safe (although IMHO, there's not much which can go wrong).