blob: c331240ea99bcc30af89fd0d5be62283bc4ddd4f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Parse tree node implementation */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035#include "node.h"
36
37node *
Guido van Rossum3f5da241990-12-20 15:06:42 +000038newtree(type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039 int type;
40{
41 node *n = NEW(node, 1);
42 if (n == NULL)
43 return NULL;
44 n->n_type = type;
45 n->n_str = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000046 n->n_lineno = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047 n->n_nchildren = 0;
48 n->n_child = NULL;
49 return n;
50}
51
52#define XXX 3 /* Node alignment factor to speed up realloc */
53#define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX)
54
55node *
Guido van Rossum3f5da241990-12-20 15:06:42 +000056addchild(n1, type, str, lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 register node *n1;
58 int type;
59 char *str;
Guido van Rossum3f5da241990-12-20 15:06:42 +000060 int lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
62 register int nch = n1->n_nchildren;
63 register int nch1 = nch+1;
64 register node *n;
65 if (XXXROUNDUP(nch) < nch1) {
66 n = n1->n_child;
67 nch1 = XXXROUNDUP(nch1);
68 RESIZE(n, node, nch1);
69 if (n == NULL)
70 return NULL;
71 n1->n_child = n;
72 }
73 n = &n1->n_child[n1->n_nchildren++];
74 n->n_type = type;
75 n->n_str = str;
Guido van Rossum3f5da241990-12-20 15:06:42 +000076 n->n_lineno = lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 n->n_nchildren = 0;
78 n->n_child = NULL;
79 return n;
80}
Guido van Rossum03a24cd1990-11-18 17:37:06 +000081
Guido van Rossum3f5da241990-12-20 15:06:42 +000082/* Forward */
83static void freechildren PROTO((node *));
84
85
86void
87freetree(n)
88 node *n;
89{
90 if (n != NULL) {
91 freechildren(n);
92 DEL(n);
93 }
94}
95
Guido van Rossum03a24cd1990-11-18 17:37:06 +000096static void
97freechildren(n)
98 node *n;
99{
100 int i;
101 for (i = NCH(n); --i >= 0; )
102 freechildren(CHILD(n, i));
103 if (n->n_child != NULL)
104 DEL(n->n_child);
105 if (STR(n) != NULL)
106 DEL(STR(n));
107}