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