-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
155 lines (140 loc) · 3.66 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INDENT ( " " )
static int g_indent_level = 0;
static
void print_indent() {
int i;
for (i = 0; i <= g_indent_level; i++) printf(INDENT);
}
static
void print_code(const char *code) {
print_indent();
puts(code);
}
static
void emit_ptr_arith(int n, int op) {
char prefix = '*';
print_indent();
if (op >= '<') {
prefix = ' ';
op = '-' - (op - '<');
printf("CHK");
}
if (n == 1) printf("(%c%c(%cptr));", op, op, prefix);
else printf("((%cptr)%c=%d);", prefix, op, n);
putchar('\n');
}
static
int get_index_of(const char *s, int c) {
char *p = strchr(s, (char)c);
return p ? (int)(p - s) : -1;
}
static
void emit_code(void) {
int c, n, op, id, commented = 0;
c = getchar();
while (c != EOF) {
id = get_index_of("[]+-<>.,", c);
if (commented && id != -1) {
printf(" */\n");
commented = 0;
}
if (id == 0) { /* '[' */
print_code("for(;*ptr;){");
++g_indent_level;
c = getchar();
}
else if (id == 1) { /* ']' */
if (--g_indent_level < 0) {
fprintf(stderr, "Error: Extra ']' found.\n");
return;
}
print_code("}");
c = getchar();
}
else if (2 <= id && id <= 5) { /* '+', '-', '<', '>' */
op = c;
n = 0;
for (;c == op; n++) c = getchar();
emit_ptr_arith(n, op);
}
else if (id == 6) { /* '.' */
print_code("putchar(*ptr);");
c = getchar();
}
else if (id == 7) { /* ',' */
print_code("*ptr=getchar();");
c = getchar();
}
else { /* others */
if (c != '\r' && c != '\n') {
if (!commented) {
printf("/* ");
commented = 1;
}
putchar(c);
}
c = getchar();
}
}
}
static
void make_out_filename(char *name, const char *in_filename) {
char *p;
strcpy(name, in_filename);
if ((p = strrchr(name, '.')) != NULL) *p = '\0';
strcat(name, ".c");
}
int main(int argc, char *argv[]) {
char out_filename[256];
if (argc >= 2) {
make_out_filename(out_filename, argv[1]);
freopen(argv[1], "r", stdin);
freopen(out_filename, "w", stdout);
}
puts("#include <stdio.h>");
puts("#include <stdlib.h>");
puts("#include <string.h>\n");
puts("typedef unsigned char byte;");
puts("static byte *ptr_start, *ptr_end, *ptr;\n");
puts("#define MEM_SIZE ( 32768 )");
puts("#ifdef NOCHECK");
puts("#define CHK(p) ( p )");
puts("#else");
puts("#define CHK(p) ( chk(p) )");
puts("static void chk(byte *p){");
puts(" if(p<ptr_start||ptr_end<=p){");
puts(" fprintf(stderr,\"Error: Memory Out Of Bounds.\\n\");");
puts(" exit(1);");
puts(" }");
puts("}");
puts("#endif\n");
puts("int main(int argc, char *argv[]){");
puts(" int i=1,mem_size=MEM_SIZE;");
puts(" for (; i < argc;){");
puts(" if (strcmp(argv[i], \"-m\") == 0){");
puts(" i++;");
puts(" if (i < argc) mem_size=atoi(argv[i++]);");
puts(" } else i++;");
puts(" }");
puts(" if (mem_size<1) mem_size=1;");
puts(" ptr_start=(byte *)calloc(mem_size, 1);");
puts(" ptr_end=ptr_start+mem_size;");
puts(" ptr=ptr_start;");
puts("/* -- translated code -- */");
emit_code();
while (g_indent_level > 0) {
print_code("break;");
--g_indent_level;
print_code("}");
fprintf(stderr, "Warning: Incorrect Nesting.\n");
}
puts("/* -- end -- */");
puts(" putchar('\\n');");
puts(" free(ptr_start);");
puts(" return 0;");
puts("}\n");
return 0;
}