Mercurial > cpython-lru_cache-weakref
annotate Parser/node.c @ 13184:efecbbf72765 legacy-trunk
Describe a bit about writing test cases for Python...
| author | Skip Montanaro <skip@pobox.com> |
|---|---|
| date | Fri, 30 Jun 2000 06:08:35 +0000 |
| parents | 63436719d12e |
| children | 916fb024ce94 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2060
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
32 #include <limits.h> |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
33 |
| 6 | 34 /* Parse tree node implementation */ |
| 35 | |
| 86 | 36 #include "pgenheaders.h" |
| 6 | 37 #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
|
38 #include "errcode.h" |
| 6 | 39 |
| 40 node * | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
41 PyNode_New(type) |
| 6 | 42 int type; |
| 43 { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
44 node *n = PyMem_NEW(node, 1); |
| 6 | 45 if (n == NULL) |
| 46 return NULL; | |
| 47 n->n_type = type; | |
| 48 n->n_str = NULL; | |
| 86 | 49 n->n_lineno = 0; |
| 6 | 50 n->n_nchildren = 0; |
| 51 n->n_child = NULL; | |
| 52 return n; | |
| 53 } | |
| 54 | |
| 55 #define XXX 3 /* Node alignment factor to speed up realloc */ | |
| 56 #define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX) | |
| 57 | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
58 int |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
59 PyNode_AddChild(n1, type, str, lineno) |
| 6 | 60 register node *n1; |
| 61 int type; | |
| 62 char *str; | |
| 86 | 63 int lineno; |
| 6 | 64 { |
| 65 register int nch = n1->n_nchildren; | |
| 66 register int nch1 = nch+1; | |
| 67 register node *n; | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
68 if (nch == SHRT_MAX || nch < 0) |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
69 return E_OVERFLOW; |
| 6 | 70 if (XXXROUNDUP(nch) < nch1) { |
| 71 n = n1->n_child; | |
| 72 nch1 = XXXROUNDUP(nch1); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
73 PyMem_RESIZE(n, node, nch1); |
| 6 | 74 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
|
75 return E_NOMEM; |
| 6 | 76 n1->n_child = n; |
| 77 } | |
| 78 n = &n1->n_child[n1->n_nchildren++]; | |
| 79 n->n_type = type; | |
| 80 n->n_str = str; | |
| 86 | 81 n->n_lineno = lineno; |
| 6 | 82 n->n_nchildren = 0; |
| 83 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
|
84 return 0; |
| 6 | 85 } |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
86 |
| 86 | 87 /* Forward */ |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
88 static void freechildren Py_PROTO((node *)); |
| 86 | 89 |
| 90 | |
| 91 void | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
92 PyNode_Free(n) |
| 86 | 93 node *n; |
| 94 { | |
| 95 if (n != NULL) { | |
| 96 freechildren(n); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
97 PyMem_DEL(n); |
| 86 | 98 } |
| 99 } | |
| 100 | |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
101 static void |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
102 freechildren(n) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
103 node *n; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
104 { |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
105 int i; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
106 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
|
107 freechildren(CHILD(n, i)); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
108 if (n->n_child != NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
109 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
|
110 if (STR(n) != NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
111 PyMem_DEL(STR(n)); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
112 } |
