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