26 |
|
#include <math.h> |
27 |
|
#include <stdlib.h> |
28 |
|
|
29 |
+ |
#include "rtmisc.h" |
30 |
+ |
#include "rtio.h" |
31 |
|
#include "rterror.h" |
32 |
|
#include "calcomp.h" |
33 |
|
|
47 |
|
unsigned int esupport = /* what to support */ |
48 |
|
E_VARIABLE | E_FUNCTION ; |
49 |
|
|
50 |
+ |
int eofc = 0; /* optional end-of-file character */ |
51 |
|
int nextc; /* lookahead character */ |
52 |
|
|
53 |
|
double (*eoper[])(EPNODE *) = { /* expression operations */ |
145 |
|
case ':': |
146 |
|
return(epcmp(ep1->v.kid->sibling, ep2->v.kid->sibling)); |
147 |
|
|
148 |
< |
case TICK: |
148 |
> |
case CLKT: |
149 |
|
case SYM: /* should never get this one */ |
150 |
|
return(0); |
151 |
|
|
185 |
|
case NUM: |
186 |
|
case CHAN: |
187 |
|
case ARG: |
188 |
< |
case TICK: |
188 |
> |
case CLKT: |
189 |
|
break; |
190 |
|
|
191 |
|
default: |
294 |
|
lasterrno = errno; |
295 |
|
errno = 0; |
296 |
|
d = pow(evalue(ep1), evalue(ep1->sibling)); |
297 |
< |
#ifdef IEEE |
298 |
< |
if (!finite(d)) |
299 |
< |
errno = EDOM; |
297 |
> |
#ifdef isnan |
298 |
> |
if (errno == 0) { |
299 |
> |
if (isnan(d)) |
300 |
> |
errno = EDOM; |
301 |
> |
else if (isinf(d)) |
302 |
> |
errno = ERANGE; |
303 |
> |
} |
304 |
|
#endif |
305 |
|
if (errno == EDOM || errno == ERANGE) { |
306 |
|
wputs("Illegal power\n"); |
418 |
|
nextc = linbuf[linepos++]; |
419 |
|
if (!lnext) |
420 |
|
lnext = nextc; |
421 |
+ |
if (nextc == eofc) { |
422 |
+ |
nextc = EOF; |
423 |
+ |
break; |
424 |
+ |
} |
425 |
|
if (nextc == '{') { |
426 |
|
scan(); |
427 |
|
while (nextc != '}') |
582 |
|
|
583 |
|
|
584 |
|
EPNODE * |
585 |
< |
getE1(void) /* E1 -> E1 ADDOP E2 */ |
585 |
> |
getE1(void) /* E1 -> E1 ADDOP E2 */ |
586 |
|
/* E2 */ |
587 |
|
{ |
588 |
|
register EPNODE *ep1, *ep2; |
604 |
|
|
605 |
|
|
606 |
|
EPNODE * |
607 |
< |
getE2(void) /* E2 -> E2 MULOP E3 */ |
607 |
> |
getE2(void) /* E2 -> E2 MULOP E3 */ |
608 |
|
/* E3 */ |
609 |
|
{ |
610 |
|
register EPNODE *ep1, *ep2; |
616 |
|
scan(); |
617 |
|
addekid(ep2, ep1); |
618 |
|
addekid(ep2, getE3()); |
619 |
< |
if (esupport&E_RCONST && |
620 |
< |
ep1->type == NUM && ep1->sibling->type == NUM) |
621 |
< |
ep2 = rconst(ep2); |
619 |
> |
if (esupport&E_RCONST) { |
620 |
> |
EPNODE *ep3 = ep1->sibling; |
621 |
> |
if (ep1->type == NUM && ep3->type == NUM) { |
622 |
> |
ep2 = rconst(ep2); |
623 |
> |
} else if (ep3->type == NUM) { |
624 |
> |
if (ep2->type == '/') { |
625 |
> |
if (ep3->v.num == 0) |
626 |
> |
syntax("divide by zero constant"); |
627 |
> |
ep2->type = '*'; /* for speed */ |
628 |
> |
ep3->v.num = 1./ep3->v.num; |
629 |
> |
} else if (ep3->v.num == 0) { |
630 |
> |
ep1->sibling = NULL; /* (E2 * 0) */ |
631 |
> |
epfree(ep2); |
632 |
> |
ep2 = ep3; |
633 |
> |
} |
634 |
> |
} else if (ep1->type == NUM && ep1->v.num == 0) { |
635 |
> |
epfree(ep3); /* (0 * E3) or (0 / E3) */ |
636 |
> |
ep1->sibling = NULL; |
637 |
> |
efree((char *)ep2); |
638 |
> |
ep2 = ep1; |
639 |
> |
} |
640 |
> |
} |
641 |
|
ep1 = ep2; |
642 |
|
} |
643 |
|
return(ep1); |
645 |
|
|
646 |
|
|
647 |
|
EPNODE * |
648 |
< |
getE3(void) /* E3 -> E4 ^ E3 */ |
648 |
> |
getE3(void) /* E3 -> E4 ^ E3 */ |
649 |
|
/* E4 */ |
650 |
|
{ |
651 |
< |
register EPNODE *ep1, *ep2; |
651 |
> |
register EPNODE *ep1, *ep2; |
652 |
|
|
653 |
< |
ep1 = getE4(); |
654 |
< |
if (nextc == '^') { |
653 |
> |
ep1 = getE4(); |
654 |
> |
if (nextc != '^') |
655 |
> |
return(ep1); |
656 |
|
ep2 = newnode(); |
657 |
|
ep2->type = nextc; |
658 |
|
scan(); |
659 |
|
addekid(ep2, ep1); |
660 |
|
addekid(ep2, getE3()); |
661 |
< |
if (esupport&E_RCONST && |
662 |
< |
ep1->type == NUM && ep1->sibling->type == NUM) |
663 |
< |
ep2 = rconst(ep2); |
661 |
> |
if (esupport&E_RCONST) { |
662 |
> |
EPNODE *ep3 = ep1->sibling; |
663 |
> |
if (ep1->type == NUM && ep3->type == NUM) { |
664 |
> |
ep2 = rconst(ep2); |
665 |
> |
} else if (ep1->type == NUM && ep1->v.num == 0) { |
666 |
> |
epfree(ep3); /* (0 ^ E3) */ |
667 |
> |
ep1->sibling = NULL; |
668 |
> |
efree((char *)ep2); |
669 |
> |
ep2 = ep1; |
670 |
> |
} else if ((ep3->type == NUM && ep3->v.num == 0) || |
671 |
> |
(ep1->type == NUM && ep1->v.num == 1)) { |
672 |
> |
epfree(ep2); /* (E4 ^ 0) or (1 ^ E3) */ |
673 |
> |
ep2 = newnode(); |
674 |
> |
ep2->type = NUM; |
675 |
> |
ep2->v.num = 1; |
676 |
> |
} |
677 |
> |
} |
678 |
|
return(ep2); |
634 |
– |
} |
635 |
– |
return(ep1); |
679 |
|
} |
680 |
|
|
681 |
|
|
682 |
|
EPNODE * |
683 |
< |
getE4(void) /* E4 -> ADDOP E5 */ |
683 |
> |
getE4(void) /* E4 -> ADDOP E5 */ |
684 |
|
/* E5 */ |
685 |
|
{ |
686 |
|
register EPNODE *ep1, *ep2; |
693 |
|
return(ep2); |
694 |
|
} |
695 |
|
if (ep2->type == UMINUS) { /* don't generate -(-E5) */ |
696 |
+ |
ep1 = ep2->v.kid; |
697 |
|
efree((char *)ep2); |
698 |
< |
return(ep2->v.kid); |
698 |
> |
return(ep1); |
699 |
|
} |
700 |
|
ep1 = newnode(); |
701 |
|
ep1->type = UMINUS; |