-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf.c
330 lines (298 loc) · 8.72 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifndef VERSION
# define VERSION
# define MAIN_VERSION 0
# define SUB_VERSION 0
# define LAST_VERSION 1
#endif
typedef enum node_type {
HEAD=1,
POINTER_ADD,
POINTER_SUB,
BYTE_ADD,
BYTE_SUB,
OUTPUT,
INPUT,
START_LOOP,
END_LOOP
} node_type;
typedef struct node {
struct node* next;
node_type type;
} node;
typedef struct proc_state {
node* c_node; // current node
node* head_node; // head node
node** stack; // loop stack
char* data; // memory
size_t stack_top; // stack top
size_t pointer; // currently data pointer
size_t ssize; // stack size, 200 by default
size_t dsize; // memory size, 200 by default
} proc_state;
node*
new_node(node* nodep, node_type type)
{
nodep->next = (node*)malloc(sizeof(node));
nodep = nodep->next;
nodep->next = NULL;
nodep->type = type;
return nodep;
}
# define NEXT_NODE(ps) ps->c_node = ps->c_node->next;
# define DEFAULT_SIZE 200
void
init_proc_state(proc_state* p)
{
p->c_node = (node*)malloc(sizeof(node));
p->c_node->type = HEAD;
p->c_node->next = NULL;
p->head_node = p->c_node;
p->stack = (node**)malloc(sizeof(node*)*DEFAULT_SIZE);
p->data = (char*)malloc(sizeof(char)*DEFAULT_SIZE);
p->dsize = DEFAULT_SIZE;
p->ssize = DEFAULT_SIZE;
p->pointer = 0;
p->stack_top = 0;
// Init data
for (int i=0;i<p->dsize;i++) {
p->data[i] = 0;
}
}
/* **DEBUG** Show Node List*/
void
print_nodes(proc_state* p)
{
node* nodep = p->head_node;
while (nodep) {
printf("type: %d ", nodep->type);
switch (nodep->type) {
case HEAD:
printf("HEAD\n");
break;
case POINTER_ADD:
printf("POINTER_ADD\n");
break;
case POINTER_SUB:
printf("POINTER_SUB\n");
break;
case BYTE_ADD:
printf("BYTE_ADD\n");
break;
case BYTE_SUB:
printf("BYTE_SUB\n");
break;
case OUTPUT:
printf("OUTPUT\n");
break;
case INPUT:
printf("INPUT\n");
break;
case START_LOOP:
printf("START_LOOP\n");
break;
case END_LOOP:
printf("END_LOOP\n");
break;
default:
fprintf(stderr, "unexceped option\n");
}
nodep = nodep->next;
}
}
/* Run Programme */
void
run_programme(proc_state* ps)
{
while (ps->c_node) {
switch (ps->c_node->type) {
case HEAD:
/* NULL */
NEXT_NODE(ps);
break;
case POINTER_ADD:
if (ps->pointer >= ps->dsize - 1) {
fprintf(stderr, "Data OverFlow\n");
goto EXIT;
}
// unclear data
//ps->data[++ps->pointer] = 0;
++ps->pointer;
NEXT_NODE(ps);
break;
case POINTER_SUB:
if (ps->pointer <= 0) {
fprintf(stderr, "Data UnderFlow\n");
goto EXIT;
}
--ps->pointer;
NEXT_NODE(ps);
break;
case BYTE_ADD:
++ps->data[ps->pointer];
NEXT_NODE(ps);
break;
case BYTE_SUB:
--ps->data[ps->pointer];
NEXT_NODE(ps);
break;
case OUTPUT:
putc(ps->data[ps->pointer], stdout);
NEXT_NODE(ps);
break;
case INPUT:
ps->data[ps->pointer] = getc(stdin);
NEXT_NODE(ps);
break;
case START_LOOP:
if (ps->data[ps->pointer] != 0) {
/* enable loop */
if (ps->stack_top >= ps->ssize) {
fprintf(stderr, "Stack OverFlow\n");
goto EXIT;
}
ps->stack[ps->stack_top++] = ps->c_node;
} else {
/* end loop */
while (ps->c_node->type != END_LOOP) {
NEXT_NODE(ps);
if (!ps->c_node) {
fprintf(stderr, "Unexcepted Loop\n");
goto EXIT;
}
}
}
NEXT_NODE(ps);
break;
case END_LOOP:
if (ps->stack_top<=0) {
fprintf(stderr, "Stack UnderFlow\n");
goto EXIT;
}
if (ps->data[ps->pointer] != 0) {
/* loop continue*/
ps->c_node = ps->stack[ps->stack_top-1]->next;
} else {
/* stop loop*/
ps->stack[--ps->stack_top] = NULL;
NEXT_NODE(ps);
}
break;
default:
fprintf(stderr, "unexceped option\n");
goto EXIT;
}
}
EXIT:
return;
}
int
parse_from_file(proc_state* proc, const char* filename) {
FILE* fp;
fp = fopen(filename, "r");
if (fp==NULL) {
fprintf(stderr, "File does not exist.\n");
return 1;
}
// parse code
node* nodep = proc->c_node;
for (;;) {
char c = fgetc(fp);
if (feof(fp)) {
break;
}
switch (c) {
case '>': nodep = new_node(nodep, POINTER_ADD);break;
case '<': nodep = new_node(nodep, POINTER_SUB);break;
case '+': nodep = new_node(nodep, BYTE_ADD);break;
case '-': nodep = new_node(nodep, BYTE_SUB);break;
case '.': nodep = new_node(nodep, OUTPUT);break;
case ',': nodep = new_node(nodep, INPUT);break;
case '[': nodep = new_node(nodep, START_LOOP);break;
case ']': nodep = new_node(nodep, END_LOOP);break;
default: continue;
}
}
fclose(fp);
return 0;
}
int
parse_from_str(proc_state* proc, const char* string) {
size_t length = strlen(string);
// parse code
node* nodep = proc->c_node;
for (int i=0;i<length;i++) {
char c = string[i];
switch (c) {
case '>': nodep = new_node(nodep, POINTER_ADD);break;
case '<': nodep = new_node(nodep, POINTER_SUB);break;
case '+': nodep = new_node(nodep, BYTE_ADD);break;
case '-': nodep = new_node(nodep, BYTE_SUB);break;
case '.': nodep = new_node(nodep, OUTPUT);break;
case ',': nodep = new_node(nodep, INPUT);break;
case '[': nodep = new_node(nodep, START_LOOP);break;
case ']': nodep = new_node(nodep, END_LOOP);break;
default: continue;
}
}
return 0;
}
void
usage()
{
fprintf(stderr, "Usage: bf [option] | file\n");
fprintf(stderr, "A Sample BrainFuck interpreter.\n");
fprintf(stderr, "\n");
fprintf(stderr, "-h --help show usage message .\n");
fprintf(stderr, "-e --exec execute BrainFuck code .\n");
fprintf(stderr, " --version output version information and exit . \n");
}
int
main(int argc, const char** argv)
{
proc_state global_proc;
init_proc_state(&global_proc);
if (argc < 2) {
usage();
return 1;
}
else {
int index = 1;
while (index<argc) {
if (strlen(argv[index])>1 && argv[index][0]=='-') {
if (strcmp(argv[index], "-h")==0 || strcmp(argv[index], "--help")==0) {
usage();
goto EXIT;
}
else if (strcmp(argv[index], "-e")==0 || strcmp(argv[index], "--exec")==0) {
parse_from_str(&global_proc, argv[++index]);
}
else if (strcmp(argv[index], "--version")==0) {
printf("bf version %d.%d.%d.\n", MAIN_VERSION, SUB_VERSION, LAST_VERSION);
goto EXIT;
}
else {
fprintf(stderr, "unexcepted options\n");
usage();
return 1;
}
} else {
const char* filename = argv[1];
if (parse_from_file(&global_proc, filename)!=0) {
goto EXIT;
}
goto RUN_MODE;
}
index += 1;
}
}
RUN_MODE:
// Run Programme
run_programme(&global_proc);
EXIT:
return 0;
ERROR:
return 1;
}