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