-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.cpp
69 lines (65 loc) · 1.65 KB
/
interpreter.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
/*
* Simple interpreter brainfuck on Cpp (C++)
* Coded by Ehsonjon (a.k.a iCoder)
* DATE: 23.07.2020 (18:03) Tajikistan Asia Time =D
* (C) 2020-2021 All rights reversed!
*/
#include <iostream> // for cout and cin !
#include <fstream> // for ifstream
#include <vector> // for our brainfuck code =D
using namespace std;
static char cpu[30000];
int main(int argc, char **argv) {
vector<char> acc;
char ch;
ifstream infile(argv[1]);
while (infile) {
infile.get(ch);
acc.push_back(ch);
}
infile.close();
unsigned int j = 0;
int brc = 0;
for (int i = 0; i < acc.size(); ++i) {
if (acc[i] == '>')
j++;
if (acc[i] == '<')
j--;
if (acc[i] == '+')
cpu[j]++;
if (acc[i] == '-')
cpu[j]--;
if (acc[i] == '.')
cout << cpu[j];
if (acc[i] == ',')
cin >> cpu[j];
if (acc[i] == '[') {
if (!cpu[j]) {
++brc;
while (brc) {
++i;
if (acc[i] == '[')
++brc;
if (acc[i] == ']')
--brc;
}
} else
continue;
} else if (acc[i] == ']') {
if (!cpu[j])
continue;
else {
if (acc[i] == ']')
brc++;
while (brc) {
--i;
if (acc[i] == '[')
brc--;
if (acc[i] == ']')
brc++;
}
--i;
}
}
}
}