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