| 1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 27 |
|
#include "calcomp.h" |
| 28 |
|
|
| 29 |
|
#define MAXLINE 256 /* maximum line length */ |
| 30 |
– |
#define MAXWORD 64 /* maximum word length */ |
| 30 |
|
|
| 31 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
| 32 |
|
|
| 34 |
– |
#define isid(c) (isalnum(c) || (c) == '_' || (c) == '.') |
| 35 |
– |
|
| 33 |
|
#define isdecimal(c) (isdigit(c) || (c) == '.') |
| 34 |
|
|
| 35 |
|
extern double atof(), pow(); |
| 323 |
|
} |
| 324 |
|
|
| 325 |
|
|
| 326 |
< |
scan() /* scan next character */ |
| 326 |
> |
getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ |
| 327 |
> |
char **fnp; |
| 328 |
> |
int *lnp; |
| 329 |
> |
char **spp; |
| 330 |
> |
FILE **fpp; |
| 331 |
|
{ |
| 332 |
+ |
if (fnp != NULL) *fnp = infile; |
| 333 |
+ |
if (lnp != NULL) *lnp = lineno; |
| 334 |
+ |
if (spp != NULL) *spp = linbuf+linepos; |
| 335 |
+ |
if (fpp != NULL) *fpp = infp; |
| 336 |
+ |
} |
| 337 |
+ |
|
| 338 |
+ |
|
| 339 |
+ |
int |
| 340 |
+ |
scan() /* scan next character, return literal next */ |
| 341 |
+ |
{ |
| 342 |
+ |
register int lnext = 0; |
| 343 |
+ |
|
| 344 |
|
do { |
| 345 |
|
if (linbuf[linepos] == '\0') |
| 346 |
|
if (infp == NULL || fgets(linbuf, MAXLINE, infp) == NULL) |
| 352 |
|
} |
| 353 |
|
else |
| 354 |
|
nextc = linbuf[linepos++]; |
| 355 |
+ |
if (!lnext) |
| 356 |
+ |
lnext = nextc; |
| 357 |
|
if (nextc == '{') { |
| 358 |
|
scan(); |
| 359 |
|
while (nextc != '}') |
| 364 |
|
scan(); |
| 365 |
|
} |
| 366 |
|
} while (isspace(nextc)); |
| 367 |
+ |
return(lnext); |
| 368 |
|
} |
| 369 |
|
|
| 370 |
|
|
| 438 |
|
getname() /* scan an identifier */ |
| 439 |
|
{ |
| 440 |
|
static char str[MAXWORD+1]; |
| 441 |
< |
register int i; |
| 441 |
> |
register int i, lnext; |
| 442 |
|
|
| 443 |
< |
for (i = 0; i < MAXWORD && isid(nextc); i++, scan()) |
| 444 |
< |
str[i] = nextc; |
| 443 |
> |
lnext = nextc; |
| 444 |
> |
for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan()) |
| 445 |
> |
str[i] = lnext; |
| 446 |
|
str[i] = '\0'; |
| 447 |
+ |
while (isid(lnext)) /* skip rest of name */ |
| 448 |
+ |
lnext = scan(); |
| 449 |
|
|
| 450 |
|
return(str); |
| 451 |
|
} |
| 454 |
|
int |
| 455 |
|
getinum() /* scan a positive integer */ |
| 456 |
|
{ |
| 457 |
< |
register int n; |
| 457 |
> |
register int n, lnext; |
| 458 |
|
|
| 459 |
|
n = 0; |
| 460 |
< |
while (isdigit(nextc)) { |
| 461 |
< |
n = n * 10 + nextc - '0'; |
| 462 |
< |
scan(); |
| 460 |
> |
lnext = nextc; |
| 461 |
> |
while (isdigit(lnext)) { |
| 462 |
> |
n = n * 10 + lnext - '0'; |
| 463 |
> |
lnext = scan(); |
| 464 |
|
} |
| 465 |
|
return(n); |
| 466 |
|
} |
| 469 |
|
double |
| 470 |
|
getnum() /* scan a positive float */ |
| 471 |
|
{ |
| 472 |
< |
register int i; |
| 472 |
> |
register int i, lnext; |
| 473 |
|
char str[MAXWORD+1]; |
| 474 |
|
|
| 475 |
|
i = 0; |
| 476 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 477 |
< |
str[i++] = nextc; |
| 478 |
< |
scan(); |
| 476 |
> |
lnext = nextc; |
| 477 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 478 |
> |
str[i++] = lnext; |
| 479 |
> |
lnext = scan(); |
| 480 |
|
} |
| 481 |
< |
if (nextc == '.' && i < MAXWORD) { |
| 482 |
< |
str[i++] = nextc; |
| 483 |
< |
scan(); |
| 484 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 485 |
< |
str[i++] = nextc; |
| 486 |
< |
scan(); |
| 481 |
> |
if (lnext == '.' && i < MAXWORD) { |
| 482 |
> |
str[i++] = lnext; |
| 483 |
> |
lnext = scan(); |
| 484 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 485 |
> |
str[i++] = lnext; |
| 486 |
> |
lnext = scan(); |
| 487 |
|
} |
| 488 |
|
} |
| 489 |
< |
if ((nextc == 'e' || nextc == 'E') && i < MAXWORD) { |
| 490 |
< |
str[i++] = nextc; |
| 491 |
< |
scan(); |
| 492 |
< |
if ((nextc == '-' || nextc == '+') && i < MAXWORD) { |
| 493 |
< |
str[i++] = nextc; |
| 494 |
< |
scan(); |
| 489 |
> |
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
| 490 |
> |
str[i++] = lnext; |
| 491 |
> |
lnext = scan(); |
| 492 |
> |
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
| 493 |
> |
str[i++] = lnext; |
| 494 |
> |
lnext = scan(); |
| 495 |
|
} |
| 496 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 497 |
< |
str[i++] = nextc; |
| 498 |
< |
scan(); |
| 496 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 497 |
> |
str[i++] = lnext; |
| 498 |
> |
lnext = scan(); |
| 499 |
|
} |
| 500 |
|
} |
| 501 |
|
str[i] = '\0'; |
| 586 |
|
ep2->v.num = -ep2->v.num; |
| 587 |
|
return(ep2); |
| 588 |
|
} |
| 589 |
+ |
if (ep2->type == UMINUS) { /* don't generate -(-E5) */ |
| 590 |
+ |
efree((char *)ep2); |
| 591 |
+ |
return(ep2->v.kid); |
| 592 |
+ |
} |
| 593 |
|
ep1 = newnode(); |
| 594 |
|
ep1->type = UMINUS; |
| 595 |
|
addekid(ep1, ep2); |
| 610 |
|
/* ARG */ |
| 611 |
|
{ |
| 612 |
|
int i; |
| 613 |
+ |
char *nam; |
| 614 |
|
register EPNODE *ep1, *ep2; |
| 615 |
|
|
| 616 |
|
if (nextc == '(') { |
| 633 |
|
#endif |
| 634 |
|
|
| 635 |
|
#if defined(VARIABLE) || defined(FUNCTION) |
| 636 |
< |
if (isalpha(nextc)) { |
| 637 |
< |
ep1 = newnode(); |
| 612 |
< |
ep1->type = VAR; |
| 613 |
< |
ep1->v.ln = varinsert(getname()); |
| 614 |
< |
|
| 636 |
> |
if (isalpha(nextc) || nextc == CNTXMARK) { |
| 637 |
> |
nam = getname(); |
| 638 |
|
#if defined(VARIABLE) && defined(FUNCTION) |
| 639 |
+ |
ep1 = NULL; |
| 640 |
|
if (curfunc != NULL) |
| 641 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
| 642 |
|
ep2 != NULL; i++, ep2 = ep2->sibling) |
| 643 |
< |
if (!strcmp(ep2->v.name, ep1->v.ln->name)) { |
| 620 |
< |
epfree(ep1); |
| 643 |
> |
if (!strcmp(ep2->v.name, nam)) { |
| 644 |
|
ep1 = newnode(); |
| 645 |
|
ep1->type = ARG; |
| 646 |
|
ep1->v.chan = i; |
| 647 |
|
break; |
| 648 |
|
} |
| 649 |
+ |
if (ep1 == NULL) |
| 650 |
|
#endif |
| 651 |
+ |
{ |
| 652 |
+ |
ep1 = newnode(); |
| 653 |
+ |
ep1->type = VAR; |
| 654 |
+ |
ep1->v.ln = varinsert(nam); |
| 655 |
+ |
} |
| 656 |
|
#ifdef FUNCTION |
| 657 |
|
if (nextc == '(') { |
| 658 |
|
ep2 = newnode(); |
| 709 |
|
} |
| 710 |
|
|
| 711 |
|
|
| 712 |
< |
isconstvar(ep) /* is ep linked to a constant? */ |
| 712 |
> |
isconstvar(ep) /* is ep linked to a constant expression? */ |
| 713 |
|
register EPNODE *ep; |
| 714 |
|
{ |
| 715 |
|
#ifdef VARIABLE |
| 716 |
|
register EPNODE *ep1; |
| 688 |
– |
|
| 717 |
|
#ifdef FUNCTION |
| 718 |
+ |
|
| 719 |
|
if (ep->type == FUNC) { |
| 720 |
< |
if (ep->v.kid->type != VAR) |
| 721 |
< |
return(0); |
| 693 |
< |
ep1 = ep->v.kid->v.ln->def; |
| 694 |
< |
if (ep1 != NULL && ep1->type != ':') |
| 695 |
< |
return(0); |
| 696 |
< |
if ((ep1 == NULL || ep1->v.kid->type != FUNC) |
| 697 |
< |
&& liblookup(ep->v.kid->v.ln->name) == NULL) |
| 698 |
< |
return(0); |
| 720 |
> |
if (!isconstfun(ep->v.kid)) |
| 721 |
> |
return(0); |
| 722 |
|
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
| 723 |
< |
if (ep1->type != NUM) |
| 723 |
> |
if (ep1->type != NUM && !isconstfun(ep1)) |
| 724 |
|
return(0); |
| 725 |
|
return(1); |
| 726 |
|
} |
| 739 |
|
return(ep->type == FUNC); |
| 740 |
|
#endif |
| 741 |
|
} |
| 742 |
+ |
|
| 743 |
+ |
|
| 744 |
+ |
#if defined(FUNCTION) && defined(VARIABLE) |
| 745 |
+ |
isconstfun(ep) /* is ep linked to a constant function? */ |
| 746 |
+ |
register EPNODE *ep; |
| 747 |
+ |
{ |
| 748 |
+ |
register EPNODE *dp; |
| 749 |
+ |
register LIBR *lp; |
| 750 |
+ |
|
| 751 |
+ |
if (ep->type != VAR) |
| 752 |
+ |
return(0); |
| 753 |
+ |
dp = ep->v.ln->def; |
| 754 |
+ |
if (dp != NULL && dp->type != ':') |
| 755 |
+ |
return(0); |
| 756 |
+ |
if ((dp == NULL || dp->v.kid->type != FUNC) |
| 757 |
+ |
&& ((lp = liblookup(ep->v.ln->name)) == NULL |
| 758 |
+ |
|| lp->atyp != ':')) |
| 759 |
+ |
return(0); |
| 760 |
+ |
return(1); |
| 761 |
+ |
} |
| 762 |
+ |
#endif |
| 763 |
|
#endif |