Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Vladimir Marangozov | 58e64a8 | 2000-09-03 23:47:08 +0000 | [diff] [blame] | 2 | /* Parse tree node implementation */ |
3 | |||||
4 | #include "pgenheaders.h" | ||||
5 | #include "node.h" | ||||
6 | #include "errcode.h" | ||||
7 | |||||
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 8 | #ifdef HAVE_LIMITS_H |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 9 | #include <limits.h> |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 10 | #endif |
11 | #ifndef INT_MAX | ||||
12 | #define INT_MAX 2147483647 | ||||
13 | #endif | ||||
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | node * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 16 | PyNode_New(int type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 18 | node *n = PyMem_NEW(node, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | if (n == NULL) |
20 | return NULL; | ||||
21 | n->n_type = type; | ||||
22 | n->n_str = NULL; | ||||
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 23 | n->n_lineno = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | n->n_nchildren = 0; |
25 | n->n_child = NULL; | ||||
26 | return n; | ||||
27 | } | ||||
28 | |||||
29 | #define XXX 3 /* Node alignment factor to speed up realloc */ | ||||
30 | #define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX) | ||||
31 | |||||
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 32 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 33 | PyNode_AddChild(register node *n1, int type, char *str, int lineno) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | { |
35 | register int nch = n1->n_nchildren; | ||||
36 | register int nch1 = nch+1; | ||||
37 | register node *n; | ||||
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 38 | if (nch == INT_MAX || nch < 0) |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 39 | return E_OVERFLOW; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | if (XXXROUNDUP(nch) < nch1) { |
41 | n = n1->n_child; | ||||
42 | nch1 = XXXROUNDUP(nch1); | ||||
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 43 | PyMem_RESIZE(n, node, nch1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | if (n == NULL) |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 45 | return E_NOMEM; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | n1->n_child = n; |
47 | } | ||||
48 | n = &n1->n_child[n1->n_nchildren++]; | ||||
49 | n->n_type = type; | ||||
50 | n->n_str = str; | ||||
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 51 | n->n_lineno = lineno; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | n->n_nchildren = 0; |
53 | n->n_child = NULL; | ||||
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 54 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | } |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 58 | static void freechildren(node *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | |
60 | |||||
61 | void | ||||
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 62 | PyNode_Free(node *n) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 63 | { |
64 | if (n != NULL) { | ||||
65 | freechildren(n); | ||||
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 66 | PyMem_DEL(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | } |
68 | } | ||||
69 | |||||
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 70 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 71 | freechildren(node *n) |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 72 | { |
73 | int i; | ||||
74 | for (i = NCH(n); --i >= 0; ) | ||||
75 | freechildren(CHILD(n, i)); | ||||
76 | if (n->n_child != NULL) | ||||
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 77 | PyMem_DEL(n->n_child); |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 78 | if (STR(n) != NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 79 | PyMem_DEL(STR(n)); |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 80 | } |