-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbf2c.c
71 lines (62 loc) · 1.72 KB
/
bf2c.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
/* A simple C program to generate C code from a Brainfuck program.
Uses a straightforward correspondence between Brainfuck operations and C
language constructs, except that consecutive identical operations are
collapsed to reduce the output size and required compilation time.
Brainfuck implementation properties:
- tape consists of 65536 characters (usually 8-bit bytes)
- tape head starts at the left end of the tape
- when reading fails (at end of file) the destination cell is unchanged
Maks Verver <maksverver@geocities.com> revised May 2010 */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
static int indent = 0;
void print(char *format, ...)
{
int i = indent;
va_list ap;
while (i--) putchar('\t');
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
putchar('\n');
}
int main()
{
int c, d, n;
print("#include <stdio.h>\n");
print("static char t[65536], *p = t;\n");
print("static void in(void) {");
++indent;
print("int c = getchar();");
print("if (c != EOF) *p = (char)c;");
--indent;
print("}\n");
print("static void out(void) {");
++indent;
print("putchar(*p);");
print("fflush(stdout);");
--indent;
print("}\n");
print("int main() {");
++indent;
for (c = getchar(); c != EOF; c = d)
{
n = 1;
while ((d = getchar()) == c) ++n;
switch (c) {
case '>': print("p+=%d;", n); break;
case '<': print("p-=%d;", n); break;
case '+': print("*p+=%d;", n); break;
case '-': print("*p-=%d;", n); break;
case ',': while (n--) print("in();"); break;
case '.': while (n--) print("out();"); break;
case '[': while (n--) print("while(*p) {"), ++indent; break;
case ']': while (n--) --indent, print("}"); break;
}
}
print("return 0;");
--indent;
print("}");
return 0;
}