-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck-compiler-negone.c
257 lines (226 loc) · 6.5 KB
/
brainfuck-compiler-negone.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_SIZE 1024
typedef struct
{
size_t *data;
size_t top;
size_t capacity;
} Stack;
char b;
char *f = NULL, *s = NULL;
size_t a_size = 65536, f_size = 65536;
// Stack operations
void init_stack(Stack *stack)
{
stack->capacity = STACK_SIZE;
stack->data = malloc(stack->capacity * sizeof(size_t));
if (stack->data == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
stack->top = 0;
}
void push(Stack *stack, size_t value)
{
if (stack->top >= stack->capacity)
{
stack->capacity *= 2;
stack->data = realloc(stack->data, stack->capacity * sizeof(size_t));
if (stack->data == NULL)
{
perror("realloc");
exit(EXIT_FAILURE);
}
}
stack->data[stack->top++] = value;
}
size_t pop(Stack *stack)
{
if (stack->top == 0)
{
fprintf(stderr, "Stack underflow\n");
exit(EXIT_FAILURE);
}
return stack->data[--stack->top];
}
int is_empty(Stack *stack)
{
return stack->top == 0;
}
void free_stack(Stack *stack)
{
free(stack->data);
}
void expand_memory(char **arr, size_t *size, int is_left_expansion)
{
size_t new_size = *size * 2;
char *new_arr = malloc(new_size);
if (new_arr == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
if (is_left_expansion)
{
memcpy(new_arr + *size, *arr, *size);
free(*arr);
}
else
{
memcpy(new_arr, *arr, *size);
free(*arr);
}
*arr = new_arr;
*size = new_size;
}
void generate_c_code(FILE *output_file, char *brainfuck_code)
{
fprintf(output_file, "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n");
fprintf(output_file, "void expand_memory(char **arr, size_t *size, int is_left_expansion) {size_t new_size = *size * 2; char *new_arr = malloc(new_size); if (new_arr == NULL) {perror(\"malloc\"); exit(EXIT_FAILURE);} if (is_left_expansion) {memset(new_arr, 0, new_size); memcpy(new_arr + *size, *arr, *size);} else {memset(new_arr, 0, new_size); memcpy(new_arr, *arr, *size);} free(*arr); *arr = new_arr; *size = new_size;}\n\n");
fprintf(output_file, "int main() {\n");
fprintf(output_file, " size_t a_size = %zu;\n\n", a_size);
fprintf(output_file, " char *array = malloc(a_size);\n");
fprintf(output_file, " if (array == NULL) {\n");
fprintf(output_file, " perror(\"malloc\");\n");
fprintf(output_file, " return EXIT_FAILURE;\n");
fprintf(output_file, " }\n");
fprintf(output_file, " memset(array, 0, a_size);\n");
fprintf(output_file, " char *ptr = array + a_size / 2;\n\n");
fprintf(output_file, " int temp = 0;\n\n");
Stack stack;
init_stack(&stack);
char *pc = brainfuck_code;
size_t loop_counter = 0;
while (*pc)
{
switch (*pc)
{
case '<':
fprintf(output_file, " if (ptr == array){expand_memory(&array, &a_size, 1); ptr = array + a_size / 2;}\n");
fprintf(output_file, " --ptr;\n");
break;
case '>':
fprintf(output_file, " if (ptr >= array + a_size - 1){expand_memory(&array, &a_size, 0); ptr = array + a_size / 2 - 1;}\n");
fprintf(output_file, " ++ptr;\n");
break;
case '+':
fprintf(output_file, " ++*ptr;\n");
break;
case '-':
fprintf(output_file, " --*ptr;\n");
break;
case '.':
fprintf(output_file, " putchar(*ptr);\n");
fprintf(output_file, " fflush(stdout);\n");
break;
case ',':
fprintf(output_file, " temp = getchar();\n");
fprintf(output_file, " if (temp == EOF)\n");
fprintf(output_file, " *ptr = -1;\n");
fprintf(output_file, " else\n");
fprintf(output_file, " *ptr = (char)temp;\n");
break;
case '[':
fprintf(output_file, " while (*ptr) {\n");
push(&stack, loop_counter);
loop_counter++;
break;
case ']':
if (is_empty(&stack))
{
fprintf(stderr, "UNBALANCED BRACKETS\n");
exit(EXIT_FAILURE);
}
loop_counter=pop(&stack);
fprintf(output_file, " }\n");
break;
}
pc++;
}
if (!is_empty(&stack))
{
fprintf(stderr, "UNBALANCED BRACKETS\n");
exit(EXIT_FAILURE);
}
fprintf(output_file, " free(array);\n");
fprintf(output_file, " return 0;\n");
fprintf(output_file, "}\n");
free_stack(&stack);
}
int main(int argc, char *argv[])
{
FILE *z;
if (argc < 2)
{
f_size = 65536;
f = malloc(f_size);
if (f == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
s = f;
while ((b = getchar()) != EOF)
{
if (s - f >= f_size - 1)
{
size_t offset = s - f;
expand_memory(&f, &f_size, 0);
s = f + offset; // Adjust pointer after realloc
}
*s++ = b;
}
*s = 0;
generate_c_code(stdout, f);
free(f);
}
for (int i = 1; i < argc; i++)
{
f_size = 65536;
f = malloc(f_size);
if (f == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
s = f;
if ((z = fopen(argv[i], "r")))
{
while ((b = getc(z)) != EOF)
{
if (s - f >= f_size - 1)
{
size_t offset = s - f;
expand_memory(&f, &f_size, 0);
s = f + offset; // Adjust pointer after realloc
}
*s++ = b;
}
*s = 0;
fclose(z);
// Generate C code and compile using cc from stdin
char cc_command[27 + strlen(argv[i])];
snprintf(cc_command, sizeof(cc_command), "cc -x c -o \"%s.exe\" -O2 -s -", argv[i]);
FILE *cc = popen(cc_command, "w");
if (cc == NULL)
{
perror("popen");
free(f);
return EXIT_FAILURE;
}
generate_c_code(cc, f);
pclose(cc);
}
else
{
perror("fopen");
free(f);
return 1;
}
free(f);
}
return 0;
}