| 22 |
|
|
| 23 |
|
/* bits in argument flag (better be right!) */ |
| 24 |
|
#define AFLAGSIZ (8*sizeof(unsigned long)) |
| 25 |
< |
#define ALISTSIZ 6 /* maximum saved argument list */ |
| 25 |
> |
#define ALISTSIZ 8 /* maximum saved argument list */ |
| 26 |
|
|
| 27 |
|
typedef struct activation { |
| 28 |
|
char *name; /* function name */ |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
int |
| 76 |
< |
fundefined(fname) /* return # of arguments for function */ |
| 77 |
< |
char *fname; |
| 76 |
> |
fundefined( /* return # of arguments for function */ |
| 77 |
> |
char *fname |
| 78 |
> |
) |
| 79 |
|
{ |
| 80 |
< |
register LIBR *lp; |
| 81 |
< |
register VARDEF *vp; |
| 80 |
> |
LIBR *lp; |
| 81 |
> |
VARDEF *vp; |
| 82 |
|
|
| 83 |
|
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
| 84 |
|
&& vp->def->v.kid->type == FUNC) |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
double |
| 94 |
< |
funvalue(fname, n, a) /* return a function value to the user */ |
| 95 |
< |
char *fname; |
| 96 |
< |
int n; |
| 97 |
< |
double *a; |
| 94 |
> |
funvalue( /* return a function value to the user */ |
| 95 |
> |
char *fname, |
| 96 |
> |
int n, |
| 97 |
> |
double *a |
| 98 |
> |
) |
| 99 |
|
{ |
| 100 |
|
ACTIVATION act; |
| 101 |
< |
register VARDEF *vp; |
| 101 |
> |
VARDEF *vp; |
| 102 |
|
double rval; |
| 103 |
|
/* push environment */ |
| 104 |
|
act.name = fname; |
| 105 |
|
act.prev = curact; |
| 106 |
|
act.ap = a; |
| 107 |
< |
if (n >= AFLAGSIZ) |
| 106 |
< |
act.an = ~0; |
| 107 |
< |
else |
| 107 |
> |
if (n < AFLAGSIZ) |
| 108 |
|
act.an = (1L<<n)-1; |
| 109 |
+ |
else { |
| 110 |
+ |
act.an = ~0; |
| 111 |
+ |
if (n > AFLAGSIZ) |
| 112 |
+ |
wputs("Excess arguments in funvalue()\n"); |
| 113 |
+ |
} |
| 114 |
|
act.fun = NULL; |
| 115 |
|
curact = &act; |
| 116 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
void |
| 129 |
< |
funset(fname, nargs, assign, fptr) /* set a library function */ |
| 130 |
< |
char *fname; |
| 131 |
< |
int nargs; |
| 132 |
< |
int assign; |
| 133 |
< |
double (*fptr)(char *); |
| 129 |
> |
funset( /* set a library function */ |
| 130 |
> |
char *fname, |
| 131 |
> |
int nargs, |
| 132 |
> |
int assign, |
| 133 |
> |
double (*fptr)(char *) |
| 134 |
> |
) |
| 135 |
|
{ |
| 136 |
|
int oldlibsize = libsize; |
| 137 |
|
char *cp; |
| 138 |
< |
register LIBR *lp; |
| 138 |
> |
LIBR *lp; |
| 139 |
|
/* check for context */ |
| 140 |
|
for (cp = fname; *cp; cp++) |
| 141 |
|
; |
| 149 |
|
quit(1); |
| 150 |
|
} |
| 151 |
|
for (lp = &library[libsize]; lp > library; lp--) |
| 152 |
< |
if (strcmp(lp[-1].fname, fname) > 0) { |
| 153 |
< |
lp[0].fname = lp[-1].fname; |
| 154 |
< |
lp[0].nargs = lp[-1].nargs; |
| 149 |
< |
lp[0].atyp = lp[-1].atyp; |
| 150 |
< |
lp[0].f = lp[-1].f; |
| 151 |
< |
} else |
| 152 |
> |
if (strcmp(lp[-1].fname, fname) > 0) |
| 153 |
> |
lp[0] = lp[-1]; |
| 154 |
> |
else |
| 155 |
|
break; |
| 156 |
|
libsize++; |
| 157 |
|
} |
| 158 |
|
if (fptr == NULL) { /* delete */ |
| 159 |
|
while (lp < &library[libsize-1]) { |
| 160 |
< |
lp[0].fname = lp[1].fname; |
| 158 |
< |
lp[0].nargs = lp[1].nargs; |
| 159 |
< |
lp[0].atyp = lp[1].atyp; |
| 160 |
< |
lp[0].f = lp[1].f; |
| 160 |
> |
lp[0] = lp[1]; |
| 161 |
|
lp++; |
| 162 |
|
} |
| 163 |
|
libsize--; |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
int |
| 176 |
< |
nargum() /* return number of available arguments */ |
| 176 |
> |
nargum(void) /* return number of available arguments */ |
| 177 |
|
{ |
| 178 |
< |
register int n; |
| 178 |
> |
int n; |
| 179 |
|
|
| 180 |
|
if (curact == NULL) |
| 181 |
|
return(0); |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
double |
| 192 |
< |
argument(n) /* return nth argument for active function */ |
| 193 |
< |
register int n; |
| 192 |
> |
argument(int n) /* return nth argument for active function */ |
| 193 |
|
{ |
| 194 |
< |
register ACTIVATION *actp = curact; |
| 195 |
< |
register EPNODE *ep = NULL; |
| 194 |
> |
ACTIVATION *actp = curact; |
| 195 |
> |
EPNODE *ep = NULL; |
| 196 |
|
double aval; |
| 197 |
|
|
| 198 |
< |
if (actp == NULL || --n < 0) { |
| 198 |
> |
if (!actp | (--n < 0)) { |
| 199 |
|
eputs("Bad call to argument!\n"); |
| 200 |
|
quit(1); |
| 201 |
|
} |
| 202 |
< |
/* already computed? */ |
| 204 |
< |
if (n < AFLAGSIZ && 1L<<n & actp->an) |
| 202 |
> |
if ((n < AFLAGSIZ) & (actp->an >> n)) /* already computed? */ |
| 203 |
|
return(actp->ap[n]); |
| 204 |
|
|
| 205 |
< |
if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) { |
| 205 |
> |
if (!actp->fun || !(ep = ekid(actp->fun, n+1))) { |
| 206 |
|
eputs(actp->name); |
| 207 |
|
eputs(": too few arguments\n"); |
| 208 |
|
quit(1); |
| 209 |
|
} |
| 210 |
< |
curact = actp->prev; /* pop environment */ |
| 210 |
> |
curact = actp->prev; /* previous context */ |
| 211 |
|
aval = evalue(ep); /* compute argument */ |
| 212 |
< |
curact = actp; /* push back environment */ |
| 213 |
< |
if (n < ALISTSIZ) { /* save value */ |
| 212 |
> |
curact = actp; /* push back context */ |
| 213 |
> |
if (n < ALISTSIZ) { /* save value if we can */ |
| 214 |
|
actp->ap[n] = aval; |
| 215 |
|
actp->an |= 1L<<n; |
| 216 |
|
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
VARDEF * |
| 222 |
< |
argf(n) /* return function def for nth argument */ |
| 225 |
< |
int n; |
| 222 |
> |
argf(int n) /* return function def for nth argument */ |
| 223 |
|
{ |
| 224 |
< |
register ACTIVATION *actp; |
| 225 |
< |
register EPNODE *ep; |
| 224 |
> |
ACTIVATION *actp; |
| 225 |
> |
EPNODE *ep; |
| 226 |
|
|
| 227 |
|
for (actp = curact; actp != NULL; actp = actp->prev) { |
| 228 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
char * |
| 260 |
< |
argfun(n) /* return function name for nth argument */ |
| 264 |
< |
int n; |
| 260 |
> |
argfun(int n) /* return function name for nth argument */ |
| 261 |
|
{ |
| 262 |
|
return(argf(n)->name); |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
|
| 266 |
|
double |
| 267 |
< |
efunc(ep) /* evaluate a function */ |
| 272 |
< |
register EPNODE *ep; |
| 267 |
> |
efunc(EPNODE *ep) /* evaluate a function */ |
| 268 |
|
{ |
| 269 |
|
ACTIVATION act; |
| 270 |
|
double alist[ALISTSIZ]; |
| 271 |
|
double rval; |
| 272 |
< |
register VARDEF *dp; |
| 272 |
> |
VARDEF *dp; |
| 273 |
|
/* push environment */ |
| 274 |
|
dp = resolve(ep->v.kid); |
| 275 |
|
act.name = dp->name; |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
LIBR * |
| 293 |
< |
liblookup(fname) /* look up a library function */ |
| 299 |
< |
char *fname; |
| 293 |
> |
liblookup(char *fname) /* look up a library function */ |
| 294 |
|
{ |
| 295 |
|
int upper, lower; |
| 296 |
< |
register int cm, i; |
| 296 |
> |
int cm, i; |
| 297 |
|
|
| 298 |
|
lower = 0; |
| 299 |
|
upper = cm = libsize; |
| 318 |
|
|
| 319 |
|
|
| 320 |
|
static double |
| 321 |
< |
libfunc(fname, vp) /* execute library function */ |
| 322 |
< |
char *fname; |
| 323 |
< |
VARDEF *vp; |
| 321 |
> |
libfunc( /* execute library function */ |
| 322 |
> |
char *fname, |
| 323 |
> |
VARDEF *vp |
| 324 |
> |
) |
| 325 |
|
{ |
| 326 |
< |
register LIBR *lp; |
| 326 |
> |
LIBR *lp; |
| 327 |
|
double d; |
| 328 |
|
int lasterrno; |
| 329 |
|
|
| 339 |
|
lasterrno = errno; |
| 340 |
|
errno = 0; |
| 341 |
|
d = (*lp->f)(lp->fname); |
| 342 |
< |
#ifdef IEEE |
| 343 |
< |
if (errno == 0) |
| 342 |
> |
#ifdef isnan |
| 343 |
> |
if (errno == 0) { |
| 344 |
|
if (isnan(d)) |
| 345 |
|
errno = EDOM; |
| 346 |
|
else if (isinf(d)) |
| 347 |
|
errno = ERANGE; |
| 348 |
+ |
} |
| 349 |
|
#endif |
| 350 |
|
if (errno == EDOM || errno == ERANGE) { |
| 351 |
|
wputs(fname); |
| 381 |
|
static double |
| 382 |
|
l_select(char *nm) /* return argument #(A1+1) */ |
| 383 |
|
{ |
| 384 |
< |
register int n; |
| 384 |
> |
int n; |
| 385 |
|
|
| 386 |
|
n = (int)(argument(1) + .5); |
| 387 |
|
if (n == 0) |