| 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(); |
| 35 |
> |
#ifndef atof |
| 36 |
> |
extern double atof(); |
| 37 |
> |
#endif |
| 38 |
> |
extern double pow(); |
| 39 |
|
extern char *fgets(), *savestr(); |
| 40 |
|
extern char *emalloc(), *ecalloc(); |
| 41 |
|
extern EPNODE *curfunc; |
| 136 |
|
case VAR: |
| 137 |
|
varfree(epar->v.ln); |
| 138 |
|
break; |
| 139 |
– |
#endif |
| 139 |
|
|
| 140 |
|
case SYM: |
| 141 |
|
freestr(epar->v.name); |
| 142 |
|
break; |
| 143 |
+ |
#endif |
| 144 |
|
|
| 145 |
|
case NUM: |
| 146 |
|
case CHAN: |
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
|
| 329 |
< |
scan() /* scan next character */ |
| 329 |
> |
getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ |
| 330 |
> |
char **fnp; |
| 331 |
> |
int *lnp; |
| 332 |
> |
char **spp; |
| 333 |
> |
FILE **fpp; |
| 334 |
|
{ |
| 335 |
+ |
if (fnp != NULL) *fnp = infile; |
| 336 |
+ |
if (lnp != NULL) *lnp = lineno; |
| 337 |
+ |
if (spp != NULL) *spp = linbuf+linepos; |
| 338 |
+ |
if (fpp != NULL) *fpp = infp; |
| 339 |
+ |
} |
| 340 |
+ |
|
| 341 |
+ |
|
| 342 |
+ |
int |
| 343 |
+ |
scan() /* scan next character, return literal next */ |
| 344 |
+ |
{ |
| 345 |
+ |
register int lnext = 0; |
| 346 |
+ |
|
| 347 |
|
do { |
| 348 |
|
if (linbuf[linepos] == '\0') |
| 349 |
|
if (infp == NULL || fgets(linbuf, MAXLINE, infp) == NULL) |
| 355 |
|
} |
| 356 |
|
else |
| 357 |
|
nextc = linbuf[linepos++]; |
| 358 |
+ |
if (!lnext) |
| 359 |
+ |
lnext = nextc; |
| 360 |
|
if (nextc == '{') { |
| 361 |
|
scan(); |
| 362 |
|
while (nextc != '}') |
| 367 |
|
scan(); |
| 368 |
|
} |
| 369 |
|
} while (isspace(nextc)); |
| 370 |
+ |
return(lnext); |
| 371 |
|
} |
| 372 |
|
|
| 373 |
|
|
| 437 |
|
} |
| 438 |
|
|
| 439 |
|
|
| 440 |
+ |
#if defined(VARIABLE) || defined(FUNCTION) |
| 441 |
|
char * |
| 442 |
|
getname() /* scan an identifier */ |
| 443 |
|
{ |
| 444 |
|
static char str[MAXWORD+1]; |
| 445 |
< |
register int i; |
| 445 |
> |
register int i, lnext; |
| 446 |
|
|
| 447 |
< |
for (i = 0; i < MAXWORD && isid(nextc); i++, scan()) |
| 448 |
< |
str[i] = nextc; |
| 447 |
> |
lnext = nextc; |
| 448 |
> |
for (i = 0; i < MAXWORD && isid(lnext); i++, lnext = scan()) |
| 449 |
> |
str[i] = lnext; |
| 450 |
|
str[i] = '\0'; |
| 451 |
+ |
while (isid(lnext)) /* skip rest of name */ |
| 452 |
+ |
lnext = scan(); |
| 453 |
|
|
| 454 |
|
return(str); |
| 455 |
|
} |
| 456 |
+ |
#endif |
| 457 |
|
|
| 458 |
|
|
| 459 |
|
int |
| 460 |
|
getinum() /* scan a positive integer */ |
| 461 |
|
{ |
| 462 |
< |
register int n; |
| 462 |
> |
register int n, lnext; |
| 463 |
|
|
| 464 |
|
n = 0; |
| 465 |
< |
while (isdigit(nextc)) { |
| 466 |
< |
n = n * 10 + nextc - '0'; |
| 467 |
< |
scan(); |
| 465 |
> |
lnext = nextc; |
| 466 |
> |
while (isdigit(lnext)) { |
| 467 |
> |
n = n * 10 + lnext - '0'; |
| 468 |
> |
lnext = scan(); |
| 469 |
|
} |
| 470 |
|
return(n); |
| 471 |
|
} |
| 474 |
|
double |
| 475 |
|
getnum() /* scan a positive float */ |
| 476 |
|
{ |
| 477 |
< |
register int i; |
| 477 |
> |
register int i, lnext; |
| 478 |
|
char str[MAXWORD+1]; |
| 479 |
|
|
| 480 |
|
i = 0; |
| 481 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 482 |
< |
str[i++] = nextc; |
| 483 |
< |
scan(); |
| 481 |
> |
lnext = nextc; |
| 482 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 483 |
> |
str[i++] = lnext; |
| 484 |
> |
lnext = scan(); |
| 485 |
|
} |
| 486 |
< |
if (nextc == '.' && i < MAXWORD) { |
| 487 |
< |
str[i++] = nextc; |
| 488 |
< |
scan(); |
| 489 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 490 |
< |
str[i++] = nextc; |
| 491 |
< |
scan(); |
| 486 |
> |
if (lnext == '.' && i < MAXWORD) { |
| 487 |
> |
str[i++] = lnext; |
| 488 |
> |
lnext = scan(); |
| 489 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 490 |
> |
str[i++] = lnext; |
| 491 |
> |
lnext = scan(); |
| 492 |
|
} |
| 493 |
|
} |
| 494 |
< |
if ((nextc == 'e' || nextc == 'E') && i < MAXWORD) { |
| 495 |
< |
str[i++] = nextc; |
| 496 |
< |
scan(); |
| 497 |
< |
if ((nextc == '-' || nextc == '+') && i < MAXWORD) { |
| 498 |
< |
str[i++] = nextc; |
| 499 |
< |
scan(); |
| 494 |
> |
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
| 495 |
> |
str[i++] = lnext; |
| 496 |
> |
lnext = scan(); |
| 497 |
> |
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
| 498 |
> |
str[i++] = lnext; |
| 499 |
> |
lnext = scan(); |
| 500 |
|
} |
| 501 |
< |
while (isdigit(nextc) && i < MAXWORD) { |
| 502 |
< |
str[i++] = nextc; |
| 503 |
< |
scan(); |
| 501 |
> |
while (isdigit(lnext) && i < MAXWORD) { |
| 502 |
> |
str[i++] = lnext; |
| 503 |
> |
lnext = scan(); |
| 504 |
|
} |
| 505 |
|
} |
| 506 |
|
str[i] = '\0'; |
| 591 |
|
ep2->v.num = -ep2->v.num; |
| 592 |
|
return(ep2); |
| 593 |
|
} |
| 594 |
+ |
if (ep2->type == UMINUS) { /* don't generate -(-E5) */ |
| 595 |
+ |
efree((char *)ep2); |
| 596 |
+ |
return(ep2->v.kid); |
| 597 |
+ |
} |
| 598 |
|
ep1 = newnode(); |
| 599 |
|
ep1->type = UMINUS; |
| 600 |
|
addekid(ep1, ep2); |
| 615 |
|
/* ARG */ |
| 616 |
|
{ |
| 617 |
|
int i; |
| 618 |
+ |
char *nam; |
| 619 |
|
register EPNODE *ep1, *ep2; |
| 620 |
|
|
| 621 |
|
if (nextc == '(') { |
| 638 |
|
#endif |
| 639 |
|
|
| 640 |
|
#if defined(VARIABLE) || defined(FUNCTION) |
| 641 |
< |
if (isalpha(nextc)) { |
| 642 |
< |
ep1 = newnode(); |
| 612 |
< |
ep1->type = VAR; |
| 613 |
< |
ep1->v.ln = varinsert(getname()); |
| 614 |
< |
|
| 641 |
> |
if (isalpha(nextc) || nextc == CNTXMARK) { |
| 642 |
> |
nam = getname(); |
| 643 |
|
#if defined(VARIABLE) && defined(FUNCTION) |
| 644 |
+ |
ep1 = NULL; |
| 645 |
|
if (curfunc != NULL) |
| 646 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
| 647 |
|
ep2 != NULL; i++, ep2 = ep2->sibling) |
| 648 |
< |
if (!strcmp(ep2->v.name, ep1->v.ln->name)) { |
| 620 |
< |
epfree(ep1); |
| 648 |
> |
if (!strcmp(ep2->v.name, nam)) { |
| 649 |
|
ep1 = newnode(); |
| 650 |
|
ep1->type = ARG; |
| 651 |
|
ep1->v.chan = i; |
| 652 |
|
break; |
| 653 |
|
} |
| 654 |
+ |
if (ep1 == NULL) |
| 655 |
|
#endif |
| 656 |
+ |
{ |
| 657 |
+ |
ep1 = newnode(); |
| 658 |
+ |
ep1->type = VAR; |
| 659 |
+ |
ep1->v.ln = varinsert(nam); |
| 660 |
+ |
} |
| 661 |
|
#ifdef FUNCTION |
| 662 |
|
if (nextc == '(') { |
| 663 |
|
ep2 = newnode(); |
| 714 |
|
} |
| 715 |
|
|
| 716 |
|
|
| 717 |
< |
isconstvar(ep) /* is ep linked to a constant? */ |
| 717 |
> |
isconstvar(ep) /* is ep linked to a constant expression? */ |
| 718 |
|
register EPNODE *ep; |
| 719 |
|
{ |
| 720 |
|
#ifdef VARIABLE |
| 721 |
|
register EPNODE *ep1; |
| 688 |
– |
|
| 722 |
|
#ifdef FUNCTION |
| 723 |
+ |
|
| 724 |
|
if (ep->type == FUNC) { |
| 725 |
< |
if (ep->v.kid->type != VAR) |
| 726 |
< |
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); |
| 725 |
> |
if (!isconstfun(ep->v.kid)) |
| 726 |
> |
return(0); |
| 727 |
|
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
| 728 |
< |
if (ep1->type != NUM) |
| 728 |
> |
if (ep1->type != NUM && !isconstfun(ep1)) |
| 729 |
|
return(0); |
| 730 |
|
return(1); |
| 731 |
|
} |
| 744 |
|
return(ep->type == FUNC); |
| 745 |
|
#endif |
| 746 |
|
} |
| 747 |
+ |
|
| 748 |
+ |
|
| 749 |
+ |
#if defined(FUNCTION) && defined(VARIABLE) |
| 750 |
+ |
isconstfun(ep) /* is ep linked to a constant function? */ |
| 751 |
+ |
register EPNODE *ep; |
| 752 |
+ |
{ |
| 753 |
+ |
register EPNODE *dp; |
| 754 |
+ |
register LIBR *lp; |
| 755 |
+ |
|
| 756 |
+ |
if (ep->type != VAR) |
| 757 |
+ |
return(0); |
| 758 |
+ |
if ((dp = ep->v.ln->def) != NULL && dp->v.kid->type == FUNC) |
| 759 |
+ |
return(dp->type == ':'); |
| 760 |
+ |
if ((lp = ep->v.ln->lib) != NULL) |
| 761 |
+ |
return(lp->atyp == ':'); |
| 762 |
+ |
return(0); |
| 763 |
+ |
} |
| 764 |
+ |
#endif |
| 765 |
|
#endif |