forked from Maxoplata/StringToINTERCAL
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringToINTERCAL.cpp
95 lines (73 loc) · 2.1 KB
/
StringToINTERCAL.cpp
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
88
89
90
91
92
93
94
95
/**
* StringToINTERCAL.cpp
*
* Converts a string to an INTERCAL script that will output said string.
* usage: g++ StringToINTERCAL.cpp -o StringToINTERCAL && ./StringToINTERCAL your string here
*
* @author Maxamilian Demian
* @link https://www.maxodev.org
* @link https://github.com/Maxoplata/StringToINTERCAL
*/
#include <iostream>
// class definition
class StringToINTERCAL {
private:
int politeCount;
std::string politeLine(std::string line) {
if (politeCount == 3) {
politeCount = 0;
return "PLEASE " + line + "\n";
}
politeCount++;
return "DO " + line + "\n";
}
public:
StringToINTERCAL() {
politeCount = 0;
}
std::string convertToINTERCAL(std::string str) {
// reset politeCount
politeCount = 0;
std::string ret = politeLine(",1 <- #" + std::to_string(str.length()));
int lastCharLoc = 256;
for (int i = 0; i < str.length(); i++) {
// convert char to its binary value
std::string charLocBinary = std::bitset<8>((int) str.at(i)).to_string();
// reverse binary string
reverse(charLocBinary.begin(), charLocBinary.end());
// convert reversed binary string to integer
int charLoc = stoi(charLocBinary, nullptr, 2);
int movePosition = 0;
if (charLoc < lastCharLoc) {
movePosition = (lastCharLoc - charLoc);
} else if (charLoc > lastCharLoc) {
movePosition = (256 - charLoc) + lastCharLoc;
}
lastCharLoc -= movePosition;
if (lastCharLoc < 1) {
lastCharLoc = 256 + lastCharLoc;
}
ret += politeLine(",1 SUB #" + std::to_string(i + 1) + " <- #" + std::to_string(movePosition));
}
ret += politeLine("READ OUT ,1");
ret += politeLine("GIVE UP");
return ret;
}
};
// 1337 codez
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]);
}
// do magic...
StringToINTERCAL myINTERCAL;
std::cout << myINTERCAL.convertToINTERCAL(inputString) << std::endl;
}
}