Pristine Ack-5.5
[Ack-5.5.git] / lang / m2 / comp / node.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7
8 /* N O D E   O F   A N   A B S T R A C T   P A R S E T R E E */
9
10 /* $Id: node.c,v 1.30 1994/06/24 12:41:56 ceriel Exp $ */
11
12 #include        "debug.h"
13
14 #include        <em_label.h>
15 #include        <em_arith.h>
16 #include        <alloc.h>
17 #include        <system.h>
18
19 #include        "LLlex.h"
20 #include        "def.h"
21 #include        "type.h"
22 #include        "node.h"
23 #include        "main.h"
24
25 static int      nsubnodes[] = {
26         0,
27         2,
28         2,
29         2,
30         2,
31         2,
32         1,
33         1,
34         2,
35         1,
36         2,
37         1,
38         2
39 };
40
41 t_node *
42 getnode(class)
43 {
44         register t_node *nd = new_node();
45
46         if (options['R']) nd->nd_flags |= ROPTION;
47         if (options['A']) nd->nd_flags |= AOPTION;
48         nd->nd_class = class;
49         return nd;
50 }
51
52 t_node *
53 dot2node(class, left, right)
54         t_node *left, *right;
55 {
56         register t_node *nd = getnode(class);
57
58         nd->nd_symb = dot.tk_symb;
59         nd->nd_lineno = dot.tk_lineno;
60         nd->nd_LEFT = left;
61         nd->nd_RIGHT = right;
62         return nd;
63 }
64
65 t_node *
66 dot2leaf(class)
67 {
68         register t_node *nd = getnode(class);
69
70         nd->nd_token = dot;
71         switch(nsubnodes[class]) {
72         case 1:
73                 nd->nd_NEXT = 0;
74                 break;
75         case 2:
76                 nd->nd_LEFT = 0;
77                 nd->nd_RIGHT = 0;
78                 break;
79         }
80         return nd;
81 }
82
83 FreeNode(nd)
84         register t_node *nd;
85 {
86         /*      Put nodes that are no longer needed back onto the free
87                 list
88         */
89         if (!nd) return;
90         switch(nsubnodes[nd->nd_class]) {
91         case 2:
92                 FreeNode(nd->nd_LEFT);
93                 FreeNode(nd->nd_RIGHT);
94                 break;
95         case 1:
96                 FreeNode(nd->nd_NEXT);
97                 break;
98         }
99         free_node(nd);
100 }
101
102 /*ARGSUSED*/
103 NodeCrash(expp)
104         t_node *expp;
105 {
106         crash("(NodeCrash) Illegal node");
107 }
108
109 /*ARGSUSED*/
110 PNodeCrash(expp)
111         t_node **expp;
112 {
113         crash("(PNodeCrash) Illegal node");
114 }
115
116 #ifdef DEBUG
117
118 extern char *symbol2str();
119
120 indnt(lvl)
121 {
122         while (lvl--) {
123                 print("  ");
124         }
125 }
126
127 printnode(nd, lvl)
128         register t_node *nd;
129 {
130         indnt(lvl);
131         print("Class: %d; Symbol: %s; Flags: %d\n", nd->nd_class, symbol2str(nd->nd_symb), nd->nd_flags);
132         if (nd->nd_type) {
133                 indnt(lvl);
134                 print("Type: ");
135                 DumpType(nd->nd_type);
136                 print("\n");
137         }
138 }
139
140 PrNode(nd, lvl)
141         register t_node *nd;
142 {
143         if (! nd) {
144                 indnt(lvl); print("<nilnode>\n");
145                 return;
146         }
147         printnode(nd, lvl);
148         switch(nsubnodes[nd->nd_class]) {
149         case 1:
150                 PrNode(nd->nd_LEFT, lvl + 1);
151                 PrNode(nd->nd_RIGHT, lvl + 1);
152                 break;
153         case 2:
154                 PrNode(nd->nd_NEXT, lvl + 1);
155                 break;
156         }
157 }
158 #endif /* DEBUG */