/
d.c
310 lines (272 loc) · 8.57 KB
/
d.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
/* ----------------------------------------------------------------------- */
/* J-Source Version 7 - COPYRIGHT 1993 Iverson Software Inc. */
/* 33 Major Street, Toronto, Ontario, Canada, M5S 2K9, (416) 925 6096 */
/* */
/* J-Source is provided "as is" without warranty of any kind. */
/* */
/* J-Source Version 7 license agreement: You may use, copy, and */
/* modify the source. You have a non-exclusive, royalty-free right */
/* to redistribute source and executable files. */
/* ----------------------------------------------------------------------- */
/* */
/* Debug facilities */
#include "j.h"
#include "x.h"
#include "p.h"
#include "d.h"
/* redefine GA to do correct cast */
#undef GA
#define GA(v,t,n,r,s) RZ(v=(DC)ga((I)(t),(I)(n),(I)(r),(I*)(s)))
#define SUSNON 0 /* susact: no op */
#define SUSRUN 1 /* susact: run again */
#define SUSRET 2 /* susact: return result */
#define SUSPOP 3 /* susact: signal result error */
#define SUSRES 4 /* susact: reset stack */
#define SUSNXT 5 /* susact: run next line */
static B debugbb; /* empty si stack debugb setting */
static B nflag; /* 1 if space required before name */
static A qstops; /* stops set by the user */
static C susact; /* action on suspension end */
static B suspend; /* 1 if stay in current suspension */
B debugb; /* debug as standard state for testing */
B deresetf; /* dbr flag; 1 if suspension is enabled */
B drun; /* do not stop after run */
A qpopres; /* result to pop to the next level */
DC sitop; /* top of SI stack */
static void dhead(void){if(outfile)fputc(COUT,outfile); jputc(qbx[9]);}
/* preface stack display line */
static void dheadp(void){dhead(); jputs(" ");}
/* preface stack display line */
static void dwr(A w){if(w){C*p=CAV(w); DO(AN(w), jputc(p[i]));}}
static void dwrq(A w){
if(all1(match(alp,w)))jputs(nflag?" a.":"a.");
else{C q=CQUOTE;
jputc(q);
if(w){C*p=CAV(w); DO(AN(w), if(q==p[i])jputc(q); jputc(p[i]));}
jputc(q);
}}
static void dname(A w){C c=*CAV(w);
if(nflag)jputc(' ');
if(c==CALPHA)jputs("x");
else if(c==COMEGA)jputs("y");
else dwr(w);
}
static void dspell(C id){C c,s[3];
s[2]=0;
spellit(id,s);
c=s[0]; if(c==CESC1||c==CESC2||nflag&&CA==ctype[c])jputc(' ');
jputs(s);
}
/*static*/ void disp(A w){C err;I t;
t=AT(w);
switch(t){
case BOOL:
case INT:
case FL:
case CMPX:
if(nflag)jputc(' ');
err=jerr; jerr=0; w=thorn1(w); jerr=err;
if(w)dwr(w); else jputs(" (ws full in numeric display) ");
break;
case NAME: dname(w); break;
case CHAR: dwrq(w); break;
case LPAR: jputc('('); break;
case RPAR: jputc(')'); break;
case ASGN: jputs(*AV(w)?"=.":"=:"); break;
case MARK: break;
default: dspell(VAV(w)->id);
}
nflag=t&NAME+NUMERIC?1:0;
}
static DCF(seedebug){A t=*(si->ln+AAV(qevm)); jputs(AV(t));}
/* display error text */
static DCF(seedefn){dwr(si->p); JSPRX(1==si->n?"[%ld]":"[:%ld]",si->ln);}
/* display function line */
static DCF(seeparse){A*s;I m,n,*sp;
n=si->ln; /* # of tokens in token array */
m=si->n;
s=DSZX+(A*)si;
sp=n+(I*)s; /* address i.#tokens to track location for error */
m=*(sp+m);
nflag=0; DO(n, if(i==m)jputs(" "); disp(s[i]));
} /* display error line */
static DCF(debdisp){
if(si){
switch(si->t){
case DCPARS: dheadp(); seeparse(si); break;
case DCDEFN: dhead(); seedefn(si); break;
case DCSCRP: dhead(); JSPRX("[-%ld]", si->ln); break;
case DCDEBG: dhead(); seedebug(si); break;
case DCNAME: dhead(); dwr(si->m); break;
}
jputc(CNL);
}}
static DCF(dispsis){
while(si){
if(DCDEBG==si->t) R;
debdisp(si);
if(DCDEFN==si->t || DCSCRP==si->t) R;
si=si->lnk;
}}
static DCF(dispsi){while(si){debdisp(si); si=si->lnk;}}
I*deba(A *stack,I i){DC d; I k,*sp,*spx;
GA(d,INT,2*i+DSZ,1L,0);
d->t=DCPARS;
d->lnk=sitop;
sitop=d; /* link new debug token array to top of si stack */
d->ln=i; /* tokens in debug token array */
d->n=i-1; /* assumed error at end of token array */
sp=DSZX+(I*)d;
memcpy(sp,stack,i*sizeof(A*));
spx=sp=i+sp;
k=0;
while(i--)*spx++=k++;
R sp;
}
DC debadd(I type){DC d;
GA(d,INT,DSZ,1,0);
d->t=type;
d->lnk=sitop; sitop=d; /* link new debug stack entry to top of si */
R d;
}
void debz(void){
if( sitop)sitop=sitop->lnk;
if(!sitop){debugb=debugbb; deresetf=0;}
}
static void susp(void){I old=tbase+ttop;C*sp;
suspend=debugb;
scad=0;
while(suspend){
immex(jgets(" ")); jerr=0;
tpop(old);
}
suspend=debugb;
}
void debug(void){A name,seq;C err,*sp,*spz,*spp;DC si;
sp=scad; spz=sczad; spp=scpad; si=sitop;
if(!debugb||!si) R;
if(DCPARS==si->t) si=si->lnk;
if(!si || DCDEFN!=si->t && DCSCRP!=si->t) R;
err=jerr; jerr=0;
if(!debadd(DCDEBG)){debugb=0; jsignal(EVSYSTEM); R;}
sitop->ln=err;
if(DCDEFN==si->t){
name=scnm(CSPARS);
seq=srd(name,local);
seq=append(sc(si->ln),seq);
symbis(name,seq,local);
susp();
scad=sp; sczad=spz; scpad=spp;
switch(susact){
case SUSNON: break;
case SUSRUN: break;
case SUSRET: symbis(name, mtv, local); break;
case SUSPOP: symbis(name, mtv, local); break;
case SUSRES: break;
case SUSNXT: symbis(name, behead(seq), local); break;
}} else if(DCSCRP==si->t){
susp();
scad=sp; sczad=spz; scpad=spp;
switch(susact){
case SUSNON: break;
case SUSRUN: scad=scpad; break;
case SUSRET: fa(qpopres); qpopres=0;
case SUSPOP: scad=0; break;
case SUSRES: break;
case SUSNXT: break;
}}
susact=SUSNON; debz();
}
static B stopsub(C *p,C *nw,I md){C c,ca,*cp,*sp; I n;
sp=strchr(p,';');
if(!sp) sp=p+strlen(p);
cp=strchr(p,':');
if((!cp)||(cp>sp)) cp=sp;
if(2==md){p=cp; cp=sp;}
sp=strchr(p, '*');
if(sp && sp<=cp) R 1;
n=strlen(nw);
while(p<cp){
if(!strncmp(p, nw, n)){
ca=*(p-1); c=*(p+n);
if((c==0||c==' '||c==':'||c==';')&&(ca==' '||ca==':')) R 1;
}
p++;
}
R 0;
}
/* check for stop before each function line; return 1 if stop requested */
/* 0 or more repetitions of the following pattern, separated by ; */
/* f m:d function name; monad line #s; dyad line #s. * means all */
B dbcheck(void){A t;C nw[10],*s,*tv;DC dv;I md,tn;
if(!qstops)R 0;
if(!sitop->lnk)R 0;
dv=sitop->lnk;
if(DCDEFN!=dv->t)R 0;
if(drun){drun=0; R 0;}
t=dv->p;
if(!t)R 0;
tn=AN(t); tv=CAV(t); s=CAV(qstops); md=dv->n; sprintf(nw,"%ld",dv->ln);
while(s){
while(' '==*s)++s;
if('*'==*s){s++; if(stopsub(s,nw,md))R 1;}
else if(!strncmp(s,tv,tn)){s+=tn; if(' '==*s&&stopsub(s,nw,md))R 1;}
s=strchr(s,';'); if(s)++s;
}
R 0;
}
F1(dbr){I k;
if(AN(w)){RE(k=i0(w)); ASSERT(0==k||1==k,EVDOMAIN); debugbb=k;}
deresetf=1; debugb=suspend=0; susact=SUSRES;
R mtm;
} /* 13!:0 reset stack; enable/disable suspension */
F1(dbs){DC si=sitop;
RZ(w);
if(si&&si->t==DCNAME) si=si->lnk;
if(si&&si->t==DCPARS) si=si->lnk;
dispsi(si);
R mtm;
} /* 13!:1 display SI stack */
F1(dbsq){RZ(w); R qstops?qstops:mtv;}
/* 13!:2 query stops */
F1(dbss){
RZ(w);
ASSERT(1==AR(w),EVRANK);
ASSERT(CHAR&AT(w),EVDOMAIN);
if(AN(w))ra(w); fa(qstops); qstops=AN(w)?w:0;
R mtv;
} /* 13!:3 set stops */
F1(dbrun){RZ(w); suspend=0; drun=1; susact=SUSRUN; R mtm;}
/* 13!:4 run again */
F1(dbnxt){RZ(w); suspend=0; drun=1; susact=SUSNXT; R mtm;}
/* 13!:5 run next */
F1(dbret){
RZ(w);
suspend=0; drun=1; susact=SUSRET;
fa(qpopres); qpopres=0; qpopres=ra(w);
R mtm;
} /* 13!:6 exit and signal result error */
F1(dbpop){RZ(w); suspend=0; drun=1; susact=SUSPOP; R mtm;}
/* 13!:7 exit and return v */
static void jsig(void){
tostdout=1; suspend=1;
if(debugb&&!spc()){
dhead(); jputs("ws full (can not debug suspend)"); jputc(CNL);
debugb=0;
}
dhead(); jputs(AV(*(jerr+AAV(qevm)))); jputc(CNL);
}
static void jsigz(void){
if(debugb)dispsis(sitop); else{dispsi(sitop); deresetf=0!=sitop;}
}
void jsignal(int e){
if(jerr)R; jerr=e; if(deresetf||!errsee) R;
jsig(); jsigz();
}
void jsignalx(int e,A w,I n){
if(jerr)R; jerr=e; if(deresetf||!errsee) R;
jsig();
dheadp(); dwr(w); jputc(CNL);
if(n!=-1){dheadp(); DO(n, jputc(' ')); jputc('^'); jputc(CNL);}
jsigz();
}