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