| 17 |
|
#include <errno.h> |
| 18 |
|
#include <math.h> |
| 19 |
|
|
| 20 |
+ |
#include "rterror.h" |
| 21 |
|
#include "calcomp.h" |
| 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 */ |
| 34 |
|
|
| 35 |
|
static ACTIVATION *curact = NULL; |
| 36 |
|
|
| 37 |
< |
static double libfunc(); |
| 37 |
> |
static double libfunc(char *fname, VARDEF *vp); |
| 38 |
|
|
| 39 |
|
#ifndef MAXLIB |
| 40 |
|
#define MAXLIB 64 /* maximum number of library functions */ |
| 41 |
|
#endif |
| 42 |
|
|
| 43 |
< |
static double l_if(), l_select(), l_rand(); |
| 44 |
< |
static double l_floor(), l_ceil(); |
| 45 |
< |
static double l_sqrt(); |
| 46 |
< |
static double l_sin(), l_cos(), l_tan(); |
| 47 |
< |
static double l_asin(), l_acos(), l_atan(), l_atan2(); |
| 48 |
< |
static double l_exp(), l_log(), l_log10(); |
| 43 |
> |
static double l_if(char *), l_select(char *); |
| 44 |
> |
static double l_min(char *), l_max(char *); |
| 45 |
> |
static double l_rand(char *); |
| 46 |
> |
static double l_floor(char *), l_ceil(char *); |
| 47 |
> |
static double l_sqrt(char *); |
| 48 |
> |
static double l_sin(char *), l_cos(char *), l_tan(char *); |
| 49 |
> |
static double l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *); |
| 50 |
> |
static double l_exp(char *), l_log(char *), l_log10(char *); |
| 51 |
|
|
| 52 |
|
/* functions must be listed alphabetically */ |
| 53 |
|
static LIBR library[MAXLIB] = { |
| 62 |
|
{ "if", 3, ':', l_if }, |
| 63 |
|
{ "log", 1, ':', l_log }, |
| 64 |
|
{ "log10", 1, ':', l_log10 }, |
| 65 |
+ |
{ "max", 1, ':', l_max }, |
| 66 |
+ |
{ "min", 1, ':', l_min }, |
| 67 |
|
{ "rand", 1, ':', l_rand }, |
| 68 |
|
{ "select", 1, ':', l_select }, |
| 69 |
|
{ "sin", 1, ':', l_sin }, |
| 71 |
|
{ "tan", 1, ':', l_tan }, |
| 72 |
|
}; |
| 73 |
|
|
| 74 |
< |
static int libsize = 16; |
| 74 |
> |
static int libsize = 18; |
| 75 |
|
|
| 76 |
|
#define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
int |
| 80 |
< |
fundefined(fname) /* return # of arguments for function */ |
| 81 |
< |
char *fname; |
| 80 |
> |
fundefined( /* return # of req'd arguments for function */ |
| 81 |
> |
char *fname |
| 82 |
> |
) |
| 83 |
|
{ |
| 84 |
< |
register LIBR *lp; |
| 85 |
< |
register VARDEF *vp; |
| 84 |
> |
LIBR *lp; |
| 85 |
> |
VARDEF *vp; |
| 86 |
|
|
| 87 |
|
if ((vp = varlookup(fname)) != NULL && vp->def != NULL |
| 88 |
|
&& vp->def->v.kid->type == FUNC) |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
double |
| 98 |
< |
funvalue(fname, n, a) /* return a function value to the user */ |
| 99 |
< |
char *fname; |
| 100 |
< |
int n; |
| 101 |
< |
double *a; |
| 98 |
> |
funvalue( /* return a function value to the user */ |
| 99 |
> |
char *fname, |
| 100 |
> |
int n, |
| 101 |
> |
double *a |
| 102 |
> |
) |
| 103 |
|
{ |
| 104 |
|
ACTIVATION act; |
| 105 |
< |
register VARDEF *vp; |
| 105 |
> |
VARDEF *vp; |
| 106 |
|
double rval; |
| 107 |
|
/* push environment */ |
| 108 |
|
act.name = fname; |
| 109 |
|
act.prev = curact; |
| 110 |
|
act.ap = a; |
| 111 |
< |
if (n >= AFLAGSIZ) |
| 105 |
< |
act.an = ~0; |
| 106 |
< |
else |
| 111 |
> |
if (n < AFLAGSIZ) |
| 112 |
|
act.an = (1L<<n)-1; |
| 113 |
+ |
else { |
| 114 |
+ |
act.an = ~0; |
| 115 |
+ |
if (n > AFLAGSIZ) |
| 116 |
+ |
wputs("Excess arguments in funvalue()\n"); |
| 117 |
+ |
} |
| 118 |
|
act.fun = NULL; |
| 119 |
|
curact = &act; |
| 120 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
void |
| 133 |
< |
funset(fname, nargs, assign, fptr) /* set a library function */ |
| 134 |
< |
char *fname; |
| 135 |
< |
int nargs; |
| 136 |
< |
int assign; |
| 137 |
< |
double (*fptr)(); |
| 133 |
> |
funset( /* set a library function */ |
| 134 |
> |
char *fname, |
| 135 |
> |
int nargs, |
| 136 |
> |
int assign, |
| 137 |
> |
double (*fptr)(char *) |
| 138 |
> |
) |
| 139 |
|
{ |
| 140 |
|
int oldlibsize = libsize; |
| 141 |
|
char *cp; |
| 142 |
< |
register LIBR *lp; |
| 142 |
> |
LIBR *lp; |
| 143 |
|
/* check for context */ |
| 144 |
|
for (cp = fname; *cp; cp++) |
| 145 |
|
; |
| 146 |
|
if (cp == fname) |
| 147 |
|
return; |
| 148 |
< |
if (cp[-1] == CNTXMARK) |
| 148 |
> |
while (cp[-1] == CNTXMARK) { |
| 149 |
|
*--cp = '\0'; |
| 150 |
+ |
if (cp == fname) return; |
| 151 |
+ |
} |
| 152 |
|
if ((lp = liblookup(fname)) == NULL) { /* insert */ |
| 153 |
+ |
if (fptr == NULL) |
| 154 |
+ |
return; /* nothing! */ |
| 155 |
|
if (libsize >= MAXLIB) { |
| 156 |
|
eputs("Too many library functons!\n"); |
| 157 |
|
quit(1); |
| 158 |
|
} |
| 159 |
|
for (lp = &library[libsize]; lp > library; lp--) |
| 160 |
< |
if (strcmp(lp[-1].fname, fname) > 0) { |
| 161 |
< |
lp[0].fname = lp[-1].fname; |
| 162 |
< |
lp[0].nargs = lp[-1].nargs; |
| 148 |
< |
lp[0].atyp = lp[-1].atyp; |
| 149 |
< |
lp[0].f = lp[-1].f; |
| 150 |
< |
} else |
| 160 |
> |
if (strcmp(lp[-1].fname, fname) > 0) |
| 161 |
> |
lp[0] = lp[-1]; |
| 162 |
> |
else |
| 163 |
|
break; |
| 164 |
|
libsize++; |
| 165 |
|
} |
| 166 |
|
if (fptr == NULL) { /* delete */ |
| 167 |
|
while (lp < &library[libsize-1]) { |
| 168 |
< |
lp[0].fname = lp[1].fname; |
| 157 |
< |
lp[0].nargs = lp[1].nargs; |
| 158 |
< |
lp[0].atyp = lp[1].atyp; |
| 159 |
< |
lp[0].f = lp[1].f; |
| 168 |
> |
lp[0] = lp[1]; |
| 169 |
|
lp++; |
| 170 |
|
} |
| 171 |
|
libsize--; |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
int |
| 184 |
< |
nargum() /* return number of available arguments */ |
| 184 |
> |
nargum(void) /* return number of available arguments */ |
| 185 |
|
{ |
| 186 |
< |
register int n; |
| 186 |
> |
int n; |
| 187 |
|
|
| 188 |
|
if (curact == NULL) |
| 189 |
|
return(0); |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
double |
| 200 |
< |
argument(n) /* return nth argument for active function */ |
| 192 |
< |
register int n; |
| 200 |
> |
argument(int n) /* return nth argument for active function */ |
| 201 |
|
{ |
| 202 |
< |
register ACTIVATION *actp = curact; |
| 203 |
< |
register EPNODE *ep; |
| 202 |
> |
ACTIVATION *actp = curact; |
| 203 |
> |
EPNODE *ep = NULL; |
| 204 |
|
double aval; |
| 205 |
|
|
| 206 |
< |
if (actp == NULL || --n < 0) { |
| 206 |
> |
if (!n) /* asking for # arguments? */ |
| 207 |
> |
return((double)nargum()); |
| 208 |
> |
|
| 209 |
> |
if (!actp | (--n < 0)) { |
| 210 |
|
eputs("Bad call to argument!\n"); |
| 211 |
|
quit(1); |
| 212 |
|
} |
| 213 |
< |
/* already computed? */ |
| 203 |
< |
if (n < AFLAGSIZ && 1L<<n & actp->an) |
| 213 |
> |
if ((n < AFLAGSIZ) & actp->an >> n) /* already computed? */ |
| 214 |
|
return(actp->ap[n]); |
| 215 |
|
|
| 216 |
< |
if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) { |
| 216 |
> |
if (!actp->fun || !(ep = ekid(actp->fun, n+1))) { |
| 217 |
|
eputs(actp->name); |
| 218 |
|
eputs(": too few arguments\n"); |
| 219 |
|
quit(1); |
| 220 |
|
} |
| 221 |
< |
curact = actp->prev; /* pop environment */ |
| 221 |
> |
curact = actp->prev; /* previous context */ |
| 222 |
|
aval = evalue(ep); /* compute argument */ |
| 223 |
< |
curact = actp; /* push back environment */ |
| 224 |
< |
if (n < ALISTSIZ) { /* save value */ |
| 223 |
> |
curact = actp; /* put back calling context */ |
| 224 |
> |
if (n < ALISTSIZ) { /* save value if room */ |
| 225 |
|
actp->ap[n] = aval; |
| 226 |
|
actp->an |= 1L<<n; |
| 227 |
|
} |
| 230 |
|
|
| 231 |
|
|
| 232 |
|
VARDEF * |
| 233 |
< |
argf(n) /* return function def for nth argument */ |
| 224 |
< |
int n; |
| 233 |
> |
argf(int n) /* return function def for nth argument */ |
| 234 |
|
{ |
| 235 |
< |
register ACTIVATION *actp; |
| 236 |
< |
register EPNODE *ep; |
| 235 |
> |
ACTIVATION *actp; |
| 236 |
> |
EPNODE *ep; |
| 237 |
|
|
| 238 |
|
for (actp = curact; actp != NULL; actp = actp->prev) { |
| 239 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
char * |
| 271 |
< |
argfun(n) /* return function name for nth argument */ |
| 263 |
< |
int n; |
| 271 |
> |
argfun(int n) /* return function name for nth argument */ |
| 272 |
|
{ |
| 273 |
|
return(argf(n)->name); |
| 274 |
|
} |
| 275 |
|
|
| 276 |
|
|
| 277 |
|
double |
| 278 |
< |
efunc(ep) /* evaluate a function */ |
| 271 |
< |
register EPNODE *ep; |
| 278 |
> |
efunc(EPNODE *ep) /* evaluate a function */ |
| 279 |
|
{ |
| 280 |
|
ACTIVATION act; |
| 281 |
|
double alist[ALISTSIZ]; |
| 282 |
|
double rval; |
| 283 |
< |
register VARDEF *dp; |
| 283 |
> |
VARDEF *dp; |
| 284 |
|
/* push environment */ |
| 285 |
|
dp = resolve(ep->v.kid); |
| 286 |
|
act.name = dp->name; |
| 301 |
|
|
| 302 |
|
|
| 303 |
|
LIBR * |
| 304 |
< |
liblookup(fname) /* look up a library function */ |
| 298 |
< |
char *fname; |
| 304 |
> |
liblookup(char *fname) /* look up a library function */ |
| 305 |
|
{ |
| 306 |
|
int upper, lower; |
| 307 |
< |
register int cm, i; |
| 307 |
> |
int cm, i; |
| 308 |
|
|
| 309 |
|
lower = 0; |
| 310 |
|
upper = cm = libsize; |
| 329 |
|
|
| 330 |
|
|
| 331 |
|
static double |
| 332 |
< |
libfunc(fname, vp) /* execute library function */ |
| 333 |
< |
char *fname; |
| 334 |
< |
VARDEF *vp; |
| 332 |
> |
libfunc( /* execute library function */ |
| 333 |
> |
char *fname, |
| 334 |
> |
VARDEF *vp |
| 335 |
> |
) |
| 336 |
|
{ |
| 337 |
< |
register LIBR *lp; |
| 337 |
> |
LIBR *lp; |
| 338 |
|
double d; |
| 339 |
|
int lasterrno; |
| 340 |
|
|
| 350 |
|
lasterrno = errno; |
| 351 |
|
errno = 0; |
| 352 |
|
d = (*lp->f)(lp->fname); |
| 353 |
< |
#ifdef IEEE |
| 354 |
< |
if (errno == 0) |
| 353 |
> |
#ifdef isnan |
| 354 |
> |
if (errno == 0) { |
| 355 |
|
if (isnan(d)) |
| 356 |
|
errno = EDOM; |
| 357 |
|
else if (isinf(d)) |
| 358 |
|
errno = ERANGE; |
| 359 |
+ |
} |
| 360 |
|
#endif |
| 361 |
|
if (errno == EDOM || errno == ERANGE) { |
| 362 |
|
wputs(fname); |
| 379 |
|
|
| 380 |
|
|
| 381 |
|
static double |
| 382 |
< |
l_if() /* if(cond, then, else) conditional expression */ |
| 382 |
> |
l_if(char *nm) /* if(cond, then, else) conditional expression */ |
| 383 |
|
/* cond evaluates true if greater than zero */ |
| 384 |
|
{ |
| 385 |
|
if (argument(1) > 0.0) |
| 390 |
|
|
| 391 |
|
|
| 392 |
|
static double |
| 393 |
< |
l_select() /* return argument #(A1+1) */ |
| 393 |
> |
l_select(char *nm) /* return argument #(A1+1) */ |
| 394 |
|
{ |
| 395 |
< |
register int n; |
| 395 |
> |
int narg = nargum(); |
| 396 |
> |
double a1 = argument(1); |
| 397 |
> |
int n = (int)(a1 + .5); |
| 398 |
|
|
| 399 |
< |
n = (int)(argument(1) + .5); |
| 390 |
< |
if (n == 0) |
| 391 |
< |
return(nargum()-1); |
| 392 |
< |
if (n < 1 || n > nargum()-1) { |
| 399 |
> |
if (a1 < -.5 || n >= narg) { |
| 400 |
|
errno = EDOM; |
| 401 |
|
return(0.0); |
| 402 |
|
} |
| 403 |
+ |
if (!n) /* asking max index? */ |
| 404 |
+ |
return(narg-1); |
| 405 |
|
return(argument(n+1)); |
| 406 |
|
} |
| 407 |
|
|
| 408 |
|
|
| 409 |
|
static double |
| 410 |
< |
l_rand() /* random function between 0 and 1 */ |
| 410 |
> |
l_max(char *nm) /* general maximum function */ |
| 411 |
|
{ |
| 412 |
+ |
int n = nargum(); |
| 413 |
+ |
int i = 1; |
| 414 |
+ |
double vmax = argument(1); |
| 415 |
+ |
|
| 416 |
+ |
while (i++ < n) { |
| 417 |
+ |
double v = argument(i); |
| 418 |
+ |
if (vmax < v) |
| 419 |
+ |
vmax = v; |
| 420 |
+ |
} |
| 421 |
+ |
return(vmax); |
| 422 |
+ |
} |
| 423 |
+ |
|
| 424 |
+ |
|
| 425 |
+ |
static double |
| 426 |
+ |
l_min(char *nm) /* general minimum function */ |
| 427 |
+ |
{ |
| 428 |
+ |
int n = nargum(); |
| 429 |
+ |
int i = 1; |
| 430 |
+ |
double vmin = argument(1); |
| 431 |
+ |
|
| 432 |
+ |
while (i++ < n) { |
| 433 |
+ |
double v = argument(i); |
| 434 |
+ |
if (vmin > v) |
| 435 |
+ |
vmin = v; |
| 436 |
+ |
} |
| 437 |
+ |
return(vmin); |
| 438 |
+ |
} |
| 439 |
+ |
|
| 440 |
+ |
|
| 441 |
+ |
static double |
| 442 |
+ |
l_rand(char *nm) /* random function between 0 and 1 */ |
| 443 |
+ |
{ |
| 444 |
|
double x; |
| 445 |
|
|
| 446 |
|
x = argument(1); |
| 452 |
|
|
| 453 |
|
|
| 454 |
|
static double |
| 455 |
< |
l_floor() /* return largest integer not greater than arg1 */ |
| 455 |
> |
l_floor(char *nm) /* return largest integer not greater than arg1 */ |
| 456 |
|
{ |
| 457 |
|
return(floor(argument(1))); |
| 458 |
|
} |
| 459 |
|
|
| 460 |
|
|
| 461 |
|
static double |
| 462 |
< |
l_ceil() /* return smallest integer not less than arg1 */ |
| 462 |
> |
l_ceil(char *nm) /* return smallest integer not less than arg1 */ |
| 463 |
|
{ |
| 464 |
|
return(ceil(argument(1))); |
| 465 |
|
} |
| 466 |
|
|
| 467 |
|
|
| 468 |
|
static double |
| 469 |
< |
l_sqrt() |
| 469 |
> |
l_sqrt(char *nm) |
| 470 |
|
{ |
| 471 |
|
return(sqrt(argument(1))); |
| 472 |
|
} |
| 473 |
|
|
| 474 |
|
|
| 475 |
|
static double |
| 476 |
< |
l_sin() |
| 476 |
> |
l_sin(char *nm) |
| 477 |
|
{ |
| 478 |
|
return(sin(argument(1))); |
| 479 |
|
} |
| 480 |
|
|
| 481 |
|
|
| 482 |
|
static double |
| 483 |
< |
l_cos() |
| 483 |
> |
l_cos(char *nm) |
| 484 |
|
{ |
| 485 |
|
return(cos(argument(1))); |
| 486 |
|
} |
| 487 |
|
|
| 488 |
|
|
| 489 |
|
static double |
| 490 |
< |
l_tan() |
| 490 |
> |
l_tan(char *nm) |
| 491 |
|
{ |
| 492 |
|
return(tan(argument(1))); |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
|
| 496 |
|
static double |
| 497 |
< |
l_asin() |
| 497 |
> |
l_asin(char *nm) |
| 498 |
|
{ |
| 499 |
|
return(asin(argument(1))); |
| 500 |
|
} |
| 501 |
|
|
| 502 |
|
|
| 503 |
|
static double |
| 504 |
< |
l_acos() |
| 504 |
> |
l_acos(char *nm) |
| 505 |
|
{ |
| 506 |
|
return(acos(argument(1))); |
| 507 |
|
} |
| 508 |
|
|
| 509 |
|
|
| 510 |
|
static double |
| 511 |
< |
l_atan() |
| 511 |
> |
l_atan(char *nm) |
| 512 |
|
{ |
| 513 |
|
return(atan(argument(1))); |
| 514 |
|
} |
| 515 |
|
|
| 516 |
|
|
| 517 |
|
static double |
| 518 |
< |
l_atan2() |
| 518 |
> |
l_atan2(char *nm) |
| 519 |
|
{ |
| 520 |
|
return(atan2(argument(1), argument(2))); |
| 521 |
|
} |
| 522 |
|
|
| 523 |
|
|
| 524 |
|
static double |
| 525 |
< |
l_exp() |
| 525 |
> |
l_exp(char *nm) |
| 526 |
|
{ |
| 527 |
|
return(exp(argument(1))); |
| 528 |
|
} |
| 529 |
|
|
| 530 |
|
|
| 531 |
|
static double |
| 532 |
< |
l_log() |
| 532 |
> |
l_log(char *nm) |
| 533 |
|
{ |
| 534 |
|
return(log(argument(1))); |
| 535 |
|
} |
| 536 |
|
|
| 537 |
|
|
| 538 |
|
static double |
| 539 |
< |
l_log10() |
| 539 |
> |
l_log10(char *nm) |
| 540 |
|
{ |
| 541 |
|
return(log10(argument(1))); |
| 542 |
|
} |