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.28 by greg, Thu Apr 2 18:00:34 2020 UTC vs.
Revision 2.38 by greg, Fri Feb 23 03:47:57 2024 UTC

# Line 58 | Line 58 | static VARDEF  *htpos;                 /* ...dfirst() and */
58   static EPNODE  *ochpos;                 /* ...dnext */
59   static EPNODE  *outchan;
60  
61 + static int  optimized = 0;              /* are we optimized? */
62 +
63   EPNODE  *curfunc = NULL;
62 #define  dname(ep)      ((ep)->v.kid->type == SYM ? \
63                        (ep)->v.kid->v.name : \
64                        (ep)->v.kid->v.kid->v.name)
64  
65  
66   void
# Line 78 | Line 77 | fcompile(                      /* get definitions from a file */
77          eputs(": cannot open\n");
78          quit(1);
79      }
80 <    initfile(fp, fname, 0);
80 > #ifdef getc_unlocked                    /* avoid stupid semaphores */
81 >    flockfile(fp);
82 > #endif
83 >   initfile(fp, fname, 0);
84      while (nextc != EOF)
85          getstatement();
86      if (fname != NULL)
87          fclose(fp);
88 + #ifdef getc_unlocked
89 +    else
90 +        funlockfile(fp);
91 + #endif
92   }
93  
94  
# Line 171 | Line 177 | dclear(                        /* delete variable definitions of name */
177          char  *name
178   )
179   {
180 <    EPNODE  *ep;
180 >    VARDEF  *vp;
181 >    EPNODE  *dp;
182  
183 <    while ((ep = dpop(name)) != NULL) {
184 <        if (ep->type == ':') {
185 <            dpush(name, ep);            /* don't clear constants */
186 <            return;
187 <        }
181 <        epfree(ep);
183 >    while ((vp = varlookup(name)) != NULL &&
184 >                (dp = vp->def) != NULL && dp->type == '=') {
185 >        vp->def = dp->sibling;
186 >        epfree(dp,1);
187 >        varfree(vp);
188      }
189   }
190  
# Line 191 | Line 197 | dremove(                       /* delete all definitions of name */
197      EPNODE  *ep;
198  
199      while ((ep = dpop(name)) != NULL)
200 <        epfree(ep);
200 >        epfree(ep,1);
201   }
202  
203  
204   int
205 < vardefined(     /* return non-zero if variable defined */
205 > vardefined(     /* return '=' or ':' if variable/constant defined */
206          char  *name
207   )
208   {
209 <    EPNODE  *dp;
209 >    EPNODE  *dp = dlookup(name);
210  
211 <    return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM);
211 >    if (dp == NULL || dp->v.kid->type != SYM)
212 >        return(0);
213 >
214 >    return(dp->type);
215   }
216  
217  
218   char *
219 < setcontext(                     /* set a new context path */
219 > calcontext(                     /* set a new context path */
220          char  *ctx
221   )
222   {
# Line 248 | Line 257 | pushcontext(           /* push on another context */
257      int  n;
258  
259      strcpy(oldcontext, context);        /* save old context */
260 <    setcontext(ctx);                    /* set new context */
260 >    calcontext(ctx);                    /* set new context */
261      n = strlen(context);                /* tack on old */
262      if (n+strlen(oldcontext) > MAXCNTX) {
263          strncpy(context+n, oldcontext, MAXCNTX-n);
# Line 285 | Line 294 | qualname(              /* get qualified name */
294      static char  nambuf[RMAXWORD+1];
295      char  *cp = nambuf, *cpp;
296                                  /* check for explicit local */
297 <    if (*nam == CNTXMARK)
297 >    if (*nam == CNTXMARK) {
298          if (lvl > 0)            /* only action is to refuse search */
299              return(NULL);
300 <        else
301 <            nam++;
293 <    else if (nam == nambuf)     /* check for repeat call */
300 >        nam++;
301 >    } else if (nam == nambuf)   /* check for repeat call */
302          return(lvl > 0 ? NULL : nam);
303                                  /* copy name to static buffer */
304      while (*nam) {
# Line 312 | Line 320 | qualname(              /* get qualified name */
320          while (*++cpp && *cpp != CNTXMARK)
321              ;
322      }
323 <    while (*cpp) {              /* copy context to static buffer */
323 >    while (*cpp) {              /* add remaining context to name */
324          if (cp >= nambuf+RMAXWORD)
325              goto toolong;
326          *cp++ = *cpp++;
# Line 350 | Line 358 | chanout(                       /* set output channels */
358  
359  
360   void
361 + doptimize(int activate)         /* optimize current and future definitions? */
362 + {
363 +    EPNODE      *ep;
364 +
365 +    if (activate && optimized)
366 +        return;                 /* already going */
367 +
368 +    if (!(optimized = activate))
369 +        return;                 /* switching off */
370 +
371 +    for (ep = dfirst(); ep != NULL; ep = dnext())
372 +        epoptimize(ep);
373 + }
374 +
375 +
376 + void
377   dcleanup(               /* clear definitions (0->vars,1->output,2->consts) */
378          int  lvl
379   )
# Line 366 | Line 390 | dcleanup(              /* clear definitions (0->vars,1->output,2->
390                  else
391                      dclear(vp->name);
392              }
393 <    if (lvl >= 1) {
394 <        for (ep = outchan; ep != NULL; ep = ep->sibling)
395 <            epfree(ep);
396 <        outchan = NULL;
397 <    }
393 >    if (lvl >= 1)
394 >        while (outchan != NULL) {
395 >            ep = outchan;
396 >            outchan = ep->sibling;
397 >            epfree(ep,1);
398 >        }
399   }
400  
401  
# Line 440 | Line 465 | libupdate(                     /* update library links */
465                                          /* if fn is NULL then relink all */
466      for (i = 0; i < NHASH; i++)
467          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
468 <            if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name))
468 >            if ((vp->lib != NULL) | (fn == NULL) || !strcmp(fn, vp->name))
469                  vp->lib = liblookup(vp->name);
470   }
471  
# Line 466 | Line 491 | varfree(                               /* release link to variable */
491          vp->next = ln->next;
492      }
493      freestr(ln->name);
494 <    efree((char *)ln);
494 >    efree(ln);
495   }
496  
497  
# Line 552 | Line 577 | addchan(                       /* add an output channel assignment */
577                  sp->sibling = ep;
578              else {
579                  sp->sibling = ep->sibling;
580 <                epfree(ep);
580 >                epfree(ep,1);
581              }
582              return;
583          }
# Line 582 | Line 607 | getstatement(void)                     /* get next statement */
607          addchan(ep);
608      } else {                            /* ordinary definition */
609          ep = getdefn();
610 <        qname = qualname(dname(ep), 0);
610 >        qname = qualname(dfn_name(ep), 0);
611          if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) {
612              if (vdef->def != NULL && epcmp(ep, vdef->def)) {
613                  wputs(qname);
# Line 606 | Line 631 | getstatement(void)                     /* get next statement */
631              syntax("';' expected");
632          scan();
633      }
634 +    if (optimized)
635 +        epoptimize(ep);                 /* optimize new statement */
636   }
637  
638  
# Line 618 | Line 645 | getdefn(void)
645   {
646      EPNODE  *ep1, *ep2;
647  
648 <    if (!isalpha(nextc) && nextc != CNTXMARK)
648 >    if (!isalpha(nextc) & (nextc != CNTXMARK))
649          syntax("illegal variable name");
650  
651      ep1 = newnode();
# Line 637 | Line 664 | getdefn(void)
664              ep2 = newnode();
665              ep2->type = SYM;
666              ep2->v.name = savestr(getname());
667 +            if (strchr(ep2->v.name, CNTXMARK) != NULL)
668 +                syntax("illegal parameter name");
669              addekid(ep1, ep2);
670          } while (nextc == ',');
671          if (nextc != ')')
# Line 645 | Line 674 | getdefn(void)
674          curfunc = ep1;
675      }
676  
677 <    if (nextc != '=' && nextc != ':')
677 >    if ((nextc != '=') & (nextc != ':'))
678          syntax("'=' or ':' expected");
679  
680      ep2 = newnode();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines