1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Compute data values using expression parser |
6 |
|
* |
13 |
|
* 1/29/87 Made variables conditional (VARIABLE) |
14 |
|
* |
15 |
|
* 5/19/88 Added constant subexpression elimination (RCONST) |
16 |
+ |
* |
17 |
+ |
* 2/19/03 Eliminated conditional compiles in favor of esupport extern. |
18 |
|
*/ |
19 |
|
|
20 |
+ |
/* ==================================================================== |
21 |
+ |
* The Radiance Software License, Version 1.0 |
22 |
+ |
* |
23 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
24 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
25 |
+ |
* |
26 |
+ |
* Redistribution and use in source and binary forms, with or without |
27 |
+ |
* modification, are permitted provided that the following conditions |
28 |
+ |
* are met: |
29 |
+ |
* |
30 |
+ |
* 1. Redistributions of source code must retain the above copyright |
31 |
+ |
* notice, this list of conditions and the following disclaimer. |
32 |
+ |
* |
33 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
34 |
+ |
* notice, this list of conditions and the following disclaimer in |
35 |
+ |
* the documentation and/or other materials provided with the |
36 |
+ |
* distribution. |
37 |
+ |
* |
38 |
+ |
* 3. The end-user documentation included with the redistribution, |
39 |
+ |
* if any, must include the following acknowledgment: |
40 |
+ |
* "This product includes Radiance software |
41 |
+ |
* (http://radsite.lbl.gov/) |
42 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
43 |
+ |
* (http://www.lbl.gov/)." |
44 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
45 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
46 |
+ |
* |
47 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
48 |
+ |
* and "The Regents of the University of California" must |
49 |
+ |
* not be used to endorse or promote products derived from this |
50 |
+ |
* software without prior written permission. For written |
51 |
+ |
* permission, please contact [email protected]. |
52 |
+ |
* |
53 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
54 |
+ |
* nor may "Radiance" appear in their name, without prior written |
55 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
56 |
+ |
* |
57 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
58 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
59 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
60 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
61 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
62 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
63 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
64 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
65 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
66 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
67 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
68 |
+ |
* SUCH DAMAGE. |
69 |
+ |
* ==================================================================== |
70 |
+ |
* |
71 |
+ |
* This software consists of voluntary contributions made by many |
72 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
73 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
74 |
+ |
* <http://www.lbl.gov/>. |
75 |
+ |
*/ |
76 |
+ |
|
77 |
|
#include <stdio.h> |
78 |
|
|
79 |
|
#include <ctype.h> |
82 |
|
|
83 |
|
#include <math.h> |
84 |
|
|
85 |
+ |
#include <stdlib.h> |
86 |
+ |
|
87 |
|
#include "calcomp.h" |
88 |
|
|
89 |
|
#define MAXLINE 256 /* maximum line length */ |
92 |
|
|
93 |
|
#define isdecimal(c) (isdigit(c) || (c) == '.') |
94 |
|
|
37 |
– |
extern char *savestr(); |
38 |
– |
extern char *emalloc(), *ecalloc(); |
39 |
– |
extern EPNODE *curfunc; |
40 |
– |
extern double efunc(), evariable(); |
95 |
|
static double euminus(), eargument(), enumber(); |
42 |
– |
#ifdef INCHAN |
96 |
|
static double echannel(); |
44 |
– |
#endif |
97 |
|
static double eadd(), esubtr(), emult(), edivi(), epow(); |
98 |
|
static double ebotch(); |
99 |
|
|
100 |
< |
#ifdef DCL_ATOF |
101 |
< |
extern double atof(); |
50 |
< |
#endif |
100 |
> |
unsigned int esupport = /* what to support */ |
101 |
> |
E_VARIABLE | E_FUNCTION | E_REDEFW; |
102 |
|
|
103 |
|
int nextc; /* lookahead character */ |
104 |
|
|
105 |
|
double (*eoper[])() = { /* expression operations */ |
106 |
|
ebotch, |
56 |
– |
#ifdef VARIABLE |
107 |
|
evariable, |
58 |
– |
#else |
59 |
– |
ebotch, |
60 |
– |
#endif |
108 |
|
enumber, |
109 |
|
euminus, |
63 |
– |
#ifdef INCHAN |
110 |
|
echannel, |
65 |
– |
#else |
66 |
– |
ebotch, |
67 |
– |
#endif |
68 |
– |
#ifdef FUNCTION |
111 |
|
efunc, |
112 |
|
eargument, |
71 |
– |
#else |
113 |
|
ebotch, |
114 |
|
ebotch, |
74 |
– |
#endif |
75 |
– |
ebotch, |
76 |
– |
ebotch, |
115 |
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
116 |
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
117 |
|
emult, |
143 |
|
EPNODE *ep; |
144 |
|
|
145 |
|
initstr(expr, NULL, 0); |
108 |
– |
#if defined(VARIABLE) && defined(FUNCTION) |
146 |
|
curfunc = NULL; |
110 |
– |
#endif |
147 |
|
ep = getE1(); |
148 |
|
if (nextc != EOF) |
149 |
|
syntax("unexpected character"); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
+ |
int |
169 |
|
epcmp(ep1, ep2) /* compare two expressions for equivalence */ |
170 |
|
register EPNODE *ep1, *ep2; |
171 |
|
{ |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
+ |
void |
217 |
|
epfree(epar) /* free a parse tree */ |
218 |
|
register EPNODE *epar; |
219 |
|
{ |
221 |
|
|
222 |
|
switch (epar->type) { |
223 |
|
|
186 |
– |
#if defined(VARIABLE) || defined(FUNCTION) |
224 |
|
case VAR: |
225 |
|
varfree(epar->v.ln); |
226 |
|
break; |
228 |
|
case SYM: |
229 |
|
freestr(epar->v.name); |
230 |
|
break; |
194 |
– |
#endif |
231 |
|
|
232 |
|
case NUM: |
233 |
|
case CHAN: |
248 |
|
} |
249 |
|
|
250 |
|
/* the following used to be a switch */ |
215 |
– |
#ifdef FUNCTION |
251 |
|
static double |
252 |
|
eargument(ep) |
253 |
|
EPNODE *ep; |
254 |
|
{ |
255 |
|
return(argument(ep->v.chan)); |
256 |
|
} |
222 |
– |
#endif |
257 |
|
|
258 |
|
static double |
259 |
|
enumber(ep) |
271 |
|
return(-evalue(ep1)); |
272 |
|
} |
273 |
|
|
240 |
– |
#ifdef INCHAN |
274 |
|
static double |
275 |
|
echannel(ep) |
276 |
|
EPNODE *ep; |
277 |
|
{ |
278 |
|
return(chanvalue(ep->v.chan)); |
279 |
|
} |
247 |
– |
#endif |
280 |
|
|
281 |
|
static double |
282 |
|
eadd(ep) |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
+ |
void |
384 |
|
initfile(fp, fn, ln) /* prepare input file */ |
385 |
|
FILE *fp; |
386 |
|
char *fn; |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
+ |
void |
402 |
|
initstr(s, fn, ln) /* prepare input string */ |
403 |
|
char *s; |
404 |
|
char *fn; |
413 |
|
} |
414 |
|
|
415 |
|
|
416 |
+ |
void |
417 |
|
getscanpos(fnp, lnp, spp, fpp) /* return current scan position */ |
418 |
|
char **fnp; |
419 |
|
int *lnp; |
485 |
|
} |
486 |
|
|
487 |
|
|
488 |
+ |
void |
489 |
|
syntax(err) /* report syntax error and quit */ |
490 |
|
char *err; |
491 |
|
{ |
511 |
|
} |
512 |
|
|
513 |
|
|
514 |
+ |
void |
515 |
|
addekid(ep, ekid) /* add a child to ep */ |
516 |
|
register EPNODE *ep; |
517 |
|
EPNODE *ekid; |
527 |
|
} |
528 |
|
|
529 |
|
|
493 |
– |
#if defined(VARIABLE) || defined(FUNCTION) |
530 |
|
char * |
531 |
|
getname() /* scan an identifier */ |
532 |
|
{ |
542 |
|
|
543 |
|
return(str); |
544 |
|
} |
509 |
– |
#endif |
545 |
|
|
546 |
|
|
547 |
|
int |
614 |
|
scan(); |
615 |
|
addekid(ep2, ep1); |
616 |
|
addekid(ep2, getE2()); |
617 |
< |
#ifdef RCONST |
618 |
< |
if (ep1->type == NUM && ep1->sibling->type == NUM) |
617 |
> |
if (esupport&E_RCONST && |
618 |
> |
ep1->type == NUM && ep1->sibling->type == NUM) |
619 |
|
ep2 = rconst(ep2); |
585 |
– |
#endif |
620 |
|
ep1 = ep2; |
621 |
|
} |
622 |
|
return(ep1); |
636 |
|
scan(); |
637 |
|
addekid(ep2, ep1); |
638 |
|
addekid(ep2, getE3()); |
639 |
< |
#ifdef RCONST |
640 |
< |
if (ep1->type == NUM && ep1->sibling->type == NUM) |
639 |
> |
if (esupport&E_RCONST && |
640 |
> |
ep1->type == NUM && ep1->sibling->type == NUM) |
641 |
|
ep2 = rconst(ep2); |
608 |
– |
#endif |
642 |
|
ep1 = ep2; |
643 |
|
} |
644 |
|
return(ep1); |
658 |
|
scan(); |
659 |
|
addekid(ep2, ep1); |
660 |
|
addekid(ep2, getE3()); |
661 |
< |
#ifdef RCONST |
662 |
< |
if (ep1->type == NUM && ep1->sibling->type == NUM) |
661 |
> |
if (esupport&E_RCONST && |
662 |
> |
ep1->type == NUM && ep1->sibling->type == NUM) |
663 |
|
ep2 = rconst(ep2); |
631 |
– |
#endif |
664 |
|
return(ep2); |
665 |
|
} |
666 |
|
return(ep1); |
716 |
|
return(ep1); |
717 |
|
} |
718 |
|
|
719 |
< |
#ifdef INCHAN |
688 |
< |
if (nextc == '$') { |
719 |
> |
if (esupport&E_INCHAN && nextc == '$') { |
720 |
|
scan(); |
721 |
|
ep1 = newnode(); |
722 |
|
ep1->type = CHAN; |
723 |
|
ep1->v.chan = getinum(); |
724 |
|
return(ep1); |
725 |
|
} |
695 |
– |
#endif |
726 |
|
|
727 |
< |
#if defined(VARIABLE) || defined(FUNCTION) |
728 |
< |
if (isalpha(nextc) || nextc == CNTXMARK) { |
729 |
< |
nam = getname(); |
730 |
< |
#if defined(VARIABLE) && defined(FUNCTION) |
731 |
< |
ep1 = NULL; |
732 |
< |
if (curfunc != NULL) |
727 |
> |
if (esupport&(E_VARIABLE|E_FUNCTION) && |
728 |
> |
(isalpha(nextc) || nextc == CNTXMARK)) { |
729 |
> |
nam = getname(); |
730 |
> |
ep1 = NULL; |
731 |
> |
if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION) |
732 |
> |
&& curfunc != NULL) |
733 |
|
for (i = 1, ep2 = curfunc->v.kid->sibling; |
734 |
|
ep2 != NULL; i++, ep2 = ep2->sibling) |
735 |
|
if (!strcmp(ep2->v.name, nam)) { |
738 |
|
ep1->v.chan = i; |
739 |
|
break; |
740 |
|
} |
741 |
< |
if (ep1 == NULL) |
712 |
< |
#endif |
713 |
< |
{ |
741 |
> |
if (ep1 == NULL) { |
742 |
|
ep1 = newnode(); |
743 |
|
ep1->type = VAR; |
744 |
|
ep1->v.ln = varinsert(nam); |
745 |
|
} |
746 |
< |
#ifdef FUNCTION |
719 |
< |
if (nextc == '(') { |
746 |
> |
if (esupport&E_FUNCTION && nextc == '(') { |
747 |
|
ep2 = newnode(); |
748 |
|
ep2->type = FUNC; |
749 |
|
addekid(ep2, ep1); |
755 |
|
if (nextc != ')') |
756 |
|
syntax("')' expected"); |
757 |
|
scan(); |
758 |
< |
} |
732 |
< |
#ifndef VARIABLE |
733 |
< |
else |
758 |
> |
} else if (!(esupport&E_VARIABLE)) |
759 |
|
syntax("'(' expected"); |
760 |
< |
#endif |
736 |
< |
#endif |
737 |
< |
#ifdef RCONST |
738 |
< |
if (isconstvar(ep1)) |
760 |
> |
if (esupport&E_RCONST && isconstvar(ep1)) |
761 |
|
ep1 = rconst(ep1); |
740 |
– |
#endif |
762 |
|
return(ep1); |
763 |
|
} |
743 |
– |
#endif |
764 |
|
|
765 |
|
if (isdecimal(nextc)) { |
766 |
|
ep1 = newnode(); |
772 |
|
} |
773 |
|
|
774 |
|
|
755 |
– |
#ifdef RCONST |
775 |
|
EPNODE * |
776 |
|
rconst(epar) /* reduce a constant expression */ |
777 |
|
register EPNODE *epar; |
790 |
|
} |
791 |
|
|
792 |
|
|
793 |
+ |
int |
794 |
|
isconstvar(ep) /* is ep linked to a constant expression? */ |
795 |
|
register EPNODE *ep; |
796 |
|
{ |
777 |
– |
#ifdef VARIABLE |
797 |
|
register EPNODE *ep1; |
779 |
– |
#ifdef FUNCTION |
798 |
|
|
799 |
< |
if (ep->type == FUNC) { |
799 |
> |
if (esupport&E_FUNCTION && ep->type == FUNC) { |
800 |
|
if (!isconstfun(ep->v.kid)) |
801 |
|
return(0); |
802 |
|
for (ep1 = ep->v.kid->sibling; ep1 != NULL; ep1 = ep1->sibling) |
804 |
|
return(0); |
805 |
|
return(1); |
806 |
|
} |
789 |
– |
#endif |
807 |
|
if (ep->type != VAR) |
808 |
|
return(0); |
809 |
|
ep1 = ep->v.ln->def; |
810 |
|
if (ep1 == NULL || ep1->type != ':') |
811 |
|
return(0); |
812 |
< |
#ifdef FUNCTION |
796 |
< |
if (ep1->v.kid->type != SYM) |
812 |
> |
if (esupport&E_FUNCTION && ep1->v.kid->type != SYM) |
813 |
|
return(0); |
798 |
– |
#endif |
814 |
|
return(1); |
800 |
– |
#else |
801 |
– |
return(ep->type == FUNC); |
802 |
– |
#endif |
815 |
|
} |
816 |
|
|
817 |
|
|
818 |
< |
#if defined(FUNCTION) && defined(VARIABLE) |
818 |
> |
int |
819 |
|
isconstfun(ep) /* is ep linked to a constant function? */ |
820 |
|
register EPNODE *ep; |
821 |
|
{ |
824 |
|
|
825 |
|
if (ep->type != VAR) |
826 |
|
return(0); |
827 |
< |
if ((dp = ep->v.ln->def) != NULL && dp->v.kid->type == FUNC) |
828 |
< |
return(dp->type == ':'); |
827 |
> |
if ((dp = ep->v.ln->def) != NULL) |
828 |
> |
if (dp->v.kid->type == FUNC) |
829 |
> |
return(dp->type == ':'); |
830 |
> |
else |
831 |
> |
return(0); /* don't identify masked library functions */ |
832 |
|
if ((lp = ep->v.ln->lib) != NULL) |
833 |
|
return(lp->atyp == ':'); |
834 |
|
return(0); |
835 |
|
} |
821 |
– |
#endif |
822 |
– |
#endif |