-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuckinterpreter.cpp
86 lines (83 loc) · 1.85 KB
/
brainfuckinterpreter.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
#include <bits/stdc++.h>
#include <stdio.h>
#include <fstream>
using namespace std;
void performAction(char c){
}
int main(int argc, char **argv)
{
if (argc > 2)
{
cout << "Expected 1 argument found 2";
return 1;
}
unsigned char cells[30000];
char ch;
bool isLooping=false,beginLoop=false;
vector<char> loopData;
for (int i = 0; i < 30000; i++)
cells[i] = 0;
int index = 0;
ifstream file;
file.open(argv[1]);
while (!file.eof())
{
if(isLooping){
ch=loopData[0];
loopData.erase(loopData.begin());
}
else
file >> ch;
if(beginLoop)
loopData.push_back(ch);
if (file.eof())
break;
// cout << ch;
switch (ch)
{
case '+':
cells[index]++;
// cout << cells[index];
break;
case '-':
cells[index]--;
// cout << int(cells[index]);
break;
case '.':
// cout << cells[index];
putchar(cells[index]);
// cout << endl;
break;
case ',':
cells[index] = getchar();
break;
case '[':
if(cells[index]==0)
break;
beginLoop=true;
break;
case ']':
if(cells[index]==0){
isLooping=false;
beginLoop=false;
loopData.clear();
break;
}
isLooping=true;
break;
case '>':
index++;
if (index == 30000)
index = 0;
break;
case '<':
index--;
if (index == -1)
index = 30000 - 1;
break;
default:
continue;
}
}
return 0;
}