blob: 2ea57b504734e64ff9e53a01b65059f8e41955e9 (
plain)
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
|
// WARNING: THIS PROGRAM PRODUCES CODE THAT ALLOWS UNRESTRICTED MEMORY ACCESS.
// USE AT YOUR OWN RISK!!!!!!!!!!!!
// This program takes input from stdin and outputs on stdout and stderr (for errors)
#include<stdio.h>
#define rightAngle "++ptr;\n" // right angle bracket
#define leftAngle "--ptr;\n" // left angle bracket
#define plus "++*ptr;\n" // plus
#define minus "--*ptr;\n" // minus
#define period "putchar(*ptr);\n" // period
#define comma "*ptr = getchar();\n" // comma
#define leftSquare "while (*ptr) {\n" // left square bracket
#define rightSquare "}\n" // right square bracket
#define indent " "
void autoIndent(int);
int main( int argc, char *argv[] ) {
int indentLevel = 1;
printf("// Generated with TK's \"fuck\" brainfuck to C parser.\n");
printf("#include<stdio.h>\n");
printf("#include<stdlib.h>\n");
printf("\n");
printf("int main() {\n");
printf(" char *ptrt;\n");
printf(" ptrt = calloc(1000, sizeof(char));\n");
printf(" char *ptr;\n");
printf(" ptr = ptrt;\n");
printf(" // The following is directly parsed\n");
char c;
while ( ( c = getchar() ) != EOF ) {
switch (c) {
case 62:
autoIndent(indentLevel);
printf(rightAngle);
break;
case 60:
autoIndent(indentLevel);
printf(leftAngle);
break;
case 43:
autoIndent(indentLevel);
printf(plus);
break;
case 45:
autoIndent(indentLevel);
printf(minus);
break;
case 46:
autoIndent(indentLevel);
printf(period);
break;
case 44:
autoIndent(indentLevel);
printf(comma);
break;
case 91:
autoIndent(indentLevel);
indentLevel++;
printf(leftSquare);
break;
case 93:
indentLevel--;
autoIndent(indentLevel);
printf(rightSquare);
break;
}
}
printf(" // Closing cleanup\n");
printf(" free(ptrt);\n");
printf(" putchar('\\n');\n");
printf(" return 0;\n}\n");
if (--indentLevel)
fprintf( stderr, "Incorrect number of brackets. - Error parsing file.\n");
return 0;
}
void autoIndent(int levels) {
int i;
for (i = 0; i < levels; i++)
printf(indent);
}
|