Mercurial > cpython-lru_cache-weakref
annotate Parser/node.c @ 1848:b724599401ff legacy-trunk
Merge back to main trunk
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 30 Aug 1994 08:27:36 +0000 |
| parents | 1cabc2b6c970 |
| children | 146890653a57 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* Parse tree node implementation */ |
| 26 | |
| 86 | 27 #include "pgenheaders.h" |
| 6 | 28 #include "node.h" |
| 29 | |
| 30 node * | |
| 86 | 31 newtree(type) |
| 6 | 32 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; | |
| 86 | 39 n->n_lineno = 0; |
| 6 | 40 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 | |
| 48 node * | |
| 86 | 49 addchild(n1, type, str, lineno) |
| 6 | 50 register node *n1; |
| 51 int type; | |
| 52 char *str; | |
| 86 | 53 int lineno; |
| 6 | 54 { |
| 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; | |
| 86 | 69 n->n_lineno = lineno; |
| 6 | 70 n->n_nchildren = 0; |
| 71 n->n_child = NULL; | |
| 72 return n; | |
| 73 } | |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
74 |
| 86 | 75 /* Forward */ |
| 76 static void freechildren PROTO((node *)); | |
| 77 | |
| 78 | |
| 79 void | |
| 80 freetree(n) | |
| 81 node *n; | |
| 82 { | |
| 83 if (n != NULL) { | |
| 84 freechildren(n); | |
| 85 DEL(n); | |
| 86 } | |
| 87 } | |
| 88 | |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
89 static void |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
90 freechildren(n) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
91 node *n; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
92 { |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
93 int i; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
94 for (i = NCH(n); --i >= 0; ) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
95 freechildren(CHILD(n, i)); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
96 if (n->n_child != NULL) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
97 DEL(n->n_child); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
98 if (STR(n) != NULL) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
99 DEL(STR(n)); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
100 } |
