1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* calprint.c - routines for printing calcomp expressions. |
9 |
* |
10 |
* 1/29/87 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include "calcomp.h" |
16 |
|
17 |
|
18 |
eprint(ep, fp) /* print a parse tree */ |
19 |
register EPNODE *ep; |
20 |
FILE *fp; |
21 |
{ |
22 |
static EPNODE *curdef = NULL; |
23 |
register EPNODE *ep1; |
24 |
|
25 |
switch (ep->type) { |
26 |
|
27 |
case VAR: |
28 |
fputs(ep->v.ln->name, fp); |
29 |
break; |
30 |
|
31 |
case SYM: |
32 |
fputs(ep->v.name, fp); |
33 |
break; |
34 |
|
35 |
case FUNC: |
36 |
eprint(ep->v.kid, fp); |
37 |
fputs("(", fp); |
38 |
ep1 = ep->v.kid->sibling; |
39 |
while (ep1 != NULL) { |
40 |
eprint(ep1, fp); |
41 |
if ((ep1 = ep1->sibling) != NULL) |
42 |
fputs(", ", fp); |
43 |
} |
44 |
fputs(")", fp); |
45 |
break; |
46 |
|
47 |
case ARG: |
48 |
if (curdef == NULL || curdef->v.kid->type != FUNC || |
49 |
(ep1 = ekid(curdef->v.kid, ep->v.chan)) == NULL) { |
50 |
eputs("Bad argument!\n"); |
51 |
quit(1); |
52 |
} |
53 |
eprint(ep1, fp); |
54 |
break; |
55 |
|
56 |
case NUM: |
57 |
fprintf(fp, "%.9g", ep->v.num); |
58 |
break; |
59 |
|
60 |
case UMINUS: |
61 |
if (ep->v.kid->type == UMINUS) { |
62 |
fputs("-(", fp); |
63 |
eprint(ep->v.kid, fp); |
64 |
fputs(")", fp); |
65 |
} else { |
66 |
fputs("-", fp); |
67 |
eprint(ep->v.kid, fp); |
68 |
} |
69 |
break; |
70 |
|
71 |
case CHAN: |
72 |
fprintf(fp, "$%d", ep->v.chan); |
73 |
break; |
74 |
|
75 |
case '=': |
76 |
case ':': |
77 |
ep1 = curdef; |
78 |
curdef = ep; |
79 |
eprint(ep->v.kid, fp); |
80 |
putc(' ', fp); |
81 |
putc(ep->type, fp); |
82 |
putc(' ', fp); |
83 |
eprint(ep->v.kid->sibling, fp); |
84 |
curdef = ep1; |
85 |
break; |
86 |
|
87 |
case '+': |
88 |
case '-': |
89 |
case '*': |
90 |
case '/': |
91 |
case '^': |
92 |
fputs("(", fp); |
93 |
eprint(ep->v.kid, fp); |
94 |
putc(' ', fp); |
95 |
putc(ep->type, fp); |
96 |
putc(' ', fp); |
97 |
eprint(ep->v.kid->sibling, fp); |
98 |
fputs(")", fp); |
99 |
break; |
100 |
|
101 |
default: |
102 |
eputs("Bad expression!\n"); |
103 |
quit(1); |
104 |
|
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
|
110 |
dprint(name, fp) /* print a definition (all if no name) */ |
111 |
char *name; |
112 |
FILE *fp; |
113 |
{ |
114 |
register EPNODE *ep; |
115 |
|
116 |
if (name == NULL) |
117 |
for (ep = dfirst(); ep != NULL; ep = dnext()) { |
118 |
eprint(ep, fp); |
119 |
fputs(";\n", fp); |
120 |
} |
121 |
else if ((ep = dlookup(name)) != NULL) { |
122 |
eprint(ep, fp); |
123 |
fputs(";\n", fp); |
124 |
} else { |
125 |
wputs(name); |
126 |
wputs(": undefined\n"); |
127 |
} |
128 |
} |