-
Notifications
You must be signed in to change notification settings - Fork 255
/
bf.c
302 lines (248 loc) · 6.21 KB
/
bf.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <libnotify.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef __clang__
#define COMPILER "clang"
#else
#define COMPILER "gcc"
#endif
struct str_s {
char* buf;
size_t size;
};
typedef struct str_s str_t;
void str_free(str_t* s) {
free(s->buf);
}
str_t readfile(const char* filename) {
const int fd = open(filename, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
struct stat file_stat;
int ret = fstat(fd, &file_stat);
if (ret < 0) {
fprintf(stderr, "Cannot fstat %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
size_t size = file_stat.st_size + 1;
char* buf = malloc(size);
if (!buf) {
fprintf(stderr, "Cannot allocate %ld bytes memory\n", size);
exit(EXIT_FAILURE);
}
ret = read(fd, buf, file_stat.st_size);
if (ret != file_stat.st_size) {
fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
buf[size - 1] = 0;
return (str_t){
.buf = buf,
.size = size
};
}
struct op_list;
enum op_type { OP_INC, OP_MOVE, OP_LOOP, OP_PRINT };
union op_value {
int offset;
struct op_list *list;
};
struct op {
enum op_type type;
union op_value value;
};
struct op_list {
struct op *ops;
size_t cap, len;
};
struct op op_new(enum op_type type, union op_value value) {
struct op op;
op.type = type;
if (type == OP_LOOP)
op.value.list = value.list;
else
op.value.offset = value.offset;
return op;
}
void op_list_free(struct op_list *list);
void op_free(struct op op) {
if (op.type == OP_LOOP) {
op_list_free(op.value.list);
free(op.value.list);
}
}
void op_list_free(struct op_list *list) {
for (size_t i = 0; i < list->len; i += 1)
op_free(list->ops[i]);
free(list->ops);
}
void op_list_grow(struct op_list *list) {
if (list->ops == NULL) {
list->cap = 4;
} else {
/* double the capacity */
list->cap <<= 1;
}
list->ops = realloc(list->ops, sizeof(struct op) * list->cap);
}
size_t op_list_length(const struct op_list *list) { return list->len; }
struct op op_list_get(const struct op_list *list, int i) {
return list->ops[i];
}
void op_list_push(struct op_list *list, struct op op) {
if (list->len == list->cap)
op_list_grow(list);
list->ops[list->len++] = op;
}
struct string_iterator {
const char *string;
int pos;
};
char string_iterator_next(struct string_iterator *it) {
return it->string[it->pos++];
}
struct printer {
int sum1;
int sum2;
bool quiet;
};
void print(struct printer *p, int n) {
if (p->quiet) {
p->sum1 = (p->sum1 + n) % 255;
p->sum2 = (p->sum2 + p->sum1) % 255;
} else {
putc(n, stdout);
}
}
int get_checksum(const struct printer *p) { return (p->sum2 << 8) | p->sum1; }
void parse(struct string_iterator *it, struct op_list *ops) {
char c;
struct op_list *loop_ops;
union op_value value;
enum op_type type;
while ((c = string_iterator_next(it))) {
switch (c) {
case '+':
type = OP_INC;
value.offset = 1;
break;
case '-':
type = OP_INC;
value.offset = -1;
break;
case '>':
type = OP_MOVE;
value.offset = 1;
break;
case '<':
type = OP_MOVE;
value.offset = -1;
break;
case '.':
type = OP_PRINT;
value.offset = 0;
break;
case '[':
loop_ops = calloc(1, sizeof(struct op_list));
parse(it, loop_ops);
type = OP_LOOP;
value.list = loop_ops;
break;
case ']':
return;
default:
continue;
}
op_list_push(ops, op_new(type, value));
}
}
struct tape {
int *tape;
size_t cap, pos;
};
char tape_get(const struct tape tape) { return tape.tape[tape.pos]; }
void tape_grow(struct tape *tape) {
size_t new_cap = tape->cap << 1;
tape->tape = realloc(tape->tape, sizeof(int) * new_cap);
int *tape_end = tape->tape + tape->cap;
memset(tape_end, 0, sizeof(int) * tape->cap);
tape->cap = new_cap;
}
void tape_move(struct tape *tape, int amount) {
tape->pos += amount;
if (tape->pos >= tape->cap)
tape_grow(tape);
}
void tape_inc(struct tape tape, int amount) { tape.tape[tape.pos] += amount; }
void eval(const struct op_list *ops, struct tape *tape, struct printer *p) {
struct op op;
for (size_t i = 0, len = op_list_length(ops); i < len; ++i) {
switch ((op = op_list_get(ops, i)).type) {
case OP_INC:
tape_inc(*tape, op.value.offset);
break;
case OP_MOVE:
tape_move(tape, op.value.offset);
break;
case OP_LOOP:
while (tape_get(*tape) > 0)
eval(op.value.list, tape, p);
break;
case OP_PRINT:
print(p, tape_get(*tape));
break;
}
}
}
void run(const char *code, struct printer *p) {
struct tape tape = {.tape = calloc(1, sizeof(int)), .cap = 1, .pos = 0};
struct op_list ops = {.ops = NULL, .cap = 0, .len = 0};
struct string_iterator it = {.string = code, .pos = 0};
parse(&it, &ops);
eval(&ops, &tape, p);
free(tape.tape);
op_list_free(&ops);
}
void verify() {
char text[] = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>"
"---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
struct printer p_left = {.sum1 = 0, .sum2 = 0, .quiet = true};
run(text, &p_left);
int left = get_checksum(&p_left);
struct printer p_right = {.sum1 = 0, .sum2 = 0, .quiet = true};
char result[] = "Hello World!\n";
size_t i = 0;
while (result[i] != '\0') {
print(&p_right, result[i++]);
}
int right = get_checksum(&p_right);
if (left != right) {
fprintf(stderr, "%d != %d", left, right);
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
verify();
struct printer p = {.sum1 = 0, .sum2 = 0, .quiet = getenv("QUIET") != NULL};
setbuf(stdout, NULL); // enable automatic flushing
if (argc < 2) {
fprintf(stderr, "Expected filename\n");
return EXIT_FAILURE;
}
const char *filename = argv[1];
__attribute__((__cleanup__(str_free))) str_t code = readfile(filename);
notify_with_pid("C/" COMPILER);
run(code.buf, &p);
notify("stop");
if (p.quiet) {
printf("Output checksum: %d\n", get_checksum(&p));
}
}