-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuckInterpreter.cpp
132 lines (124 loc) · 3.7 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
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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int findMatchedRight(const string &line, int pos);
int findMatchedLeft(const string &line, int pos);
int main(int argc, char *args[])
{
if (argc != 1)
{
}
while (true)
{
cout << "Input a brainf**k line." << endl
<< "Input \"exit\" to exit." << endl;
string perLine;
getline(cin, perLine);
if (perLine == "exit")
return 0;
size_t size = 10;
char *buffer = new char[size]{};
int pos = 0;
for (int i = 0; i < perLine.length();)
{
switch (perLine[i])
{
case '+':
buffer[pos]++;
break;
case '-':
buffer[pos]--;
break;
case '>':
pos++;
if (pos > size) //if allocated memory is used up
{
size += 10;
buffer = static_cast<char *>(realloc(buffer, size)); //just extend the memory
}
break;
case '<':
pos--;
if (pos < 0) //obviously a minus 'pos' is invalid
{
cout << endl
<< "Oops! Over flow error!";
return 1;
}
break;
case '.':
cout << buffer[pos];
break;
case ',':
buffer[pos] = getchar();
break;
case '[':
if (buffer[pos] == 0)
{
i = findMatchedRight(perLine, i + 1) + 1;
if (i == -1)
{
cout << endl << "Unexpected symbol '['.";
goto end;
}
continue;
}
break;
case ']':
i = findMatchedLeft(perLine, i - 1);
if (i == -1)
{
cout << endl << "Unexpected symbol ']'.";
goto end;
}
continue;
default:
cout << "Undefined symbol"
<< "'" << perLine[i] << "'.";
return 1;
}
++i;
}
end:
delete[] buffer;
buffer = nullptr;
}
return 0;
}
//find matched ']'; returns -1 if nothing found
int findMatchedRight(const string &line, int pos)
{
int left = 0;
for (; pos < line.length(); ++pos)
{
if (line[pos] == '[') //A '[' means that there is a nested '[]' blocks...
++left; //So we increase the value of 'left' to mark it.
else if (line[pos] == ']') //if we found a ']'
{
if (!left) //left == 0 means the ']' we found does match initial '['
return pos;
else //In this case, the ']' we found marks the end of a nested block
--left; //So we decrease 'left', means we have reached the end of one nested block
}
}
return -1;
}
//find matched '['; returns -1 if nothing found
int findMatchedLeft(const string &line, int pos)
{
int right = 0;
for (; pos >= 0; --pos)
{
if (line[pos] == ']')
++right;
else if (line[pos] == '[')
{
if (!right)
return pos;
else
--right;
}
}
return -1;
}