-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcc.c
142 lines (125 loc) · 2.69 KB
/
bcc.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
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t data_size = 30000;
size_t instruction_size = 200;
size_t instruction_count;
int *jumps;
char *instructions;
char *data;
int dp = 0;
int ip = 0;
static void debug_print(char instruction, char *data, int dp, int ip)
{
#ifdef DEBUG
if (DEBUG) {
printf("Instruction: %c; dp: %d; data[dp]: %d; ip: %d\n",
instruction, dp, data[dp], ip);
}
#endif
}
static void build_jump_list(void)
{
jumps = malloc(instruction_count);
memset(jumps, -1, instruction_count);
int jump_stack[instruction_count];
int p_count = 0;
for (int offset = 0; offset < instruction_size; offset++) {
if (instructions[offset] == '[') {
p_count += 1;
jump_stack[p_count - 1] = offset;
} else if (instructions[offset] == ']') {
p_count -= 1;
int match = jump_stack[p_count];
jumps[offset] = match;
jumps[match] = offset;
jump_stack[p_count] = 0;
}
}
}
static void upsize(void **arr, size_t *curSize, size_t neededSize)
{
while (neededSize >= *curSize) {
void *tmp = realloc(*arr, 2 * *curSize * sizeof(*arr));
if (tmp) {
*arr = tmp;
*curSize *= 2;
}
}
}
int main(int argc, char **argv)
{
if (argc == 1) {
fprintf(stdout, "usage: bcc filename.bf\n");
return 1;
}
data = calloc(data_size, sizeof(char));
instructions = calloc(instruction_size, sizeof(char));
char c;
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
return 1;
}
while ((c = (char)getc(file)) != EOF) {
if (c != '[' && c != ']' && c != '>' && c != '<' && c != '+' &&
c != '-' && c != '.' && c != ',') {
continue;
}
upsize((void **)&instructions, &instruction_size,
instruction_count);
instructions[instruction_count++] = c;
}
fclose(file);
build_jump_list();
while (ip <= instruction_count) {
uint8_t instruction = instructions[ip];
switch (instruction) {
case '>':
if (dp < data_size)
dp += 1;
debug_print('>', data, dp, ip);
break;
case '<':
if (dp > 0)
dp -= 1;
debug_print('<', data, dp, ip);
break;
case '+':
data[dp] += 1;
debug_print('+', data, dp, ip);
break;
case '-':
data[dp] -= 1;
debug_print('-', data, dp, ip);
break;
case '.':
debug_print('.', data, dp, ip);
putchar(data[dp]);
break;
case ',':
debug_print(',', data, dp, ip);
scanf("%c", &data[dp]);
break;
case '[': {
debug_print('[', data, dp, ip);
if (data[dp] == 0) {
ip = jumps[ip];
}
} break;
case ']': {
debug_print(']', data, dp, ip);
if (data[dp] != 0) {
ip = jumps[ip];
}
} break;
}
ip += 1;
}
free(instructions);
free(data);
free(jumps);
printf("\n");
return 0;
}