-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfi.c
92 lines (85 loc) · 1.92 KB
/
bfi.c
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
#include <fcntl.h>
#include <libgen.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define CONFIG_MEM_SIZE 30000
#define CONFIG_STACK_SIZE 200
int main(int argc, char **argv)
{
int fd;
struct stat buf;
unsigned char memory[CONFIG_MEM_SIZE];
int memory_index = 0;
unsigned int stack[CONFIG_STACK_SIZE];
int stack_index = 0;
char *instr;
int instr_index = 0;
if (argc != 2)
{
fprintf(stderr, "usage: %s FILE\n", basename(argv[0]));
exit(1);
}
if ((fd = open(argv[1], O_RDONLY)) == -1)
{
perror(argv[1]);
exit(2);
}
fstat(fd, &buf);
instr = malloc(buf.st_size);
read(fd, instr, buf.st_size);
close(fd);
for (int i = 0; i < CONFIG_MEM_SIZE; ++i)
memory[i] = 0;
while (instr_index < buf.st_size)
{
switch (instr[instr_index])
{
case '+':
memory[memory_index] += 1;
break;
case '-':
memory[memory_index] -= 1;
break;
case '>':
memory_index = (memory_index + 1) % CONFIG_MEM_SIZE;
break;
case '<':
memory_index = (memory_index - 1) % CONFIG_MEM_SIZE;
break;
case '.':
write(STDOUT_FILENO, memory + memory_index, 1);
break;
case ',':
read(STDIN_FILENO, memory + memory_index, 1);
break;
case '[':
if (memory[memory_index])
{
stack[stack_index] = instr_index;
++stack_index;
}
else
{
int depth_count = 1;
while (depth_count && instr_index < buf.st_size)
{
++instr_index;
if (instr[instr_index] == '[')
++depth_count;
else if (instr[instr_index] == ']')
--depth_count;
}
}
break;
case ']':
--stack_index;
instr_index = stack[stack_index];
continue;
}
++instr_index;
}
free(instr);
}