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