blob: fc065aa34c53135dd9dc3b730c727cd6475f6408 (
plain) (
blame)
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
|
#include <stdio.h>
#include <stack>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string>
using namespace std;
class Brainfuck {
private:
static const int init_length = 1024;
char *tape;
stack<int> loop_stack;
char *tape_pos;
public:
Brainfuck() : tape(new char[init_length]), tape_pos(tape + (init_length / 2)) {}
Brainfuck(int _length): tape(new char [_length]), tape_pos(tape + (_length / 2)) {}
void interpret(string& code) {
for (int i = 0; code[i]; ++i) {
switch (code[i]) {
case '\n': break;
case '<': --(this->tape_pos); break;
case '>': ++(this->tape_pos); break;
case '-': -- *(this->tape_pos); break;
case '+': ++ *(this->tape_pos); break;
case '[': this->loop_stack.push(i); break;
case ']':
if (*(this->tape_pos)) {
i = this->loop_stack.top();
} else {
this->loop_stack.pop();
}
break;
case '.': putchar(*this->tape_pos); break;
case ',': *(this->tape_pos) = (char)getchar();
default: break;
}
}
}
};
int main(int argc, char *argv[])
{
Brainfuck bf;
if (argc != 2) {
printf("Usage: <executable> <text_file_name>\n");
return 1;
}
int fd;
if (!(fd = open(argv[1], O_RDONLY))) {
printf("Error opening file \"%s\"\n", argv[1]);
return 1;
};
char buff[128];
string code;
while (int lth = read(fd, buff, 128)) {
buff[lth] = '\0';
code += buff;
}
bf.interpret(code);
return 0;
}
|