Mercurial > cpython-lru_cache-weakref
annotate Python/compile.c @ 2147:838ef9768821 legacy-trunk
added $& to $(LIB) target for Sequent
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 17 Jan 1995 16:11:29 +0000 |
| parents | 53aa51433f7f |
| children | cdb373f65bf3 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
1 /*********************************************************** |
|
2062
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2052
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2052
diff
changeset
|
3 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); |
|
2052
d7e91437f0a2
Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
2005
diff
changeset
|
339 fatal("com_addbyte: byte out of range"); |
|
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 | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
449 com_addopnamestr(c, op, name) |
| 69 | 450 struct compiling *c; |
| 451 int op; | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
452 char *name; |
| 69 | 453 { |
| 454 object *v; | |
| 455 int i; | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
456 if (name == NULL || (v = newstringobject(name)) == NULL) { |
| 69 | 457 c->c_errors++; |
| 458 i = 255; | |
| 459 } | |
| 460 else { | |
| 461 i = com_addname(c, v); | |
| 462 DECREF(v); | |
| 463 } | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
464 /* 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
|
465 switch (op) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
466 case LOAD_NAME: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
467 case STORE_NAME: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
468 case DELETE_NAME: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
469 if (dictlookup(c->c_globals, name) != NULL) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
470 switch (op) { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
471 case LOAD_NAME: op = LOAD_GLOBAL; break; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
472 case STORE_NAME: op = STORE_GLOBAL; break; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
473 case DELETE_NAME: op = DELETE_GLOBAL; break; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
474 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
475 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
476 } |
| 69 | 477 com_addoparg(c, op, i); |
| 478 } | |
| 479 | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
480 static void |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
481 com_addopname(c, op, n) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
482 struct compiling *c; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
483 int op; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
484 node *n; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
485 { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
486 object *v; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
487 char *name; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
488 char buffer[1000]; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
489 /* XXX it is possible to write this code without the 1000 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
490 chars on the total length of dotted names, I just can't be |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
491 bothered right now */ |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
492 if (TYPE(n) == STAR) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
493 name = "*"; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
494 else if (TYPE(n) == dotted_name) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
495 char *p = buffer; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
496 int i; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
497 name = buffer; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
498 for (i = 0; i < NCH(n); i += 2) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
499 char *s = STR(CHILD(n, i)); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
500 if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
501 err_setstr(MemoryError, |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
502 "dotted_name too long"); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
503 name == NULL; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
504 break; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
505 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
506 if (p != buffer) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
507 *p++ = '.'; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
508 strcpy(p, s); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
509 p = strchr(p, '\0'); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
510 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
511 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
512 else { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
513 REQ(n, NAME); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
514 name = STR(n); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
515 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
516 com_addopnamestr(c, op, name); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
517 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
518 |
| 69 | 519 static object * |
| 520 parsenumber(s) | |
| 521 char *s; | |
| 522 { | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
523 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
|
524 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
|
525 extern double atof PROTO((const char *)); |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
526 char *end; |
| 69 | 527 long x; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
528 errno = 0; |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
529 end = s + strlen(s) - 1; |
| 744 | 530 if (*end == 'l' || *end == 'L') |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
531 return long_scan(s, 0); |
|
617
755bb8efcc2d
Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents:
584
diff
changeset
|
532 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
|
533 x = (long) mystrtoul(s, &end, 0); |
|
617
755bb8efcc2d
Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents:
584
diff
changeset
|
534 else |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
535 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
|
536 if (*end == '\0') { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
537 if (errno != 0) { |
|
493
57667ab1e194
Changed some RuntimeErrors.
Guido van Rossum <guido@python.org>
parents:
482
diff
changeset
|
538 err_setstr(OverflowError, |
| 544 | 539 "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
|
540 return NULL; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
541 } |
| 69 | 542 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
|
543 } |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
544 /* 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
|
545 return newfloatobject(atof(s)); |
| 69 | 546 } |
| 547 | |
| 548 static object * | |
| 549 parsestr(s) | |
| 550 char *s; | |
| 551 { | |
| 552 object *v; | |
| 553 int len; | |
| 554 char *buf; | |
| 555 char *p; | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
556 char *end; |
| 69 | 557 int c; |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
558 int quote = *s; |
|
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
559 if (quote != '\'' && quote != '\"') { |
| 69 | 560 err_badcall(); |
| 561 return NULL; | |
| 562 } | |
| 563 s++; | |
| 564 len = strlen(s); | |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
565 if (s[--len] != quote) { |
| 69 | 566 err_badcall(); |
| 567 return NULL; | |
| 568 } | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
569 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
|
570 s += 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
571 len -= 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
572 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
|
573 err_badcall(); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
574 return NULL; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
575 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
576 } |
| 69 | 577 if (strchr(s, '\\') == NULL) |
| 578 return newsizedstringobject(s, len); | |
| 579 v = newsizedstringobject((char *)NULL, len); | |
| 580 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
|
581 end = s + len; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
582 while (s < end) { |
| 69 | 583 if (*s != '\\') { |
| 584 *p++ = *s++; | |
| 585 continue; | |
| 586 } | |
| 587 s++; | |
| 588 switch (*s++) { | |
| 589 /* 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
|
590 case '\n': break; |
| 69 | 591 case '\\': *p++ = '\\'; break; |
| 592 case '\'': *p++ = '\''; break; | |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
593 case '\"': *p++ = '\"'; break; |
| 69 | 594 case 'b': *p++ = '\b'; break; |
| 595 case 'f': *p++ = '\014'; break; /* FF */ | |
| 596 case 't': *p++ = '\t'; break; | |
| 597 case 'n': *p++ = '\n'; break; | |
| 598 case 'r': *p++ = '\r'; break; | |
| 599 case 'v': *p++ = '\013'; break; /* VT */ | |
| 600 case 'a': *p++ = '\007'; break; /* BEL, not classic C */ | |
| 601 case '0': case '1': case '2': case '3': | |
| 602 case '4': case '5': case '6': case '7': | |
| 603 c = s[-1] - '0'; | |
| 604 if ('0' <= *s && *s <= '7') { | |
| 605 c = (c<<3) + *s++ - '0'; | |
| 606 if ('0' <= *s && *s <= '7') | |
| 607 c = (c<<3) + *s++ - '0'; | |
| 608 } | |
| 609 *p++ = c; | |
| 610 break; | |
| 611 case 'x': | |
| 612 if (isxdigit(*s)) { | |
| 613 sscanf(s, "%x", &c); | |
| 614 *p++ = c; | |
| 615 do { | |
| 616 s++; | |
| 617 } while (isxdigit(*s)); | |
| 618 break; | |
| 619 } | |
| 620 /* FALLTHROUGH */ | |
| 621 default: *p++ = '\\'; *p++ = s[-1]; break; | |
| 622 } | |
| 623 } | |
| 624 resizestring(&v, (int)(p - buf)); | |
| 625 return v; | |
| 626 } | |
| 627 | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
628 static object * |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
629 parsestrplus(n) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
630 node *n; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
631 { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
632 object *v; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
633 int i; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
634 REQ(CHILD(n, 0), STRING); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
635 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
636 /* String literal concatenation */ |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
637 for (i = 1; i < NCH(n) && v != NULL; i++) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
638 joinstring_decref(&v, parsestr(STR(CHILD(n, i)))); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
639 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
640 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
641 return v; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
642 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
643 |
| 69 | 644 static void |
| 645 com_list_constructor(c, n) | |
| 646 struct compiling *c; | |
| 647 node *n; | |
| 648 { | |
| 649 int len; | |
| 650 int i; | |
| 651 if (TYPE(n) != testlist) | |
| 652 REQ(n, exprlist); | |
| 653 /* exprlist: expr (',' expr)* [',']; likewise for testlist */ | |
| 654 len = (NCH(n) + 1) / 2; | |
| 655 for (i = 0; i < NCH(n); i += 2) | |
| 656 com_node(c, CHILD(n, i)); | |
| 657 com_addoparg(c, BUILD_LIST, len); | |
| 658 } | |
| 659 | |
| 660 static void | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
661 com_dictmaker(c, n) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
662 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
663 node *n; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
664 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
665 int i; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
666 /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
667 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
|
668 /* 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
|
669 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
|
670 com_addbyte(c, DUP_TOP); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
671 com_node(c, CHILD(n, i+2)); /* value */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
672 com_addbyte(c, ROT_TWO); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
673 com_node(c, CHILD(n, i)); /* key */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
674 com_addbyte(c, STORE_SUBSCR); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
675 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
676 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
677 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
678 static void |
| 69 | 679 com_atom(c, n) |
| 680 struct compiling *c; | |
| 681 node *n; | |
| 682 { | |
| 683 node *ch; | |
| 684 object *v; | |
| 685 int i; | |
| 686 REQ(n, atom); | |
| 687 ch = CHILD(n, 0); | |
| 688 switch (TYPE(ch)) { | |
| 689 case LPAR: | |
| 690 if (TYPE(CHILD(n, 1)) == RPAR) | |
| 691 com_addoparg(c, BUILD_TUPLE, 0); | |
| 692 else | |
| 693 com_node(c, CHILD(n, 1)); | |
| 694 break; | |
| 695 case LSQB: | |
| 696 if (TYPE(CHILD(n, 1)) == RSQB) | |
| 697 com_addoparg(c, BUILD_LIST, 0); | |
| 698 else | |
| 699 com_list_constructor(c, CHILD(n, 1)); | |
| 700 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
701 case LBRACE: /* '{' [dictmaker] '}' */ |
| 69 | 702 com_addoparg(c, BUILD_MAP, 0); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
703 if (TYPE(CHILD(n, 1)) != RBRACE) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
704 com_dictmaker(c, CHILD(n, 1)); |
| 69 | 705 break; |
| 706 case BACKQUOTE: | |
| 707 com_node(c, CHILD(n, 1)); | |
| 708 com_addbyte(c, UNARY_CONVERT); | |
| 709 break; | |
| 710 case NUMBER: | |
| 711 if ((v = parsenumber(STR(ch))) == NULL) { | |
| 712 c->c_errors++; | |
| 713 i = 255; | |
| 714 } | |
| 715 else { | |
| 716 i = com_addconst(c, v); | |
| 717 DECREF(v); | |
| 718 } | |
| 719 com_addoparg(c, LOAD_CONST, i); | |
| 720 break; | |
| 721 case STRING: | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
722 v = parsestrplus(n); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
723 if (v == NULL) { |
| 69 | 724 c->c_errors++; |
| 725 i = 255; | |
| 726 } | |
| 727 else { | |
| 728 i = com_addconst(c, v); | |
| 729 DECREF(v); | |
| 730 } | |
| 731 com_addoparg(c, LOAD_CONST, i); | |
| 732 break; | |
| 733 case NAME: | |
| 734 com_addopname(c, LOAD_NAME, ch); | |
| 735 break; | |
| 736 default: | |
| 737 fprintf(stderr, "node type %d\n", TYPE(ch)); | |
| 738 err_setstr(SystemError, "com_atom: unexpected node type"); | |
| 739 c->c_errors++; | |
| 740 } | |
| 741 } | |
| 742 | |
| 743 static void | |
| 744 com_slice(c, n, op) | |
| 745 struct compiling *c; | |
| 746 node *n; | |
| 747 int op; | |
| 748 { | |
| 749 if (NCH(n) == 1) { | |
| 750 com_addbyte(c, op); | |
| 751 } | |
| 752 else if (NCH(n) == 2) { | |
| 753 if (TYPE(CHILD(n, 0)) != COLON) { | |
| 754 com_node(c, CHILD(n, 0)); | |
| 755 com_addbyte(c, op+1); | |
| 756 } | |
| 757 else { | |
| 758 com_node(c, CHILD(n, 1)); | |
| 759 com_addbyte(c, op+2); | |
| 760 } | |
| 761 } | |
| 762 else { | |
| 763 com_node(c, CHILD(n, 0)); | |
| 764 com_node(c, CHILD(n, 2)); | |
| 765 com_addbyte(c, op+3); | |
| 766 } | |
| 767 } | |
| 768 | |
| 769 static void | |
| 770 com_apply_subscript(c, n) | |
| 771 struct compiling *c; | |
| 772 node *n; | |
| 773 { | |
| 774 REQ(n, subscript); | |
| 775 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) { | |
| 776 /* It's a single subscript */ | |
| 777 com_node(c, CHILD(n, 0)); | |
| 778 com_addbyte(c, BINARY_SUBSCR); | |
| 779 } | |
| 780 else { | |
| 781 /* It's a slice: [expr] ':' [expr] */ | |
| 782 com_slice(c, n, SLICE); | |
| 783 } | |
| 784 } | |
| 785 | |
| 786 static void | |
| 787 com_call_function(c, n) | |
| 788 struct compiling *c; | |
| 789 node *n; /* EITHER testlist OR ')' */ | |
| 790 { | |
| 791 if (TYPE(n) == RPAR) { | |
| 482 | 792 com_addoparg(c, BUILD_TUPLE, 0); |
| 793 com_addbyte(c, BINARY_CALL); | |
| 69 | 794 } |
| 795 else { | |
| 482 | 796 REQ(n, testlist); |
| 797 com_list(c, n, 1); | |
| 69 | 798 com_addbyte(c, BINARY_CALL); |
| 799 } | |
| 800 } | |
| 801 | |
| 802 static void | |
| 803 com_select_member(c, n) | |
| 804 struct compiling *c; | |
| 805 node *n; | |
| 806 { | |
| 807 com_addopname(c, LOAD_ATTR, n); | |
| 808 } | |
| 809 | |
| 810 static void | |
| 811 com_apply_trailer(c, n) | |
| 812 struct compiling *c; | |
| 813 node *n; | |
| 814 { | |
| 815 REQ(n, trailer); | |
| 816 switch (TYPE(CHILD(n, 0))) { | |
| 817 case LPAR: | |
| 818 com_call_function(c, CHILD(n, 1)); | |
| 819 break; | |
| 820 case DOT: | |
| 821 com_select_member(c, CHILD(n, 1)); | |
| 822 break; | |
| 823 case LSQB: | |
| 824 com_apply_subscript(c, CHILD(n, 1)); | |
| 825 break; | |
| 826 default: | |
| 827 err_setstr(SystemError, | |
| 828 "com_apply_trailer: unknown trailer type"); | |
| 829 c->c_errors++; | |
| 830 } | |
| 831 } | |
| 832 | |
| 833 static void | |
| 834 com_factor(c, n) | |
| 835 struct compiling *c; | |
| 836 node *n; | |
| 837 { | |
| 838 int i; | |
| 839 REQ(n, factor); | |
| 840 if (TYPE(CHILD(n, 0)) == PLUS) { | |
| 841 com_factor(c, CHILD(n, 1)); | |
| 842 com_addbyte(c, UNARY_POSITIVE); | |
| 843 } | |
| 844 else if (TYPE(CHILD(n, 0)) == MINUS) { | |
| 845 com_factor(c, CHILD(n, 1)); | |
| 846 com_addbyte(c, UNARY_NEGATIVE); | |
| 847 } | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
848 else if (TYPE(CHILD(n, 0)) == TILDE) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
849 com_factor(c, CHILD(n, 1)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
850 com_addbyte(c, UNARY_INVERT); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
851 } |
| 69 | 852 else { |
| 853 com_atom(c, CHILD(n, 0)); | |
| 854 for (i = 1; i < NCH(n); i++) | |
| 855 com_apply_trailer(c, CHILD(n, i)); | |
| 856 } | |
| 857 } | |
| 858 | |
| 859 static void | |
| 860 com_term(c, n) | |
| 861 struct compiling *c; | |
| 862 node *n; | |
| 863 { | |
| 864 int i; | |
| 865 int op; | |
| 866 REQ(n, term); | |
| 867 com_factor(c, CHILD(n, 0)); | |
| 868 for (i = 2; i < NCH(n); i += 2) { | |
| 869 com_factor(c, CHILD(n, i)); | |
| 870 switch (TYPE(CHILD(n, i-1))) { | |
| 871 case STAR: | |
| 872 op = BINARY_MULTIPLY; | |
| 873 break; | |
| 874 case SLASH: | |
| 875 op = BINARY_DIVIDE; | |
| 876 break; | |
| 877 case PERCENT: | |
| 878 op = BINARY_MODULO; | |
| 879 break; | |
| 880 default: | |
| 881 err_setstr(SystemError, | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
882 "com_term: operator not *, / or %"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
883 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
884 op = 255; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
885 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
886 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
887 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
888 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
889 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
890 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
891 com_arith_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
892 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
893 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
894 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
895 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
896 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
897 REQ(n, arith_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
898 com_term(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
899 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
900 com_term(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
901 switch (TYPE(CHILD(n, i-1))) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
902 case PLUS: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
903 op = BINARY_ADD; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
904 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
905 case MINUS: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
906 op = BINARY_SUBTRACT; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
907 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
908 default: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
909 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
910 "com_arith_expr: operator not + or -"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
911 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
912 op = 255; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
913 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
914 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
915 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
916 } |
|
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 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
919 com_shift_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
920 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
921 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
922 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
923 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
924 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
925 REQ(n, shift_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
926 com_arith_expr(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
927 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
928 com_arith_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
929 switch (TYPE(CHILD(n, i-1))) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
930 case LEFTSHIFT: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
931 op = BINARY_LSHIFT; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
932 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
933 case RIGHTSHIFT: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
934 op = BINARY_RSHIFT; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
935 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
936 default: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
937 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
938 "com_shift_expr: operator not << or >>"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
939 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
940 op = 255; |
|
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 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
943 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
944 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
945 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
946 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
947 com_and_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
948 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
949 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
950 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
951 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
952 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
953 REQ(n, and_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
954 com_shift_expr(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
955 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
956 com_shift_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
957 if (TYPE(CHILD(n, i-1)) == AMPER) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
958 op = BINARY_AND; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
959 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
960 else { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
961 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
962 "com_and_expr: operator not &"); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
963 c->c_errors++; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
964 op = 255; |
|
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 com_addbyte(c, op); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
967 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
968 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
969 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
970 static void |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
971 com_xor_expr(c, n) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
972 struct compiling *c; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
973 node *n; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
974 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
975 int i; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
976 int op; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
977 REQ(n, xor_expr); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
978 com_and_expr(c, CHILD(n, 0)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
979 for (i = 2; i < NCH(n); i += 2) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
980 com_and_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
981 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
982 op = BINARY_XOR; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
983 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
984 else { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
985 err_setstr(SystemError, |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
986 "com_xor_expr: operator not ^"); |
| 69 | 987 c->c_errors++; |
| 988 op = 255; | |
| 989 } | |
| 990 com_addbyte(c, op); | |
| 991 } | |
| 992 } | |
| 993 | |
| 994 static void | |
| 995 com_expr(c, n) | |
| 996 struct compiling *c; | |
| 997 node *n; | |
| 998 { | |
| 999 int i; | |
| 1000 int op; | |
| 1001 REQ(n, expr); | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1002 com_xor_expr(c, CHILD(n, 0)); |
| 69 | 1003 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
|
1004 com_xor_expr(c, CHILD(n, i)); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1005 if (TYPE(CHILD(n, i-1)) == VBAR) { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1006 op = BINARY_OR; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1007 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1008 else { |
| 69 | 1009 err_setstr(SystemError, |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1010 "com_expr: expr operator not |"); |
| 69 | 1011 c->c_errors++; |
| 1012 op = 255; | |
| 1013 } | |
| 1014 com_addbyte(c, op); | |
| 1015 } | |
| 1016 } | |
| 1017 | |
| 1018 static enum cmp_op | |
| 1019 cmp_type(n) | |
| 1020 node *n; | |
| 1021 { | |
| 1022 REQ(n, comp_op); | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
1023 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
| 69 | 1024 | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 1025 if (NCH(n) == 1) { | |
| 1026 n = CHILD(n, 0); | |
| 1027 switch (TYPE(n)) { | |
| 1028 case LESS: return LT; | |
| 1029 case GREATER: return GT; | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
1030 case EQEQUAL: /* == */ |
| 69 | 1031 case EQUAL: return EQ; |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
1032 case LESSEQUAL: return LE; |
|
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
1033 case GREATEREQUAL: return GE; |
|
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
1034 case NOTEQUAL: return NE; /* <> or != */ |
| 69 | 1035 case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 1036 if (strcmp(STR(n), "is") == 0) return IS; | |
| 1037 } | |
| 1038 } | |
| 1039 else if (NCH(n) == 2) { | |
| 1040 switch (TYPE(CHILD(n, 0))) { | |
| 1041 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 1042 return NOT_IN; | |
| 1043 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 1044 return IS_NOT; | |
| 1045 } | |
| 1046 } | |
| 1047 return BAD; | |
| 1048 } | |
| 1049 | |
| 1050 static void | |
| 1051 com_comparison(c, n) | |
| 1052 struct compiling *c; | |
| 1053 node *n; | |
| 1054 { | |
| 1055 int i; | |
| 1056 enum cmp_op op; | |
| 1057 int anchor; | |
| 1058 REQ(n, comparison); /* comparison: expr (comp_op expr)* */ | |
| 1059 com_expr(c, CHILD(n, 0)); | |
| 1060 if (NCH(n) == 1) | |
| 1061 return; | |
| 1062 | |
| 1063 /**************************************************************** | |
| 1064 The following code is generated for all but the last | |
| 1065 comparison in a chain: | |
| 1066 | |
| 1067 label: on stack: opcode: jump to: | |
| 1068 | |
| 1069 a <code to load b> | |
| 1070 a, b DUP_TOP | |
| 1071 a, b, b ROT_THREE | |
| 1072 b, a, b COMPARE_OP | |
| 1073 b, 0-or-1 JUMP_IF_FALSE L1 | |
| 1074 b, 1 POP_TOP | |
| 1075 b | |
| 1076 | |
| 1077 We are now ready to repeat this sequence for the next | |
| 1078 comparison in the chain. | |
| 1079 | |
| 1080 For the last we generate: | |
| 1081 | |
| 1082 b <code to load c> | |
| 1083 b, c COMPARE_OP | |
| 1084 0-or-1 | |
| 1085 | |
| 1086 If there were any jumps to L1 (i.e., there was more than one | |
| 1087 comparison), we generate: | |
| 1088 | |
| 1089 0-or-1 JUMP_FORWARD L2 | |
| 1090 L1: b, 0 ROT_TWO | |
| 1091 0, b POP_TOP | |
| 1092 0 | |
| 1093 L2: | |
| 1094 ****************************************************************/ | |
| 1095 | |
| 1096 anchor = 0; | |
| 1097 | |
| 1098 for (i = 2; i < NCH(n); i += 2) { | |
| 1099 com_expr(c, CHILD(n, i)); | |
| 1100 if (i+2 < NCH(n)) { | |
| 1101 com_addbyte(c, DUP_TOP); | |
| 1102 com_addbyte(c, ROT_THREE); | |
| 1103 } | |
| 1104 op = cmp_type(CHILD(n, i-1)); | |
| 1105 if (op == BAD) { | |
| 1106 err_setstr(SystemError, | |
| 1107 "com_comparison: unknown comparison op"); | |
| 1108 c->c_errors++; | |
| 1109 } | |
| 1110 com_addoparg(c, COMPARE_OP, op); | |
| 1111 if (i+2 < NCH(n)) { | |
| 1112 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1113 com_addbyte(c, POP_TOP); | |
| 1114 } | |
| 1115 } | |
| 1116 | |
| 1117 if (anchor) { | |
| 1118 int anchor2 = 0; | |
| 1119 com_addfwref(c, JUMP_FORWARD, &anchor2); | |
| 1120 com_backpatch(c, anchor); | |
| 1121 com_addbyte(c, ROT_TWO); | |
| 1122 com_addbyte(c, POP_TOP); | |
| 1123 com_backpatch(c, anchor2); | |
| 1124 } | |
| 1125 } | |
| 1126 | |
| 1127 static void | |
| 1128 com_not_test(c, n) | |
| 1129 struct compiling *c; | |
| 1130 node *n; | |
| 1131 { | |
| 1132 REQ(n, not_test); /* 'not' not_test | comparison */ | |
| 1133 if (NCH(n) == 1) { | |
| 1134 com_comparison(c, CHILD(n, 0)); | |
| 1135 } | |
| 1136 else { | |
| 1137 com_not_test(c, CHILD(n, 1)); | |
| 1138 com_addbyte(c, UNARY_NOT); | |
| 1139 } | |
| 1140 } | |
| 1141 | |
| 1142 static void | |
| 1143 com_and_test(c, n) | |
| 1144 struct compiling *c; | |
| 1145 node *n; | |
| 1146 { | |
| 1147 int i; | |
| 1148 int anchor; | |
| 1149 REQ(n, and_test); /* not_test ('and' not_test)* */ | |
| 1150 anchor = 0; | |
| 1151 i = 0; | |
| 1152 for (;;) { | |
| 1153 com_not_test(c, CHILD(n, i)); | |
| 1154 if ((i += 2) >= NCH(n)) | |
| 1155 break; | |
| 1156 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1157 com_addbyte(c, POP_TOP); | |
| 1158 } | |
| 1159 if (anchor) | |
| 1160 com_backpatch(c, anchor); | |
| 1161 } | |
| 1162 | |
| 1163 static void | |
| 1164 com_test(c, n) | |
| 1165 struct compiling *c; | |
| 1166 node *n; | |
| 1167 { | |
|
1417
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1168 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
|
1169 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
|
1170 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
|
1171 int i; |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1172 int argcount; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1173 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
|
1174 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
|
1175 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
|
1176 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
|
1177 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
|
1178 } |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1179 else { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1180 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
|
1181 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
|
1182 } |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1183 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
|
1184 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
|
1185 if (ndefs > 0) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1186 com_addoparg(c, SET_FUNC_ARGS, argcount); |
| 69 | 1187 } |
|
1417
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1188 else { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1189 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
|
1190 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
|
1191 for (;;) { |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1192 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
|
1193 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
|
1194 break; |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1195 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
|
1196 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
|
1197 } |
|
f7edfa6d0dcb
change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
1198 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
|
1199 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
|
1200 } |
| 69 | 1201 } |
| 1202 | |
| 1203 static void | |
| 482 | 1204 com_list(c, n, toplevel) |
| 69 | 1205 struct compiling *c; |
| 1206 node *n; | |
| 482 | 1207 int toplevel; /* If nonzero, *always* build a tuple */ |
| 69 | 1208 { |
| 1209 /* exprlist: expr (',' expr)* [',']; likewise for testlist */ | |
| 482 | 1210 if (NCH(n) == 1 && !toplevel) { |
| 69 | 1211 com_node(c, CHILD(n, 0)); |
| 1212 } | |
| 1213 else { | |
| 1214 int i; | |
| 1215 int len; | |
| 1216 len = (NCH(n) + 1) / 2; | |
| 1217 for (i = 0; i < NCH(n); i += 2) | |
| 1218 com_node(c, CHILD(n, i)); | |
| 1219 com_addoparg(c, BUILD_TUPLE, len); | |
| 1220 } | |
| 1221 } | |
| 1222 | |
| 1223 | |
| 1224 /* Begin of assignment compilation */ | |
| 1225 | |
| 1226 static void com_assign_name PROTO((struct compiling *, node *, int)); | |
| 1227 static void com_assign PROTO((struct compiling *, node *, int)); | |
| 1228 | |
| 1229 static void | |
| 1230 com_assign_attr(c, n, assigning) | |
| 1231 struct compiling *c; | |
| 1232 node *n; | |
| 1233 int assigning; | |
| 1234 { | |
| 1235 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); | |
| 1236 } | |
| 1237 | |
| 1238 static void | |
| 1239 com_assign_slice(c, n, assigning) | |
| 1240 struct compiling *c; | |
| 1241 node *n; | |
| 1242 int assigning; | |
| 1243 { | |
| 1244 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE); | |
| 1245 } | |
| 1246 | |
| 1247 static void | |
| 1248 com_assign_subscript(c, n, assigning) | |
| 1249 struct compiling *c; | |
| 1250 node *n; | |
| 1251 int assigning; | |
| 1252 { | |
| 1253 com_node(c, n); | |
| 1254 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR); | |
| 1255 } | |
| 1256 | |
| 1257 static void | |
| 1258 com_assign_trailer(c, n, assigning) | |
| 1259 struct compiling *c; | |
| 1260 node *n; | |
| 1261 int assigning; | |
| 1262 { | |
| 1263 REQ(n, trailer); | |
| 1264 switch (TYPE(CHILD(n, 0))) { | |
| 1265 case LPAR: /* '(' [exprlist] ')' */ | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1266 err_setstr(SyntaxError, "can't assign to function call"); |
| 69 | 1267 c->c_errors++; |
| 1268 break; | |
| 1269 case DOT: /* '.' NAME */ | |
| 1270 com_assign_attr(c, CHILD(n, 1), assigning); | |
| 1271 break; | |
| 1272 case LSQB: /* '[' subscript ']' */ | |
| 1273 n = CHILD(n, 1); | |
| 1274 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */ | |
| 1275 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON) | |
| 1276 com_assign_slice(c, n, assigning); | |
| 1277 else | |
| 1278 com_assign_subscript(c, CHILD(n, 0), assigning); | |
| 1279 break; | |
| 1280 default: | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1281 err_setstr(SystemError, "unknown trailer type"); |
| 69 | 1282 c->c_errors++; |
| 1283 } | |
| 1284 } | |
| 1285 | |
| 1286 static void | |
| 1287 com_assign_tuple(c, n, assigning) | |
| 1288 struct compiling *c; | |
| 1289 node *n; | |
| 1290 int assigning; | |
| 1291 { | |
| 1292 int i; | |
| 1293 if (TYPE(n) != testlist) | |
| 1294 REQ(n, exprlist); | |
| 1295 if (assigning) | |
| 1296 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); | |
| 1297 for (i = 0; i < NCH(n); i += 2) | |
| 1298 com_assign(c, CHILD(n, i), assigning); | |
| 1299 } | |
| 1300 | |
| 1301 static void | |
| 1302 com_assign_list(c, n, assigning) | |
| 1303 struct compiling *c; | |
| 1304 node *n; | |
| 1305 int assigning; | |
| 1306 { | |
| 1307 int i; | |
| 1308 if (assigning) | |
| 1309 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2); | |
| 1310 for (i = 0; i < NCH(n); i += 2) | |
| 1311 com_assign(c, CHILD(n, i), assigning); | |
| 1312 } | |
| 1313 | |
| 1314 static void | |
| 1315 com_assign_name(c, n, assigning) | |
| 1316 struct compiling *c; | |
| 1317 node *n; | |
| 1318 int assigning; | |
| 1319 { | |
| 1320 REQ(n, NAME); | |
| 1321 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); | |
| 1322 } | |
| 1323 | |
| 1324 static void | |
| 1325 com_assign(c, n, assigning) | |
| 1326 struct compiling *c; | |
| 1327 node *n; | |
| 1328 int assigning; | |
| 1329 { | |
| 1330 /* Loop to avoid trivial recursion */ | |
| 1331 for (;;) { | |
| 1332 switch (TYPE(n)) { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1333 |
| 69 | 1334 case exprlist: |
| 1335 case testlist: | |
| 1336 if (NCH(n) > 1) { | |
| 1337 com_assign_tuple(c, n, assigning); | |
| 1338 return; | |
| 1339 } | |
| 1340 n = CHILD(n, 0); | |
| 1341 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1342 |
| 69 | 1343 case test: |
| 1344 case and_test: | |
| 1345 case not_test: | |
| 1346 case comparison: | |
| 1347 case expr: | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1348 case xor_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1349 case and_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1350 case shift_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1351 case arith_expr: |
| 69 | 1352 case term: |
| 1353 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
|
1354 err_setstr(SyntaxError, |
| 69 | 1355 "can't assign to operator"); |
| 1356 c->c_errors++; | |
| 1357 return; | |
| 1358 } | |
| 1359 n = CHILD(n, 0); | |
| 1360 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1361 |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1362 case factor: /* ('+'|'-'|'~') factor | atom trailer* */ |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
1363 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
|
1364 err_setstr(SyntaxError, |
| 69 | 1365 "can't assign to operator"); |
| 1366 c->c_errors++; | |
| 1367 return; | |
| 1368 } | |
| 1369 if (NCH(n) > 1) { /* trailer present */ | |
| 1370 int i; | |
| 1371 com_node(c, CHILD(n, 0)); | |
| 1372 for (i = 1; i+1 < NCH(n); i++) { | |
| 1373 com_apply_trailer(c, CHILD(n, i)); | |
| 1374 } /* NB i is still alive */ | |
| 1375 com_assign_trailer(c, | |
| 1376 CHILD(n, i), assigning); | |
| 1377 return; | |
| 1378 } | |
| 1379 n = CHILD(n, 0); | |
| 1380 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1381 |
| 69 | 1382 case atom: |
| 1383 switch (TYPE(CHILD(n, 0))) { | |
| 1384 case LPAR: | |
| 1385 n = CHILD(n, 1); | |
| 1386 if (TYPE(n) == RPAR) { | |
| 1387 /* XXX Should allow () = () ??? */ | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1388 err_setstr(SyntaxError, |
| 69 | 1389 "can't assign to ()"); |
| 1390 c->c_errors++; | |
| 1391 return; | |
| 1392 } | |
| 1393 break; | |
| 1394 case LSQB: | |
| 1395 n = CHILD(n, 1); | |
| 1396 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
|
1397 err_setstr(SyntaxError, |
| 69 | 1398 "can't assign to []"); |
| 1399 c->c_errors++; | |
| 1400 return; | |
| 1401 } | |
| 1402 com_assign_list(c, n, assigning); | |
| 1403 return; | |
| 1404 case NAME: | |
| 1405 com_assign_name(c, CHILD(n, 0), assigning); | |
| 1406 return; | |
| 1407 default: | |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1408 err_setstr(SyntaxError, |
| 544 | 1409 "can't assign to literal"); |
| 69 | 1410 c->c_errors++; |
| 1411 return; | |
| 1412 } | |
| 1413 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1414 |
| 69 | 1415 default: |
| 1416 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 1417 err_setstr(SystemError, "com_assign: bad node"); | |
| 1418 c->c_errors++; | |
| 1419 return; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1420 |
| 69 | 1421 } |
| 1422 } | |
| 1423 } | |
| 1424 | |
| 1425 static void | |
| 1426 com_expr_stmt(c, n) | |
| 1427 struct compiling *c; | |
| 1428 node *n; | |
| 1429 { | |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1430 REQ(n, expr_stmt); /* testlist ('=' testlist)* */ |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1431 com_node(c, CHILD(n, NCH(n)-1)); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1432 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
|
1433 if (c->c_interactive) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1434 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
|
1435 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1436 com_addbyte(c, POP_TOP); |
| 69 | 1437 } |
| 1438 else { | |
| 1439 int i; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1440 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
|
1441 if (i+2 < NCH(n)-2) |
| 69 | 1442 com_addbyte(c, DUP_TOP); |
| 1443 com_assign(c, CHILD(n, i), 1/*assign*/); | |
| 1444 } | |
| 1445 } | |
| 1446 } | |
| 1447 | |
| 1448 static void | |
| 1449 com_print_stmt(c, n) | |
| 1450 struct compiling *c; | |
| 1451 node *n; | |
| 1452 { | |
| 1453 int i; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1454 REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1455 for (i = 1; i < NCH(n); i += 2) { |
| 69 | 1456 com_node(c, CHILD(n, i)); |
| 1457 com_addbyte(c, PRINT_ITEM); | |
| 1458 } | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1459 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
| 69 | 1460 com_addbyte(c, PRINT_NEWLINE); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1461 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
| 69 | 1462 } |
| 1463 | |
| 1464 static void | |
| 1465 com_return_stmt(c, n) | |
| 1466 struct compiling *c; | |
| 1467 node *n; | |
| 1468 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1469 REQ(n, return_stmt); /* 'return' [testlist] */ |
| 86 | 1470 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
|
1471 err_setstr(SyntaxError, "'return' outside function"); |
| 86 | 1472 c->c_errors++; |
| 1473 } | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1474 if (NCH(n) < 2) |
| 69 | 1475 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1476 else | |
| 1477 com_node(c, CHILD(n, 1)); | |
| 1478 com_addbyte(c, RETURN_VALUE); | |
| 1479 } | |
| 1480 | |
| 1481 static void | |
| 1482 com_raise_stmt(c, n) | |
| 1483 struct compiling *c; | |
| 1484 node *n; | |
| 1485 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1486 REQ(n, raise_stmt); /* 'raise' test [',' test] */ |
| 69 | 1487 com_node(c, CHILD(n, 1)); |
| 1488 if (NCH(n) > 3) | |
| 1489 com_node(c, CHILD(n, 3)); | |
| 1490 else | |
| 1491 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); | |
| 1492 com_addbyte(c, RAISE_EXCEPTION); | |
| 1493 } | |
| 1494 | |
| 1495 static void | |
| 1496 com_import_stmt(c, n) | |
| 1497 struct compiling *c; | |
| 1498 node *n; | |
| 1499 { | |
| 1500 int i; | |
| 1501 REQ(n, import_stmt); | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1502 /* 'import' dotted_name (',' dotted_name)* | |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1503 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
| 69 | 1504 if (STR(CHILD(n, 0))[0] == 'f') { |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1505 /* 'from' dotted_name 'import' ... */ |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1506 REQ(CHILD(n, 1), dotted_name); |
| 69 | 1507 com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
| 1508 for (i = 3; i < NCH(n); i += 2) | |
| 1509 com_addopname(c, IMPORT_FROM, CHILD(n, i)); | |
| 1510 com_addbyte(c, POP_TOP); | |
| 1511 } | |
| 1512 else { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1513 /* 'import' ... */ |
| 69 | 1514 for (i = 1; i < NCH(n); i += 2) { |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1515 REQ(CHILD(n, i), dotted_name); |
| 69 | 1516 com_addopname(c, IMPORT_NAME, CHILD(n, i)); |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1517 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0)); |
| 69 | 1518 } |
| 1519 } | |
| 1520 } | |
| 1521 | |
| 1522 static void | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1523 com_global_stmt(c, n) |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1524 struct compiling *c; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1525 node *n; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1526 { |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1527 int i; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1528 REQ(n, global_stmt); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1529 /* 'global' NAME (',' NAME)* */ |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1530 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
|
1531 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
|
1532 c->c_errors++; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1533 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1534 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1535 |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1536 #define strequ(a, b) (strcmp((a), (b)) == 0) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1537 |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1538 static void |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1539 com_access_stmt(c, n) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1540 struct compiling *c; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1541 node *n; |
|
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 int i, j, k, mode, imode; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1544 object *vmode; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1545 REQ(n, access_stmt); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1546 /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1547 accesstype: NAME+ */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1548 |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1549 /* Find where the colon is */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1550 i = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1551 while (TYPE(CHILD(n,i-1)) != COLON) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1552 i += 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1553 |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1554 /* Calculate the mode mask */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1555 mode = 0; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1556 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
|
1557 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
|
1558 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
|
1559 if (strequ(STR(CHILD(CHILD(n,j),k)), "public")) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1560 p = 0; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1561 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
|
1562 p = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1563 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
|
1564 p = 2; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1565 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
|
1566 r = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1567 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
|
1568 w = 1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1569 else /* XXX should make this an exception */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1570 fprintf(stderr, "bad access type %s\n", |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1571 STR(CHILD(CHILD(n,j),k))); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1572 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1573 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
|
1574 r = w = 1; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1575 if (p == 0) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1576 if (r == 1) mode |= AC_R_PUBLIC; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1577 if (w == 1) mode |= AC_W_PUBLIC; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1578 } else if (p == 1) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1579 if (r == 1) mode |= AC_R_PROTECTED; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1580 if (w == 1) mode |= AC_W_PROTECTED; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1581 } else { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1582 if (r == 1) mode |= AC_R_PRIVATE; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1583 if (w == 1) mode |= AC_W_PRIVATE; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1584 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1585 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1586 vmode = newintobject((long)mode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1587 imode = com_addconst(c, vmode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1588 XDECREF(vmode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1589 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
|
1590 com_addoparg(c, LOAD_CONST, imode); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1591 com_addopname(c, ACCESS_MODE, CHILD(n, i)); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1592 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1593 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
1594 |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
1595 static void |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1596 com_exec_stmt(c, n) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1597 struct compiling *c; |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1598 node *n; |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1599 { |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1600 REQ(n, exec_stmt); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1601 /* 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
|
1602 com_node(c, CHILD(n, 1)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1603 if (NCH(n) >= 4) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1604 com_node(c, CHILD(n, 3)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1605 else |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1606 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
|
1607 if (NCH(n) >= 6) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1608 com_node(c, CHILD(n, 5)); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1609 else |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1610 com_addbyte(c, DUP_TOP); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1611 com_addbyte(c, EXEC_STMT); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1612 } |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1613 |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
1614 static void |
| 69 | 1615 com_if_stmt(c, n) |
| 1616 struct compiling *c; | |
| 1617 node *n; | |
| 1618 { | |
| 1619 int i; | |
| 1620 int anchor = 0; | |
| 1621 REQ(n, if_stmt); | |
| 1622 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ | |
| 1623 for (i = 0; i+3 < NCH(n); i+=4) { | |
| 1624 int a = 0; | |
| 86 | 1625 node *ch = CHILD(n, i+1); |
| 1626 if (i > 0) | |
| 1627 com_addoparg(c, SET_LINENO, ch->n_lineno); | |
| 69 | 1628 com_node(c, CHILD(n, i+1)); |
| 1629 com_addfwref(c, JUMP_IF_FALSE, &a); | |
| 1630 com_addbyte(c, POP_TOP); | |
| 1631 com_node(c, CHILD(n, i+3)); | |
| 1632 com_addfwref(c, JUMP_FORWARD, &anchor); | |
| 1633 com_backpatch(c, a); | |
| 1634 com_addbyte(c, POP_TOP); | |
| 1635 } | |
| 1636 if (i+2 < NCH(n)) | |
| 1637 com_node(c, CHILD(n, i+2)); | |
| 1638 com_backpatch(c, anchor); | |
| 1639 } | |
| 1640 | |
| 1641 static void | |
| 1642 com_while_stmt(c, n) | |
| 1643 struct compiling *c; | |
| 1644 node *n; | |
| 1645 { | |
| 1646 int break_anchor = 0; | |
| 1647 int anchor = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1648 int save_begin = c->c_begin; |
| 69 | 1649 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1650 com_addfwref(c, SETUP_LOOP, &break_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1651 block_push(c, SETUP_LOOP); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1652 c->c_begin = c->c_nexti; |
| 86 | 1653 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1654 com_node(c, CHILD(n, 1)); |
| 1655 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1656 com_addbyte(c, POP_TOP); | |
| 86 | 1657 c->c_loops++; |
| 69 | 1658 com_node(c, CHILD(n, 3)); |
| 86 | 1659 c->c_loops--; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1660 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1661 c->c_begin = save_begin; |
| 69 | 1662 com_backpatch(c, anchor); |
| 1663 com_addbyte(c, POP_TOP); | |
| 1664 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1665 block_pop(c, SETUP_LOOP); |
| 69 | 1666 if (NCH(n) > 4) |
| 1667 com_node(c, CHILD(n, 6)); | |
| 1668 com_backpatch(c, break_anchor); | |
| 1669 } | |
| 1670 | |
| 1671 static void | |
| 1672 com_for_stmt(c, n) | |
| 1673 struct compiling *c; | |
| 1674 node *n; | |
| 1675 { | |
| 1676 object *v; | |
| 1677 int break_anchor = 0; | |
| 1678 int anchor = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1679 int save_begin = c->c_begin; |
| 69 | 1680 REQ(n, for_stmt); |
| 1681 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ | |
| 1682 com_addfwref(c, SETUP_LOOP, &break_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1683 block_push(c, SETUP_LOOP); |
| 69 | 1684 com_node(c, CHILD(n, 3)); |
| 1685 v = newintobject(0L); | |
| 1686 if (v == NULL) | |
| 1687 c->c_errors++; | |
| 1688 com_addoparg(c, LOAD_CONST, com_addconst(c, v)); | |
| 1689 XDECREF(v); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1690 c->c_begin = c->c_nexti; |
| 86 | 1691 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1692 com_addfwref(c, FOR_LOOP, &anchor); |
| 1693 com_assign(c, CHILD(n, 1), 1/*assigning*/); | |
| 86 | 1694 c->c_loops++; |
| 69 | 1695 com_node(c, CHILD(n, 5)); |
| 86 | 1696 c->c_loops--; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1697 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1698 c->c_begin = save_begin; |
| 69 | 1699 com_backpatch(c, anchor); |
| 1700 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1701 block_pop(c, SETUP_LOOP); |
| 69 | 1702 if (NCH(n) > 8) |
| 1703 com_node(c, CHILD(n, 8)); | |
| 1704 com_backpatch(c, break_anchor); | |
| 1705 } | |
| 1706 | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1707 /* 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
|
1708 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1709 SETUP_FINALLY L |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1710 <code for S> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1711 POP_BLOCK |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1712 LOAD_CONST <nil> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1713 L: <code for Sf> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1714 END_FINALLY |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1715 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1716 The special instructions use the block stack. Each block |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1717 stack entry contains the instruction that created it (here |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1718 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
|
1719 block stack entry was created, and a label (here L). |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1720 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1721 SETUP_FINALLY: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1722 Pushes the current value stack level and the label |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1723 onto the block stack. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1724 POP_BLOCK: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1725 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
|
1726 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
|
1727 block stack. (The label is ignored.) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1728 END_FINALLY: |
| 86 | 1729 Pops a variable number of entries from the *value* stack |
| 1730 and re-raises the exception they specify. The number of | |
| 1731 entries popped depends on the (pseudo) exception type. | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1732 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1733 The block stack is unwound when an exception is raised: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1734 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
|
1735 onto the value stack (and the exception condition is cleared), |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1736 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
|
1737 stack. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1738 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1739 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
| 86 | 1740 (The contents of the value stack is shown in [], with the top |
| 1741 at the right; 'tb' is trace-back info, 'val' the exception's | |
| 1742 associated value, and 'exc' the exception.) | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1743 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1744 Value stack Label Instruction Argument |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1745 [] SETUP_EXCEPT L1 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1746 [] <code for S> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1747 [] POP_BLOCK |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1748 [] JUMP_FORWARD L0 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1749 |
| 86 | 1750 [tb, val, exc] L1: DUP ) |
| 1751 [tb, val, exc, exc] <evaluate E1> ) | |
| 1752 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 | |
| 1753 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) | |
| 1754 [tb, val, exc, 1] POP ) | |
| 1755 [tb, val, exc] POP | |
| 1756 [tb, val] <assign to V1> (or POP if no V1) | |
| 1757 [tb] POP | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1758 [] <code for S1> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1759 JUMP_FORWARD L0 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1760 |
| 86 | 1761 [tb, val, exc, 0] L2: POP |
| 1762 [tb, val, exc] DUP | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1763 .............................etc....................... |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1764 |
| 86 | 1765 [tb, val, exc, 0] Ln+1: POP |
| 1766 [tb, val, exc] END_FINALLY # re-raise exception | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1767 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1768 [] L0: <next statement> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1769 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1770 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
|
1771 */ |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1772 |
| 69 | 1773 static void |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1774 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
|
1775 struct compiling *c; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1776 node *n; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1777 { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1778 int except_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1779 int end_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1780 int else_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1781 int i; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1782 node *ch; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1783 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1784 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
|
1785 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
|
1786 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
|
1787 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
|
1788 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
|
1789 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
|
1790 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
|
1791 for (i = 3; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1792 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
|
1793 i += 3) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1794 /* 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
|
1795 if (except_anchor == 0) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1796 err_setstr(SyntaxError, |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1797 "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
|
1798 c->c_errors++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1799 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1800 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1801 except_anchor = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1802 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
|
1803 if (NCH(ch) > 1) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1804 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
|
1805 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
|
1806 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
|
1807 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
|
1808 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
|
1809 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1810 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
|
1811 if (NCH(ch) > 3) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1812 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
|
1813 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1814 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
|
1815 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
|
1816 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
|
1817 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
|
1818 if (except_anchor) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1819 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
|
1820 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
|
1821 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1822 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1823 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
|
1824 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
|
1825 if (i < NCH(n)) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1826 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
|
1827 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
|
1828 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1829 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1830 static void |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1831 com_try_finally(c, n) |
| 69 | 1832 struct compiling *c; |
| 1833 node *n; | |
| 1834 { | |
| 1835 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
|
1836 node *ch; |
|
792
dc3b455f7632
Only * can be used for varargs argument lists
Guido van Rossum <guido@python.org>
parents:
744
diff
changeset
|
1837 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1838 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
|
1839 block_push(c, SETUP_FINALLY); |
| 69 | 1840 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
|
1841 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
|
1842 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
|
1843 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
|
1844 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
|
1845 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
|
1846 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
|
1847 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
|
1848 com_node(c, ch); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1849 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
|
1850 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
|
1851 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1852 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1853 static void |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1854 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
|
1855 struct compiling *c; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1856 node *n; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1857 { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1858 REQ(n, try_stmt); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1859 /* '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
|
1860 | 'try' ':' suite 'finally' ':' suite */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1861 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
|
1862 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
|
1863 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1864 com_try_except(c, n); |
| 69 | 1865 } |
| 1866 | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1867 static object * |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1868 get_docstring(n) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1869 node *n; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1870 { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1871 switch (TYPE(n)) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1872 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1873 case suite: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1874 if (NCH(n) == 1) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1875 return get_docstring(CHILD(n, 0)); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1876 else { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1877 int i; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1878 for (i = 0; i < NCH(n); i++) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1879 node *ch = CHILD(n, i); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1880 if (TYPE(ch) == stmt) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1881 return get_docstring(ch); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1882 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1883 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1884 break; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1885 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1886 case stmt: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1887 case simple_stmt: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1888 case small_stmt: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1889 return get_docstring(CHILD(n, 0)); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1890 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1891 case expr_stmt: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1892 case testlist: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1893 case test: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1894 case and_test: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1895 case not_test: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1896 case comparison: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1897 case expr: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1898 case xor_expr: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1899 case and_expr: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1900 case shift_expr: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1901 case arith_expr: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1902 case term: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1903 case factor: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1904 if (NCH(n) == 1) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1905 return get_docstring(CHILD(n, 0)); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1906 break; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1907 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1908 case atom: |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1909 if (TYPE(CHILD(n, 0)) == STRING) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1910 return parsestrplus(n); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1911 break; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1912 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1913 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1914 return NULL; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1915 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
1916 |
| 69 | 1917 static void |
| 1918 com_suite(c, n) | |
| 1919 struct compiling *c; | |
| 1920 node *n; | |
| 1921 { | |
| 1922 REQ(n, suite); | |
| 1923 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ | |
| 1924 if (NCH(n) == 1) { | |
| 1925 com_node(c, CHILD(n, 0)); | |
| 1926 } | |
| 1927 else { | |
| 1928 int i; | |
| 1929 for (i = 0; i < NCH(n); i++) { | |
| 1930 node *ch = CHILD(n, i); | |
| 1931 if (TYPE(ch) == stmt) | |
| 1932 com_node(c, ch); | |
| 1933 } | |
| 1934 } | |
| 1935 } | |
| 1936 | |
| 744 | 1937 /* ARGSUSED */ |
| 69 | 1938 static void |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1939 com_continue_stmt(c, n) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1940 struct compiling *c; |
| 744 | 1941 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
|
1942 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1943 int i = c->c_nblocks; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1944 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
|
1945 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1946 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1947 else { |
|
627
1d65f28c0aa9
Raise SyntaxError for syntax errors detected in this phase.
Guido van Rossum <guido@python.org>
parents:
617
diff
changeset
|
1948 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
|
1949 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1950 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1951 /* XXX Could allow it inside a 'finally' clause |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1952 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
|
1953 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1954 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1955 static int |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1956 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
|
1957 struct compiling *c; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1958 node *n; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1959 int *argcount_return; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1960 { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1961 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
|
1962 if (TYPE(n) == lambdef) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1963 /* lambdef: 'lambda' [varargslist] ':' test */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1964 n = CHILD(n, 1); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1965 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1966 else { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1967 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
|
1968 n = CHILD(n, 2); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1969 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
|
1970 n = CHILD(n, 1); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1971 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1972 if (TYPE(n) != varargslist) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1973 return -1; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1974 /* varargslist: |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1975 (fpdef ['=' test] ',')* '*' NAME | |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1976 fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1977 nch = NCH(n); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1978 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
|
1979 star = 1; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1980 nch -= 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1981 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1982 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1983 star = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1984 nargs = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1985 ndefs = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1986 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
|
1987 int t; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1988 nargs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1989 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1990 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1991 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1992 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
|
1993 if (t == EQUAL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1994 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1995 ndefs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1996 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
|
1997 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1998 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
1999 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2000 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
|
2001 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2002 else { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2003 /* 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
|
2004 if (ndefs) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2005 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
|
2006 com_addconst(c, None)); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2007 ndefs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2008 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2009 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2010 if (t != COMMA) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2011 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2012 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2013 if (star) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2014 nargs ^= 0x4000; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2015 *argcount_return = nargs; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2016 if (ndefs > 0) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2017 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
|
2018 return ndefs; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2019 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2020 |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2021 static void |
| 69 | 2022 com_funcdef(c, n) |
| 2023 struct compiling *c; | |
| 2024 node *n; | |
| 2025 { | |
| 2026 object *v; | |
| 2027 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
|
2028 v = (object *)compile(n, c->c_filename); |
| 69 | 2029 if (v == NULL) |
| 2030 c->c_errors++; | |
| 2031 else { | |
| 2032 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
|
2033 int argcount; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2034 int ndefs = com_argdefs(c, n, &argcount); |
| 69 | 2035 com_addoparg(c, LOAD_CONST, i); |
| 2036 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
|
2037 if (ndefs > 0) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2038 com_addoparg(c, SET_FUNC_ARGS, argcount); |
| 69 | 2039 com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 2040 DECREF(v); | |
| 2041 } | |
| 2042 } | |
| 2043 | |
| 2044 static void | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2045 com_bases(c, n) |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2046 struct compiling *c; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2047 node *n; |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2048 { |
| 744 | 2049 int i; |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2050 REQ(n, testlist); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2051 /* testlist: test (',' test)* [','] */ |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2052 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
|
2053 com_node(c, CHILD(n, i)); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2054 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
|
2055 } |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2056 |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2057 static void |
| 69 | 2058 com_classdef(c, n) |
| 2059 struct compiling *c; | |
| 2060 node *n; | |
| 2061 { | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2062 int i; |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2063 object *v; |
| 69 | 2064 REQ(n, classdef); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2065 /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2066 if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2067 c->c_errors++; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2068 return; |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2069 } |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2070 /* Push the class name on the stack */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2071 i = com_addconst(c, v); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2072 com_addoparg(c, LOAD_CONST, i); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2073 DECREF(v); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2074 /* Push the tuple of base classes on the stack */ |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2075 if (TYPE(CHILD(n, 2)) != LPAR) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2076 com_addoparg(c, BUILD_TUPLE, 0); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2077 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2078 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
|
2079 v = (object *)compile(n, c->c_filename); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2080 if (v == NULL) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2081 c->c_errors++; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2082 else { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2083 i = com_addconst(c, v); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2084 com_addoparg(c, LOAD_CONST, i); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2085 com_addbyte(c, BUILD_FUNCTION); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2086 com_addbyte(c, UNARY_CALL); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2087 com_addbyte(c, BUILD_CLASS); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2088 com_addopname(c, STORE_NAME, CHILD(n, 1)); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2089 DECREF(v); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2090 } |
| 69 | 2091 } |
| 2092 | |
| 2093 static void | |
| 2094 com_node(c, n) | |
| 2095 struct compiling *c; | |
| 2096 node *n; | |
| 2097 { | |
| 2098 switch (TYPE(n)) { | |
| 2099 | |
| 2100 /* Definition nodes */ | |
| 2101 | |
| 2102 case funcdef: | |
| 2103 com_funcdef(c, n); | |
| 2104 break; | |
| 2105 case classdef: | |
| 2106 com_classdef(c, n); | |
| 2107 break; | |
| 2108 | |
| 2109 /* Trivial parse tree nodes */ | |
| 2110 | |
| 2111 case stmt: | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2112 case small_stmt: |
| 86 | 2113 case flow_stmt: |
| 2114 com_node(c, CHILD(n, 0)); | |
| 2115 break; | |
| 2116 | |
| 69 | 2117 case simple_stmt: |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2118 /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2119 com_addoparg(c, SET_LINENO, n->n_lineno); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2120 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2121 int i; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2122 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
|
2123 com_node(c, CHILD(n, i)); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2124 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2125 break; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2126 |
| 69 | 2127 case compound_stmt: |
| 86 | 2128 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 2129 com_node(c, CHILD(n, 0)); |
| 2130 break; | |
| 2131 | |
| 2132 /* Statement nodes */ | |
| 2133 | |
| 2134 case expr_stmt: | |
| 2135 com_expr_stmt(c, n); | |
| 2136 break; | |
| 2137 case print_stmt: | |
| 2138 com_print_stmt(c, n); | |
| 2139 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2140 case del_stmt: /* 'del' exprlist */ |
| 69 | 2141 com_assign(c, CHILD(n, 1), 0/*delete*/); |
| 2142 break; | |
| 2143 case pass_stmt: | |
| 2144 break; | |
| 2145 case break_stmt: | |
| 86 | 2146 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
|
2147 err_setstr(SyntaxError, "'break' outside loop"); |
| 86 | 2148 c->c_errors++; |
| 2149 } | |
| 69 | 2150 com_addbyte(c, BREAK_LOOP); |
| 2151 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2152 case continue_stmt: |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2153 com_continue_stmt(c, n); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
2154 break; |
| 69 | 2155 case return_stmt: |
| 2156 com_return_stmt(c, n); | |
| 2157 break; | |
| 2158 case raise_stmt: | |
| 2159 com_raise_stmt(c, n); | |
| 2160 break; | |
| 2161 case import_stmt: | |
| 2162 com_import_stmt(c, n); | |
| 2163 break; | |
|
466
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2164 case global_stmt: |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2165 com_global_stmt(c, n); |
|
90cbcb0b97dc
Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents:
409
diff
changeset
|
2166 break; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2167 case access_stmt: |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2168 com_access_stmt(c, n); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1262
diff
changeset
|
2169 break; |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2170 case exec_stmt: |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2171 com_exec_stmt(c, n); |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2172 break; |
| 69 | 2173 case if_stmt: |
| 2174 com_if_stmt(c, n); | |
| 2175 break; | |
| 2176 case while_stmt: | |
| 2177 com_while_stmt(c, n); | |
| 2178 break; | |
| 2179 case for_stmt: | |
| 2180 com_for_stmt(c, n); | |
| 2181 break; | |
| 2182 case try_stmt: | |
| 2183 com_try_stmt(c, n); | |
| 2184 break; | |
| 2185 case suite: | |
| 2186 com_suite(c, n); | |
| 2187 break; | |
| 2188 | |
| 2189 /* Expression nodes */ | |
| 2190 | |
| 2191 case testlist: | |
| 482 | 2192 com_list(c, n, 0); |
| 69 | 2193 break; |
| 2194 case test: | |
| 2195 com_test(c, n); | |
| 2196 break; | |
| 2197 case and_test: | |
| 2198 com_and_test(c, n); | |
| 2199 break; | |
| 2200 case not_test: | |
| 2201 com_not_test(c, n); | |
| 2202 break; | |
| 2203 case comparison: | |
| 2204 com_comparison(c, n); | |
| 2205 break; | |
| 2206 case exprlist: | |
| 482 | 2207 com_list(c, n, 0); |
| 69 | 2208 break; |
| 2209 case expr: | |
| 2210 com_expr(c, n); | |
| 2211 break; | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2212 case xor_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2213 com_xor_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2214 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2215 case and_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2216 com_and_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2217 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2218 case shift_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2219 com_shift_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2220 break; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2221 case arith_expr: |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2222 com_arith_expr(c, n); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
388
diff
changeset
|
2223 break; |
| 69 | 2224 case term: |
| 2225 com_term(c, n); | |
| 2226 break; | |
| 2227 case factor: | |
| 2228 com_factor(c, n); | |
| 2229 break; | |
| 2230 case atom: | |
| 2231 com_atom(c, n); | |
| 2232 break; | |
| 2233 | |
| 2234 default: | |
| 2235 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 2236 err_setstr(SystemError, "com_node: unexpected node type"); | |
| 2237 c->c_errors++; | |
| 2238 } | |
| 2239 } | |
| 2240 | |
| 2241 static void com_fplist PROTO((struct compiling *, node *)); | |
| 2242 | |
| 2243 static void | |
| 2244 com_fpdef(c, n) | |
| 2245 struct compiling *c; | |
| 2246 node *n; | |
| 2247 { | |
| 2248 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ | |
| 2249 if (TYPE(CHILD(n, 0)) == LPAR) | |
| 2250 com_fplist(c, CHILD(n, 1)); | |
| 2251 else | |
| 2252 com_addopname(c, STORE_NAME, CHILD(n, 0)); | |
| 2253 } | |
| 2254 | |
| 2255 static void | |
| 2256 com_fplist(c, n) | |
| 2257 struct compiling *c; | |
| 2258 node *n; | |
| 2259 { | |
| 544 | 2260 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
| 69 | 2261 if (NCH(n) == 1) { |
| 2262 com_fpdef(c, CHILD(n, 0)); | |
| 2263 } | |
| 2264 else { | |
| 2265 int i; | |
| 2266 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); | |
| 2267 for (i = 0; i < NCH(n); i += 2) | |
| 2268 com_fpdef(c, CHILD(n, i)); | |
| 2269 } | |
| 2270 } | |
| 2271 | |
| 2272 static void | |
| 584 | 2273 com_arglist(c, n) |
| 2274 struct compiling *c; | |
| 2275 node *n; | |
| 2276 { | |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2277 int nch, op, nargs, i, t; |
| 584 | 2278 REQ(n, varargslist); |
|
617
755bb8efcc2d
Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents:
584
diff
changeset
|
2279 /* varargslist: |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2280 (fpdef ['=' test] ',')* '*' NAME | |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2281 fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2282 nch = NCH(n); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2283 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
|
2284 op = UNPACK_VARARG; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2285 nch -= 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2286 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2287 else |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2288 op = UNPACK_ARG; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2289 nargs = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2290 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
|
2291 nargs++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2292 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2293 if (i >= nch) |
| 584 | 2294 break; |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2295 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
|
2296 if (t == EQUAL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2297 i += 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2298 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2299 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2300 t = TYPE(CHILD(n, i)); |
| 584 | 2301 } |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2302 if (t != COMMA) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2303 break; |
| 584 | 2304 } |
| 2305 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
|
2306 for (i = 0; i < nch; i++) { |
| 584 | 2307 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
|
2308 i++; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2309 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2310 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2311 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
|
2312 if (t == EQUAL) { |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2313 i += 2; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2314 if (i >= nch) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2315 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2316 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
|
2317 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2318 if (t != COMMA) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2319 break; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2320 } |
| 584 | 2321 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
|
2322 com_addopname(c, STORE_NAME, CHILD(n, nch+1)); |
| 584 | 2323 } |
| 2324 | |
| 2325 static void | |
| 69 | 2326 com_file_input(c, n) |
| 2327 struct compiling *c; | |
| 2328 node *n; | |
| 2329 { | |
| 2330 int i; | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2331 object *doc; |
| 69 | 2332 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2333 doc = get_docstring(n); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2334 if (doc != NULL) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2335 int i = com_addconst(c, doc); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2336 DECREF(doc); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2337 com_addoparg(c, LOAD_CONST, i); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2338 com_addopnamestr(c, STORE_NAME, "__doc__"); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2339 } |
| 69 | 2340 for (i = 0; i < NCH(n); i++) { |
| 2341 node *ch = CHILD(n, i); | |
| 2342 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) | |
| 2343 com_node(c, ch); | |
| 2344 } | |
| 2345 } | |
| 2346 | |
| 2347 /* Top-level compile-node interface */ | |
| 2348 | |
| 2349 static void | |
| 2350 compile_funcdef(c, n) | |
| 2351 struct compiling *c; | |
| 2352 node *n; | |
| 2353 { | |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2354 object *doc; |
| 69 | 2355 node *ch; |
| 2356 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
|
2357 c->c_name = STR(CHILD(n, 1)); |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2358 doc = get_docstring(CHILD(n, 4)); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2359 if (doc != NULL) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2360 (void) com_addconst(c, doc); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2361 DECREF(doc); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2362 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2363 com_addoparg(c, RESERVE_FAST, com_addconst(c, None)); /* Patched! */ |
| 584 | 2364 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 2365 ch = CHILD(ch, 1); /* ')' | varargslist */ | |
| 69 | 2366 if (TYPE(ch) == RPAR) |
| 482 | 2367 com_addoparg(c, UNPACK_ARG, 0); |
| 584 | 2368 else |
| 2369 com_arglist(c, ch); | |
| 86 | 2370 c->c_infunction = 1; |
| 69 | 2371 com_node(c, CHILD(n, 4)); |
| 86 | 2372 c->c_infunction = 0; |
| 69 | 2373 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2374 com_addbyte(c, RETURN_VALUE); | |
| 2375 } | |
| 2376 | |
| 2377 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
|
2378 compile_lambdef(c, n) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2379 struct compiling *c; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2380 node *n; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2381 { |
|
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
|
2382 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
|
2383 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
|
2384 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
|
2385 |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2386 ch = CHILD(n, 1); |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2387 (void) com_addconst(c, None); |
|
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
|
2388 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
|
2389 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
|
2390 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
|
2391 } |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2392 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
|
2393 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
|
2394 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
|
2395 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
|
2396 } |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
2397 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2398 com_addbyte(c, RETURN_VALUE); |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2399 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2400 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
2401 static void |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2402 compile_classdef(c, n) |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2403 struct compiling *c; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2404 node *n; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2405 { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2406 node *ch; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2407 object *doc; |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2408 REQ(n, classdef); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2409 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2410 c->c_name = STR(CHILD(n, 1)); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2411 ch = CHILD(n, NCH(n)-1); /* The suite */ |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2412 doc = get_docstring(ch); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2413 if (doc != NULL) { |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2414 int i = com_addconst(c, doc); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2415 DECREF(doc); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2416 com_addoparg(c, LOAD_CONST, i); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2417 com_addopnamestr(c, STORE_NAME, "__doc__"); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2418 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2419 else |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2420 (void) com_addconst(c, None); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2421 com_node(c, ch); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2422 com_addbyte(c, LOAD_LOCALS); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2423 com_addbyte(c, RETURN_VALUE); |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2424 } |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2425 |
|
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2426 static void |
| 69 | 2427 compile_node(c, n) |
| 2428 struct compiling *c; | |
| 2429 node *n; | |
| 2430 { | |
| 86 | 2431 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2432 | |
| 69 | 2433 switch (TYPE(n)) { |
| 2434 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2435 case single_input: /* One interactive command */ |
| 69 | 2436 /* 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
|
2437 c->c_interactive++; |
| 69 | 2438 n = CHILD(n, 0); |
| 2439 if (TYPE(n) != NEWLINE) | |
| 2440 com_node(c, n); | |
| 86 | 2441 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2442 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
|
2443 c->c_interactive--; |
| 69 | 2444 break; |
| 2445 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2446 case file_input: /* A whole file, or built-in function exec() */ |
| 69 | 2447 com_file_input(c, n); |
| 86 | 2448 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2449 com_addbyte(c, RETURN_VALUE); | |
| 69 | 2450 break; |
| 2451 | |
|
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
|
2452 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
|
2453 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
|
2454 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
|
2455 break; |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2456 |
|
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
|
2457 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
|
2458 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
|
2459 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
|
2460 |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2461 case funcdef: /* A function definition */ |
| 69 | 2462 compile_funcdef(c, n); |
| 2463 break; | |
| 2464 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
2465 case classdef: /* A class definition */ |
|
2089
53aa51433f7f
Two independent chages:
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
2466 compile_classdef(c, n); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2467 break; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
2468 |
| 69 | 2469 default: |
| 2470 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 2471 err_setstr(SystemError, "compile_node: unexpected node type"); | |
| 2472 c->c_errors++; | |
| 2473 } | |
| 2474 } | |
| 2475 | |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2476 /* 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
|
2477 |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2478 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
|
2479 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
|
2480 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
|
2481 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
|
2482 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2483 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
|
2484 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
|
2485 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
|
2486 statements. |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2487 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2488 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
|
2489 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
|
2490 |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2491 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
|
2492 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
|
2493 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
|
2494 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
|
2495 level). |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2496 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2497 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
|
2498 */ |
|
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 static void |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2501 optimize(c) |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2502 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
|
2503 { |
|
203
ceb36ec6198d
BUGFIX! Instructions are unsigned bytes.
Guido van Rossum <guido@python.org>
parents:
176
diff
changeset
|
2504 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
|
2505 object *locals; |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2506 int nlocals; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2507 int opcode; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2508 int oparg; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2509 object *name; |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2510 int fast_reserved; |
|
2052
d7e91437f0a2
Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
2005
diff
changeset
|
2511 object *error_type, *error_value, *error_traceback; |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2512 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2513 #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
|
2514 #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
|
2515 #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
|
2516 #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
|
2517 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2518 locals = newdictobject(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2519 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
|
2520 c->c_errors++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2521 return; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2522 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2523 nlocals = 0; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2524 |
|
2052
d7e91437f0a2
Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
2005
diff
changeset
|
2525 err_fetch(&error_type, &error_value, &error_traceback); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2526 |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2527 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
|
2528 for (;;) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2529 opcode = NEXTOP(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2530 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
|
2531 break; |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2532 if (opcode == EXEC_STMT) |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
2533 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
|
2534 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
|
2535 oparg = NEXTARG(); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2536 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
|
2537 opcode == IMPORT_FROM) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2538 object *v; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2539 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
|
2540 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
|
2541 continue; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2542 err_clear(); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2543 v = newintobject(nlocals); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2544 if (v == NULL) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2545 c->c_errors++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2546 goto err; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2547 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2548 nlocals++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2549 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
|
2550 DECREF(v); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2551 c->c_errors++; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2552 goto err; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2553 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2554 DECREF(v); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2555 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2556 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2557 |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2558 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
|
2559 /* 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
|
2560 goto end; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2561 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2562 |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2563 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
|
2564 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
|
2565 for (;;) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2566 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
|
2567 opcode = NEXTOP(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2568 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
|
2569 break; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2570 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
|
2571 oparg = NEXTARG(); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2572 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
|
2573 int i; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2574 object *localmap = newtupleobject(nlocals); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2575 int pos; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2576 object *key, *value; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2577 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
|
2578 err_clear(); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2579 continue; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2580 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2581 pos = 0; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2582 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
|
2583 int j; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2584 if (!is_intobject(value)) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2585 continue; |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2586 j = getintvalue(value); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2587 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
|
2588 INCREF(key); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2589 settupleitem(localmap, j, key); |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2590 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2591 } |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2592 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
|
2593 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
|
2594 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
|
2595 fast_reserved = 1; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2596 continue; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2597 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2598 if (!fast_reserved) |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2599 continue; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2600 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
|
2601 opcode == STORE_NAME || |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2602 opcode == DELETE_NAME) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2603 object *v; |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2604 int i; |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2605 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
|
2606 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
|
2607 if (v == NULL) { |
| 336 | 2608 err_clear(); |
|
1833
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2609 if (opcode == LOAD_NAME) |
|
39a4c15acb32
* Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents:
1417
diff
changeset
|
2610 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
|
2611 continue; |
| 336 | 2612 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2613 i = getintvalue(v); |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2614 switch (opcode) { |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2615 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
|
2616 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
|
2617 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
|
2618 } |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2619 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
|
2620 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
|
2621 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2622 } |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2623 |
|
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2624 end: |
|
2052
d7e91437f0a2
Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents:
2005
diff
changeset
|
2625 err_restore(error_type, error_value, error_traceback); |
|
1262
f820e6917f07
Changes to speed up local variables enormously, by avoiding dictionary
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2626 err: |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2627 DECREF(locals); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2628 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
2629 |
| 69 | 2630 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
|
2631 compile(n, filename) |
| 69 | 2632 node *n; |
| 86 | 2633 char *filename; |
| 69 | 2634 { |
| 2635 struct compiling sc; | |
| 2636 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
|
2637 if (!com_init(&sc, filename)) |
| 69 | 2638 return NULL; |
| 2639 compile_node(&sc, n); | |
| 2640 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
|
2641 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
|
2642 optimize(&sc); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2643 co = NULL; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2644 if (sc.c_errors == 0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2645 object *v, *w; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2646 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
|
2647 w = newstringobject(sc.c_name); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2648 if (v != NULL && w != NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2649 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
|
2650 sc.c_names, v, w); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2651 XDECREF(v); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1029
diff
changeset
|
2652 XDECREF(w); |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
2653 } |
| 69 | 2654 com_free(&sc); |
| 2655 return co; | |
| 2656 } |
