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