| 1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
|
#endif |
| 6 |
|
|
| 7 |
|
/* |
| 24 |
|
|
| 25 |
|
#include <errno.h> |
| 26 |
|
|
| 27 |
+ |
#include <math.h> |
| 28 |
+ |
|
| 29 |
|
#include "calcomp.h" |
| 30 |
|
|
| 31 |
< |
#define MAXLINE 256 /* maximum line length */ |
| 31 |
> |
#define MAXLINE 256 /* maximum line length */ |
| 32 |
|
|
| 33 |
< |
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
| 33 |
> |
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
| 34 |
|
|
| 35 |
< |
#define isdecimal(c) (isdigit(c) || (c) == '.') |
| 35 |
> |
#define isdecimal(c) (isdigit(c) || (c) == '.') |
| 36 |
|
|
| 37 |
< |
#ifndef atof |
| 36 |
< |
extern double atof(); |
| 37 |
< |
#endif |
| 38 |
< |
extern double pow(); |
| 39 |
< |
extern char *fgets(), *savestr(); |
| 37 |
> |
extern char *savestr(); |
| 38 |
|
extern char *emalloc(), *ecalloc(); |
| 39 |
|
extern EPNODE *curfunc; |
| 40 |
|
extern double efunc(), evariable(); |
| 41 |
< |
static double euminus(), echannel(), eargument(), enumber(); |
| 41 |
> |
static double euminus(), eargument(), enumber(); |
| 42 |
> |
#ifdef INCHAN |
| 43 |
> |
static double echannel(); |
| 44 |
> |
#endif |
| 45 |
|
static double eadd(), esubtr(), emult(), edivi(), epow(); |
| 46 |
|
static double ebotch(); |
| 46 |
– |
extern int errno; |
| 47 |
|
|
| 48 |
+ |
#ifdef DCL_ATOF |
| 49 |
+ |
extern double atof(); |
| 50 |
+ |
#endif |
| 51 |
+ |
|
| 52 |
|
int nextc; /* lookahead character */ |
| 53 |
|
|
| 54 |
< |
double (*eoper[])() = { /* expression operations */ |
| 54 |
> |
double (*eoper[])() = { /* expression operations */ |
| 55 |
|
ebotch, |
| 56 |
< |
#ifdef VARIABLE |
| 56 |
> |
#ifdef VARIABLE |
| 57 |
|
evariable, |
| 58 |
|
#else |
| 59 |
|
ebotch, |
| 60 |
|
#endif |
| 61 |
|
enumber, |
| 62 |
|
euminus, |
| 63 |
< |
#ifdef INCHAN |
| 63 |
> |
#ifdef INCHAN |
| 64 |
|
echannel, |
| 65 |
|
#else |
| 66 |
|
ebotch, |
| 67 |
|
#endif |
| 68 |
< |
#ifdef FUNCTION |
| 68 |
> |
#ifdef FUNCTION |
| 69 |
|
efunc, |
| 70 |
|
eargument, |
| 71 |
|
#else |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
+ |
epcmp(ep1, ep2) /* compare two expressions for equivalence */ |
| 133 |
+ |
register EPNODE *ep1, *ep2; |
| 134 |
+ |
{ |
| 135 |
+ |
double d; |
| 136 |
+ |
|
| 137 |
+ |
if (ep1->type != ep2->type) |
| 138 |
+ |
return(1); |
| 139 |
+ |
|
| 140 |
+ |
switch (ep1->type) { |
| 141 |
+ |
|
| 142 |
+ |
case VAR: |
| 143 |
+ |
return(ep1->v.ln != ep2->v.ln); |
| 144 |
+ |
|
| 145 |
+ |
case NUM: |
| 146 |
+ |
if (ep2->v.num == 0) |
| 147 |
+ |
return(ep1->v.num != 0); |
| 148 |
+ |
d = ep1->v.num / ep2->v.num; |
| 149 |
+ |
return(d > 1.000000000001 | d < 0.999999999999); |
| 150 |
+ |
|
| 151 |
+ |
case CHAN: |
| 152 |
+ |
case ARG: |
| 153 |
+ |
return(ep1->v.chan != ep2->v.chan); |
| 154 |
+ |
|
| 155 |
+ |
case '=': |
| 156 |
+ |
case ':': |
| 157 |
+ |
return(epcmp(ep1->v.kid->sibling, ep2->v.kid->sibling)); |
| 158 |
+ |
|
| 159 |
+ |
case TICK: |
| 160 |
+ |
case SYM: /* should never get this one */ |
| 161 |
+ |
return(0); |
| 162 |
+ |
|
| 163 |
+ |
default: |
| 164 |
+ |
ep1 = ep1->v.kid; |
| 165 |
+ |
ep2 = ep2->v.kid; |
| 166 |
+ |
while (ep1 != NULL) { |
| 167 |
+ |
if (ep2 == NULL) |
| 168 |
+ |
return(1); |
| 169 |
+ |
if (epcmp(ep1, ep2)) |
| 170 |
+ |
return(1); |
| 171 |
+ |
ep1 = ep1->sibling; |
| 172 |
+ |
ep2 = ep2->sibling; |
| 173 |
+ |
} |
| 174 |
+ |
return(ep2 != NULL); |
| 175 |
+ |
} |
| 176 |
+ |
} |
| 177 |
+ |
|
| 178 |
+ |
|
| 179 |
|
epfree(epar) /* free a parse tree */ |
| 180 |
< |
register EPNODE *epar; |
| 180 |
> |
register EPNODE *epar; |
| 181 |
|
{ |
| 182 |
|
register EPNODE *ep; |
| 183 |
|
|
| 187 |
|
case VAR: |
| 188 |
|
varfree(epar->v.ln); |
| 189 |
|
break; |
| 139 |
– |
#endif |
| 190 |
|
|
| 191 |
|
case SYM: |
| 192 |
|
freestr(epar->v.name); |
| 193 |
|
break; |
| 194 |
+ |
#endif |
| 195 |
|
|
| 196 |
|
case NUM: |
| 197 |
|
case CHAN: |
| 200 |
|
break; |
| 201 |
|
|
| 202 |
|
default: |
| 203 |
< |
for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) |
| 203 |
> |
while ((ep = epar->v.kid) != NULL) { |
| 204 |
> |
epar->v.kid = ep->sibling; |
| 205 |
|
epfree(ep); |
| 206 |
+ |
} |
| 207 |
|
break; |
| 208 |
|
|
| 209 |
|
} |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
/* the following used to be a switch */ |
| 215 |
< |
#ifdef FUNCTION |
| 215 |
> |
#ifdef FUNCTION |
| 216 |
|
static double |
| 217 |
|
eargument(ep) |
| 218 |
< |
EPNODE *ep; |
| 218 |
> |
EPNODE *ep; |
| 219 |
|
{ |
| 220 |
|
return(argument(ep->v.chan)); |
| 221 |
|
} |
| 223 |
|
|
| 224 |
|
static double |
| 225 |
|
enumber(ep) |
| 226 |
< |
EPNODE *ep; |
| 226 |
> |
EPNODE *ep; |
| 227 |
|
{ |
| 228 |
|
return(ep->v.num); |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
static double |
| 232 |
|
euminus(ep) |
| 233 |
< |
EPNODE *ep; |
| 233 |
> |
EPNODE *ep; |
| 234 |
|
{ |
| 235 |
|
register EPNODE *ep1 = ep->v.kid; |
| 236 |
|
|
| 237 |
|
return(-evalue(ep1)); |
| 238 |
|
} |
| 239 |
|
|
| 240 |
< |
#ifdef INCHAN |
| 240 |
> |
#ifdef INCHAN |
| 241 |
|
static double |
| 242 |
|
echannel(ep) |
| 243 |
< |
EPNODE *ep; |
| 243 |
> |
EPNODE *ep; |
| 244 |
|
{ |
| 245 |
|
return(chanvalue(ep->v.chan)); |
| 246 |
|
} |
| 248 |
|
|
| 249 |
|
static double |
| 250 |
|
eadd(ep) |
| 251 |
< |
EPNODE *ep; |
| 251 |
> |
EPNODE *ep; |
| 252 |
|
{ |
| 253 |
|
register EPNODE *ep1 = ep->v.kid; |
| 254 |
|
|
| 257 |
|
|
| 258 |
|
static double |
| 259 |
|
esubtr(ep) |
| 260 |
< |
EPNODE *ep; |
| 260 |
> |
EPNODE *ep; |
| 261 |
|
{ |
| 262 |
|
register EPNODE *ep1 = ep->v.kid; |
| 263 |
|
|
| 266 |
|
|
| 267 |
|
static double |
| 268 |
|
emult(ep) |
| 269 |
< |
EPNODE *ep; |
| 269 |
> |
EPNODE *ep; |
| 270 |
|
{ |
| 271 |
|
register EPNODE *ep1 = ep->v.kid; |
| 272 |
|
|
| 275 |
|
|
| 276 |
|
static double |
| 277 |
|
edivi(ep) |
| 278 |
< |
EPNODE *ep; |
| 278 |
> |
EPNODE *ep; |
| 279 |
|
{ |
| 280 |
|
register EPNODE *ep1 = ep->v.kid; |
| 281 |
|
double d; |
| 291 |
|
|
| 292 |
|
static double |
| 293 |
|
epow(ep) |
| 294 |
< |
EPNODE *ep; |
| 294 |
> |
EPNODE *ep; |
| 295 |
|
{ |
| 296 |
|
register EPNODE *ep1 = ep->v.kid; |
| 297 |
|
double d; |
| 298 |
< |
int lasterrno; |
| 298 |
> |
int lasterrno; |
| 299 |
|
|
| 300 |
|
lasterrno = errno; |
| 301 |
|
errno = 0; |
| 302 |
|
d = pow(evalue(ep1), evalue(ep1->sibling)); |
| 303 |
< |
#ifdef IEEE |
| 303 |
> |
#ifdef IEEE |
| 304 |
|
if (!finite(d)) |
| 305 |
|
errno = EDOM; |
| 306 |
|
#endif |
| 314 |
|
|
| 315 |
|
static double |
| 316 |
|
ebotch(ep) |
| 317 |
< |
EPNODE *ep; |
| 317 |
> |
EPNODE *ep; |
| 318 |
|
{ |
| 319 |
|
eputs("Bad expression!\n"); |
| 320 |
|
quit(1); |
| 323 |
|
|
| 324 |
|
EPNODE * |
| 325 |
|
ekid(ep, n) /* return pointer to a node's nth kid */ |
| 326 |
< |
register EPNODE *ep; |
| 326 |
> |
register EPNODE *ep; |
| 327 |
|
register int n; |
| 328 |
|
{ |
| 329 |
|
|
| 337 |
|
|
| 338 |
|
int |
| 339 |
|
nekids(ep) /* return # of kids for node ep */ |
| 340 |
< |
register EPNODE *ep; |
| 340 |
> |
register EPNODE *ep; |
| 341 |
|
{ |
| 342 |
|
register int n = 0; |
| 343 |
|
|
| 353 |
|
char *fn; |
| 354 |
|
int ln; |
| 355 |
|
{ |
| 356 |
< |
static char inpbuf[MAXLINE]; |
| 356 |
> |
static char inpbuf[MAXLINE]; |
| 357 |
|
|
| 358 |
|
infp = fp; |
| 359 |
|
linbuf = inpbuf; |
| 425 |
|
|
| 426 |
|
|
| 427 |
|
char * |
| 428 |
< |
ltoa(l) /* convert long to ascii */ |
| 428 |
> |
long2ascii(l) /* convert long to ascii */ |
| 429 |
|
long l; |
| 430 |
|
{ |
| 431 |
< |
static char buf[16]; |
| 431 |
> |
static char buf[16]; |
| 432 |
|
register char *cp; |
| 433 |
< |
int neg = 0; |
| 433 |
> |
int neg = 0; |
| 434 |
|
|
| 435 |
|
if (l == 0) |
| 436 |
|
return("0"); |
| 459 |
|
if (infile != NULL) eputs(infile); |
| 460 |
|
if (lineno != 0) { |
| 461 |
|
eputs(infile != NULL ? ", line " : "line "); |
| 462 |
< |
eputs(ltoa((long)lineno)); |
| 462 |
> |
eputs(long2ascii((long)lineno)); |
| 463 |
|
} |
| 464 |
< |
eputs(": syntax error:\n"); |
| 464 |
> |
eputs(":\n"); |
| 465 |
|
} |
| 466 |
|
eputs(linbuf); |
| 467 |
|
if (linbuf[strlen(linbuf)-1] != '\n') |
| 476 |
|
|
| 477 |
|
|
| 478 |
|
addekid(ep, ekid) /* add a child to ep */ |
| 479 |
< |
register EPNODE *ep; |
| 480 |
< |
EPNODE *ekid; |
| 479 |
> |
register EPNODE *ep; |
| 480 |
> |
EPNODE *ekid; |
| 481 |
|
{ |
| 482 |
|
if (ep->v.kid == NULL) |
| 483 |
|
ep->v.kid = ekid; |
| 490 |
|
} |
| 491 |
|
|
| 492 |
|
|
| 493 |
+ |
#if defined(VARIABLE) || defined(FUNCTION) |
| 494 |
|
char * |
| 495 |
|
getname() /* scan an identifier */ |
| 496 |
|
{ |
| 497 |
< |
static char str[MAXWORD+1]; |
| 497 |
> |
static char str[MAXWORD+1]; |
| 498 |
|
register int i, lnext; |
| 499 |
|
|
| 500 |
|
lnext = nextc; |
| 506 |
|
|
| 507 |
|
return(str); |
| 508 |
|
} |
| 509 |
+ |
#endif |
| 510 |
|
|
| 511 |
|
|
| 512 |
|
int |
| 537 |
|
lnext = scan(); |
| 538 |
|
} |
| 539 |
|
if (lnext == '.' && i < MAXWORD) { |
| 540 |
< |
str[i++] = lnext; |
| 541 |
< |
lnext = scan(); |
| 540 |
> |
str[i++] = lnext; |
| 541 |
> |
lnext = scan(); |
| 542 |
> |
if (i == 1 && !isdigit(lnext)) |
| 543 |
> |
syntax("badly formed number"); |
| 544 |
|
while (isdigit(lnext) && i < MAXWORD) { |
| 545 |
|
str[i++] = lnext; |
| 546 |
|
lnext = scan(); |
| 547 |
|
} |
| 548 |
|
} |
| 549 |
< |
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
| 550 |
< |
str[i++] = lnext; |
| 551 |
< |
lnext = scan(); |
| 552 |
< |
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
| 549 |
> |
if ((lnext == 'e' | lnext == 'E') && i < MAXWORD) { |
| 550 |
> |
str[i++] = lnext; |
| 551 |
> |
lnext = scan(); |
| 552 |
> |
if ((lnext == '-' | lnext == '+') && i < MAXWORD) { |
| 553 |
|
str[i++] = lnext; |
| 554 |
|
lnext = scan(); |
| 555 |
|
} |
| 556 |
+ |
if (!isdigit(lnext)) |
| 557 |
+ |
syntax("missing exponent"); |
| 558 |
|
while (isdigit(lnext) && i < MAXWORD) { |
| 559 |
|
str[i++] = lnext; |
| 560 |
|
lnext = scan(); |
| 568 |
|
|
| 569 |
|
EPNODE * |
| 570 |
|
getE1() /* E1 -> E1 ADDOP E2 */ |
| 571 |
< |
/* E2 */ |
| 571 |
> |
/* E2 */ |
| 572 |
|
{ |
| 573 |
|
register EPNODE *ep1, *ep2; |
| 574 |
|
|
| 579 |
|
scan(); |
| 580 |
|
addekid(ep2, ep1); |
| 581 |
|
addekid(ep2, getE2()); |
| 582 |
< |
#ifdef RCONST |
| 582 |
> |
#ifdef RCONST |
| 583 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
| 584 |
|
ep2 = rconst(ep2); |
| 585 |
|
#endif |
| 591 |
|
|
| 592 |
|
EPNODE * |
| 593 |
|
getE2() /* E2 -> E2 MULOP E3 */ |
| 594 |
< |
/* E3 */ |
| 594 |
> |
/* E3 */ |
| 595 |
|
{ |
| 596 |
|
register EPNODE *ep1, *ep2; |
| 597 |
|
|
| 602 |
|
scan(); |
| 603 |
|
addekid(ep2, ep1); |
| 604 |
|
addekid(ep2, getE3()); |
| 605 |
< |
#ifdef RCONST |
| 605 |
> |
#ifdef RCONST |
| 606 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
| 607 |
|
ep2 = rconst(ep2); |
| 608 |
|
#endif |
| 614 |
|
|
| 615 |
|
EPNODE * |
| 616 |
|
getE3() /* E3 -> E4 ^ E3 */ |
| 617 |
< |
/* E4 */ |
| 617 |
> |
/* E4 */ |
| 618 |
|
{ |
| 619 |
|
register EPNODE *ep1, *ep2; |
| 620 |
|
|
| 625 |
|
scan(); |
| 626 |
|
addekid(ep2, ep1); |
| 627 |
|
addekid(ep2, getE3()); |
| 628 |
< |
#ifdef RCONST |
| 628 |
> |
#ifdef RCONST |
| 629 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
| 630 |
|
ep2 = rconst(ep2); |
| 631 |
|
#endif |
| 637 |
|
|
| 638 |
|
EPNODE * |
| 639 |
|
getE4() /* E4 -> ADDOP E5 */ |
| 640 |
< |
/* E5 */ |
| 640 |
> |
/* E5 */ |
| 641 |
|
{ |
| 642 |
|
register EPNODE *ep1, *ep2; |
| 643 |
|
|
| 665 |
|
|
| 666 |
|
EPNODE * |
| 667 |
|
getE5() /* E5 -> (E1) */ |
| 668 |
< |
/* VAR */ |
| 669 |
< |
/* NUM */ |
| 670 |
< |
/* $N */ |
| 671 |
< |
/* FUNC(E1,..) */ |
| 672 |
< |
/* ARG */ |
| 668 |
> |
/* VAR */ |
| 669 |
> |
/* NUM */ |
| 670 |
> |
/* $N */ |
| 671 |
> |
/* FUNC(E1,..) */ |
| 672 |
> |
/* ARG */ |
| 673 |
|
{ |
| 674 |
< |
int i; |
| 674 |
> |
int i; |
| 675 |
|
char *nam; |
| 676 |
|
register EPNODE *ep1, *ep2; |
| 677 |
|
|
| 684 |
|
return(ep1); |
| 685 |
|
} |
| 686 |
|
|
| 687 |
< |
#ifdef INCHAN |
| 687 |
> |
#ifdef INCHAN |
| 688 |
|
if (nextc == '$') { |
| 689 |
|
scan(); |
| 690 |
|
ep1 = newnode(); |
| 701 |
|
ep1 = NULL; |
| 702 |
|
if (curfunc != NULL) |
| 703 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
| 704 |
< |
ep2 != NULL; i++, ep2 = ep2->sibling) |
| 704 |
> |
ep2 != NULL; i++, ep2 = ep2->sibling) |
| 705 |
|
if (!strcmp(ep2->v.name, nam)) { |
| 706 |
|
ep1 = newnode(); |
| 707 |
|
ep1->type = ARG; |
| 715 |
|
ep1->type = VAR; |
| 716 |
|
ep1->v.ln = varinsert(nam); |
| 717 |
|
} |
| 718 |
< |
#ifdef FUNCTION |
| 718 |
> |
#ifdef FUNCTION |
| 719 |
|
if (nextc == '(') { |
| 720 |
|
ep2 = newnode(); |
| 721 |
|
ep2->type = FUNC; |
| 729 |
|
syntax("')' expected"); |
| 730 |
|
scan(); |
| 731 |
|
} |
| 732 |
< |
#ifndef VARIABLE |
| 732 |
> |
#ifndef VARIABLE |
| 733 |
|
else |
| 734 |
|
syntax("'(' expected"); |
| 735 |
|
#endif |
| 736 |
|
#endif |
| 737 |
< |
#ifdef RCONST |
| 737 |
> |
#ifdef RCONST |
| 738 |
|
if (isconstvar(ep1)) |
| 739 |
|
ep1 = rconst(ep1); |
| 740 |
|
#endif |
| 752 |
|
} |
| 753 |
|
|
| 754 |
|
|
| 755 |
< |
#ifdef RCONST |
| 755 |
> |
#ifdef RCONST |
| 756 |
|
EPNODE * |
| 757 |
|
rconst(epar) /* reduce a constant expression */ |
| 758 |
< |
register EPNODE *epar; |
| 758 |
> |
register EPNODE *epar; |
| 759 |
|
{ |
| 760 |
|
register EPNODE *ep; |
| 761 |
|
|
| 764 |
|
errno = 0; |
| 765 |
|
ep->v.num = evalue(epar); |
| 766 |
|
if (errno) |
| 767 |
< |
syntax("bad constant expression"); |
| 767 |
> |
syntax("bad constant expression"); |
| 768 |
|
epfree(epar); |
| 769 |
|
|
| 770 |
|
return(ep); |
| 772 |
|
|
| 773 |
|
|
| 774 |
|
isconstvar(ep) /* is ep linked to a constant expression? */ |
| 775 |
< |
register EPNODE *ep; |
| 775 |
> |
register EPNODE *ep; |
| 776 |
|
{ |
| 777 |
< |
#ifdef VARIABLE |
| 777 |
> |
#ifdef VARIABLE |
| 778 |
|
register EPNODE *ep1; |
| 779 |
< |
#ifdef FUNCTION |
| 779 |
> |
#ifdef FUNCTION |
| 780 |
|
|
| 781 |
|
if (ep->type == FUNC) { |
| 782 |
|
if (!isconstfun(ep->v.kid)) |
| 792 |
|
ep1 = ep->v.ln->def; |
| 793 |
|
if (ep1 == NULL || ep1->type != ':') |
| 794 |
|
return(0); |
| 795 |
< |
#ifdef FUNCTION |
| 795 |
> |
#ifdef FUNCTION |
| 796 |
|
if (ep1->v.kid->type != SYM) |
| 797 |
|
return(0); |
| 798 |
|
#endif |
| 805 |
|
|
| 806 |
|
#if defined(FUNCTION) && defined(VARIABLE) |
| 807 |
|
isconstfun(ep) /* is ep linked to a constant function? */ |
| 808 |
< |
register EPNODE *ep; |
| 808 |
> |
register EPNODE *ep; |
| 809 |
|
{ |
| 810 |
|
register EPNODE *dp; |
| 811 |
|
register LIBR *lp; |
| 812 |
|
|
| 813 |
|
if (ep->type != VAR) |
| 814 |
|
return(0); |
| 815 |
< |
dp = ep->v.ln->def; |
| 816 |
< |
if (dp != NULL && dp->type != ':') |
| 817 |
< |
return(0); |
| 818 |
< |
if ((dp == NULL || dp->v.kid->type != FUNC) |
| 819 |
< |
&& ((lp = liblookup(ep->v.ln->name)) == NULL |
| 761 |
< |
|| lp->atyp != ':')) |
| 762 |
< |
return(0); |
| 763 |
< |
return(1); |
| 815 |
> |
if ((dp = ep->v.ln->def) != NULL && dp->v.kid->type == FUNC) |
| 816 |
> |
return(dp->type == ':'); |
| 817 |
> |
if ((lp = ep->v.ln->lib) != NULL) |
| 818 |
> |
return(lp->atyp == ':'); |
| 819 |
> |
return(0); |
| 820 |
|
} |
| 821 |
|
#endif |
| 822 |
|
#endif |