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