-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf2c.c
209 lines (180 loc) · 5.04 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
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
/*
* Simple brainfuck2c converter on pure C
* Coded by Ehsonjon (a.k.a iCoder)
* DATE: 23.07.2020 (18:03) Tajikistan Asia Time =D
* (C) 2020-2021 All rights reversed!
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define STRING_SIZE 1024
typedef struct CommandOptions
{
bool doCompile;
bool deleteSource;
char * fileName;
char outFileName[STRING_SIZE];
char outFileC[STRING_SIZE];
} CommandOptions;
bool parse_command_line(CommandOptions * options, int argc, char * argv[])
{
//help message
if (argc == 1) {
fprintf(stderr, "\n");
fprintf(stderr, "How to use : %s filename [-o output|-c|-d|-r]\n",argv[0]);
fprintf(stderr, "-o output : Set output file name\n-c : Do not compile\n-d : Do not delete C source file\n");
fprintf(stderr, "[INFO] : You SHOULD type 'export MALLOC_CHECK_=0' manually to remove warning.\n");
fprintf(stderr, "\n");
return false;
}
// set the default options
options->doCompile = true;
options->deleteSource = true;
options->fileName = argv[1];
strncpy(options->outFileName, options->fileName, STRING_SIZE);
strncat(options->outFileName, ".o", STRING_SIZE);
// parse the remaining options
int i;
bool isSetOut = false;
for (i = 0; i < argc; i++)
{
if (isSetOut){
isSetOut = false;
strncpy(options->outFileName, argv[i], STRING_SIZE);
}
else if (strcmp(argv[i],"-c") == 0) {
options->doCompile = false;
}
else if (strcmp(argv[i],"-d") == 0) {
options->deleteSource = false;
}
else if (strcmp(argv[i],"-o") == 0) {
isSetOut = true;
}
}
strncpy(options->outFileC,options->outFileName,STRING_SIZE);
strncat(options->outFileC,".c", STRING_SIZE);
// don't delete the source if we won't compile it
if(!options->doCompile)
{
options->deleteSource = false;
}
return true;
}
void write_header(FILE * cFile)
{
fputs("#include <stdio.h>\n", cFile);
fputs("#include <stdlib.h>\n", cFile);
fputs("int main(){\n", cFile);
fputs("unsigned char* _=(unsigned char*)malloc(32*1024);/*32kB*/if(_==0){printf(\"MEMORY ERROR!\\n\");return 1;}\n",cFile);
}
void write_footer(FILE * cFile)
{
fputs("free(_);\nreturn 0;\n}\n", cFile);
}
int bf_fgetc(FILE * bfFile)
{
int c;
do {
c = fgetc(bfFile);
} while(c != EOF && c != '[' && c != ']' && c != '<' && c != '>' && c != '.'
&& c != ',' && c != '+' && c != '-');
return c;
}
void compile_to_c(FILE * bfFile, FILE * cFile)
{
write_header(cFile);
int add = 0;
char prevC = '\0';
//write codes
char c = bf_fgetc(bfFile);
do {
int movement_counter = 0;
while ( c == '>' || c == '<')
{
movement_counter += c == '>' ? 1 : -1;
c = bf_fgetc(bfFile);
}
if (movement_counter)
{
fprintf(cFile,"_ += %d;", movement_counter);
}
int value_counter = 0;
while ( c == '+' || c == '-')
{
value_counter += c == '+' ? 1 : -1;
c = bf_fgetc(bfFile);
}
if (value_counter)
{
fprintf(cFile,"*_ += %d;",value_counter);
}
if (c == '.')
{
fprintf(cFile, "putchar(*_);\n");
c = bf_fgetc(bfFile);
}
if (c == ',')
{
fprintf(cFile, "*_ = getchar();\n");
c = bf_fgetc(bfFile);
}
if (c == '[')
{
fprintf(cFile, "while(*_) {\n");
c = bf_fgetc(bfFile);
}
if (c == ']')
{
fprintf(cFile, "}\n");
c = bf_fgetc(bfFile);
}
} while(c!=EOF);
write_footer(cFile);
}
void compileCode(CommandOptions * options)
{
printf("Compile with GCC...\n");
char op[2048] = "gcc ";
strncat(op,options->outFileC, 2048);
strncat(op," -o ", 2048);
strncat(op,options->outFileName, 2048);
system(op);
}
int main(int argc, char* argv[]){
CommandOptions options;
if( !parse_command_line(&options, argc, argv) )
{
return 1;
}
printf("[INFO] : You may type 'export MALLOC_CHECK_=0' manually to remove warning.\n");
//bf file
FILE* bfFile = fopen(options.fileName,"r");
if(bfFile==NULL){
fprintf(stderr, "[ERROR] : FILE %s DOES NOT EXIST\n",options.fileName);
return 2;
}
//c source code
FILE* cFile = fopen(options.outFileC, "w");
if(!cFile)
{
fclose(bfFile);
fprintf(stderr, "[ERROR]: COULD NOT OPEN %s FILE FOR WRITING" , options.outFileC);
return 3;
}
compile_to_c(bfFile, cFile);
fclose(bfFile);
fclose(cFile);
if(options.doCompile){
compileCode(&options);
}else{
printf("Output C code : %s\n", options.outFileC);
}
if( options.deleteSource )
{
unlink(options.outFileC);
}
printf("Done.\nOutput file name : %s\n",options.outFileName);
return 0;
}