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.22 by schorsch, Sun Mar 28 20:33:12 2004 UTC vs.
Revision 2.36 by greg, Tue Sep 26 18:33:14 2023 UTC

# Line 27 | Line 27 | static const char      RCSid[] = "$Id$";
27  
28   #include "copyright.h"
29  
30 #include  <stdio.h>
31 #include  <string.h>
30   #include  <ctype.h>
31  
32   #include  "rterror.h"
# Line 61 | Line 59 | static EPNODE  *ochpos;                        /* ...dnext */
59   static EPNODE  *outchan;
60  
61   EPNODE  *curfunc = NULL;
64 #define  dname(ep)      ((ep)->v.kid->type == SYM ? \
65                        (ep)->v.kid->v.name : \
66                        (ep)->v.kid->v.kid->v.name)
62  
63  
64   void
# Line 80 | Line 75 | fcompile(                      /* get definitions from a file */
75          eputs(": cannot open\n");
76          quit(1);
77      }
78 <    initfile(fp, fname, 0);
78 > #ifdef getc_unlocked                    /* avoid stupid semaphores */
79 >    flockfile(fp);
80 > #endif
81 >   initfile(fp, fname, 0);
82      while (nextc != EOF)
83          getstatement();
84      if (fname != NULL)
85          fclose(fp);
86 + #ifdef getc_unlocked
87 +    else
88 +        funlockfile(fp);
89 + #endif
90   }
91  
92  
# Line 115 | Line 117 | evariable(                     /* evaluate a variable */
117          EPNODE  *ep
118   )
119   {
120 <    register VARDEF  *dp = ep->v.ln;
120 >    VARDEF  *dp = ep->v.ln;
121  
122      return(dvalue(dp->name, dp->def));
123   }
# Line 129 | Line 131 | varset(                /* set a variable's value */
131   )
132   {
133      char  *qname;
134 <    register EPNODE  *ep1, *ep2;
134 >    EPNODE  *ep1, *ep2;
135                                          /* get qualified name */
136      qname = qualname(vname, 0);
137                                          /* check for quick set */
138 <    if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) {
138 >    if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM &&
139 >                (ep1->type == ':') <= (assign == ':')) {
140          ep2 = ep1->v.kid->sibling;
141          if (ep2->type == NUM) {
142              ep2->v.num = val;
# Line 141 | Line 144 | varset(                /* set a variable's value */
144              return;
145          }
146      }
147 +    if (ep1 != NULL && esupport&E_REDEFW) {
148 +        wputs(qname);
149 +        if (ep1->type == ':')
150 +            wputs(": reset constant expression\n");
151 +        else
152 +            wputs(": reset expression\n");
153 +    }
154                                          /* hand build definition */
155      ep1 = newnode();
156      ep1->type = assign;
# Line 152 | Line 162 | varset(                /* set a variable's value */
162      ep2->type = NUM;
163      ep2->v.num = val;
164      addekid(ep1, ep2);
165 <    dremove(qname);
165 >    if (assign == ':')
166 >        dremove(qname);
167 >    else
168 >        dclear(qname);
169      dpush(qname, ep1);
170   }
171  
# Line 162 | Line 175 | dclear(                        /* delete variable definitions of name */
175          char  *name
176   )
177   {
178 <    register EPNODE  *ep;
178 >    EPNODE  *ep;
179  
180      while ((ep = dpop(name)) != NULL) {
181          if (ep->type == ':') {
# Line 179 | Line 192 | dremove(                       /* delete all definitions of name */
192          char  *name
193   )
194   {
195 <    register EPNODE  *ep;
195 >    EPNODE  *ep;
196  
197      while ((ep = dpop(name)) != NULL)
198          epfree(ep);
# Line 187 | Line 200 | dremove(                       /* delete all definitions of name */
200  
201  
202   int
203 < vardefined(     /* return non-zero if variable defined */
203 > vardefined(     /* return '=' or ':' if variable/constant defined */
204          char  *name
205   )
206   {
207 <    register EPNODE  *dp;
207 >    EPNODE  *dp = dlookup(name);
208  
209 <    return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM);
209 >    if (dp == NULL || dp->v.kid->type != SYM)
210 >        return(0);
211 >
212 >    return(dp->type);
213   }
214  
215  
216   char *
217 < setcontext(                     /* set a new context path */
218 <        register char  *ctx
217 > calcontext(                     /* set a new context path */
218 >        char  *ctx
219   )
220   {
221 <    register char  *cpp;
221 >    char  *cpp;
222  
223      if (ctx == NULL)
224          return(context);                /* just asking */
# Line 236 | Line 252 | pushcontext(           /* push on another context */
252   )
253   {
254      char  oldcontext[MAXCNTX+1];
255 <    register int  n;
255 >    int  n;
256  
257      strcpy(oldcontext, context);        /* save old context */
258 <    setcontext(ctx);                    /* set new context */
258 >    calcontext(ctx);                    /* set new context */
259      n = strlen(context);                /* tack on old */
260      if (n+strlen(oldcontext) > MAXCNTX) {
261          strncpy(context+n, oldcontext, MAXCNTX-n);
# Line 253 | Line 269 | pushcontext(           /* push on another context */
269   char *
270   popcontext(void)                        /* pop off top context */
271   {
272 <    register char  *cp1, *cp2;
272 >    char  *cp1, *cp2;
273  
274      if (!context[0])                    /* nothing left to pop */
275          return(context);
# Line 269 | Line 285 | popcontext(void)                       /* pop off top context */
285  
286   char *
287   qualname(               /* get qualified name */
288 <        register char  *nam,
288 >        char  *nam,
289          int  lvl
290   )
291   {
292      static char  nambuf[RMAXWORD+1];
293 <    register char  *cp = nambuf, *cpp;
293 >    char  *cp = nambuf, *cpp;
294                                  /* check for explicit local */
295 <    if (*nam == CNTXMARK)
295 >    if (*nam == CNTXMARK) {
296          if (lvl > 0)            /* only action is to refuse search */
297              return(NULL);
298 <        else
299 <            nam++;
284 <    else if (nam == nambuf)     /* check for repeat call */
298 >        nam++;
299 >    } else if (nam == nambuf)   /* check for repeat call */
300          return(lvl > 0 ? NULL : nam);
301                                  /* copy name to static buffer */
302      while (*nam) {
# Line 316 | Line 331 | toolong:
331  
332   int
333   incontext(                      /* is qualified name in current context? */
334 <        register char  *qn
334 >        char  *qn
335   )
336   {
337      if (!context[0])                    /* global context accepts all */
# Line 332 | Line 347 | chanout(                       /* set output channels */
347          void  (*cs)(int n, double v)
348   )
349   {
350 <    register EPNODE  *ep;
350 >    EPNODE  *ep;
351  
352      for (ep = outchan; ep != NULL; ep = ep->sibling)
353          (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling));
# Line 345 | Line 360 | dcleanup(              /* clear definitions (0->vars,1->output,2->
360          int  lvl
361   )
362   {
363 <    register int  i;
364 <    register VARDEF  *vp;
365 <    register EPNODE  *ep;
363 >    int  i;
364 >    VARDEF  *vp;
365 >    EPNODE  *ep;
366                                  /* if context is global, clear all */
367      for (i = 0; i < NHASH; i++)
368          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
# Line 357 | Line 372 | dcleanup(              /* clear definitions (0->vars,1->output,2->
372                  else
373                      dclear(vp->name);
374              }
375 <    if (lvl >= 1) {
376 <        for (ep = outchan; ep != NULL; ep = ep->sibling)
375 >    if (lvl >= 1)
376 >        while (outchan != NULL) {
377 >            ep = outchan;
378 >            outchan = ep->sibling;
379 >            ep->sibling = NULL;
380              epfree(ep);
381 <        outchan = NULL;
364 <    }
381 >        }
382   }
383  
384  
# Line 370 | Line 387 | dlookup(                       /* look up a definition */
387          char  *name
388   )
389   {
390 <    register VARDEF  *vp;
390 >    VARDEF  *vp;
391      
392      if ((vp = varlookup(name)) == NULL)
393          return(NULL);
# Line 384 | Line 401 | varlookup(                     /* look up a variable */
401   )
402   {
403      int  lvl = 0;
404 <    register char  *qname;
405 <    register VARDEF  *vp;
404 >    char  *qname;
405 >    VARDEF  *vp;
406                                  /* find most qualified match */
407      while ((qname = qualname(name, lvl++)) != NULL)
408          for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
# Line 400 | Line 417 | varinsert(                     /* get a link to a variable */
417          char  *name
418   )
419   {
420 <    register VARDEF  *vp;
420 >    VARDEF  *vp;
421      int  hv;
422      
423      if ((vp = varlookup(name)) != NULL) {
# Line 426 | Line 443 | libupdate(                     /* update library links */
443          char  *fn
444   )
445   {
446 <    register int  i;
447 <    register VARDEF  *vp;
446 >    int  i;
447 >    VARDEF  *vp;
448                                          /* if fn is NULL then relink all */
449      for (i = 0; i < NHASH; i++)
450          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
451 <            if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name))
451 >            if ((vp->lib != NULL) | (fn == NULL) || !strcmp(fn, vp->name))
452                  vp->lib = liblookup(vp->name);
453   }
454  
455  
456   void
457   varfree(                                /* release link to variable */
458 <        register VARDEF  *ln
458 >        VARDEF   *ln
459   )
460   {
461 <    register VARDEF  *vp;
461 >    VARDEF  *vp;
462      int  hv;
463  
464      if (--ln->nlinks > 0)
# Line 457 | Line 474 | varfree(                               /* release link to variable */
474          vp->next = ln->next;
475      }
476      freestr(ln->name);
477 <    efree((char *)ln);
477 >    efree(ln);
478   }
479  
480  
# Line 474 | Line 491 | dfirst(void)                   /* return pointer to first definition *
491   EPNODE *
492   dnext(void)                             /* return pointer to next definition */
493   {
494 <    register EPNODE  *ep;
495 <    register char  *nm;
494 >    EPNODE  *ep;
495 >    char  *nm;
496  
497      while (htndx < NHASH) {
498          if (htpos == NULL)
# Line 499 | Line 516 | dpop(                  /* pop a definition */
516          char  *name
517   )
518   {
519 <    register VARDEF  *vp;
520 <    register EPNODE  *dp;
519 >    VARDEF  *vp;
520 >    EPNODE  *dp;
521      
522      if ((vp = varlookup(name)) == NULL || vp->def == NULL)
523          return(NULL);
# Line 514 | Line 531 | dpop(                  /* pop a definition */
531   void
532   dpush(                  /* push on a definition */
533          char  *nm,
534 <        register EPNODE  *ep
534 >        EPNODE   *ep
535   )
536   {
537 <    register VARDEF  *vp;
537 >    VARDEF  *vp;
538  
539      vp = varinsert(nm);
540      ep->sibling = vp->def;
# Line 531 | Line 548 | addchan(                       /* add an output channel assignment */
548   )
549   {
550      int  ch = sp->v.kid->v.chan;
551 <    register EPNODE  *ep, *epl;
551 >    EPNODE  *ep, *epl;
552  
553      for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling)
554          if (ep->v.kid->v.chan >= ch) {
# Line 559 | Line 576 | addchan(                       /* add an output channel assignment */
576   void
577   getstatement(void)                      /* get next statement */
578   {
579 <    register EPNODE  *ep;
579 >    EPNODE  *ep;
580      char  *qname;
581 <    register VARDEF  *vdef;
581 >    VARDEF  *vdef;
582  
583      if (nextc == ';') {         /* empty statement */
584          scan();
# Line 573 | Line 590 | getstatement(void)                     /* get next statement */
590          addchan(ep);
591      } else {                            /* ordinary definition */
592          ep = getdefn();
593 <        qname = qualname(dname(ep), 0);
593 >        qname = qualname(dfn_name(ep), 0);
594          if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) {
595              if (vdef->def != NULL && epcmp(ep, vdef->def)) {
596                  wputs(qname);
# Line 607 | Line 624 | getdefn(void)
624          /*      FUNC(SYM,..) = E1 */
625          /*      FUNC(SYM,..) : E1 */
626   {
627 <    register EPNODE  *ep1, *ep2;
627 >    EPNODE  *ep1, *ep2;
628  
629 <    if (!isalpha(nextc) && nextc != CNTXMARK)
629 >    if (!isalpha(nextc) & (nextc != CNTXMARK))
630          syntax("illegal variable name");
631  
632      ep1 = newnode();
# Line 624 | Line 641 | getdefn(void)
641          do {
642              scan();
643              if (!isalpha(nextc))
644 <                syntax("illegal variable name");
644 >                syntax("illegal parameter name");
645              ep2 = newnode();
646              ep2->type = SYM;
647              ep2->v.name = savestr(getname());
648 +            if (strchr(ep2->v.name, CNTXMARK) != NULL)
649 +                syntax("illegal parameter name");
650              addekid(ep1, ep2);
651          } while (nextc == ',');
652          if (nextc != ')')
# Line 636 | Line 655 | getdefn(void)
655          curfunc = ep1;
656      }
657  
658 <    if (nextc != '=' && nextc != ':')
658 >    if ((nextc != '=') & (nextc != ':'))
659          syntax("'=' or ':' expected");
660  
661      ep2 = newnode();
# Line 647 | Line 666 | getdefn(void)
666  
667      if (ep1->type == SYM && ep1->sibling->type != NUM) {
668          ep1 = newnode();
669 <        ep1->type = TICK;
669 >        ep1->type = CLKT;
670          ep1->v.tick = 0;
671          addekid(ep2, ep1);
672          ep1 = newnode();
# Line 663 | Line 682 | getdefn(void)
682   EPNODE *
683   getchan(void)                   /* A -> $N = E1 */
684   {
685 <    register EPNODE  *ep1, *ep2;
685 >    EPNODE  *ep1, *ep2;
686  
687      if (nextc != '$')
688          syntax("missing '$'");
# Line 693 | Line 712 | getchan(void)                  /* A -> $N = E1 */
712  
713  
714   static double                   /* evaluate a variable */
715 < dvalue(char  *name, EPNODE      *d)
715 > dvalue(char *name, EPNODE *d)
716   {
717 <    register EPNODE  *ep1, *ep2;
717 >    EPNODE  *ep1, *ep2;
718      
719      if (d == NULL || d->v.kid->type != SYM) {
720          eputs(name);
# Line 705 | Line 724 | dvalue(char  *name, EPNODE     *d)
724      ep1 = d->v.kid->sibling;                    /* get expression */
725      if (ep1->type == NUM)
726          return(ep1->v.num);                     /* return if number */
727 +    if (esupport&E_RCONST && d->type == ':') {
728 +        wputs(name);
729 +        wputs(": assigned non-constant value\n");
730 +    }
731      ep2 = ep1->sibling;                         /* check time */
732      if (eclock >= MAXCLOCK)
733          eclock = 1;                             /* wrap clock counter */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines