| 37 |
|
|
| 38 |
|
#define envalue(ep) ((ep)->type==NUM ? (ep)->v.num : evalue(ep)) |
| 39 |
|
|
| 40 |
< |
static double euminus(EPNODE *), eargument(EPNODE *), enumber(EPNODE *); |
| 40 |
> |
static double euminus(EPNODE *), enumber(EPNODE *); |
| 41 |
|
static double echannel(EPNODE *); |
| 42 |
|
static double eadd(EPNODE *), esubtr(EPNODE *), |
| 43 |
|
emult(EPNODE *), edivi(EPNODE *), |
| 92 |
|
EPNODE *ep; |
| 93 |
|
|
| 94 |
|
initstr(expr, NULL, 0); |
| 95 |
< |
curfunc = NULL; |
| 95 |
> |
ecurfunc = NULL; |
| 96 |
|
ep = getE1(); |
| 97 |
|
if (nextc != EOF) |
| 98 |
< |
syntax("unexpected character"); |
| 98 |
> |
esyntax("unexpected character"); |
| 99 |
|
return(ep); |
| 100 |
|
} |
| 101 |
|
|
| 208 |
|
} |
| 209 |
|
if (frep) |
| 210 |
|
efree(epar); |
| 211 |
+ |
else |
| 212 |
+ |
memset(epar, 0, sizeof(EPNODE)); |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
|
| 220 |
|
{ |
| 221 |
|
EPNODE *ep; |
| 222 |
|
|
| 223 |
< |
if (epar->nkids < 0) /* we don't really handle this properly */ |
| 224 |
< |
epar->nkids *= -1; |
| 223 |
> |
if (epar->nkids < 0) /* can't handle array allocations */ |
| 224 |
> |
return; |
| 225 |
|
|
| 226 |
|
for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) |
| 227 |
< |
while (ep->type == epar->type) { |
| 227 |
> |
while (ep->type == epar->type && ep->nkids > 0) { |
| 228 |
|
EPNODE *ep1 = ep->v.kid; |
| 229 |
|
while (ep1->sibling != NULL) |
| 230 |
|
ep1 = ep1->sibling; |
| 231 |
|
ep1->sibling = ep->sibling; |
| 232 |
< |
epar->nkids += nekids(ep) - 1; |
| 232 |
> |
epar->nkids += ep->nkids - 1; |
| 233 |
|
ep1 = ep->v.kid; |
| 234 |
|
*ep = *ep1; |
| 235 |
|
efree(ep1); /* not epfree()! */ |
| 238 |
|
|
| 239 |
|
|
| 240 |
|
void |
| 241 |
< |
epoptimize( /* flatten operations and lists -> arrays */ |
| 241 |
> |
epoptimize( /* flatten operations, lists -> arrays */ |
| 242 |
|
EPNODE *epar |
| 243 |
|
) |
| 244 |
|
{ |
| 245 |
|
EPNODE *ep; |
| 246 |
|
|
| 247 |
|
if ((epar->type == '+') | (epar->type == '*')) |
| 248 |
< |
epflatten(epar); /* commutative & associative */ |
| 248 |
> |
epflatten(epar); /* flatten associative operations */ |
| 249 |
|
|
| 250 |
|
if (epar->nkids) /* do children if any */ |
| 251 |
|
for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) |
| 268 |
|
|
| 269 |
|
/* the following used to be a switch */ |
| 270 |
|
static double |
| 269 |
– |
eargument( |
| 270 |
– |
EPNODE *ep |
| 271 |
– |
) |
| 272 |
– |
{ |
| 273 |
– |
return(argument(ep->v.chan)); |
| 274 |
– |
} |
| 275 |
– |
|
| 276 |
– |
static double |
| 271 |
|
enumber( |
| 272 |
|
EPNODE *ep |
| 273 |
|
) |
| 340 |
|
) |
| 341 |
|
{ |
| 342 |
|
EPNODE *ep1 = ep->v.kid; |
| 343 |
< |
EPNODE *ep2 = ep1->sibling; |
| 350 |
< |
double d; |
| 343 |
> |
double den = evalue(ep1->sibling); |
| 344 |
|
|
| 345 |
< |
d = envalue(ep2); |
| 353 |
< |
if (d == 0.0) { |
| 345 |
> |
if (den == 0.0) { |
| 346 |
|
wputs("Division by zero\n"); |
| 347 |
|
errno = ERANGE; |
| 348 |
|
return(0.0); |
| 349 |
|
} |
| 350 |
< |
return(envalue(ep1) / d); |
| 350 |
> |
return(envalue(ep1) / den); |
| 351 |
|
} |
| 352 |
|
|
| 353 |
|
static double |
| 423 |
|
lineno = ln; |
| 424 |
|
linepos = 0; |
| 425 |
|
inpbuf[0] = '\0'; |
| 426 |
< |
scan(); |
| 426 |
> |
escan(); |
| 427 |
|
} |
| 428 |
|
|
| 429 |
|
|
| 439 |
|
lineno = ln; |
| 440 |
|
linbuf = s; |
| 441 |
|
linepos = 0; |
| 442 |
< |
scan(); |
| 442 |
> |
escan(); |
| 443 |
|
} |
| 444 |
|
|
| 445 |
|
|
| 459 |
|
|
| 460 |
|
|
| 461 |
|
int |
| 462 |
< |
scan(void) /* scan next character, return literal next */ |
| 462 |
> |
escan(void) /* scan next character, return literal next */ |
| 463 |
|
{ |
| 464 |
|
int lnext = 0; |
| 465 |
|
|
| 481 |
|
break; |
| 482 |
|
} |
| 483 |
|
if (nextc == '{') { |
| 484 |
< |
scan(); |
| 484 |
> |
escan(); |
| 485 |
|
while (nextc != '}') |
| 486 |
|
if (nextc == EOF) |
| 487 |
< |
syntax("'}' expected"); |
| 487 |
> |
esyntax("'}' expected"); |
| 488 |
|
else |
| 489 |
< |
scan(); |
| 490 |
< |
scan(); |
| 489 |
> |
escan(); |
| 490 |
> |
escan(); |
| 491 |
|
} |
| 492 |
|
} while (isspace(nextc)); |
| 493 |
|
return(lnext); |
| 522 |
|
|
| 523 |
|
|
| 524 |
|
void |
| 525 |
< |
syntax( /* report syntax error and quit */ |
| 525 |
> |
esyntax( /* report syntax error and quit */ |
| 526 |
|
char *err |
| 527 |
|
) |
| 528 |
|
{ |
| 554 |
|
EPNODE *ek |
| 555 |
|
) |
| 556 |
|
{ |
| 557 |
< |
if (ep->nkids < 0) /* we don't really handle this properly */ |
| 558 |
< |
ep->nkids *= -1; |
| 557 |
> |
if (ep->nkids < 0) { |
| 558 |
> |
eputs("Cannot add kid to EPNODE array\n"); |
| 559 |
> |
quit(1); |
| 560 |
> |
} |
| 561 |
|
ep->nkids++; |
| 562 |
|
if (ep->v.kid == NULL) |
| 563 |
|
ep->v.kid = ek; |
| 577 |
|
int i, lnext; |
| 578 |
|
|
| 579 |
|
lnext = nextc; |
| 580 |
< |
for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = scan()) |
| 580 |
> |
for (i = 0; i < RMAXWORD && isid(lnext); i++, lnext = escan()) |
| 581 |
|
str[i] = lnext; |
| 582 |
|
str[i] = '\0'; |
| 583 |
|
while (isid(lnext)) /* skip rest of name */ |
| 584 |
< |
lnext = scan(); |
| 584 |
> |
lnext = escan(); |
| 585 |
|
|
| 586 |
|
return(str); |
| 587 |
|
} |
| 596 |
|
lnext = nextc; |
| 597 |
|
while (isdigit(lnext)) { |
| 598 |
|
n = n * 10 + lnext - '0'; |
| 599 |
< |
lnext = scan(); |
| 599 |
> |
lnext = escan(); |
| 600 |
|
} |
| 601 |
|
return(n); |
| 602 |
|
} |
| 612 |
|
lnext = nextc; |
| 613 |
|
while (isdigit(lnext) && i < RMAXWORD) { |
| 614 |
|
str[i++] = lnext; |
| 615 |
< |
lnext = scan(); |
| 615 |
> |
lnext = escan(); |
| 616 |
|
} |
| 617 |
|
if ((lnext == '.') & (i < RMAXWORD)) { |
| 618 |
|
str[i++] = lnext; |
| 619 |
< |
lnext = scan(); |
| 619 |
> |
lnext = escan(); |
| 620 |
|
if (i == 1 && !isdigit(lnext)) |
| 621 |
< |
syntax("badly formed number"); |
| 621 |
> |
esyntax("badly formed number"); |
| 622 |
|
while (isdigit(lnext) && i < RMAXWORD) { |
| 623 |
|
str[i++] = lnext; |
| 624 |
< |
lnext = scan(); |
| 624 |
> |
lnext = escan(); |
| 625 |
|
} |
| 626 |
|
} |
| 627 |
|
if ((lnext == 'e') | (lnext == 'E') && i < RMAXWORD) { |
| 628 |
|
str[i++] = lnext; |
| 629 |
< |
lnext = scan(); |
| 629 |
> |
lnext = escan(); |
| 630 |
|
if ((lnext == '-') | (lnext == '+') && i < RMAXWORD) { |
| 631 |
|
str[i++] = lnext; |
| 632 |
< |
lnext = scan(); |
| 632 |
> |
lnext = escan(); |
| 633 |
|
} |
| 634 |
|
if (!isdigit(lnext)) |
| 635 |
< |
syntax("missing exponent"); |
| 635 |
> |
esyntax("missing exponent"); |
| 636 |
|
while (isdigit(lnext) && i < RMAXWORD) { |
| 637 |
|
str[i++] = lnext; |
| 638 |
< |
lnext = scan(); |
| 638 |
> |
lnext = escan(); |
| 639 |
|
} |
| 640 |
|
} |
| 641 |
|
str[i] = '\0'; |
| 654 |
|
while ((nextc == '+') | (nextc == '-')) { |
| 655 |
|
ep2 = newnode(); |
| 656 |
|
ep2->type = nextc; |
| 657 |
< |
scan(); |
| 657 |
> |
escan(); |
| 658 |
|
addekid(ep2, ep1); |
| 659 |
|
addekid(ep2, getE2()); |
| 660 |
|
if (esupport&E_RCONST && |
| 676 |
|
while ((nextc == '*') | (nextc == '/')) { |
| 677 |
|
ep2 = newnode(); |
| 678 |
|
ep2->type = nextc; |
| 679 |
< |
scan(); |
| 679 |
> |
escan(); |
| 680 |
|
addekid(ep2, ep1); |
| 681 |
|
addekid(ep2, getE3()); |
| 682 |
|
if (esupport&E_RCONST) { |
| 686 |
|
} else if (ep3->type == NUM) { |
| 687 |
|
if (ep2->type == '/') { |
| 688 |
|
if (ep3->v.num == 0) |
| 689 |
< |
syntax("divide by zero constant"); |
| 689 |
> |
esyntax("divide by zero constant"); |
| 690 |
|
ep2->type = '*'; /* for speed */ |
| 691 |
|
ep3->v.num = 1./ep3->v.num; |
| 692 |
|
} else if (ep3->v.num == 0) { |
| 718 |
|
return(ep1); |
| 719 |
|
ep2 = newnode(); |
| 720 |
|
ep2->type = nextc; |
| 721 |
< |
scan(); |
| 721 |
> |
escan(); |
| 722 |
|
addekid(ep2, ep1); |
| 723 |
|
addekid(ep2, getE3()); |
| 724 |
|
if (esupport&E_RCONST) { |
| 732 |
|
ep2 = ep1; |
| 733 |
|
} else if ((ep3->type == NUM && ep3->v.num == 0) | |
| 734 |
|
(ep1->type == NUM && ep1->v.num == 1)) { |
| 735 |
< |
epfree(ep2,1); /* (E4 ^ 0) or (1 ^ E3) */ |
| 742 |
< |
ep2 = newnode(); |
| 735 |
> |
epfree(ep2,0); /* (E4 ^ 0) or (1 ^ E3) */ |
| 736 |
|
ep2->type = NUM; |
| 737 |
|
ep2->v.num = 1; |
| 738 |
|
} else if (ep3->type == NUM && ep3->v.num == 1) { |
| 753 |
|
EPNODE *ep1, *ep2; |
| 754 |
|
|
| 755 |
|
if (nextc == '-') { |
| 756 |
< |
scan(); |
| 756 |
> |
escan(); |
| 757 |
|
ep2 = getE5(); |
| 758 |
|
if (ep2->type == NUM) { |
| 759 |
|
ep2->v.num = -ep2->v.num; |
| 770 |
|
return(ep1); |
| 771 |
|
} |
| 772 |
|
if (nextc == '+') |
| 773 |
< |
scan(); |
| 773 |
> |
escan(); |
| 774 |
|
return(getE5()); |
| 775 |
|
} |
| 776 |
|
|
| 788 |
|
EPNODE *ep1, *ep2; |
| 789 |
|
|
| 790 |
|
if (nextc == '(') { |
| 791 |
< |
scan(); |
| 791 |
> |
escan(); |
| 792 |
|
ep1 = getE1(); |
| 793 |
|
if (nextc != ')') |
| 794 |
< |
syntax("')' expected"); |
| 795 |
< |
scan(); |
| 794 |
> |
esyntax("')' expected"); |
| 795 |
> |
escan(); |
| 796 |
|
return(ep1); |
| 797 |
|
} |
| 798 |
|
if (esupport&E_INCHAN && nextc == '$') { |
| 799 |
< |
scan(); |
| 799 |
> |
escan(); |
| 800 |
|
ep1 = newnode(); |
| 801 |
|
ep1->type = CHAN; |
| 802 |
|
ep1->v.chan = getinum(); |
| 807 |
|
nam = getname(); |
| 808 |
|
ep1 = NULL; |
| 809 |
|
if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION) |
| 810 |
< |
&& curfunc != NULL) |
| 811 |
< |
for (i = 1, ep2 = curfunc->v.kid->sibling; |
| 810 |
> |
&& ecurfunc != NULL) |
| 811 |
> |
for (i = 1, ep2 = ecurfunc->v.kid->sibling; |
| 812 |
|
ep2 != NULL; i++, ep2 = ep2->sibling) |
| 813 |
|
if (!strcmp(ep2->v.name, nam)) { |
| 814 |
|
ep1 = newnode(); |
| 827 |
|
addekid(ep2, ep1); |
| 828 |
|
ep1 = ep2; |
| 829 |
|
do { |
| 830 |
< |
scan(); |
| 830 |
> |
escan(); |
| 831 |
|
addekid(ep1, getE1()); |
| 832 |
|
} while (nextc == ','); |
| 833 |
|
if (nextc != ')') |
| 834 |
< |
syntax("')' expected"); |
| 835 |
< |
scan(); |
| 834 |
> |
esyntax("')' expected"); |
| 835 |
> |
escan(); |
| 836 |
|
} else if (!(esupport&E_VARIABLE)) |
| 837 |
< |
syntax("'(' expected"); |
| 837 |
> |
esyntax("'(' expected"); |
| 838 |
|
if (esupport&E_RCONST && isconstvar(ep1)) |
| 839 |
|
ep1 = rconst(ep1); |
| 840 |
|
return(ep1); |
| 845 |
|
ep1->v.num = getnum(); |
| 846 |
|
return(ep1); |
| 847 |
|
} |
| 848 |
< |
syntax("unexpected character"); |
| 848 |
> |
esyntax("unexpected character"); |
| 849 |
|
return NULL; /* pro forma return */ |
| 850 |
|
} |
| 851 |
|
|
| 862 |
|
errno = 0; |
| 863 |
|
ep->v.num = evalue(epar); |
| 864 |
|
if ((errno == EDOM) | (errno == ERANGE)) |
| 865 |
< |
syntax("bad constant expression"); |
| 865 |
> |
esyntax("bad constant expression"); |
| 866 |
|
epfree(epar,1); |
| 867 |
|
|
| 868 |
|
return(ep); |
| 901 |
|
) |
| 902 |
|
{ |
| 903 |
|
EPNODE *dp; |
| 904 |
< |
LIBR *lp; |
| 904 |
> |
ELIBR *lp; |
| 905 |
|
|
| 906 |
|
if (ep->type != VAR) |
| 907 |
|
return(0); |