Mercurial > cpython-lru_cache-weakref
annotate Python/compile.c @ 388:31d509e94271 legacy-trunk
Comment out 'abort()' call.
Changed comparison operators.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 20 Oct 1991 20:12:38 +0000 |
| parents | 64189ba7e23b |
| children | b5fe6482072f |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
1 /*********************************************************** |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
120
diff
changeset
|
3 Netherlands. |
|
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 XXX Include function name in code (and module names?) | |
| 31 */ | |
| 69 | 32 |
| 86 | 33 #include "allobjects.h" |
| 34 | |
| 69 | 35 #include "node.h" |
| 36 #include "token.h" | |
| 37 #include "graminit.h" | |
| 38 #include "compile.h" | |
| 39 #include "opcode.h" | |
| 86 | 40 #include "structmember.h" |
| 41 | |
| 42 #include <ctype.h> | |
| 43 | |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
44 extern int errno; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
45 |
| 86 | 46 #define OFF(x) offsetof(codeobject, x) |
| 47 | |
| 48 static struct memberlist code_memberlist[] = { | |
| 49 {"co_code", T_OBJECT, OFF(co_code)}, | |
| 50 {"co_consts", T_OBJECT, OFF(co_consts)}, | |
| 51 {"co_names", T_OBJECT, OFF(co_names)}, | |
| 52 {"co_filename", T_OBJECT, OFF(co_filename)}, | |
| 53 {NULL} /* Sentinel */ | |
| 54 }; | |
| 55 | |
| 56 static object * | |
| 57 code_getattr(co, name) | |
| 58 codeobject *co; | |
| 59 char *name; | |
| 60 { | |
| 61 return getmember((char *)co, code_memberlist, name); | |
| 62 } | |
| 69 | 63 |
| 64 static void | |
| 86 | 65 code_dealloc(co) |
| 66 codeobject *co; | |
| 69 | 67 { |
| 86 | 68 XDECREF(co->co_code); |
| 69 XDECREF(co->co_consts); | |
| 70 XDECREF(co->co_names); | |
| 71 XDECREF(co->co_filename); | |
| 72 DEL(co); | |
| 69 | 73 } |
| 74 | |
| 75 typeobject Codetype = { | |
| 76 OB_HEAD_INIT(&Typetype) | |
| 77 0, | |
| 78 "code", | |
| 79 sizeof(codeobject), | |
| 80 0, | |
| 81 code_dealloc, /*tp_dealloc*/ | |
| 82 0, /*tp_print*/ | |
| 86 | 83 code_getattr, /*tp_getattr*/ |
| 69 | 84 0, /*tp_setattr*/ |
| 85 0, /*tp_compare*/ | |
| 86 0, /*tp_repr*/ | |
| 87 0, /*tp_as_number*/ | |
| 88 0, /*tp_as_sequence*/ | |
| 89 0, /*tp_as_mapping*/ | |
| 90 }; | |
| 91 | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
92 codeobject * |
| 86 | 93 newcodeobject(code, consts, names, filename) |
| 69 | 94 object *code; |
| 95 object *consts; | |
| 96 object *names; | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
97 object *filename; |
| 69 | 98 { |
| 99 codeobject *co; | |
| 100 int i; | |
| 101 /* Check argument types */ | |
| 102 if (code == NULL || !is_stringobject(code) || | |
| 103 consts == NULL || !is_listobject(consts) || | |
| 104 names == NULL || !is_listobject(names)) { | |
| 105 err_badcall(); | |
| 106 return NULL; | |
| 107 } | |
| 108 /* Make sure the list of names contains only strings */ | |
| 109 for (i = getlistsize(names); --i >= 0; ) { | |
| 110 object *v = getlistitem(names, i); | |
| 111 if (v == NULL || !is_stringobject(v)) { | |
| 112 err_badcall(); | |
| 113 return NULL; | |
| 114 } | |
| 115 } | |
| 116 co = NEWOBJ(codeobject, &Codetype); | |
| 117 if (co != NULL) { | |
| 118 INCREF(code); | |
| 119 co->co_code = (stringobject *)code; | |
| 120 INCREF(consts); | |
| 121 co->co_consts = consts; | |
| 122 INCREF(names); | |
| 123 co->co_names = names; | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
124 INCREF(filename); |
|
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
125 co->co_filename = filename; |
| 69 | 126 } |
| 127 return co; | |
| 128 } | |
| 129 | |
| 130 | |
| 131 /* Data structure used internally */ | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
132 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
133 #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
|
134 |
| 69 | 135 struct compiling { |
| 136 object *c_code; /* string */ | |
| 137 object *c_consts; /* list of objects */ | |
| 138 object *c_names; /* list of strings (names) */ | |
| 139 int c_nexti; /* index into c_code */ | |
| 140 int c_errors; /* counts errors occurred */ | |
| 86 | 141 int c_infunction; /* set when compiling a function */ |
| 142 int c_loops; /* counts nested loops */ | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
143 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
|
144 int c_block[MAXBLOCKS]; /* stack of block types */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
145 int c_nblocks; /* current block stack level */ |
| 86 | 146 char *c_filename; /* filename of current node */ |
| 69 | 147 }; |
| 148 | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
149 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
150 /* Interface to the block stack */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
151 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
152 static void |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
153 block_push(c, type) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
154 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
155 int type; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
156 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
157 if (c->c_nblocks >= MAXBLOCKS) { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
158 err_setstr(TypeError, "too many statically nested blocks"); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
159 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
160 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
161 else { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
162 c->c_block[c->c_nblocks++] = type; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
163 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
164 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
165 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
166 static void |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
167 block_pop(c, type) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
168 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
169 int type; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
170 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
171 if (c->c_nblocks > 0) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
172 c->c_nblocks--; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
173 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
|
174 err_setstr(SystemError, "bad block pop"); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
175 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
176 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
177 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
178 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
179 |
| 69 | 180 /* Prototypes */ |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
181 |
| 86 | 182 static int com_init PROTO((struct compiling *, char *)); |
| 69 | 183 static void com_free PROTO((struct compiling *)); |
| 184 static void com_done PROTO((struct compiling *)); | |
| 185 static void com_node PROTO((struct compiling *, struct _node *)); | |
| 186 static void com_addbyte PROTO((struct compiling *, int)); | |
| 187 static void com_addint PROTO((struct compiling *, int)); | |
| 188 static void com_addoparg PROTO((struct compiling *, int, int)); | |
| 189 static void com_addfwref PROTO((struct compiling *, int, int *)); | |
| 190 static void com_backpatch PROTO((struct compiling *, int)); | |
| 191 static int com_add PROTO((struct compiling *, object *, object *)); | |
| 192 static int com_addconst PROTO((struct compiling *, object *)); | |
| 193 static int com_addname PROTO((struct compiling *, object *)); | |
| 194 static void com_addopname PROTO((struct compiling *, int, node *)); | |
| 195 | |
| 196 static int | |
| 86 | 197 com_init(c, filename) |
| 69 | 198 struct compiling *c; |
| 86 | 199 char *filename; |
| 69 | 200 { |
|
160
afa48a96a444
Create code string with initial size of 1000 instead of 0.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
201 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL) |
| 69 | 202 goto fail_3; |
| 203 if ((c->c_consts = newlistobject(0)) == NULL) | |
| 204 goto fail_2; | |
| 205 if ((c->c_names = newlistobject(0)) == NULL) | |
| 206 goto fail_1; | |
| 207 c->c_nexti = 0; | |
| 208 c->c_errors = 0; | |
| 86 | 209 c->c_infunction = 0; |
| 210 c->c_loops = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
211 c->c_begin = 0; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
212 c->c_nblocks = 0; |
| 86 | 213 c->c_filename = filename; |
| 69 | 214 return 1; |
| 215 | |
| 216 fail_1: | |
| 217 DECREF(c->c_consts); | |
| 218 fail_2: | |
| 219 DECREF(c->c_code); | |
| 220 fail_3: | |
| 221 return 0; | |
| 222 } | |
| 223 | |
| 224 static void | |
| 225 com_free(c) | |
| 226 struct compiling *c; | |
| 227 { | |
| 228 XDECREF(c->c_code); | |
| 229 XDECREF(c->c_consts); | |
| 230 XDECREF(c->c_names); | |
| 231 } | |
| 232 | |
| 233 static void | |
| 234 com_done(c) | |
| 235 struct compiling *c; | |
| 236 { | |
| 237 if (c->c_code != NULL) | |
| 238 resizestring(&c->c_code, c->c_nexti); | |
| 239 } | |
| 240 | |
| 241 static void | |
| 242 com_addbyte(c, byte) | |
| 243 struct compiling *c; | |
| 244 int byte; | |
| 245 { | |
| 246 int len; | |
| 247 if (byte < 0 || byte > 255) { | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
248 /* |
| 86 | 249 fprintf(stderr, "XXX compiling bad byte: %d\n", byte); |
| 250 abort(); | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
251 */ |
| 69 | 252 err_setstr(SystemError, "com_addbyte: byte out of range"); |
| 253 c->c_errors++; | |
| 254 } | |
| 255 if (c->c_code == NULL) | |
| 256 return; | |
| 257 len = getstringsize(c->c_code); | |
| 258 if (c->c_nexti >= len) { | |
| 259 if (resizestring(&c->c_code, len+1000) != 0) { | |
| 260 c->c_errors++; | |
| 261 return; | |
| 262 } | |
| 263 } | |
| 264 getstringvalue(c->c_code)[c->c_nexti++] = byte; | |
| 265 } | |
| 266 | |
| 267 static void | |
| 268 com_addint(c, x) | |
| 269 struct compiling *c; | |
| 270 int x; | |
| 271 { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
272 com_addbyte(c, x & 0xff); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
273 com_addbyte(c, x >> 8); /* XXX x should be positive */ |
| 69 | 274 } |
| 275 | |
| 276 static void | |
| 277 com_addoparg(c, op, arg) | |
| 278 struct compiling *c; | |
| 279 int op; | |
| 280 int arg; | |
| 281 { | |
| 282 com_addbyte(c, op); | |
| 283 com_addint(c, arg); | |
| 284 } | |
| 285 | |
| 286 static void | |
| 287 com_addfwref(c, op, p_anchor) | |
| 288 struct compiling *c; | |
| 289 int op; | |
| 290 int *p_anchor; | |
| 291 { | |
| 292 /* Compile a forward reference for backpatching */ | |
| 293 int here; | |
| 294 int anchor; | |
| 295 com_addbyte(c, op); | |
| 296 here = c->c_nexti; | |
| 297 anchor = *p_anchor; | |
| 298 *p_anchor = here; | |
| 299 com_addint(c, anchor == 0 ? 0 : here - anchor); | |
| 300 } | |
| 301 | |
| 302 static void | |
| 303 com_backpatch(c, anchor) | |
| 304 struct compiling *c; | |
| 305 int anchor; /* Must be nonzero */ | |
| 306 { | |
| 307 unsigned char *code = (unsigned char *) getstringvalue(c->c_code); | |
| 308 int target = c->c_nexti; | |
| 309 int lastanchor = 0; | |
| 310 int dist; | |
| 311 int prev; | |
| 312 for (;;) { | |
| 313 /* Make the JUMP instruction at anchor point to target */ | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
314 prev = code[anchor] + (code[anchor+1] << 8); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
315 dist = target - (anchor+2); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
316 code[anchor] = dist & 0xff; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
317 code[anchor+1] = dist >> 8; |
| 69 | 318 if (!prev) |
| 319 break; | |
| 320 lastanchor = anchor; | |
| 321 anchor -= prev; | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 /* Handle constants and names uniformly */ | |
| 326 | |
| 327 static int | |
| 328 com_add(c, list, v) | |
| 329 struct compiling *c; | |
| 330 object *list; | |
| 331 object *v; | |
| 332 { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
333 int n = getlistsize(list); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
334 int i; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
335 for (i = n; --i >= 0; ) { |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
336 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
|
337 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
|
338 return i; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
339 } |
| 69 | 340 if (addlistitem(list, v) != 0) |
| 341 c->c_errors++; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
342 return n; |
| 69 | 343 } |
| 344 | |
| 345 static int | |
| 346 com_addconst(c, v) | |
| 347 struct compiling *c; | |
| 348 object *v; | |
| 349 { | |
| 350 return com_add(c, c->c_consts, v); | |
| 351 } | |
| 352 | |
| 353 static int | |
| 354 com_addname(c, v) | |
| 355 struct compiling *c; | |
| 356 object *v; | |
| 357 { | |
| 358 return com_add(c, c->c_names, v); | |
| 359 } | |
| 360 | |
| 361 static void | |
| 362 com_addopname(c, op, n) | |
| 363 struct compiling *c; | |
| 364 int op; | |
| 365 node *n; | |
| 366 { | |
| 367 object *v; | |
| 368 int i; | |
| 369 char *name; | |
| 370 if (TYPE(n) == STAR) | |
| 371 name = "*"; | |
| 372 else { | |
| 373 REQ(n, NAME); | |
| 374 name = STR(n); | |
| 375 } | |
| 376 if ((v = newstringobject(name)) == NULL) { | |
| 377 c->c_errors++; | |
| 378 i = 255; | |
| 379 } | |
| 380 else { | |
| 381 i = com_addname(c, v); | |
| 382 DECREF(v); | |
| 383 } | |
| 384 com_addoparg(c, op, i); | |
| 385 } | |
| 386 | |
| 387 static object * | |
| 388 parsenumber(s) | |
| 389 char *s; | |
| 390 { | |
| 391 extern long strtol(); | |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
392 extern double strtod(); |
| 69 | 393 char *end = s; |
| 394 long x; | |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
395 double xx; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
396 errno = 0; |
| 69 | 397 x = strtol(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
|
398 if (*end == '\0') { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
399 if (errno != 0) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
400 err_setstr(RuntimeError, "integer constant too large"); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
401 return NULL; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
402 } |
| 69 | 403 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
|
404 } |
|
226
7e83115edb6a
Added long integer support.
Guido van Rossum <guido@python.org>
parents:
203
diff
changeset
|
405 if (*end == 'l' || *end == 'L') { |
|
7e83115edb6a
Added long integer support.
Guido van Rossum <guido@python.org>
parents:
203
diff
changeset
|
406 extern object *long_scan(); |
|
7e83115edb6a
Added long integer support.
Guido van Rossum <guido@python.org>
parents:
203
diff
changeset
|
407 return long_scan(s, 0); |
|
7e83115edb6a
Added long integer support.
Guido van Rossum <guido@python.org>
parents:
203
diff
changeset
|
408 } |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
409 errno = 0; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
410 xx = strtod(s, &end); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
411 if (*end == '\0') { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
412 if (errno != 0) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
413 err_setstr(RuntimeError, "float constant too large"); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
414 return NULL; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
415 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
416 return newfloatobject(xx); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
417 } |
| 69 | 418 err_setstr(RuntimeError, "bad number syntax"); |
| 419 return NULL; | |
| 420 } | |
| 421 | |
| 422 static object * | |
| 423 parsestr(s) | |
| 424 char *s; | |
| 425 { | |
| 426 object *v; | |
| 427 int len; | |
| 428 char *buf; | |
| 429 char *p; | |
| 430 int c; | |
| 431 if (*s != '\'') { | |
| 432 err_badcall(); | |
| 433 return NULL; | |
| 434 } | |
| 435 s++; | |
| 436 len = strlen(s); | |
| 437 if (s[--len] != '\'') { | |
| 438 err_badcall(); | |
| 439 return NULL; | |
| 440 } | |
| 441 if (strchr(s, '\\') == NULL) | |
| 442 return newsizedstringobject(s, len); | |
| 443 v = newsizedstringobject((char *)NULL, len); | |
| 444 p = buf = getstringvalue(v); | |
| 445 while (*s != '\0' && *s != '\'') { | |
| 446 if (*s != '\\') { | |
| 447 *p++ = *s++; | |
| 448 continue; | |
| 449 } | |
| 450 s++; | |
| 451 switch (*s++) { | |
| 452 /* XXX This assumes ASCII! */ | |
| 453 case '\\': *p++ = '\\'; break; | |
| 454 case '\'': *p++ = '\''; break; | |
| 455 case 'b': *p++ = '\b'; break; | |
| 456 case 'f': *p++ = '\014'; break; /* FF */ | |
| 457 case 't': *p++ = '\t'; break; | |
| 458 case 'n': *p++ = '\n'; break; | |
| 459 case 'r': *p++ = '\r'; break; | |
| 460 case 'v': *p++ = '\013'; break; /* VT */ | |
| 461 case 'E': *p++ = '\033'; break; /* ESC, not C */ | |
| 462 case 'a': *p++ = '\007'; break; /* BEL, not classic C */ | |
| 463 case '0': case '1': case '2': case '3': | |
| 464 case '4': case '5': case '6': case '7': | |
| 465 c = s[-1] - '0'; | |
| 466 if ('0' <= *s && *s <= '7') { | |
| 467 c = (c<<3) + *s++ - '0'; | |
| 468 if ('0' <= *s && *s <= '7') | |
| 469 c = (c<<3) + *s++ - '0'; | |
| 470 } | |
| 471 *p++ = c; | |
| 472 break; | |
| 473 case 'x': | |
| 474 if (isxdigit(*s)) { | |
| 475 sscanf(s, "%x", &c); | |
| 476 *p++ = c; | |
| 477 do { | |
| 478 s++; | |
| 479 } while (isxdigit(*s)); | |
| 480 break; | |
| 481 } | |
| 482 /* FALLTHROUGH */ | |
| 483 default: *p++ = '\\'; *p++ = s[-1]; break; | |
| 484 } | |
| 485 } | |
| 486 resizestring(&v, (int)(p - buf)); | |
| 487 return v; | |
| 488 } | |
| 489 | |
| 490 static void | |
| 491 com_list_constructor(c, n) | |
| 492 struct compiling *c; | |
| 493 node *n; | |
| 494 { | |
| 495 int len; | |
| 496 int i; | |
| 497 object *v, *w; | |
| 498 if (TYPE(n) != testlist) | |
| 499 REQ(n, exprlist); | |
| 500 /* exprlist: expr (',' expr)* [',']; likewise for testlist */ | |
| 501 len = (NCH(n) + 1) / 2; | |
| 502 for (i = 0; i < NCH(n); i += 2) | |
| 503 com_node(c, CHILD(n, i)); | |
| 504 com_addoparg(c, BUILD_LIST, len); | |
| 505 } | |
| 506 | |
| 507 static void | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
508 com_dictmaker(c, n) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
509 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
510 node *n; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
511 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
512 int i; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
513 /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
514 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
|
515 /* 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
|
516 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
|
517 com_addbyte(c, DUP_TOP); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
518 com_node(c, CHILD(n, i+2)); /* value */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
519 com_addbyte(c, ROT_TWO); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
520 com_node(c, CHILD(n, i)); /* key */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
521 com_addbyte(c, STORE_SUBSCR); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
522 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
523 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
524 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
525 static void |
| 69 | 526 com_atom(c, n) |
| 527 struct compiling *c; | |
| 528 node *n; | |
| 529 { | |
| 530 node *ch; | |
| 531 object *v; | |
| 532 int i; | |
| 533 REQ(n, atom); | |
| 534 ch = CHILD(n, 0); | |
| 535 switch (TYPE(ch)) { | |
| 536 case LPAR: | |
| 537 if (TYPE(CHILD(n, 1)) == RPAR) | |
| 538 com_addoparg(c, BUILD_TUPLE, 0); | |
| 539 else | |
| 540 com_node(c, CHILD(n, 1)); | |
| 541 break; | |
| 542 case LSQB: | |
| 543 if (TYPE(CHILD(n, 1)) == RSQB) | |
| 544 com_addoparg(c, BUILD_LIST, 0); | |
| 545 else | |
| 546 com_list_constructor(c, CHILD(n, 1)); | |
| 547 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
548 case LBRACE: /* '{' [dictmaker] '}' */ |
| 69 | 549 com_addoparg(c, BUILD_MAP, 0); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
550 if (TYPE(CHILD(n, 1)) != RBRACE) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
551 com_dictmaker(c, CHILD(n, 1)); |
| 69 | 552 break; |
| 553 case BACKQUOTE: | |
| 554 com_node(c, CHILD(n, 1)); | |
| 555 com_addbyte(c, UNARY_CONVERT); | |
| 556 break; | |
| 557 case NUMBER: | |
| 558 if ((v = parsenumber(STR(ch))) == NULL) { | |
| 559 c->c_errors++; | |
| 560 i = 255; | |
| 561 } | |
| 562 else { | |
| 563 i = com_addconst(c, v); | |
| 564 DECREF(v); | |
| 565 } | |
| 566 com_addoparg(c, LOAD_CONST, i); | |
| 567 break; | |
| 568 case STRING: | |
| 569 if ((v = parsestr(STR(ch))) == NULL) { | |
| 570 c->c_errors++; | |
| 571 i = 255; | |
| 572 } | |
| 573 else { | |
| 574 i = com_addconst(c, v); | |
| 575 DECREF(v); | |
| 576 } | |
| 577 com_addoparg(c, LOAD_CONST, i); | |
| 578 break; | |
| 579 case NAME: | |
| 580 com_addopname(c, LOAD_NAME, ch); | |
| 581 break; | |
| 582 default: | |
| 583 fprintf(stderr, "node type %d\n", TYPE(ch)); | |
| 584 err_setstr(SystemError, "com_atom: unexpected node type"); | |
| 585 c->c_errors++; | |
| 586 } | |
| 587 } | |
| 588 | |
| 589 static void | |
| 590 com_slice(c, n, op) | |
| 591 struct compiling *c; | |
| 592 node *n; | |
| 593 int op; | |
| 594 { | |
| 595 if (NCH(n) == 1) { | |
| 596 com_addbyte(c, op); | |
| 597 } | |
| 598 else if (NCH(n) == 2) { | |
| 599 if (TYPE(CHILD(n, 0)) != COLON) { | |
| 600 com_node(c, CHILD(n, 0)); | |
| 601 com_addbyte(c, op+1); | |
| 602 } | |
| 603 else { | |
| 604 com_node(c, CHILD(n, 1)); | |
| 605 com_addbyte(c, op+2); | |
| 606 } | |
| 607 } | |
| 608 else { | |
| 609 com_node(c, CHILD(n, 0)); | |
| 610 com_node(c, CHILD(n, 2)); | |
| 611 com_addbyte(c, op+3); | |
| 612 } | |
| 613 } | |
| 614 | |
| 615 static void | |
| 616 com_apply_subscript(c, n) | |
| 617 struct compiling *c; | |
| 618 node *n; | |
| 619 { | |
| 620 REQ(n, subscript); | |
| 621 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) { | |
| 622 /* It's a single subscript */ | |
| 623 com_node(c, CHILD(n, 0)); | |
| 624 com_addbyte(c, BINARY_SUBSCR); | |
| 625 } | |
| 626 else { | |
| 627 /* It's a slice: [expr] ':' [expr] */ | |
| 628 com_slice(c, n, SLICE); | |
| 629 } | |
| 630 } | |
| 631 | |
| 632 static void | |
| 633 com_call_function(c, n) | |
| 634 struct compiling *c; | |
| 635 node *n; /* EITHER testlist OR ')' */ | |
| 636 { | |
| 637 if (TYPE(n) == RPAR) { | |
| 638 com_addbyte(c, UNARY_CALL); | |
| 639 } | |
| 640 else { | |
| 641 com_node(c, n); | |
| 642 com_addbyte(c, BINARY_CALL); | |
| 643 } | |
| 644 } | |
| 645 | |
| 646 static void | |
| 647 com_select_member(c, n) | |
| 648 struct compiling *c; | |
| 649 node *n; | |
| 650 { | |
| 651 com_addopname(c, LOAD_ATTR, n); | |
| 652 } | |
| 653 | |
| 654 static void | |
| 655 com_apply_trailer(c, n) | |
| 656 struct compiling *c; | |
| 657 node *n; | |
| 658 { | |
| 659 REQ(n, trailer); | |
| 660 switch (TYPE(CHILD(n, 0))) { | |
| 661 case LPAR: | |
| 662 com_call_function(c, CHILD(n, 1)); | |
| 663 break; | |
| 664 case DOT: | |
| 665 com_select_member(c, CHILD(n, 1)); | |
| 666 break; | |
| 667 case LSQB: | |
| 668 com_apply_subscript(c, CHILD(n, 1)); | |
| 669 break; | |
| 670 default: | |
| 671 err_setstr(SystemError, | |
| 672 "com_apply_trailer: unknown trailer type"); | |
| 673 c->c_errors++; | |
| 674 } | |
| 675 } | |
| 676 | |
| 677 static void | |
| 678 com_factor(c, n) | |
| 679 struct compiling *c; | |
| 680 node *n; | |
| 681 { | |
| 682 int i; | |
| 683 REQ(n, factor); | |
| 684 if (TYPE(CHILD(n, 0)) == PLUS) { | |
| 685 com_factor(c, CHILD(n, 1)); | |
| 686 com_addbyte(c, UNARY_POSITIVE); | |
| 687 } | |
| 688 else if (TYPE(CHILD(n, 0)) == MINUS) { | |
| 689 com_factor(c, CHILD(n, 1)); | |
| 690 com_addbyte(c, UNARY_NEGATIVE); | |
| 691 } | |
| 692 else { | |
| 693 com_atom(c, CHILD(n, 0)); | |
| 694 for (i = 1; i < NCH(n); i++) | |
| 695 com_apply_trailer(c, CHILD(n, i)); | |
| 696 } | |
| 697 } | |
| 698 | |
| 699 static void | |
| 700 com_term(c, n) | |
| 701 struct compiling *c; | |
| 702 node *n; | |
| 703 { | |
| 704 int i; | |
| 705 int op; | |
| 706 REQ(n, term); | |
| 707 com_factor(c, CHILD(n, 0)); | |
| 708 for (i = 2; i < NCH(n); i += 2) { | |
| 709 com_factor(c, CHILD(n, i)); | |
| 710 switch (TYPE(CHILD(n, i-1))) { | |
| 711 case STAR: | |
| 712 op = BINARY_MULTIPLY; | |
| 713 break; | |
| 714 case SLASH: | |
| 715 op = BINARY_DIVIDE; | |
| 716 break; | |
| 717 case PERCENT: | |
| 718 op = BINARY_MODULO; | |
| 719 break; | |
| 720 default: | |
| 721 err_setstr(SystemError, | |
| 722 "com_term: term operator not *, / or %"); | |
| 723 c->c_errors++; | |
| 724 op = 255; | |
| 725 } | |
| 726 com_addbyte(c, op); | |
| 727 } | |
| 728 } | |
| 729 | |
| 730 static void | |
| 731 com_expr(c, n) | |
| 732 struct compiling *c; | |
| 733 node *n; | |
| 734 { | |
| 735 int i; | |
| 736 int op; | |
| 737 REQ(n, expr); | |
| 738 com_term(c, CHILD(n, 0)); | |
| 739 for (i = 2; i < NCH(n); i += 2) { | |
| 740 com_term(c, CHILD(n, i)); | |
| 741 switch (TYPE(CHILD(n, i-1))) { | |
| 742 case PLUS: | |
| 743 op = BINARY_ADD; | |
| 744 break; | |
| 745 case MINUS: | |
| 746 op = BINARY_SUBTRACT; | |
| 747 break; | |
| 748 default: | |
| 749 err_setstr(SystemError, | |
| 750 "com_expr: expr operator not + or -"); | |
| 751 c->c_errors++; | |
| 752 op = 255; | |
| 753 } | |
| 754 com_addbyte(c, op); | |
| 755 } | |
| 756 } | |
| 757 | |
| 758 static enum cmp_op | |
| 759 cmp_type(n) | |
| 760 node *n; | |
| 761 { | |
| 762 REQ(n, comp_op); | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
763 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
| 69 | 764 | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 765 if (NCH(n) == 1) { | |
| 766 n = CHILD(n, 0); | |
| 767 switch (TYPE(n)) { | |
| 768 case LESS: return LT; | |
| 769 case GREATER: return GT; | |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
770 case EQEQUAL: /* == */ |
| 69 | 771 case EQUAL: return EQ; |
|
388
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
772 case LESSEQUAL: return LE; |
|
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
773 case GREATEREQUAL: return GE; |
|
31d509e94271
Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents:
336
diff
changeset
|
774 case NOTEQUAL: return NE; /* <> or != */ |
| 69 | 775 case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 776 if (strcmp(STR(n), "is") == 0) return IS; | |
| 777 } | |
| 778 } | |
| 779 else if (NCH(n) == 2) { | |
| 780 int t2 = TYPE(CHILD(n, 1)); | |
| 781 switch (TYPE(CHILD(n, 0))) { | |
| 782 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 783 return NOT_IN; | |
| 784 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 785 return IS_NOT; | |
| 786 } | |
| 787 } | |
| 788 return BAD; | |
| 789 } | |
| 790 | |
| 791 static void | |
| 792 com_comparison(c, n) | |
| 793 struct compiling *c; | |
| 794 node *n; | |
| 795 { | |
| 796 int i; | |
| 797 enum cmp_op op; | |
| 798 int anchor; | |
| 799 REQ(n, comparison); /* comparison: expr (comp_op expr)* */ | |
| 800 com_expr(c, CHILD(n, 0)); | |
| 801 if (NCH(n) == 1) | |
| 802 return; | |
| 803 | |
| 804 /**************************************************************** | |
| 805 The following code is generated for all but the last | |
| 806 comparison in a chain: | |
| 807 | |
| 808 label: on stack: opcode: jump to: | |
| 809 | |
| 810 a <code to load b> | |
| 811 a, b DUP_TOP | |
| 812 a, b, b ROT_THREE | |
| 813 b, a, b COMPARE_OP | |
| 814 b, 0-or-1 JUMP_IF_FALSE L1 | |
| 815 b, 1 POP_TOP | |
| 816 b | |
| 817 | |
| 818 We are now ready to repeat this sequence for the next | |
| 819 comparison in the chain. | |
| 820 | |
| 821 For the last we generate: | |
| 822 | |
| 823 b <code to load c> | |
| 824 b, c COMPARE_OP | |
| 825 0-or-1 | |
| 826 | |
| 827 If there were any jumps to L1 (i.e., there was more than one | |
| 828 comparison), we generate: | |
| 829 | |
| 830 0-or-1 JUMP_FORWARD L2 | |
| 831 L1: b, 0 ROT_TWO | |
| 832 0, b POP_TOP | |
| 833 0 | |
| 834 L2: | |
| 835 ****************************************************************/ | |
| 836 | |
| 837 anchor = 0; | |
| 838 | |
| 839 for (i = 2; i < NCH(n); i += 2) { | |
| 840 com_expr(c, CHILD(n, i)); | |
| 841 if (i+2 < NCH(n)) { | |
| 842 com_addbyte(c, DUP_TOP); | |
| 843 com_addbyte(c, ROT_THREE); | |
| 844 } | |
| 845 op = cmp_type(CHILD(n, i-1)); | |
| 846 if (op == BAD) { | |
| 847 err_setstr(SystemError, | |
| 848 "com_comparison: unknown comparison op"); | |
| 849 c->c_errors++; | |
| 850 } | |
| 851 com_addoparg(c, COMPARE_OP, op); | |
| 852 if (i+2 < NCH(n)) { | |
| 853 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 854 com_addbyte(c, POP_TOP); | |
| 855 } | |
| 856 } | |
| 857 | |
| 858 if (anchor) { | |
| 859 int anchor2 = 0; | |
| 860 com_addfwref(c, JUMP_FORWARD, &anchor2); | |
| 861 com_backpatch(c, anchor); | |
| 862 com_addbyte(c, ROT_TWO); | |
| 863 com_addbyte(c, POP_TOP); | |
| 864 com_backpatch(c, anchor2); | |
| 865 } | |
| 866 } | |
| 867 | |
| 868 static void | |
| 869 com_not_test(c, n) | |
| 870 struct compiling *c; | |
| 871 node *n; | |
| 872 { | |
| 873 REQ(n, not_test); /* 'not' not_test | comparison */ | |
| 874 if (NCH(n) == 1) { | |
| 875 com_comparison(c, CHILD(n, 0)); | |
| 876 } | |
| 877 else { | |
| 878 com_not_test(c, CHILD(n, 1)); | |
| 879 com_addbyte(c, UNARY_NOT); | |
| 880 } | |
| 881 } | |
| 882 | |
| 883 static void | |
| 884 com_and_test(c, n) | |
| 885 struct compiling *c; | |
| 886 node *n; | |
| 887 { | |
| 888 int i; | |
| 889 int anchor; | |
| 890 REQ(n, and_test); /* not_test ('and' not_test)* */ | |
| 891 anchor = 0; | |
| 892 i = 0; | |
| 893 for (;;) { | |
| 894 com_not_test(c, CHILD(n, i)); | |
| 895 if ((i += 2) >= NCH(n)) | |
| 896 break; | |
| 897 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 898 com_addbyte(c, POP_TOP); | |
| 899 } | |
| 900 if (anchor) | |
| 901 com_backpatch(c, anchor); | |
| 902 } | |
| 903 | |
| 904 static void | |
| 905 com_test(c, n) | |
| 906 struct compiling *c; | |
| 907 node *n; | |
| 908 { | |
| 909 int i; | |
| 910 int anchor; | |
| 911 REQ(n, test); /* and_test ('and' and_test)* */ | |
| 912 anchor = 0; | |
| 913 i = 0; | |
| 914 for (;;) { | |
| 915 com_and_test(c, CHILD(n, i)); | |
| 916 if ((i += 2) >= NCH(n)) | |
| 917 break; | |
| 918 com_addfwref(c, JUMP_IF_TRUE, &anchor); | |
| 919 com_addbyte(c, POP_TOP); | |
| 920 } | |
| 921 if (anchor) | |
| 922 com_backpatch(c, anchor); | |
| 923 } | |
| 924 | |
| 925 static void | |
| 926 com_list(c, n) | |
| 927 struct compiling *c; | |
| 928 node *n; | |
| 929 { | |
| 930 /* exprlist: expr (',' expr)* [',']; likewise for testlist */ | |
| 931 if (NCH(n) == 1) { | |
| 932 com_node(c, CHILD(n, 0)); | |
| 933 } | |
| 934 else { | |
| 935 int i; | |
| 936 int len; | |
| 937 len = (NCH(n) + 1) / 2; | |
| 938 for (i = 0; i < NCH(n); i += 2) | |
| 939 com_node(c, CHILD(n, i)); | |
| 940 com_addoparg(c, BUILD_TUPLE, len); | |
| 941 } | |
| 942 } | |
| 943 | |
| 944 | |
| 945 /* Begin of assignment compilation */ | |
| 946 | |
| 947 static void com_assign_name PROTO((struct compiling *, node *, int)); | |
| 948 static void com_assign PROTO((struct compiling *, node *, int)); | |
| 949 | |
| 950 static void | |
| 951 com_assign_attr(c, n, assigning) | |
| 952 struct compiling *c; | |
| 953 node *n; | |
| 954 int assigning; | |
| 955 { | |
| 956 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); | |
| 957 } | |
| 958 | |
| 959 static void | |
| 960 com_assign_slice(c, n, assigning) | |
| 961 struct compiling *c; | |
| 962 node *n; | |
| 963 int assigning; | |
| 964 { | |
| 965 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE); | |
| 966 } | |
| 967 | |
| 968 static void | |
| 969 com_assign_subscript(c, n, assigning) | |
| 970 struct compiling *c; | |
| 971 node *n; | |
| 972 int assigning; | |
| 973 { | |
| 974 com_node(c, n); | |
| 975 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR); | |
| 976 } | |
| 977 | |
| 978 static void | |
| 979 com_assign_trailer(c, n, assigning) | |
| 980 struct compiling *c; | |
| 981 node *n; | |
| 982 int assigning; | |
| 983 { | |
| 984 char *name; | |
| 985 REQ(n, trailer); | |
| 986 switch (TYPE(CHILD(n, 0))) { | |
| 987 case LPAR: /* '(' [exprlist] ')' */ | |
| 988 err_setstr(TypeError, "can't assign to function call"); | |
| 989 c->c_errors++; | |
| 990 break; | |
| 991 case DOT: /* '.' NAME */ | |
| 992 com_assign_attr(c, CHILD(n, 1), assigning); | |
| 993 break; | |
| 994 case LSQB: /* '[' subscript ']' */ | |
| 995 n = CHILD(n, 1); | |
| 996 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */ | |
| 997 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON) | |
| 998 com_assign_slice(c, n, assigning); | |
| 999 else | |
| 1000 com_assign_subscript(c, CHILD(n, 0), assigning); | |
| 1001 break; | |
| 1002 default: | |
| 1003 err_setstr(TypeError, "unknown trailer type"); | |
| 1004 c->c_errors++; | |
| 1005 } | |
| 1006 } | |
| 1007 | |
| 1008 static void | |
| 1009 com_assign_tuple(c, n, assigning) | |
| 1010 struct compiling *c; | |
| 1011 node *n; | |
| 1012 int assigning; | |
| 1013 { | |
| 1014 int i; | |
| 1015 if (TYPE(n) != testlist) | |
| 1016 REQ(n, exprlist); | |
| 1017 if (assigning) | |
| 1018 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); | |
| 1019 for (i = 0; i < NCH(n); i += 2) | |
| 1020 com_assign(c, CHILD(n, i), assigning); | |
| 1021 } | |
| 1022 | |
| 1023 static void | |
| 1024 com_assign_list(c, n, assigning) | |
| 1025 struct compiling *c; | |
| 1026 node *n; | |
| 1027 int assigning; | |
| 1028 { | |
| 1029 int i; | |
| 1030 if (assigning) | |
| 1031 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2); | |
| 1032 for (i = 0; i < NCH(n); i += 2) | |
| 1033 com_assign(c, CHILD(n, i), assigning); | |
| 1034 } | |
| 1035 | |
| 1036 static void | |
| 1037 com_assign_name(c, n, assigning) | |
| 1038 struct compiling *c; | |
| 1039 node *n; | |
| 1040 int assigning; | |
| 1041 { | |
| 1042 REQ(n, NAME); | |
| 1043 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); | |
| 1044 } | |
| 1045 | |
| 1046 static void | |
| 1047 com_assign(c, n, assigning) | |
| 1048 struct compiling *c; | |
| 1049 node *n; | |
| 1050 int assigning; | |
| 1051 { | |
| 1052 /* Loop to avoid trivial recursion */ | |
| 1053 for (;;) { | |
| 1054 switch (TYPE(n)) { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1055 |
| 69 | 1056 case exprlist: |
| 1057 case testlist: | |
| 1058 if (NCH(n) > 1) { | |
| 1059 com_assign_tuple(c, n, assigning); | |
| 1060 return; | |
| 1061 } | |
| 1062 n = CHILD(n, 0); | |
| 1063 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1064 |
| 69 | 1065 case test: |
| 1066 case and_test: | |
| 1067 case not_test: | |
| 1068 if (NCH(n) > 1) { | |
| 1069 err_setstr(TypeError, | |
| 1070 "can't assign to operator"); | |
| 1071 c->c_errors++; | |
| 1072 return; | |
| 1073 } | |
| 1074 n = CHILD(n, 0); | |
| 1075 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1076 |
| 69 | 1077 case comparison: |
| 1078 if (NCH(n) > 1) { | |
| 1079 err_setstr(TypeError, | |
| 1080 "can't assign to operator"); | |
| 1081 c->c_errors++; | |
| 1082 return; | |
| 1083 } | |
| 1084 n = CHILD(n, 0); | |
| 1085 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1086 |
| 69 | 1087 case expr: |
| 1088 if (NCH(n) > 1) { | |
| 1089 err_setstr(TypeError, | |
| 1090 "can't assign to operator"); | |
| 1091 c->c_errors++; | |
| 1092 return; | |
| 1093 } | |
| 1094 n = CHILD(n, 0); | |
| 1095 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1096 |
| 69 | 1097 case term: |
| 1098 if (NCH(n) > 1) { | |
| 1099 err_setstr(TypeError, | |
| 1100 "can't assign to operator"); | |
| 1101 c->c_errors++; | |
| 1102 return; | |
| 1103 } | |
| 1104 n = CHILD(n, 0); | |
| 1105 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1106 |
| 69 | 1107 case factor: /* ('+'|'-') factor | atom trailer* */ |
| 1108 if (TYPE(CHILD(n, 0)) != atom) { /* '+' | '-' */ | |
| 1109 err_setstr(TypeError, | |
| 1110 "can't assign to operator"); | |
| 1111 c->c_errors++; | |
| 1112 return; | |
| 1113 } | |
| 1114 if (NCH(n) > 1) { /* trailer present */ | |
| 1115 int i; | |
| 1116 com_node(c, CHILD(n, 0)); | |
| 1117 for (i = 1; i+1 < NCH(n); i++) { | |
| 1118 com_apply_trailer(c, CHILD(n, i)); | |
| 1119 } /* NB i is still alive */ | |
| 1120 com_assign_trailer(c, | |
| 1121 CHILD(n, i), assigning); | |
| 1122 return; | |
| 1123 } | |
| 1124 n = CHILD(n, 0); | |
| 1125 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1126 |
| 69 | 1127 case atom: |
| 1128 switch (TYPE(CHILD(n, 0))) { | |
| 1129 case LPAR: | |
| 1130 n = CHILD(n, 1); | |
| 1131 if (TYPE(n) == RPAR) { | |
| 1132 /* XXX Should allow () = () ??? */ | |
| 1133 err_setstr(TypeError, | |
| 1134 "can't assign to ()"); | |
| 1135 c->c_errors++; | |
| 1136 return; | |
| 1137 } | |
| 1138 break; | |
| 1139 case LSQB: | |
| 1140 n = CHILD(n, 1); | |
| 1141 if (TYPE(n) == RSQB) { | |
| 1142 err_setstr(TypeError, | |
| 1143 "can't assign to []"); | |
| 1144 c->c_errors++; | |
| 1145 return; | |
| 1146 } | |
| 1147 com_assign_list(c, n, assigning); | |
| 1148 return; | |
| 1149 case NAME: | |
| 1150 com_assign_name(c, CHILD(n, 0), assigning); | |
| 1151 return; | |
| 1152 default: | |
| 1153 err_setstr(TypeError, | |
| 1154 "can't assign to constant"); | |
| 1155 c->c_errors++; | |
| 1156 return; | |
| 1157 } | |
| 1158 break; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1159 |
| 69 | 1160 default: |
| 1161 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 1162 err_setstr(SystemError, "com_assign: bad node"); | |
| 1163 c->c_errors++; | |
| 1164 return; | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1165 |
| 69 | 1166 } |
| 1167 } | |
| 1168 } | |
| 1169 | |
| 1170 static void | |
| 1171 com_expr_stmt(c, n) | |
| 1172 struct compiling *c; | |
| 1173 node *n; | |
| 1174 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1175 REQ(n, expr_stmt); /* exprlist ('=' exprlist)* */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1176 com_node(c, CHILD(n, NCH(n)-1)); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1177 if (NCH(n) == 1) { |
| 69 | 1178 com_addbyte(c, PRINT_EXPR); |
| 1179 } | |
| 1180 else { | |
| 1181 int i; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1182 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
|
1183 if (i+2 < NCH(n)-2) |
| 69 | 1184 com_addbyte(c, DUP_TOP); |
| 1185 com_assign(c, CHILD(n, i), 1/*assign*/); | |
| 1186 } | |
| 1187 } | |
| 1188 } | |
| 1189 | |
| 1190 static void | |
| 1191 com_print_stmt(c, n) | |
| 1192 struct compiling *c; | |
| 1193 node *n; | |
| 1194 { | |
| 1195 int i; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1196 REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1197 for (i = 1; i < NCH(n); i += 2) { |
| 69 | 1198 com_node(c, CHILD(n, i)); |
| 1199 com_addbyte(c, PRINT_ITEM); | |
| 1200 } | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1201 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
| 69 | 1202 com_addbyte(c, PRINT_NEWLINE); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1203 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
| 69 | 1204 } |
| 1205 | |
| 1206 static void | |
| 1207 com_return_stmt(c, n) | |
| 1208 struct compiling *c; | |
| 1209 node *n; | |
| 1210 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1211 REQ(n, return_stmt); /* 'return' [testlist] */ |
| 86 | 1212 if (!c->c_infunction) { |
| 1213 err_setstr(TypeError, "'return' outside function"); | |
| 1214 c->c_errors++; | |
| 1215 } | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1216 if (NCH(n) < 2) |
| 69 | 1217 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1218 else | |
| 1219 com_node(c, CHILD(n, 1)); | |
| 1220 com_addbyte(c, RETURN_VALUE); | |
| 1221 } | |
| 1222 | |
| 1223 static void | |
| 1224 com_raise_stmt(c, n) | |
| 1225 struct compiling *c; | |
| 1226 node *n; | |
| 1227 { | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1228 REQ(n, raise_stmt); /* 'raise' test [',' test] */ |
| 69 | 1229 com_node(c, CHILD(n, 1)); |
| 1230 if (NCH(n) > 3) | |
| 1231 com_node(c, CHILD(n, 3)); | |
| 1232 else | |
| 1233 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); | |
| 1234 com_addbyte(c, RAISE_EXCEPTION); | |
| 1235 } | |
| 1236 | |
| 1237 static void | |
| 1238 com_import_stmt(c, n) | |
| 1239 struct compiling *c; | |
| 1240 node *n; | |
| 1241 { | |
| 1242 int i; | |
| 1243 REQ(n, import_stmt); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1244 /* 'import' NAME (',' NAME)* | |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1245 'from' NAME 'import' ('*' | NAME (',' NAME)*) */ |
| 69 | 1246 if (STR(CHILD(n, 0))[0] == 'f') { |
| 1247 /* 'from' NAME 'import' ... */ | |
| 1248 REQ(CHILD(n, 1), NAME); | |
| 1249 com_addopname(c, IMPORT_NAME, CHILD(n, 1)); | |
| 1250 for (i = 3; i < NCH(n); i += 2) | |
| 1251 com_addopname(c, IMPORT_FROM, CHILD(n, i)); | |
| 1252 com_addbyte(c, POP_TOP); | |
| 1253 } | |
| 1254 else { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1255 /* 'import' ... */ |
| 69 | 1256 for (i = 1; i < NCH(n); i += 2) { |
| 1257 com_addopname(c, IMPORT_NAME, CHILD(n, i)); | |
| 1258 com_addopname(c, STORE_NAME, CHILD(n, i)); | |
| 1259 } | |
| 1260 } | |
| 1261 } | |
| 1262 | |
| 1263 static void | |
| 1264 com_if_stmt(c, n) | |
| 1265 struct compiling *c; | |
| 1266 node *n; | |
| 1267 { | |
| 1268 int i; | |
| 1269 int anchor = 0; | |
| 1270 REQ(n, if_stmt); | |
| 1271 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ | |
| 1272 for (i = 0; i+3 < NCH(n); i+=4) { | |
| 1273 int a = 0; | |
| 86 | 1274 node *ch = CHILD(n, i+1); |
| 1275 if (i > 0) | |
| 1276 com_addoparg(c, SET_LINENO, ch->n_lineno); | |
| 69 | 1277 com_node(c, CHILD(n, i+1)); |
| 1278 com_addfwref(c, JUMP_IF_FALSE, &a); | |
| 1279 com_addbyte(c, POP_TOP); | |
| 1280 com_node(c, CHILD(n, i+3)); | |
| 1281 com_addfwref(c, JUMP_FORWARD, &anchor); | |
| 1282 com_backpatch(c, a); | |
| 1283 com_addbyte(c, POP_TOP); | |
| 1284 } | |
| 1285 if (i+2 < NCH(n)) | |
| 1286 com_node(c, CHILD(n, i+2)); | |
| 1287 com_backpatch(c, anchor); | |
| 1288 } | |
| 1289 | |
| 1290 static void | |
| 1291 com_while_stmt(c, n) | |
| 1292 struct compiling *c; | |
| 1293 node *n; | |
| 1294 { | |
| 1295 int break_anchor = 0; | |
| 1296 int anchor = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1297 int save_begin = c->c_begin; |
| 69 | 1298 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1299 com_addfwref(c, SETUP_LOOP, &break_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1300 block_push(c, SETUP_LOOP); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1301 c->c_begin = c->c_nexti; |
| 86 | 1302 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1303 com_node(c, CHILD(n, 1)); |
| 1304 com_addfwref(c, JUMP_IF_FALSE, &anchor); | |
| 1305 com_addbyte(c, POP_TOP); | |
| 86 | 1306 c->c_loops++; |
| 69 | 1307 com_node(c, CHILD(n, 3)); |
| 86 | 1308 c->c_loops--; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1309 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1310 c->c_begin = save_begin; |
| 69 | 1311 com_backpatch(c, anchor); |
| 1312 com_addbyte(c, POP_TOP); | |
| 1313 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1314 block_pop(c, SETUP_LOOP); |
| 69 | 1315 if (NCH(n) > 4) |
| 1316 com_node(c, CHILD(n, 6)); | |
| 1317 com_backpatch(c, break_anchor); | |
| 1318 } | |
| 1319 | |
| 1320 static void | |
| 1321 com_for_stmt(c, n) | |
| 1322 struct compiling *c; | |
| 1323 node *n; | |
| 1324 { | |
| 1325 object *v; | |
| 1326 int break_anchor = 0; | |
| 1327 int anchor = 0; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1328 int save_begin = c->c_begin; |
| 69 | 1329 REQ(n, for_stmt); |
| 1330 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ | |
| 1331 com_addfwref(c, SETUP_LOOP, &break_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1332 block_push(c, SETUP_LOOP); |
| 69 | 1333 com_node(c, CHILD(n, 3)); |
| 1334 v = newintobject(0L); | |
| 1335 if (v == NULL) | |
| 1336 c->c_errors++; | |
| 1337 com_addoparg(c, LOAD_CONST, com_addconst(c, v)); | |
| 1338 XDECREF(v); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1339 c->c_begin = c->c_nexti; |
| 86 | 1340 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1341 com_addfwref(c, FOR_LOOP, &anchor); |
| 1342 com_assign(c, CHILD(n, 1), 1/*assigning*/); | |
| 86 | 1343 c->c_loops++; |
| 69 | 1344 com_node(c, CHILD(n, 5)); |
| 86 | 1345 c->c_loops--; |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1346 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1347 c->c_begin = save_begin; |
| 69 | 1348 com_backpatch(c, anchor); |
| 1349 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1350 block_pop(c, SETUP_LOOP); |
| 69 | 1351 if (NCH(n) > 8) |
| 1352 com_node(c, CHILD(n, 8)); | |
| 1353 com_backpatch(c, break_anchor); | |
| 1354 } | |
| 1355 | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1356 /* Although 'execpt' and 'finally' clauses can be combined |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1357 syntactically, they are compiled separately. In fact, |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1358 try: S |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1359 except E1: S1 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1360 except E2: S2 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1361 ... |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1362 finally: Sf |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1363 is equivalent to |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1364 try: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1365 try: S |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1366 except E1: S1 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1367 except E2: S2 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1368 ... |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1369 finally: Sf |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1370 meaning that the 'finally' clause is entered even if things |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1371 go wrong again in an exception handler. Note that this is |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1372 not the case for exception handlers: at most one is entered. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1373 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1374 Code generated for "try: S finally: Sf" is as follows: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1375 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1376 SETUP_FINALLY L |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1377 <code for S> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1378 POP_BLOCK |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1379 LOAD_CONST <nil> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1380 L: <code for Sf> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1381 END_FINALLY |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1382 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1383 The special instructions use the block stack. Each block |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1384 stack entry contains the instruction that created it (here |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1385 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
|
1386 block stack entry was created, and a label (here L). |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1387 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1388 SETUP_FINALLY: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1389 Pushes the current value stack level and the label |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1390 onto the block stack. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1391 POP_BLOCK: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1392 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
|
1393 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
|
1394 block stack. (The label is ignored.) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1395 END_FINALLY: |
| 86 | 1396 Pops a variable number of entries from the *value* stack |
| 1397 and re-raises the exception they specify. The number of | |
| 1398 entries popped depends on the (pseudo) exception type. | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1399 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1400 The block stack is unwound when an exception is raised: |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1401 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
|
1402 onto the value stack (and the exception condition is cleared), |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1403 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
|
1404 stack. |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1405 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1406 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
| 86 | 1407 (The contents of the value stack is shown in [], with the top |
| 1408 at the right; 'tb' is trace-back info, 'val' the exception's | |
| 1409 associated value, and 'exc' the exception.) | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1410 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1411 Value stack Label Instruction Argument |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1412 [] SETUP_EXCEPT L1 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1413 [] <code for S> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1414 [] POP_BLOCK |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1415 [] JUMP_FORWARD L0 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1416 |
| 86 | 1417 [tb, val, exc] L1: DUP ) |
| 1418 [tb, val, exc, exc] <evaluate E1> ) | |
| 1419 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 | |
| 1420 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) | |
| 1421 [tb, val, exc, 1] POP ) | |
| 1422 [tb, val, exc] POP | |
| 1423 [tb, val] <assign to V1> (or POP if no V1) | |
| 1424 [tb] POP | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1425 [] <code for S1> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1426 JUMP_FORWARD L0 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1427 |
| 86 | 1428 [tb, val, exc, 0] L2: POP |
| 1429 [tb, val, exc] DUP | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1430 .............................etc....................... |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1431 |
| 86 | 1432 [tb, val, exc, 0] Ln+1: POP |
| 1433 [tb, val, exc] END_FINALLY # re-raise exception | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1434 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1435 [] L0: <next statement> |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1436 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1437 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
|
1438 */ |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1439 |
| 69 | 1440 static void |
| 1441 com_try_stmt(c, n) | |
| 1442 struct compiling *c; | |
| 1443 node *n; | |
| 1444 { | |
| 1445 int finally_anchor = 0; | |
| 1446 int except_anchor = 0; | |
| 1447 REQ(n, try_stmt); | |
| 1448 /* 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite] */ | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1449 |
| 69 | 1450 if (NCH(n) > 3 && TYPE(CHILD(n, NCH(n)-3)) != except_clause) { |
| 1451 /* Have a 'finally' clause */ | |
| 1452 com_addfwref(c, SETUP_FINALLY, &finally_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1453 block_push(c, SETUP_FINALLY); |
| 69 | 1454 } |
| 1455 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == except_clause) { | |
| 1456 /* Have an 'except' clause */ | |
| 1457 com_addfwref(c, SETUP_EXCEPT, &except_anchor); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1458 block_push(c, SETUP_EXCEPT); |
| 69 | 1459 } |
| 1460 com_node(c, CHILD(n, 2)); | |
| 1461 if (except_anchor) { | |
| 1462 int end_anchor = 0; | |
| 1463 int i; | |
| 1464 node *ch; | |
| 1465 com_addbyte(c, POP_BLOCK); | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1466 block_pop(c, SETUP_EXCEPT); |
| 69 | 1467 com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 1468 com_backpatch(c, except_anchor); | |
| 1469 for (i = 3; | |
| 1470 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; | |
| 1471 i += 3) { | |
| 1472 /* except_clause: 'except' [expr [',' expr]] */ | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1473 if (except_anchor == 0) { |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1474 err_setstr(TypeError, |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1475 "default 'except:' must be last"); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1476 c->c_errors++; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1477 break; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1478 } |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1479 except_anchor = 0; |
| 86 | 1480 com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 69 | 1481 if (NCH(ch) > 1) { |
| 1482 com_addbyte(c, DUP_TOP); | |
| 1483 com_node(c, CHILD(ch, 1)); | |
| 1484 com_addoparg(c, COMPARE_OP, EXC_MATCH); | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1485 com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 69 | 1486 com_addbyte(c, POP_TOP); |
| 1487 } | |
| 1488 com_addbyte(c, POP_TOP); | |
| 1489 if (NCH(ch) > 3) | |
| 1490 com_assign(c, CHILD(ch, 3), 1/*assigning*/); | |
| 1491 else | |
| 1492 com_addbyte(c, POP_TOP); | |
| 86 | 1493 com_addbyte(c, POP_TOP); |
| 69 | 1494 com_node(c, CHILD(n, i+2)); |
| 1495 com_addfwref(c, JUMP_FORWARD, &end_anchor); | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1496 if (except_anchor) { |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1497 com_backpatch(c, except_anchor); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1498 com_addbyte(c, POP_TOP); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1499 } |
| 69 | 1500 } |
| 1501 com_addbyte(c, END_FINALLY); | |
| 1502 com_backpatch(c, end_anchor); | |
| 1503 } | |
| 1504 if (finally_anchor) { | |
| 86 | 1505 node *ch; |
| 69 | 1506 com_addbyte(c, POP_BLOCK); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1507 block_pop(c, SETUP_FINALLY); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1508 block_push(c, END_FINALLY); |
| 69 | 1509 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1510 com_backpatch(c, finally_anchor); | |
| 86 | 1511 ch = CHILD(n, NCH(n)-1); |
| 1512 com_addoparg(c, SET_LINENO, ch->n_lineno); | |
| 1513 com_node(c, ch); | |
| 69 | 1514 com_addbyte(c, END_FINALLY); |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1515 block_pop(c, END_FINALLY); |
| 69 | 1516 } |
| 1517 } | |
| 1518 | |
| 1519 static void | |
| 1520 com_suite(c, n) | |
| 1521 struct compiling *c; | |
| 1522 node *n; | |
| 1523 { | |
| 1524 REQ(n, suite); | |
| 1525 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ | |
| 1526 if (NCH(n) == 1) { | |
| 1527 com_node(c, CHILD(n, 0)); | |
| 1528 } | |
| 1529 else { | |
| 1530 int i; | |
| 1531 for (i = 0; i < NCH(n); i++) { | |
| 1532 node *ch = CHILD(n, i); | |
| 1533 if (TYPE(ch) == stmt) | |
| 1534 com_node(c, ch); | |
| 1535 } | |
| 1536 } | |
| 1537 } | |
| 1538 | |
| 1539 static void | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1540 com_continue_stmt(c, n) |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1541 struct compiling *c; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1542 node *n; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1543 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1544 int i = c->c_nblocks; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1545 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
|
1546 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1547 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1548 else { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1549 err_setstr(TypeError, "'continue' not properly in loop"); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1550 c->c_errors++; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1551 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1552 /* XXX Could allow it inside a 'finally' clause |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1553 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
|
1554 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1555 |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1556 static void |
| 69 | 1557 com_funcdef(c, n) |
| 1558 struct compiling *c; | |
| 1559 node *n; | |
| 1560 { | |
| 1561 object *v; | |
| 1562 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ | |
| 86 | 1563 v = (object *)compile(n, c->c_filename); |
| 69 | 1564 if (v == NULL) |
| 1565 c->c_errors++; | |
| 1566 else { | |
| 1567 int i = com_addconst(c, v); | |
| 1568 com_addoparg(c, LOAD_CONST, i); | |
| 1569 com_addbyte(c, BUILD_FUNCTION); | |
| 1570 com_addopname(c, STORE_NAME, CHILD(n, 1)); | |
| 1571 DECREF(v); | |
| 1572 } | |
| 1573 } | |
| 1574 | |
| 1575 static void | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1576 com_bases(c, n) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1577 struct compiling *c; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1578 node *n; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1579 { |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1580 int i, nbases; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1581 REQ(n, baselist); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1582 /* |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1583 baselist: atom arguments (',' atom arguments)* |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1584 arguments: '(' [testlist] ')' |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1585 */ |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1586 for (i = 0; i < NCH(n); i += 3) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1587 com_node(c, CHILD(n, i)); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1588 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 3); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1589 } |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1590 |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1591 static void |
| 69 | 1592 com_classdef(c, n) |
| 1593 struct compiling *c; | |
| 1594 node *n; | |
| 1595 { | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1596 object *v; |
| 69 | 1597 REQ(n, classdef); |
| 1598 /* | |
| 1599 classdef: 'class' NAME parameters ['=' baselist] ':' suite | |
| 1600 baselist: atom arguments (',' atom arguments)* | |
| 1601 arguments: '(' [testlist] ')' | |
| 1602 */ | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1603 if (NCH(n) == 7) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1604 com_bases(c, CHILD(n, 4)); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1605 else |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1606 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 86 | 1607 v = (object *)compile(n, c->c_filename); |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1608 if (v == NULL) |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1609 c->c_errors++; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1610 else { |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1611 int i = com_addconst(c, v); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1612 com_addoparg(c, LOAD_CONST, i); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1613 com_addbyte(c, BUILD_FUNCTION); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1614 com_addbyte(c, UNARY_CALL); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1615 com_addbyte(c, BUILD_CLASS); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1616 com_addopname(c, STORE_NAME, CHILD(n, 1)); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1617 DECREF(v); |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1618 } |
| 69 | 1619 } |
| 1620 | |
| 1621 static void | |
| 1622 com_node(c, n) | |
| 1623 struct compiling *c; | |
| 1624 node *n; | |
| 1625 { | |
| 1626 switch (TYPE(n)) { | |
| 1627 | |
| 1628 /* Definition nodes */ | |
| 1629 | |
| 1630 case funcdef: | |
| 1631 com_funcdef(c, n); | |
| 1632 break; | |
| 1633 case classdef: | |
| 1634 com_classdef(c, n); | |
| 1635 break; | |
| 1636 | |
| 1637 /* Trivial parse tree nodes */ | |
| 1638 | |
| 1639 case stmt: | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1640 case small_stmt: |
| 86 | 1641 case flow_stmt: |
| 1642 com_node(c, CHILD(n, 0)); | |
| 1643 break; | |
| 1644 | |
| 69 | 1645 case simple_stmt: |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1646 /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1647 com_addoparg(c, SET_LINENO, n->n_lineno); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1648 { |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1649 int i; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1650 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
|
1651 com_node(c, CHILD(n, i)); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1652 } |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1653 break; |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1654 |
| 69 | 1655 case compound_stmt: |
| 86 | 1656 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 69 | 1657 com_node(c, CHILD(n, 0)); |
| 1658 break; | |
| 1659 | |
| 1660 /* Statement nodes */ | |
| 1661 | |
| 1662 case expr_stmt: | |
| 1663 com_expr_stmt(c, n); | |
| 1664 break; | |
| 1665 case print_stmt: | |
| 1666 com_print_stmt(c, n); | |
| 1667 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1668 case del_stmt: /* 'del' exprlist */ |
| 69 | 1669 com_assign(c, CHILD(n, 1), 0/*delete*/); |
| 1670 break; | |
| 1671 case pass_stmt: | |
| 1672 break; | |
| 1673 case break_stmt: | |
| 86 | 1674 if (c->c_loops == 0) { |
| 1675 err_setstr(TypeError, "'break' outside loop"); | |
| 1676 c->c_errors++; | |
| 1677 } | |
| 69 | 1678 com_addbyte(c, BREAK_LOOP); |
| 1679 break; | |
|
317
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1680 case continue_stmt: |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1681 com_continue_stmt(c, n); |
|
325b40d77235
New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents:
303
diff
changeset
|
1682 break; |
| 69 | 1683 case return_stmt: |
| 1684 com_return_stmt(c, n); | |
| 1685 break; | |
| 1686 case raise_stmt: | |
| 1687 com_raise_stmt(c, n); | |
| 1688 break; | |
| 1689 case import_stmt: | |
| 1690 com_import_stmt(c, n); | |
| 1691 break; | |
| 1692 case if_stmt: | |
| 1693 com_if_stmt(c, n); | |
| 1694 break; | |
| 1695 case while_stmt: | |
| 1696 com_while_stmt(c, n); | |
| 1697 break; | |
| 1698 case for_stmt: | |
| 1699 com_for_stmt(c, n); | |
| 1700 break; | |
| 1701 case try_stmt: | |
| 1702 com_try_stmt(c, n); | |
| 1703 break; | |
| 1704 case suite: | |
| 1705 com_suite(c, n); | |
| 1706 break; | |
| 1707 | |
| 1708 /* Expression nodes */ | |
| 1709 | |
| 1710 case testlist: | |
| 1711 com_list(c, n); | |
| 1712 break; | |
| 1713 case test: | |
| 1714 com_test(c, n); | |
| 1715 break; | |
| 1716 case and_test: | |
| 1717 com_and_test(c, n); | |
| 1718 break; | |
| 1719 case not_test: | |
| 1720 com_not_test(c, n); | |
| 1721 break; | |
| 1722 case comparison: | |
| 1723 com_comparison(c, n); | |
| 1724 break; | |
| 1725 case exprlist: | |
| 1726 com_list(c, n); | |
| 1727 break; | |
| 1728 case expr: | |
| 1729 com_expr(c, n); | |
| 1730 break; | |
| 1731 case term: | |
| 1732 com_term(c, n); | |
| 1733 break; | |
| 1734 case factor: | |
| 1735 com_factor(c, n); | |
| 1736 break; | |
| 1737 case atom: | |
| 1738 com_atom(c, n); | |
| 1739 break; | |
| 1740 | |
| 1741 default: | |
| 1742 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 1743 err_setstr(SystemError, "com_node: unexpected node type"); | |
| 1744 c->c_errors++; | |
| 1745 } | |
| 1746 } | |
| 1747 | |
| 1748 static void com_fplist PROTO((struct compiling *, node *)); | |
| 1749 | |
| 1750 static void | |
| 1751 com_fpdef(c, n) | |
| 1752 struct compiling *c; | |
| 1753 node *n; | |
| 1754 { | |
| 1755 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ | |
| 1756 if (TYPE(CHILD(n, 0)) == LPAR) | |
| 1757 com_fplist(c, CHILD(n, 1)); | |
| 1758 else | |
| 1759 com_addopname(c, STORE_NAME, CHILD(n, 0)); | |
| 1760 } | |
| 1761 | |
| 1762 static void | |
| 1763 com_fplist(c, n) | |
| 1764 struct compiling *c; | |
| 1765 node *n; | |
| 1766 { | |
| 1767 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* */ | |
| 1768 if (NCH(n) == 1) { | |
| 1769 com_fpdef(c, CHILD(n, 0)); | |
| 1770 } | |
| 1771 else { | |
| 1772 int i; | |
| 1773 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); | |
| 1774 for (i = 0; i < NCH(n); i += 2) | |
| 1775 com_fpdef(c, CHILD(n, i)); | |
| 1776 } | |
| 1777 } | |
| 1778 | |
| 1779 static void | |
| 1780 com_file_input(c, n) | |
| 1781 struct compiling *c; | |
| 1782 node *n; | |
| 1783 { | |
| 1784 int i; | |
| 1785 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ | |
| 1786 for (i = 0; i < NCH(n); i++) { | |
| 1787 node *ch = CHILD(n, i); | |
| 1788 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) | |
| 1789 com_node(c, ch); | |
| 1790 } | |
| 1791 } | |
| 1792 | |
| 1793 /* Top-level compile-node interface */ | |
| 1794 | |
| 1795 static void | |
| 1796 compile_funcdef(c, n) | |
| 1797 struct compiling *c; | |
| 1798 node *n; | |
| 1799 { | |
| 1800 node *ch; | |
| 1801 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ | |
| 1802 ch = CHILD(n, 2); /* parameters: '(' [fplist] ')' */ | |
| 1803 ch = CHILD(ch, 1); /* ')' | fplist */ | |
| 1804 if (TYPE(ch) == RPAR) | |
| 1805 com_addbyte(c, REFUSE_ARGS); | |
| 1806 else { | |
| 1807 com_addbyte(c, REQUIRE_ARGS); | |
| 1808 com_fplist(c, ch); | |
| 1809 } | |
| 86 | 1810 c->c_infunction = 1; |
| 69 | 1811 com_node(c, CHILD(n, 4)); |
| 86 | 1812 c->c_infunction = 0; |
| 69 | 1813 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1814 com_addbyte(c, RETURN_VALUE); | |
| 1815 } | |
| 1816 | |
| 1817 static void | |
| 1818 compile_node(c, n) | |
| 1819 struct compiling *c; | |
| 1820 node *n; | |
| 1821 { | |
| 86 | 1822 com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1823 | |
| 69 | 1824 switch (TYPE(n)) { |
| 1825 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1826 case single_input: /* One interactive command */ |
| 69 | 1827 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
| 86 | 1828 com_addbyte(c, REFUSE_ARGS); |
| 69 | 1829 n = CHILD(n, 0); |
| 1830 if (TYPE(n) != NEWLINE) | |
| 1831 com_node(c, n); | |
| 86 | 1832 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1833 com_addbyte(c, RETURN_VALUE); | |
| 69 | 1834 break; |
| 1835 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1836 case file_input: /* A whole file, or built-in function exec() */ |
| 86 | 1837 com_addbyte(c, REFUSE_ARGS); |
| 69 | 1838 com_file_input(c, n); |
| 86 | 1839 com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1840 com_addbyte(c, RETURN_VALUE); | |
| 69 | 1841 break; |
| 1842 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1843 case expr_input: /* Built-in function eval() */ |
| 86 | 1844 com_addbyte(c, REFUSE_ARGS); |
| 69 | 1845 com_node(c, CHILD(n, 0)); |
| 86 | 1846 com_addbyte(c, RETURN_VALUE); |
| 69 | 1847 break; |
| 1848 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1849 case eval_input: /* Built-in function input() */ |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1850 com_addbyte(c, REFUSE_ARGS); |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1851 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
|
1852 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
|
1853 break; |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1854 |
|
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1855 case funcdef: /* A function definition */ |
| 69 | 1856 compile_funcdef(c, n); |
| 1857 break; | |
| 1858 | |
|
120
c9a5fe1660f5
Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
1859 case classdef: /* A class definition */ |
| 86 | 1860 /* 'class' NAME parameters ['=' baselist] ':' suite */ |
| 1861 com_addbyte(c, REFUSE_ARGS); | |
| 1862 com_node(c, CHILD(n, NCH(n)-1)); | |
| 1863 com_addbyte(c, LOAD_LOCALS); | |
| 1864 com_addbyte(c, RETURN_VALUE); | |
|
73
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1865 break; |
|
3028acaa56b0
Compile class definitions.
Guido van Rossum <guido@python.org>
parents:
69
diff
changeset
|
1866 |
| 69 | 1867 default: |
| 1868 fprintf(stderr, "node type %d\n", TYPE(n)); | |
| 1869 err_setstr(SystemError, "compile_node: unexpected node type"); | |
| 1870 c->c_errors++; | |
| 1871 } | |
| 1872 } | |
| 1873 | |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1874 /* Optimization for local and global variables. |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1875 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1876 Attempt to replace all LOAD_NAME instructions that refer to a local |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1877 variable with LOAD_LOCAL instructions, and all that refer to a global |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1878 variable with LOAD_GLOBAL instructions. |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1879 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1880 To find all local variables, we check all STORE_NAME and IMPORT_FROM |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1881 instructions. This yields all local variables, including arguments, |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1882 function definitions, class definitions and import statements. |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1883 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1884 There is one leak: 'from foo import *' introduces local variables |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1885 that we can't know while compiling. If this is the case, LOAD_GLOBAL |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1886 instructions are not generated -- LOAD_NAME is left in place for |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1887 globals, since it first checks for globals (LOAD_LOCAL is still used |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1888 for recognized locals, since it doesn't hurt). |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1889 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1890 This optimization means that using the same name as a global and |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1891 as a local variable within the same scope is now illegal, which |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1892 is a change to the language! Also using eval() to introduce new |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1893 local variables won't work. But both were bad practice at best. |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1894 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1895 The optimization doesn't save much: basically, it saves one |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1896 unsuccessful dictionary lookup per global (or built-in) variable |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1897 reference. On the (slow!) Mac Plus, with 4 local variables, |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1898 this saving was measured to be about 0.18 ms. We might save more |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1899 by using a different data structure to hold local variables, like |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1900 an array indexed by variable number. |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1901 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1902 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
|
1903 */ |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1904 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1905 static void |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1906 optimizer(co) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1907 codeobject *co; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1908 { |
|
203
ceb36ec6198d
BUGFIX! Instructions are unsigned bytes.
Guido van Rossum <guido@python.org>
parents:
176
diff
changeset
|
1909 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
|
1910 object *locals; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1911 int opcode; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1912 int oparg; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1913 object *name; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1914 int star_used; |
|
203
ceb36ec6198d
BUGFIX! Instructions are unsigned bytes.
Guido van Rossum <guido@python.org>
parents:
176
diff
changeset
|
1915 |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1916 #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
|
1917 #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
|
1918 #define GETITEM(v, i) (getlistitem((v), (i))) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1919 #define GETNAMEOBJ(i) (GETITEM(co->co_names, (i))) |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1920 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1921 locals = newdictobject(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1922 if (locals == NULL) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1923 err_clear(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1924 return; /* For now, this is OK */ |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1925 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1926 |
|
203
ceb36ec6198d
BUGFIX! Instructions are unsigned bytes.
Guido van Rossum <guido@python.org>
parents:
176
diff
changeset
|
1927 next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1928 for (;;) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1929 opcode = NEXTOP(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1930 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
|
1931 break; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1932 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
|
1933 oparg = NEXTARG(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1934 if (opcode == STORE_NAME || opcode == IMPORT_FROM) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1935 name = GETNAMEOBJ(oparg); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1936 if (dict2insert(locals, name, None) != 0) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1937 DECREF(locals); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1938 return; /* Sorry */ |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1939 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1940 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1941 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1942 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1943 star_used = (dictlookup(locals, "*") != NULL); |
|
203
ceb36ec6198d
BUGFIX! Instructions are unsigned bytes.
Guido van Rossum <guido@python.org>
parents:
176
diff
changeset
|
1944 next_instr = (unsigned char *) GETSTRINGVALUE(co->co_code); |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1945 for (;;) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1946 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
|
1947 opcode = NEXTOP(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1948 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
|
1949 break; |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1950 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
|
1951 oparg = NEXTARG(); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1952 if (opcode == LOAD_NAME) { |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1953 name = GETNAMEOBJ(oparg); |
| 336 | 1954 if (dict2lookup(locals, name) != NULL) |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1955 *cur_instr = LOAD_LOCAL; |
| 336 | 1956 else { |
| 1957 err_clear(); | |
| 1958 if (!star_used) | |
| 1959 *cur_instr = LOAD_GLOBAL; | |
| 1960 } | |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1961 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1962 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1963 |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1964 DECREF(locals); |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1965 } |
|
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1966 |
| 69 | 1967 codeobject * |
| 86 | 1968 compile(n, filename) |
| 69 | 1969 node *n; |
| 86 | 1970 char *filename; |
| 69 | 1971 { |
| 1972 struct compiling sc; | |
| 1973 codeobject *co; | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
1974 object *v; |
| 86 | 1975 if (!com_init(&sc, filename)) |
| 69 | 1976 return NULL; |
| 1977 compile_node(&sc, n); | |
| 1978 com_done(&sc); | |
|
266
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
1979 if (sc.c_errors == 0 && (v = newstringobject(filename)) != NULL) { |
|
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
1980 co = newcodeobject(sc.c_code, sc.c_consts, sc.c_names, v); |
|
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
1981 DECREF(v); |
|
b5826bf29677
Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents:
238
diff
changeset
|
1982 } |
| 69 | 1983 else |
| 1984 co = NULL; | |
| 1985 com_free(&sc); | |
|
238
fd816ed17e35
Don't optimize <string> and <stdin> code
Guido van Rossum <guido@python.org>
parents:
226
diff
changeset
|
1986 if (co != NULL && filename[0] != '<') |
|
176
229168a2fe28
Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents:
160
diff
changeset
|
1987 optimizer(co); |
| 69 | 1988 return co; |
| 1989 } |
