/
bf.c
199 lines (178 loc) · 4.57 KB
/
bf.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
// vim: ts=4:sw=4
//[{ }
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEBUG 0
#define FLUSH_ON_WRITE 0
#define MAX_FILE_SIZE 1000000 // 1 MB
#define MEM_SIZE 100000 // 100 KB
#define MAX_LOOPS 100000
static char* array;
static char* ptr;
struct loop {
unsigned begin;
unsigned end;
};
static struct loop* loops;
static unsigned numLoops = 0;
void printHelp(const char* fn) {
fprintf(stderr, "usage: %s <input files>\n", fn);
return;
}
void printLoops(void) {
register unsigned i;
puts("Loops:");
for(i = 0; i < numLoops; ++i) {
struct loop l = loops[i];
printf("\t%u b:%u e:%u\n", i, l.begin, l.end);
}
return;
}
void jumpForward(unsigned* pos) {
register unsigned i;
for(i = 0; i < numLoops; ++i) {
if(loops[i].begin == *pos) {
*pos = loops[i].end;
return;
}
}
}
void jumpBackward(unsigned* pos) {
register unsigned i;
for(i = 0; i < numLoops; ++i) {
if(loops[i].end == *pos) {
*pos = loops[i].begin;
return;
}
}
}
int performOperation(const char* buffer, unsigned* pos) {
switch(buffer[*pos]) {
case '>':
ptr++;
break;
case '<':
ptr--;
break;
case '+':
++*ptr;
break;
case '-':
--*ptr;
break;
case '.':
putchar(*ptr);
if(FLUSH_ON_WRITE)
fflush(stdout);
break;
case ',':
*ptr = getchar();
break;
case '[':
if(*ptr != 0) break;
jumpForward(pos);
break;
case ']':
if(*ptr == 0) break;
jumpBackward(pos);
break;
default:
// fprintf(stderr, "Unrecognized token %c at pos %d\n", buffer[*pos], *pos);
// return 1;
break;
}
return 0;
}
int interpret(const char* filename) {
FILE* file;
file = fopen(filename, "r");
if(!file) {
fprintf(stderr, "failed to read file: %s\n", filename);
return 1;
}
// reset data
memset(array, 0, MEM_SIZE * sizeof(char));
memset(loops, 0, MAX_LOOPS * sizeof(struct loop));
ptr = array;
char* buffer = (char*) malloc(sizeof(char) * MAX_FILE_SIZE);
// read file into memory, skipping all unknown tokens
char c;
unsigned initpos = 0;
int currentLoop = -1; // start counting at 0
int builtLoops = 0;
while((c = getc(file)) != EOF) {
switch(c) {
// create loop jump lookup array
case '[':
currentLoop++;
loops[currentLoop].begin = initpos;
loops[currentLoop].end = MAX_FILE_SIZE;
goto set;
case ']':
for(signed i = currentLoop; i >= 0; --i) {
// set end point for last unset loop
if(loops[i].end == MAX_FILE_SIZE) {
loops[i].end = initpos;
break;
}
}
builtLoops++;
case '>':
case '<':
case '+':
case '-':
case '.':
case ',':
set:
buffer[initpos++] = c;
break;
default:
// fprintf(stderr, "Unrecognized token %c at pos %d\n", buffer[*pos], *pos);
// return 1;
break;
}
}
if(currentLoop - builtLoops != -1) {
puts("Unbalanced loops detected");
free(buffer);
return 1;
}
numLoops = builtLoops;
if(DEBUG) {
printLoops();
buffer[initpos + 1] = 0; // so we can treat it is a string
puts(buffer);
}
// interpret
unsigned pos = 0;
while(pos < initpos) {
// printf("%d %c\n", pos, buffer[pos]);
if(performOperation(buffer, &pos)) {
break;
}
++pos;
}
free(buffer);
fclose(file);
return 0;
}
int main(int argc, char** argv) {
if(argc == 1) {
printHelp(argv[0]);
return 1;
}
array = (char*) malloc(sizeof(char) * MEM_SIZE);
loops = (struct loop*) malloc(sizeof(struct loop) * MAX_LOOPS);
register int i;
for(i = 1; i < argc; ++i) {
if(interpret(argv[i]))
return 1;
}
return 0;
}
/* ]
* +[----->+++<]>+++.---------.++++++++++++.--------.[--->+<]>-
* ---.+[---->+<]>+++.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.+
* [---->+<]>+++.-[--->++<]>--.---.+++++++++++++.-------------.
*/