blob: 9ce7a31f27f96592e896e8736f905c7eeaf2c6c0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Parse tree node implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028#include "node.h"
29
30node *
Guido van Rossum3f5da241990-12-20 15:06:42 +000031newtree(type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032 int type;
33{
34 node *n = NEW(node, 1);
35 if (n == NULL)
36 return NULL;
37 n->n_type = type;
38 n->n_str = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000039 n->n_lineno = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040 n->n_nchildren = 0;
41 n->n_child = NULL;
42 return n;
43}
44
45#define XXX 3 /* Node alignment factor to speed up realloc */
46#define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX)
47
48node *
Guido van Rossum3f5da241990-12-20 15:06:42 +000049addchild(n1, type, str, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050 register node *n1;
51 int type;
52 char *str;
Guido van Rossum3f5da241990-12-20 15:06:42 +000053 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054{
55 register int nch = n1->n_nchildren;
56 register int nch1 = nch+1;
57 register node *n;
58 if (XXXROUNDUP(nch) < nch1) {
59 n = n1->n_child;
60 nch1 = XXXROUNDUP(nch1);
61 RESIZE(n, node, nch1);
62 if (n == NULL)
63 return NULL;
64 n1->n_child = n;
65 }
66 n = &n1->n_child[n1->n_nchildren++];
67 n->n_type = type;
68 n->n_str = str;
Guido van Rossum3f5da241990-12-20 15:06:42 +000069 n->n_lineno = lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 n->n_nchildren = 0;
71 n->n_child = NULL;
72 return n;
73}
Guido van Rossum03a24cd1990-11-18 17:37:06 +000074
Guido van Rossum3f5da241990-12-20 15:06:42 +000075/* Forward */
76static void freechildren PROTO((node *));
77
78
79void
80freetree(n)
81 node *n;
82{
83 if (n != NULL) {
84 freechildren(n);
85 DEL(n);
86 }
87}
88
Guido van Rossum03a24cd1990-11-18 17:37:06 +000089static void
90freechildren(n)
91 node *n;
92{
93 int i;
94 for (i = NCH(n); --i >= 0; )
95 freechildren(CHILD(n, i));
96 if (n->n_child != NULL)
97 DEL(n->n_child);
98 if (STR(n) != NULL)
99 DEL(STR(n));
100}