Pristine Ack-5.5
[Ack-5.5.git] / lang / pc / comp / node.c
1 /* 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 */
2
3 #include        "debug.h"
4
5 #include        <alloc.h>
6 #include        <em_arith.h>
7 #include        <em_label.h>
8 #include        <system.h>
9
10 #include        "LLlex.h"
11 #include        "node.h"
12 #include        "type.h"
13
14 struct node *
15 MkNode(class, left, right, token)
16         struct node *left, *right;
17         struct token *token;
18 {
19         /*      Create a node and initialize it with the given parameters
20         */
21         register struct node *nd = new_node();
22
23         nd->nd_left = left;
24         nd->nd_right = right;
25         nd->nd_token = *token;
26         nd->nd_class = class;
27         nd->nd_type = error_type;
28         return nd;
29 }
30
31 struct node *
32 MkLeaf(class, token)
33         struct token *token;
34 {
35         register struct node *nd = new_node();
36
37         nd->nd_left = nd->nd_right = NULLNODE;
38         nd->nd_token = *token;
39         nd->nd_type = error_type;
40         nd->nd_class = class;
41         return nd;
42 }
43
44 FreeNode(nd)
45         register struct node *nd;
46 {
47         /*      Put nodes that are no longer needed back onto the free list
48         */
49         if( !nd ) return;
50         FreeNode(nd->nd_left);
51         FreeNode(nd->nd_right);
52         free_node(nd);
53 }
54
55 NodeCrash(expp)
56         struct node *expp;
57 {
58         crash("Illegal node %d", expp->nd_class);
59 }
60
61 #ifdef DEBUG
62
63 extern char *symbol2str();
64
65 indnt(lvl)
66 {
67         while( lvl-- )
68                 print("  ");
69 }
70
71 printnode(nd, lvl)
72         register struct node *nd;
73 {
74         indnt(lvl);
75         print("Class: %d; Symbol: %s\n", nd->nd_class, symbol2str(nd->nd_symb));
76         if( nd->nd_type )       {
77                 indnt(lvl);
78                 print("Type: ");
79                 DumpType(nd->nd_type);
80                 print("\n");
81         }
82 }
83
84 PrNode(nd, lvl)
85         register struct node *nd;
86 {
87         if( !nd )       {
88                 indnt(lvl); print("<nilnode>\n");
89                 return;
90         }
91         PrNode(nd->nd_left, lvl + 1);
92         printnode(nd, lvl);
93         PrNode(nd->nd_right, lvl + 1);
94 }
95 #endif