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