-
Notifications
You must be signed in to change notification settings - Fork 0
/
bil.c
461 lines (394 loc) · 12.1 KB
/
bil.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "inc/mpc.h"
// #include <editline/history.h>
#include <editline/readline.h>
#include <stdio.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define STYLE_BOLD "\033[1m"
#define STYLE_UNDERLINE "\033[4m"
char tape[33000];
char* ip;
char inp;
mpc_parser_t* Symbol;
mpc_parser_t* Comment;
mpc_parser_t* Loop;
mpc_parser_t* Expr;
mpc_parser_t* Bf;
mpc_parser_t* Number;
mpc_parser_t* Command;
mpc_parser_t* CommandName;
mpc_parser_t* InvalidCommandArg;
void eval_symbol(char* a) {
if(strcmp(a, "+") == 0) { ++*ip; }
if(strcmp(a, "-") == 0) { --*ip; }
if(strcmp(a, ">") == 0) { ip++; }
if(strcmp(a, "<") == 0) { ip--; }
if(strcmp(a, ".") == 0) { printf("%c", *ip); }
if(strcmp(a, ",") == 0) {
scanf("%c", &inp);
*ip = inp;
if (*ip == 0 || *ip == 10) {
scanf("%c", &inp);
*ip = inp;
}
}
}
void eval_loop(mpc_ast_t* t);
void eval_loop(mpc_ast_t* t) {
for (int i = 0;; ++i) {
if (strstr(t->children[i]->tag, "loop")) {
eval_loop(t->children[i]);
}
else if (strstr(t->children[i]->tag, "expr")) {
if (t->children[i]->children_num == 0)
eval_symbol(t->children[i]->contents);
else
for (int j = 0; j < t->children[i]->children_num; ++j) {
eval_symbol(t->children[i]->children[j]->contents);
}
}
else if (strcmp(t->children[i]->contents, "[") == 0) {
if (*ip == 0)
break;
}
else if (strcmp(t->children[i]->contents, "]") == 0) {
i = -1;
}
}
}
void print_tape(int index);
void command_print(char* index) {
errno = 0;
if (strcmp(index, "ip") == 0) {
print_tape(ip - tape);
} else {
long i = strtol(index, NULL, 10);
errno != ERANGE ? print_tape(i) : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
}
}
void command_help() {
printf("TODO: Help text");
}
void command_seek(char* index) {
errno = 0;
if (strcmp(index, "ip") == 0) {
printf(STYLE_BOLD ANSI_COLOR_YELLOW "Value not supplied. Operation not done.\n" ANSI_COLOR_RESET);
} else {
long i = strtol(index, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
ip = tape + i;
}
}
void command_peek(char* index) {
errno = 0;
if (strcmp(index, "ip") == 0) {
printf(STYLE_BOLD ANSI_COLOR_YELLOW "Value not supplied. Operation not done.\n" ANSI_COLOR_RESET);
} else {
long i = strtol(index, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
printf("%c\n", *(tape+i));
}
}
void command_peekd(char* index) {
errno = 0;
if (strcmp(index, "ip") == 0) {
printf(STYLE_BOLD ANSI_COLOR_YELLOW "Value not supplied. Operation not done.\n" ANSI_COLOR_RESET);
} else {
long i = strtol(index, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
printf("%d\n", *(tape+i));
}
}
void command_peekh(char* index) {
errno = 0;
if (strcmp(index, "ip") == 0) {
printf(STYLE_BOLD ANSI_COLOR_YELLOW "Value not supplied. Operation not done.\n" ANSI_COLOR_RESET);
} else {
long i = strtol(index, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
printf("%#x\n", *(tape+i));
}
}
void command_inc(char* value) {
errno = 0;
if (strcmp(value, "ip") == 0) {
printf(STYLE_BOLD ANSI_COLOR_YELLOW "Value not supplied. Operation not done.\n" ANSI_COLOR_RESET);
} else {
long val = strtol(value, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
*ip += val;
}
}
void command_dec(char* value) {
errno = 0;
if (strcmp(value, "ip") == 0) {
printf(STYLE_BOLD ANSI_COLOR_YELLOW "Value not supplied. Operation not done.\n" ANSI_COLOR_RESET);
} else {
long val = strtol(value, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
*ip -= val;
}
}
// 00000 111 222 333 444 555 666 777 888 .... ....
void dump_row(int row) {
int start = row * 8;
if (row == (ip - tape)/8)
printf(STYLE_BOLD ANSI_COLOR_GREEN "%5d " ANSI_COLOR_RESET, start);
else
printf("%5d ", start);
for (int i = 0; i < 8; ++i) {
if ((start + i) == (ip - tape))
printf(STYLE_BOLD ANSI_COLOR_GREEN " %3d " ANSI_COLOR_RESET, *(tape + start + i));
else
printf(" %3d ", *(tape + start + i));
if (i == 3)
printf(" ");
}
printf(" ");
for (int i = 0; i < 8; ++i) {
if ((start + i) == (ip - tape))
printf(STYLE_BOLD ANSI_COLOR_GREEN);
if (*(tape + start + i) == 0) {
printf(". ");
} else {
printf("%c ", *(tape + start + i));
}
printf(ANSI_COLOR_RESET);
if (i == 3)
printf(" ");
}
printf("\n");
}
void command_dump(char* value) {
errno = 0;
if (strcmp(value, "ip") == 0) {
if ((ip - tape) < 40) {
int count = ((ip - tape)/8) + 5;
for (int i = 0; i < count; ++i) {
dump_row(i);
}
} else {
int start_row = (ip - tape - 40)/8;
int end_row = (ip - tape + 48)/8;
int count = end_row - start_row;
for (int i = start_row; i < start_row + count; ++i) {
dump_row(i);
}
}
} else {
long count = strtol(value, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
for (int i = 0; i < count; ++i) {
dump_row(i);
}
}
}
void dump_row_hex(int row) {
int start = row * 8;
if (row == (ip - tape)/8)
printf(STYLE_BOLD ANSI_COLOR_GREEN "%5d " ANSI_COLOR_RESET, start);
else
printf("%5x ", start);
for (int i = 0; i < 8; ++i) {
if ((start + i) == (ip - tape))
printf(STYLE_BOLD ANSI_COLOR_GREEN " %3x " ANSI_COLOR_RESET, *(tape + start + i));
else
printf(" %3x ", *(tape + start + i));
if (i == 3)
printf(" ");
}
printf(" ");
for (int i = 0; i < 8; ++i) {
if ((start + i) == (ip - tape))
printf(STYLE_BOLD ANSI_COLOR_GREEN);
if (*(tape + start + i) == 0) {
printf(". ");
} else {
printf("%c ", *(tape + start + i));
}
printf(ANSI_COLOR_RESET);
if (i == 3)
printf(" ");
}
printf("\n");
}
void command_dump_hex(char* value) {
errno = 0;
if (strcmp(value, "ip") == 0) {
if ((ip - tape) < 40) {
int count = ((ip - tape)/8) + 5;
for (int i = 0; i < count; ++i) {
dump_row_hex(i);
}
} else {
int start_row = (ip - tape - 40)/8;
int end_row = (ip - tape + 48)/8;
int count = end_row - start_row;
for (int i = start_row; i < start_row + count; ++i) {
dump_row_hex(i);
}
}
} else {
long count = strtol(value, NULL, 10);
(errno != ERANGE) ? 1 : printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Number\n" ANSI_COLOR_RESET);
for (int i = 0; i < count; ++i) {
dump_row_hex(i);
}
}
}
void eval_command(mpc_ast_t* t) {
char* arg;
char* command_name;
for (int i = 0; i < t->children_num; i++) {
if (strstr(t->children[i]->tag, "inval_command_arg")) {
printf(STYLE_BOLD ANSI_COLOR_RED "Invalid Arguments\n" ANSI_COLOR_RESET);
return;
}
}
if (t->children_num == 0) {
command_name = t->contents;
arg = "ip";
}
else if (t->children_num == 2) {
command_name = t->children[0]->contents;
arg = t->children[1]->contents;
}
else {
printf(STYLE_BOLD ANSI_COLOR_RED "Error: Too many arguments\n" ANSI_COLOR_RESET);
}
if ((strcmp(command_name, "%%help") == 0) || (strcmp(command_name, "%%h") == 0)) { command_help(); }
if ((strcmp(command_name, "%%print") == 0) || (strcmp(command_name, "%%p") == 0)) { command_print(arg); }
if ((strcmp(command_name, "%%seek") == 0) || (strcmp(command_name, "%%s") == 0)) { command_seek(arg); }
if ((strcmp(command_name, "%%peek") == 0) || (strcmp(command_name, "%%pk") == 0)) { command_peek(arg); }
if ((strcmp(command_name, "%%peekd") == 0) || (strcmp(command_name, "%%pd") == 0)) { command_peekd(arg); }
if ((strcmp(command_name, "%%peekh") == 0) || (strcmp(command_name, "%%ph") == 0)) { command_peekh(arg); }
if ((strcmp(command_name, "%%inc") == 0) || (strcmp(command_name, "%%i") == 0)) { command_inc(arg); }
if ((strcmp(command_name, "%%dec") == 0) || (strcmp(command_name, "%%d") == 0)) { command_dec(arg); }
if ((strcmp(command_name, "%%dump") == 0) || (strcmp(command_name, "%%dd") == 0)) { command_dump(arg); }
if ((strcmp(command_name, "%%dumph") == 0) || (strcmp(command_name, "%%dh") == 0)) { command_dump_hex(arg); }
}
void read_bf(mpc_ast_t* t) {
if (strstr(t->tag, "symbol")) {
return eval_symbol(t->contents);
}
if (strstr(t->tag, "loop")) {
return eval_loop(t);
}
if (strstr(t->tag, "command")) {
return eval_command(t);
}
for (int i = 0; i < t->children_num; ++i) {
if (strcmp(t->children[i]->contents, "[") == 0) { continue; }
if (strcmp(t->children[i]->contents, "]") == 0) { continue; }
if(strcmp(t->children[i]->tag, "regex") == 0) { continue; }
if(strstr(t->children[i]->tag, "comment")) { continue; }
read_bf(t->children[i]);
}
}
void print_tape(int index) {
int start = (index/10) * 10;
int end = start + (10 - 1);
int _index = index % 10;
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf("\n");
for (int i = 0; i < w.ws_col; ++i) {
printf("-");
}
printf("\n");
printf("\n" STYLE_BOLD "Start" ANSI_COLOR_RESET " = %d\n", start);
printf(STYLE_BOLD "End" ANSI_COLOR_RESET " = %d\n", end);
printf(STYLE_BOLD "Index" ANSI_COLOR_RESET " = %d\n", index);
printf("\n+");
for (int i = start; i <= end; ++i) {
printf("-----+");
}
printf("\n|");
for (int i = start; i <= end; ++i) {
if ((i) == (ip - tape))
printf(STYLE_BOLD ANSI_COLOR_GREEN " %03d" ANSI_COLOR_RESET " |", tape[i]);
else
printf(" %03d |", tape[i]);
}
printf("\n+");
for (int i = start; i <= end; ++i) {
printf("-----+");
}
printf("\n");
for (int i = 0; i < _index; ++i) {
printf(" ");
}
printf(STYLE_BOLD ANSI_COLOR_BLUE " ^" ANSI_COLOR_RESET);
printf("\n");
for (int i = 0; i < _index; ++i) {
printf(" ");
}
printf(STYLE_BOLD ANSI_COLOR_BLUE " %3d\n\n" ANSI_COLOR_RESET, index);
}
int main(int argc, char** argv) {
Symbol = mpc_new("symbol");
Comment = mpc_new("comment");
Loop = mpc_new("loop");
Number = mpc_new("number");
InvalidCommandArg = mpc_new("inval_command_arg");
CommandName = mpc_new("command_name");
Command = mpc_new("command");
Expr = mpc_new("expr");
Bf = mpc_new("bf");
mpca_lang(MPCA_LANG_DEFAULT,
" \
symbol : '+' | '-' | '.' | ',' | '<' | '>' | '+' ; \
comment : /[^\\+\\-\\.,\\[\\]><%%]/ ; \
loop : '[' <expr>* ']' ; \
number : /[0-9ip]+/ ; \
inval_command_arg : /[^0-9ip]+/ ; \
command_name : /%%[a-z]+/ ; \
command : <command_name> <number> | <command_name> <inval_command_arg> | <command_name> ; \
expr : <loop> | <symbol>+ | <comment> ; \
bf : /^/ <expr>* /$/ | <command> ; \
",
Symbol, Comment, Loop, Number, InvalidCommandArg, CommandName, Command, Expr, Bf);
ip = tape;
if (argc == 1) {
puts(STYLE_BOLD ANSI_COLOR_BLUE "BIL! Version 0.0.0.1.0" ANSI_COLOR_RESET);
puts("Press " STYLE_BOLD ANSI_COLOR_RED "Ctrl+c" ANSI_COLOR_RESET " to Exit\n");
while (1) {
char* input = readline("bil> ");
add_history(input);
mpc_result_t r;
if (mpc_parse("<stdin>", input, Bf, &r)) {
read_bf(r.output);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
}
if (argc >= 2) {
for (int i = 1; i < argc; ++i) {
mpc_result_t r;
if (mpc_parse_contents(argv[i], Bf, &r)) {
read_bf(r.output);
putchar('\n');
print_tape(ip - tape);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
}
}
mpc_cleanup(8, Symbol, Comment, Loop, Number, InvalidCommandArg, CommandName, Command, Expr, Bf);
// free(ip);
return 0;
}