Back to index

radiance  4R0+20100331
calprnt.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: calprnt.c,v 2.5 2003/11/14 17:22:06 schorsch Exp $";
00003 #endif
00004 /*
00005  *  calprint.c - routines for printing calcomp expressions.
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 #include  <stdio.h>
00011 
00012 #include  "rterror.h"
00013 #include  "calcomp.h"
00014 
00015 
00016 void
00017 eprint(ep, fp)                     /* print a parse tree */
00018 register EPNODE  *ep;
00019 FILE  *fp;
00020 {
00021     static EPNODE  *curdef = NULL;
00022     register EPNODE  *ep1 = NULL;
00023 
00024     switch (ep->type) {
00025 
00026        case VAR:
00027            fputs(ep->v.ln->name, fp);
00028            break;
00029 
00030        case SYM:
00031            fputs(ep->v.name, fp);
00032            break;
00033 
00034        case FUNC:
00035            eprint(ep->v.kid, fp);
00036            fputc('(', fp);
00037            ep1 = ep->v.kid->sibling;
00038            while (ep1 != NULL) {
00039               eprint(ep1, fp);
00040               if ((ep1 = ep1->sibling) != NULL)
00041                   fputs(", ", fp);
00042            }
00043            fputc(')', fp);
00044            break;
00045 
00046        case ARG:
00047            if (curdef == NULL || curdef->v.kid->type != FUNC ||
00048                      (ep1 = ekid(curdef->v.kid, ep->v.chan)) == NULL) {
00049               eputs("Bad argument!\n");
00050               quit(1);
00051            }
00052            eprint(ep1, fp);
00053            break;
00054 
00055        case NUM:
00056            fprintf(fp, "%.9g", ep->v.num);
00057            break;
00058 
00059        case UMINUS:
00060            fputc('-', fp);
00061            eprint(ep->v.kid, fp);
00062            break;
00063 
00064        case CHAN:
00065            fprintf(fp, "$%d", ep->v.chan);
00066            break;
00067        
00068        case '=':
00069        case ':':
00070            ep1 = curdef;
00071            curdef = ep;
00072            eprint(ep->v.kid, fp);
00073            fputc(' ', fp);
00074            fputc(ep->type, fp);
00075            fputc(' ', fp);
00076            eprint(ep->v.kid->sibling, fp);
00077            curdef = ep1;
00078            break;
00079            
00080        case '+':
00081        case '-':
00082        case '*':
00083        case '/':
00084        case '^':
00085            fputc('(', fp);
00086            eprint(ep->v.kid, fp);
00087            fputc(' ', fp);
00088            fputc(ep->type, fp);
00089            fputc(' ', fp);
00090            eprint(ep->v.kid->sibling, fp);
00091            fputc(')', fp);
00092            break;
00093 
00094        default:
00095            eputs("Bad expression!\n");
00096            quit(1);
00097 
00098     }
00099 
00100 }
00101 
00102 
00103 void
00104 dprint(name, fp)            /* print a definition (all if no name) */
00105 char  *name;
00106 FILE  *fp;
00107 {
00108     register EPNODE  *ep;
00109     
00110     if (name == NULL)
00111        for (ep = dfirst(); ep != NULL; ep = dnext()) {
00112            eprint(ep, fp);
00113            fputs(";\n", fp);
00114        }
00115     else if ((ep = dlookup(name)) != NULL) {
00116        eprint(ep, fp);
00117        fputs(";\n", fp);
00118     } else {
00119        wputs(name);
00120        wputs(": undefined\n");
00121     }
00122 }