1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
20 |
|
|
21 |
|
#include <errno.h> |
22 |
|
|
23 |
+ |
#include <math.h> |
24 |
+ |
|
25 |
|
#include "calcomp.h" |
26 |
|
|
27 |
< |
|
27 |
> |
/* bits in argument flag (better be right!) */ |
28 |
> |
#define AFLAGSIZ (8*sizeof(unsigned long)) |
29 |
|
#define ALISTSIZ 6 /* maximum saved argument list */ |
30 |
|
|
31 |
|
typedef struct activation { |
90 |
|
|
91 |
|
extern char *savestr(), *emalloc(); |
92 |
|
|
90 |
– |
extern LIBR *liblookup(); |
91 |
– |
|
93 |
|
extern VARDEF *argf(); |
94 |
|
|
95 |
|
#ifdef VARIABLE |
104 |
|
fundefined(fname) /* return # of arguments for function */ |
105 |
|
char *fname; |
106 |
|
{ |
107 |
< |
LIBR *lp; |
107 |
> |
register LIBR *lp; |
108 |
|
register VARDEF *vp; |
109 |
|
|
110 |
< |
if ((vp = varlookup(fname)) == NULL || vp->def == NULL |
111 |
< |
|| vp->def->v.kid->type != FUNC) |
111 |
< |
if ((lp = liblookup(fname)) == NULL) |
112 |
< |
return(0); |
113 |
< |
else |
114 |
< |
return(lp->nargs); |
115 |
< |
else |
110 |
> |
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
111 |
> |
&& vp->def->v.kid->type == FUNC) |
112 |
|
return(nekids(vp->def->v.kid) - 1); |
113 |
+ |
lp = vp != NULL ? vp->lib : liblookup(fname); |
114 |
+ |
if (lp == NULL) |
115 |
+ |
return(0); |
116 |
+ |
return(lp->nargs); |
117 |
|
} |
118 |
|
|
119 |
|
|
130 |
|
act.name = fname; |
131 |
|
act.prev = curact; |
132 |
|
act.ap = a; |
133 |
< |
act.an = (1L<<n)-1; |
133 |
> |
if (n >= AFLAGSIZ) |
134 |
> |
act.an = ~0; |
135 |
> |
else |
136 |
> |
act.an = (1L<<n)-1; |
137 |
|
act.fun = NULL; |
138 |
|
curact = &act; |
139 |
|
|
154 |
|
int assign; |
155 |
|
double (*fptr)(); |
156 |
|
{ |
157 |
+ |
int oldlibsize = libsize; |
158 |
|
register LIBR *lp; |
159 |
|
|
160 |
< |
if ((lp = liblookup(fname)) == NULL) { |
160 |
> |
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
161 |
|
if (libsize >= MAXLIB) { |
162 |
|
eputs("Too many library functons!\n"); |
163 |
|
quit(1); |
172 |
|
break; |
173 |
|
libsize++; |
174 |
|
} |
175 |
< |
lp[0].fname = savestr(fname); |
176 |
< |
lp[0].nargs = nargs; |
177 |
< |
lp[0].atyp = assign; |
178 |
< |
lp[0].f = fptr; |
175 |
> |
if (fptr == NULL) { /* delete */ |
176 |
> |
while (lp < &library[libsize-1]) { |
177 |
> |
lp[0].fname = lp[1].fname; |
178 |
> |
lp[0].nargs = lp[1].nargs; |
179 |
> |
lp[0].atyp = lp[1].atyp; |
180 |
> |
lp[0].f = lp[1].f; |
181 |
> |
lp++; |
182 |
> |
} |
183 |
> |
libsize--; |
184 |
> |
} else { /* or assign */ |
185 |
> |
lp[0].fname = fname; /* string must be static! */ |
186 |
> |
lp[0].nargs = nargs; |
187 |
> |
lp[0].atyp = assign; |
188 |
> |
lp[0].f = fptr; |
189 |
> |
} |
190 |
> |
if (libsize != oldlibsize) |
191 |
> |
libupdate(fname); /* relink library */ |
192 |
|
} |
193 |
|
|
194 |
|
|
213 |
|
register int n; |
214 |
|
{ |
215 |
|
register ACTIVATION *actp = curact; |
216 |
< |
EPNODE *ep; |
216 |
> |
register EPNODE *ep; |
217 |
|
double aval; |
218 |
|
|
219 |
|
if (actp == NULL || --n < 0) { |
221 |
|
quit(1); |
222 |
|
} |
223 |
|
/* already computed? */ |
224 |
< |
if (1L<<n & actp->an) |
224 |
> |
if (n < AFLAGSIZ && 1L<<n & actp->an) |
225 |
|
return(actp->ap[n]); |
226 |
|
|
227 |
|
if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) { |
340 |
|
|
341 |
|
|
342 |
|
#ifndef VARIABLE |
343 |
+ |
static VARDEF *varlist = NULL; /* our list of dummy variables */ |
344 |
+ |
|
345 |
+ |
|
346 |
|
VARDEF * |
347 |
|
varinsert(vname) /* dummy variable insert */ |
348 |
|
char *vname; |
353 |
|
vp->name = savestr(vname); |
354 |
|
vp->nlinks = 1; |
355 |
|
vp->def = NULL; |
356 |
< |
vp->lib = NULL; |
357 |
< |
vp->next = NULL; |
356 |
> |
vp->lib = liblookup(vname); |
357 |
> |
vp->next = varlist; |
358 |
> |
varlist = vp; |
359 |
|
return(vp); |
360 |
|
} |
361 |
|
|
363 |
|
varfree(vp) /* free dummy variable */ |
364 |
|
register VARDEF *vp; |
365 |
|
{ |
366 |
+ |
register VARDEF *vp2; |
367 |
+ |
|
368 |
+ |
if (vp == varlist) |
369 |
+ |
varlist = vp->next; |
370 |
+ |
else { |
371 |
+ |
for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next) |
372 |
+ |
; |
373 |
+ |
vp2->next = vp->next; |
374 |
+ |
} |
375 |
|
freestr(vp->name); |
376 |
|
efree((char *)vp); |
377 |
|
} |
378 |
+ |
|
379 |
+ |
|
380 |
+ |
libupdate(nm) /* update library */ |
381 |
+ |
char *nm; |
382 |
+ |
{ |
383 |
+ |
register VARDEF *vp; |
384 |
+ |
|
385 |
+ |
for (vp = varlist; vp != NULL; vp = vp->next) |
386 |
+ |
vp->lib = liblookup(vp->name); |
387 |
+ |
} |
388 |
|
#endif |
389 |
|
|
390 |
|
|
397 |
|
static double |
398 |
|
libfunc(fname, vp) /* execute library function */ |
399 |
|
char *fname; |
400 |
< |
register VARDEF *vp; |
400 |
> |
VARDEF *vp; |
401 |
|
{ |
402 |
< |
VARDEF dumdef; |
402 |
> |
register LIBR *lp; |
403 |
|
double d; |
404 |
|
int lasterrno; |
405 |
|
|
406 |
< |
if (vp == NULL) { |
407 |
< |
vp = &dumdef; |
408 |
< |
vp->lib = NULL; |
409 |
< |
} |
410 |
< |
if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) && |
371 |
< |
(vp->lib = liblookup(fname)) == NULL) || |
372 |
< |
vp->lib->f == NULL) { |
406 |
> |
if (vp != NULL) |
407 |
> |
lp = vp->lib; |
408 |
> |
else |
409 |
> |
lp = liblookup(fname); |
410 |
> |
if (lp == NULL) { |
411 |
|
eputs(fname); |
412 |
|
eputs(": undefined function\n"); |
413 |
|
quit(1); |
414 |
|
} |
415 |
|
lasterrno = errno; |
416 |
|
errno = 0; |
417 |
< |
d = (*vp->lib->f)(); |
417 |
> |
d = (*lp->f)(lp->fname); |
418 |
|
#ifdef IEEE |
419 |
< |
if (!finite(d)) |
420 |
< |
errno = EDOM; |
419 |
> |
if (errno == 0) |
420 |
> |
if (isnan(d)) |
421 |
> |
errno = EDOM; |
422 |
> |
else if (isinf(d)) |
423 |
> |
errno = ERANGE; |
424 |
|
#endif |
425 |
|
if (errno) { |
426 |
|
wputs(fname); |
427 |
< |
wputs(": bad call\n"); |
427 |
> |
if (errno == EDOM) |
428 |
> |
wputs(": domain error\n"); |
429 |
> |
else if (errno == ERANGE) |
430 |
> |
wputs(": range error\n"); |
431 |
> |
else |
432 |
> |
wputs(": error in call\n"); |
433 |
|
return(0.0); |
434 |
|
} |
435 |
|
errno = lasterrno; |
472 |
|
static double |
473 |
|
l_rand() /* random function between 0 and 1 */ |
474 |
|
{ |
429 |
– |
extern double floor(); |
475 |
|
double x; |
476 |
|
|
477 |
|
x = argument(1); |
485 |
|
static double |
486 |
|
l_floor() /* return largest integer not greater than arg1 */ |
487 |
|
{ |
443 |
– |
extern double floor(); |
444 |
– |
|
488 |
|
return(floor(argument(1))); |
489 |
|
} |
490 |
|
|
492 |
|
static double |
493 |
|
l_ceil() /* return smallest integer not less than arg1 */ |
494 |
|
{ |
452 |
– |
extern double ceil(); |
453 |
– |
|
495 |
|
return(ceil(argument(1))); |
496 |
|
} |
497 |
|
|
500 |
|
static double |
501 |
|
l_sqrt() |
502 |
|
{ |
462 |
– |
extern double sqrt(); |
463 |
– |
|
503 |
|
return(sqrt(argument(1))); |
504 |
|
} |
505 |
|
|
507 |
|
static double |
508 |
|
l_sin() |
509 |
|
{ |
471 |
– |
extern double sin(); |
472 |
– |
|
510 |
|
return(sin(argument(1))); |
511 |
|
} |
512 |
|
|
514 |
|
static double |
515 |
|
l_cos() |
516 |
|
{ |
480 |
– |
extern double cos(); |
481 |
– |
|
517 |
|
return(cos(argument(1))); |
518 |
|
} |
519 |
|
|
521 |
|
static double |
522 |
|
l_tan() |
523 |
|
{ |
489 |
– |
extern double tan(); |
490 |
– |
|
524 |
|
return(tan(argument(1))); |
525 |
|
} |
526 |
|
|
528 |
|
static double |
529 |
|
l_asin() |
530 |
|
{ |
498 |
– |
extern double asin(); |
499 |
– |
|
531 |
|
return(asin(argument(1))); |
532 |
|
} |
533 |
|
|
535 |
|
static double |
536 |
|
l_acos() |
537 |
|
{ |
507 |
– |
extern double acos(); |
508 |
– |
|
538 |
|
return(acos(argument(1))); |
539 |
|
} |
540 |
|
|
542 |
|
static double |
543 |
|
l_atan() |
544 |
|
{ |
516 |
– |
extern double atan(); |
517 |
– |
|
545 |
|
return(atan(argument(1))); |
546 |
|
} |
547 |
|
|
549 |
|
static double |
550 |
|
l_atan2() |
551 |
|
{ |
525 |
– |
extern double atan2(); |
526 |
– |
|
552 |
|
return(atan2(argument(1), argument(2))); |
553 |
|
} |
554 |
|
|
556 |
|
static double |
557 |
|
l_exp() |
558 |
|
{ |
534 |
– |
extern double exp(); |
535 |
– |
|
559 |
|
return(exp(argument(1))); |
560 |
|
} |
561 |
|
|
563 |
|
static double |
564 |
|
l_log() |
565 |
|
{ |
543 |
– |
extern double log(); |
544 |
– |
|
566 |
|
return(log(argument(1))); |
567 |
|
} |
568 |
|
|
570 |
|
static double |
571 |
|
l_log10() |
572 |
|
{ |
552 |
– |
extern double log10(); |
553 |
– |
|
573 |
|
return(log10(argument(1))); |
574 |
|
} |
575 |
|
#endif |