-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb-01.c
146 lines (125 loc) · 2.57 KB
/
pb-01.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
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#define TAB_SIZE 4
#define MEM_MAX 30000
#define PRC_MAX 256
#define STK_MAX 256
unsigned short s[STK_MAX]={0};
unsigned char m[MEM_MAX]={0};
unsigned char *c;
ssize_t p[PRC_MAX],sp,d;
size_t mp,cp,scp,n,ln,cl,i;
FILE *f;
int ch;
void getpos(ssize_t scp) {
ssize_t i;
if(scp>=0) {
ln=1;cl=1;
for(i=0;i<scp;i++) {
if(c[i]=='\n') {ln++;cl=0;}
if(c[i]=='\t') cl+=TAB_SIZE; else cl++;
}
printf(" AT %zd:%zd\n",ln,cl);
}
}
void die(ssize_t scp,const char *fmt,...) {
va_list args;
printf("\n");
va_start(args,fmt);
vprintf(fmt,args);
va_end(args);
if(scp>=0) getpos(scp); else printf("\n");
exit(1);
}
int main(int argc,char **argv) {
if(argc<2) {
die(-1,"usage: %s filename\n",argv[0]);
}
if((f=fopen(argv[1],"rb"))==NULL) {
die(-1,"EFOPEN");
}
fseek(f,0L,SEEK_END);
n=ftell(f);
rewind(f);
if((c=malloc(sizeof(*c)*n))==NULL) {
die(-1,"EMALLOC");
}
if(fread(c,sizeof(*c),n,f)!=n) {
die(-1,"EFREAD");
}
fclose(f);
for(i=0;i<PRC_MAX;i++) {
p[i]=-1;
}
mp=0;
cp=0;
ln=1;
cl=1;
sp=STK_MAX;
while(cp<n) {
d=1;
switch(c[cp]) {
case '+': m[mp]++; break;
case '-': m[mp]--; break;
case '<': if(mp==0) die(cp,"PBEBLWMIN"); else mp--; break;
case '>': if(mp==MEM_MAX-1) die(cp,"PBEABVMAX"); else mp++; break;
case '.': putchar(m[mp]); break;
case ',': m[mp]=((ch=getchar())==EOF?0:ch); break;
case '[':
scp=cp;
while(d!=0) {
if(cp==n-1) die(scp,"PBEOBNOMAT");
cp++;
d-=(c[cp]==']')-(c[cp]=='[');
}
if(m[mp]!=0) {
cp=scp;
}
break;
case ']':
scp=cp;
while(d!=0) {
if(cp==0) die(scp,"PBECBNOMAT");
cp--;
d-=(c[cp]=='[')-(c[cp]==']');
}
if(m[mp]==0) {
cp=scp;
}
break;
case '(':
scp=cp;
while(d!=0) {
if(cp==n-1) die(scp,"PBEOPNOMAT");
cp++;
d-=(c[cp]==')')-(c[cp]=='(');
}
p[m[mp]]=scp;
break;
case ')':
scp=cp;
while(d!=0) {
if(cp==0) die(scp,"PBECPNOMAT");
cp--;
d-=(c[cp]=='(')-(c[cp]==')');
}
cp=scp;
if(sp==STK_MAX) die(scp,"PBESUNDFLW"); else cp=s[sp++];
break;
case ':':
if(p[m[mp]]==-1) die(cp,"PBENOPROC");
if(sp==0) die(cp,"PBESOVRFLW"); else { s[--sp]=cp; cp=p[m[mp]]; }
break;
case '#':
while(cp<n-1 && c[cp]!='\n') {
cp++;
}
break;
default: break;
}
cp++;
}
free(c);
return 0;
}