-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToBrainfuck.cpp
More file actions
87 lines (76 loc) · 2.83 KB
/
StringToBrainfuck.cpp
File metadata and controls
87 lines (76 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* StringToBrainfuck.cpp
*
* Converts a string to a Brainfuck script that will output said string.
* usage: g++ StringToBrainfuck.cpp -o StringToBrainfuck && ./StringToBrainfuck your string here
*
* @author Maxamilian Demian
* @link https://www.maxodev.org
* @link https://github.com/Maxoplata/StringToBrainfuck
*/
#include <iostream>
#include <math.h>
int main(int argc, char *argv[]) {
// if we have arguments passed to the script
if (argc > 1) {
// build input string
std::string inputString = "";
for (int i = 1; i < argc; i++) {
if (inputString != "") {
inputString.append(" ");
}
inputString.append(argv[i]);
}
// the Brainfuck code we will output in the end
std::string bfCode = "";
/**
* our current location on the "tape" (pointer 1).
* we use pointer 0 as a multiplier for pointer 1 to shorten the output script.
*
* e.g.
* A(65) = ++++++[>++++++++++<-]>+++++.
* ++++++ = add 6 to current pointer value (pointer 0)
* [ = while current pointer (pointer 0)'s value > 0
* > = move pointer ahead one (to pointer 1)
* ++++++++++ = add 10 to current pointer value (pointer 1)
* < = move pointer back one (to pointer 0)
* - = subtract 1 from current pointervalue (pointer 0)
* ] = end while loop
* > = move pointer ahead one (to pointer 1)
* +++++ = add 5 to current pointer value (pointer 1)
* . = print out character at current pointer value (pointer 1, value 65, char 'A')
*
* instead of:
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
*/
int currentLocation = 0;
// iterate through each character in the string
for (int i = 0; i < inputString.length(); i++) {
// get the Unicode code for the current character
int charVal = (int) inputString.at(i);
if (charVal > currentLocation) {
// move ahead on the "tape" to build the character
bfCode += std::string(floor((charVal - currentLocation) / 10), '+');
bfCode += "[>++++++++++<-]>";
bfCode += std::string(((charVal - currentLocation) % 10), '+');
} else if (charVal < currentLocation) {
// move backwards on the "tape" to build the character
bfCode += std::string(floor((currentLocation - charVal) / 10), '+');
bfCode += "[>----------<-]>";
bfCode += std::string(((currentLocation - charVal) % 10), '-');
} else {
// delete the "<" from the previous command as we are on the same character
// and we will want to print it out again
bfCode = bfCode.substr(0, (bfCode.length() - 1));
}
// print out the current character
bfCode += ".";
// if we are not on the last letter of the string, move pointer position back to 0
if (i < (inputString.length() - 1)) {
bfCode += "<";
}
currentLocation = charVal;
}
std::cout << bfCode << std::endl;
}
}