ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/caldefn.c
(Generate patch)

Comparing ray/src/common/caldefn.c (file contents):
Revision 2.12 by greg, Sat Feb 22 02:07:21 2003 UTC vs.
Revision 2.33 by greg, Fri Apr 8 23:32:25 2022 UTC

# Line 25 | Line 25 | static const char      RCSid[] = "$Id$";
25   *  2/19/03     Eliminated conditional compiles in favor of esupport extern.
26   */
27  
28 < /* ====================================================================
29 < * The Radiance Software License, Version 1.0
30 < *
31 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
32 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
33 < *
34 < * Redistribution and use in source and binary forms, with or without
35 < * modification, are permitted provided that the following conditions
36 < * are met:
37 < *
38 < * 1. Redistributions of source code must retain the above copyright
39 < *         notice, this list of conditions and the following disclaimer.
40 < *
41 < * 2. Redistributions in binary form must reproduce the above copyright
42 < *       notice, this list of conditions and the following disclaimer in
43 < *       the documentation and/or other materials provided with the
44 < *       distribution.
45 < *
46 < * 3. The end-user documentation included with the redistribution,
47 < *           if any, must include the following acknowledgment:
48 < *             "This product includes Radiance software
49 < *                 (http://radsite.lbl.gov/)
50 < *                 developed by the Lawrence Berkeley National Laboratory
51 < *               (http://www.lbl.gov/)."
52 < *       Alternately, this acknowledgment may appear in the software itself,
53 < *       if and wherever such third-party acknowledgments normally appear.
54 < *
55 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
56 < *       and "The Regents of the University of California" must
57 < *       not be used to endorse or promote products derived from this
58 < *       software without prior written permission. For written
59 < *       permission, please contact [email protected].
60 < *
61 < * 5. Products derived from this software may not be called "Radiance",
62 < *       nor may "Radiance" appear in their name, without prior written
63 < *       permission of Lawrence Berkeley National Laboratory.
64 < *
65 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
66 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
67 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
68 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
69 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
70 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
71 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
72 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
73 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
74 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
75 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 < * SUCH DAMAGE.
77 < * ====================================================================
78 < *
79 < * This software consists of voluntary contributions made by many
80 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
81 < * information on Lawrence Berkeley National Laboratory, please see
82 < * <http://www.lbl.gov/>.
83 < */
28 > #include "copyright.h"
29  
85 #include  <stdio.h>
86
87 #include  <string.h>
88
30   #include  <ctype.h>
31  
32 + #include  "rterror.h"
33 + #include  "rtio.h"
34 + #include  "rtmisc.h"
35   #include  "calcomp.h"
36  
37   #ifndef  NHASH
# Line 98 | Line 42 | static const char      RCSid[] = "$Id$";
42  
43   #define  newnode()      (EPNODE *)ecalloc(1, sizeof(EPNODE))
44  
45 < static double  dvalue();
45 > static double  dvalue(char  *name, EPNODE *d);
46  
47   #define  MAXCLOCK       (1L<<31)        /* clock wrap value */
48  
# Line 121 | Line 65 | EPNODE *curfunc = NULL;
65  
66  
67   void
68 < fcompile(fname)                 /* get definitions from a file */
69 < char  *fname;
68 > fcompile(                       /* get definitions from a file */
69 >        char  *fname
70 > )
71   {
72      FILE  *fp;
73  
# Line 142 | Line 87 | char  *fname;
87  
88  
89   void
90 < scompile(str, fn, ln)           /* get definitions from a string */
91 < char  *str;
92 < char  *fn;
93 < int  ln;
90 > scompile(               /* get definitions from a string */
91 >        char  *str,
92 >        char  *fn,
93 >        int  ln
94 > )
95   {
96      initstr(str, fn, ln);
97      while (nextc != EOF)
# Line 154 | Line 100 | int  ln;
100  
101  
102   double
103 < varvalue(vname)                 /* return a variable's value */
104 < char  *vname;
103 > varvalue(                       /* return a variable's value */
104 >        char  *vname
105 > )
106   {
107      return(dvalue(vname, dlookup(vname)));
108   }
109  
110  
111   double
112 < evariable(ep)                   /* evaluate a variable */
113 < EPNODE  *ep;
112 > evariable(                      /* evaluate a variable */
113 >        EPNODE  *ep
114 > )
115   {
116 <    register VARDEF  *dp = ep->v.ln;
116 >    VARDEF  *dp = ep->v.ln;
117  
118      return(dvalue(dp->name, dp->def));
119   }
120  
121  
122   void
123 < varset(vname, assign, val)      /* set a variable's value */
124 < char  *vname;
125 < int  assign;
126 < double  val;
123 > varset(         /* set a variable's value */
124 >        char  *vname,
125 >        int  assign,
126 >        double  val
127 > )
128   {
129      char  *qname;
130 <    register EPNODE  *ep1, *ep2;
130 >    EPNODE  *ep1, *ep2;
131                                          /* get qualified name */
132      qname = qualname(vname, 0);
133                                          /* check for quick set */
134 <    if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) {
134 >    if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM &&
135 >                (ep1->type == ':') <= (assign == ':')) {
136          ep2 = ep1->v.kid->sibling;
137          if (ep2->type == NUM) {
138              ep2->v.num = val;
# Line 190 | Line 140 | double val;
140              return;
141          }
142      }
143 +    if (ep1 != NULL && esupport&E_REDEFW) {
144 +        wputs(qname);
145 +        if (ep1->type == ':')
146 +            wputs(": reset constant expression\n");
147 +        else
148 +            wputs(": reset expression\n");
149 +    }
150                                          /* hand build definition */
151      ep1 = newnode();
152      ep1->type = assign;
# Line 201 | Line 158 | double val;
158      ep2->type = NUM;
159      ep2->v.num = val;
160      addekid(ep1, ep2);
161 <    dremove(qname);
161 >    if (assign == ':')
162 >        dremove(qname);
163 >    else
164 >        dclear(qname);
165      dpush(qname, ep1);
166   }
167  
168  
169   void
170 < dclear(name)                    /* delete variable definitions of name */
171 < char  *name;
170 > dclear(                 /* delete variable definitions of name */
171 >        char  *name
172 > )
173   {
174 <    register EPNODE  *ep;
174 >    EPNODE  *ep;
175  
176      while ((ep = dpop(name)) != NULL) {
177          if (ep->type == ':') {
# Line 223 | Line 184 | char  *name;
184  
185  
186   void
187 < dremove(name)                   /* delete all definitions of name */
188 < char  *name;
187 > dremove(                        /* delete all definitions of name */
188 >        char  *name
189 > )
190   {
191 <    register EPNODE  *ep;
191 >    EPNODE  *ep;
192  
193      while ((ep = dpop(name)) != NULL)
194          epfree(ep);
# Line 234 | Line 196 | char  *name;
196  
197  
198   int
199 < vardefined(name)        /* return non-zero if variable defined */
200 < char  *name;
199 > vardefined(     /* return '=' or ':' if variable/constant defined */
200 >        char  *name
201 > )
202   {
203 <    register EPNODE  *dp;
203 >    EPNODE  *dp = dlookup(name);
204  
205 <    return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM);
205 >    if (dp == NULL || dp->v.kid->type != SYM)
206 >        return(0);
207 >
208 >    return(dp->type);
209   }
210  
211  
212   char *
213 < setcontext(ctx)                 /* set a new context path */
214 < register char  *ctx;
213 > calcontext(                     /* set a new context path */
214 >        char  *ctx
215 > )
216   {
217 <    register char  *cpp;
217 >    char  *cpp;
218  
219      if (ctx == NULL)
220          return(context);                /* just asking */
# Line 276 | Line 243 | register char  *ctx;
243  
244  
245   char *
246 < pushcontext(ctx)                /* push on another context */
247 < char  *ctx;
246 > pushcontext(            /* push on another context */
247 >        char  *ctx
248 > )
249   {
282    extern char  *strncpy(), *strcpy();
250      char  oldcontext[MAXCNTX+1];
251 <    register int  n;
251 >    int  n;
252  
253      strcpy(oldcontext, context);        /* save old context */
254 <    setcontext(ctx);                    /* set new context */
254 >    calcontext(ctx);                    /* set new context */
255      n = strlen(context);                /* tack on old */
256      if (n+strlen(oldcontext) > MAXCNTX) {
257          strncpy(context+n, oldcontext, MAXCNTX-n);
# Line 296 | Line 263 | char  *ctx;
263  
264  
265   char *
266 < popcontext()                    /* pop off top context */
266 > popcontext(void)                        /* pop off top context */
267   {
268 <    register char  *cp1, *cp2;
268 >    char  *cp1, *cp2;
269  
270      if (!context[0])                    /* nothing left to pop */
271          return(context);
# Line 306 | Line 273 | popcontext()                   /* pop off top context */
273      while (*++cp2 && *cp2 != CNTXMARK)
274          ;
275      cp1 = context;                      /* copy tail to front */
276 <    while (*cp1++ = *cp2++)
276 >    while ( (*cp1++ = *cp2++) )
277          ;
278      return(context);
279   }
280  
281  
282   char *
283 < qualname(nam, lvl)              /* get qualified name */
284 < register char  *nam;
285 < int  lvl;
283 > qualname(               /* get qualified name */
284 >        char  *nam,
285 >        int  lvl
286 > )
287   {
288 <    static char  nambuf[MAXWORD+1];
289 <    register char  *cp = nambuf, *cpp;
288 >    static char  nambuf[RMAXWORD+1];
289 >    char  *cp = nambuf, *cpp;
290                                  /* check for explicit local */
291      if (*nam == CNTXMARK)
292          if (lvl > 0)            /* only action is to refuse search */
# Line 329 | Line 297 | int  lvl;
297          return(lvl > 0 ? NULL : nam);
298                                  /* copy name to static buffer */
299      while (*nam) {
300 <        if (cp >= nambuf+MAXWORD)
300 >        if (cp >= nambuf+RMAXWORD)
301                  goto toolong;
302          *cp++ = *nam++;
303      }
# Line 348 | Line 316 | int  lvl;
316              ;
317      }
318      while (*cpp) {              /* copy context to static buffer */
319 <        if (cp >= nambuf+MAXWORD)
319 >        if (cp >= nambuf+RMAXWORD)
320              goto toolong;
321          *cp++ = *cpp++;
322      }
# Line 359 | Line 327 | toolong:
327  
328  
329   int
330 < incontext(qn)                   /* is qualified name in current context? */
331 < register char  *qn;
330 > incontext(                      /* is qualified name in current context? */
331 >        char  *qn
332 > )
333   {
334      if (!context[0])                    /* global context accepts all */
335          return(1);
# Line 371 | Line 340 | register char  *qn;
340  
341  
342   void
343 < chanout(cs)                     /* set output channels */
344 < int  (*cs)();
343 > chanout(                        /* set output channels */
344 >        void  (*cs)(int n, double v)
345 > )
346   {
347 <    register EPNODE  *ep;
347 >    EPNODE  *ep;
348  
349      for (ep = outchan; ep != NULL; ep = ep->sibling)
350          (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling));
# Line 383 | Line 353 | int  (*cs)();
353  
354  
355   void
356 < dcleanup(lvl)           /* clear definitions (0->vars,1->output,2->consts) */
357 < int  lvl;
356 > dcleanup(               /* clear definitions (0->vars,1->output,2->consts) */
357 >        int  lvl
358 > )
359   {
360 <    register int  i;
361 <    register VARDEF  *vp;
362 <    register EPNODE  *ep;
360 >    int  i;
361 >    VARDEF  *vp;
362 >    EPNODE  *ep;
363                                  /* if context is global, clear all */
364      for (i = 0; i < NHASH; i++)
365          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
366 <            if (incontext(vp->name))
366 >            if (incontext(vp->name)) {
367                  if (lvl >= 2)
368                      dremove(vp->name);
369                  else
370                      dclear(vp->name);
371 +            }
372      if (lvl >= 1) {
373          for (ep = outchan; ep != NULL; ep = ep->sibling)
374              epfree(ep);
# Line 406 | Line 378 | int  lvl;
378  
379  
380   EPNODE *
381 < dlookup(name)                   /* look up a definition */
382 < char  *name;
381 > dlookup(                        /* look up a definition */
382 >        char  *name
383 > )
384   {
385 <    register VARDEF  *vp;
385 >    VARDEF  *vp;
386      
387      if ((vp = varlookup(name)) == NULL)
388          return(NULL);
# Line 418 | Line 391 | char  *name;
391  
392  
393   VARDEF *
394 < varlookup(name)                 /* look up a variable */
395 < char  *name;
394 > varlookup(                      /* look up a variable */
395 >        char  *name
396 > )
397   {
398      int  lvl = 0;
399 <    register char  *qname;
400 <    register VARDEF  *vp;
399 >    char  *qname;
400 >    VARDEF  *vp;
401                                  /* find most qualified match */
402      while ((qname = qualname(name, lvl++)) != NULL)
403          for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
# Line 434 | Line 408 | char  *name;
408  
409  
410   VARDEF *
411 < varinsert(name)                 /* get a link to a variable */
412 < char  *name;
411 > varinsert(                      /* get a link to a variable */
412 >        char  *name
413 > )
414   {
415 <    register VARDEF  *vp;
415 >    VARDEF  *vp;
416      int  hv;
417      
418      if ((vp = varlookup(name)) != NULL) {
# Line 459 | Line 434 | char  *name;
434  
435  
436   void
437 < libupdate(fn)                   /* update library links */
438 < char  *fn;
437 > libupdate(                      /* update library links */
438 >        char  *fn
439 > )
440   {
441 <    register int  i;
442 <    register VARDEF  *vp;
441 >    int  i;
442 >    VARDEF  *vp;
443                                          /* if fn is NULL then relink all */
444      for (i = 0; i < NHASH; i++)
445          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
446 <            if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name))
446 >            if ((vp->lib != NULL) | (fn == NULL) || !strcmp(fn, vp->name))
447                  vp->lib = liblookup(vp->name);
448   }
449  
450  
451   void
452 < varfree(ln)                             /* release link to variable */
453 < register VARDEF  *ln;
452 > varfree(                                /* release link to variable */
453 >        VARDEF   *ln
454 > )
455   {
456 <    register VARDEF  *vp;
456 >    VARDEF  *vp;
457      int  hv;
458  
459      if (--ln->nlinks > 0)
# Line 497 | Line 474 | register VARDEF         *ln;
474  
475  
476   EPNODE *
477 < dfirst()                        /* return pointer to first definition */
477 > dfirst(void)                    /* return pointer to first definition */
478   {
479      htndx = 0;
480      htpos = NULL;
# Line 507 | Line 484 | dfirst()                       /* return pointer to first definition */
484  
485  
486   EPNODE *
487 < dnext()                         /* return pointer to next definition */
487 > dnext(void)                             /* return pointer to next definition */
488   {
489 <    register EPNODE  *ep;
490 <    register char  *nm;
489 >    EPNODE  *ep;
490 >    char  *nm;
491  
492      while (htndx < NHASH) {
493          if (htpos == NULL)
# Line 530 | Line 507 | dnext()                                /* return pointer to next definition */
507  
508  
509   EPNODE *
510 < dpop(name)                      /* pop a definition */
511 < char  *name;
510 > dpop(                   /* pop a definition */
511 >        char  *name
512 > )
513   {
514 <    register VARDEF  *vp;
515 <    register EPNODE  *dp;
514 >    VARDEF  *vp;
515 >    EPNODE  *dp;
516      
517      if ((vp = varlookup(name)) == NULL || vp->def == NULL)
518          return(NULL);
# Line 546 | Line 524 | char  *name;
524  
525  
526   void
527 < dpush(nm, ep)                   /* push on a definition */
528 < char  *nm;
529 < register EPNODE  *ep;
527 > dpush(                  /* push on a definition */
528 >        char  *nm,
529 >        EPNODE   *ep
530 > )
531   {
532 <    register VARDEF  *vp;
532 >    VARDEF  *vp;
533  
534      vp = varinsert(nm);
535      ep->sibling = vp->def;
# Line 559 | Line 538 | register EPNODE         *ep;
538  
539  
540   void
541 < addchan(sp)                     /* add an output channel assignment */
542 < EPNODE  *sp;
541 > addchan(                        /* add an output channel assignment */
542 >        EPNODE  *sp
543 > )
544   {
545      int  ch = sp->v.kid->v.chan;
546 <    register EPNODE  *ep, *epl;
546 >    EPNODE  *ep, *epl;
547  
548      for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling)
549          if (ep->v.kid->v.chan >= ch) {
# Line 589 | Line 569 | EPNODE *sp;
569  
570  
571   void
572 < getstatement()                  /* get next statement */
572 > getstatement(void)                      /* get next statement */
573   {
574 <    register EPNODE  *ep;
574 >    EPNODE  *ep;
575      char  *qname;
576 <    register VARDEF  *vdef;
576 >    VARDEF  *vdef;
577  
578      if (nextc == ';') {         /* empty statement */
579          scan();
# Line 606 | Line 586 | getstatement()                 /* get next statement */
586      } else {                            /* ordinary definition */
587          ep = getdefn();
588          qname = qualname(dname(ep), 0);
589 <        if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL)
589 >        if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) {
590              if (vdef->def != NULL && epcmp(ep, vdef->def)) {
591                  wputs(qname);
592                  if (vdef->def->type == ':')
# Line 617 | Line 597 | getstatement()                 /* get next statement */
597                  wputs(qname);
598                  wputs(": definition hides library function\n");
599              }
600 +        }
601          if (ep->type == ':')
602              dremove(qname);
603          else
# Line 632 | Line 613 | getstatement()                 /* get next statement */
613  
614  
615   EPNODE *
616 < getdefn()                       /* A -> SYM = E1 */
617 <                                /*      SYM : E1 */
618 <                                /*      FUNC(SYM,..) = E1 */
619 <                                /*      FUNC(SYM,..) : E1 */
616 > getdefn(void)
617 >        /* A -> SYM = E1 */
618 >        /*      SYM : E1 */
619 >        /*      FUNC(SYM,..) = E1 */
620 >        /*      FUNC(SYM,..) : E1 */
621   {
622 <    register EPNODE  *ep1, *ep2;
622 >    EPNODE  *ep1, *ep2;
623  
624 <    if (!isalpha(nextc) && nextc != CNTXMARK)
624 >    if (!isalpha(nextc) & (nextc != CNTXMARK))
625          syntax("illegal variable name");
626  
627      ep1 = newnode();
# Line 654 | Line 636 | getdefn()                      /* A -> SYM = E1 */
636          do {
637              scan();
638              if (!isalpha(nextc))
639 <                syntax("illegal variable name");
639 >                syntax("illegal parameter name");
640              ep2 = newnode();
641              ep2->type = SYM;
642              ep2->v.name = savestr(getname());
643 +            if (strchr(ep2->v.name, CNTXMARK) != NULL)
644 +                syntax("illegal parameter name");
645              addekid(ep1, ep2);
646          } while (nextc == ',');
647          if (nextc != ')')
# Line 666 | Line 650 | getdefn()                      /* A -> SYM = E1 */
650          curfunc = ep1;
651      }
652  
653 <    if (nextc != '=' && nextc != ':')
653 >    if ((nextc != '=') & (nextc != ':'))
654          syntax("'=' or ':' expected");
655  
656      ep2 = newnode();
# Line 677 | Line 661 | getdefn()                      /* A -> SYM = E1 */
661  
662      if (ep1->type == SYM && ep1->sibling->type != NUM) {
663          ep1 = newnode();
664 <        ep1->type = TICK;
664 >        ep1->type = CLKT;
665          ep1->v.tick = 0;
666          addekid(ep2, ep1);
667          ep1 = newnode();
# Line 691 | Line 675 | getdefn()                      /* A -> SYM = E1 */
675  
676  
677   EPNODE *
678 < getchan()                       /* A -> $N = E1 */
678 > getchan(void)                   /* A -> $N = E1 */
679   {
680 <    register EPNODE  *ep1, *ep2;
680 >    EPNODE  *ep1, *ep2;
681  
682      if (nextc != '$')
683          syntax("missing '$'");
# Line 722 | Line 706 | getchan()                      /* A -> $N = E1 */
706   */
707  
708  
709 < static double
710 < dvalue(name, d)                 /* evaluate a variable */
727 < char  *name;
728 < EPNODE  *d;
709 > static double                   /* evaluate a variable */
710 > dvalue(char *name, EPNODE *d)
711   {
712 <    register EPNODE  *ep1, *ep2;
712 >    EPNODE  *ep1, *ep2;
713      
714      if (d == NULL || d->v.kid->type != SYM) {
715          eputs(name);
# Line 737 | Line 719 | EPNODE *d;
719      ep1 = d->v.kid->sibling;                    /* get expression */
720      if (ep1->type == NUM)
721          return(ep1->v.num);                     /* return if number */
722 +    if (esupport&E_RCONST && d->type == ':') {
723 +        wputs(name);
724 +        wputs(": assigned non-constant value\n");
725 +    }
726      ep2 = ep1->sibling;                         /* check time */
727      if (eclock >= MAXCLOCK)
728          eclock = 1;                             /* wrap clock counter */
729      if (ep2->v.tick < MAXCLOCK &&
730 <                ep2->v.tick == 0 | ep2->v.tick != eclock) {
730 >                (ep2->v.tick == 0) | (ep2->v.tick != eclock)) {
731          ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock;
732          ep2 = ep2->sibling;
733          ep2->v.num = evalue(ep1);               /* needs new value */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines