1 |
< |
/* Copyright (c) 1986 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 */ |
30 |
< |
#define MAXWORD 64 /* maximum word 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 isid(c) (isalnum(c) || (c) == '_' || (c) == '.') |
35 |
> |
#define isdecimal(c) (isdigit(c) || (c) == '.') |
36 |
|
|
37 |
< |
#define isdecimal(c) (isdigit(c) || (c) == '.') |
38 |
< |
|
39 |
< |
extern double atof(), pow(); |
37 |
> |
#ifndef atof |
38 |
> |
extern double atof(); |
39 |
> |
#endif |
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; |
132 |
|
|
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: |
159 |
|
} |
160 |
|
|
161 |
|
/* the following used to be a switch */ |
162 |
< |
#ifdef FUNCTION |
162 |
> |
#ifdef FUNCTION |
163 |
|
static double |
164 |
|
eargument(ep) |
165 |
< |
EPNODE *ep; |
165 |
> |
EPNODE *ep; |
166 |
|
{ |
167 |
|
return(argument(ep->v.chan)); |
168 |
|
} |
170 |
|
|
171 |
|
static double |
172 |
|
enumber(ep) |
173 |
< |
EPNODE *ep; |
173 |
> |
EPNODE *ep; |
174 |
|
{ |
175 |
|
return(ep->v.num); |
176 |
|
} |
177 |
|
|
178 |
|
static double |
179 |
|
euminus(ep) |
180 |
< |
EPNODE *ep; |
180 |
> |
EPNODE *ep; |
181 |
|
{ |
182 |
|
register EPNODE *ep1 = ep->v.kid; |
183 |
|
|
184 |
|
return(-evalue(ep1)); |
185 |
|
} |
186 |
|
|
187 |
< |
#ifdef INCHAN |
187 |
> |
#ifdef INCHAN |
188 |
|
static double |
189 |
|
echannel(ep) |
190 |
< |
EPNODE *ep; |
190 |
> |
EPNODE *ep; |
191 |
|
{ |
192 |
|
return(chanvalue(ep->v.chan)); |
193 |
|
} |
195 |
|
|
196 |
|
static double |
197 |
|
eadd(ep) |
198 |
< |
EPNODE *ep; |
198 |
> |
EPNODE *ep; |
199 |
|
{ |
200 |
|
register EPNODE *ep1 = ep->v.kid; |
201 |
|
|
204 |
|
|
205 |
|
static double |
206 |
|
esubtr(ep) |
207 |
< |
EPNODE *ep; |
207 |
> |
EPNODE *ep; |
208 |
|
{ |
209 |
|
register EPNODE *ep1 = ep->v.kid; |
210 |
|
|
213 |
|
|
214 |
|
static double |
215 |
|
emult(ep) |
216 |
< |
EPNODE *ep; |
216 |
> |
EPNODE *ep; |
217 |
|
{ |
218 |
|
register EPNODE *ep1 = ep->v.kid; |
219 |
|
|
222 |
|
|
223 |
|
static double |
224 |
|
edivi(ep) |
225 |
< |
EPNODE *ep; |
225 |
> |
EPNODE *ep; |
226 |
|
{ |
227 |
|
register EPNODE *ep1 = ep->v.kid; |
228 |
|
double d; |
238 |
|
|
239 |
|
static double |
240 |
|
epow(ep) |
241 |
< |
EPNODE *ep; |
241 |
> |
EPNODE *ep; |
242 |
|
{ |
243 |
|
register EPNODE *ep1 = ep->v.kid; |
244 |
|
double d; |
245 |
< |
int lasterrno; |
245 |
> |
int lasterrno; |
246 |
|
|
247 |
|
lasterrno = errno; |
248 |
|
errno = 0; |
249 |
|
d = pow(evalue(ep1), evalue(ep1->sibling)); |
250 |
< |
#ifdef IEEE |
250 |
> |
#ifdef IEEE |
251 |
|
if (!finite(d)) |
252 |
|
errno = EDOM; |
253 |
|
#endif |
261 |
|
|
262 |
|
static double |
263 |
|
ebotch(ep) |
264 |
< |
EPNODE *ep; |
264 |
> |
EPNODE *ep; |
265 |
|
{ |
266 |
|
eputs("Bad expression!\n"); |
267 |
|
quit(1); |
270 |
|
|
271 |
|
EPNODE * |
272 |
|
ekid(ep, n) /* return pointer to a node's nth kid */ |
273 |
< |
register EPNODE *ep; |
273 |
> |
register EPNODE *ep; |
274 |
|
register int n; |
275 |
|
{ |
276 |
|
|
284 |
|
|
285 |
|
int |
286 |
|
nekids(ep) /* return # of kids for node ep */ |
287 |
< |
register EPNODE *ep; |
287 |
> |
register EPNODE *ep; |
288 |
|
{ |
289 |
|
register int n = 0; |
290 |
|
|
300 |
|
char *fn; |
301 |
|
int ln; |
302 |
|
{ |
303 |
< |
static char inpbuf[MAXLINE]; |
303 |
> |
static char inpbuf[MAXLINE]; |
304 |
|
|
305 |
|
infp = fp; |
306 |
|
linbuf = inpbuf; |
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 |
|
|
375 |
|
ltoa(l) /* convert long to ascii */ |
376 |
|
long l; |
377 |
|
{ |
378 |
< |
static char buf[16]; |
378 |
> |
static char buf[16]; |
379 |
|
register char *cp; |
380 |
< |
int neg = 0; |
380 |
> |
int neg = 0; |
381 |
|
|
382 |
|
if (l == 0) |
383 |
|
return("0"); |
408 |
|
eputs(infile != NULL ? ", line " : "line "); |
409 |
|
eputs(ltoa((long)lineno)); |
410 |
|
} |
411 |
< |
eputs(": syntax error:\n"); |
411 |
> |
eputs(":\n"); |
412 |
|
} |
413 |
|
eputs(linbuf); |
414 |
|
if (linbuf[strlen(linbuf)-1] != '\n') |
423 |
|
|
424 |
|
|
425 |
|
addekid(ep, ekid) /* add a child to ep */ |
426 |
< |
register EPNODE *ep; |
427 |
< |
EPNODE *ekid; |
426 |
> |
register EPNODE *ep; |
427 |
> |
EPNODE *ekid; |
428 |
|
{ |
429 |
|
if (ep->v.kid == NULL) |
430 |
|
ep->v.kid = ekid; |
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; |
444 |
> |
static char str[MAXWORD+1]; |
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'; |
511 |
|
|
512 |
|
EPNODE * |
513 |
|
getE1() /* E1 -> E1 ADDOP E2 */ |
514 |
< |
/* E2 */ |
514 |
> |
/* E2 */ |
515 |
|
{ |
516 |
|
register EPNODE *ep1, *ep2; |
517 |
|
|
522 |
|
scan(); |
523 |
|
addekid(ep2, ep1); |
524 |
|
addekid(ep2, getE2()); |
525 |
< |
#ifdef RCONST |
525 |
> |
#ifdef RCONST |
526 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
527 |
|
ep2 = rconst(ep2); |
528 |
|
#endif |
534 |
|
|
535 |
|
EPNODE * |
536 |
|
getE2() /* E2 -> E2 MULOP E3 */ |
537 |
< |
/* E3 */ |
537 |
> |
/* E3 */ |
538 |
|
{ |
539 |
|
register EPNODE *ep1, *ep2; |
540 |
|
|
545 |
|
scan(); |
546 |
|
addekid(ep2, ep1); |
547 |
|
addekid(ep2, getE3()); |
548 |
< |
#ifdef RCONST |
548 |
> |
#ifdef RCONST |
549 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
550 |
|
ep2 = rconst(ep2); |
551 |
|
#endif |
557 |
|
|
558 |
|
EPNODE * |
559 |
|
getE3() /* E3 -> E4 ^ E3 */ |
560 |
< |
/* E4 */ |
560 |
> |
/* E4 */ |
561 |
|
{ |
562 |
|
register EPNODE *ep1, *ep2; |
563 |
|
|
568 |
|
scan(); |
569 |
|
addekid(ep2, ep1); |
570 |
|
addekid(ep2, getE3()); |
571 |
< |
#ifdef RCONST |
571 |
> |
#ifdef RCONST |
572 |
|
if (ep1->type == NUM && ep1->sibling->type == NUM) |
573 |
|
ep2 = rconst(ep2); |
574 |
|
#endif |
580 |
|
|
581 |
|
EPNODE * |
582 |
|
getE4() /* E4 -> ADDOP E5 */ |
583 |
< |
/* E5 */ |
583 |
> |
/* E5 */ |
584 |
|
{ |
585 |
|
register EPNODE *ep1, *ep2; |
586 |
|
|
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); |
608 |
|
|
609 |
|
EPNODE * |
610 |
|
getE5() /* E5 -> (E1) */ |
611 |
< |
/* VAR */ |
612 |
< |
/* NUM */ |
613 |
< |
/* $N */ |
614 |
< |
/* FUNC(E1,..) */ |
615 |
< |
/* ARG */ |
611 |
> |
/* VAR */ |
612 |
> |
/* NUM */ |
613 |
> |
/* $N */ |
614 |
> |
/* FUNC(E1,..) */ |
615 |
> |
/* ARG */ |
616 |
|
{ |
617 |
< |
int i; |
617 |
> |
int i; |
618 |
> |
char *nam; |
619 |
|
register EPNODE *ep1, *ep2; |
620 |
|
|
621 |
|
if (nextc == '(') { |
627 |
|
return(ep1); |
628 |
|
} |
629 |
|
|
630 |
< |
#ifdef INCHAN |
630 |
> |
#ifdef INCHAN |
631 |
|
if (nextc == '$') { |
632 |
|
scan(); |
633 |
|
ep1 = newnode(); |
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); |
647 |
> |
ep2 != NULL; i++, ep2 = ep2->sibling) |
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 |
< |
#ifdef FUNCTION |
656 |
> |
{ |
657 |
> |
ep1 = newnode(); |
658 |
> |
ep1->type = VAR; |
659 |
> |
ep1->v.ln = varinsert(nam); |
660 |
> |
} |
661 |
> |
#ifdef FUNCTION |
662 |
|
if (nextc == '(') { |
663 |
|
ep2 = newnode(); |
664 |
|
ep2->type = FUNC; |
672 |
|
syntax("')' expected"); |
673 |
|
scan(); |
674 |
|
} |
675 |
< |
#ifndef VARIABLE |
675 |
> |
#ifndef VARIABLE |
676 |
|
else |
677 |
|
syntax("'(' expected"); |
678 |
|
#endif |
679 |
|
#endif |
680 |
< |
#ifdef RCONST |
680 |
> |
#ifdef RCONST |
681 |
|
if (isconstvar(ep1)) |
682 |
|
ep1 = rconst(ep1); |
683 |
|
#endif |
695 |
|
} |
696 |
|
|
697 |
|
|
698 |
< |
#ifdef RCONST |
698 |
> |
#ifdef RCONST |
699 |
|
EPNODE * |
700 |
|
rconst(epar) /* reduce a constant expression */ |
701 |
< |
register EPNODE *epar; |
701 |
> |
register EPNODE *epar; |
702 |
|
{ |
703 |
|
register EPNODE *ep; |
704 |
|
|
707 |
|
errno = 0; |
708 |
|
ep->v.num = evalue(epar); |
709 |
|
if (errno) |
710 |
< |
syntax("bad constant expression"); |
710 |
> |
syntax("bad constant expression"); |
711 |
|
epfree(epar); |
712 |
|
|
713 |
|
return(ep); |
714 |
|
} |
715 |
|
|
716 |
|
|
717 |
< |
isconstvar(ep) /* is ep linked to a constant? */ |
718 |
< |
register EPNODE *ep; |
717 |
> |
isconstvar(ep) /* is ep linked to a constant expression? */ |
718 |
> |
register EPNODE *ep; |
719 |
|
{ |
720 |
< |
#ifdef VARIABLE |
720 |
> |
#ifdef VARIABLE |
721 |
|
register EPNODE *ep1; |
722 |
< |
|
723 |
< |
#ifdef FUNCTION |
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 |
|
} |
735 |
|
ep1 = ep->v.ln->def; |
736 |
|
if (ep1 == NULL || ep1->type != ':') |
737 |
|
return(0); |
738 |
< |
#ifdef FUNCTION |
738 |
> |
#ifdef FUNCTION |
739 |
|
if (ep1->v.kid->type != SYM) |
740 |
|
return(0); |
741 |
|
#endif |
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 |