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