Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | bab9d03 | 1992-04-05 14:26:55 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 3 | Netherlands. |
| 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Parse tree node implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | #include "node.h" |
| 29 | |
| 30 | node * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 31 | newtree(type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | n->n_lineno = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | addchild(n1, type, str, lineno) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | register node *n1; |
| 51 | int type; |
| 52 | char *str; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | int lineno; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 69 | n->n_lineno = lineno; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | n->n_nchildren = 0; |
| 71 | n->n_child = NULL; |
| 72 | return n; |
| 73 | } |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 89 | static void |
| 90 | freechildren(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 | } |