|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
3 | 3 | #include <stdint.h> |
| 4 | +#include <time.h> |
| 5 | +#include <string.h> |
| 6 | +#include <assert.h> |
4 | 7 |
|
5 | 8 | #include "file.h" |
6 | 9 |
|
7 | 10 | #define NUM_ELEMENTS 32768 // 2 ^ 15 |
| 11 | +#define INDENT " " |
| 12 | + |
| 13 | +char* make_indent(int count) { |
| 14 | + size_t len = sizeof(INDENT) * count; |
| 15 | + |
| 16 | + char* alloc = malloc(len + 1); |
| 17 | + |
| 18 | + assert(alloc != NULL); |
| 19 | + |
| 20 | + alloc[0] = '\0'; |
| 21 | + |
| 22 | + for (int i = 0; i < count; i++) { |
| 23 | + strcat(alloc, INDENT); |
| 24 | + } |
| 25 | + |
| 26 | + return alloc; |
| 27 | +} |
| 28 | + |
| 29 | +int indent_level = 0; |
| 30 | +void fprint(FILE *fptr, const char *str, int indent_increment) { |
| 31 | + assert(indent_level >= 0); |
| 32 | + if (indent_increment == -1) indent_level--; |
| 33 | + fprintf(fptr, "%s%s", make_indent(indent_level), str); |
| 34 | + if (indent_increment != -1) indent_level += indent_increment; |
| 35 | +} |
8 | 36 |
|
9 | 37 | int main(int argc, char **argv) |
10 | 38 | { |
11 | | - if (argc < 2) { |
12 | | - printf("bf: no file provided.\n"); |
| 39 | + if (argc < 3) { |
| 40 | + printf("bf: invalid usage.\n"); |
| 41 | + printf("Usage: bf <input> <output>"); |
13 | 42 | return EXIT_FAILURE; |
14 | 43 | } |
15 | 44 |
|
16 | | - char* filename = argv[1]; |
| 45 | + char* ifilename = argv[1]; |
| 46 | + char* ofilename = argv[2]; |
17 | 47 |
|
18 | | - char* file = read_file(filename); |
| 48 | + char* file = read_file(ifilename); |
| 49 | + FILE* fptr = open_file(ofilename); |
19 | 50 |
|
20 | 51 | if (file == NULL) { |
21 | 52 | fprintf(stderr, "bf: failed to read file.\n"); |
22 | 53 | return EXIT_FAILURE; |
23 | 54 | } |
24 | 55 |
|
25 | | - uint8_t *array = (uint8_t *)calloc(NUM_ELEMENTS, sizeof(uint8_t)); |
26 | | - uint16_t dp = 0; |
27 | | - |
28 | | - if (array == NULL) { |
29 | | - fprintf(stderr, "bf: memory allocation failed.\n"); |
30 | | - return EXIT_FAILURE; |
31 | | - } |
| 56 | + |
| 57 | + fprintf(fptr, "/* this file was automatically generated by bf.c (https://github.com/soulWritesCode/bf.c) at %ld */\n", time(NULL)); |
| 58 | + fprintf(fptr, "#include <stdio.h>\n"); |
| 59 | + fprintf(fptr, "#include <stdlib.h>\n"); |
| 60 | + fprintf(fptr, "\n"); |
| 61 | + fprint(fptr, "int main() {\n", 1); |
| 62 | + fprint(fptr, "char *tape = calloc(32768, sizeof(char));\n", 0); |
| 63 | + fprint(fptr, "char *el = tape;\n", 0); |
32 | 64 |
|
33 | 65 | char *element = file; |
34 | 66 | while (*element != '\0') { |
35 | 67 | switch (*element) { |
36 | 68 | case '>': |
37 | | - dp = (dp + 1 + NUM_ELEMENTS) % NUM_ELEMENTS; |
| 69 | + fprint(fptr, "el++;\n", 0); |
38 | 70 | break; |
39 | 71 | case '<': |
40 | | - dp = (dp - 1 + NUM_ELEMENTS) % NUM_ELEMENTS; |
| 72 | + fprint(fptr, "el--;\n", 0); |
41 | 73 | break; |
42 | 74 | case '-': |
43 | | - array[dp] = array[dp] - 1; |
| 75 | + fprint(fptr, "(*el)--;\n", 0); |
44 | 76 | break; |
45 | 77 | case '+': |
46 | | - array[dp] = array[dp] + 1; |
| 78 | + fprint(fptr, "(*el)++;\n", 0); |
47 | 79 | break; |
48 | 80 | case '.': |
49 | | - printf("%c", (char)array[dp]); |
| 81 | + fprint(fptr, "printf(\"%%c\", *el);\n", 0); |
50 | 82 | break; |
51 | 83 | case ',': |
52 | | - array[dp] = getchar(); |
| 84 | + fprint(fptr, "*el = getchar();\n", 0); |
53 | 85 | break; |
54 | 86 | case '[': |
55 | | - if (array[dp] == 0) { |
56 | | - int depth = 1; |
57 | | - while (depth > 0) { |
58 | | - element++; |
59 | | - if (*element == '[') depth++; |
60 | | - if (*element == ']') depth--; |
61 | | - } |
62 | | - } |
| 87 | + fprint(fptr, "while (*el != 0) {\n", 1); |
63 | 88 | break; |
64 | 89 | case ']': |
65 | | - if (array[dp] != 0) { |
66 | | - int depth = 1; |
67 | | - while (depth > 0) { |
68 | | - element--; |
69 | | - if (element < file) break; |
70 | | - if (*element == ']') depth++; |
71 | | - if (*element == '[') depth--; |
72 | | - } |
73 | | - } |
| 90 | + fprint(fptr, "}\n", -1); |
| 91 | + break; |
74 | 92 | } |
75 | 93 |
|
76 | 94 | element++; |
77 | 95 | } |
78 | 96 |
|
| 97 | + fprintf(fptr, "}\n"); |
| 98 | + fclose(fptr); |
79 | 99 | free(file); |
80 | | - free(array); |
81 | | - array = NULL; |
82 | 100 |
|
83 | 101 | return 0; |
84 | 102 |
|
|
0 commit comments