-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbf.c
77 lines (72 loc) · 1.23 KB
/
sbf.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
#include <stdio.h>
#define MEMSIZE 30000
#define BF_RIGHT '>'
#define BF_LEFT '<'
#define BF_INC '+'
#define BF_DEC '-'
#define BF_OUT '.'
#define BF_IN ','
#define BF_LOOPS '['
#define BF_LOOPE ']'
FILE *srcfile;
int srclen = 0;
char src[MEMSIZE];
char mem[MEMSIZE];
char *ip = src;
char *dp = mem;
int interpret() {
for (;;) {
switch (*ip) {
case BF_RIGHT:
++dp;
break;
case BF_LEFT:
--dp;
break;
case BF_INC:
++(*dp);
break;
case BF_DEC:
--(*dp);
break;
case BF_OUT:
putchar(*dp);
break;
case BF_IN:
*dp=(int)getchar();
break;
}
if (srclen == 0)
break;
else
ip++;
srclen--;
}
return 0;
}
int main(int argc, char *argv[] ) {
if (argc != 2) {
printf("Usage: %s sourcefile\n", argv[0]);
}
else {
srcfile = fopen(argv[1], "r");
if (srcfile == 0) {
printf("Couldn't open the source file.\n");
}
else {
int c = 0;
int buf;
/* int i = 0; */
while ( ( buf = fgetc(srcfile) ) != EOF ) {
src[c] = buf;
c++;
}
srclen = c;
/* for (i = 0; i < c; i++)
printf("%c", src[i]); */
interpret();
printf("\n");
}
}
return 0;
}