Skip to content

Commit ed38caf

Browse files
Changed from an interpreter to a compiler
1 parent 8f9b8f1 commit ed38caf

3 files changed

Lines changed: 66 additions & 36 deletions

File tree

src/file.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ char* read_file(const char* filename)
2222

2323
return buf;
2424
}
25+
26+
FILE *open_file(const char* filename) {
27+
FILE *fptr;
28+
fptr = fopen(filename, "w");
29+
30+
assert(fptr != NULL);
31+
32+
return fptr;
33+
}

src/file.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include <stdio.h>
22
#include <string.h>
33
#include <stdlib.h>
4+
#include <assert.h>
45

56
char* read_file(const char* filename);
7+
8+
FILE *open_file(const char* filename);

src/main.c

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,102 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <stdint.h>
4+
#include <time.h>
5+
#include <string.h>
6+
#include <assert.h>
47

58
#include "file.h"
69

710
#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+
}
836

937
int main(int argc, char **argv)
1038
{
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>");
1342
return EXIT_FAILURE;
1443
}
1544

16-
char* filename = argv[1];
45+
char* ifilename = argv[1];
46+
char* ofilename = argv[2];
1747

18-
char* file = read_file(filename);
48+
char* file = read_file(ifilename);
49+
FILE* fptr = open_file(ofilename);
1950

2051
if (file == NULL) {
2152
fprintf(stderr, "bf: failed to read file.\n");
2253
return EXIT_FAILURE;
2354
}
2455

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);
3264

3365
char *element = file;
3466
while (*element != '\0') {
3567
switch (*element) {
3668
case '>':
37-
dp = (dp + 1 + NUM_ELEMENTS) % NUM_ELEMENTS;
69+
fprint(fptr, "el++;\n", 0);
3870
break;
3971
case '<':
40-
dp = (dp - 1 + NUM_ELEMENTS) % NUM_ELEMENTS;
72+
fprint(fptr, "el--;\n", 0);
4173
break;
4274
case '-':
43-
array[dp] = array[dp] - 1;
75+
fprint(fptr, "(*el)--;\n", 0);
4476
break;
4577
case '+':
46-
array[dp] = array[dp] + 1;
78+
fprint(fptr, "(*el)++;\n", 0);
4779
break;
4880
case '.':
49-
printf("%c", (char)array[dp]);
81+
fprint(fptr, "printf(\"%%c\", *el);\n", 0);
5082
break;
5183
case ',':
52-
array[dp] = getchar();
84+
fprint(fptr, "*el = getchar();\n", 0);
5385
break;
5486
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);
6388
break;
6489
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;
7492
}
7593

7694
element++;
7795
}
7896

97+
fprintf(fptr, "}\n");
98+
fclose(fptr);
7999
free(file);
80-
free(array);
81-
array = NULL;
82100

83101
return 0;
84102

0 commit comments

Comments
 (0)