-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbfck.c
275 lines (227 loc) · 5.67 KB
/
bfck.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* The MIT License
*
* Copyright (c) 2010 Georgios Migdos <cyberpython@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#define PROGRAM_NAME "Bfck"
#define VERSION_NUMBER "0.1"
#define PROGRAM_INFO_MSG "A simple Brainf*ck interpreter witten in C.\nDistributed under the terms of the MIT license.\nCopyright © 2010 Georgios Migdos.\n\nUsage:\n\tbfck <input_file>\n"
#define DATA_ARRAY_SIZE 30720 //30KB
#define EXECUTION_FINISHED_MSG "\n\n--Execution finished.--\n"
#define FILE_OPEN_FAILED_ERROR_MSG "Error opening file\n"
#define UNMATCHED_BRACKET_ERROR_MSG "Unmatched bracket.\n"
char data[DATA_ARRAY_SIZE];
int dataIndex = 0;
int maxDataIndex = DATA_ARRAY_SIZE-1;
FILE *pSourceCodeFile= NULL;
void error(const char *errorMsg){
fputs(errorMsg, stderr);
}
void errorC(int c){
printf("%d\n", c);
fputc(c, stderr);
}
void fatalError(const char *errorMsg){
fputs(errorMsg, stderr);
exit(EXIT_FAILURE);
}
void msg(const char *message){
fputs(message, stdout);
}
void msgC(int c){
fputc(c, stdout);
}
void printProgramInfo(){
fprintf(stdout, "%s version %s\n%s", PROGRAM_NAME, VERSION_NUMBER, PROGRAM_INFO_MSG);
}
char readC(){
char c = fgetc(stdin);
__fpurge(stdin); // Use of non-standard C function fpurge
return c;
}
void initData(){
int i=0;
for(i=0; i<DATA_ARRAY_SIZE; i++){
data[i] = 0;
}
}
int isValidCmd(int cmd){
if ( (cmd == '>') || (cmd=='<') || (cmd=='+') || (cmd=='-') || (cmd=='.') || (cmd==',') || (cmd=='[') || (cmd==']') ){
return 0;
}
return 1;
}
int getNextCommand(){
if(pSourceCodeFile == NULL){
return EOF;
}
char buf[1];
fread( buf, sizeof(char), 1, pSourceCodeFile);
int cmd = buf[0];
while ((isValidCmd(cmd) != 0) &&(!feof(pSourceCodeFile)) ){
fread( buf, sizeof(char), 1, pSourceCodeFile);
cmd = buf[0];
}
return cmd;
}
int incrementDataPointer(){
if(dataIndex<maxDataIndex){
dataIndex++;
return 0;
}
return 1;
}
int decrementDataPointer(){
if(dataIndex>0){
dataIndex--;
return 0;
}
return 1;
}
int incrementData(){
data[dataIndex] ++;
return 0;
}
int decrementData(){
data[dataIndex] --;
return 0;
}
int writeByte(){
msgC(data[dataIndex]);
return 0;
}
int readByte(){
data[dataIndex] = readC();
return 0;
}
long int findClosingBracket(){
long int currentPos = ftell(pSourceCodeFile);
long int openBrackets = 1;
long int result = -1;
char buf[1];
int cmd = 0;
while(!feof(pSourceCodeFile) && (openBrackets>0)){
fread( buf, sizeof(char), 1, pSourceCodeFile);
cmd = buf[0];
if(cmd==']'){
openBrackets--;
}else if(cmd=='['){
openBrackets++;
}
}
if(openBrackets == 0){
result = ftell(pSourceCodeFile);
}
fseek(pSourceCodeFile, currentPos, SEEK_SET);
return result;
}
long int findOpeningBracket(){
long int currentPos = ftell(pSourceCodeFile);
long int closedBrackets = 1;
long int result = -1;
char buf[1];
int cmd = 0;
while(!(ftell(pSourceCodeFile)<=0) && (closedBrackets>0)){
fseek(pSourceCodeFile, -2, SEEK_CUR);
fread( buf, sizeof(char), 1, pSourceCodeFile);
cmd = buf[0];
if(cmd=='['){
closedBrackets--;
}else if(cmd==']'){
closedBrackets++;
}
}
if(closedBrackets == 0){
result = ftell(pSourceCodeFile);
}
fseek(pSourceCodeFile, currentPos, SEEK_SET);
return result;
}
int jumpZero(){
long int matchingBracketPosition = findClosingBracket();
if(matchingBracketPosition == -1){
fatalError(UNMATCHED_BRACKET_ERROR_MSG);
return 1;
}else{
if(data[dataIndex] == 0){
fseek(pSourceCodeFile, matchingBracketPosition, SEEK_SET);
}
}
return 0;
}
int jumpBackNonZero(){
long int matchingBracketPosition = findOpeningBracket();
if(matchingBracketPosition == -1){
fatalError(UNMATCHED_BRACKET_ERROR_MSG);
return 1;
}else{
if(data[dataIndex] != 0){
fseek(pSourceCodeFile, matchingBracketPosition, SEEK_SET);
}
}
return 0;
}
int execCommand(int cmd){
if(isValidCmd(cmd) == 0){
if(cmd == '>'){
return incrementDataPointer();
}else if (cmd == '<'){
return decrementDataPointer();
}else if (cmd == '+'){
return incrementData();
}else if (cmd == '-'){
return decrementData();
}else if (cmd == '.'){
return writeByte();
}else if (cmd == ','){
return readByte();
}else if (cmd == '['){
return jumpZero();
}else if (cmd == ']'){
return jumpBackNonZero();
}
}
return 0;
}
int main(int argc, char *argv[]){
if(argc <= 1){
printProgramInfo();
return 0;
}
initData();
dataIndex = 0;
pSourceCodeFile = fopen(argv[1], "rb");
int cmd = 0;
if (pSourceCodeFile==NULL){
fatalError(FILE_OPEN_FAILED_ERROR_MSG);
}
while(!feof(pSourceCodeFile)){
cmd = getNextCommand();
if(cmd!=EOF){
execCommand(cmd);
}
}
fclose(pSourceCodeFile);
msg(EXECUTION_FINISHED_MSG);
return 0;
}