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