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