Mercurial > cpython-lru_cache-weakref
annotate Python/compile.c @ 2005:ed5d0ad5ea2f legacy-trunk
set name to <lambda> instead of None for lambdas
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 10 Nov 1994 22:40:34 +0000 |
| parents | 39a4c15acb32 |
| children | d7e91437f0a2 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
1 /*********************************************************** |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
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:
1029
diff
changeset
|
3 Amsterdam, The Netherlands. |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
4 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
5 All Rights Reserved |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
6 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
12 Centrum or CWI not be used in advertising or publicity pertaining to |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
13 distribution of the software without specific, written prior permission. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
14 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
22 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
23 ******************************************************************/ |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
24 |
| 69 | 25 /* Compile an expression node to intermediate code */ |
| 26 | |
| 86 | 27 /* XXX TO DO: |
| 28 XXX Compute maximum needed stack sizes while compiling | |
| 29 XXX Generate simple jump for break/return outside 'try...finally' | |
| 30 */ | |
| 69 | 31 |
| 86 | 32 #include "allobjects.h" |
| 33 | |
| 69 | 34 #include "node.h" |
| 35 #include "token.h" | |
| 36 #include "graminit.h" | |
| 37 #include "compile.h" | |
| 38 #include "opcode.h" | |
| 86 | 39 #include "structmember.h" |
| 40 | |
| 41 #include <ctype.h> | |
|
1319
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
42 #include <errno.h> |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
43 |
| 86 | 44 #define OFF(x) offsetof(codeobject, x) |
| 45 | |
| 46 static struct memberlist code_memberlist[] = { | |
|
568
92509d304e47
add READONLY to member list.
Guido van Rossum <guido@python.org>
parents:
544
diff
changeset
|
47 {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
|
92509d304e47
add READONLY to member list.
Guido van Rossum <guido@python.org>
parents:
544
diff
changeset
|
48 {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
|
92509d304e47
add READONLY to member list.
Guido van Rossum <guido@python.org>
parents:
544
diff
changeset
|
49 {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
|
92509d304e47
add READONLY to member list.
Guido van Rossum <guido@python.org>
parents:
544
diff
changeset
|
50 {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
51 {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
| 86 | 52 {NULL} /* Sentinel */ |
| 53 }; | |
| 54 | |
| 55 static object * | |
| 56 code_getattr(co, name) | |
| 57 codeobject *co; | |
| 58 char *name; | |
| 59 { | |
| 60 return getmember((char *)co, code_memberlist, name); | |
| 61 } | |
| 69 | 62 |
| 63 static void | |
| 86 | 64 code_dealloc(co) |
| 65 codeobject *co; | |
| 69 | 66 { |
| 86 | 67 XDECREF(co->co_code); |
| 68 XDECREF(co->co_consts); | |
| 69 XDECREF(co->co_names); | |
| 70 XDECREF(co->co_filename); | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
71 XDECREF(co->co_name); |
| 86 | 72 DEL(co); |
| 69 | 73 } |
| 74 | |
|
1029
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
75 static object * |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
76 code_repr(co) |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
77 codeobject *co; |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
78 { |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
79 char buf[500]; |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
80 int lineno = -1; |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
81 char *p = GETSTRINGVALUE(co->co_code); |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
82 char *filename = "???"; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
83 char *name = "???"; |
|
1029
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
84 if (*p == SET_LINENO) |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
85 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8); |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
86 if (co->co_filename && is_stringobject(co->co_filename)) |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
87 filename = getstringvalue(co->co_filename); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
88 if (co->co_name && is_stringobject(co->co_name)) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
89 name = getstringvalue(co->co_name); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
90 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>", |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
91 name, (long)co, filename, lineno); |
|
1029
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
92 return newstringobject(buf); |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
93 } |
|
2f9805c963e8
Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents:
792
diff
changeset
|
94 |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
95 static int |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
96 code_compare(co, cp) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
97 codeobject *co, *cp; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
98 { |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
99 int cmp; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
100 cmp = cmpobject((object *)co->co_code, (object *)cp->co_code); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
101 if (cmp) return cmp; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
102 cmp = cmpobject(co->co_consts, cp->co_consts); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
103 if (cmp) return cmp; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
104 cmp = cmpobject(co->co_names, cp->co_names); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
105 return cmp; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
106 } |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
107 |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
108 static long |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
109 code_hash(co) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
110 codeobject *co; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
111 { |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
112 long h, h1, h2, h3; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
113 h1 = hashobject((object *)co->co_code); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
114 if (h1 == -1) return -1; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
115 h2 = hashobject(co->co_consts); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
116 if (h2 == -1) return -1; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
117 h3 = hashobject(co->co_names); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
118 if (h3 == -1) return -1; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
119 h = h1 ^ h2 ^ h3; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
120 if (h == -1) h = -2; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
121 return h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
122 } |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
123 |
| 69 | 124 typeobject Codetype = { |
| 125 OB_HEAD_INIT(&Typetype) | |
| 126 0, | |
| 127 "code", | |
| 128 sizeof(codeobject), | |
| 129 0, | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
130 (destructor)code_dealloc, /*tp_dealloc*/ |
| 69 | 131 0, /*tp_print*/ |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
132 (getattrfunc)code_getattr, /*tp_getattr*/ |
| 69 | 133 0, /*tp_setattr*/ |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
134 (cmpfunc)code_compare, /*tp_compare*/ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
135 (reprfunc)code_repr, /*tp_repr*/ |
| 69 | 136 0, /*tp_as_number*/ |
| 137 0, /*tp_as_sequence*/ | |
| 138 0, /*tp_as_mapping*/ | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
139 (hashfunc)code_hash, /*tp_hash*/ |
| 69 | 140 }; |
| 141 | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
142 codeobject * |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
143 newcodeobject(code, consts, names, filename, name) |
| 69 | 144 object *code; |
| 145 object *consts; | |
| 146 object *names; | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
147 object *filename; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
148 object *name; |
| 69 | 149 { |
| 150 codeobject *co; | |
| 151 int i; | |
| 152 /* Check argument types */ | |
| 153 if (code == NULL || !is_stringobject(code) || | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
154 consts == NULL || |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
155 names == NULL || |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
156 name == NULL || !(is_stringobject(name) || name == None)) { |
| 69 | 157 err_badcall(); |
| 158 return NULL; | |
| 159 } | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
160 /* Allow two lists instead of two tuples */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
161 if (is_listobject(consts) && is_listobject(names)) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
162 consts = listtuple(consts); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
163 if (consts == NULL) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
164 return NULL; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
165 names = listtuple(names); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
166 if (names == NULL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
167 DECREF(consts); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
168 return NULL; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
169 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
170 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
171 else if (!is_tupleobject(consts) && !is_tupleobject(names)) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
172 err_badcall(); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
173 return NULL; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
174 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
175 else { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
176 INCREF(consts); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
177 INCREF(names); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
178 } |
| 69 | 179 /* Make sure the list of names contains only strings */ |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
180 for (i = gettuplesize(names); --i >= 0; ) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
181 object *v = gettupleitem(names, i); |
| 69 | 182 if (v == NULL || !is_stringobject(v)) { |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
183 DECREF(consts); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
184 DECREF(names); |
| 69 | 185 err_badcall(); |
| 186 return NULL; | |
| 187 } | |
| 188 } | |
| 189 co = NEWOBJ(codeobject, &Codetype); | |
| 190 if (co != NULL) { | |
| 191 INCREF(code); | |
| 192 co->co_code = (stringobject *)code; | |
| 193 co->co_consts = consts; | |
| 194 co->co_names = names; | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
195 INCREF(filename); |
|
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
196 co->co_filename = filename; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
197 INCREF(name); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
198 co->co_name = name; |
| 69 | 199 } |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
200 else { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
201 DECREF(consts); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
202 DECREF(names); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
203 } |
| 69 | 204 return co; |
| 205 } | |
| 206 | |
| 207 | |
| 208 /* Data structure used internally */ | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
209 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
210 #define MAXBLOCKS 20 /* Max static block nesting within a function */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
211 |
| 69 | 212 struct compiling { |
| 213 object *c_code; /* string */ | |
| 214 object *c_consts; /* list of objects */ | |
| 215 object *c_names; /* list of strings (names) */ | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
216 object *c_globals; /* dictionary */ |
| 69 | 217 int c_nexti; /* index into c_code */ |
| 218 int c_errors; /* counts errors occurred */ | |
| 86 | 219 int c_infunction; /* set when compiling a function */ |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
220 int c_interactive; /* generating code for interactive command */ |
| 86 | 221 int c_loops; /* counts nested loops */ |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
222 int c_begin; /* begin of current loop, for 'continue' */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
223 int c_block[MAXBLOCKS]; /* stack of block types */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
224 int c_nblocks; /* current block stack level */ |
| 86 | 225 char *c_filename; /* filename of current node */ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
226 char *c_name; /* name of object (e.g. function) */ |
| 69 | 227 }; |
| 228 | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
229 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
230 /* Interface to the block stack */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
231 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
232 static void |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
233 block_push(c, type) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
234 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
235 int type; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
236 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
237 if (c->c_nblocks >= MAXBLOCKS) { |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
238 err_setstr(SystemError, "too many statically nested blocks"); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
239 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
240 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
241 else { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
242 c->c_block[c->c_nblocks++] = type; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
243 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
244 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
245 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
246 static void |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
247 block_pop(c, type) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
248 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
249 int type; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
250 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
251 if (c->c_nblocks > 0) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
252 c->c_nblocks--; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
253 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
254 err_setstr(SystemError, "bad block pop"); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
255 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
256 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
257 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
258 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
259 |
| 69 | 260 /* Prototypes */ |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
261 |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
262 static int com_init PROTO((struct compiling *, char *)); |
| 69 | 263 static void com_free PROTO((struct compiling *)); |
| 264 static void com_done PROTO((struct compiling *)); | |
| 265 static void com_node PROTO((struct compiling *, struct _node *)); | |
| 266 static void com_addbyte PROTO((struct compiling *, int)); | |
| 267 static void com_addint PROTO((struct compiling *, int)); | |
| 268 static void com_addoparg PROTO((struct compiling *, int, int)); | |
| 269 static void com_addfwref PROTO((struct compiling *, int, int *)); | |
| 270 static void com_backpatch PROTO((struct compiling *, int)); | |
| 271 static int com_add PROTO((struct compiling *, object *, object *)); | |
| 272 static int com_addconst PROTO((struct compiling *, object *)); | |
| 273 static int com_addname PROTO((struct compiling *, object *)); | |
| 274 static void com_addopname PROTO((struct compiling *, int, node *)); | |
| 482 | 275 static void com_list PROTO((struct compiling *, node *, int)); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
276 static int com_argdefs PROTO((struct compiling *, node *, int *)); |
| 69 | 277 |
| 278 static int | |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
279 com_init(c, filename) |
| 69 | 280 struct compiling *c; |
| 86 | 281 char *filename; |
| 69 | 282 { |
|
160
afa48a96a444
Create code string with initial size of 1000 instead of 0.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
283 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL) |
| 69 | 284 goto fail_3; |
| 285 if ((c->c_consts = newlistobject(0)) == NULL) | |
| 286 goto fail_2; | |
| 287 if ((c->c_names = newlistobject(0)) == NULL) | |
| 288 goto fail_1; | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
289 if ((c->c_globals = newdictobject()) == NULL) |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
290 goto fail_0; |
| 69 | 291 c->c_nexti = 0; |
| 292 c->c_errors = 0; | |
| 86 | 293 c->c_infunction = 0; |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
294 c->c_interactive = 0; |
| 86 | 295 c->c_loops = 0; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
296 c->c_begin = 0; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
297 c->c_nblocks = 0; |
| 86 | 298 c->c_filename = filename; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
299 c->c_name = "?"; |
| 69 | 300 return 1; |
| 301 | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
302 fail_0: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
303 DECREF(c->c_names); |
| 69 | 304 fail_1: |
| 305 DECREF(c->c_consts); | |
| 306 fail_2: | |
| 307 DECREF(c->c_code); | |
| 308 fail_3: | |
| 309 return 0; | |
| 310 } | |
| 311 | |
| 312 static void | |
| 313 com_free(c) | |
| 314 struct compiling *c; | |
| 315 { | |
| 316 XDECREF(c->c_code); | |
| 317 XDECREF(c->c_consts); | |
| 318 XDECREF(c->c_names); | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
319 XDECREF(c->c_globals); |
| 69 | 320 } |
| 321 | |
| 322 static void | |
| 323 com_done(c) | |
| 324 struct compiling *c; | |
| 325 { | |
| 326 if (c->c_code != NULL) | |
| 327 resizestring(&c->c_code, c->c_nexti); | |
| 328 } | |
| 329 | |
| 330 static void | |
| 331 com_addbyte(c, byte) | |
| 332 struct compiling *c; | |
| 333 int byte; | |
| 334 { | |
| 335 int len; | |
| 336 if (byte < 0 || byte > 255) { | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
337 /* |
| 86 | 338 fprintf(stderr, "XXX compiling bad byte: %d\n", byte); |
| 339 abort(); | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
340 */ |
| 69 | 341 err_setstr(SystemError, "com_addbyte: byte out of range"); |
| 342 c->c_errors++; | |
| 343 } | |
| 344 if (c->c_code == NULL) | |
| 345 return; | |
| 346 len = getstringsize(c->c_code); | |
| 347 if (c->c_nexti >= len) { | |
| 348 if (resizestring(&c->c_code, len+1000) != 0) { | |
| 349 c->c_errors++; | |
| 350 return; | |
| 351 } | |
| 352 } | |
| 353 getstringvalue(c->c_code)[c->c_nexti++] = byte; | |
| 354 } | |
| 355 | |
| 356 static void | |
| 357 com_addint(c, x) | |
| 358 struct compiling *c; | |
| 359 int x; | |
| 360 { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
361 com_addbyte(c, x & 0xff); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
362 com_addbyte(c, x >> 8); /* XXX x should be positive */ |
| 69 | 363 } |
| 364 | |
| 365 static void | |
| 366 com_addoparg(c, op, arg) | |
| 367 struct compiling *c; | |
| 368 int op; | |
| 369 int arg; | |
| 370 { | |
| 371 com_addbyte(c, op); | |
| 372 com_addint(c, arg); | |
| 373 } | |
| 374 | |
| 375 static void | |
| 376 com_addfwref(c, op, p_anchor) | |
| 377 struct compiling *c; | |
| 378 int op; | |
| 379 int *p_anchor; | |
| 380 { | |
| 381 /* Compile a forward reference for backpatching */ | |
| 382 int here; | |
| 383 int anchor; | |
| 384 com_addbyte(c, op); | |
| 385 here = c->c_nexti; | |
| 386 anchor = *p_anchor; | |
| 387 *p_anchor = here; | |
| 388 com_addint(c, anchor == 0 ? 0 : here - anchor); | |
| 389 } | |
| 390 | |
| 391 static void | |
| 392 com_backpatch(c, anchor) | |
| 393 struct compiling *c; | |
| 394 int anchor; /* Must be nonzero */ | |
| 395 { | |
| 396 unsigned char *code = (unsigned char *) getstringvalue(c->c_code); | |
| 397 int target = c->c_nexti; | |
| 398 int dist; | |
| 399 int prev; | |
| 400 for (;;) { | |
| 401 /* Make the JUMP instruction at anchor point to target */ | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
402 prev = code[anchor] + (code[anchor+1] << 8); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
403 dist = target - (anchor+2); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
404 code[anchor] = dist & 0xff; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
405 code[anchor+1] = dist >> 8; |
| 69 | 406 if (!prev) |
| 407 break; | |
| 408 anchor -= prev; | |
| 409 } | |
| 410 } | |
| 411 | |
| 544 | 412 /* Handle literals and names uniformly */ |
| 69 | 413 |
| 414 static int | |
| 415 com_add(c, list, v) | |
| 416 struct compiling *c; | |
| 417 object *list; | |
| 418 object *v; | |
| 419 { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
420 int n = getlistsize(list); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
421 int i; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
422 for (i = n; --i >= 0; ) { |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
423 object *w = getlistitem(list, i); |
|
303
149dd121d0a6
Check for identical types before comparing objects to see if they
Guido van Rossum <guido@python.org>
parents:
266
diff
changeset
|
424 if (v->ob_type == w->ob_type && cmpobject(v, w) == 0) |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
425 return i; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
426 } |
| 69 | 427 if (addlistitem(list, v) != 0) |
| 428 c->c_errors++; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
429 return n; |
| 69 | 430 } |
| 431 | |
| 432 static int | |
| 433 com_addconst(c, v) | |
| 434 struct compiling *c; | |
| 435 object *v; | |
| 436 { | |
| 437 return com_add(c, c->c_consts, v); | |
| 438 } | |
| 439 | |
| 440 static int | |
| 441 com_addname(c, v) | |
| 442 struct compiling *c; | |
| 443 object *v; | |
| 444 { | |
| 445 return com_add(c, c->c_names, v); | |
| 446 } | |
| 447 | |
| 448 static void | |
| 449 com_addopname(c, op, n) | |
| 450 struct compiling *c; | |
| 451 int op; | |
| 452 node *n; | |
| 453 { | |
| 454 object *v; | |
| 455 int i; | |
| 456 char *name; | |
| 457 if (TYPE(n) == STAR) | |
| 458 name = "*"; | |
| 459 else { | |
| 460 REQ(n, NAME); | |
| 461 name = STR(n); | |
| 462 } | |
| 463 if ((v = newstringobject(name)) == NULL) { | |
| 464 c->c_errors++; | |
| 465 i = 255; | |
| 466 } | |
| 467 else { | |
| 468 i = com_addname(c, v); | |
| 469 DECREF(v); | |
| 470 } | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
471 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */ |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
472 switch (op) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
473 case LOAD_NAME: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
474 case STORE_NAME: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
475 case DELETE_NAME: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
476 if (dictlookup(c->c_globals, name) != NULL) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
477 switch (op) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
478 case LOAD_NAME: op = LOAD_GLOBAL; break; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
479 case STORE_NAME: op = STORE_GLOBAL; break; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
480 case DELETE_NAME: op = DELETE_GLOBAL; break; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
481 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
482 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
483 } |
| 69 | 484 com_addoparg(c, op, i); |
| 485 } | |
| 486 | |
| 487 static object * | |
| 488 parsenumber(s) | |
| 489 char *s; | |
| 490 { | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
491 extern long mystrtol PROTO((const char *, char **, int)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
492 extern unsigned long mystrtoul PROTO((const char *, char **, int)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
493 extern double atof PROTO((const char *)); |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
494 char *end; |
| 69 | 495 long x; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
496 errno = 0; |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
497 end = s + strlen(s) - 1; |
| 744 | 498 if (*end == 'l' || *end == 'L') |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
499 return long_scan(s, 0); |
|
617
755bb8efcc2d
Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents:
584
diff
changeset
|
500 if (s[0] == '0') |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
501 x = (long) mystrtoul(s, &end, 0); |
|
617
755bb8efcc2d
Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents:
584
diff
changeset
|
502 else |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
503 x = mystrtol(s, &end, 0); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
504 if (*end == '\0') { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
505 if (errno != 0) { |
|
493
57667ab1e194
Changed some RuntimeErrors.
Guido van Rossum <guido@python.org>
parents:
482
diff
changeset
|
506 err_setstr(OverflowError, |
| 544 | 507 "integer literal too large"); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
508 return NULL; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
509 } |
| 69 | 510 return newintobject(x); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
511 } |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
512 /* XXX Huge floats may silently fail */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
513 return newfloatobject(atof(s)); |
| 69 | 514 } |
| 515 | |
| 516 static object * | |
| 517 parsestr(s) | |
| 518 char *s; | |
| 519 { | |
| 520 object *v; | |
| 521 int len; | |
| 522 char *buf; | |
| 523 char *p; | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
524 char *end; |
| 69 | 525 int c; |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
526 int quote = *s; |
|
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
527 if (quote != '\'' && quote != '\"') { |
| 69 | 528 err_badcall(); |
| 529 return NULL; | |
| 530 } | |
| 531 s++; | |
| 532 len = strlen(s); | |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
533 if (s[--len] != quote) { |
| 69 | 534 err_badcall(); |
| 535 return NULL; | |
| 536 } | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
537 if (len >= 4 && s[0] == quote && s[1] == quote) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
538 s += 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
539 len -= 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
540 if (s[--len] != quote || s[--len] != quote) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
541 err_badcall(); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
542 return NULL; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
543 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
544 } |
| 69 | 545 if (strchr(s, '\\') == NULL) |
| 546 return newsizedstringobject(s, len); | |
| 547 v = newsizedstringobject((char *)NULL, len); | |
| 548 p = buf = getstringvalue(v); | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
549 end = s + len; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
550 while (s < end) { |
| 69 | 551 if (*s != '\\') { |
| 552 *p++ = *s++; | |
| 553 continue; | |
| 554 } | |
| 555 s++; | |
| 556 switch (*s++) { | |
| 557 /* XXX This assumes ASCII! */ | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
558 case '\n': break; |
| 69 | 559 case '\\': *p++ = '\\'; break; |
| 560 case '\'': *p++ = '\''; break; | |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
561 case '\"': *p++ = '\"'; break; |
| 69 | 562 case 'b': *p++ = '\b'; break; |
| 563 case 'f': *p++ = '\014'; break; /* FF */ | |
| 564 case 't': *p++ = '\t'; break; | |
| 565 case 'n': *p++ = '\n'; break; | |
| 566 case 'r': *p++ = '\r'; break; | |
| 567 case 'v': *p++ = '\013'; break; /* VT */ | |
| 568 case 'a': *p++ = '\007'; break; /* BEL, not classic C */ | |
| 569 case '0': case '1': case '2': case '3': | |
| 570 case '4': case '5': case '6': case '7': | |
| 571 c = s[-1] - '0'; | |
| 572 if ('0' <= *s && *s <= '7') { | |
| 573 c = (c<<3) + *s++ - '0'; | |
| 574 if ('0' <= *s && *s <= '7') | |
| 575 c = (c<<3) + *s++ - '0'; | |
| 576 } | |
| 577 *p++ = c; | |
| 578 break; | |
| 579 case 'x': | |
| 580 if (isxdigit(*s)) { | |
| 581 sscanf(s, "%x", &c); | |
| 582 *p++ = c; | |
| 583 do { | |
| 584 s++; | |
| 585 } while (isxdigit(*s)); | |
| 586 break; | |
| 587 } | |
| 588 /* FALLTHROUGH */ | |
| 589 default: *p++ = '\\'; *p++ = s[-1]; break; | |
| 590 } | |
| 591 } | |
| 592 resizestring(&v, (int)(p - buf)); | |
| 593 return v; | |
| 594 } | |
| 595 | |
| 596 static void | |
| 597 com_list_constructor(c, n) | |
| 598 struct compiling *c; | |
| 599 node *n; | |
| 600 { | |
| 601 int len; | |
| 602 int i; | |
| 603 if (TYPE(n) != testlist) | |
| 604 REQ(n, exprlist); | |
| 605 /* exprlist: expr (',' expr)* [',']; likewise for testlist */ | |
| 606 len = (NCH(n) + 1) / 2; | |
| 607 for (i = 0; i < NCH(n); i += 2) | |
| 608 com_node(c, CHILD(n, i)); | |
| 609 com_addoparg(c, BUILD_LIST, len); | |
| 610 } | |
| 611 | |
| 612 static void | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
613 com_dictmaker(c, n) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
614 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
615 node *n; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
616 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
617 int i; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
618 /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
619 for (i = 0; i+2 < NCH(n); i += 4) { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
620 /* We must arrange things just right for STORE_SUBSCR. |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
621 It wants the stack to look like (value) (dict) (key) */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
622 com_addbyte(c, DUP_TOP); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
623 com_node(c, CHILD(n, i+2)); /* value */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
624 com_addbyte(c, ROT_TWO); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
625 com_node(c, CHILD(n, i)); /* key */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
626 com_addbyte(c, STORE_SUBSCR); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
627 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
628 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
629 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
630 static void |
| 69 | 631 com_atom(c, n) |
| 632 struct compiling *c; | |
| 633 node *n; | |
| 634 { | |
| 635 node *ch; | |
| 636 object *v; | |
| 637 int i; | |
| 638 REQ(n, atom); | |
| 639 ch = CHILD(n, 0); | |
| 640 switch (TYPE(ch)) { | |
| 641 case LPAR: | |
| 642 if (TYPE(CHILD(n, 1)) == RPAR) | |
| 643 com_addoparg(c, BUILD_TUPLE, 0); | |
| 644 else | |
| 645 com_node(c, CHILD(n, 1)); | |
| 646 break; | |
| 647 case LSQB: | |
| 648 if (TYPE(CHILD(n, 1)) == RSQB) | |
| 649 com_addoparg(c, BUILD_LIST, 0); | |
| 650 else | |
| 651 com_list_constructor(c, CHILD(n, 1)); | |
| 652 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
653 case LBRACE: /* '{' [dictmaker] '}' */ |
| 69 | 654 com_addoparg(c, BUILD_MAP, 0); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
655 if (TYPE(CHILD(n, 1)) != RBRACE) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
656 com_dictmaker(c, CHILD(n, 1)); |
| 69 | 657 break; |
| 658 case BACKQUOTE: | |
| 659 com_node(c, CHILD(n, 1)); | |
| 660 com_addbyte(c, UNARY_CONVERT); | |
| 661 break; | |
| 662 case NUMBER: | |
| 663 if ((v = parsenumber(STR(ch))) == NULL) { | |
| 664 c->c_errors++; | |
| 665 i = 255; | |
| 666 } | |
| 667 else { | |
| 668 i = com_addconst(c, v); | |
| 669 DECREF(v); | |
| 670 } | |
| 671 com_addoparg(c, LOAD_CONST, i); | |
| 672 break; | |
| 673 case STRING: | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
674 if ((v = parsestr(STR(ch))) != NULL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
675 /* String literal concatenation */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
676 for (i = 1; i < NCH(n) && v != NULL; i++) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
677 joinstring_decref(&v, |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
678 parsestr(STR(CHILD(n, i)))); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
679 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
680 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
681 if (v == NULL) { |
| 69 | 682 c->c_errors++; |
| 683 i = 255; | |
| 684 } | |
| 685 else { | |
| 686 i = com_addconst(c, v); | |
| 687 DECREF(v); | |
| 688 } | |
| 689 com_addoparg(c, LOAD_CONST, i); | |
| 690 break; | |
| 691 case NAME: | |
| 692 com_addopname(c, LOAD_NAME, ch); | |
| 693 break; | |
| 694 default: | |
| 695 fprintf(stderr, "node type %d\n", TYPE(ch)); | |
| 696 err_setstr(SystemError, "com_atom: unexpected node type"); | |
| 697 c->c_errors++; | |
| 698 } | |
| 699 } | |
| 700 | |
| 701 static void | |
| 702 com_slice(c, n, op) | |
| 703 struct compiling *c; | |
| 704 node *n; | |
| 705 int op; | |
| 706 { | |
| 707 if (NCH(n) == 1) { | |
| 708 com_addbyte(c, op); | |
| 709 } | |
| 710 else if (NCH(n) == 2) { | |
| 711 if (TYPE(CHILD(n, 0)) != COLON) { | |
| 712 com_node(c, CHILD(n, 0)); | |
| 713 com_addbyte(c, op+1); | |
| 714 } | |
| 715 else { | |
| 716 com_node(c, CHILD(n, 1)); | |
| 717 com_addbyte(c, op+2); | |
| 718 } | |
| 719 } | |
| 720 else { | |
| 721 com_node(c, CHILD(n, 0)); | |
| 722 com_node(c, CHILD(n, 2)); | |
| 723 com_addbyte(c, op+3); | |
| 724 } | |
| 725 } | |
| 726 | |
| 727 static void | |
| 728 com_apply_subscript(c, n) | |
| 729 struct compiling *c; | |
| 730 node *n; | |
| 731 { | |
| 732 REQ(n, subscript); | |
| 733 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) { | |
| 734 /* It's a single subscript */ | |
| 735 com_node(c, CHILD(n, 0)); | |
| 736 com_addbyte(c, BINARY_SUBSCR); | |
| 737 } | |
| 738 else { | |
| 739 /* It's a slice: [expr] ':' [expr] */ | |
| 740 com_slice(c, n, SLICE); | |
| 741 } | |
| 742 } | |
| 743 | |
| 744 static void | |
| 745 com_call_function(c, n) | |
| 746 struct compiling *c; | |
| 747 node *n; /* EITHER testlist OR ')' */ | |
| 748 { | |
| 749 if (TYPE(n) == RPAR) { | |
| 482 | 750 com_addoparg(c, BUILD_TUPLE, 0); |
| 751 com_addbyte(c, BINARY_CALL); | |
| 69 | 752 } |
| 753 else { | |
| 482 | 754 REQ(n, testlist); |
| 755 com_list(c, n, 1); | |
| 69 | 756 com_addbyte(c, BINARY_CALL); |
| 757 } | |
| 758 } | |
| 759 | |
| 760 static void | |
| 761 com_select_member(c, n) | |
| 762 struct compiling *c; | |
| 763 node *n; | |
| 764 { | |
| 765 com_addopname(c, LOAD_ATTR, n); | |
| 766 } | |
| 767 | |
| 768 static void | |
| 769 com_apply_trailer(c, n) | |
| 770 struct compiling *c; | |
| 771 node *n; | |
| 772 { | |
| 773 REQ(n, trailer); | |
| 774 switch (TYPE(CHILD(n, 0))) { | |
| 775 case LPAR: | |
| 776 com_call_function(c, CHILD(n, 1)); | |
| 777 break; | |
| 778 case DOT: | |
| 779 com_select_member(c, CHILD(n, 1)); | |
| 780 break; | |
| 781 case LSQB: | |
| 782 com_apply_subscript(c, CHILD(n, 1)); | |
| 783 break; | |
| 784 default: | |
| 785 err_setstr(SystemError, | |
| 786 "com_apply_trailer: unknown trailer type"); | |
| 787 c->c_errors++; | |
| 788 } | |
| 789 } | |
| 790 | |
| 791 static void | |
| 792 com_factor(c, n) | |
| 793 struct compiling *c; | |
| 794 node *n; | |
| 795 { | |
| 796 int i; | |
| 797 REQ(n, factor); | |
| 798 if (TYPE(CHILD(n, 0)) == PLUS) { | |
| 799 com_factor(c, CHILD(n, 1)); | |
| 800 com_addbyte(c, UNARY_POSITIVE); | |
| 801 } | |
| 802 else if (TYPE(CHILD(n, 0)) == MINUS) { | |
| 803 com_factor(c, CHILD(n, 1)); | |
| 804 com_addbyte(c, UNARY_NEGATIVE); | |
| 805 } | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
806 else if (TYPE(CHILD(n, 0)) == TILDE) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
807 com_factor(c, CHILD(n, 1)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
808 com_addbyte(c, UNARY_INVERT); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
809 } |
| 69 | 810 else { |
| 811 com_atom(c, CHILD(n, 0)); | |
| 812 for (i = 1; i < NCH(n); i++) | |
| 813 com_apply_trailer(c, CHILD(n, i)); | |
| 814 } | |
| 815 } | |
| 816 | |
| 817 static void | |
| 818 com_term(c, n) | |
| 819 struct compiling *c; | |
| 820 node *n; | |
| 821 { | |
| 822 int i; | |
| 823 int op; | |
| 824 REQ(n, term); | |
| 825 com_factor(c, CHILD(n, 0)); | |
| 826 for (i = 2; i < NCH(n); i += 2) { | |
| 827 com_factor(c, CHILD(n, i)); | |
| 828 switch (TYPE(CHILD(n, i-1))) { | |
| 829 case STAR: | |
| 830 op = BINARY_MULTIPLY; | |
| 831 break; | |
| 832 case SLASH: | |
| 833 op = BINARY_DIVIDE; | |
| 834 break; | |
| 835 case PERCENT: | |
| 836 op = BINARY_MODULO; | |
| 837 break; | |
| 838 default: | |
| 839 err_setstr(SystemError, | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
840 "com_term: operator not *, / or %"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
841 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
842 op = 255; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
843 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
844 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
845 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
846 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
847 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
848 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
849 com_arith_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
850 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
851 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
852 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
853 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
854 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
855 REQ(n, arith_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
856 com_term(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
857 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
858 com_term(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
859 switch (TYPE(CHILD(n, i-1))) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
860 case PLUS: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
861 op = BINARY_ADD; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
862 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
863 case MINUS: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
864 op = BINARY_SUBTRACT; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
865 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
866 default: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
867 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
868 "com_arith_expr: operator not + or -"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
869 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
870 op = 255; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
871 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
872 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
873 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
874 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
875 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
876 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
877 com_shift_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
878 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
879 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
880 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
881 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
882 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
883 REQ(n, shift_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
884 com_arith_expr(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
885 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
886 com_arith_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
887 switch (TYPE(CHILD(n, i-1))) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
888 case LEFTSHIFT: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
889 op = BINARY_LSHIFT; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
890 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
891 case RIGHTSHIFT: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
892 op = BINARY_RSHIFT; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
893 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
894 default: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
895 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
896 "com_shift_expr: operator not << or >>"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
897 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
898 op = 255; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
899 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
900 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
901 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
902 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
903 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
904 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
905 com_and_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
906 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
907 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
908 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
909 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
910 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
911 REQ(n, and_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
912 com_shift_expr(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
913 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
914 com_shift_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
915 if (TYPE(CHILD(n, i-1)) == AMPER) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
916 op = BINARY_AND; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
917 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
918 else { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
919 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
920 "com_and_expr: operator not &"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
921 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
922 op = 255; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
923 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
924 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
925 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
926 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
927 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
928 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
929 com_xor_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
930 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
931 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
932 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
933 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
934 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
935 REQ(n, xor_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
936 com_and_expr(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
937 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
938 com_and_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
939 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
940 op = BINARY_XOR; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
941 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
942 else { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
943 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
944 "com_xor_expr: operator not ^"); |
| 69 | 945 c->c_errors++; |
| 946 op = 255; | |
| 947 } | |
| 948 com_addbyte(c, op); | |
| 949 } | |
| 950 } | |
| 951 | |
| 952 static void | |
| 953 com_expr(c, n) | |
| 954 struct compiling *c; | |
| 955 node *n; | |
| 956 { | |
| 957 int i; | |
| 958 int op; | |
| 959 REQ(n, expr); | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
960 com_xor_expr(c, CHILD(n, 0)); |
| 69 | 961 for (i = 2; i < NCH(n); i += 2) { |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
962 com_xor_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
963 if (TYPE(CHILD(n, i-1)) == VBAR) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
964 op = BINARY_OR; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
965 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
966 else { |
| 69 | 967 err_setstr(SystemError, |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
968 "com_expr: expr operator not |"); |
| 69 | 969 c->c_errors++; |
| 970 op = 255; | |
| 971 } | |
| 972 com_addbyte(c, op); | |
| 973 } | |
| 974 } | |
| 975 | |
| 976 static enum cmp_op | |
| 977 cmp_type(n) | |
| 978 node *n; | |
| 979 { | |
| 980 REQ(n, comp_op); | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
981 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
| 69 | 982 | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 983 if (NCH(n) == 1) { | |
| 984 n = CHILD(n, 0); | |
| 985 switch (TYPE(n)) { | |
| 986 case LESS: return LT; | |
| 987 case GREATER: return GT; | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
988 case EQEQUAL: /* == */ |
| 69 | 989 case EQUAL: return EQ; |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
990 case LESSEQUAL: return LE; |
|
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
991 case GREATEREQUAL: return GE; |
|
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
992 case NOTEQUAL: return NE; /* <> or != */ |
| 69 | 993 case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 994 if (strcmp(STR(n), "is") == 0) return IS; | |
| 995 } | |
| 996 } | |
| 997 else if (NCH(n) == 2) { | |
| 998 switch (TYPE(CHILD(n, 0))) { | |
| 999 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 1000 return NOT_IN; | |
| 1001 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 1002 return IS_NOT; | |
| 1003 } | |
| 1004 } | |
| 1005 return BAD; | |
| 1006 } | |
| 1007 | |
| 1008 static void | |
| 1009 com_comparison(c, n) | |
| 1010 struct compiling *c; | |
| 1011 node *n; | |
| 1012 { | |
| 1013 int i; | |
| 1014 enum cmp_op op; | |
| 1015 int anchor; | |
| 1016 REQ(n, comparison); /* comparison: expr (comp_op expr)* */ | |
| 1017 com_expr(c, CHILD(n, 0)); | |
| 1018 if (NCH(n) == 1) | |
| 1019 return; | |
| 1020 | |
| 1021 /**************************************************************** | |
| 1022 The following code is generated for all but the last | |
| 1023 comparison in a chain: | |
| 1024 | |
| 1025 label: on stack: opcode: jump to: | |
| 1026 | |
| 1027 a <code to load b> | |
| 1028 a, b DUP_TOP | |
| 1029 a, b, b ROT_THREE | |
| 1030 b, a, b COMPARE_OP | |
| 1031 b, 0-or-1 JUMP_IF_FALSE L1 | |
| 1032 b, 1 POP_TOP | |
| 1033 b | |
| 1034 | |
| 1035 We are now ready to repeat this sequence for the next | |
| 1036 comparison in the chain. | |
| 1037 | |
| 1038 For the last we generate: | |
| 1039 | |
| 1040 b <code to load c> | |
| 1041 b, c COMPARE_OP | |
| 1042 0-or-1 | |
| 1043 | |
| 1044 If there were any jumps to L1 (i.e., there was more than one | |
| 1045 comparison), we generate: | |
| 1046 | |
| 1047 0-or-1 JUMP_FORWARD L2 | |
| 1048 L1: b, 0 ROT_TWO | |
| 1049 0, b POP_TOP | |
| 1050 0 | |
| 1051 L2: | |
| 1052 ****************************************************************/ | |
| 1053 | |
| 1054 anchor = 0; | |
| 1055 | |
| 1056 for (i = 2; i < NCH(n); i += 2) { | |
| 1057 com_expr(c, CHILD(n, i)); | |
| 1058 if (i+2 < NCH(n)) { | |
| 1059 com_addbyte(c, DUP_TOP); | |
| 1060 com_addbyte(c, ROT_THREE); | |
| 1061 } | |
| 1062 op = cmp_type(CHILD(n, i-1)); | |
| 1063 if (op == BAD) { | |
| 1064 err_setstr(SystemError, | |
| 1065 "com_comparison: unknown comparison op"); | |
| 1066 c->c_errors++; | |
| 1067 } | |
| 1068 com_addoparg(c, COMPARE_OP, op); | |
| 1069 if (i+2 < NCH(n)) { | |
| 1070 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1071 com_addbyte(c, POP_TOP); | |
| 1072 } | |
| 1073 } | |
| 1074 | |
| 1075 if (anchor) { | |
| 1076 int anchor2 = 0; | |
| 1077 com_addfwref(c, JUMP_FORWARD, &anchor2); | |
| 1078 com_backpatch(c, anchor); | |
| 1079 com_addbyte(c, ROT_TWO); | |
| 1080 com_addbyte(c, POP_TOP); | |
| 1081 com_backpatch(c, anchor2); | |
| 1082 } | |
| 1083 } | |
| 1084 | |
| 1085 static void | |
| 1086 com_not_test(c, n) | |
| 1087 struct compiling *c; | |
| 1088 node *n; | |
| 1089 { | |
| 1090 REQ(n, not_test); /* 'not' not_test | comparison */ | |
| 1091 if (NCH(n) == 1) { | |
| 1092 com_comparison(c, CHILD(n, 0)); | |
| 1093 } | |
| 1094 else { | |
| 1095 com_not_test(c, CHILD(n, 1)); | |
| 1096 com_addbyte(c, UNARY_NOT); | |
| 1097 } | |
| 1098 } | |
| 1099 | |
| 1100 static void | |
| 1101 com_and_test(c, n) | |
| 1102 struct compiling *c; | |
| 1103 node *n; | |
| 1104 { | |
| 1105 int i; | |
| 1106 int anchor; | |
| 1107 REQ(n, and_test); /* not_test ('and' not_test)* */ | |
| 1108 anchor = 0; | |
| 1109 i = 0; | |
| 1110 for (;;) { | |
| 1111 com_not_test(c, CHILD(n, i)); | |
| 1112 if ((i += 2) >= NCH(n)) | |
| 1113 break; | |
| 1114 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1115 com_addbyte(c, POP_TOP); | |
| 1116 } | |
| 1117 if (anchor) | |
| 1118 com_backpatch(c, anchor); | |
| 1119 } | |
| 1120 | |
| 1121 static void | |
| 1122 com_test(c, n) | |
| 1123 struct compiling *c; | |
| 1124 node *n; | |
| 1125 { | |
|
1417
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1126 REQ(n, test); /* and_test ('and' and_test)* | lambdef */ |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1127 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1128 object *v; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1129 int i; |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1130 int argcount; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1131 int ndefs = com_argdefs(c, CHILD(n, 0), &argcount); |
|
1417
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1132 v = (object *) compile(CHILD(n, 0), c->c_filename); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1133 if (v == NULL) { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1134 c->c_errors++; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1135 i = 255; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1136 } |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1137 else { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1138 i = com_addconst(c, v); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1139 DECREF(v); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1140 } |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1141 com_addoparg(c, LOAD_CONST, i); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1142 com_addbyte(c, BUILD_FUNCTION); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1143 if (ndefs > 0) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1144 com_addoparg(c, SET_FUNC_ARGS, argcount); |
| 69 | 1145 } |
|
1417
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1146 else { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1147 int anchor = 0; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1148 int i = 0; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1149 for (;;) { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1150 com_and_test(c, CHILD(n, i)); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1151 if ((i += 2) >= NCH(n)) |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1152 break; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1153 com_addfwref(c, JUMP_IF_TRUE, &anchor); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1154 com_addbyte(c, POP_TOP); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1155 } |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1156 if (anchor) |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1157 com_backpatch(c, anchor); |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1158 } |
| 69 | 1159 } |
| 1160 | |
| 1161 static void | |
| 482 | 1162 com_list(c, n, toplevel) |
| 69 | 1163 struct compiling *c; |
| 1164 node *n; | |
| 482 | 1165 int toplevel; /* If nonzero, *always* build a tuple */ |
| 69 | 1166 { |
| 1167 /* exprlist: expr (',' expr)* [',']; likewise for testlist */ | |
| 482 | 1168 if (NCH(n) == 1 && !toplevel) { |
| 69 | 1169 com_node(c, CHILD(n, 0)); |
| 1170 } | |
| 1171 else { | |
| 1172 int i; | |
| 1173 int len; | |
| 1174 len = (NCH(n) + 1) / 2; | |
| 1175 for (i = 0; i < NCH(n); i += 2) | |
| 1176 com_node(c, CHILD(n, i)); | |
| 1177 com_addoparg(c, BUILD_TUPLE, len); | |
| 1178 } | |
| 1179 } | |
| 1180 | |
| 1181 | |
| 1182 /* Begin of assignment compilation */ | |
| 1183 | |
| 1184 static void com_assign_name PROTO((struct compiling *, node *, int)); | |
| 1185 static void com_assign PROTO((struct compiling *, node *, int)); | |
| 1186 | |
| 1187 static void | |
| 1188 com_assign_attr(c, n, assigning) | |
| 1189 struct compiling *c; | |
| 1190 node *n; | |
| 1191 int assigning; | |
| 1192 { | |
| 1193 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); | |
| 1194 } | |
| 1195 | |
| 1196 static void | |
| 1197 com_assign_slice(c, n, assigning) | |
| 1198 struct compiling *c; | |
| 1199 node *n; | |
| 1200 int assigning; | |
| 1201 { | |
| 1202 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE); | |
| 1203 } | |
| 1204 | |
| 1205 static void | |
| 1206 com_assign_subscript(c, n, assigning) | |
| 1207 struct compiling *c; | |
| 1208 node *n; | |
| 1209 int assigning; | |
| 1210 { | |
| 1211 com_node(c, n); | |
| 1212 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR); | |
| 1213 } | |
| 1214 | |
| 1215 static void | |
| 1216 com_assign_trailer(c, n, assigning) | |
| 1217 struct compiling *c; | |
| 1218 node *n; | |
| 1219 int assigning; | |
| 1220 { | |
| 1221 REQ(n, trailer); | |
| 1222 switch (TYPE(CHILD(n, 0))) { | |
| 1223 case LPAR: /* '(' [exprlist] ')' */ | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1224 err_setstr(SyntaxError, "can't assign to function call"); |
| 69 | 1225 c->c_errors++; |
| 1226 break; | |
| 1227 case DOT: /* '.' NAME */ | |
| 1228 com_assign_attr(c, CHILD(n, 1), assigning); | |
| 1229 break; | |
| 1230 case LSQB: /* '[' subscript ']' */ | |
| 1231 n = CHILD(n, 1); | |
| 1232 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */ | |
| 1233 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON) | |
| 1234 com_assign_slice(c, n, assigning); | |
| 1235 else | |
| 1236 com_assign_subscript(c, CHILD(n, 0), assigning); | |
| 1237 break; | |
| 1238 default: | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1239 err_setstr(SystemError, "unknown trailer type"); |
| 69 | 1240 c->c_errors++; |
| 1241 } | |
| 1242 } | |
| 1243 | |
| 1244 static void | |
| 1245 com_assign_tuple(c, n, assigning) | |
| 1246 struct compiling *c; | |
| 1247 node *n; | |
| 1248 int assigning; | |
| 1249 { | |
| 1250 int i; | |
| 1251 if (TYPE(n) != testlist) | |
| 1252 REQ(n, exprlist); | |
| 1253 if (assigning) | |
| 1254 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); | |
| 1255 for (i = 0; i < NCH(n); i += 2) | |
| 1256 com_assign(c, CHILD(n, i), assigning); | |
| 1257 } | |
| 1258 | |
| 1259 static void | |
| 1260 com_assign_list(c, n, assigning) | |
| 1261 struct compiling *c; | |
| 1262 node *n; | |
| 1263 int assigning; | |
| 1264 { | |
| 1265 int i; | |
| 1266 if (assigning) | |
| 1267 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2); | |
| 1268 for (i = 0; i < NCH(n); i += 2) | |
| 1269 com_assign(c, CHILD(n, i), assigning); | |
| 1270 } | |
| 1271 | |
| 1272 static void | |
| 1273 com_assign_name(c, n, assigning) | |
| 1274 struct compiling *c; | |
| 1275 node *n; | |
| 1276 int assigning; | |
| 1277 { | |
| 1278 REQ(n, NAME); | |
| 1279 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); | |
| 1280 } | |
| 1281 | |
| 1282 static void | |
| 1283 com_assign(c, n, assigning) | |
| 1284 struct compiling *c; | |
| 1285 node *n; | |
| 1286 int assigning; | |
| 1287 { | |
| 1288 /* Loop to avoid trivial recursion */ | |
| 1289 for (;;) { | |
| 1290 switch (TYPE(n)) { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1291 |
| 69 | 1292 case exprlist: |
| 1293 case testlist: | |
| 1294 if (NCH(n) > 1) { | |
| 1295 com_assign_tuple(c, n, assigning); | |
| 1296 return; | |
| 1297 } | |
| 1298 n = CHILD(n, 0); | |
| 1299 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1300 |
| 69 | 1301 case test: |
| 1302 case and_test: | |
| 1303 case not_test: | |
| 1304 case comparison: | |
| 1305 case expr: | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1306 case xor_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1307 case and_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1308 case shift_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1309 case arith_expr: |
| 69 | 1310 case term: |
| 1311 if (NCH(n) > 1) { | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1312 err_setstr(SyntaxError, |
| 69 | 1313 "can't assign to operator"); |
| 1314 c->c_errors++; | |
| 1315 return; | |
| 1316 } | |
| 1317 n = CHILD(n, 0); | |
| 1318 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1319 |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1320 case factor: /* ('+'|'-'|'~') factor | atom trailer* */ |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1321 if (TYPE(CHILD(n, 0)) != atom) { /* '+'|'-'|'~' */ |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1322 err_setstr(SyntaxError, |
| 69 | 1323 "can't assign to operator"); |
| 1324 c->c_errors++; | |
| 1325 return; | |
| 1326 } | |
| 1327 if (NCH(n) > 1) { /* trailer present */ | |
| 1328 int i; | |
| 1329 com_node(c, CHILD(n, 0)); | |
| 1330 for (i = 1; i+1 < NCH(n); i++) { | |
| 1331 com_apply_trailer(c, CHILD(n, i)); | |
| 1332 } /* NB i is still alive */ | |
| 1333 com_assign_trailer(c, | |
| 1334 CHILD(n, i), assigning); | |
| 1335 return; | |
| 1336 } | |
| 1337 n = CHILD(n, 0); | |
| 1338 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1339 |
| 69 | 1340 case atom: |
| 1341 switch (TYPE(CHILD(n, 0))) { | |
| 1342 case LPAR: | |
| 1343 n = CHILD(n, 1); | |
| 1344 if (TYPE(n) == RPAR) { | |
| 1345 /* XXX Should allow () = () ??? */ | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1346 err_setstr(SyntaxError, |
| 69 | 1347 "can't assign to ()"); |
| 1348 c->c_errors++; | |
| 1349 return; | |
| 1350 } | |
| 1351 break; | |
| 1352 case LSQB: | |
| 1353 n = CHILD(n, 1); | |
| 1354 if (TYPE(n) == RSQB) { | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1355 err_setstr(SyntaxError, |
| 69 | 1356 "can't assign to []"); |
| 1357 c->c_errors++; | |
| 1358 return; | |
| 1359 } | |
| 1360 com_assign_list(c, n, assigning); | |
| 1361 return; | |
| 1362 case NAME: | |
| 1363 com_assign_name(c, CHILD(n, 0), assigning); | |
| 1364 return; | |
| 1365 default: | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1366 err_setstr(SyntaxError, |
| 544 | 1367 "can't assign to literal"); |
| 69 | 1368 c->c_errors++; |
| 1369 return; | |
| 1370 } | |
| 1371 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1372 |
| 69 | 1373 default: |
| 1374 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 1375 err_setstr(SystemError, "com_assign: bad node"); | |
| 1376 c->c_errors++; | |
| 1377 return; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1378 |
| 69 | 1379 } |
| 1380 } | |
| 1381 } | |
| 1382 | |
| 1383 static void | |
| 1384 com_expr_stmt(c, n) | |
| 1385 struct compiling *c; | |
| 1386 node *n; | |
| 1387 { | |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1388 REQ(n, expr_stmt); /* testlist ('=' testlist)* */ |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1389 com_node(c, CHILD(n, NCH(n)-1)); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1390 if (NCH(n) == 1) { |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1391 if (c->c_interactive) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1392 com_addbyte(c, PRINT_EXPR); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1393 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1394 com_addbyte(c, POP_TOP); |
| 69 | 1395 } |
| 1396 else { | |
| 1397 int i; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1398 for (i = 0; i < NCH(n)-2; i+=2) { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1399 if (i+2 < NCH(n)-2) |
| 69 | 1400 com_addbyte(c, DUP_TOP); |
| 1401 com_assign(c, CHILD(n, i), 1/*assign*/); | |
| 1402 } | |
| 1403 } | |
| 1404 } | |
| 1405 | |
| 1406 static void | |
| 1407 com_print_stmt(c, n) | |
| 1408 struct compiling *c; | |
| 1409 node *n; | |
| 1410 { | |
| 1411 int i; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1412 REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1413 for (i = 1; i < NCH(n); i += 2) { |
| 69 | 1414 com_node(c, CHILD(n, i)); |
| 1415 com_addbyte(c, PRINT_ITEM); | |
| 1416 } | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1417 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
| 69 | 1418 com_addbyte(c, PRINT_NEWLINE); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1419 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
| 69 | 1420 } |
| 1421 | |
| 1422 static void | |
| 1423 com_return_stmt(c, n) | |
| 1424 struct compiling *c; | |
| 1425 node *n; | |
| 1426 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1427 REQ(n, return_stmt); /* 'return' [testlist] */ |
| 86 | 1428 if (!c->c_infunction) { |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1429 err_setstr(SyntaxError, "'return' outside function"); |
| 86 | 1430 c->c_errors++; |
| 1431 } | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1432 if (NCH(n) < 2) |
| 69 | 1433 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1434 else | |
| 1435 com_node(c, CHILD(n, 1)); | |
| 1436 com_addbyte(c, RETURN_VALUE); | |
| 1437 } | |
| 1438 | |
| 1439 static void | |
| 1440 com_raise_stmt(c, n) | |
| 1441 struct compiling *c; | |
| 1442 node *n; | |
| 1443 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1444 REQ(n, raise_stmt); /* 'raise' test [',' test] */ |
| 69 | 1445 com_node(c, CHILD(n, 1)); |
| 1446 if (NCH(n) > 3) | |
| 1447 com_node(c, CHILD(n, 3)); | |
| 1448 else | |
| 1449 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); | |
| 1450 com_addbyte(c, RAISE_EXCEPTION); | |
| 1451 } | |
| 1452 | |
| 1453 static void | |
| 1454 com_import_stmt(c, n) | |
| 1455 struct compiling *c; | |
| 1456 node *n; | |
| 1457 { | |
| 1458 int i; | |
| 1459 REQ(n, import_stmt); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1460 /* 'import' NAME (',' NAME)* | |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1461 'from' NAME 'import' ('*' | NAME (',' NAME)*) */ |
| 69 | 1462 if (STR(CHILD(n, 0))[0] == 'f') { |
| 1463 /* 'from' NAME 'import' ... */ | |
| 1464 REQ(CHILD(n, 1), NAME); | |
| 1465 com_addopname(c, IMPORT_NAME, CHILD(n, 1)); | |
| 1466 for (i = 3; i < NCH(n); i += 2) | |
| 1467 com_addopname(c, IMPORT_FROM, CHILD(n, i)); | |
| 1468 com_addbyte(c, POP_TOP); | |
| 1469 } | |
| 1470 else { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1471 /* 'import' ... */ |
| 69 | 1472 for (i = 1; i < NCH(n); i += 2) { |
| 1473 com_addopname(c, IMPORT_NAME, CHILD(n, i)); | |
| 1474 com_addopname(c, STORE_NAME, CHILD(n, i)); | |
| 1475 } | |
| 1476 } | |
| 1477 } | |
| 1478 | |
| 1479 static void | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1480 com_global_stmt(c, n) |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1481 struct compiling *c; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1482 node *n; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1483 { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1484 int i; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1485 REQ(n, global_stmt); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1486 /* 'global' NAME (',' NAME)* */ |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1487 for (i = 1; i < NCH(n); i += 2) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1488 if (dictinsert(c->c_globals, STR(CHILD(n, i)), None) != 0) |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1489 c->c_errors++; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1490 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1491 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1492 |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1493 #define strequ(a, b) (strcmp((a), (b)) == 0) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1494 |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1495 static void |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1496 com_access_stmt(c, n) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1497 struct compiling *c; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1498 node *n; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1499 { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1500 int i, j, k, mode, imode; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1501 object *vmode; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1502 REQ(n, access_stmt); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1503 /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1504 accesstype: NAME+ */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1505 |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1506 /* Find where the colon is */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1507 i = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1508 while (TYPE(CHILD(n,i-1)) != COLON) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1509 i += 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1510 |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1511 /* Calculate the mode mask */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1512 mode = 0; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1513 for (j = i; j < NCH(n); j += 2) { |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1514 int r = 0, w = 0, p = 0; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1515 for (k = 0; k < NCH(CHILD(n,j)); k++) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1516 if (strequ(STR(CHILD(CHILD(n,j),k)), "public")) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1517 p = 0; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1518 else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected")) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1519 p = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1520 else if (strequ(STR(CHILD(CHILD(n,j),k)), "private")) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1521 p = 2; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1522 else if (strequ(STR(CHILD(CHILD(n,j),k)), "read")) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1523 r = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1524 else if (strequ(STR(CHILD(CHILD(n,j),k)), "write")) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1525 w = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1526 else /* XXX should make this an exception */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1527 fprintf(stderr, "bad access type %s\n", |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1528 STR(CHILD(CHILD(n,j),k))); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1529 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1530 if (r == 0 && w == 0) |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1531 r = w = 1; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1532 if (p == 0) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1533 if (r == 1) mode |= AC_R_PUBLIC; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1534 if (w == 1) mode |= AC_W_PUBLIC; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1535 } else if (p == 1) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1536 if (r == 1) mode |= AC_R_PROTECTED; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1537 if (w == 1) mode |= AC_W_PROTECTED; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1538 } else { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1539 if (r == 1) mode |= AC_R_PRIVATE; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1540 if (w == 1) mode |= AC_W_PRIVATE; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1541 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1542 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1543 vmode = newintobject((long)mode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1544 imode = com_addconst(c, vmode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1545 XDECREF(vmode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1546 for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1547 com_addoparg(c, LOAD_CONST, imode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1548 com_addopname(c, ACCESS_MODE, CHILD(n, i)); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1549 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1550 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1551 |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1552 static void |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1553 com_exec_stmt(c, n) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1554 struct compiling *c; |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1555 node *n; |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1556 { |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1557 REQ(n, exec_stmt); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1558 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1559 com_node(c, CHILD(n, 1)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1560 if (NCH(n) >= 4) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1561 com_node(c, CHILD(n, 3)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1562 else |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1563 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1564 if (NCH(n) >= 6) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1565 com_node(c, CHILD(n, 5)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1566 else |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1567 com_addbyte(c, DUP_TOP); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1568 com_addbyte(c, EXEC_STMT); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1569 } |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1570 |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1571 static void |
| 69 | 1572 com_if_stmt(c, n) |
| 1573 struct compiling *c; | |
| 1574 node *n; | |
| 1575 { | |
| 1576 int i; | |
| 1577 int anchor = 0; | |
| 1578 REQ(n, if_stmt); | |
| 1579 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ | |
| 1580 for (i = 0; i+3 < NCH(n); i+=4) { | |
| 1581 int a = 0; | |
| 86 | 1582 node *ch = CHILD(n, i+1); |
| 1583 if (i > 0) | |
| 1584 com_addoparg(c, SET_LINENO, ch->n_lineno); | |
| 69 | 1585 com_node(c, CHILD(n, i+1)); |
| 1586 com_addfwref(c, JUMP_IF_FALSE, &a); | |
| 1587 com_addbyte(c, POP_TOP); | |
| 1588 com_node(c, CHILD(n, i+3)); | |
| 1589 com_addfwref(c, JUMP_FORWARD, &anchor); | |
| 1590 com_backpatch(c, a); | |
| 1591 com_addbyte(c, POP_TOP); | |
| 1592 } | |
| 1593 if (i+2 < NCH(n)) | |
| 1594 com_node(c, CHILD(n, i+2)); | |
| 1595 com_backpatch(c, anchor); | |
| 1596 } | |
| 1597 | |
| 1598 static void | |
| 1599 com_while_stmt(c, n) | |
| 1600 struct compiling *c; | |
| 1601 node *n; | |
| 1602 { | |
| 1603 int break_anchor = 0; | |
| 1604 int anchor = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1605 int save_begin = c->c_begin; |
| 69 | 1606 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1607 com_addfwref(c, SETUP_LOOP, &break_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1608 block_push(c, SETUP_LOOP); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1609 c->c_begin = c->c_nexti; |
| 86 | 1610 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1611 com_node(c, CHILD(n, 1)); |
| 1612 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1613 com_addbyte(c, POP_TOP); | |
| 86 | 1614 c->c_loops++; |
| 69 | 1615 com_node(c, CHILD(n, 3)); |
| 86 | 1616 c->c_loops--; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1617 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1618 c->c_begin = save_begin; |
| 69 | 1619 com_backpatch(c, anchor); |
| 1620 com_addbyte(c, POP_TOP); | |
| 1621 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1622 block_pop(c, SETUP_LOOP); |
| 69 | 1623 if (NCH(n) > 4) |
| 1624 com_node(c, CHILD(n, 6)); | |
| 1625 com_backpatch(c, break_anchor); | |
| 1626 } | |
| 1627 | |
| 1628 static void | |
| 1629 com_for_stmt(c, n) | |
| 1630 struct compiling *c; | |
| 1631 node *n; | |
| 1632 { | |
| 1633 object *v; | |
| 1634 int break_anchor = 0; | |
| 1635 int anchor = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1636 int save_begin = c->c_begin; |
| 69 | 1637 REQ(n, for_stmt); |
| 1638 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ | |
| 1639 com_addfwref(c, SETUP_LOOP, &break_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1640 block_push(c, SETUP_LOOP); |
| 69 | 1641 com_node(c, CHILD(n, 3)); |
| 1642 v = newintobject(0L); | |
| 1643 if (v == NULL) | |
| 1644 c->c_errors++; | |
| 1645 com_addoparg(c, LOAD_CONST, com_addconst(c, v)); | |
| 1646 XDECREF(v); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1647 c->c_begin = c->c_nexti; |
| 86 | 1648 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1649 com_addfwref(c, FOR_LOOP, &anchor); |
| 1650 com_assign(c, CHILD(n, 1), 1/*assigning*/); | |
| 86 | 1651 c->c_loops++; |
| 69 | 1652 com_node(c, CHILD(n, 5)); |
| 86 | 1653 c->c_loops--; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1654 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1655 c->c_begin = save_begin; |
| 69 | 1656 com_backpatch(c, anchor); |
| 1657 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1658 block_pop(c, SETUP_LOOP); |
| 69 | 1659 if (NCH(n) > 8) |
| 1660 com_node(c, CHILD(n, 8)); | |
| 1661 com_backpatch(c, break_anchor); | |
| 1662 } | |
| 1663 | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1664 /* Code generated for "try: S finally: Sf" is as follows: |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1665 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1666 SETUP_FINALLY L |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1667 <code for S> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1668 POP_BLOCK |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1669 LOAD_CONST <nil> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1670 L: <code for Sf> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1671 END_FINALLY |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1672 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1673 The special instructions use the block stack. Each block |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1674 stack entry contains the instruction that created it (here |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1675 SETUP_FINALLY), the level of the value stack at the time the |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1676 block stack entry was created, and a label (here L). |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1677 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1678 SETUP_FINALLY: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1679 Pushes the current value stack level and the label |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1680 onto the block stack. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1681 POP_BLOCK: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1682 Pops en entry from the block stack, and pops the value |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1683 stack until its level is the same as indicated on the |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1684 block stack. (The label is ignored.) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1685 END_FINALLY: |
| 86 | 1686 Pops a variable number of entries from the *value* stack |
| 1687 and re-raises the exception they specify. The number of | |
| 1688 entries popped depends on the (pseudo) exception type. | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1689 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1690 The block stack is unwound when an exception is raised: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1691 when a SETUP_FINALLY entry is found, the exception is pushed |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1692 onto the value stack (and the exception condition is cleared), |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1693 and the interpreter jumps to the label gotten from the block |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1694 stack. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1695 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1696 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
| 86 | 1697 (The contents of the value stack is shown in [], with the top |
| 1698 at the right; 'tb' is trace-back info, 'val' the exception's | |
| 1699 associated value, and 'exc' the exception.) | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1700 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1701 Value stack Label Instruction Argument |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1702 [] SETUP_EXCEPT L1 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1703 [] <code for S> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1704 [] POP_BLOCK |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1705 [] JUMP_FORWARD L0 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1706 |
| 86 | 1707 [tb, val, exc] L1: DUP ) |
| 1708 [tb, val, exc, exc] <evaluate E1> ) | |
| 1709 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 | |
| 1710 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) | |
| 1711 [tb, val, exc, 1] POP ) | |
| 1712 [tb, val, exc] POP | |
| 1713 [tb, val] <assign to V1> (or POP if no V1) | |
| 1714 [tb] POP | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1715 [] <code for S1> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1716 JUMP_FORWARD L0 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1717 |
| 86 | 1718 [tb, val, exc, 0] L2: POP |
| 1719 [tb, val, exc] DUP | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1720 .............................etc....................... |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1721 |
| 86 | 1722 [tb, val, exc, 0] Ln+1: POP |
| 1723 [tb, val, exc] END_FINALLY # re-raise exception | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1724 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1725 [] L0: <next statement> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1726 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1727 Of course, parts are not generated if Vi or Ei is not present. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1728 */ |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1729 |
| 69 | 1730 static void |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1731 com_try_except(c, n) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1732 struct compiling *c; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1733 node *n; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1734 { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1735 int except_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1736 int end_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1737 int else_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1738 int i; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1739 node *ch; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1740 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1741 com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1742 block_push(c, SETUP_EXCEPT); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1743 com_node(c, CHILD(n, 2)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1744 com_addbyte(c, POP_BLOCK); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1745 block_pop(c, SETUP_EXCEPT); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1746 com_addfwref(c, JUMP_FORWARD, &else_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1747 com_backpatch(c, except_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1748 for (i = 3; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1749 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1750 i += 3) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1751 /* except_clause: 'except' [expr [',' expr]] */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1752 if (except_anchor == 0) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1753 err_setstr(SyntaxError, |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1754 "default 'except:' must be last"); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1755 c->c_errors++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1756 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1757 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1758 except_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1759 com_addoparg(c, SET_LINENO, ch->n_lineno); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1760 if (NCH(ch) > 1) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1761 com_addbyte(c, DUP_TOP); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1762 com_node(c, CHILD(ch, 1)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1763 com_addoparg(c, COMPARE_OP, EXC_MATCH); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1764 com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1765 com_addbyte(c, POP_TOP); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1766 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1767 com_addbyte(c, POP_TOP); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1768 if (NCH(ch) > 3) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1769 com_assign(c, CHILD(ch, 3), 1/*assigning*/); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1770 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1771 com_addbyte(c, POP_TOP); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1772 com_addbyte(c, POP_TOP); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1773 com_node(c, CHILD(n, i+2)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1774 com_addfwref(c, JUMP_FORWARD, &end_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1775 if (except_anchor) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1776 com_backpatch(c, except_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1777 com_addbyte(c, POP_TOP); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1778 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1779 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1780 com_addbyte(c, END_FINALLY); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1781 com_backpatch(c, else_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1782 if (i < NCH(n)) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1783 com_node(c, CHILD(n, i+2)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1784 com_backpatch(c, end_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1785 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1786 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1787 static void |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1788 com_try_finally(c, n) |
| 69 | 1789 struct compiling *c; |
| 1790 node *n; | |
| 1791 { | |
| 1792 int finally_anchor = 0; | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1793 node *ch; |
|
792
dc3b455f7632
Only * can be used for varargs argument lists
Guido van Rossum <guido@python.org>
parents:
744
diff
changeset
|
1794 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1795 com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1796 block_push(c, SETUP_FINALLY); |
| 69 | 1797 com_node(c, CHILD(n, 2)); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1798 com_addbyte(c, POP_BLOCK); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1799 block_pop(c, SETUP_FINALLY); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1800 block_push(c, END_FINALLY); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1801 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1802 com_backpatch(c, finally_anchor); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1803 ch = CHILD(n, NCH(n)-1); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1804 com_addoparg(c, SET_LINENO, ch->n_lineno); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1805 com_node(c, ch); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1806 com_addbyte(c, END_FINALLY); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1807 block_pop(c, END_FINALLY); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1808 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1809 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1810 static void |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1811 com_try_stmt(c, n) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1812 struct compiling *c; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1813 node *n; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1814 { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1815 REQ(n, try_stmt); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1816 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1817 | 'try' ':' suite 'finally' ':' suite */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1818 if (TYPE(CHILD(n, 3)) != except_clause) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1819 com_try_finally(c, n); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1820 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1821 com_try_except(c, n); |
| 69 | 1822 } |
| 1823 | |
| 1824 static void | |
| 1825 com_suite(c, n) | |
| 1826 struct compiling *c; | |
| 1827 node *n; | |
| 1828 { | |
| 1829 REQ(n, suite); | |
| 1830 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ | |
| 1831 if (NCH(n) == 1) { | |
| 1832 com_node(c, CHILD(n, 0)); | |
| 1833 } | |
| 1834 else { | |
| 1835 int i; | |
| 1836 for (i = 0; i < NCH(n); i++) { | |
| 1837 node *ch = CHILD(n, i); | |
| 1838 if (TYPE(ch) == stmt) | |
| 1839 com_node(c, ch); | |
| 1840 } | |
| 1841 } | |
| 1842 } | |
| 1843 | |
| 744 | 1844 /* ARGSUSED */ |
| 69 | 1845 static void |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1846 com_continue_stmt(c, n) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1847 struct compiling *c; |
| 744 | 1848 node *n; /* Not used, but passed for consistency */ |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1849 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1850 int i = c->c_nblocks; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1851 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1852 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1853 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1854 else { |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1855 err_setstr(SyntaxError, "'continue' not properly in loop"); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1856 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1857 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1858 /* XXX Could allow it inside a 'finally' clause |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1859 XXX if we could pop the exception still on the stack */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1860 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1861 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1862 static int |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1863 com_argdefs(c, n, argcount_return) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1864 struct compiling *c; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1865 node *n; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1866 int *argcount_return; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1867 { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1868 int i, nch, nargs, ndefs, star; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1869 if (TYPE(n) == lambdef) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1870 /* lambdef: 'lambda' [varargslist] ':' test */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1871 n = CHILD(n, 1); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1872 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1873 else { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1874 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1875 n = CHILD(n, 2); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1876 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1877 n = CHILD(n, 1); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1878 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1879 if (TYPE(n) != varargslist) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1880 return -1; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1881 /* varargslist: |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1882 (fpdef ['=' test] ',')* '*' NAME | |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1883 fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1884 nch = NCH(n); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1885 if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1886 star = 1; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1887 nch -= 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1888 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1889 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1890 star = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1891 nargs = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1892 ndefs = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1893 for (i = 0; i < nch; i++) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1894 int t; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1895 nargs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1896 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1897 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1898 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1899 t = TYPE(CHILD(n, i)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1900 if (t == EQUAL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1901 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1902 ndefs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1903 com_node(c, CHILD(n, i)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1904 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1905 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1906 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1907 t = TYPE(CHILD(n, i)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1908 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1909 else { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1910 /* Treat "(a=1, b)" as "(a=1, b=None)" */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1911 if (ndefs) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1912 com_addoparg(c, LOAD_CONST, |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1913 com_addconst(c, None)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1914 ndefs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1915 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1916 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1917 if (t != COMMA) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1918 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1919 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1920 if (star) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1921 nargs ^= 0x4000; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1922 *argcount_return = nargs; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1923 if (ndefs > 0) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1924 com_addoparg(c, BUILD_TUPLE, ndefs); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1925 return ndefs; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1926 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1927 |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1928 static void |
| 69 | 1929 com_funcdef(c, n) |
| 1930 struct compiling *c; | |
| 1931 node *n; | |
| 1932 { | |
| 1933 object *v; | |
| 1934 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ | |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1935 v = (object *)compile(n, c->c_filename); |
| 69 | 1936 if (v == NULL) |
| 1937 c->c_errors++; | |
| 1938 else { | |
| 1939 int i = com_addconst(c, v); | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1940 int argcount; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1941 int ndefs = com_argdefs(c, n, &argcount); |
| 69 | 1942 com_addoparg(c, LOAD_CONST, i); |
| 1943 com_addbyte(c, BUILD_FUNCTION); | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1944 if (ndefs > 0) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1945 com_addoparg(c, SET_FUNC_ARGS, argcount); |
| 69 | 1946 com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 1947 DECREF(v); | |
| 1948 } | |
| 1949 } | |
| 1950 | |
| 1951 static void | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1952 com_bases(c, n) |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1953 struct compiling *c; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1954 node *n; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1955 { |
| 744 | 1956 int i; |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1957 REQ(n, testlist); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1958 /* testlist: test (',' test)* [','] */ |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1959 for (i = 0; i < NCH(n); i += 2) |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1960 com_node(c, CHILD(n, i)); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1961 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1962 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1963 |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1964 static void |
| 69 | 1965 com_classdef(c, n) |
| 1966 struct compiling *c; | |
| 1967 node *n; | |
| 1968 { | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1969 int i; |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1970 object *v; |
| 69 | 1971 REQ(n, classdef); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1972 /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1973 if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1974 c->c_errors++; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1975 return; |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1976 } |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1977 /* Push the class name on the stack */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1978 i = com_addconst(c, v); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1979 com_addoparg(c, LOAD_CONST, i); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1980 DECREF(v); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1981 /* Push the tuple of base classes on the stack */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1982 if (TYPE(CHILD(n, 2)) != LPAR) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1983 com_addoparg(c, BUILD_TUPLE, 0); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1984 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1985 com_bases(c, CHILD(n, 3)); |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1986 v = (object *)compile(n, c->c_filename); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1987 if (v == NULL) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1988 c->c_errors++; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1989 else { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1990 i = com_addconst(c, v); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1991 com_addoparg(c, LOAD_CONST, i); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1992 com_addbyte(c, BUILD_FUNCTION); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1993 com_addbyte(c, UNARY_CALL); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1994 com_addbyte(c, BUILD_CLASS); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1995 com_addopname(c, STORE_NAME, CHILD(n, 1)); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1996 DECREF(v); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1997 } |
| 69 | 1998 } |
| 1999 | |
| 2000 static void | |
| 2001 com_node(c, n) | |
| 2002 struct compiling *c; | |
| 2003 node *n; | |
| 2004 { | |
| 2005 switch (TYPE(n)) { | |
| 2006 | |
| 2007 /* Definition nodes */ | |
| 2008 | |
| 2009 case funcdef: | |
| 2010 com_funcdef(c, n); | |
| 2011 break; | |
| 2012 case classdef: | |
| 2013 com_classdef(c, n); | |
| 2014 break; | |
| 2015 | |
| 2016 /* Trivial parse tree nodes */ | |
| 2017 | |
| 2018 case stmt: | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2019 case small_stmt: |
| 86 | 2020 case flow_stmt: |
| 2021 com_node(c, CHILD(n, 0)); | |
| 2022 break; | |
| 2023 | |
| 69 | 2024 case simple_stmt: |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2025 /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2026 com_addoparg(c, SET_LINENO, n->n_lineno); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2027 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2028 int i; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2029 for (i = 0; i < NCH(n)-1; i += 2) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2030 com_node(c, CHILD(n, i)); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2031 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2032 break; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2033 |
| 69 | 2034 case compound_stmt: |
| 86 | 2035 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 2036 com_node(c, CHILD(n, 0)); |
| 2037 break; | |
| 2038 | |
| 2039 /* Statement nodes */ | |
| 2040 | |
| 2041 case expr_stmt: | |
| 2042 com_expr_stmt(c, n); | |
| 2043 break; | |
| 2044 case print_stmt: | |
| 2045 com_print_stmt(c, n); | |
| 2046 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2047 case del_stmt: /* 'del' exprlist */ |
| 69 | 2048 com_assign(c, CHILD(n, 1), 0/*delete*/); |
| 2049 break; | |
| 2050 case pass_stmt: | |
| 2051 break; | |
| 2052 case break_stmt: | |
| 86 | 2053 if (c->c_loops == 0) { |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
2054 err_setstr(SyntaxError, "'break' outside loop"); |
| 86 | 2055 c->c_errors++; |
| 2056 } | |
| 69 | 2057 com_addbyte(c, BREAK_LOOP); |
| 2058 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2059 case continue_stmt: |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2060 com_continue_stmt(c, n); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2061 break; |
| 69 | 2062 case return_stmt: |
| 2063 com_return_stmt(c, n); | |
| 2064 break; | |
| 2065 case raise_stmt: | |
| 2066 com_raise_stmt(c, n); | |
| 2067 break; | |
| 2068 case import_stmt: | |
| 2069 com_import_stmt(c, n); | |
| 2070 break; | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2071 case global_stmt: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2072 com_global_stmt(c, n); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2073 break; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2074 case access_stmt: |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2075 com_access_stmt(c, n); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2076 break; |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2077 case exec_stmt: |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2078 com_exec_stmt(c, n); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2079 break; |
| 69 | 2080 case if_stmt: |
| 2081 com_if_stmt(c, n); | |
| 2082 break; | |
| 2083 case while_stmt: | |
| 2084 com_while_stmt(c, n); | |
| 2085 break; | |
| 2086 case for_stmt: | |
| 2087 com_for_stmt(c, n); | |
| 2088 break; | |
| 2089 case try_stmt: | |
| 2090 com_try_stmt(c, n); | |
| 2091 break; | |
| 2092 case suite: | |
| 2093 com_suite(c, n); | |
| 2094 break; | |
| 2095 | |
| 2096 /* Expression nodes */ | |
| 2097 | |
| 2098 case testlist: | |
| 482 | 2099 com_list(c, n, 0); |
| 69 | 2100 break; |
| 2101 case test: | |
| 2102 com_test(c, n); | |
| 2103 break; | |
| 2104 case and_test: | |
| 2105 com_and_test(c, n); | |
| 2106 break; | |
| 2107 case not_test: | |
| 2108 com_not_test(c, n); | |
| 2109 break; | |
| 2110 case comparison: | |
| 2111 com_comparison(c, n); | |
| 2112 break; | |
| 2113 case exprlist: | |
| 482 | 2114 com_list(c, n, 0); |
| 69 | 2115 break; |
| 2116 case expr: | |
| 2117 com_expr(c, n); | |
| 2118 break; | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2119 case xor_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2120 com_xor_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2121 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2122 case and_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2123 com_and_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2124 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2125 case shift_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2126 com_shift_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2127 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2128 case arith_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2129 com_arith_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2130 break; |
| 69 | 2131 case term: |
| 2132 com_term(c, n); | |
| 2133 break; | |
| 2134 case factor: | |
| 2135 com_factor(c, n); | |
| 2136 break; | |
| 2137 case atom: | |
| 2138 com_atom(c, n); | |
| 2139 break; | |
| 2140 | |
| 2141 default: | |
| 2142 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 2143 err_setstr(SystemError, "com_node: unexpected node type"); | |
| 2144 c->c_errors++; | |
| 2145 } | |
| 2146 } | |
| 2147 | |
| 2148 static void com_fplist PROTO((struct compiling *, node *)); | |
| 2149 | |
| 2150 static void | |
| 2151 com_fpdef(c, n) | |
| 2152 struct compiling *c; | |
| 2153 node *n; | |
| 2154 { | |
| 2155 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ | |
| 2156 if (TYPE(CHILD(n, 0)) == LPAR) | |
| 2157 com_fplist(c, CHILD(n, 1)); | |
| 2158 else | |
| 2159 com_addopname(c, STORE_NAME, CHILD(n, 0)); | |
| 2160 } | |
| 2161 | |
| 2162 static void | |
| 2163 com_fplist(c, n) | |
| 2164 struct compiling *c; | |
| 2165 node *n; | |
| 2166 { | |
| 544 | 2167 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
| 69 | 2168 if (NCH(n) == 1) { |
| 2169 com_fpdef(c, CHILD(n, 0)); | |
| 2170 } | |
| 2171 else { | |
| 2172 int i; | |
| 2173 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); | |
| 2174 for (i = 0; i < NCH(n); i += 2) | |
| 2175 com_fpdef(c, CHILD(n, i)); | |
| 2176 } | |
| 2177 } | |
| 2178 | |
| 2179 static void | |
| 584 | 2180 com_arglist(c, n) |
| 2181 struct compiling *c; | |
| 2182 node *n; | |
| 2183 { | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2184 int nch, op, nargs, i, t; |
| 584 | 2185 REQ(n, varargslist); |
|
617
755bb8efcc2d
Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents:
584
diff
changeset
|
2186 /* varargslist: |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2187 (fpdef ['=' test] ',')* '*' NAME | |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2188 fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2189 nch = NCH(n); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2190 if (nch >= 2 && TYPE(CHILD(n, nch-2)) == STAR) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2191 op = UNPACK_VARARG; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2192 nch -= 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2193 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2194 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2195 op = UNPACK_ARG; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2196 nargs = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2197 for (i = 0; i < nch; i++) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2198 nargs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2199 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2200 if (i >= nch) |
| 584 | 2201 break; |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2202 t = TYPE(CHILD(n, i)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2203 if (t == EQUAL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2204 i += 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2205 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2206 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2207 t = TYPE(CHILD(n, i)); |
| 584 | 2208 } |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2209 if (t != COMMA) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2210 break; |
| 584 | 2211 } |
| 2212 com_addoparg(c, op, nargs); | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2213 for (i = 0; i < nch; i++) { |
| 584 | 2214 com_fpdef(c, CHILD(n, i)); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2215 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2216 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2217 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2218 t = TYPE(CHILD(n, i)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2219 if (t == EQUAL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2220 i += 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2221 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2222 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2223 t = TYPE(CHILD(n, i)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2224 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2225 if (t != COMMA) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2226 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2227 } |
| 584 | 2228 if (op == UNPACK_VARARG) |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2229 com_addopname(c, STORE_NAME, CHILD(n, nch+1)); |
| 584 | 2230 } |
| 2231 | |
| 2232 static void | |
| 69 | 2233 com_file_input(c, n) |
| 2234 struct compiling *c; | |
| 2235 node *n; | |
| 2236 { | |
| 2237 int i; | |
| 2238 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ | |
| 2239 for (i = 0; i < NCH(n); i++) { | |
| 2240 node *ch = CHILD(n, i); | |
| 2241 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) | |
| 2242 com_node(c, ch); | |
| 2243 } | |
| 2244 } | |
| 2245 | |
| 2246 /* Top-level compile-node interface */ | |
| 2247 | |
| 2248 static void | |
| 2249 compile_funcdef(c, n) | |
| 2250 struct compiling *c; | |
| 2251 node *n; | |
| 2252 { | |
| 2253 node *ch; | |
| 2254 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2255 c->c_name = STR(CHILD(n, 1)); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2256 com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); /* Patched! */ |
| 584 | 2257 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 2258 ch = CHILD(ch, 1); /* ')' | varargslist */ | |
| 69 | 2259 if (TYPE(ch) == RPAR) |
| 482 | 2260 com_addoparg(c, UNPACK_ARG, 0); |
| 584 | 2261 else |
| 2262 com_arglist(c, ch); | |
| 86 | 2263 c->c_infunction = 1; |
| 69 | 2264 com_node(c, CHILD(n, 4)); |
| 86 | 2265 c->c_infunction = 0; |
| 69 | 2266 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2267 com_addbyte(c, RETURN_VALUE); | |
| 2268 } | |
| 2269 | |
| 2270 static void | |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2271 compile_lambdef(c, n) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2272 struct compiling *c; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2273 node *n; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2274 { |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2275 node *ch; |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2276 REQ(n, lambdef); /* lambdef: 'lambda' [parameters] ':' test */ |
|
2005
ed5d0ad5ea2f
set name to <lambda> instead of None for lambdas
Guido van Rossum <guido@python.org>
parents:
1833
diff
changeset
|
2277 c->c_name = "<lambda>"; |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2278 |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2279 ch = CHILD(n, 1); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2280 if (TYPE(ch) == COLON) { |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2281 com_addoparg(c, UNPACK_ARG, 0); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2282 com_node(c, CHILD(n, 2)); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2283 } |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2284 else { |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2285 com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2286 com_arglist(c, ch); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2287 com_node(c, CHILD(n, 3)); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2288 } |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2289 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2290 com_addbyte(c, RETURN_VALUE); |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2291 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2292 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2293 static void |
| 69 | 2294 compile_node(c, n) |
| 2295 struct compiling *c; | |
| 2296 node *n; | |
| 2297 { | |
| 86 | 2298 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2299 | |
| 69 | 2300 switch (TYPE(n)) { |
| 2301 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2302 case single_input: /* One interactive command */ |
| 69 | 2303 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2304 c->c_interactive++; |
| 69 | 2305 n = CHILD(n, 0); |
| 2306 if (TYPE(n) != NEWLINE) | |
| 2307 com_node(c, n); | |
| 86 | 2308 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2309 com_addbyte(c, RETURN_VALUE); | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2310 c->c_interactive--; |
| 69 | 2311 break; |
| 2312 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2313 case file_input: /* A whole file, or built-in function exec() */ |
| 69 | 2314 com_file_input(c, n); |
| 86 | 2315 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2316 com_addbyte(c, RETURN_VALUE); | |
| 69 | 2317 break; |
| 2318 | |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2319 case eval_input: /* Built-in function input() */ |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2320 com_node(c, CHILD(n, 0)); |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2321 com_addbyte(c, RETURN_VALUE); |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2322 break; |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2323 |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2324 case lambdef: /* anonymous function definition */ |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2325 compile_lambdef(c, n); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2326 break; |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2327 |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2328 case funcdef: /* A function definition */ |
| 69 | 2329 compile_funcdef(c, n); |
| 2330 break; | |
| 2331 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2332 case classdef: /* A class definition */ |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2333 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2334 c->c_name = STR(CHILD(n, 1)); |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2335 com_node(c, CHILD(n, NCH(n)-1)); /* The suite */ |
| 86 | 2336 com_addbyte(c, LOAD_LOCALS); |
| 2337 com_addbyte(c, RETURN_VALUE); | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2338 break; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2339 |
| 69 | 2340 default: |
| 2341 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 2342 err_setstr(SystemError, "compile_node: unexpected node type"); | |
| 2343 c->c_errors++; | |
| 2344 } | |
| 2345 } | |
| 2346 | |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2347 /* Optimization for local variables in functions (and *only* functions). |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2348 |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2349 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2350 instructions that refer to local variables with LOAD_FAST etc. |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2351 The latter instructions are much faster because they don't need to |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2352 look up the variable name in a dictionary. |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2353 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2354 To find all local variables, we check all STORE_NAME, IMPORT_FROM and |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2355 DELETE_NAME instructions. This yields all local variables, including |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2356 arguments, function definitions, class definitions and import |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2357 statements. |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2358 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2359 All remaining LOAD_NAME instructions must refer to non-local (global |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2360 or builtin) variables, so are replaced by LOAD_GLOBAL. |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2361 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2362 There are two problems: 'from foo import *' and 'exec' may introduce |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2363 local variables that we can't know while compiling. If this is the |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2364 case, we don't optimize at all (this rarely happens, since exec is |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2365 rare, & this form of import statement is mostly used at the module |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2366 level). |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2367 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2368 NB: this modifies the string object co->co_code! |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2369 */ |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2370 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2371 static void |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2372 optimize(c) |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2373 struct compiling *c; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2374 { |
|
203
ceb36ec6198d
BUGFIX! Instructions are unsigned bytes.
Guido van Rossum <guido@python.org>
parents:
176
diff
changeset
|
2375 unsigned char *next_instr, *cur_instr; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2376 object *locals; |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2377 int nlocals; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2378 int opcode; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2379 int oparg; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2380 object *name; |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2381 int fast_reserved; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2382 object *error_type, *error_value; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2383 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2384 #define NEXTOP() (*next_instr++) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2385 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2386 #define GETITEM(v, i) (getlistitem((v), (i))) |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2387 #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i))) |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2388 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2389 locals = newdictobject(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2390 if (locals == NULL) { |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2391 c->c_errors++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2392 return; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2393 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2394 nlocals = 0; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2395 |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2396 err_get(&error_type, &error_value); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2397 |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2398 next_instr = (unsigned char *) getstringvalue(c->c_code); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2399 for (;;) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2400 opcode = NEXTOP(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2401 if (opcode == STOP_CODE) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2402 break; |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2403 if (opcode == EXEC_STMT) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2404 goto end; /* Don't optimize if exec present */ |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2405 if (HAS_ARG(opcode)) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2406 oparg = NEXTARG(); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2407 if (opcode == STORE_NAME || opcode == DELETE_NAME || |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2408 opcode == IMPORT_FROM) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2409 object *v; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2410 name = GETNAMEOBJ(oparg); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2411 if (dict2lookup(locals, name) != NULL) |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2412 continue; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2413 err_clear(); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2414 v = newintobject(nlocals); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2415 if (v == NULL) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2416 c->c_errors++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2417 goto err; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2418 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2419 nlocals++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2420 if (dict2insert(locals, name, v) != 0) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2421 DECREF(v); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2422 c->c_errors++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2423 goto err; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2424 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2425 DECREF(v); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2426 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2427 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2428 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2429 if (dictlookup(locals, "*") != NULL) { |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2430 /* Don't optimize anything */ |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2431 goto end; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2432 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2433 |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2434 next_instr = (unsigned char *) getstringvalue(c->c_code); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2435 fast_reserved = 0; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2436 for (;;) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2437 cur_instr = next_instr; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2438 opcode = NEXTOP(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2439 if (opcode == STOP_CODE) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2440 break; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2441 if (HAS_ARG(opcode)) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2442 oparg = NEXTARG(); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2443 if (opcode == RESERVE_FAST) { |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2444 int i; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2445 object *localmap = newtupleobject(nlocals); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2446 int pos; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2447 object *key, *value; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2448 if (localmap == NULL) { /* XXX mask error */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2449 err_clear(); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2450 continue; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2451 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2452 pos = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2453 while (mappinggetnext(locals, &pos, &key, &value)) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2454 int j; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2455 if (!is_intobject(value)) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2456 continue; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2457 j = getintvalue(value); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2458 if (0 <= j && j < nlocals) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2459 INCREF(key); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2460 settupleitem(localmap, j, key); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2461 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2462 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2463 i = com_addconst(c, localmap); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2464 cur_instr[1] = i & 0xff; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2465 cur_instr[2] = (i>>8) & 0xff; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2466 fast_reserved = 1; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2467 continue; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2468 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2469 if (!fast_reserved) |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2470 continue; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2471 if (opcode == LOAD_NAME || |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2472 opcode == STORE_NAME || |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2473 opcode == DELETE_NAME) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2474 object *v; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2475 int i; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2476 name = GETNAMEOBJ(oparg); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2477 v = dict2lookup(locals, name); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2478 if (v == NULL) { |
| 336 | 2479 err_clear(); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2480 if (opcode == LOAD_NAME) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2481 cur_instr[0] = LOAD_GLOBAL; |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2482 continue; |
| 336 | 2483 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2484 i = getintvalue(v); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2485 switch (opcode) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2486 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2487 case STORE_NAME: cur_instr[0] = STORE_FAST; break; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2488 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2489 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2490 cur_instr[1] = i & 0xff; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2491 cur_instr[2] = (i>>8) & 0xff; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2492 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2493 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2494 |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2495 end: |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2496 err_setval(error_type, error_value); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2497 err: |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2498 DECREF(locals); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2499 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2500 |
| 69 | 2501 codeobject * |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2502 compile(n, filename) |
| 69 | 2503 node *n; |
| 86 | 2504 char *filename; |
| 69 | 2505 { |
| 2506 struct compiling sc; | |
| 2507 codeobject *co; | |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2508 if (!com_init(&sc, filename)) |
| 69 | 2509 return NULL; |
| 2510 compile_node(&sc, n); | |
| 2511 com_done(&sc); | |
|
1415
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2512 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2513 optimize(&sc); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2514 co = NULL; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2515 if (sc.c_errors == 0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2516 object *v, *w; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2517 v = newstringobject(sc.c_filename); |
|
2005
ed5d0ad5ea2f
set name to <lambda> instead of None for lambdas
Guido van Rossum <guido@python.org>
parents:
1833
diff
changeset
|
2518 w = newstringobject(sc.c_name); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2519 if (v != NULL && w != NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2520 co = newcodeobject(sc.c_code, sc.c_consts, |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2521 sc.c_names, v, w); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2522 XDECREF(v); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2523 XDECREF(w); |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
2524 } |
| 69 | 2525 com_free(&sc); |
| 2526 return co; | |
| 2527 } |
