1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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 |
38 |
|
extern double atof(); |
39 |
|
#endif |
38 |
– |
extern double pow(); |
40 |
|
extern char *fgets(), *savestr(); |
41 |
|
extern char *emalloc(), *ecalloc(); |
42 |
|
extern EPNODE *curfunc; |
44 |
|
static double euminus(), echannel(), eargument(), enumber(); |
45 |
|
static double eadd(), esubtr(), emult(), edivi(), epow(); |
46 |
|
static double ebotch(); |
46 |
– |
extern int errno; |
47 |
|
|
48 |
|
int nextc; /* lookahead character */ |
49 |
|
|
50 |
< |
double (*eoper[])() = { /* expression operations */ |
50 |
> |
double (*eoper[])() = { /* expression operations */ |
51 |
|
ebotch, |
52 |
< |
#ifdef VARIABLE |
52 |
> |
#ifdef VARIABLE |
53 |
|
evariable, |
54 |
|
#else |
55 |
|
ebotch, |
56 |
|
#endif |
57 |
|
enumber, |
58 |
|
euminus, |
59 |
< |
#ifdef INCHAN |
59 |
> |
#ifdef INCHAN |
60 |
|
echannel, |
61 |
|
#else |
62 |
|
ebotch, |
63 |
|
#endif |
64 |
< |
#ifdef FUNCTION |
64 |
> |
#ifdef FUNCTION |
65 |
|
efunc, |
66 |
|
eargument, |
67 |
|
#else |
126 |
|
|
127 |
|
|
128 |
|
epfree(epar) /* free a parse tree */ |
129 |
< |
register EPNODE *epar; |
129 |
> |
register EPNODE *epar; |
130 |
|
{ |
131 |
< |
register EPNODE *ep; |
131 |
> |
register EPNODE *ep, *epn; |
132 |
|
|
133 |
|
switch (epar->type) { |
134 |
|
|
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: |
149 |
|
break; |
150 |
|
|
151 |
|
default: |
152 |
< |
for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) |
152 |
> |
for (ep = epar->v.kid; ep != NULL; ep = epn) { |
153 |
> |
epn = ep->sibling; |
154 |
|
epfree(ep); |
155 |
+ |
} |
156 |
|
break; |
157 |
|
|
158 |
|
} |
161 |
|
} |
162 |
|
|
163 |
|
/* the following used to be a switch */ |
164 |
< |
#ifdef FUNCTION |
164 |
> |
#ifdef FUNCTION |
165 |
|
static double |
166 |
|
eargument(ep) |
167 |
< |
EPNODE *ep; |
167 |
> |
EPNODE *ep; |
168 |
|
{ |
169 |
|
return(argument(ep->v.chan)); |
170 |
|
} |
172 |
|
|
173 |
|
static double |
174 |
|
enumber(ep) |
175 |
< |
EPNODE *ep; |
175 |
> |
EPNODE *ep; |
176 |
|
{ |
177 |
|
return(ep->v.num); |
178 |
|
} |
179 |
|
|
180 |
|
static double |
181 |
|
euminus(ep) |
182 |
< |
EPNODE *ep; |
182 |
> |
EPNODE *ep; |
183 |
|
{ |
184 |
|
register EPNODE *ep1 = ep->v.kid; |
185 |
|
|
186 |
|
return(-evalue(ep1)); |
187 |
|
} |
188 |
|
|
189 |
< |
#ifdef INCHAN |
189 |
> |
#ifdef INCHAN |
190 |
|
static double |
191 |
|
echannel(ep) |
192 |
< |
EPNODE *ep; |
192 |
> |
EPNODE *ep; |
193 |
|
{ |
194 |
|
return(chanvalue(ep->v.chan)); |
195 |
|
} |
197 |
|
|
198 |
|
static double |
199 |
|
eadd(ep) |
200 |
< |
EPNODE *ep; |
200 |
> |
EPNODE *ep; |
201 |
|
{ |
202 |
|
register EPNODE *ep1 = ep->v.kid; |
203 |
|
|
206 |
|
|
207 |
|
static double |
208 |
|
esubtr(ep) |
209 |
< |
EPNODE *ep; |
209 |
> |
EPNODE *ep; |
210 |
|
{ |
211 |
|
register EPNODE *ep1 = ep->v.kid; |
212 |
|
|
215 |
|
|
216 |
|
static double |
217 |
|
emult(ep) |
218 |
< |
EPNODE *ep; |
218 |
> |
EPNODE *ep; |
219 |
|
{ |
220 |
|
register EPNODE *ep1 = ep->v.kid; |
221 |
|
|
224 |
|
|
225 |
|
static double |
226 |
|
edivi(ep) |
227 |
< |
EPNODE *ep; |
227 |
> |
EPNODE *ep; |
228 |
|
{ |
229 |
|
register EPNODE *ep1 = ep->v.kid; |
230 |
|
double d; |
240 |
|
|
241 |
|
static double |
242 |
|
epow(ep) |
243 |
< |
EPNODE *ep; |
243 |
> |
EPNODE *ep; |
244 |
|
{ |
245 |
|
register EPNODE *ep1 = ep->v.kid; |
246 |
|
double d; |
247 |
< |
int lasterrno; |
247 |
> |
int lasterrno; |
248 |
|
|
249 |
|
lasterrno = errno; |
250 |
|
errno = 0; |
251 |
|
d = pow(evalue(ep1), evalue(ep1->sibling)); |
252 |
< |
#ifdef IEEE |
252 |
> |
#ifdef IEEE |
253 |
|
if (!finite(d)) |
254 |
|
errno = EDOM; |
255 |
|
#endif |
263 |
|
|
264 |
|
static double |
265 |
|
ebotch(ep) |
266 |
< |
EPNODE *ep; |
266 |
> |
EPNODE *ep; |
267 |
|
{ |
268 |
|
eputs("Bad expression!\n"); |
269 |
|
quit(1); |
272 |
|
|
273 |
|
EPNODE * |
274 |
|
ekid(ep, n) /* return pointer to a node's nth kid */ |
275 |
< |
register EPNODE *ep; |
275 |
> |
register EPNODE *ep; |
276 |
|
register int n; |
277 |
|
{ |
278 |
|
|
286 |
|
|
287 |
|
int |
288 |
|
nekids(ep) /* return # of kids for node ep */ |
289 |
< |
register EPNODE *ep; |
289 |
> |
register EPNODE *ep; |
290 |
|
{ |
291 |
|
register int n = 0; |
292 |
|
|
302 |
|
char *fn; |
303 |
|
int ln; |
304 |
|
{ |
305 |
< |
static char inpbuf[MAXLINE]; |
305 |
> |
static char inpbuf[MAXLINE]; |
306 |
|
|
307 |
|
infp = fp; |
308 |
|
linbuf = inpbuf; |
374 |
|
|
375 |
|
|
376 |
|
char * |
377 |
< |
ltoa(l) /* convert long to ascii */ |
377 |
> |
long2ascii(l) /* convert long to ascii */ |
378 |
|
long l; |
379 |
|
{ |
380 |
< |
static char buf[16]; |
380 |
> |
static char buf[16]; |
381 |
|
register char *cp; |
382 |
< |
int neg = 0; |
382 |
> |
int neg = 0; |
383 |
|
|
384 |
|
if (l == 0) |
385 |
|
return("0"); |
408 |
|
if (infile != NULL) eputs(infile); |
409 |
|
if (lineno != 0) { |
410 |
|
eputs(infile != NULL ? ", line " : "line "); |
411 |
< |
eputs(ltoa((long)lineno)); |
411 |
> |
eputs(long2ascii((long)lineno)); |
412 |
|
} |
413 |
< |
eputs(": syntax error:\n"); |
413 |
> |
eputs(":\n"); |
414 |
|
} |
415 |
|
eputs(linbuf); |
416 |
|
if (linbuf[strlen(linbuf)-1] != '\n') |
425 |
|
|
426 |
|
|
427 |
|
addekid(ep, ekid) /* add a child to ep */ |
428 |
< |
register EPNODE *ep; |
429 |
< |
EPNODE *ekid; |
428 |
> |
register EPNODE *ep; |
429 |
> |
EPNODE *ekid; |
430 |
|
{ |
431 |
|
if (ep->v.kid == NULL) |
432 |
|
ep->v.kid = ekid; |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
+ |
#if defined(VARIABLE) || defined(FUNCTION) |
443 |
|
char * |
444 |
|
getname() /* scan an identifier */ |
445 |
|
{ |
446 |
< |
static char str[MAXWORD+1]; |
446 |
> |
static char str[MAXWORD+1]; |
447 |
|
register int i, lnext; |
448 |
|
|
449 |
|
lnext = nextc; |
455 |
|
|
456 |
|
return(str); |
457 |
|
} |
458 |
+ |
#endif |
459 |
|
|
460 |
|
|
461 |
|
int |
486 |
|
lnext = scan(); |
487 |
|
} |
488 |
|
if (lnext == '.' && i < MAXWORD) { |
489 |
< |
str[i++] = lnext; |
490 |
< |
lnext = scan(); |
489 |
> |
str[i++] = lnext; |
490 |
> |
lnext = scan(); |
491 |
|
while (isdigit(lnext) && i < MAXWORD) { |
492 |
|
str[i++] = lnext; |
493 |
|
lnext = scan(); |
494 |
|
} |
495 |
|
} |
496 |
|
if ((lnext == 'e' || lnext == 'E') && i < MAXWORD) { |
497 |
< |
str[i++] = lnext; |
498 |
< |
lnext = scan(); |
497 |
> |
str[i++] = lnext; |
498 |
> |
lnext = scan(); |
499 |
|
if ((lnext == '-' || lnext == '+') && i < MAXWORD) { |
500 |
|
str[i++] = lnext; |
501 |
|
lnext = scan(); |
513 |
|
|
514 |
|
EPNODE * |
515 |
|
getE1() /* E1 -> E1 ADDOP E2 */ |
516 |
< |
/* E2 */ |
516 |
> |
/* E2 */ |
517 |
|
{ |
518 |
|
register EPNODE *ep1, *ep2; |
519 |
|
|
524 |
|
scan(); |
525 |
|
addekid(ep2, ep1); |
526 |
|
addekid(ep2, getE2()); |
527 |
< |
#ifdef RCONST |
527 |
> |
#ifdef RCONST |
528 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
529 |
|
ep2 = rconst(ep2); |
530 |
|
#endif |
536 |
|
|
537 |
|
EPNODE * |
538 |
|
getE2() /* E2 -> E2 MULOP E3 */ |
539 |
< |
/* E3 */ |
539 |
> |
/* E3 */ |
540 |
|
{ |
541 |
|
register EPNODE *ep1, *ep2; |
542 |
|
|
547 |
|
scan(); |
548 |
|
addekid(ep2, ep1); |
549 |
|
addekid(ep2, getE3()); |
550 |
< |
#ifdef RCONST |
550 |
> |
#ifdef RCONST |
551 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
552 |
|
ep2 = rconst(ep2); |
553 |
|
#endif |
559 |
|
|
560 |
|
EPNODE * |
561 |
|
getE3() /* E3 -> E4 ^ E3 */ |
562 |
< |
/* E4 */ |
562 |
> |
/* E4 */ |
563 |
|
{ |
564 |
|
register EPNODE *ep1, *ep2; |
565 |
|
|
570 |
|
scan(); |
571 |
|
addekid(ep2, ep1); |
572 |
|
addekid(ep2, getE3()); |
573 |
< |
#ifdef RCONST |
573 |
> |
#ifdef RCONST |
574 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
575 |
|
ep2 = rconst(ep2); |
576 |
|
#endif |
582 |
|
|
583 |
|
EPNODE * |
584 |
|
getE4() /* E4 -> ADDOP E5 */ |
585 |
< |
/* E5 */ |
585 |
> |
/* E5 */ |
586 |
|
{ |
587 |
|
register EPNODE *ep1, *ep2; |
588 |
|
|
610 |
|
|
611 |
|
EPNODE * |
612 |
|
getE5() /* E5 -> (E1) */ |
613 |
< |
/* VAR */ |
614 |
< |
/* NUM */ |
615 |
< |
/* $N */ |
616 |
< |
/* FUNC(E1,..) */ |
617 |
< |
/* ARG */ |
613 |
> |
/* VAR */ |
614 |
> |
/* NUM */ |
615 |
> |
/* $N */ |
616 |
> |
/* FUNC(E1,..) */ |
617 |
> |
/* ARG */ |
618 |
|
{ |
619 |
< |
int i; |
619 |
> |
int i; |
620 |
|
char *nam; |
621 |
|
register EPNODE *ep1, *ep2; |
622 |
|
|
629 |
|
return(ep1); |
630 |
|
} |
631 |
|
|
632 |
< |
#ifdef INCHAN |
632 |
> |
#ifdef INCHAN |
633 |
|
if (nextc == '$') { |
634 |
|
scan(); |
635 |
|
ep1 = newnode(); |
646 |
|
ep1 = NULL; |
647 |
|
if (curfunc != NULL) |
648 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
649 |
< |
ep2 != NULL; i++, ep2 = ep2->sibling) |
649 |
> |
ep2 != NULL; i++, ep2 = ep2->sibling) |
650 |
|
if (!strcmp(ep2->v.name, nam)) { |
651 |
|
ep1 = newnode(); |
652 |
|
ep1->type = ARG; |
660 |
|
ep1->type = VAR; |
661 |
|
ep1->v.ln = varinsert(nam); |
662 |
|
} |
663 |
< |
#ifdef FUNCTION |
663 |
> |
#ifdef FUNCTION |
664 |
|
if (nextc == '(') { |
665 |
|
ep2 = newnode(); |
666 |
|
ep2->type = FUNC; |
674 |
|
syntax("')' expected"); |
675 |
|
scan(); |
676 |
|
} |
677 |
< |
#ifndef VARIABLE |
677 |
> |
#ifndef VARIABLE |
678 |
|
else |
679 |
|
syntax("'(' expected"); |
680 |
|
#endif |
681 |
|
#endif |
682 |
< |
#ifdef RCONST |
682 |
> |
#ifdef RCONST |
683 |
|
if (isconstvar(ep1)) |
684 |
|
ep1 = rconst(ep1); |
685 |
|
#endif |
697 |
|
} |
698 |
|
|
699 |
|
|
700 |
< |
#ifdef RCONST |
700 |
> |
#ifdef RCONST |
701 |
|
EPNODE * |
702 |
|
rconst(epar) /* reduce a constant expression */ |
703 |
< |
register EPNODE *epar; |
703 |
> |
register EPNODE *epar; |
704 |
|
{ |
705 |
|
register EPNODE *ep; |
706 |
|
|
709 |
|
errno = 0; |
710 |
|
ep->v.num = evalue(epar); |
711 |
|
if (errno) |
712 |
< |
syntax("bad constant expression"); |
712 |
> |
syntax("bad constant expression"); |
713 |
|
epfree(epar); |
714 |
|
|
715 |
|
return(ep); |
717 |
|
|
718 |
|
|
719 |
|
isconstvar(ep) /* is ep linked to a constant expression? */ |
720 |
< |
register EPNODE *ep; |
720 |
> |
register EPNODE *ep; |
721 |
|
{ |
722 |
< |
#ifdef VARIABLE |
722 |
> |
#ifdef VARIABLE |
723 |
|
register EPNODE *ep1; |
724 |
< |
#ifdef FUNCTION |
724 |
> |
#ifdef FUNCTION |
725 |
|
|
726 |
|
if (ep->type == FUNC) { |
727 |
|
if (!isconstfun(ep->v.kid)) |
737 |
|
ep1 = ep->v.ln->def; |
738 |
|
if (ep1 == NULL || ep1->type != ':') |
739 |
|
return(0); |
740 |
< |
#ifdef FUNCTION |
740 |
> |
#ifdef FUNCTION |
741 |
|
if (ep1->v.kid->type != SYM) |
742 |
|
return(0); |
743 |
|
#endif |
750 |
|
|
751 |
|
#if defined(FUNCTION) && defined(VARIABLE) |
752 |
|
isconstfun(ep) /* is ep linked to a constant function? */ |
753 |
< |
register EPNODE *ep; |
753 |
> |
register EPNODE *ep; |
754 |
|
{ |
755 |
|
register EPNODE *dp; |
756 |
|
register LIBR *lp; |
759 |
|
return(0); |
760 |
|
if ((dp = ep->v.ln->def) != NULL && dp->v.kid->type == FUNC) |
761 |
|
return(dp->type == ':'); |
762 |
< |
if ((lp = liblookup(ep->v.ln->name)) != NULL) |
762 |
> |
if ((lp = ep->v.ln->lib) != NULL) |
763 |
|
return(lp->atyp == ':'); |
764 |
|
return(0); |
765 |
|
} |