-
Notifications
You must be signed in to change notification settings - Fork 0
/
VaginaFuck.cpp
187 lines (163 loc) · 4.69 KB
/
VaginaFuck.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
###################################################################
# Original Source: https://github.com/texus/Brainfuck-interpreter #
###################################################################
*/
#include <locale>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stack>
std::string inputFile(const std::string& filename)
{
std::string commands;
std::ifstream file(filename);
if (!file.is_open())
throw std::runtime_error("Failed to open '" + filename + "'.");
while (file.good()) {
commands.push_back(file.get());
}
/*
bug error, only output ASCII char if I use this code with Unicode;
uncomment if you want to compile this just for ASCII code
std::cout << "File interpreted!" << std::endl;
long fin_size = file.tellg();
file.seekg(0,file.beg);
std::cout << "File Size: "<<fin_size<<"B\n";
std::cout << " " << std::endl;
*/
return commands;
}
void interpretCode(const std::string& commands)
{
std::locale::global(std::locale(""));
std::cin.imbue(std::locale());
std::cout.imbue(std::locale());
std::cerr.imbue(std::locale());
std::wcin.imbue(std::locale());
std::wcout.imbue(std::locale());
std::wcerr.imbue(std::locale());
std::ofstream outfile;
outfile.open("out.vfto");
std::vector<int> data(1, 0);
std::vector<int>::iterator dataPtr = data.begin();
std::string::const_iterator instructionPtr = commands.begin();
std::stack<std::string::const_iterator> instructionStack;
while (instructionPtr != commands.end())
{
switch (*instructionPtr)
{
case '<':
{
dataPtr--;
break;
}
case '>':
{
dataPtr++;
if (dataPtr == data.end()) {
data.push_back(0);
dataPtr = data.end()-1;
}
break;
}
case '+':
{
(*dataPtr) += 1;
break;
}
case '-':
{
(*dataPtr) -= 1;
break;
}
case '*':
{
(*dataPtr) *= 4;
// (*dataPtr) *= 2;
// uncomment if you want the ASCII function
break;
}
case '.':
{
std::wcout << wchar_t(*dataPtr);
break;
}
case '@':
/* Custom brainfuck command
but cannot display the unicode char in output file because the fact that
filestream is not a member of std, so comment this case if you want to
compile this for ASCII only purpose (You can fix this!)*/
{
outfile << char(*dataPtr);
break;
}
case ',':
{
char input;
std::cin >> input;
*dataPtr = input;
}
case '[':
{
instructionStack.push(instructionPtr);
if (*dataPtr == 0)
{
auto startInstructionPtr = instructionPtr;
while (++instructionPtr != commands.end())
{
if (*instructionPtr == '[')
instructionStack.push(instructionPtr);
else if (*instructionPtr == ']')
{
if (instructionStack.empty())
throw std::runtime_error("error at syntax ']': syntax '[' expected!");
auto tempInstructionPtr = instructionStack.top();
instructionStack.pop();
if (startInstructionPtr == tempInstructionPtr)
break;
}
}
}
break;
}
case ']':
{
if (instructionStack.empty())
throw std::runtime_error("error at syntax ']': syntax '[' expected!");
if (*dataPtr != 0)
instructionPtr = instructionStack.top();
else
instructionStack.pop();
break;
}
default:
break;
}
instructionPtr++;
}
if (!instructionStack.empty())
throw std::runtime_error("error at syntax '[': syntax ']' expected!");
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cout << "VaginaFuck Interpreter 2.0" << std::endl;
std::cout << " " << std::endl;
std::cout << "Usage: ./VaginaFuck filename.bf" << std::endl;
return 1;
}
try
{
std::string commands = inputFile(argv[1]);
interpretCode(commands);
}
catch (const std::exception& e)
{
std::cout << std::endl << e.what() << std::endl;
}
return 0;
}