Mercurial > cpython-lru_cache-weakref
annotate Parser/node.c @ 14150:302dfd53b3c8 legacy-trunk
Make sure Thomas Wouters has enough to do. ;-)
(Some of this is in-progress; this also serves as a reminded to me to
check this out once it is written.)
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Thu, 24 Aug 2000 00:50:27 +0000 |
| parents | 21ec69a56228 |
| children | 6f6f965c5e2d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
2 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
5 All rights reserved. |
| 145 | 6 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
7 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 145 | 9 ******************************************************************/ |
| 10 | |
|
14147
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
11 #ifdef HAVE_LIMITS_H |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
12 #include <limits.h> |
|
14147
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
13 #endif |
|
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
14 #ifndef INT_MAX |
|
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
15 #define INT_MAX 2147483647 |
|
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
16 #endif |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
17 |
| 6 | 18 /* Parse tree node implementation */ |
| 19 | |
| 86 | 20 #include "pgenheaders.h" |
| 6 | 21 #include "node.h" |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
22 #include "errcode.h" |
| 6 | 23 |
| 24 node * | |
| 13659 | 25 PyNode_New(int type) |
| 6 | 26 { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
27 node *n = PyMem_NEW(node, 1); |
| 6 | 28 if (n == NULL) |
| 29 return NULL; | |
| 30 n->n_type = type; | |
| 31 n->n_str = NULL; | |
| 86 | 32 n->n_lineno = 0; |
| 6 | 33 n->n_nchildren = 0; |
| 34 n->n_child = NULL; | |
| 35 return n; | |
| 36 } | |
| 37 | |
| 38 #define XXX 3 /* Node alignment factor to speed up realloc */ | |
| 39 #define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX) | |
| 40 | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
41 int |
| 13659 | 42 PyNode_AddChild(register node *n1, int type, char *str, int lineno) |
| 6 | 43 { |
| 44 register int nch = n1->n_nchildren; | |
| 45 register int nch1 = nch+1; | |
| 46 register node *n; | |
|
14147
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
47 if (nch == INT_MAX || nch < 0) |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
48 return E_OVERFLOW; |
| 6 | 49 if (XXXROUNDUP(nch) < nch1) { |
| 50 n = n1->n_child; | |
| 51 nch1 = XXXROUNDUP(nch1); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
52 PyMem_RESIZE(n, node, nch1); |
| 6 | 53 if (n == NULL) |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
54 return E_NOMEM; |
| 6 | 55 n1->n_child = n; |
| 56 } | |
| 57 n = &n1->n_child[n1->n_nchildren++]; | |
| 58 n->n_type = type; | |
| 59 n->n_str = str; | |
| 86 | 60 n->n_lineno = lineno; |
| 6 | 61 n->n_nchildren = 0; |
| 62 n->n_child = NULL; | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
63 return 0; |
| 6 | 64 } |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
65 |
| 86 | 66 /* Forward */ |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
67 static void freechildren(node *); |
| 86 | 68 |
| 69 | |
| 70 void | |
| 13659 | 71 PyNode_Free(node *n) |
| 86 | 72 { |
| 73 if (n != NULL) { | |
| 74 freechildren(n); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
75 PyMem_DEL(n); |
| 86 | 76 } |
| 77 } | |
| 78 | |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
79 static void |
| 13659 | 80 freechildren(node *n) |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
81 { |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
82 int i; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
83 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
|
84 freechildren(CHILD(n, i)); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
85 if (n->n_child != NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
86 PyMem_DEL(n->n_child); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
87 if (STR(n) != NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
88 PyMem_DEL(STR(n)); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
89 } |
