-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcfc.c
256 lines (178 loc) · 4.91 KB
/
bbcfc.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
#include <stdio.h>
// Type defs
typedef char* string;
// Global variables
// Previous character and the value count, for optimisations
int prevChar = 0;
int prevValue = 0;
char prevSpecial = ' ';
int tabDepth = 0;
void printPrefix() {
printf("#include <stdio.h>");
printf("\n\nchar array[1024];");
printf("\nchar *i;");
printf("\n\nvoid decPointer() {");
printf("\n\t// <{1}");
printf("\n\ti--;");
printf("\n}");
printf("\n\nvoid subPointer(int a) {");
printf("\n\t// <{a}");
printf("\n\ti -= a;");
printf("\n}");
printf("\n\nvoid incPointer() {");
printf("\n\t// >{1}");
printf("\n\ti++;");
printf("\n}");
printf("\n\nvoid addPointer(int a) {");
printf("\n\t// >{a}");
printf("\n\ti += a;");
printf("\n}");
printf("\n\nvoid decValue() {");
printf("\n\t// -{1}");
printf("\n\t*i -= 1;");
printf("\n}");
printf("\n\nvoid subValue(int a) {");
printf("\n\t// -{a}");
printf("\n\t*i -= a;");
printf("\n}");
printf("\n\nvoid incValue() {");
printf("\n\t// +{1}");
printf("\n\t*i += 1;");
printf("\n}");
printf("\n\nvoid addValue(int a) {");
printf("\n\t// +{a}");
printf("\n\t*i += a;");
printf("\n}");
printf("\n\nvoid output() {");
printf("\n\t// .");
printf("\n\tprintf(\"%%c\", *i);");
printf("\n}");
printf("\n\nvoid input() {");
printf("\n\t// ,");
printf("\n\t*i = getchar();");
printf("\n}");
printf("\n\nint main() {");
printf("\n\t// Initialise pointer");
printf("\n\ti = array;\n\n");
}
// Print extra tabs for while loops
void printTabs() {
if (tabDepth > 0) {
for (int i = 0; i < tabDepth; i++) {
printf("\t");
}
}
}
// Print the finalised output
void printOutput(char inChar, int inValue) {
if (inChar != ' ') {
printTabs();
switch (inChar) {
case '<':
if (inValue > 1) {
printf("\tsubPointer(%i);\n", inValue);
} else {
printf("\tdecPointer();\n");
}
break;
case '>':
if (inValue > 1) {
printf("\taddPointer(%i);\n", inValue);
} else {
printf("\tincPointer();\n");
}
break;
case '-':
if (inValue > 1) {
printf("\tsubValue(%i);\n", inValue);
} else {
printf("\tdecValue();\n");
}
break;
case '+':
if (inValue > 1) {
printf("\taddValue(%i);\n", inValue);
} else {
printf("\tincValue();\n");
}
break;
case '.':
printf("\toutput();\n");
break;
case ',':
printf("\tinput();\n");
break;
case '[':
printf("\twhile (*i != 0) {\n");
tabDepth++;
break;
case ']':
printf("}\n");
tabDepth--;
break;
}
}
}
// Processing for each char
void processChar(char inChar) {
int charType = 0;
int direction = 0;
if (inChar == '<' || inChar == '>') {
charType = 1;
if (inChar == '>') {
direction = 1;
}
} else if (inChar == '-' || inChar == '+') {
charType = 2;
if (inChar == '+') {
direction = 1;
}
}
if (charType == prevChar && charType != 0) {
if (direction == 0) {
prevValue--;
} else {
prevValue++;
}
} else {
if (prevChar == 1) {
if (prevValue > 0) {
printOutput('>', prevValue);
} else if (prevValue < 0) {
printOutput('<', -prevValue);
}
} else if (prevChar == 2) {
if (prevValue > 0) {
printOutput('+', prevValue);
} else if (prevValue < 0) {
printOutput('-', -prevValue);
}
} else {
printOutput(prevSpecial, 1);
}
prevChar = charType;
if (charType == 0) {
prevSpecial = inChar;
}
if (direction == 1) {
prevValue = 1;
} else {
prevValue = -1;
}
}
}
// Main method which receives chars and calls processChar()
int main() {
// Output premade 'prefix'
printPrefix();
// Char to store current received char in
char c;
// Loop to execute for every char received
while ((c = getchar()) != EOF) {
processChar(c);
}
processChar(' ');
printf("\n\treturn 0;");
printf("\n}");
return 0;
}