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