Last set of CW Pro 5 projects (probably)
[python/dscho.git] / Parser / node.c
blob6844965ea837fe77410a3124becaf4199d84148e
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 #ifdef HAVE_LIMITS_H
12 #include <limits.h>
13 #endif
14 #ifndef INT_MAX
15 #define INT_MAX 2147483647
16 #endif
18 /* Parse tree node implementation */
20 #include "pgenheaders.h"
21 #include "node.h"
22 #include "errcode.h"
24 node *
25 PyNode_New(int type)
27 node *n = PyMem_NEW(node, 1);
28 if (n == NULL)
29 return NULL;
30 n->n_type = type;
31 n->n_str = NULL;
32 n->n_lineno = 0;
33 n->n_nchildren = 0;
34 n->n_child = NULL;
35 return n;
38 #define XXX 3 /* Node alignment factor to speed up realloc */
39 #define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX)
41 int
42 PyNode_AddChild(register node *n1, int type, char *str, int lineno)
44 register int nch = n1->n_nchildren;
45 register int nch1 = nch+1;
46 register node *n;
47 if (nch == INT_MAX || nch < 0)
48 return E_OVERFLOW;
49 if (XXXROUNDUP(nch) < nch1) {
50 n = n1->n_child;
51 nch1 = XXXROUNDUP(nch1);
52 PyMem_RESIZE(n, node, nch1);
53 if (n == NULL)
54 return E_NOMEM;
55 n1->n_child = n;
57 n = &n1->n_child[n1->n_nchildren++];
58 n->n_type = type;
59 n->n_str = str;
60 n->n_lineno = lineno;
61 n->n_nchildren = 0;
62 n->n_child = NULL;
63 return 0;
66 /* Forward */
67 static void freechildren(node *);
70 void
71 PyNode_Free(node *n)
73 if (n != NULL) {
74 freechildren(n);
75 PyMem_DEL(n);
79 static void
80 freechildren(node *n)
82 int i;
83 for (i = NCH(n); --i >= 0; )
84 freechildren(CHILD(n, i));
85 if (n->n_child != NULL)
86 PyMem_DEL(n->n_child);
87 if (STR(n) != NULL)
88 PyMem_DEL(STR(n));