-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
239 lines (213 loc) · 5.43 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <chrono>
#include <cstdint>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <unordered_map>
#include <vector>
#define PROGRAM_SIZE 30000
std::string get_file_contents(const char *filename) {
std::ifstream myfile(filename);
std::stringstream strStream;
strStream << myfile.rdbuf();
std::string content = strStream.str();
return content;
}
void filterProgram(const char *text, char *program) {
std::string allowed = "><+-.,[]";
while (*text) {
if (allowed.find(*text) != std::string::npos) {
uint8_t dupes = 0;
if (*text == '[' && *(text + 1) == '-' && *(text + 2) == ']' &&
*(text + 3) == '>') {
*program = 'C';
program++;
text += 4;
dupes = 1;
while (*text == '[' && *(text + 1) == '-' && *(text + 2) == ']' &&
*(text + 3) == '>') {
dupes += 1;
text += 4;
}
text--;
*program = dupes;
} else if (*text == '[' && *(text + 1) == '-' && *(text + 2) == ']') {
*program = 'N';
text++;
text++;
} else if (*text == '[' && *(text + 1) == '-' && *(text + 2) == '>' &&
*(text + 3) == '+' && *(text + 4) == '<' &&
*(text + 5) == ']') {
*program = 'S';
text += 5;
} else if (*text == '+') {
while (*text == '+') {
dupes++;
text++;
}
text--;
if (dupes > 1) {
*program = 'P';
program++;
*program = dupes;
} else {
*program = *text;
}
} else if (*text == '-') {
while (*text == '-') {
dupes++;
text++;
}
text--;
if (dupes > 1) {
*program = 'M';
program++;
*program = dupes;
} else {
*program = *text;
}
} else if (*text == '>') {
while (*text == '>') {
dupes++;
text++;
}
text--;
if (dupes > 1) {
*program = 'R';
program++;
*program = dupes;
} else {
*program = *text;
}
} else if (*text == '<') {
while (*text == '<') {
dupes++;
text++;
}
text--;
if (dupes > 1) {
*program = 'L';
program++;
*program = dupes;
} else {
*program = *text;
}
} else {
*program = *text;
}
program++;
}
text++;
}
}
// Function to precompute jump locations for loops
void precomputeJumps(const char *program, int *jumps, int programSize) {
// Initialize jumps array with -1 to indicate no jump
for (int i = 0; i < programSize; i++) {
jumps[i] = -1;
}
std::deque<int> stack;
int position = 0;
while (program[position] != '\0') {
if (program[position] == '[') {
stack.push_back(position);
} else if (program[position] == ']') {
if (stack.empty()) {
std::cerr << "Unmatched ']' at position " << position << std::endl;
exit(1);
}
int matchingBracket = stack.back();
stack.pop_back();
// Store the positions
jumps[matchingBracket] = position;
jumps[position] = matchingBracket;
}
position++;
}
if (!stack.empty()) {
std::cerr << "Unmatched '[' at position " << stack.back() << std::endl;
exit(1);
}
}
void executeCommand(char *&memory, int *&jumps, char *&program,
char *programStart) {
int index = program - programStart; // Explicitly cast to int
switch (*program) {
case '>':
memory++;
break;
case '<':
memory--;
break;
case '+':
(*memory)++;
break;
case '-':
(*memory)--;
break;
case '.':
putchar(*memory);
break;
case ',':
*memory = getchar();
break;
case '[':
if (*memory == 0) {
program = programStart + jumps[index]; // Jump forward to matching ]
}
break;
case ']':
if (*memory != 0) {
program = programStart + jumps[index]; // Jump back to matching [
}
break;
case 'N':
*memory = 0;
break;
case 'P':
*memory += *(++program);
break;
case 'M':
*memory -= *(++program);
break;
case 'R':
memory += *(++program);
break;
case 'L':
memory -= *(++program);
break;
case 'S':
*(memory + 1) = *memory;
*memory = 0;
break;
case 'C':
memset(memory, 0, *(++program));
memory += *program;
}
}
int main(int _, char *argv[]) {
auto start = std::chrono::high_resolution_clock::now();
std::string code = get_file_contents(argv[1]);
char *memory = (char *)malloc(PROGRAM_SIZE * sizeof(char));
char *program = (char *)malloc(PROGRAM_SIZE * sizeof(char));
char *programStart = program; // Save the original pointer for free later
char *memoryStart = memory; // Save the original pointer for free later
memset(memory, 0, PROGRAM_SIZE);
memset(program, 0, PROGRAM_SIZE);
filterProgram(code.c_str(), program);
// Precompute jump locations for loops
int *jumps = (int *)malloc(PROGRAM_SIZE * sizeof(int));
precomputeJumps(program, jumps, PROGRAM_SIZE);
while (*program) {
executeCommand(memory, jumps, program, programStart);
program++;
}
free(memoryStart);
free(programStart);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
printf("Program time: %.6f seconds\n", elapsed.count());
}