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