xref: /original-bsd/usr.bin/pascal/pdx/tree/prtree.c (revision 0b685140)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)prtree.c 1.1 01/18/82";
4 
5 /*
6  * Print a tree back out in Pascal form.
7  */
8 
9 #include "defs.h"
10 #include "tree.h"
11 #include "sym.h"
12 #include "sym/btypes.h"
13 #include "tree.rep"
14 
15 prtree(p)
16 NODE *p;
17 {
18 	OP op;
19 
20 	if (p == NIL) {
21 		return;
22 	}
23 	op = p->op;
24 	if (op < O_NOP || op > O_LASTOP) {
25 		panic("bad op %d in prtree", p->op);
26 	}
27 	switch (op) {
28 		case O_NAME: {
29 			SYM *s;
30 
31 			s = p->nameval;
32 			if (isredirected() || isambiguous(s)) {
33 				printwhich(s);
34 			} else {
35 				printf("%s", name(s));
36 			}
37 			break;
38 		}
39 
40 		case O_QNAME:
41 			prtree(p->left);
42 			printf(".%s", name(p->right->nameval));
43 			break;
44 
45 		case O_QLINE:
46 			prtree(p->left);
47 			printf(":");
48 			prtree(p->right);
49 			break;
50 
51 		case O_LCON:
52 			push(long, p->lconval);
53 			printval(p->nodetype);
54 			break;
55 
56 		case O_FCON:
57 			printf("%g", p->fconval);
58 			break;
59 
60 		case O_SCON:
61 			printf("'%s'", p->sconval);
62 			break;
63 
64 		case O_INDEX:
65 			prtree(p->left);
66 			printf("[");
67 			prtree(p->right);
68 			printf("]");
69 			break;
70 
71 		case O_COMMA:
72 			prtree(p->left);
73 			if (p->right != NIL) {
74 				printf(", ");
75 				prtree(p->right);
76 			}
77 			break;
78 
79 		case O_RVAL:
80 		case O_ITOF:
81 			prtree(p->left);
82 			break;
83 
84 		case O_CALL:
85 			prtree(p->left);
86 			if (p->right != NIL) {
87 				printf("(");
88 				prtree(p->right);
89 				printf(")");
90 			}
91 			break;
92 
93 		case O_INDIR:
94 			prtree(p->left);
95 			if (!isvarparam(p->left->nodetype)) {
96 				printf("^");
97 			}
98 			break;
99 
100 		default:
101 			switch(degree(op)) {
102 				case BINARY:
103 					prtree(p->left);
104 					printf("%s", opinfo[op].opstring);
105 					prtree(p->right);
106 					break;
107 
108 				case UNARY:
109 					printf("%s", opinfo[op].opstring);
110 					prtree(p->left);
111 					break;
112 
113 				default:
114 					panic("bad op %d in prtree", op);
115 			}
116 			break;
117 	}
118 }
119 
120 /*
121  * Print an error associated with a particular tree.
122  * The string is searched for a "%t" which is replaced by
123  * the printed representation of the tree.
124  */
125 
126 /* VARARGS2 */
127 trerror(s, tree, a, b, c, d, e, f, g, h, i, j)
128 char *s;
129 NODE *tree;
130 {
131 	register char *p;
132 
133 	fflush(stdout);
134 	for (p = s; *p != '\0'; p++) {
135 		if (p[0] == '%' && p[1] == 't') {
136 			fputc('"', stderr);
137 			prtree(tree);
138 			fflush(stdout);
139 			fputc('"', stderr);
140 			error(&p[2], a, b, c, d, e, f, g, h, i, j);
141 			/* NOTREACHED */
142 		}
143 		fputc(*p, stderr);
144 	}
145 	panic("bad call to trerror");
146 }
147