-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
361 lines (307 loc) · 10.9 KB
/
Copy pathmain.c
File metadata and controls
361 lines (307 loc) · 10.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#define CLAP_IMPLEMENTATION
#include "clap.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAKE_ARRAY_TYPE(Type, name) \
typedef struct { \
Type *items; \
size_t count; \
} name
typedef enum {
DP_INC,
DP_DEC,
DATA_INC,
DATA_DEC,
INPUT,
OUTPUT,
JZ,
JNZ
} token_type;
MAKE_ARRAY_TYPE(token_type, tok_type_arr);
typedef struct {
token_type type;
union {
size_t jumploc; // for jz, jnz
int numtimes; // otherwise
};
} token;
MAKE_ARRAY_TYPE(token, tok_arr);
typedef struct {
unsigned char *cells;
int length;
int dataPtr;
} memory;
int make_token(const char c) {
switch (c) {
case '>': return DP_INC;
case '<': return DP_DEC;
case '+': return DATA_INC;
case '-': return DATA_DEC;
case '[': return JZ;
case ']': return JNZ;
case '.': return OUTPUT;
case ',': return INPUT;
default: return -1;
}
}
int tokenise(const char *input, const size_t inp_len, tok_type_arr *tok_types) {
for (size_t i = 0; i < inp_len; i++) {
int tok_type = make_token(input[i]);
if (tok_type >= 0) {
tok_types->items[tok_types->count++] = (token_type)tok_type;
}
}
return 0;
}
int can_collapse(token_type a, token_type b) {
switch (a) {
case JZ: // FALLTHROUGH
case JNZ: return 0;
case DP_INC:
if (b == DP_DEC) { return -1; }
break;
case DP_DEC:
if (b == DP_INC) { return -1; }
break;
case DATA_INC:
if (b == DATA_DEC) { return -1; }
break;
case DATA_DEC:
if (b == DATA_INC) { return -1; }
break;
default: break;
}
return a == b;
}
/* Parses TOKS into CMDS, combining collapsable tokens.
Returns a negative value if an unbalanced closing
bracket is found, or a positive value if an
unbalanced opening bracket is found. */
int parse(tok_type_arr *tok_types, tok_arr *toks) {
// combine collapsable tokens
int param_counter = 1;
size_t tok_type_scanner = 0;
while (tok_type_scanner < tok_types->count) {
token_type curr_tok_type = tok_types->items[tok_type_scanner];
for (;;) {
if (tok_type_scanner++ >= tok_types->count) { break; }
int collapse_sign =
can_collapse(curr_tok_type, tok_types->items[tok_type_scanner]);
if (collapse_sign == 0) { break; }
param_counter += collapse_sign;
}
toks->items[toks->count++] =
(token){curr_tok_type, {.numtimes = param_counter}};
param_counter = 1;
}
// jump location resolution
size_t jump_idx_stack[toks->count * sizeof(int)];
size_t jump_idx_stack_head = 0;
for (size_t i = 0; i < toks->count; i++) {
switch (toks->items[i].type) {
case JZ: jump_idx_stack[jump_idx_stack_head++] = i; break;
case JNZ:
// trying to pop from empty stack
if (jump_idx_stack_head == 0) { return -1 - (int)i; }
size_t jump_pos = jump_idx_stack[--jump_idx_stack_head];
toks->items[i].jumploc = jump_pos;
toks->items[jump_pos].jumploc = i;
break;
default: break;
}
}
// leftover items in stack
if (jump_idx_stack_head != 0) {
return (int)jump_idx_stack[--jump_idx_stack_head] + 1;
}
return 0;
}
/* Execute CMDS given MEMORY of lengith MEMSIZE and the current
DATAPTR position. On success returns the number of characters printed,
a negative return means that DATAPTR went out of bounds. */
int interpret_cmds(memory *mem, tok_arr *toks) {
size_t cmd_ptr = 0;
int num_printed = 0;
while (cmd_ptr < toks->count) {
token curr_tok = toks->items[cmd_ptr];
switch (curr_tok.type) {
case DP_INC:
mem->dataPtr += curr_tok.numtimes;
if (mem->dataPtr > mem->length) { return -2; }
break;
case DP_DEC:
// unsigned ints, cannot check for < 0 after decrement
if (mem->dataPtr < curr_tok.numtimes) { return -1; }
mem->dataPtr -= curr_tok.numtimes;
break;
case DATA_INC: mem->cells[mem->dataPtr] += curr_tok.numtimes; break;
case DATA_DEC: mem->cells[mem->dataPtr] -= curr_tok.numtimes; break;
case INPUT: mem->cells[mem->dataPtr] = (unsigned char)getchar(); break;
case OUTPUT:
for (int i = 0; i < curr_tok.numtimes; i++) {
putchar(mem->cells[mem->dataPtr]);
num_printed++;
}
break;
case JZ:
if (mem->cells[mem->dataPtr] == 0) { cmd_ptr = curr_tok.jumploc; }
break;
case JNZ:
if (mem->cells[mem->dataPtr] != 0) { cmd_ptr = curr_tok.jumploc; }
break;
}
cmd_ptr++;
}
return num_printed;
}
/* Runs INP as bf code, given MEMORY, DATAPTR, etc. Like interpretCmds,
returns number of characters printed and a negative value on error. */
int run_bf(size_t inp_len, const char *inp, memory *mem, int debug) {
tok_type_arr toks = {calloc(inp_len, sizeof(token_type)), 0};
tokenise(inp, inp_len, &toks);
if (debug) {
printf("------- Generated tokens start -------\n");
for (size_t i = 0; i < toks.count; i++) {
printf("%d ", toks.items[i]);
}
printf("\n------- Generated tokens end -------\n");
}
tok_arr cmds = {calloc(toks.count, sizeof(token)), 0};
int parse_err = parse(&toks, &cmds);
if (parse_err < 0) {
fprintf(stderr, "Unbalanced closing bracket found at position %d\n",
abs(parse_err + 1));
return -1;
} else if (parse_err > 0) {
fprintf(stderr, "Unbalanced opening bracket found at position %d\n",
parse_err - 1);
return -1;
}
if (debug) {
printf("------- Generated commands start -------\n");
for (size_t i = 0; i < cmds.count; i++) {
// numtimes isnt technically correct here...
printf("%d: %d\n", cmds.items[i].type, cmds.items[i].numtimes);
}
printf("------- Generated commands end -------\n");
printf("------- Output start -------\n");
}
int chars_printed = interpret_cmds(mem, &cmds);
if (debug) { printf("\n------- Output end -------\n"); }
if (chars_printed < 0) {
fprintf(stderr, "Data pointer out of bounds!\n");
return -1;
}
return chars_printed;
}
/* Helper function that reads the entirety of the
file at FILEPATH into the string pointed to by OUT. */
int read_file(const char *file_path, char **out) {
FILE *file = fopen(file_path, "r");
if (file == NULL) { return errno; }
if (fseek(file, 0, SEEK_END) < 0) { return errno; }
unsigned long file_length = (unsigned long)ftell(file);
if (file_length <= 0) { return errno; }
if (fseek(file, 0, SEEK_SET) < 0) { return errno; }
char *data = malloc(file_length);
if (data == NULL) { return errno; }
if (fread(data, 1, file_length, file) < file_length) {
return ferror(file);
}
if (data == NULL) {
perror("filling input buffer failed");
return 1;
}
// null terminate
data[file_length - 1] = 0;
if (fclose(file) == EOF) { return errno; }
*out = data;
return 0;
}
#define HELP_TEXT \
"Usage:\n" \
" %s [OPTIONS] run [-d] <file> Run a file containing bf code\n" \
" %s [OPTIONS] repl Run bf code interactively in a repl\n" \
"\n" \
"Glabal options:\n" \
" --help, -h Print this help message\n" \
" --memsize, -m Set the maximum bf memory tape size\n" \
"\n" \
"Mode specific options:\n" \
" --debug, -d With `run`, show debug information\n"
void show_help(const char *prog_name) {
fprintf(stderr, HELP_TEXT, prog_name, prog_name);
}
int main(int argc, char **argv) {
char *prog_name = argv[0];
clap_arg args[] = {{"--help", "-h", 0, NULL},
{"--memsize", "-m", 1, NULL},
{"repl", "", 0, NULL},
{"--debug", "-d", 0, NULL},
{"run", "", 1, NULL}};
clap_arg_array args_arr = {args, sizeof(args) / sizeof(args[0])};
clap_parsed_array *parsed = malloc(sizeof(clap_parsed_array));
clap_unexpected_array *unexpected = malloc(sizeof(clap_unexpected_array));
int parse_err = clap_parse_args(&args_arr, argc, argv, parsed, unexpected);
if (parse_err != 0) {
fprintf(stderr, "Failed to parse arguments!\n\n");
show_help(prog_name);
return EXIT_FAILURE;
}
if (clap_has_flag(parsed, "--help")) {
show_help(prog_name);
return EXIT_SUCCESS;
}
clap_parsed *opt;
size_t bfMemSize = 30000;
if ((opt = clap_get_opt(parsed, "--memsize")) != NULL) {
bfMemSize = strtoul(opt->params[0], NULL, 10);
}
if (clap_has_flag(parsed, "repl")) {
printf("cbf: a simple interactive brainfuck interpreter\n"
"(memory tape %zu x 1 byte cells)\n"
"Type `exit` or CTRL-D to exit\n",
bfMemSize);
memory bfmem = {calloc(bfMemSize, 1), (int)bfMemSize, 0};
char line[200];
for (;;) {
printf("bf> ");
fflush(stdout);
const char *lineErr = fgets(line, sizeof(line), stdin);
if (lineErr == NULL || strcmp(line, "exit\n") == 0) {
if (feof(stdin)) { putchar('\n'); }
break;
}
int charsPrinted = run_bf(strlen(line), line, &bfmem, 0);
if (charsPrinted != 0) { putchar('\n'); }
}
return EXIT_SUCCESS;
}
int debug = 0;
if (clap_has_flag(parsed, "--debug")) { debug = 1; }
if ((opt = clap_get_opt(parsed, "run")) != NULL) {
if (argc < 3) {
fprintf(stderr, "Please provide a file!\n\n");
show_help(prog_name);
return EXIT_FAILURE;
}
const char *fileName = opt->params[0];
char *inp;
int readErr = read_file(fileName, &inp);
if (readErr != 0) {
fprintf(stderr, "failed to read file %s: %s\n", fileName,
strerror(readErr));
return EXIT_FAILURE;
}
memory bfMem = {calloc(bfMemSize, 1), (int)bfMemSize, 0};
size_t inpLen = strlen(inp);
int numCharsPrinted = run_bf(inpLen, inp, &bfMem, debug);
if (numCharsPrinted < 0) { return numCharsPrinted; }
return 0;
}
fprintf(stderr, "Provided arguments not recognised!\n\n");
show_help(prog_name);
return EXIT_FAILURE;
}