-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfmt.c
executable file
·208 lines (182 loc) · 4.42 KB
/
bfmt.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
//bfmt version 1.0
//todo
//do convert code
//do interactive bfmt
//make a better parser
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
char *convert(char *code, char *lang) //Convert pseudocode
{
if(strcmp(lang, "c") == 0)
printf("c");
if(strcmp(lang, "python") == 0)
printf("python");
return "lol";
}
int isBFChar(char c) //Determines if the character is a BF character
{
if(c == '+' || c == '-' || c == '<' || c == '>' || c == '[' || c == ']' || c == '.' || c == ',')
{
return 1;
}
return 0;
}
int fileCount(char *fileName) //Counts the number of characters in a file
{
FILE *file;
file = fopen(fileName, "r");
int bfCount = 0;
char temp;
while((temp=fgetc(file)) != EOF)
if(isBFChar(temp))
bfCount++;
fclose(file);
return bfCount;
}
char *readFile(char *fileName) //Reads all BF characters into a code string
{
FILE *file;
file = fopen(fileName, "r");
int fileLength = fileCount(fileName);
char *code = malloc(fileLength * sizeof(char));
char *orig = code;
char c;
file = fopen(fileName, "r");
while((c = (char)fgetc(file)) != EOF)
{
if(isBFChar(c))
{
*code = c;
code++;
}
}
*code = '\0';
printf("[CODE]:\n");
char *temp = orig;
int i;
for(i = 0; i < fileLength; i++, temp++)
{
printf("%c", *temp);
}
printf("\n");
fclose(file);
return orig;
}
int writeFile(char *oFileName, char *output) //Writes only the BF code to a file
{
FILE *ofile;
int outputLength = strlen(output);
ofile = fopen(oFileName, "w");
int i;
for(i = 0; i < outputLength; i++, output++)
{
fputc(*output, ofile);
}
fclose(ofile);
return 0;
}
int interpret(char *input) //Interprets the input
{
printf("[INTERPRETING]:\n");
unsigned char loopDepth = 0;
unsigned char *cells = malloc(30000 * sizeof(char));
while(*input != EOF)
{
switch(*input)
{
case '>': //Move cell right
++cells;
break;
case '<': //Move cell left
--cells;
break;
case '+': //Increase current cell by 1
++*cells;
break;
case '-': //Decrease current cell by 1
--*cells;
break;
case '[': //Starting loop
loopDepth = 1;
if(*cells == 0)
{
do
{
*input++;
if(*input == '[') loopDepth++;
if(*input == ']') loopDepth--;
} while(loopDepth > 0);
}
break;
case ']': //Ending loop
loopDepth = 1;
if(*cells != 0)
{
do
{
*input--;
if(*input == '[') loopDepth--;
if(*input == ']') loopDepth++;
} while(loopDepth > 0);
}
break;
case '.': //Print current cell
putchar(*cells);
break;
case ',':
*cells = getchar();
break;
}
*input++;
}
return 0;
}
int main(int argc, char *argv[]) // Main duh
{
printf("bfmt v1.0\n");
char *input = NULL;
char *iFileName = NULL;
char *oFileName = NULL;
int de = 0;
char c;
while((c = getopt(argc, argv, "qiodn:")) != -1)
{
switch (c)
{
case 'n': //An output file with the code only
oFileName = optarg;
break;
case 'd': //Toggles debug mode
de = 1;
break;
case 'o': //An output file for the BF output
break;
case 'i': //A input file containing all input asked for the program
break;
case 'q': //Places optimization in an output file
break;
}
}
if(argc > 1)
{
iFileName = argv[optind];
if((input = readFile(iFileName)) == NULL) return 0;
interpret(input);
if(oFileName != NULL) writeFile(oFileName, input);
}
else
{
iFileName = "bfcode.txt";
if((input = readFile(iFileName)) == NULL) return 0;
interpret(input);
if(oFileName != NULL) writeFile(oFileName, input);
}
int i;
scanf("%d",&i);
free(input);
return 0;
}