annotate Python/compile.c @ 16408:ec709ee8e6fa legacy-trunk

add missing DECREF (thanks, Barry)
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 02 Feb 2001 02:58:48 +0000
parents 80c0b280a4f9
children aa844711463a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 120
diff changeset
1
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Compile an expression node to intermediate code */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4 /* XXX TO DO:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
5 XXX add __doc__ attribute == co_doc to code object attributes?
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
6 XXX (it's currently the first item of the co_const tuple)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
7 XXX Generate simple jump for break/return outside 'try...finally'
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
8 XXX Allow 'continue' inside finally clause of try-finally
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
9 XXX New opcode for loading the initial index for a for loop
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
10 XXX other JAR tricks?
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
11 */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
13 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
14
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #include "node.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #include "token.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #include "graminit.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #include "compile.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #include "opcode.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
20 #include "structmember.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
21
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
22 #include <ctype.h>
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
23
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
24 /* Three symbols from graminit.h are also defined in Python.h, with
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
25 Py_ prefixes to their names. Python.h can't include graminit.h
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
26 (which defines too many confusing symbols), but we can check here
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
27 that they haven't changed (which is very unlikely, but possible). */
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
28 #if Py_single_input != single_input
5566
19d4a2874645 Indent the #error directives so a classic K&R cpp doesn't see them.
Guido van Rossum <guido@python.org>
parents: 5453
diff changeset
29 #error "single_input has changed -- update Py_single_input in Python.h"
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
30 #endif
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
31 #if Py_file_input != file_input
5566
19d4a2874645 Indent the #error directives so a classic K&R cpp doesn't see them.
Guido van Rossum <guido@python.org>
parents: 5453
diff changeset
32 #error "file_input has changed -- update Py_file_input in Python.h"
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
33 #endif
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
34 #if Py_eval_input != eval_input
5566
19d4a2874645 Indent the #error directives so a classic K&R cpp doesn't see them.
Guido van Rossum <guido@python.org>
parents: 5453
diff changeset
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
36 #endif
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
37
5164
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
38 int Py_OptimizeFlag = 0;
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
39
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
40 #define OP_DELETE 0
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
41 #define OP_ASSIGN 1
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
42 #define OP_APPLY 2
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
43
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
44 #define VAR_LOAD 0
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
45 #define VAR_STORE 1
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
46 #define VAR_DELETE 2
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
47
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
48 #define TYPE_FUNCTION 1
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
49 #define TYPE_CLASS 2
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
50 #define TYPE_MODULE 3
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
51
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
52 #define LOCAL 1
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
53 #define GLOBAL_EXPLICIT 2
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
54 #define GLOBAL_IMPLICIT 3
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
55 #define FREE 4
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
56 #define CELL 5
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
57
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
58 #define DEL_CLOSURE_ERROR \
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
59 "can not delete variable '%.400s' referenced in nested scope"
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
60
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
61 #define DUPLICATE_ARGUMENT \
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
62 "duplicate argument '%s' in function definition"
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
63
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
64 #define ILLEGAL_IMPORT_STAR \
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
65 "'from ... import *' may only occur in a module scope"
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
66
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
67 #define MANGLE_LEN 256
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
68
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
69 #define OFF(x) offsetof(PyCodeObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
70
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
71 static struct memberlist code_memberlist[] = {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
72 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
73 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
74 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
75 {"co_flags", T_INT, OFF(co_flags), READONLY},
568
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
76 {"co_code", T_OBJECT, OFF(co_code), READONLY},
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
77 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
78 {"co_names", T_OBJECT, OFF(co_names), READONLY},
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
79 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
80 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
81 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
568
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
82 {"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
83 {"co_name", T_OBJECT, OFF(co_name), READONLY},
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
84 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
85 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
86 {NULL} /* Sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
87 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
88
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
89 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
90 code_getattr(PyCodeObject *co, char *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
91 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
92 return PyMember_Get((char *)co, code_memberlist, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
93 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
96 code_dealloc(PyCodeObject *co)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
98 Py_XDECREF(co->co_code);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
99 Py_XDECREF(co->co_consts);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
100 Py_XDECREF(co->co_names);
5901
ba49a6ef7214 Plug a leak in code_dealloc() (and reordered the deallocs to match the
Guido van Rossum <guido@python.org>
parents: 5754
diff changeset
101 Py_XDECREF(co->co_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
102 Py_XDECREF(co->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
103 Py_XDECREF(co->co_cellvars);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
104 Py_XDECREF(co->co_filename);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
105 Py_XDECREF(co->co_name);
5901
ba49a6ef7214 Plug a leak in code_dealloc() (and reordered the deallocs to match the
Guido van Rossum <guido@python.org>
parents: 5754
diff changeset
106 Py_XDECREF(co->co_lnotab);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12538
diff changeset
107 PyObject_DEL(co);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
110 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
111 code_repr(PyCodeObject *co)
1029
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
112 {
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
113 char buf[500];
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
114 int lineno = -1;
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
115 char *filename = "???";
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
116 char *name = "???";
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
117
12278
647bf71912f0 Vladimir Marangozov: This fixes the line number in the string
Guido van Rossum <guido@python.org>
parents: 12086
diff changeset
118 if (co->co_firstlineno != 0)
647bf71912f0 Vladimir Marangozov: This fixes the line number in the string
Guido van Rossum <guido@python.org>
parents: 12086
diff changeset
119 lineno = co->co_firstlineno;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
120 if (co->co_filename && PyString_Check(co->co_filename))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
121 filename = PyString_AS_STRING(co->co_filename);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
122 if (co->co_name && PyString_Check(co->co_name))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
123 name = PyString_AS_STRING(co->co_name);
13227
c2df5d28034d Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13096
diff changeset
124 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
c2df5d28034d Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13096
diff changeset
125 name, co, filename, lineno);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
126 return PyString_FromString(buf);
1029
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
127 }
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
128
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
129 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
130 code_compare(PyCodeObject *co, PyCodeObject *cp)
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
131 {
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
132 int cmp;
12333
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
133 cmp = PyObject_Compare(co->co_name, cp->co_name);
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
134 if (cmp) return cmp;
6186
2e755d1ff661 Cprrect stuoid tyops -- was comparing variabes with themselves because
Guido van Rossum <guido@python.org>
parents: 5901
diff changeset
135 cmp = co->co_argcount - cp->co_argcount;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
136 if (cmp) return cmp;
6186
2e755d1ff661 Cprrect stuoid tyops -- was comparing variabes with themselves because
Guido van Rossum <guido@python.org>
parents: 5901
diff changeset
137 cmp = co->co_nlocals - cp->co_nlocals;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
138 if (cmp) return cmp;
6186
2e755d1ff661 Cprrect stuoid tyops -- was comparing variabes with themselves because
Guido van Rossum <guido@python.org>
parents: 5901
diff changeset
139 cmp = co->co_flags - cp->co_flags;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
140 if (cmp) return cmp;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
141 cmp = PyObject_Compare(co->co_code, cp->co_code);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
142 if (cmp) return cmp;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
143 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
144 if (cmp) return cmp;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
145 cmp = PyObject_Compare(co->co_names, cp->co_names);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
146 if (cmp) return cmp;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
147 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
148 if (cmp) return cmp;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
149 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
150 if (cmp) return cmp;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
151 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
152 return cmp;
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
153 }
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
154
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
155 static long
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
156 code_hash(PyCodeObject *co)
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
157 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
158 long h, h0, h1, h2, h3, h4, h5, h6;
12333
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
159 h0 = PyObject_Hash(co->co_name);
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
160 if (h0 == -1) return -1;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
161 h1 = PyObject_Hash(co->co_code);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
162 if (h1 == -1) return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
163 h2 = PyObject_Hash(co->co_consts);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
164 if (h2 == -1) return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
165 h3 = PyObject_Hash(co->co_names);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
166 if (h3 == -1) return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
167 h4 = PyObject_Hash(co->co_varnames);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
168 if (h4 == -1) return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
169 h5 = PyObject_Hash(co->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
170 if (h5 == -1) return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
171 h6 = PyObject_Hash(co->co_cellvars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
172 if (h6 == -1) return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
173 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
174 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
175 if (h == -1) h = -2;
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
176 return h;
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
177 }
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
178
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
179 PyTypeObject PyCode_Type = {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
180 PyObject_HEAD_INIT(&PyType_Type)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 0,
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 "code",
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
183 sizeof(PyCodeObject),
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 0,
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
185 (destructor)code_dealloc, /*tp_dealloc*/
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
186 0, /*tp_print*/
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
187 (getattrfunc)code_getattr, /*tp_getattr*/
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 0, /*tp_setattr*/
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
189 (cmpfunc)code_compare, /*tp_compare*/
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
190 (reprfunc)code_repr, /*tp_repr*/
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 0, /*tp_as_number*/
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 0, /*tp_as_sequence*/
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 0, /*tp_as_mapping*/
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
194 (hashfunc)code_hash, /*tp_hash*/
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 };
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
5287
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 5264
diff changeset
197 #define NAME_CHARS \
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 5264
diff changeset
198 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 5264
diff changeset
199
16006
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
200 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
201
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
202 static int
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
203 all_name_chars(unsigned char *s)
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
204 {
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
205 static char ok_name_char[256];
16060
596edf8129a0 SF patch #103336: Missing cast.
Tim Peters <tim.peters@gmail.com>
parents: 16015
diff changeset
206 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
16006
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
207
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
208 if (ok_name_char[*name_chars] == 0) {
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
209 unsigned char *p;
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
210 for (p = name_chars; *p; p++)
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
211 ok_name_char[*p] = 1;
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
212 }
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
213 while (*s) {
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
214 if (ok_name_char[*s++] == 0)
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
215 return 0;
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
216 }
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
217 return 1;
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
218 }
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
219
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
220 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
221 intern_strings(PyObject *tuple)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
222 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
223 int i;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
224
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
225 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
226 PyObject *v = PyTuple_GET_ITEM(tuple, i);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
227 if (v == NULL || !PyString_Check(v)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
228 Py_FatalError("non-string found in code slot");
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
229 PyErr_BadInternalCall();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
230 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
231 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
232 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
233 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
234 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
235 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
236
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
237 PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
238 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
239 PyObject *code, PyObject *consts, PyObject *names,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
240 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
241 PyObject *filename, PyObject *name, int firstlineno,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
242 PyObject *lnotab)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
244 PyCodeObject *co;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 int i;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
246 PyBufferProcs *pb;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 /* Check argument types */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
248 if (argcount < 0 || nlocals < 0 ||
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
249 code == NULL ||
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
250 consts == NULL || !PyTuple_Check(consts) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
251 names == NULL || !PyTuple_Check(names) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
252 varnames == NULL || !PyTuple_Check(varnames) ||
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
253 freevars == NULL || !PyTuple_Check(freevars) ||
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
254 cellvars == NULL || !PyTuple_Check(cellvars) ||
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
255 name == NULL || !PyString_Check(name) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
256 filename == NULL || !PyString_Check(filename) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
257 lnotab == NULL || !PyString_Check(lnotab)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
258 PyErr_BadInternalCall();
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 return NULL;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 }
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
261 pb = code->ob_type->tp_as_buffer;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
262 if (pb == NULL ||
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
263 pb->bf_getreadbuffer == NULL ||
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
264 pb->bf_getsegcount == NULL ||
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
265 (*pb->bf_getsegcount)(code, NULL) != 1)
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
266 {
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
267 PyErr_BadInternalCall();
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
268 return NULL;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
269 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
270 intern_strings(names);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
271 intern_strings(varnames);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
272 if (freevars == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
273 freevars = PyTuple_New(0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
274 intern_strings(freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
275 if (cellvars == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
276 cellvars = PyTuple_New(0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
277 intern_strings(cellvars);
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
278 /* Intern selected string constants */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
279 for (i = PyTuple_Size(consts); --i >= 0; ) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
280 PyObject *v = PyTuple_GetItem(consts, i);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
281 if (!PyString_Check(v))
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
282 continue;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
283 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
284 continue;
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
285 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
287 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 if (co != NULL) {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
289 co->co_argcount = argcount;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
290 co->co_nlocals = nlocals;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
291 co->co_stacksize = stacksize;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
292 co->co_flags = flags;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
293 Py_INCREF(code);
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
294 co->co_code = code;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
295 Py_INCREF(consts);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 co->co_consts = consts;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
297 Py_INCREF(names);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 co->co_names = names;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
299 Py_INCREF(varnames);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
300 co->co_varnames = varnames;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
301 Py_INCREF(freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
302 co->co_freevars = freevars;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
303 Py_INCREF(cellvars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
304 co->co_cellvars = cellvars;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
305 Py_INCREF(filename);
266
b5826bf29677 Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents: 238
diff changeset
306 co->co_filename = filename;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
307 Py_INCREF(name);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
308 co->co_name = name;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
309 co->co_firstlineno = firstlineno;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
310 Py_INCREF(lnotab);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
311 co->co_lnotab = lnotab;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
312 /* PyObject_Print((PyObject *)co, stderr, 0); */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 return co;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 /* Data structure used internally */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
319
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
320 /* The compiler uses two passes to generate bytecodes. The first pass
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
321 builds the symbol table. The second pass generates the bytecode.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
322
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
323 The first pass uses a single symtable struct. The second pass uses
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
324 a compiling struct for each code block. The compiling structs
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
325 share a reference to the symtable.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
326
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
327 The two passes communicate via symtable_load_symbols() and via
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
328 is_local() and is_global(). The former initializes several slots
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
329 in the compiling struct: c_varnames, c_locals, c_nlocals,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
330 c_argcount, c_globals, and c_flags.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
331 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
332
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 struct compiling {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
334 PyObject *c_code; /* string */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
335 PyObject *c_consts; /* list of objects */
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
336 PyObject *c_const_dict; /* inverse of c_consts */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
337 PyObject *c_names; /* list of strings (names) */
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
338 PyObject *c_name_dict; /* inverse of c_names */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
339 PyObject *c_globals; /* dictionary (value=None) */
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
340 PyObject *c_locals; /* dictionary (value=localID) */
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
341 PyObject *c_varnames; /* list (inverse of c_locals) */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
342 PyObject *c_freevars; /* dictionary (value=None) */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
343 PyObject *c_cellvars; /* list */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
344 int c_nlocals; /* index of next local */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
345 int c_argcount; /* number of top-level arguments */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
346 int c_flags; /* same as co_flags */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 int c_nexti; /* index into c_code */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 int c_errors; /* counts errors occurred */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
349 int c_infunction; /* set when compiling a function */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
350 int c_interactive; /* generating code for interactive command */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
351 int c_loops; /* counts nested loops */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
352 int c_begin; /* begin of current loop, for 'continue' */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
353 int c_block[CO_MAXBLOCKS]; /* stack of block types */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
354 int c_nblocks; /* current block stack level */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
355 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
356 char *c_name; /* name of object (e.g. function) */
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
357 int c_lineno; /* Current line number */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
358 int c_stacklevel; /* Current stack level */
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
359 int c_maxstacklevel; /* Maximum stack level */
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
360 int c_firstlineno;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
361 PyObject *c_lnotab; /* Table mapping address to line number */
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
362 int c_last_addr, c_last_line, c_lnotab_next;
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
363 char *c_private; /* for private name mangling */
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
364 int c_tmpname; /* temporary local name counter */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
365 int c_nested; /* Is block nested funcdef or lamdef? */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
366 int c_closure; /* Is nested w/freevars? */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
367 struct symtable *c_symtable; /* pointer to module symbol table */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 };
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
370 /* A symbol table is constructed each time PyNode_Compile() is
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
371 called. The table walks the entire parse tree and identifies each
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
372 use or definition of a variable.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
373
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
374 The symbol table contains a dictionary for each code block in a
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
375 module: The symbol dictionary for the block. They keys of these
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
376 dictionaries are the name of all variables used or defined in the
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
377 block; the integer values are used to store several flags,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
378 e.g. DEF_PARAM indicates that a variable is a parameter to a
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
379 function.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
380
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
381 The slots st_cur_XXX pointers always refer to the current code
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
382 block. The st_cur slot is the symbol dictionary. The st_cur_id
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
383 slot is the id is the key in st_symbols. The st_cur_name slot is
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
384 the name of the current scope. The st_cur_type slot is one of
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
385 TYPE_FUNCTION, TYPE_CLASS, or TYPE_MODULE. The st_cur_children is
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
386 a list of the ids of the current node's children.
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
387
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
388 The st_symbols slot is a dictionary that maps code block ids to
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
389 symbol dictionaries. The keys are generated by a counter that is
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
390 incremented each time a new code block is found. The counter is
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
391 identifies a specific scope, because both passes walk the parse
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
392 tree in the same order.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
393
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
394 The st_varnames slot is a dictionary that maps code block ids to
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
395 parameter lists. The st_global slot always refers to the symbol
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
396 dictionary for the module.
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
397
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
398 The st_children slot is a dictionary that maps ids to a list
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
399 containing the ids of its children.
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
400 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
401
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
402 struct symtable {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
403 int st_pass; /* pass == 1 or 2 */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
404 PyObject *st_symbols; /* dictionary of symbol tables */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
405 PyObject *st_varnames; /* dictionary of parameter lists */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
406 PyObject *st_stack; /* stack of namespace info */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
407 PyObject *st_children; /* dictionary (id=[ids]) */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
408 PyObject *st_cur; /* borrowed ref to dict in st_symbols */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
409 PyObject *st_cur_name; /* string, name of current scope */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
410 PyObject *st_cur_id; /* int id of current code block */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
411 PyObject *st_cur_children; /* ref to current children list */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
412 int st_cur_type; /* type of current scope */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
413 PyObject *st_global; /* borrowed ref to MODULE in st_symbols */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
414 int st_scopes; /* number of scopes */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
415 int st_errors; /* number of errors */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
416 char *st_private; /* name of current class or NULL */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
417 int st_tmpname; /* temporary name counter */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
418 int st_nested; /* bool (true if nested scope) */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
419 };
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
420
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
421 #define TOP "global"
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
422 #define NOOPT ".noopt"
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
423
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
424 /* Flags for def-use information */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
425
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
426 #define DEF_GLOBAL 1 /* global stmt */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
427 #define DEF_LOCAL 2 /* assignment in code block */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
428 #define DEF_PARAM 2<<1 /* formal parameter */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
429 #define USE 2<<2 /* name is used */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
430 #define DEF_STAR 2<<3 /* parameter is star arg */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
431 #define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
432 #define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
433 #define DEF_FREE 2<<6 /* name used by not defined in nested scope */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
434 #define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
435 #define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
436 #define DEF_IMPORT 2<<9 /* assignment occurred via import */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
437
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
438 int is_free(int v)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
439 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
440 if ((v & (USE | DEF_FREE))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
441 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
442 return 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
443 if (v & DEF_FREE_CLASS)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
444 return 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
445 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
446 }
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
447
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
448 /* Error message including line number */
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
449
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
450 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
451 com_error(struct compiling *c, PyObject *exc, char *msg)
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
452 {
13993
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
453 PyObject *v, *tb, *tmp;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
454 if (c == NULL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
455 /* Error occurred via symtable call to
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
456 is_constant_false */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
457 PyErr_SetString(exc, msg);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
458 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
459 }
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
460 c->c_errors++;
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
461 if (c->c_lineno <= 1) {
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
462 /* Unknown line number or single interactive command */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
463 PyErr_SetString(exc, msg);
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
464 return;
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
465 }
13993
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
466 v = PyString_FromString(msg);
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
467 if (v == NULL)
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
468 return; /* MemoryError, too bad */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
469 PyErr_SetObject(exc, v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
470 Py_DECREF(v);
13993
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
471
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
472 /* add attributes for the line number and filename for the error */
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
473 PyErr_Fetch(&exc, &v, &tb);
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
474 PyErr_NormalizeException(&exc, &v, &tb);
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
475 tmp = PyInt_FromLong(c->c_lineno);
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
476 if (tmp == NULL)
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
477 PyErr_Clear();
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
478 else {
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
479 if (PyObject_SetAttrString(v, "lineno", tmp))
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
480 PyErr_Clear();
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
481 Py_DECREF(tmp);
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
482 }
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
483 if (c->c_filename != NULL) {
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
484 tmp = PyString_FromString(c->c_filename);
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
485 if (tmp == NULL)
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
486 PyErr_Clear();
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
487 else {
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
488 if (PyObject_SetAttrString(v, "filename", tmp))
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
489 PyErr_Clear();
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
490 Py_DECREF(tmp);
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
491 }
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
492 }
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
493 PyErr_Restore(exc, v, tb);
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
494 }
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
495
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
496
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
497 /* Interface to the block stack */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
498
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
499 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
500 block_push(struct compiling *c, int type)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
501 {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
502 if (c->c_nblocks >= CO_MAXBLOCKS) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
503 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
504 "too many statically nested blocks");
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
505 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
506 else {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
507 c->c_block[c->c_nblocks++] = type;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
508 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
509 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
510
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
511 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
512 block_pop(struct compiling *c, int type)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
513 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
514 if (c->c_nblocks > 0)
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
515 c->c_nblocks--;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
516 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
517 com_error(c, PyExc_SystemError, "bad block pop");
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
518 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
519 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
520
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
521
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
522 /* Prototype forward declarations */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
523
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
524 static int com_init(struct compiling *, char *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
525 static void com_free(struct compiling *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
526 static void com_push(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
527 static void com_pop(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
528 static void com_done(struct compiling *);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
529 static void com_node(struct compiling *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
530 static void com_factor(struct compiling *, node *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
531 static void com_addbyte(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
532 static void com_addint(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
533 static void com_addoparg(struct compiling *, int, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
534 static void com_addfwref(struct compiling *, int, int *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
535 static void com_backpatch(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
536 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
537 static int com_addconst(struct compiling *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
538 static int com_addname(struct compiling *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
539 static void com_addopname(struct compiling *, int, node *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
540 static void com_list(struct compiling *, node *, int);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
541 static void com_list_iter(struct compiling *, node *, node *, char *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
542 static int com_argdefs(struct compiling *, node *);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
543 static void com_assign(struct compiling *, node *, int, node *);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
544 static void com_assign_name(struct compiling *, node *, int);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
545 static PyCodeObject *icompile(node *, struct compiling *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
546 static PyCodeObject *jcompile(node *, char *, struct compiling *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
547 static PyObject *parsestrplus(node *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
548 static PyObject *parsestr(char *);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
549 static node *get_rawdocstring(node *);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
551 static int get_ref_type(struct compiling *, char *);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
552
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
553 /* symtable operations */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
554 static int symtable_build(struct compiling *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
555 static int symtable_load_symbols(struct compiling *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
556 static struct symtable *symtable_init(void);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
557 static void symtable_free(struct symtable *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
558 static int symtable_enter_scope(struct symtable *, char *, int);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
559 static int symtable_exit_scope(struct symtable *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
560 static int symtable_update_cur(struct symtable *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
561 static int symtable_add_def(struct symtable *, char *, int);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
562 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
563
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
564 static void symtable_node(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
565 static void symtable_funcdef(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
566 static void symtable_default_args(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
567 static void symtable_params(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
568 static void symtable_params_fplist(struct symtable *, node *n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
569 static void symtable_global(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
570 static void symtable_import(struct symtable *, node *);
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
571 static void symtable_assign(struct symtable *, node *, int);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
572 static void symtable_list_comprehension(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
573
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
574 static int symtable_update_free_vars(struct symtable *);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
575 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
576 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
577
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
578 /* helper */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
579 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
580 do_pad(int pad)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
581 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
582 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
583 for (i = 0; i < pad; ++i)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
584 fprintf(stderr, " ");
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
585 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
586
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
587 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
588 dump(node *n, int pad, int depth)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
589 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
590 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
591 if (depth == 0)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
592 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
593 do_pad(pad);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
594 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
595 if (depth > 0)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
596 depth--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
597 for (i = 0; i < NCH(n); ++i)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
598 dump(CHILD(n, i), pad + 1, depth);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
599 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
600
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
601 #define DUMP(N) dump(N, 0, -1)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
602
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
604 com_init(struct compiling *c, char *filename)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
606 memset((void *)c, '\0', sizeof(struct compiling));
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
607 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
608 1000)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
609 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
610 if ((c->c_consts = PyList_New(0)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
611 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
612 if ((c->c_const_dict = PyDict_New()) == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
613 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
614 if ((c->c_names = PyList_New(0)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
615 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
616 if ((c->c_name_dict = PyDict_New()) == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
617 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
618 if ((c->c_locals = PyDict_New()) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
619 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
620 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
621 1000)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
622 goto fail;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
623 c->c_globals = NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
624 c->c_varnames = NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
625 c->c_freevars = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
626 c->c_cellvars = NULL;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
627 c->c_nlocals = 0;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
628 c->c_argcount = 0;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
629 c->c_flags = 0;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 c->c_nexti = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 c->c_errors = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
632 c->c_infunction = 0;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
633 c->c_interactive = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
634 c->c_loops = 0;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
635 c->c_begin = 0;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
636 c->c_nblocks = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
637 c->c_filename = filename;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
638 c->c_name = "?";
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
639 c->c_lineno = 0;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
640 c->c_stacklevel = 0;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
641 c->c_maxstacklevel = 0;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
642 c->c_firstlineno = 0;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
643 c->c_last_addr = 0;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
644 c->c_last_line = 0;
16096
fb67122e5c1e com_init(): My entry into the smallest patch possible category.
Barry Warsaw <barry@python.org>
parents: 16060
diff changeset
645 c->c_lnotab_next = 0;
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
646 c->c_tmpname = 0;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
647 c->c_nested = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
648 c->c_closure = 0;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
649 c->c_symtable = NULL;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 return 1;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
651
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
652 fail:
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
653 com_free(c);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 return 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
658 com_free(struct compiling *c)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
660 Py_XDECREF(c->c_code);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
661 Py_XDECREF(c->c_consts);
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
662 Py_XDECREF(c->c_const_dict);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
663 Py_XDECREF(c->c_names);
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
664 Py_XDECREF(c->c_name_dict);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
665 Py_XDECREF(c->c_globals);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
666 Py_XDECREF(c->c_locals);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
667 Py_XDECREF(c->c_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
668 Py_XDECREF(c->c_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
669 Py_XDECREF(c->c_cellvars);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
670 Py_XDECREF(c->c_lnotab);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
672
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
674 com_push(struct compiling *c, int n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
675 {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
676 c->c_stacklevel += n;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
677 if (c->c_stacklevel > c->c_maxstacklevel)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
678 c->c_maxstacklevel = c->c_stacklevel;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
679 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
680
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
681 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
682 com_pop(struct compiling *c, int n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
683 {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
684 if (c->c_stacklevel < n) {
9049
cbf72b2210ae Comment out the print statement about underflow. (This only seems to
Guido van Rossum <guido@python.org>
parents: 8328
diff changeset
685 /* fprintf(stderr,
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
686 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
687 c->c_filename, c->c_lineno,
9049
cbf72b2210ae Comment out the print statement about underflow. (This only seems to
Guido van Rossum <guido@python.org>
parents: 8328
diff changeset
688 c->c_nexti, c->c_stacklevel, n); */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
689 c->c_stacklevel = 0;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
690 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
691 else
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
692 c->c_stacklevel -= n;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
693 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
694
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
695 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
696 com_done(struct compiling *c)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 if (c->c_code != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
699 _PyString_Resize(&c->c_code, c->c_nexti);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
700 if (c->c_lnotab != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
701 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
705 com_addbyte(struct compiling *c, int byte)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 int len;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
708 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
709 assert(byte >= 0 && byte <= 255);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 if (byte < 0 || byte > 255) {
388
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
711 /*
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
712 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2005
diff changeset
713 fatal("com_addbyte: byte out of range");
388
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
714 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
715 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
716 "com_addbyte: byte out of range");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 if (c->c_code == NULL)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 return;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
720 len = PyString_Size(c->c_code);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 if (c->c_nexti >= len) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
722 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
727 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
731 com_addint(struct compiling *c, int x)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 {
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
733 com_addbyte(c, x & 0xff);
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
734 com_addbyte(c, x >> 8); /* XXX x should be positive */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
738 com_add_lnotab(struct compiling *c, int addr, int line)
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
739 {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
740 int size;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
741 char *p;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
742 if (c->c_lnotab == NULL)
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
743 return;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
744 size = PyString_Size(c->c_lnotab);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
745 if (c->c_lnotab_next+2 > size) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
746 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
747 c->c_errors++;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
748 return;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
749 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
750 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
751 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
752 *p++ = addr;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
753 *p++ = line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
754 c->c_lnotab_next += 2;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
755 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
756
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
757 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
758 com_set_lineno(struct compiling *c, int lineno)
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
759 {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
760 c->c_lineno = lineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
761 if (c->c_firstlineno == 0) {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
762 c->c_firstlineno = c->c_last_line = lineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
763 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
764 else {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
765 int incr_addr = c->c_nexti - c->c_last_addr;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
766 int incr_line = lineno - c->c_last_line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
767 while (incr_addr > 0 || incr_line > 0) {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
768 int trunc_addr = incr_addr;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
769 int trunc_line = incr_line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
770 if (trunc_addr > 255)
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
771 trunc_addr = 255;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
772 if (trunc_line > 255)
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
773 trunc_line = 255;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
774 com_add_lnotab(c, trunc_addr, trunc_line);
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
775 incr_addr -= trunc_addr;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
776 incr_line -= trunc_line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
777 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
778 c->c_last_addr = c->c_nexti;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
779 c->c_last_line = lineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
780 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
781 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
782
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
783 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
784 com_addoparg(struct compiling *c, int op, int arg)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 {
14147
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
786 int extended_arg = arg >> 16;
5164
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
787 if (op == SET_LINENO) {
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
788 com_set_lineno(c, arg);
5164
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
789 if (Py_OptimizeFlag)
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
790 return;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
791 }
14147
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
792 if (extended_arg){
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
793 com_addbyte(c, EXTENDED_ARG);
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
794 com_addint(c, extended_arg);
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
795 arg &= 0xffff;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
796 }
5164
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
797 com_addbyte(c, op);
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
798 com_addint(c, arg);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
800
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
802 com_addfwref(struct compiling *c, int op, int *p_anchor)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 /* Compile a forward reference for backpatching */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 int here;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 int anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 com_addbyte(c, op);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 here = c->c_nexti;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 anchor = *p_anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 *p_anchor = here;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 com_addint(c, anchor == 0 ? 0 : here - anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
815 com_backpatch(struct compiling *c, int anchor)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
817 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 int target = c->c_nexti;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 int dist;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 int prev;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 for (;;) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 /* Make the JUMP instruction at anchor point to target */
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
823 prev = code[anchor] + (code[anchor+1] << 8);
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
824 dist = target - (anchor+2);
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
825 code[anchor] = dist & 0xff;
14147
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
826 dist >>= 8;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
827 code[anchor+1] = dist;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
828 dist >>= 8;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
829 if (dist) {
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
830 com_error(c, PyExc_SystemError,
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
831 "com_backpatch: offset too large");
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
832 break;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
833 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 if (!prev)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 anchor -= prev;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
838 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
839
544
d0baa8300005 Minor changes.
Guido van Rossum <guido@python.org>
parents: 493
diff changeset
840 /* Handle literals and names uniformly */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
843 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
845 PyObject *w, *t, *np=NULL;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
846 long n;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
847
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
848 t = Py_BuildValue("(OO)", v, v->ob_type);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
849 if (t == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
850 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
851 w = PyDict_GetItem(dict, t);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
852 if (w != NULL) {
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
853 n = PyInt_AsLong(w);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
854 } else {
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
855 n = PyList_Size(list);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
856 np = PyInt_FromLong(n);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
857 if (np == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
858 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
859 if (PyList_Append(list, v) != 0)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
860 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
861 if (PyDict_SetItem(dict, t, np) != 0)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
862 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
863 Py_DECREF(np);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
864 }
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
865 Py_DECREF(t);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
866 return n;
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
867 fail:
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
868 Py_XDECREF(np);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
869 Py_XDECREF(t);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
870 c->c_errors++;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
871 return 0;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
873
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
875 com_addconst(struct compiling *c, PyObject *v)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
877 return com_add(c, c->c_consts, c->c_const_dict, v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
879
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
881 com_addname(struct compiling *c, PyObject *v)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
882 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
883 return com_add(c, c->c_names, c->c_name_dict, v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
885
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
886 static int
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
887 mangle(char *p, char *name, char *buffer, size_t maxlen)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
888 {
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
889 /* Name mangling: __private becomes _classname__private.
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
890 This is independent from how the name is used. */
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
891 size_t nlen, plen;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
892 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
893 return 0;
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
894 nlen = strlen(name);
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
895 if (nlen+2 >= maxlen)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
896 return 0; /* Don't mangle __extremely_long_names */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
897 if (name[nlen-1] == '_' && name[nlen-2] == '_')
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
898 return 0; /* Don't mangle __whatever__ */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
899 /* Strip leading underscores from class name */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
900 while (*p == '_')
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
901 p++;
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
902 if (*p == '\0')
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
903 return 0; /* Don't mangle if class is just underscores */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
904 plen = strlen(p);
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
905 if (plen + nlen >= maxlen)
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
906 plen = maxlen-nlen-2; /* Truncate class name if too long */
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
907 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
908 buffer[0] = '_';
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
909 strncpy(buffer+1, p, plen);
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
910 strcpy(buffer+1+plen, name);
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
911 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
912 return 1;
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
913 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
914
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 static void
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
916 com_addop_name(struct compiling *c, int op, char *name)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
918 PyObject *v;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
919 int i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
920 char buffer[MANGLE_LEN];
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
921
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
922 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
923 name = buffer;
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
924 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 i = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
928 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
929 i = com_addname(c, v);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
930 Py_DECREF(v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
932 com_addoparg(c, op, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
933 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
934
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
935 #define NAME_LOCAL 0
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
936 #define NAME_GLOBAL 1
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
937 #define NAME_DEFAULT 2
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
938 #define NAME_CLOSURE 3
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
939
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
940 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
941 com_lookup_arg(PyObject *dict, PyObject *name)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
942 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
943 PyObject *v = PyDict_GetItem(dict, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
944 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
945 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
946 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
947 return PyInt_AS_LONG(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
948 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
949
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
950 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
951 com_addop_varname(struct compiling *c, int kind, char *name)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
952 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
953 PyObject *v;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
954 int i, reftype;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
955 int scope = NAME_DEFAULT;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
956 int op = STOP_CODE;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
957 char buffer[MANGLE_LEN];
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
958
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
959 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
960 name = buffer;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
961 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
962 c->c_errors++;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
963 i = 255;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
964 goto done;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
965 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
966
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
967 reftype = get_ref_type(c, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
968 switch (reftype) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
969 case LOCAL:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
970 if (c->c_symtable->st_cur_type == TYPE_FUNCTION)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
971 scope = NAME_LOCAL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
972 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
973 case GLOBAL_EXPLICIT:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
974 scope = NAME_GLOBAL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
975 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
976 case GLOBAL_IMPLICIT:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
977 if (c->c_flags & CO_OPTIMIZED)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
978 scope = NAME_GLOBAL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
979 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
980 case FREE:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
981 case CELL:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
982 scope = NAME_CLOSURE;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
983 break;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
984 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
985
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
986 i = com_addname(c, v);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
987 if (scope == NAME_LOCAL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
988 i = com_lookup_arg(c->c_locals, v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
989 else if (reftype == FREE)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
990 i = com_lookup_arg(c->c_freevars, v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
991 else if (reftype == CELL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
992 i = com_lookup_arg(c->c_cellvars, v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
993 if (i == -1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
994 c->c_errors++; /* XXX no exception set */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
995 i = 255;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
996 goto done;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
997 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
998 Py_DECREF(v);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
999
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1000 switch (kind) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1001 case VAR_LOAD:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1002 switch (scope) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1003 case NAME_LOCAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1004 op = LOAD_FAST;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1005 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1006 case NAME_GLOBAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1007 op = LOAD_GLOBAL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1008 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1009 case NAME_DEFAULT:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1010 op = LOAD_NAME;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1011 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1012 case NAME_CLOSURE:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1013 op = LOAD_DEREF;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1014 break;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1015 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1016 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1017 case VAR_STORE:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1018 switch (scope) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1019 case NAME_LOCAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1020 op = STORE_FAST;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1021 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1022 case NAME_GLOBAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1023 op = STORE_GLOBAL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1024 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1025 case NAME_DEFAULT:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1026 op = STORE_NAME;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1027 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1028 case NAME_CLOSURE:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1029 op = STORE_DEREF;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1030 break;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1031 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1032 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1033 case VAR_DELETE:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1034 switch (scope) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1035 case NAME_LOCAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1036 op = DELETE_FAST;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1037 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1038 case NAME_GLOBAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1039 op = DELETE_GLOBAL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1040 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1041 case NAME_DEFAULT:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1042 op = DELETE_NAME;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1043 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1044 case NAME_CLOSURE: {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
1045 char buf[500];
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1046 sprintf(buf, DEL_CLOSURE_ERROR, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1047 com_error(c, PyExc_SyntaxError, buf);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1048 i = 255;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1049 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1050 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1051 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1052 break;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1053 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1054 done:
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1055 /* fprintf(stderr, " addoparg(op=%d, arg=%d)\n", op, i);*/
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 com_addoparg(c, op, i);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1059 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1060 com_addopname(struct compiling *c, int op, node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1061 {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1062 char *name;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1063 char buffer[1000];
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1064 /* XXX it is possible to write this code without the 1000
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1065 chars on the total length of dotted names, I just can't be
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1066 bothered right now */
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1067 if (TYPE(n) == STAR)
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1068 name = "*";
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1069 else if (TYPE(n) == dotted_name) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1070 char *p = buffer;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1071 int i;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1072 name = buffer;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1073 for (i = 0; i < NCH(n); i += 2) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1074 char *s = STR(CHILD(n, i));
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1075 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1076 com_error(c, PyExc_MemoryError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1077 "dotted_name too long");
2367
915fabec64a3 fix typo (== for =) in assignment
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
1078 name = NULL;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1079 break;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1080 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1081 if (p != buffer)
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1082 *p++ = '.';
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1083 strcpy(p, s);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1084 p = strchr(p, '\0');
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1085 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1086 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1087 else {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1088 REQ(n, NAME);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1089 name = STR(n);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1090 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1091 com_addop_name(c, op, name);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1092 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1093
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1094 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1095 parsenumber(struct compiling *co, char *s)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 {
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1097 char *end;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1098 long x;
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1099 double dx;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1100 #ifndef WITHOUT_COMPLEX
3766
516490c8192c Py_complex; and WITHOUT_COMPLEX added to getargs.c
Guido van Rossum <guido@python.org>
parents: 3366
diff changeset
1101 Py_complex c;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1102 int imflag;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1103 #endif
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1104
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1105 errno = 0;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1106 end = s + strlen(s) - 1;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1107 #ifndef WITHOUT_COMPLEX
3366
c25865884649 only use 'j' for imaginary constants
Guido van Rossum <guido@python.org>
parents: 3349
diff changeset
1108 imflag = *end == 'j' || *end == 'J';
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1109 #endif
744
834dd4a77ee2 fix what lint found
Guido van Rossum <guido@python.org>
parents: 703
diff changeset
1110 if (*end == 'l' || *end == 'L')
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1111 return PyLong_FromString(s, (char **)0, 0);
617
755bb8efcc2d Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents: 584
diff changeset
1112 if (s[0] == '0')
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1113 x = (long) PyOS_strtoul(s, &end, 0);
617
755bb8efcc2d Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents: 584
diff changeset
1114 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1115 x = PyOS_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
1116 if (*end == '\0') {
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1117 if (errno != 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1118 com_error(co, PyExc_OverflowError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1119 "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
1120 return NULL;
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1121 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1122 return PyInt_FromLong(x);
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1123 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1124 /* XXX Huge floats may silently fail */
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1125 #ifndef WITHOUT_COMPLEX
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1126 if (imflag) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1127 c.real = 0.;
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1128 PyFPE_START_PROTECT("atof", return 0)
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1129 c.imag = atof(s);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5183
diff changeset
1130 PyFPE_END_PROTECT(c)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1131 return PyComplex_FromCComplex(c);
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1132 }
6453
c9b139b9c5ff Fixed for WITHOUT_COMPLEX compilation (Jack)
Guido van Rossum <guido@python.org>
parents: 6186
diff changeset
1133 else
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1134 #endif
6453
c9b139b9c5ff Fixed for WITHOUT_COMPLEX compilation (Jack)
Guido van Rossum <guido@python.org>
parents: 6186
diff changeset
1135 {
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1136 PyFPE_START_PROTECT("atof", return 0)
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1137 dx = atof(s);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5183
diff changeset
1138 PyFPE_END_PROTECT(dx)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1139 return PyFloat_FromDouble(dx);
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1140 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1143 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1144 parsestr(char *s)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1145 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1146 PyObject *v;
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1147 size_t len;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 char *buf;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 char *p;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1150 char *end;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 int c;
5264
38878f1cd4f1 Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents: 5233
diff changeset
1152 int first = *s;
38878f1cd4f1 Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents: 5233
diff changeset
1153 int quote = first;
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1154 int rawmode = 0;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1155 int unicode = 0;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1156 if (isalpha(quote) || quote == '_') {
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1157 if (quote == 'u' || quote == 'U') {
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1158 quote = *++s;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1159 unicode = 1;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1160 }
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1161 if (quote == 'r' || quote == 'R') {
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1162 quote = *++s;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1163 rawmode = 1;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1164 }
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1165 }
1364
132602d854d8 Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1166 if (quote != '\'' && quote != '\"') {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1167 PyErr_BadInternalCall();
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168 return NULL;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 s++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 len = strlen(s);
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1172 if (len > INT_MAX) {
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1173 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1174 return NULL;
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1175 }
1364
132602d854d8 Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1176 if (s[--len] != quote) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1177 PyErr_BadInternalCall();
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 return NULL;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1180 if (len >= 4 && s[0] == quote && s[1] == quote) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1181 s += 2;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1182 len -= 2;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1183 if (s[--len] != quote || s[--len] != quote) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1184 PyErr_BadInternalCall();
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1185 return NULL;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1186 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1187 }
12538
859e500f0760 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12522
diff changeset
1188 if (unicode || Py_UnicodeFlag) {
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1189 if (rawmode)
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1190 return PyUnicode_DecodeRawUnicodeEscape(
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1191 s, len, NULL);
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1192 else
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1193 return PyUnicode_DecodeUnicodeEscape(
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1194 s, len, NULL);
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1195 }
14382
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1196 if (rawmode || strchr(s, '\\') == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1197 return PyString_FromStringAndSize(s, len);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1198 v = PyString_FromStringAndSize((char *)NULL, len);
14382
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1199 if (v == NULL)
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1200 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1201 p = buf = PyString_AsString(v);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1202 end = s + len;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1203 while (s < end) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 if (*s != '\\') {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 *p++ = *s++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 continue;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208 s++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 switch (*s++) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 /* XXX This assumes ASCII! */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1211 case '\n': break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212 case '\\': *p++ = '\\'; break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213 case '\'': *p++ = '\''; break;
1364
132602d854d8 Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1214 case '\"': *p++ = '\"'; break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1215 case 'b': *p++ = '\b'; break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1216 case 'f': *p++ = '\014'; break; /* FF */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1217 case 't': *p++ = '\t'; break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 case 'n': *p++ = '\n'; break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 case 'r': *p++ = '\r'; break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220 case 'v': *p++ = '\013'; break; /* VT */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 case '0': case '1': case '2': case '3':
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 case '4': case '5': case '6': case '7':
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224 c = s[-1] - '0';
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 if ('0' <= *s && *s <= '7') {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 c = (c<<3) + *s++ - '0';
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227 if ('0' <= *s && *s <= '7')
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 c = (c<<3) + *s++ - '0';
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1230 *p++ = c;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232 case 'x':
14382
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1233 if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) {
6501
9ef9cadd978c Don't use sscanf(s, "%x", &c) to parse \xX... escapes; hardcode it.
Guido van Rossum <guido@python.org>
parents: 6453
diff changeset
1234 unsigned int x = 0;
14382
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1235 c = Py_CHARMASK(*s);
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1236 s++;
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1237 if (isdigit(c))
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1238 x = c - '0';
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1239 else if (islower(c))
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1240 x = 10 + c - 'a';
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1241 else
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1242 x = 10 + c - 'A';
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1243 x = x << 4;
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1244 c = Py_CHARMASK(*s);
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1245 s++;
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1246 if (isdigit(c))
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1247 x += c - '0';
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1248 else if (islower(c))
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1249 x += 10 + c - 'a';
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1250 else
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1251 x += 10 + c - 'A';
6501
9ef9cadd978c Don't use sscanf(s, "%x", &c) to parse \xX... escapes; hardcode it.
Guido van Rossum <guido@python.org>
parents: 6453
diff changeset
1252 *p++ = x;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1253 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 }
14382
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1255 PyErr_SetString(PyExc_ValueError, "invalid \\x escape");
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1256 Py_DECREF(v);
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1257 return NULL;
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1258 default:
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1259 *p++ = '\\';
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1260 *p++ = s[-1];
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
1261 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1264 _PyString_Resize(&v, (int)(p - buf));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265 return v;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1268 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1269 parsestrplus(node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1270 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1271 PyObject *v;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1272 int i;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1273 REQ(CHILD(n, 0), STRING);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1274 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1275 /* String literal concatenation */
12388
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1276 for (i = 1; i < NCH(n); i++) {
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1277 PyObject *s;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1278 s = parsestr(STR(CHILD(n, i)));
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1279 if (s == NULL)
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1280 goto onError;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1281 if (PyString_Check(v) && PyString_Check(s)) {
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1282 PyString_ConcatAndDel(&v, s);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1283 if (v == NULL)
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1284 goto onError;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1285 }
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1286 else {
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1287 PyObject *temp;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1288 temp = PyUnicode_Concat(v, s);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1289 Py_DECREF(s);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1290 if (temp == NULL)
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1291 goto onError;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1292 Py_DECREF(v);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1293 v = temp;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1294 }
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1295 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1296 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1297 return v;
12388
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1298
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1299 onError:
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1300 Py_XDECREF(v);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
1301 return NULL;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1302 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1303
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 static void
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1305 com_list_for(struct compiling *c, node *n, node *e, char *t)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1306 {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1307 PyObject *v;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1308 int anchor = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1309 int save_begin = c->c_begin;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1310
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1311 /* list_iter: for v in expr [list_iter] */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1312 com_node(c, CHILD(n, 3)); /* expr */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1313 v = PyInt_FromLong(0L);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1314 if (v == NULL)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1315 c->c_errors++;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1316 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1317 com_push(c, 1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1318 Py_XDECREF(v);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1319 c->c_begin = c->c_nexti;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1320 com_addoparg(c, SET_LINENO, n->n_lineno);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1321 com_addfwref(c, FOR_LOOP, &anchor);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1322 com_push(c, 1);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1323 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1324 c->c_loops++;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1325 com_list_iter(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1326 c->c_loops--;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1327 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1328 c->c_begin = save_begin;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1329 com_backpatch(c, anchor);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1330 com_pop(c, 2); /* FOR_LOOP has popped these */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1331 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1332
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1333 static void
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1334 com_list_if(struct compiling *c, node *n, node *e, char *t)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1335 {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1336 int anchor = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1337 int a = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1338 /* list_iter: 'if' test [list_iter] */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1339 com_addoparg(c, SET_LINENO, n->n_lineno);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1340 com_node(c, CHILD(n, 1));
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1341 com_addfwref(c, JUMP_IF_FALSE, &a);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1342 com_addbyte(c, POP_TOP);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1343 com_pop(c, 1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1344 com_list_iter(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1345 com_addfwref(c, JUMP_FORWARD, &anchor);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1346 com_backpatch(c, a);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1347 /* We jump here with an extra entry which we now pop */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1348 com_addbyte(c, POP_TOP);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1349 com_backpatch(c, anchor);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1350 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1351
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1352 static void
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1353 com_list_iter(struct compiling *c,
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1354 node *p, /* parent of list_iter node */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1355 node *e, /* element expression node */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1356 char *t /* name of result list temp local */)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1357 {
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1358 /* list_iter is the last child in a listmaker, list_for, or list_if */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1359 node *n = CHILD(p, NCH(p)-1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1360 if (TYPE(n) == list_iter) {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1361 n = CHILD(n, 0);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1362 switch (TYPE(n)) {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1363 case list_for:
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1364 com_list_for(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1365 break;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1366 case list_if:
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1367 com_list_if(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1368 break;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1369 default:
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1370 com_error(c, PyExc_SystemError,
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1371 "invalid list_iter node type");
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1372 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1373 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1374 else {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1375 com_addop_varname(c, VAR_LOAD, t);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1376 com_push(c, 1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1377 com_node(c, e);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1378 com_addoparg(c, CALL_FUNCTION, 1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1379 com_addbyte(c, POP_TOP);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1380 com_pop(c, 2);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1381 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1382 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1383
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1384 static void
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1385 com_list_comprehension(struct compiling *c, node *n)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1386 {
14117
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
1387 /* listmaker: test list_for */
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1388 char tmpname[12];
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1389 sprintf(tmpname, "[%d]", ++c->c_tmpname);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1390 com_addoparg(c, BUILD_LIST, 0);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1391 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1392 com_push(c, 2);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1393 com_addop_name(c, LOAD_ATTR, "append");
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1394 com_addop_varname(c, VAR_STORE, tmpname);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1395 com_pop(c, 1);
14117
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
1396 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1397 com_addop_varname(c, VAR_DELETE, tmpname);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1398 --c->c_tmpname;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1399 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1400
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1401 static void
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1402 com_listmaker(struct compiling *c, node *n)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1403 {
14117
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
1404 /* listmaker: test ( list_for | (',' test)* [','] ) */
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
1405 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1406 com_list_comprehension(c, n);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1407 else {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1408 int len = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1409 int i;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1410 for (i = 0; i < NCH(n); i += 2, len++)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1411 com_node(c, CHILD(n, i));
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1412 com_addoparg(c, BUILD_LIST, len);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1413 com_pop(c, len-1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1414 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1415 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1416
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1417 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1418 com_dictmaker(struct compiling *c, node *n)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1419 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1420 int i;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1421 /* dictmaker: test ':' test (',' test ':' value)* [','] */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1422 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
1423 /* 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
1424 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
1425 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1426 com_push(c, 1);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1427 com_node(c, CHILD(n, i+2)); /* value */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1428 com_addbyte(c, ROT_TWO);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1429 com_node(c, CHILD(n, i)); /* key */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1430 com_addbyte(c, STORE_SUBSCR);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1431 com_pop(c, 3);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1432 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1433 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1434
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1435 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1436 com_atom(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1438 node *ch;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1439 PyObject *v;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1440 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1441 REQ(n, atom);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1442 ch = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443 switch (TYPE(ch)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444 case LPAR:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1445 if (TYPE(CHILD(n, 1)) == RPAR) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1446 com_addoparg(c, BUILD_TUPLE, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1447 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1448 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1449 else
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1450 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1451 break;
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1452 case LSQB: /* '[' [listmaker] ']' */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1453 if (TYPE(CHILD(n, 1)) == RSQB) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1454 com_addoparg(c, BUILD_LIST, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1455 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1456 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1457 else
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1458 com_listmaker(c, CHILD(n, 1));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1459 break;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1460 case LBRACE: /* '{' [dictmaker] '}' */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1461 com_addoparg(c, BUILD_MAP, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1462 com_push(c, 1);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1463 if (TYPE(CHILD(n, 1)) == dictmaker)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1464 com_dictmaker(c, CHILD(n, 1));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1465 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1466 case BACKQUOTE:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1467 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1468 com_addbyte(c, UNARY_CONVERT);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1469 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1470 case NUMBER:
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1471 if ((v = parsenumber(c, STR(ch))) == NULL) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1472 i = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1473 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1474 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1475 i = com_addconst(c, v);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1476 Py_DECREF(v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1477 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1478 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1479 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1480 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1481 case STRING:
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1482 v = parsestrplus(n);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1483 if (v == NULL) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1484 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485 i = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1488 i = com_addconst(c, v);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1489 Py_DECREF(v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1490 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1491 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1492 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1493 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1494 case NAME:
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1495 com_addop_varname(c, VAR_LOAD, STR(ch));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1496 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1497 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1498 default:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1499 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1500 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1501 "com_atom: unexpected node type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1502 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1503 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1504
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1505 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1506 com_slice(struct compiling *c, node *n, int op)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1507 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1508 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1509 com_addbyte(c, op);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1511 else if (NCH(n) == 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1512 if (TYPE(CHILD(n, 0)) != COLON) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1513 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1514 com_addbyte(c, op+1);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1515 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1516 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1517 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1518 com_addbyte(c, op+2);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1519 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1520 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1521 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1522 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524 com_node(c, CHILD(n, 2));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1525 com_addbyte(c, op+3);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1526 com_pop(c, 2);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1528 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1530 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1531 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1532 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1533 if (NCH(n) == 1) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1534 com_addbyte(c, DUP_TOP);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1535 com_push(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1536 com_addbyte(c, SLICE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1537 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1538 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1539 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1540 com_addbyte(c, ROT_TWO);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1541 com_addbyte(c, STORE_SLICE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1542 com_pop(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1543 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1544 com_node(c, CHILD(n, 0));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1545 com_addoparg(c, DUP_TOPX, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1546 com_push(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1547 com_addbyte(c, SLICE+1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1548 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1549 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1550 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1551 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1552 com_addbyte(c, ROT_THREE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1553 com_addbyte(c, STORE_SLICE+1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1554 com_pop(c, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1555 } else if (NCH(n) == 2) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1556 com_node(c, CHILD(n, 1));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1557 com_addoparg(c, DUP_TOPX, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1558 com_push(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1559 com_addbyte(c, SLICE+2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1560 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1561 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1562 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1563 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1564 com_addbyte(c, ROT_THREE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1565 com_addbyte(c, STORE_SLICE+2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1566 com_pop(c, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1567 } else {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1568 com_node(c, CHILD(n, 0));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1569 com_node(c, CHILD(n, 2));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1570 com_addoparg(c, DUP_TOPX, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1571 com_push(c, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1572 com_addbyte(c, SLICE+3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1573 com_pop(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1574 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1575 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1576 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1577 com_addbyte(c, ROT_FOUR);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1578 com_addbyte(c, STORE_SLICE+3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1579 com_pop(c, 4);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1580 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1581 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1582
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1583 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1584 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1585 {
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1586 node *m;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1587 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1588 if (NCH(n) == 1) {
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1589 if (*pkeywords != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1590 com_error(c, PyExc_SyntaxError,
13993
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
1591 "non-keyword arg after keyword arg");
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1592 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1593 else {
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1594 com_node(c, CHILD(n, 0));
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1595 }
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1596 return;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1597 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1598 m = n;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1599 do {
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1600 m = CHILD(m, 0);
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1601 } while (NCH(m) == 1);
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1602 if (TYPE(m) != NAME) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1603 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1604 "keyword can't be an expression");
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1605 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1606 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1607 PyObject *v = PyString_InternFromString(STR(m));
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1608 if (v != NULL && *pkeywords == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1609 *pkeywords = PyDict_New();
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1610 if (v == NULL || *pkeywords == NULL)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1611 c->c_errors++;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1612 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1613 if (PyDict_GetItem(*pkeywords, v) != NULL)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1614 com_error(c, PyExc_SyntaxError,
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1615 "duplicate keyword argument");
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1616 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1617 if (PyDict_SetItem(*pkeywords, v, v) != 0)
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1618 c->c_errors++;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1619 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1620 com_push(c, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1621 Py_DECREF(v);
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1622 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1623 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1624 com_node(c, CHILD(n, 2));
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1625 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1626
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1627 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1628 com_call_function(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1629 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1630 if (TYPE(n) == RPAR) {
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1631 com_addoparg(c, CALL_FUNCTION, 0);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1632 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1633 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1634 PyObject *keywords = NULL;
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1635 int i, na, nk;
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1636 int lineno = n->n_lineno;
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1637 int star_flag = 0;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1638 int starstar_flag = 0;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1639 int opcode;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1640 REQ(n, arglist);
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1641 na = 0;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1642 nk = 0;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1643 for (i = 0; i < NCH(n); i += 2) {
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1644 node *ch = CHILD(n, i);
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1645 if (TYPE(ch) == STAR ||
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1646 TYPE(ch) == DOUBLESTAR)
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1647 break;
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1648 if (ch->n_lineno != lineno) {
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1649 lineno = ch->n_lineno;
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1650 com_addoparg(c, SET_LINENO, lineno);
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1651 }
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
1652 com_argument(c, ch, &keywords);
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1653 if (keywords == NULL)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1654 na++;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1655 else
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1656 nk++;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1657 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1658 Py_XDECREF(keywords);
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1659 while (i < NCH(n)) {
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1660 node *tok = CHILD(n, i);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1661 node *ch = CHILD(n, i+1);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1662 i += 3;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1663 switch (TYPE(tok)) {
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1664 case STAR: star_flag = 1; break;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1665 case DOUBLESTAR: starstar_flag = 1; break;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1666 }
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1667 com_node(c, ch);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1668 }
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1669 if (na > 255 || nk > 255) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1670 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1671 "more than 255 arguments");
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
1672 }
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1673 if (star_flag || starstar_flag)
12086
96ea1a800b23 remove reference (vestigal) to CALL_FUNCTION_STAR
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12082
diff changeset
1674 opcode = CALL_FUNCTION_VAR - 1 +
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1675 star_flag + (starstar_flag << 1);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1676 else
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1677 opcode = CALL_FUNCTION;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1678 com_addoparg(c, opcode, na | (nk << 8));
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
1679 com_pop(c, na + 2*nk + star_flag + starstar_flag);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1680 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1681 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1682
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1683 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1684 com_select_member(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1685 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1686 com_addopname(c, LOAD_ATTR, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1687 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1688
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1689 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1690 com_sliceobj(struct compiling *c, node *n)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1691 {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1692 int i=0;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1693 int ns=2; /* number of slice arguments */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1694 node *ch;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1695
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1696 /* first argument */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1697 if (TYPE(CHILD(n,i)) == COLON) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1698 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1699 com_push(c, 1);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1700 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1701 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1702 else {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1703 com_node(c, CHILD(n,i));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1704 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1705 REQ(CHILD(n,i),COLON);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1706 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1707 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1708 /* second argument */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1709 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1710 com_node(c, CHILD(n,i));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1711 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1712 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1713 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1714 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1715 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1716 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1717 /* remaining arguments */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1718 for (; i < NCH(n); i++) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1719 ns++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1720 ch=CHILD(n,i);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1721 REQ(ch, sliceop);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1722 if (NCH(ch) == 1) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1723 /* right argument of ':' missing */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1724 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1725 com_push(c, 1);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1726 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1727 else
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1728 com_node(c, CHILD(ch,1));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1729 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1730 com_addoparg(c, BUILD_SLICE, ns);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1731 com_pop(c, 1 + (ns == 3));
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1732 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1733
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1734 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1735 com_subscript(struct compiling *c, node *n)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1736 {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1737 node *ch;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1738 REQ(n, subscript);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1739 ch = CHILD(n,0);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1740 /* check for rubber index */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1741 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
4403
3b3f7c3f6da2 Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents: 4315
diff changeset
1742 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1743 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1744 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1745 else {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1746 /* check for slice */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1747 if ((TYPE(ch) == COLON || NCH(n) > 1))
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1748 com_sliceobj(c, n);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1749 else {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1750 REQ(ch, test);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1751 com_node(c, ch);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1752 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1753 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1754 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1755
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1756 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1757 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1758 {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1759 int i, op;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1760 REQ(n, subscriptlist);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1761 /* Check to make backward compatible slice behavior for '[i:j]' */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1762 if (NCH(n) == 1) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1763 node *sub = CHILD(n, 0); /* subscript */
13873
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1764 /* 'Basic' slice, should have exactly one colon. */
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1765 if ((TYPE(CHILD(sub, 0)) == COLON
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1766 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1767 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1768 {
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1769 switch (assigning) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1770 case OP_DELETE:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1771 op = DELETE_SLICE;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1772 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1773 case OP_ASSIGN:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1774 op = STORE_SLICE;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1775 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1776 case OP_APPLY:
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1777 op = SLICE;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1778 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1779 default:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1780 com_augassign_slice(c, sub, assigning, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1781 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1782 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1783 com_slice(c, sub, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1784 if (op == STORE_SLICE)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1785 com_pop(c, 2);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1786 else if (op == DELETE_SLICE)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1787 com_pop(c, 1);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1788 return;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1789 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1790 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1791 /* Else normal subscriptlist. Compile each subscript. */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1792 for (i = 0; i < NCH(n); i += 2)
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1793 com_subscript(c, CHILD(n, i));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1794 /* Put multiple subscripts into a tuple */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1795 if (NCH(n) > 1) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1796 i = (NCH(n)+1) / 2;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1797 com_addoparg(c, BUILD_TUPLE, i);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1798 com_pop(c, i-1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1799 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1800 switch (assigning) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1801 case OP_DELETE:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1802 op = DELETE_SUBSCR;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1803 i = 2;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1804 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1805 default:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1806 case OP_ASSIGN:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1807 op = STORE_SUBSCR;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1808 i = 3;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1809 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1810 case OP_APPLY:
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1811 op = BINARY_SUBSCR;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1812 i = 1;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1813 break;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1814 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1815 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1816 com_addoparg(c, DUP_TOPX, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1817 com_push(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1818 com_addbyte(c, BINARY_SUBSCR);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1819 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1820 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1821 com_addbyte(c, assigning);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1822 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1823 com_addbyte(c, ROT_THREE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1824 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1825 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1826 com_pop(c, i);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1827 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1828
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1829 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1830 com_apply_trailer(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1831 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1832 REQ(n, trailer);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1833 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1834 case LPAR:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1835 com_call_function(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1836 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1837 case DOT:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1838 com_select_member(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1839 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1840 case LSQB:
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1841 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1842 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1843 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1844 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1845 "com_apply_trailer: unknown trailer type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1846 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1847 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1848
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1849 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1850 com_power(struct compiling *c, node *n)
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1851 {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1852 int i;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1853 REQ(n, power);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1854 com_atom(c, CHILD(n, 0));
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1855 for (i = 1; i < NCH(n); i++) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1856 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1857 com_factor(c, CHILD(n, i+1));
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1858 com_addbyte(c, BINARY_POWER);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1859 com_pop(c, 1);
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1860 break;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1861 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1862 else
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1863 com_apply_trailer(c, CHILD(n, i));
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1864 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1865 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1866
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1867 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1868 com_factor(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1869 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1870 REQ(n, factor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1871 if (TYPE(CHILD(n, 0)) == PLUS) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1872 com_factor(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1873 com_addbyte(c, UNARY_POSITIVE);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1874 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1875 else if (TYPE(CHILD(n, 0)) == MINUS) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1876 com_factor(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1877 com_addbyte(c, UNARY_NEGATIVE);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1878 }
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1879 else if (TYPE(CHILD(n, 0)) == TILDE) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1880 com_factor(c, CHILD(n, 1));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1881 com_addbyte(c, UNARY_INVERT);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1882 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1883 else {
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1884 com_power(c, CHILD(n, 0));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1885 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1886 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1887
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1888 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1889 com_term(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1890 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1891 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1892 int op;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1893 REQ(n, term);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1894 com_factor(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1895 for (i = 2; i < NCH(n); i += 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1896 com_factor(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1897 switch (TYPE(CHILD(n, i-1))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1898 case STAR:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1899 op = BINARY_MULTIPLY;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1900 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1901 case SLASH:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1902 op = BINARY_DIVIDE;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1903 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1904 case PERCENT:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1905 op = BINARY_MODULO;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1906 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1907 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1908 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1909 "com_term: operator not *, / or %");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1910 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1911 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1912 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1913 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1914 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1915 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1916
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1917 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1918 com_arith_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1919 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1920 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1921 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1922 REQ(n, arith_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1923 com_term(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1924 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1925 com_term(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1926 switch (TYPE(CHILD(n, i-1))) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1927 case PLUS:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1928 op = BINARY_ADD;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1929 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1930 case MINUS:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1931 op = BINARY_SUBTRACT;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1932 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1933 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1934 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1935 "com_arith_expr: operator not + or -");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1936 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1937 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1938 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1939 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1940 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1941 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1942
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1943 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1944 com_shift_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1945 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1946 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1947 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1948 REQ(n, shift_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1949 com_arith_expr(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1950 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1951 com_arith_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1952 switch (TYPE(CHILD(n, i-1))) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1953 case LEFTSHIFT:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1954 op = BINARY_LSHIFT;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1955 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1956 case RIGHTSHIFT:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1957 op = BINARY_RSHIFT;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1958 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1959 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1960 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1961 "com_shift_expr: operator not << or >>");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1962 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1963 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1964 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1965 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1966 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1967 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1968
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1969 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1970 com_and_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1971 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1972 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1973 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1974 REQ(n, and_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1975 com_shift_expr(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1976 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1977 com_shift_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1978 if (TYPE(CHILD(n, i-1)) == AMPER) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1979 op = BINARY_AND;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1980 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1981 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1982 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1983 "com_and_expr: operator not &");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1984 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1985 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1986 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1987 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1988 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1989 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1990
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1991 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1992 com_xor_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1993 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1994 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1995 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1996 REQ(n, xor_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1997 com_and_expr(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1998 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
1999 com_and_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2000 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2001 op = BINARY_XOR;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2002 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2003 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2004 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2005 "com_xor_expr: operator not ^");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2006 op = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2007 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2008 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2009 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2010 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2011 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2012
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2013 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2014 com_expr(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2015 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2016 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2017 int op;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2018 REQ(n, expr);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2019 com_xor_expr(c, CHILD(n, 0));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2020 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
2021 com_xor_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2022 if (TYPE(CHILD(n, i-1)) == VBAR) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2023 op = BINARY_OR;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2024 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2025 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2026 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2027 "com_expr: expr operator not |");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2028 op = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2029 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2030 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2031 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2032 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2033 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2034
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2035 static enum cmp_op
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2036 cmp_type(node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2037 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2038 REQ(n, comp_op);
388
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
2039 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2040 | 'in' | 'not' 'in' | 'is' | 'is' not' */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2041 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2042 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2043 switch (TYPE(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2044 case LESS: return LT;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2045 case GREATER: return GT;
388
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
2046 case EQEQUAL: /* == */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2047 case EQUAL: return EQ;
388
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
2048 case LESSEQUAL: return LE;
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
2049 case GREATEREQUAL: return GE;
31d509e94271 Comment out 'abort()' call.
Guido van Rossum <guido@python.org>
parents: 336
diff changeset
2050 case NOTEQUAL: return NE; /* <> or != */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2051 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2052 if (strcmp(STR(n), "is") == 0) return IS;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2053 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2054 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2055 else if (NCH(n) == 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2056 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2057 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2058 return NOT_IN;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2059 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2060 return IS_NOT;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2061 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2062 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2063 return BAD;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2064 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2065
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2066 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2067 com_comparison(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2068 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2069 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2070 enum cmp_op op;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2071 int anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2072 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2073 com_expr(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2074 if (NCH(n) == 1)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2075 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2076
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2077 /****************************************************************
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2078 The following code is generated for all but the last
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2079 comparison in a chain:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2080
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2081 label: on stack: opcode: jump to:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2082
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2083 a <code to load b>
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2084 a, b DUP_TOP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2085 a, b, b ROT_THREE
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2086 b, a, b COMPARE_OP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2087 b, 0-or-1 JUMP_IF_FALSE L1
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2088 b, 1 POP_TOP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2089 b
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2090
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2091 We are now ready to repeat this sequence for the next
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2092 comparison in the chain.
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2093
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2094 For the last we generate:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2095
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2096 b <code to load c>
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2097 b, c COMPARE_OP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2098 0-or-1
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2099
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2100 If there were any jumps to L1 (i.e., there was more than one
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2101 comparison), we generate:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2102
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2103 0-or-1 JUMP_FORWARD L2
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2104 L1: b, 0 ROT_TWO
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2105 0, b POP_TOP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2106 0
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2107 L2: 0-or-1
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2108 ****************************************************************/
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2109
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2110 anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2111
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2112 for (i = 2; i < NCH(n); i += 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2113 com_expr(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2114 if (i+2 < NCH(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2115 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2116 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2117 com_addbyte(c, ROT_THREE);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2118 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2119 op = cmp_type(CHILD(n, i-1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2120 if (op == BAD) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2121 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2122 "com_comparison: unknown comparison op");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2123 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2124 com_addoparg(c, COMPARE_OP, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2125 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2126 if (i+2 < NCH(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2127 com_addfwref(c, JUMP_IF_FALSE, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2128 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2129 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2130 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2131 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2132
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2133 if (anchor) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2134 int anchor2 = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2135 com_addfwref(c, JUMP_FORWARD, &anchor2);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2136 com_backpatch(c, anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2137 com_addbyte(c, ROT_TWO);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2138 com_addbyte(c, POP_TOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2139 com_backpatch(c, anchor2);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2140 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2141 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2142
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2143 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2144 com_not_test(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2145 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2146 REQ(n, not_test); /* 'not' not_test | comparison */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2147 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2148 com_comparison(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2149 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2150 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2151 com_not_test(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2152 com_addbyte(c, UNARY_NOT);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2153 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2154 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2155
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2156 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2157 com_and_test(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2158 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2159 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2160 int anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2161 REQ(n, and_test); /* not_test ('and' not_test)* */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2162 anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2163 i = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2164 for (;;) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2165 com_not_test(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2166 if ((i += 2) >= NCH(n))
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2167 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2168 com_addfwref(c, JUMP_IF_FALSE, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2169 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2170 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2171 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2172 if (anchor)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2173 com_backpatch(c, anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2174 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2175
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2176 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2177 com_make_closure(struct compiling *c, PyCodeObject *co)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2178 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2179 int i, free = PyTuple_GET_SIZE(co->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2180 if (free == 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2181 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2182 for (i = 0; i < free; ++i) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2183 /* Bypass com_addop_varname because it will generate
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2184 LOAD_DEREF but LOAD_CLOSURE is needed.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2185 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2186 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2187 int arg, reftype;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2188
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2189 /* Special case: If a class contains a method with a
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2190 free variable that has the same name as a method,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2191 the name will be considered free *and* local in the
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2192 class. It should be handled by the closure, as
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2193 well as by the normal name loookup logic.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2194 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2195 reftype = get_ref_type(c, PyString_AS_STRING(name));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2196 if (reftype == CELL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2197 arg = com_lookup_arg(c->c_cellvars, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2198 else /* (reftype == FREE) */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2199 arg = com_lookup_arg(c->c_freevars, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2200 if (arg == -1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2201 fprintf(stderr, "lookup %s in %s %d %d\n",
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
2202 PyObject_REPR(name), c->c_name, reftype, arg);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2203 Py_FatalError("com_make_closure()");
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2204 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2205 com_addoparg(c, LOAD_CLOSURE, arg);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2206
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2207 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2208 com_push(c, free);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2209 return 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2210 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2211
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2212 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2213 com_test(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2214 {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2215 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2216 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2217 PyObject *co;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2218 int i, closure;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
2219 int ndefs = com_argdefs(c, CHILD(n, 0));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2220 symtable_enter_scope(c->c_symtable, "lambda", lambdef);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2221 co = (PyObject *) icompile(CHILD(n, 0), c);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2222 symtable_exit_scope(c->c_symtable);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2223 if (co == NULL) {
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2224 c->c_errors++;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2225 i = 255;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2226 closure = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2227 } else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2228 i = com_addconst(c, co);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2229 Py_DECREF(co);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2230 closure = com_make_closure(c, (PyCodeObject *)co);
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2231 }
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2232 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2233 com_push(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2234 if (closure)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2235 com_addoparg(c, MAKE_CLOSURE, ndefs);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2236 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2237 com_addoparg(c, MAKE_FUNCTION, ndefs);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2238 com_pop(c, ndefs);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2239 }
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2240 else {
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2241 int anchor = 0;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2242 int i = 0;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2243 for (;;) {
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2244 com_and_test(c, CHILD(n, i));
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2245 if ((i += 2) >= NCH(n))
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2246 break;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2247 com_addfwref(c, JUMP_IF_TRUE, &anchor);
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2248 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2249 com_pop(c, 1);
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2250 }
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2251 if (anchor)
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2252 com_backpatch(c, anchor);
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2253 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2254 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2255
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2256 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2257 com_list(struct compiling *c, node *n, int toplevel)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2258 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2259 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
482
ee899a81570d New argument passing.
Guido van Rossum <guido@python.org>
parents: 466
diff changeset
2260 if (NCH(n) == 1 && !toplevel) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2261 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2262 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2263 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2264 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2265 int len;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2266 len = (NCH(n) + 1) / 2;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2267 for (i = 0; i < NCH(n); i += 2)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2268 com_node(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2269 com_addoparg(c, BUILD_TUPLE, len);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2270 com_pop(c, len-1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2271 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2272 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2273
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2274
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2275 /* Begin of assignment compilation */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2276
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2277
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2278 static void
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2279 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2280 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2281 com_addbyte(c, DUP_TOP);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2282 com_push(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2283 com_addopname(c, LOAD_ATTR, n);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2284 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2285 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2286 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2287 com_addbyte(c, ROT_TWO);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2288 com_addopname(c, STORE_ATTR, n);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2289 com_pop(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2290 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2291
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2292 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2293 com_assign_attr(struct compiling *c, node *n, int assigning)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2294 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2295 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2296 com_pop(c, assigning ? 2 : 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2297 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2298
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2299 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2300 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2301 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2302 REQ(n, trailer);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2303 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2304 case LPAR: /* '(' [exprlist] ')' */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2305 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2306 "can't assign to function call");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2307 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2308 case DOT: /* '.' NAME */
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2309 if (assigning > OP_APPLY)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2310 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2311 else
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2312 com_assign_attr(c, CHILD(n, 1), assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2313 break;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2314 case LSQB: /* '[' subscriptlist ']' */
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2315 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2316 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2317 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2318 com_error(c, PyExc_SystemError, "unknown trailer type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2319 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2320 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2321
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2322 static void
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
2323 com_assign_sequence(struct compiling *c, node *n, int assigning)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2324 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2325 int i;
14178
dff22d4a48da Fix allowable node-types for assignment, need to add 'listmaker'.
Thomas Wouters <thomas@python.org>
parents: 14163
diff changeset
2326 if (TYPE(n) != testlist && TYPE(n) != listmaker)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2327 REQ(n, exprlist);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2328 if (assigning) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2329 i = (NCH(n)+1)/2;
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
2330 com_addoparg(c, UNPACK_SEQUENCE, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2331 com_push(c, i-1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2332 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2333 for (i = 0; i < NCH(n); i += 2)
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2334 com_assign(c, CHILD(n, i), assigning, NULL);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2335 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2336
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2337 static void
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2338 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2339 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2340 REQ(n, NAME);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2341 com_addop_varname(c, VAR_LOAD, STR(n));
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2342 com_push(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2343 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2344 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2345 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2346 com_assign_name(c, n, OP_ASSIGN);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2347 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2348
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2349 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2350 com_assign_name(struct compiling *c, node *n, int assigning)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2351 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2352 REQ(n, NAME);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2353 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2354 if (assigning)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2355 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2356 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2357
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2358 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2359 com_assign(struct compiling *c, node *n, int assigning, node *augn)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2360 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2361 /* Loop to avoid trivial recursion */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2362 for (;;) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2363 switch (TYPE(n)) {
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2364
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2365 case exprlist:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2366 case testlist:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2367 if (NCH(n) > 1) {
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2368 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2369 com_error(c, PyExc_SyntaxError,
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2370 "augmented assign to tuple not possible");
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2371 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2372 }
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
2373 com_assign_sequence(c, n, assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2374 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2375 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2376 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2377 break;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2378
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2379 case test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2380 case and_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2381 case not_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2382 case comparison:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2383 case expr:
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2384 case xor_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2385 case and_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2386 case shift_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2387 case arith_expr:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2388 case term:
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2389 case factor:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2390 if (NCH(n) > 1) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2391 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2392 "can't assign to operator");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2393 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2394 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2395 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2396 break;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2397
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2398 case power: /* atom trailer* ('**' power)* */
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2399 /* ('+'|'-'|'~') factor | atom trailer* */
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2400 if (TYPE(CHILD(n, 0)) != atom) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2401 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2402 "can't assign to operator");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2403 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2404 }
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2405 if (NCH(n) > 1) { /* trailer or exponent present */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2406 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2407 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2408 for (i = 1; i+1 < NCH(n); i++) {
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2409 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2410 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2411 "can't assign to operator");
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2412 return;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2413 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2414 com_apply_trailer(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2415 } /* NB i is still alive */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2416 com_assign_trailer(c,
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2417 CHILD(n, i), assigning, augn);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2418 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2419 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2420 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2421 break;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2422
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2423 case atom:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2424 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2425 case LPAR:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2426 n = CHILD(n, 1);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2427 if (TYPE(n) == RPAR) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2428 /* XXX Should allow () = () ??? */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2429 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2430 "can't assign to ()");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2431 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2432 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2433 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2434 com_error(c, PyExc_SyntaxError,
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2435 "augmented assign to tuple not possible");
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2436 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2437 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2438 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2439 case LSQB:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2440 n = CHILD(n, 1);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2441 if (TYPE(n) == RSQB) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2442 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2443 "can't assign to []");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2444 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2445 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2446 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2447 com_error(c, PyExc_SyntaxError,
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2448 "augmented assign to list not possible");
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2449 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2450 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2451 if (NCH(n) > 1
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2452 && TYPE(CHILD(n, 1)) == list_for) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2453 com_error(c, PyExc_SyntaxError,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
2454 "can't assign to list comprehension");
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2455 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2456 }
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
2457 com_assign_sequence(c, n, assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2458 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2459 case NAME:
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2460 if (assigning > OP_APPLY)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2461 com_augassign_name(c, CHILD(n, 0),
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2462 assigning, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2463 else
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2464 com_assign_name(c, CHILD(n, 0),
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2465 assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2466 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2467 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2468 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2469 "can't assign to literal");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2470 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2471 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2472 break;
3963
00722990b80c Removed unused var; added error check for ``lambda: x=1''.
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
2473
00722990b80c Removed unused var; added error check for ``lambda: x=1''.
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
2474 case lambdef:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2475 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2476 "can't assign to lambda");
3963
00722990b80c Removed unused var; added error check for ``lambda: x=1''.
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
2477 return;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2478
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2479 default:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2480 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2481 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2482 "com_assign: bad node");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2483 return;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2484
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2485 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2486 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2487 }
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2488
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2489 static void
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2490 com_augassign(struct compiling *c, node *n)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2491 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2492 int opcode;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2493
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2494 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2495 case '+': opcode = INPLACE_ADD; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2496 case '-': opcode = INPLACE_SUBTRACT; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2497 case '/': opcode = INPLACE_DIVIDE; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2498 case '%': opcode = INPLACE_MODULO; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2499 case '<': opcode = INPLACE_LSHIFT; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2500 case '>': opcode = INPLACE_RSHIFT; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2501 case '&': opcode = INPLACE_AND; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2502 case '^': opcode = INPLACE_XOR; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2503 case '|': opcode = INPLACE_OR; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2504 case '*':
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2505 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2506 opcode = INPLACE_POWER;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2507 else
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2508 opcode = INPLACE_MULTIPLY;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2509 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2510 default:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2511 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2512 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2513 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2514 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2515 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2516
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2517 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2518 com_expr_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2519 {
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2520 REQ(n, expr_stmt);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2521 /* testlist (('=' testlist)* | augassign testlist) */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2522 /* Forget it if we have just a doc string here */
5264
38878f1cd4f1 Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents: 5233
diff changeset
2523 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2524 return;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2525 if (NCH(n) == 1) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2526 com_node(c, CHILD(n, NCH(n)-1));
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
2527 if (c->c_interactive)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
2528 com_addbyte(c, PRINT_EXPR);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
2529 else
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
2530 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2531 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2532 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2533 else if (TYPE(CHILD(n,1)) == augassign)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2534 com_augassign(c, n);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2535 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2536 int i;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2537 com_node(c, CHILD(n, NCH(n)-1));
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2538 for (i = 0; i < NCH(n)-2; i+=2) {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2539 if (i+2 < NCH(n)-2) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2540 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2541 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2542 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2543 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2544 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2545 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2546 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2547
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2548 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2549 com_assert_stmt(struct compiling *c, node *n)
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2550 {
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2551 int a = 0, b = 0;
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2552 int i;
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2553 REQ(n, assert_stmt); /* 'assert' test [',' test] */
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2554 /* Generate code like for
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2555
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2556 if __debug__:
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2557 if not <test>:
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2558 raise AssertionError [, <message>]
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2559
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2560 where <message> is the second test, if present.
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2561 */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2562
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2563 /* XXX should __debug__ and AssertionError get inserted into
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2564 the symbol table? they don't follow the normal rules
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2565 because they are always loaded as globals */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2566
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2567 if (Py_OptimizeFlag)
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2568 return;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2569 com_addop_name(c, LOAD_GLOBAL, "__debug__");
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2570 com_push(c, 1);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2571 com_addfwref(c, JUMP_IF_FALSE, &a);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2572 com_addbyte(c, POP_TOP);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2573 com_pop(c, 1);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2574 com_node(c, CHILD(n, 1));
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2575 com_addfwref(c, JUMP_IF_TRUE, &b);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2576 com_addbyte(c, POP_TOP);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2577 com_pop(c, 1);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2578 /* Raise that exception! */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2579 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2580 com_push(c, 1);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2581 i = NCH(n)/2; /* Either 2 or 4 */
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2582 if (i > 1)
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2583 com_node(c, CHILD(n, 3));
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2584 com_addoparg(c, RAISE_VARARGS, i);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2585 com_pop(c, i);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2586 /* The interpreter does not fall through */
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2587 /* All jumps converge here */
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2588 com_backpatch(c, a);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2589 com_backpatch(c, b);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2590 com_addbyte(c, POP_TOP);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2591 }
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2592
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
2593 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2594 com_print_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2595 {
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2596 int i = 1;
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2597 node* stream = NULL;
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2598
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2599 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2600
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2601 /* are we using the extended print form? */
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2602 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2603 stream = CHILD(n, 2);
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2604 com_node(c, stream);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2605 /* stack: [...] => [... stream] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2606 com_push(c, 1);
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2607 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2608 i = 4;
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2609 else
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2610 i = 3;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2611 }
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2612 for (; i < NCH(n); i += 2) {
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2613 if (stream != NULL) {
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2614 com_addbyte(c, DUP_TOP);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2615 /* stack: [stream] => [stream stream] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2616 com_push(c, 1);
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2617 com_node(c, CHILD(n, i));
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2618 /* stack: [stream stream] => [stream stream obj] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2619 com_addbyte(c, ROT_TWO);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2620 /* stack: [stream stream obj] => [stream obj stream] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2621 com_addbyte(c, PRINT_ITEM_TO);
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2622 /* stack: [stream obj stream] => [stream] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2623 com_pop(c, 2);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2624 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2625 else {
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2626 com_node(c, CHILD(n, i));
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2627 /* stack: [...] => [... obj] */
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2628 com_addbyte(c, PRINT_ITEM);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2629 com_pop(c, 1);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2630 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2631 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2632 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2633 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2634 if (stream != NULL) {
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2635 /* must pop the extra stream object off the stack */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2636 com_addbyte(c, POP_TOP);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2637 /* stack: [... stream] => [...] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2638 com_pop(c, 1);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2639 }
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2640 }
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2641 else {
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2642 if (stream != NULL) {
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2643 /* this consumes the last stream object on stack */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2644 com_addbyte(c, PRINT_NEWLINE_TO);
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
2645 /* stack: [... stream] => [...] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2646 com_pop(c, 1);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2647 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2648 else
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2649 com_addbyte(c, PRINT_NEWLINE);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
2650 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2651 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2652
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2653 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2654 com_return_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2655 {
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2656 REQ(n, return_stmt); /* 'return' [testlist] */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2657 if (!c->c_infunction) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2658 com_error(c, PyExc_SyntaxError, "'return' outside function");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2659 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2660 if (NCH(n) < 2) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2661 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2662 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2663 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2664 else
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2665 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2666 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2667 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2668 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2669
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2670 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2671 com_raise_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2672 {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2673 int i;
7946
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2674 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2675 if (NCH(n) > 1) {
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2676 com_node(c, CHILD(n, 1));
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2677 if (NCH(n) > 3) {
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2678 com_node(c, CHILD(n, 3));
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2679 if (NCH(n) > 5)
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2680 com_node(c, CHILD(n, 5));
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
2681 }
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2682 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2683 i = NCH(n)/2;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2684 com_addoparg(c, RAISE_VARARGS, i);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2685 com_pop(c, i);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2686 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2687
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2688 static void
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2689 com_from_import(struct compiling *c, node *n)
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2690 {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2691 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2692 com_push(c, 1);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2693 if (NCH(n) > 1) {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2694 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2695 com_error(c, PyExc_SyntaxError, "invalid syntax");
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2696 return;
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2697 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2698 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2699 } else
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2700 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2701 com_pop(c, 1);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2702 }
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2703
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2704 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2705 com_import_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2706 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2707 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2708 REQ(n, import_stmt);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2709 /* 'import' dotted_name (',' dotted_name)* |
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2710 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2711 if (STR(CHILD(n, 0))[0] == 'f') {
15438
433458651eb4 Plug a memory leak in com_import_stmt(): the tuple created to hold the
Guido van Rossum <guido@python.org>
parents: 15408
diff changeset
2712 PyObject *tup;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2713 /* 'from' dotted_name 'import' ... */
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2714 REQ(CHILD(n, 1), dotted_name);
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2715
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2716 if (TYPE(CHILD(n, 3)) == STAR) {
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2717 tup = Py_BuildValue("(s)", "*");
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2718 } else {
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2719 tup = PyTuple_New((NCH(n) - 2)/2);
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2720 for (i = 3; i < NCH(n); i += 2) {
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2721 PyTuple_SET_ITEM(tup, (i-3)/2,
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2722 PyString_FromString(STR(
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2723 CHILD(CHILD(n, i), 0))));
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2724 }
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2725 }
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2726 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
15438
433458651eb4 Plug a memory leak in com_import_stmt(): the tuple created to hold the
Guido van Rossum <guido@python.org>
parents: 15408
diff changeset
2727 Py_DECREF(tup);
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2728 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2729 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2730 if (TYPE(CHILD(n, 3)) == STAR)
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2731 com_addbyte(c, IMPORT_STAR);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2732 else {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2733 for (i = 3; i < NCH(n); i += 2)
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2734 com_from_import(c, CHILD(n, i));
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2735 com_addbyte(c, POP_TOP);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2736 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2737 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2738 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2739 else {
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2740 /* 'import' ... */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2741 for (i = 1; i < NCH(n); i += 2) {
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2742 node *subn = CHILD(n, i);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2743 REQ(subn, dotted_as_name);
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2744 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
2745 com_push(c, 1);
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2746 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2747 if (NCH(subn) > 1) {
14230
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
2748 int j;
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
2749 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2750 com_error(c, PyExc_SyntaxError,
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2751 "invalid syntax");
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2752 return;
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2753 }
14230
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
2754 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
2755 com_addopname(c, LOAD_ATTR,
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2756 CHILD(CHILD(subn, 0),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2757 j));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2758 com_addop_varname(c, VAR_STORE,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2759 STR(CHILD(subn, 2)));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
2760 } else
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2761 com_addop_varname(c, VAR_STORE,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2762 STR(CHILD(CHILD(subn, 0),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2763 0)));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2764 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2765 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2766 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2767 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2768
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2769 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2770 com_exec_stmt(struct compiling *c, node *n)
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2771 {
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2772 REQ(n, exec_stmt);
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2773 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2774 com_node(c, CHILD(n, 1));
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2775 if (NCH(n) >= 4)
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2776 com_node(c, CHILD(n, 3));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2777 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2778 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2779 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2780 }
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2781 if (NCH(n) >= 6)
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2782 com_node(c, CHILD(n, 5));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2783 else {
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2784 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2785 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2786 }
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2787 com_addbyte(c, EXEC_STMT);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2788 com_pop(c, 3);
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2789 }
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2790
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2791 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2792 is_constant_false(struct compiling *c, node *n)
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2793 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2794 PyObject *v;
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2795 int i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2796 /* argument c will be NULL when called from symtable_node() */
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2797
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2798 /* Label to avoid tail recursion */
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2799 next:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2800 switch (TYPE(n)) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2801
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2802 case suite:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2803 if (NCH(n) == 1) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2804 n = CHILD(n, 0);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2805 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2806 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2807 /* Fall through */
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2808 case file_input:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2809 for (i = 0; i < NCH(n); i++) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2810 node *ch = CHILD(n, i);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2811 if (TYPE(ch) == stmt) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2812 n = ch;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2813 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2814 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2815 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2816 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2817
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2818 case stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2819 case simple_stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2820 case small_stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2821 n = CHILD(n, 0);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2822 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2823
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2824 case expr_stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2825 case testlist:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2826 case test:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2827 case and_test:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2828 case not_test:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2829 case comparison:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2830 case expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2831 case xor_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2832 case and_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2833 case shift_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2834 case arith_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2835 case term:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2836 case factor:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2837 case power:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2838 case atom:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2839 if (NCH(n) == 1) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2840 n = CHILD(n, 0);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2841 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2842 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2843 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2844
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2845 case NAME:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2846 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2847 return 1;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2848 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2849
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2850 case NUMBER:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2851 v = parsenumber(c, STR(n));
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2852 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2853 PyErr_Clear();
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2854 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2855 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2856 i = PyObject_IsTrue(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2857 Py_DECREF(v);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2858 return i == 0;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2859
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2860 case STRING:
5183
636757e812a4 Fix dumb bug calling parsestrplus with wrong node as argument.
Guido van Rossum <guido@python.org>
parents: 5181
diff changeset
2861 v = parsestr(STR(n));
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2862 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2863 PyErr_Clear();
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2864 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2865 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2866 i = PyObject_IsTrue(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2867 Py_DECREF(v);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2868 return i == 0;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2869
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2870 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2871 return 0;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2872 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2873
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
2874 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2875 com_if_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2876 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2877 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2878 int anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2879 REQ(n, if_stmt);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2880 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2881 for (i = 0; i+3 < NCH(n); i+=4) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2882 int a = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2883 node *ch = CHILD(n, i+1);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2884 if (is_constant_false(c, ch))
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2885 continue;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2886 if (i > 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2887 com_addoparg(c, SET_LINENO, ch->n_lineno);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2888 com_node(c, ch);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2889 com_addfwref(c, JUMP_IF_FALSE, &a);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2890 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2891 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2892 com_node(c, CHILD(n, i+3));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2893 com_addfwref(c, JUMP_FORWARD, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2894 com_backpatch(c, a);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2895 /* We jump here with an extra entry which we now pop */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2896 com_addbyte(c, POP_TOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2897 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2898 if (i+2 < NCH(n))
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2899 com_node(c, CHILD(n, i+2));
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2900 if (anchor)
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
2901 com_backpatch(c, anchor);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2902 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2903
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2904 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2905 com_while_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2906 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2907 int break_anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2908 int anchor = 0;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2909 int save_begin = c->c_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2910 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2911 com_addfwref(c, SETUP_LOOP, &break_anchor);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2912 block_push(c, SETUP_LOOP);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2913 c->c_begin = c->c_nexti;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2914 com_addoparg(c, SET_LINENO, n->n_lineno);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2915 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2916 com_addfwref(c, JUMP_IF_FALSE, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2917 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2918 com_pop(c, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2919 c->c_loops++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2920 com_node(c, CHILD(n, 3));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2921 c->c_loops--;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2922 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2923 c->c_begin = save_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2924 com_backpatch(c, anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2925 /* We jump here with one entry more on the stack */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2926 com_addbyte(c, POP_TOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2927 com_addbyte(c, POP_BLOCK);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2928 block_pop(c, SETUP_LOOP);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2929 if (NCH(n) > 4)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2930 com_node(c, CHILD(n, 6));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2931 com_backpatch(c, break_anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2932 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2933
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2934 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2935 com_for_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2936 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2937 PyObject *v;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2938 int break_anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2939 int anchor = 0;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2940 int save_begin = c->c_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2941 REQ(n, for_stmt);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2942 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2943 com_addfwref(c, SETUP_LOOP, &break_anchor);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2944 block_push(c, SETUP_LOOP);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2945 com_node(c, CHILD(n, 3));
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2946 v = PyInt_FromLong(0L);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2947 if (v == NULL)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2948 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2949 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2950 com_push(c, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2951 Py_XDECREF(v);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2952 c->c_begin = c->c_nexti;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2953 com_addoparg(c, SET_LINENO, n->n_lineno);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2954 com_addfwref(c, FOR_LOOP, &anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2955 com_push(c, 1);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2956 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2957 c->c_loops++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2958 com_node(c, CHILD(n, 5));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2959 c->c_loops--;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2960 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2961 c->c_begin = save_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2962 com_backpatch(c, anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2963 com_pop(c, 2); /* FOR_LOOP has popped these */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2964 com_addbyte(c, POP_BLOCK);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2965 block_pop(c, SETUP_LOOP);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2966 if (NCH(n) > 8)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2967 com_node(c, CHILD(n, 8));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2968 com_backpatch(c, break_anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2969 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2970
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
2971 /* Code generated for "try: S finally: Sf" is as follows:
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2972
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2973 SETUP_FINALLY L
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2974 <code for S>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2975 POP_BLOCK
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2976 LOAD_CONST <nil>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2977 L: <code for Sf>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2978 END_FINALLY
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2979
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2980 The special instructions use the block stack. Each block
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2981 stack entry contains the instruction that created it (here
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2982 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
2983 block stack entry was created, and a label (here L).
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2984
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2985 SETUP_FINALLY:
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2986 Pushes the current value stack level and the label
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2987 onto the block stack.
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2988 POP_BLOCK:
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2989 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
2990 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
2991 block stack. (The label is ignored.)
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2992 END_FINALLY:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2993 Pops a variable number of entries from the *value* stack
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2994 and re-raises the exception they specify. The number of
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
2995 entries popped depends on the (pseudo) exception type.
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2996
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2997 The block stack is unwound when an exception is raised:
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
2998 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
2999 onto the value stack (and the exception condition is cleared),
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3000 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
3001 stack.
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3002
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3003 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3004 (The contents of the value stack is shown in [], with the top
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3005 at the right; 'tb' is trace-back info, 'val' the exception's
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3006 associated value, and 'exc' the exception.)
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3007
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3008 Value stack Label Instruction Argument
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3009 [] SETUP_EXCEPT L1
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3010 [] <code for S>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3011 [] POP_BLOCK
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3012 [] JUMP_FORWARD L0
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3013
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3014 [tb, val, exc] L1: DUP )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3015 [tb, val, exc, exc] <evaluate E1> )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3016 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3017 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3018 [tb, val, exc, 1] POP )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3019 [tb, val, exc] POP
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3020 [tb, val] <assign to V1> (or POP if no V1)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3021 [tb] POP
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3022 [] <code for S1>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3023 JUMP_FORWARD L0
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3024
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3025 [tb, val, exc, 0] L2: POP
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3026 [tb, val, exc] DUP
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3027 .............................etc.......................
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3028
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3029 [tb, val, exc, 0] Ln+1: POP
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3030 [tb, val, exc] END_FINALLY # re-raise exception
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3031
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3032 [] L0: <next statement>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3033
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3034 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
3035 */
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3036
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3037 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3038 com_try_except(struct compiling *c, node *n)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3039 {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3040 int except_anchor = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3041 int end_anchor = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3042 int else_anchor = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3043 int i;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3044 node *ch;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3045
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3046 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3047 block_push(c, SETUP_EXCEPT);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3048 com_node(c, CHILD(n, 2));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3049 com_addbyte(c, POP_BLOCK);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3050 block_pop(c, SETUP_EXCEPT);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3051 com_addfwref(c, JUMP_FORWARD, &else_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3052 com_backpatch(c, except_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3053 for (i = 3;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3054 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3055 i += 3) {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3056 /* except_clause: 'except' [expr [',' var]] */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3057 if (except_anchor == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3058 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3059 "default 'except:' must be last");
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3060 break;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3061 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3062 except_anchor = 0;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3063 com_push(c, 3); /* tb, val, exc pushed by exception */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3064 com_addoparg(c, SET_LINENO, ch->n_lineno);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3065 if (NCH(ch) > 1) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3066 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3067 com_push(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3068 com_node(c, CHILD(ch, 1));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3069 com_addoparg(c, COMPARE_OP, EXC_MATCH);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3070 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3071 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3072 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3073 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3074 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3075 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3076 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3077 if (NCH(ch) > 3)
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3078 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3079 else {
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3080 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3081 com_pop(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3082 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3083 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3084 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3085 com_node(c, CHILD(n, i+2));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3086 com_addfwref(c, JUMP_FORWARD, &end_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3087 if (except_anchor) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3088 com_backpatch(c, except_anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3089 /* We come in with [tb, val, exc, 0] on the
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3090 stack; one pop and it's the same as
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3091 expected at the start of the loop */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3092 com_addbyte(c, POP_TOP);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3093 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3094 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3095 /* We actually come in here with [tb, val, exc] but the
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3096 END_FINALLY will zap those and jump around.
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3097 The c_stacklevel does not reflect them so we need not pop
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3098 anything. */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3099 com_addbyte(c, END_FINALLY);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3100 com_backpatch(c, else_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3101 if (i < NCH(n))
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3102 com_node(c, CHILD(n, i+2));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3103 com_backpatch(c, end_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3104 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3105
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3106 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3107 com_try_finally(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3108 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3109 int finally_anchor = 0;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3110 node *ch;
792
dc3b455f7632 Only * can be used for varargs argument lists
Guido van Rossum <guido@python.org>
parents: 744
diff changeset
3111
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3112 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3113 block_push(c, SETUP_FINALLY);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3114 com_node(c, CHILD(n, 2));
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3115 com_addbyte(c, POP_BLOCK);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3116 block_pop(c, SETUP_FINALLY);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3117 block_push(c, END_FINALLY);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3118 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3119 /* While the generated code pushes only one item,
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3120 the try-finally handling can enter here with
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3121 up to three items. OK, here are the details:
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3122 3 for an exception, 2 for RETURN, 1 for BREAK. */
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3123 com_push(c, 3);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3124 com_backpatch(c, finally_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3125 ch = CHILD(n, NCH(n)-1);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3126 com_addoparg(c, SET_LINENO, ch->n_lineno);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3127 com_node(c, ch);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3128 com_addbyte(c, END_FINALLY);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3129 block_pop(c, END_FINALLY);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3130 com_pop(c, 3); /* Matches the com_push above */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3131 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3132
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3133 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3134 com_try_stmt(struct compiling *c, node *n)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3135 {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3136 REQ(n, try_stmt);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3137 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3138 | 'try' ':' suite 'finally' ':' suite */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3139 if (TYPE(CHILD(n, 3)) != except_clause)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3140 com_try_finally(c, n);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3141 else
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3142 com_try_except(c, n);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3143 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3144
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3145 static node *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3146 get_rawdocstring(node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3147 {
2241
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3148 int i;
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3149
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3150 /* Label to avoid tail recursion */
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3151 next:
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3152 switch (TYPE(n)) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3153
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3154 case suite:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3155 if (NCH(n) == 1) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3156 n = CHILD(n, 0);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3157 goto next;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3158 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3159 /* Fall through */
2241
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3160 case file_input:
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3161 for (i = 0; i < NCH(n); i++) {
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3162 node *ch = CHILD(n, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3163 if (TYPE(ch) == stmt) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3164 n = ch;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3165 goto next;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3166 }
2241
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3167 }
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3168 break;
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
3169
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3170 case stmt:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3171 case simple_stmt:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3172 case small_stmt:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3173 n = CHILD(n, 0);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3174 goto next;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3175
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3176 case expr_stmt:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3177 case testlist:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3178 case test:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3179 case and_test:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3180 case not_test:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3181 case comparison:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3182 case expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3183 case xor_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3184 case and_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3185 case shift_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3186 case arith_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3187 case term:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3188 case factor:
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3189 case power:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3190 if (NCH(n) == 1) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3191 n = CHILD(n, 0);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3192 goto next;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3193 }
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3194 break;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3195
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3196 case atom:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3197 if (TYPE(CHILD(n, 0)) == STRING)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3198 return n;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3199 break;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3200
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3201 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3202 return NULL;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3203 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3204
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3205 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3206 get_docstring(node *n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3207 {
9987
b7bcc2cda1b3 Implement -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
3208 /* Don't generate doc-strings if run with -OO */
b7bcc2cda1b3 Implement -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
3209 if (Py_OptimizeFlag > 1)
b7bcc2cda1b3 Implement -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
3210 return NULL;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3211 n = get_rawdocstring(n);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3212 if (n == NULL)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3213 return NULL;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3214 return parsestrplus(n);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3215 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3216
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3217 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3218 com_suite(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3219 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3220 REQ(n, suite);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3221 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3222 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3223 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3224 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3225 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3226 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3227 for (i = 0; i < NCH(n); i++) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3228 node *ch = CHILD(n, i);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3229 if (TYPE(ch) == stmt)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3230 com_node(c, ch);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3231 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3232 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3233 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3234
744
834dd4a77ee2 fix what lint found
Guido van Rossum <guido@python.org>
parents: 703
diff changeset
3235 /* ARGSUSED */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3236 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3237 com_continue_stmt(struct compiling *c, node *n)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3238 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3239 int i = c->c_nblocks;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3240 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
3241 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3242 }
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3243 else if (i <= 0) {
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3244 /* at the outer level */
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3245 com_error(c, PyExc_SyntaxError,
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3246 "'continue' not properly in loop");
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3247 }
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3248 else {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3249 int j;
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3250 for (j = i-1; j >= 0; --j) {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3251 if (c->c_block[j] == SETUP_LOOP)
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3252 break;
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3253 }
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3254 if (j >= 0) {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3255 /* there is a loop, but something interferes */
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3256 for (; i > j; --i) {
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3257 if (c->c_block[i] == SETUP_EXCEPT ||
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3258 c->c_block[i] == SETUP_FINALLY) {
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3259 com_addoparg(c, CONTINUE_LOOP,
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3260 c->c_begin);
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3261 return;
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3262 }
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3263 if (c->c_block[i] == END_FINALLY) {
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3264 com_error(c, PyExc_SyntaxError,
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3265 "'continue' not supported inside 'finally' clause");
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3266 return;
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
3267 }
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3268 }
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
3269 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3270 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3271 "'continue' not properly in loop");
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3272 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3273 /* XXX Could allow it inside a 'finally' clause
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3274 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
3275 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3276
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3277 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3278 com_argdefs(struct compiling *c, node *n)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3279 {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3280 int i, nch, nargs, ndefs;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3281 if (TYPE(n) == lambdef) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3282 /* lambdef: 'lambda' [varargslist] ':' test */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3283 n = CHILD(n, 1);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3284 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3285 else {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3286 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3287 n = CHILD(n, 2);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3288 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3289 n = CHILD(n, 1);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3290 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3291 if (TYPE(n) != varargslist)
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3292 return 0;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3293 /* varargslist:
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3294 (fpdef ['=' test] ',')* '*' ....... |
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3295 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3296 nch = NCH(n);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3297 nargs = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3298 ndefs = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3299 for (i = 0; i < nch; i++) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3300 int t;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3301 if (TYPE(CHILD(n, i)) == STAR ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3302 TYPE(CHILD(n, i)) == DOUBLESTAR)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
3303 break;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3304 nargs++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3305 i++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3306 if (i >= nch)
3103
e51e17b2d88b fix bug with missing default for last arg (discovered by Tommy Burnette)
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
3307 t = RPAR; /* Anything except EQUAL or COMMA */
e51e17b2d88b fix bug with missing default for last arg (discovered by Tommy Burnette)
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
3308 else
e51e17b2d88b fix bug with missing default for last arg (discovered by Tommy Burnette)
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
3309 t = TYPE(CHILD(n, i));
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3310 if (t == EQUAL) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3311 i++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3312 ndefs++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3313 com_node(c, CHILD(n, i));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3314 i++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3315 if (i >= nch)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3316 break;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3317 t = TYPE(CHILD(n, i));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3318 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3319 else {
9214
c259c3c4d0cc Treat def f(a, b=1, c): ... as an error (missing default for c)
Guido van Rossum <guido@python.org>
parents: 9049
diff changeset
3320 /* Treat "(a=1, b)" as an error */
c259c3c4d0cc Treat def f(a, b=1, c): ... as an error (missing default for c)
Guido van Rossum <guido@python.org>
parents: 9049
diff changeset
3321 if (ndefs)
c259c3c4d0cc Treat def f(a, b=1, c): ... as an error (missing default for c)
Guido van Rossum <guido@python.org>
parents: 9049
diff changeset
3322 com_error(c, PyExc_SyntaxError,
9215
1995cdbe3384 Eh, better error message for the previous change. It now says
Guido van Rossum <guido@python.org>
parents: 9214
diff changeset
3323 "non-default argument follows default argument");
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3324 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3325 if (t != COMMA)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3326 break;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3327 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3328 return ndefs;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3329 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3330
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3331 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3332 com_funcdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3333 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3334 PyObject *co;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3335 int ndefs;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3336 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3337 ndefs = com_argdefs(c, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3338 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n));
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3339 co = (PyObject *)icompile(n, c);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3340 symtable_exit_scope(c->c_symtable);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3341 if (co == NULL)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3342 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3343 else {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3344 int closure = com_make_closure(c, (PyCodeObject *)co);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3345 int i = com_addconst(c, co);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3346 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3347 com_push(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3348 if (closure)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3349 com_addoparg(c, MAKE_CLOSURE, ndefs);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3350 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3351 com_addoparg(c, MAKE_FUNCTION, ndefs);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3352 com_pop(c, ndefs);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3353 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3354 com_pop(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3355 Py_DECREF(co);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3356 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3357 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3358
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3359 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3360 com_bases(struct compiling *c, node *n)
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3361 {
744
834dd4a77ee2 fix what lint found
Guido van Rossum <guido@python.org>
parents: 703
diff changeset
3362 int i;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3363 REQ(n, testlist);
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3364 /* testlist: test (',' test)* [','] */
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3365 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
3366 com_node(c, CHILD(n, i));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3367 i = (NCH(n)+1) / 2;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3368 com_addoparg(c, BUILD_TUPLE, i);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3369 com_pop(c, i-1);
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3370 }
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3371
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3372 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3373 com_classdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3374 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3375 int i;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3376 PyObject *co, *v;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3377 char *name;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3378
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3379 REQ(n, classdef);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3380 /* classdef: class NAME ['(' testlist ')'] ':' suite */
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
3381 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3382 c->c_errors++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3383 return;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3384 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3385 /* Push the class name on the stack */
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3386 i = com_addconst(c, v);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3387 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3388 com_push(c, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3389 Py_DECREF(v);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3390 /* Push the tuple of base classes on the stack */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3391 if (TYPE(CHILD(n, 2)) != LPAR) {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3392 com_addoparg(c, BUILD_TUPLE, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3393 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3394 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3395 else
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
3396 com_bases(c, CHILD(n, 3));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3397 name = STR(CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3398 symtable_enter_scope(c->c_symtable, name, TYPE(n));
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3399 co = (PyObject *)icompile(n, c);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3400 symtable_exit_scope(c->c_symtable);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3401 if (co == NULL)
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3402 c->c_errors++;
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3403 else {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3404 int closure = com_make_closure(c, (PyCodeObject *)co);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3405 i = com_addconst(c, co);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3406 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3407 com_push(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3408 if (closure)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3409 com_addoparg(c, MAKE_CLOSURE, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3410 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3411 com_addoparg(c, MAKE_FUNCTION, 0);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3412 com_addoparg(c, CALL_FUNCTION, 0);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3413 com_addbyte(c, BUILD_CLASS);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3414 com_pop(c, 2);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3415 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3416 Py_DECREF(co);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3417 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3418 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3419
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3420 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3421 com_node(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3422 {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3423 loop:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3424 switch (TYPE(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3425
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3426 /* Definition nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3427
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3428 case funcdef:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3429 com_funcdef(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3430 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3431 case classdef:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3432 com_classdef(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3433 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3434
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3435 /* Trivial parse tree nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3436
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3437 case stmt:
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3438 case small_stmt:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3439 case flow_stmt:
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3440 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3441 goto loop;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3442
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3443 case simple_stmt:
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3444 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3445 com_addoparg(c, SET_LINENO, n->n_lineno);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3446 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3447 int i;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3448 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
3449 com_node(c, CHILD(n, i));
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3450 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3451 break;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3452
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3453 case compound_stmt:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3454 com_addoparg(c, SET_LINENO, n->n_lineno);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3455 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3456 goto loop;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3457
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3458 /* Statement nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3459
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3460 case expr_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3461 com_expr_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3462 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3463 case print_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3464 com_print_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3465 break;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3466 case del_stmt: /* 'del' exprlist */
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3467 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3468 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3469 case pass_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3470 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3471 case break_stmt:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3472 if (c->c_loops == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3473 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3474 "'break' outside loop");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3475 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3476 com_addbyte(c, BREAK_LOOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3477 break;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3478 case continue_stmt:
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3479 com_continue_stmt(c, n);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3480 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3481 case return_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3482 com_return_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3483 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3484 case raise_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3485 com_raise_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3486 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3487 case import_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3488 com_import_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3489 break;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3490 case global_stmt:
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
3491 break;
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3492 case exec_stmt:
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3493 com_exec_stmt(c, n);
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3494 break;
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3495 case assert_stmt:
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3496 com_assert_stmt(c, n);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3497 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3498 case if_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3499 com_if_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3500 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3501 case while_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3502 com_while_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3503 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3504 case for_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3505 com_for_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3506 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3507 case try_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3508 com_try_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3509 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3510 case suite:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3511 com_suite(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3512 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3513
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3514 /* Expression nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3515
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3516 case testlist:
482
ee899a81570d New argument passing.
Guido van Rossum <guido@python.org>
parents: 466
diff changeset
3517 com_list(c, n, 0);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3518 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3519 case test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3520 com_test(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3521 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3522 case and_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3523 com_and_test(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3524 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3525 case not_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3526 com_not_test(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3527 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3528 case comparison:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3529 com_comparison(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3530 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3531 case exprlist:
482
ee899a81570d New argument passing.
Guido van Rossum <guido@python.org>
parents: 466
diff changeset
3532 com_list(c, n, 0);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3533 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3534 case expr:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3535 com_expr(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3536 break;
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3537 case xor_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3538 com_xor_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3539 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3540 case and_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3541 com_and_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3542 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3543 case shift_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3544 com_shift_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3545 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3546 case arith_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3547 com_arith_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3548 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3549 case term:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3550 com_term(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3551 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3552 case factor:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3553 com_factor(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3554 break;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3555 case power:
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3556 com_power(c, n);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3557 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3558 case atom:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3559 com_atom(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3560 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3561
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3562 default:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3563 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3564 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3565 "com_node: unexpected node type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3566 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3567 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3568
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
3569 static void com_fplist(struct compiling *, node *);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3570
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3571 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3572 com_fpdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3573 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3574 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3575 if (TYPE(CHILD(n, 0)) == LPAR)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3576 com_fplist(c, CHILD(n, 1));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3577 else {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3578 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3579 com_pop(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3580 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3581 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3582
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3583 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3584 com_fplist(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3585 {
544
d0baa8300005 Minor changes.
Guido van Rossum <guido@python.org>
parents: 493
diff changeset
3586 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3587 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3588 com_fpdef(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3589 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3590 else {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3591 int i = (NCH(n)+1)/2;
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
3592 com_addoparg(c, UNPACK_SEQUENCE, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3593 com_push(c, i-1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3594 for (i = 0; i < NCH(n); i += 2)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3595 com_fpdef(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3596 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3597 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3598
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3599 /* XXX This function could probably be made simpler, because it
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3600 doesn't do anything except generate code for complex arguments.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3601 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3602
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3603 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3604 com_arglist(struct compiling *c, node *n)
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3605 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3606 int nch, i, narg;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3607 int complex = 0;
5754
9ed26afeeaa5 Fix bug reported by Just: anonymous arguments used for tuples should
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
3608 char nbuf[10];
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3609 REQ(n, varargslist);
617
755bb8efcc2d Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents: 584
diff changeset
3610 /* varargslist:
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3611 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3612 nch = NCH(n);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3613 /* Enter all arguments in table of locals */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3614 for (i = 0, narg = 0; i < nch; i++) {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3615 node *ch = CHILD(n, i);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3616 node *fp;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3617 char *name;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3618 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
3619 break;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3620 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3621 fp = CHILD(ch, 0);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3622 if (TYPE(fp) == NAME) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3623 PyObject *v;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3624 name = STR(fp);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3625 v = PyDict_GetItemString(c->c_cellvars, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3626 if (v) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3627 com_addoparg(c, LOAD_FAST, narg);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3628 com_addoparg(c, STORE_DEREF,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3629 PyInt_AS_LONG(v));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3630 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3631 } else {
5754
9ed26afeeaa5 Fix bug reported by Just: anonymous arguments used for tuples should
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
3632 name = nbuf;
9ed26afeeaa5 Fix bug reported by Just: anonymous arguments used for tuples should
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
3633 sprintf(nbuf, ".%d", i);
3766
516490c8192c Py_complex; and WITHOUT_COMPLEX added to getargs.c
Guido van Rossum <guido@python.org>
parents: 3366
diff changeset
3634 complex = 1;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
3635 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3636 narg++;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3637 /* all name updates handled by symtable */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3638 if (++i >= nch)
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3639 break;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3640 ch = CHILD(n, i);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3641 if (TYPE(ch) == EQUAL)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3642 i += 2;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3643 else
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3644 REQ(ch, COMMA);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3645 }
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3646 /* Handle *arguments */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3647 if (i < nch) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3648 node *ch;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3649 ch = CHILD(n, i);
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3650 if (TYPE(ch) != DOUBLESTAR) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3651 REQ(ch, STAR);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3652 ch = CHILD(n, i+1);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3653 if (TYPE(ch) == NAME) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3654 i += 3;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3655 }
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3656 }
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3657 }
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3658 /* Handle **keywords */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3659 if (i < nch) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3660 node *ch;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3661 ch = CHILD(n, i);
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3662 if (TYPE(ch) != DOUBLESTAR) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3663 REQ(ch, STAR);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3664 ch = CHILD(n, i+1);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3665 REQ(ch, STAR);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3666 ch = CHILD(n, i+2);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3667 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3668 else
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3669 ch = CHILD(n, i+1);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3670 REQ(ch, NAME);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3671 }
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3672 if (complex) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3673 /* Generate code for complex arguments only after
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3674 having counted the simple arguments */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3675 int ilocal = 0;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3676 for (i = 0; i < nch; i++) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3677 node *ch = CHILD(n, i);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3678 node *fp;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3679 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3680 break;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3681 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3682 fp = CHILD(ch, 0);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3683 if (TYPE(fp) != NAME) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3684 com_addoparg(c, LOAD_FAST, ilocal);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3685 com_push(c, 1);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3686 com_fpdef(c, ch);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3687 }
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3688 ilocal++;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3689 if (++i >= nch)
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3690 break;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3691 ch = CHILD(n, i);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3692 if (TYPE(ch) == EQUAL)
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3693 i += 2;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3694 else
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3695 REQ(ch, COMMA);
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3696 }
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3697 }
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3698 }
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3699
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3700 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3701 com_file_input(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3702 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3703 int i;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3704 PyObject *doc;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3705 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3706 doc = get_docstring(n);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3707 if (doc != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3708 int i = com_addconst(c, doc);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3709 Py_DECREF(doc);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3710 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3711 com_push(c, 1);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3712 com_addop_name(c, STORE_NAME, "__doc__");
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3713 com_pop(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3714 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3715 for (i = 0; i < NCH(n); i++) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3716 node *ch = CHILD(n, i);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3717 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3718 com_node(c, ch);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3719 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3720 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3721
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3722 /* Top-level compile-node interface */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3723
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3724 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3725 compile_funcdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3726 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3727 PyObject *doc;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3728 node *ch;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3729 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
3730 c->c_name = STR(CHILD(n, 1));
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3731 doc = get_docstring(CHILD(n, 4));
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3732 if (doc != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3733 (void) com_addconst(c, doc);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3734 Py_DECREF(doc);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3735 }
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3736 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3737 (void) com_addconst(c, Py_None); /* No docstring */
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3738 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3739 ch = CHILD(ch, 1); /* ')' | varargslist */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3740 if (TYPE(ch) == varargslist)
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
3741 com_arglist(c, ch);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3742 c->c_infunction = 1;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3743 com_node(c, CHILD(n, 4));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3744 c->c_infunction = 0;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3745 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3746 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3747 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3748 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3749 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3750
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3751 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3752 compile_lambdef(struct compiling *c, node *n)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
3753 {
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3754 node *ch;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3755 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
2005
ed5d0ad5ea2f set name to <lambda> instead of None for lambdas
Guido van Rossum <guido@python.org>
parents: 1833
diff changeset
3756 c->c_name = "<lambda>";
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3757
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3758 ch = CHILD(n, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3759 (void) com_addconst(c, Py_None); /* No docstring */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3760 if (TYPE(ch) == varargslist) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3761 com_arglist(c, ch);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3762 ch = CHILD(n, 3);
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3763 }
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3764 else
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3765 ch = CHILD(n, 2);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3766 com_node(c, ch);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
3767 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3768 com_pop(c, 1);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
3769 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
3770
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
3771 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3772 compile_classdef(struct compiling *c, node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3773 {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3774 node *ch;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3775 PyObject *doc;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3776 REQ(n, classdef);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3777 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3778 c->c_name = STR(CHILD(n, 1));
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3779 c->c_private = c->c_name;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3780 ch = CHILD(n, NCH(n)-1); /* The suite */
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3781 doc = get_docstring(ch);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3782 if (doc != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3783 int i = com_addconst(c, doc);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3784 Py_DECREF(doc);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3785 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3786 com_push(c, 1);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3787 com_addop_name(c, STORE_NAME, "__doc__");
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3788 com_pop(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3789 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3790 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3791 (void) com_addconst(c, Py_None);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3792 com_node(c, ch);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3793 com_addbyte(c, LOAD_LOCALS);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3794 com_push(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3795 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3796 com_pop(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3797 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3798
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3799 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3800 compile_node(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3801 {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3802 com_addoparg(c, SET_LINENO, n->n_lineno);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3803
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3804 switch (TYPE(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3805
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3806 case single_input: /* One interactive command */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3807 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3808 c->c_interactive++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3809 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3810 if (TYPE(n) != NEWLINE)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3811 com_node(c, n);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3812 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3813 com_push(c, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3814 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3815 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3816 c->c_interactive--;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3817 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3818
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3819 case file_input: /* A whole file, or built-in function exec() */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3820 com_file_input(c, n);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3821 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3822 com_push(c, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3823 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3824 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3825 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3826
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3827 case eval_input: /* Built-in function input() */
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3828 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
3829 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3830 com_pop(c, 1);
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3831 break;
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3832
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3833 case lambdef: /* anonymous function definition */
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3834 compile_lambdef(c, n);
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3835 break;
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3836
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3837 case funcdef: /* A function definition */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3838 compile_funcdef(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3839 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3840
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
3841 case classdef: /* A class definition */
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3842 compile_classdef(c, n);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3843 break;
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3844
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3845 default:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3846 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3847 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3848 "compile_node: unexpected node type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3849 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3850 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3851
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3852 static PyObject *
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3853 dict_keys_inorder(PyObject *dict, int offset)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3854 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3855 PyObject *tuple, *k, *v;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3856 int i, pos = 0, size = PyDict_Size(dict);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3857
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3858 tuple = PyTuple_New(size);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3859 if (tuple == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3860 return NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3861 while (PyDict_Next(dict, &pos, &k, &v)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3862 i = PyInt_AS_LONG(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3863 Py_INCREF(k);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3864 PyTuple_SET_ITEM(tuple, i - offset, k);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3865 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3866 return tuple;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3867 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3868
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3869 PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3870 PyNode_Compile(node *n, char *filename)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3871 {
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3872 return jcompile(n, filename, NULL);
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3873 }
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3874
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3875 static PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3876 icompile(node *n, struct compiling *base)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3877 {
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3878 return jcompile(n, base->c_filename, base);
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3879 }
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3880
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3881 static PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3882 jcompile(node *n, char *filename, struct compiling *base)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3883 {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3884 struct compiling sc;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3885 PyCodeObject *co;
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
3886 if (!com_init(&sc, filename))
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3887 return NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3888 if (base) {
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3889 sc.c_private = base->c_private;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3890 sc.c_symtable = base->c_symtable;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3891 /* c_symtable still points to parent's symbols */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3892 if (base->c_nested
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3893 || (sc.c_symtable->st_cur_type == TYPE_FUNCTION))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3894 sc.c_nested = 1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3895 } else {
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
3896 sc.c_private = NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3897 if (symtable_build(&sc, n) < 0) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3898 com_free(&sc);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3899 return NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3900 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3901 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3902 co = NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3903 if (symtable_load_symbols(&sc) < 0)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3904 goto exit;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3905 compile_node(&sc, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3906 com_done(&sc);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
3907 if (sc.c_errors == 0) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3908 PyObject *consts, *names, *varnames, *filename, *name,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3909 *freevars, *cellvars;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3910 consts = PyList_AsTuple(sc.c_consts);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3911 names = PyList_AsTuple(sc.c_names);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3912 varnames = PyList_AsTuple(sc.c_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3913 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3914 freevars = dict_keys_inorder(sc.c_freevars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3915 PyTuple_GET_SIZE(cellvars));
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
3916 filename = PyString_InternFromString(sc.c_filename);
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
3917 name = PyString_InternFromString(sc.c_name);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3918 if (!PyErr_Occurred())
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3919 co = PyCode_New(sc.c_argcount,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3920 sc.c_nlocals,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3921 sc.c_maxstacklevel,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3922 sc.c_flags,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3923 sc.c_code,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3924 consts,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3925 names,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3926 varnames,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3927 freevars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3928 cellvars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3929 filename,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3930 name,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3931 sc.c_firstlineno,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3932 sc.c_lnotab);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3933 Py_XDECREF(consts);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3934 Py_XDECREF(names);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3935 Py_XDECREF(varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3936 Py_XDECREF(freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3937 Py_XDECREF(cellvars);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3938 Py_XDECREF(filename);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3939 Py_XDECREF(name);
266
b5826bf29677 Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents: 238
diff changeset
3940 }
13884
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3941 else if (!PyErr_Occurred()) {
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3942 /* This could happen if someone called PyErr_Clear() after an
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3943 error was reported above. That's not supposed to happen,
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3944 but I just plugged one case and I'm not sure there can't be
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3945 others. In that case, raise SystemError so that at least
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3946 it gets reported instead dumping core. */
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3947 PyErr_SetString(PyExc_SystemError, "lost syntax error");
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
3948 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3949 exit:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3950 if (base == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3951 symtable_free(sc.c_symtable);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3952 com_free(&sc);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3953 return co;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3954 }
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3955
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3956 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3957 PyCode_Addr2Line(PyCodeObject *co, int addrq)
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3958 {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3959 int size = PyString_Size(co->co_lnotab) / 2;
11290
ccdf5dc2acf6 Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 9987
diff changeset
3960 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3961 int line = co->co_firstlineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3962 int addr = 0;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3963 while (--size >= 0) {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3964 addr += *p++;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3965 if (addr > addrq)
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3966 break;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3967 line += *p++;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3968 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3969 return line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
3970 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3971
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3972 /* The test for LOCAL must come before the test for FREE in order to
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3973 handle classes where name is both local and free. The local var is
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3974 a method and the free var is a free var referenced within a method.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3975 */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3976
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3977 static int
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3978 get_ref_type(struct compiling *c, char *name)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3979 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3980 PyObject *v;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3981 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3982 return CELL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3983 if (PyDict_GetItemString(c->c_locals, name) != NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3984 return LOCAL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3985 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3986 return FREE;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3987 v = PyDict_GetItemString(c->c_globals, name);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3988 if (v) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3989 if (v == Py_None)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3990 return GLOBAL_EXPLICIT;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3991 else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3992 return GLOBAL_IMPLICIT;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3993 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3994 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3995 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3996 char buf[250];
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3997 sprintf(buf, "unknown scope for %.100s in %.100s (%s)",
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
3998 name, c->c_name,
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
3999 PyObject_REPR(c->c_symtable->st_cur_id));
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4000 Py_FatalError(buf);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4001 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4002 return -1; /* can't get here */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4003 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4004
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4005 static int
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4006 symtable_build(struct compiling *c, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4007 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4008 if ((c->c_symtable = symtable_init()) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4009 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4010 if (symtable_enter_scope(c->c_symtable, TOP, TYPE(n)) < 0)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4011 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4012 symtable_node(c->c_symtable, n);
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4013 if (c->c_symtable->st_errors > 0)
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4014 return -1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4015 /* reset for second pass */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4016 c->c_symtable->st_scopes = 1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4017 c->c_symtable->st_pass = 2;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4018 return 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4019 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4020
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4021 static int
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4022 symtable_load_symbols(struct compiling *c)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4023 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4024 static PyObject *implicit = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4025 PyObject *name, *varnames, *v;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4026 int i, info, pos;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4027 int nlocals, nfrees, ncells;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4028 struct symtable *st = c->c_symtable;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4029
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4030 if (implicit == NULL) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4031 implicit = PyInt_FromLong(1);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4032 if (implicit == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4033 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4034 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4035 v = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4036
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4037 varnames = PyDict_GetItem(st->st_varnames, st->st_cur_id);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4038 if (varnames == NULL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4039 varnames = PyList_New(0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4040 if (varnames == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4041 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4042 } else
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4043 Py_INCREF(varnames);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4044 c->c_varnames = varnames;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4045
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4046 c->c_globals = PyDict_New();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4047 if (c->c_globals == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4048 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4049 c->c_freevars = PyDict_New();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4050 if (c->c_freevars == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4051 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4052 c->c_cellvars = PyDict_New();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4053 if (c->c_cellvars == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4054 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4055
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4056 nlocals = PyList_GET_SIZE(varnames);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4057 c->c_argcount = nlocals;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4058 nfrees = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4059 ncells = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4060 for (i = 0; i < nlocals; ++i) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4061 v = PyInt_FromLong(i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4062 if (PyDict_SetItem(c->c_locals,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4063 PyList_GET_ITEM(varnames, i), v) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4064 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4065 Py_DECREF(v);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4066 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4067
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4068 /* XXX The cases below define the rules for whether a name is
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4069 local or global. The logic could probably be clearer. */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4070 pos = 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4071 while (PyDict_Next(st->st_cur, &pos, &name, &v)) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4072 info = PyInt_AS_LONG(v);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4073
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4074 if (info & DEF_FREE_GLOBAL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4075 /* undo the original DEF_FREE */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4076 info &= ~(DEF_FREE | DEF_FREE_CLASS);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4077
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4078
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4079 /* Seperate logic for DEF_FREE. If it occurs in a
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4080 function, it indicates a local that we must
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4081 allocate storage for (a cell var). If it occurs in
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4082 a class, then the class has a method and a free
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4083 variable with the same name.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4084 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4085
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4086 if ((info & (DEF_FREE | DEF_FREE_CLASS))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4087 && (info & (DEF_LOCAL | DEF_PARAM))) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4088 PyObject *dict;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4089 if (st->st_cur_type == TYPE_FUNCTION) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4090 v = PyInt_FromLong(ncells++);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4091 dict = c->c_cellvars;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4092 } else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4093 v = PyInt_FromLong(nfrees++);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4094 dict = c->c_freevars;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4095 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4096 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4097 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4098 if (PyDict_SetItem(dict, name, v) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4099 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4100 Py_DECREF(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4101 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4102
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4103 if (info & DEF_STAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4104 c->c_argcount--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4105 c->c_flags |= CO_VARARGS;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4106 } else if (info & DEF_DOUBLESTAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4107 c->c_argcount--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4108 c->c_flags |= CO_VARKEYWORDS;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4109 } else if (info & DEF_INTUPLE)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4110 c->c_argcount--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4111 else if (info & DEF_GLOBAL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4112 if ((info & DEF_PARAM)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4113 && (PyString_AS_STRING(name)[0] != '.')){
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4114 char buf[500];
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4115 sprintf(buf,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4116 "name '%.400s' is local and global",
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4117 PyString_AS_STRING(name));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4118 com_error(c, PyExc_SyntaxError, buf);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4119 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4120 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4121 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4122 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4123 } else if (info & DEF_FREE_GLOBAL) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4124 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4125 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4126 } else if ((info & DEF_LOCAL) && !(info & DEF_PARAM)) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4127 v = PyInt_FromLong(nlocals++);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4128 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4129 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4130 if (PyDict_SetItem(c->c_locals, name, v) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4131 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4132 Py_DECREF(v);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4133 if (st->st_cur_type != TYPE_CLASS)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4134 if (PyList_Append(c->c_varnames, name) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4135 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4136 } else if (is_free(info)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4137 if (st->st_nested) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4138 v = PyInt_FromLong(nfrees++);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4139 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4140 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4141 if (PyDict_SetItem(c->c_freevars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4142 name, v) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4143 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4144 Py_DECREF(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4145 } else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4146 if (PyDict_SetItem(c->c_globals, name,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4147 implicit) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4148 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4149 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4150 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4151 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4152
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4153 /* The cell vars are the first elements of the closure,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4154 followed by the free vars. Update the offsets in
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4155 c_freevars to account for number of cellvars. */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4156 pos = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4157 while (PyDict_Next(c->c_freevars, &pos, &name, &v)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4158 int i = PyInt_AS_LONG(v) + ncells;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4159 PyObject *o = PyInt_FromLong(i);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4160 if (PyDict_SetItem(c->c_freevars, name, o) < 0) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4161 Py_DECREF(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4162 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4163 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4164 Py_DECREF(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4165 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4166
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4167 if (st->st_cur_type == TYPE_FUNCTION)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4168 c->c_nlocals = nlocals;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4169
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4170 if (st->st_cur_type != TYPE_MODULE)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4171 c->c_flags |= CO_NEWLOCALS;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4172 if (st->st_cur_type == TYPE_FUNCTION) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4173 if (PyDict_GetItemString(st->st_cur, NOOPT) == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4174 c->c_flags |= CO_OPTIMIZED;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4175 else if (ncells || nfrees) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4176 char buf[256];
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4177 /* XXX need better error message */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4178 sprintf(buf,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4179 "function %.100s: may not use lexical scoping"
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4180 " and 'import *' or exec in same function",
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4181 PyString_AS_STRING(st->st_cur_name));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4182 com_error(c, PyExc_SyntaxError, buf);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4183 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4184 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4185 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4186 return 0;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4187
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4188 fail:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4189 /* is this always the right thing to do? */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4190 Py_XDECREF(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4191 return -1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4192 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4193
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4194 static struct symtable *
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4195 symtable_init()
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4196 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4197 struct symtable *st;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4198 PyObject *d;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4199
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4200 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4201 if (st == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4202 return NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4203 st->st_pass = 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4204 if ((st->st_stack = PyList_New(0)) == NULL)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4205 goto fail;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4206 if ((st->st_symbols = PyDict_New()) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4207 goto fail;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4208 if ((st->st_children = PyDict_New()) == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4209 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4210 if ((st->st_varnames = PyDict_New()) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4211 goto fail;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4212 if ((d = PyDict_New()) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4213 goto fail;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4214 if (PyDict_SetItemString(st->st_symbols, TOP, d) < 0)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4215 goto fail;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4216 Py_DECREF(d);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4217 st->st_global = d;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4218 st->st_cur = NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4219 st->st_cur_id = NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4220 st->st_cur_name = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4221 st->st_cur_children = NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4222 st->st_cur_type = 0;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4223 st->st_nested = 0;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4224 st->st_scopes = 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4225 st->st_errors = 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4226 st->st_tmpname = 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4227 st->st_private = NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4228 return st;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4229 fail:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4230 symtable_free(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4231 return NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4232 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4233
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4234 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4235 symtable_free(struct symtable *st)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4236 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4237 Py_XDECREF(st->st_symbols);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4238 Py_XDECREF(st->st_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4239 Py_XDECREF(st->st_children);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4240 Py_XDECREF(st->st_stack);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4241 Py_XDECREF(st->st_cur_id);
16353
ae04d614a3e0 plug leak detected by Barry
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16257
diff changeset
4242 Py_XDECREF(st->st_cur_name);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4243 PyMem_Free((void *)st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4244 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4245
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4246 static PyObject *
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4247 make_scope_info(PyObject *id, PyObject *name, int nested, int type)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4248 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4249 PyObject *t, *i1 = NULL, *i2 = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4250
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4251 t = PyTuple_New(4);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4252 if (t == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4253 return NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4254 i1 = PyInt_FromLong(nested);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4255 if (i1 == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4256 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4257 i2 = PyInt_FromLong(type);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4258 if (i2 == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4259 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4260
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4261 Py_INCREF(name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4262 Py_INCREF(id);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4263 PyTuple_SET_ITEM(t, 0, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4264 PyTuple_SET_ITEM(t, 1, id);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4265 /* i1 & i2 alloced here; don't need incref */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4266 PyTuple_SET_ITEM(t, 2, i1);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4267 PyTuple_SET_ITEM(t, 3, i2);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4268 return t;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4269 fail:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4270 Py_XDECREF(t);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4271 Py_XDECREF(i1);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4272 Py_XDECREF(i2);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4273 return NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4274 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4275
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4276 /* When the compiler exits a scope, it must should update the scope's
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4277 free variable information with the list of free variables in its
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4278 children.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4279
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4280 Variables that are free in children and defined in the current
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4281 scope are cellvars.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4282
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4283 If the scope being exited is defined at the top-level (st_nested is
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4284 false), free variables in children that are not defined here are
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4285 implicit globals.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4286
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4287 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4288
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4289 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4290 symtable_update_free_vars(struct symtable *st)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4291 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4292 PyObject *dict, *o, *child, *name;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4293 int i, def;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4294
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4295 if (st->st_cur_type == TYPE_CLASS)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4296 def = DEF_FREE_CLASS;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4297 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4298 def = DEF_FREE;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4299 for (i = 0; i < PyList_GET_SIZE(st->st_cur_children); ++i) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4300 int pos = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4301
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4302 child = PyList_GET_ITEM(st->st_cur_children, i);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4303 dict = PyDict_GetItem(st->st_symbols, child);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4304 if (dict == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4305 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4306 while (PyDict_Next(dict, &pos, &name, &o)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4307 int v = PyInt_AS_LONG(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4308 if (!(is_free(v)))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4309 continue; /* avoids indentation */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4310 if (st->st_nested) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4311 if (symtable_add_def_o(st, st->st_cur,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4312 name, def) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4313 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4314 } else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4315 if (symtable_check_global(st, child,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4316 name) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4317 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4318 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4319 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4320 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4321
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4322 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4323 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4324
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4325 /* If the current scope is a non-nested class or if name is not
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4326 defined in the current, non-nested scope, then it is an implicit
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4327 global in all nested scopes.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4328 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4329
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4330 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4331 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4332 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4333 PyObject *o;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4334 int v;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4335
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4336 if (st->st_cur_type == TYPE_CLASS)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4337 return symtable_undo_free(st, child, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4338 o = PyDict_GetItem(st->st_cur, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4339 if (o == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4340 return symtable_undo_free(st, child, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4341 v = PyInt_AS_LONG(o);
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4342 if (is_free(v) || (v & DEF_GLOBAL))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4343 return symtable_undo_free(st, child, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4344 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4345 return symtable_add_def_o(st, st->st_cur, name, DEF_FREE);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4346 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4347
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4348 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4349 symtable_undo_free(struct symtable *st, PyObject *id,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4350 PyObject *name)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4351 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4352 int i, v, x;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4353 PyObject *dict, *children, *info;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4354
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4355 dict = PyDict_GetItem(st->st_symbols, id);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4356 if (dict == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4357 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4358 info = PyDict_GetItem(dict, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4359 if (info == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4360 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4361 v = PyInt_AS_LONG(info);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4362 if (is_free(v)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4363 if (symtable_add_def_o(st, dict, name,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4364 DEF_FREE_GLOBAL) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4365 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4366 } else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4367 /* If the name is defined here or declared global,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4368 then the recursion stops. */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4369 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4370
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4371 children = PyDict_GetItem(st->st_children, id);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4372 for (i = 0; i < PyList_GET_SIZE(children); ++i) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4373 x = symtable_undo_free(st, PyList_GET_ITEM(children, i),
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4374 name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4375 if (x < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4376 return x;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4377 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4378 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4379 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4380
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4381
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4382 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4383 symtable_exit_scope(struct symtable *st)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4384 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4385 PyObject *o;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4386 int end;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4387
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4388 if (st->st_pass == 1)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4389 symtable_update_free_vars(st);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4390 if (st->st_cur_name) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4391 Py_XDECREF(st->st_cur_name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4392 Py_XDECREF(st->st_cur_id);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4393 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4394 end = PyList_GET_SIZE(st->st_stack) - 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4395 o = PyList_GET_ITEM(st->st_stack, end);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4396 st->st_cur_name = PyTuple_GET_ITEM(o, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4397 st->st_cur_id = PyTuple_GET_ITEM(o, 1);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4398 st->st_nested = PyInt_AS_LONG(PyTuple_GET_ITEM(o, 2));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4399 st->st_cur_type = PyInt_AS_LONG(PyTuple_GET_ITEM(o, 3));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4400 if (PySequence_DelItem(st->st_stack, end) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4401 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4402 return symtable_update_cur(st);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4403 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4404
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4405 static int
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4406 symtable_enter_scope(struct symtable *st, char *name, int type)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4407 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4408 PyObject *o;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4409
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4410 if (st->st_cur) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4411 /* push current scope info on stack */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4412 o = make_scope_info(st->st_cur_id, st->st_cur_name,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4413 st->st_nested, st->st_cur_type);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4414 if (o == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4415 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4416 if (PyList_Append(st->st_stack, o) < 0) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4417 Py_DECREF(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4418 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4419 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4420 Py_DECREF(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4421 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4422 st->st_cur_name = PyString_FromString(name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4423 if (st->st_nested || st->st_cur_type == TYPE_FUNCTION)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4424 st->st_nested = 1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4425 switch (type) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4426 case funcdef:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4427 case lambdef:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4428 st->st_cur_type = TYPE_FUNCTION;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4429 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4430 case classdef:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4431 st->st_cur_type = TYPE_CLASS;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4432 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4433 case single_input:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4434 case eval_input:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4435 case file_input:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4436 st->st_cur_type = TYPE_MODULE;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4437 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4438 default:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4439 fprintf(stderr, "invalid symtable scope: %d\n", type);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4440 return -1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4441 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4442 /* update st_cur_id and parent's st_cur_children */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4443 o = PyInt_FromLong(st->st_scopes++);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4444 if (o == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4445 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4446 if (st->st_cur_children) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4447 if (PyList_Append(st->st_cur_children, o) < 0) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4448 Py_DECREF(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4449 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4450 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4451 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4452 st->st_cur_id = o;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4453 /* create st_cur_children list */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4454 o = PyList_New(0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4455 if (o == NULL)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4456 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4457 if (PyDict_SetItem(st->st_children, st->st_cur_id, o) < 0) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4458 Py_DECREF(o);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4459 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4460 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4461 Py_DECREF(o);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4462
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4463 return symtable_update_cur(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4464 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4465
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4466 static int
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4467 symtable_update_cur(struct symtable *st)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4468 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4469 PyObject *s, *d, *l;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4470
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4471 s = st->st_cur_id;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4472 d = PyDict_GetItem(st->st_symbols, s);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4473 if (d == NULL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4474 if ((d = PyDict_New()) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4475 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4476 if (PyObject_SetItem(st->st_symbols, s, d) < 0) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4477 Py_DECREF(d);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4478 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4479 }
16408
ec709ee8e6fa add missing DECREF (thanks, Barry)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16398
diff changeset
4480 Py_DECREF(d);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4481 if (st->st_cur_type == TYPE_FUNCTION) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4482 if ((l = PyList_New(0)) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4483 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4484 if (PyDict_SetItem(st->st_varnames, s, l) < 0) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4485 Py_DECREF(l);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4486 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4487 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4488 Py_DECREF(l);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4489 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4490 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4491
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4492 st->st_cur = d;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4493
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4494 d = PyDict_GetItem(st->st_children, s);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4495 if (d == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4496 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4497 st->st_cur_children = d;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4498 return 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4499 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4500
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4501 static int
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4502 symtable_add_def(struct symtable *st, char *name, int flag)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4503 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4504 PyObject *s;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4505 char buffer[MANGLE_LEN];
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4506
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4507 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4508 name = buffer;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4509 if ((s = PyString_InternFromString(name)) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4510 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4511 return symtable_add_def_o(st, st->st_cur, s, flag);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4512 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4513
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4514 /* Must only be called with mangled names */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4515
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4516 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4517 symtable_add_def_o(struct symtable *st, PyObject *dict,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4518 PyObject *name, int flag)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4519 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4520 PyObject *o;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4521 int val;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4522
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4523 if ((o = PyDict_GetItem(dict, name))) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4524 val = PyInt_AS_LONG(o);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4525 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4526 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4527 PyString_AsString(name));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4528 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4529 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4530 val |= flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4531 } else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4532 val = flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4533 o = PyInt_FromLong(val);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4534 if (PyDict_SetItem(dict, name, o) < 0) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4535 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4536 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4537 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4538 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4539
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4540 if (flag & DEF_PARAM) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4541 PyObject *l = PyDict_GetItem(st->st_varnames,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4542 st->st_cur_id);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4543 if (l == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4544 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4545 if (PyList_Append(l, name) < 0)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4546 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4547 } else if (flag & DEF_GLOBAL) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4548 /* XXX need to update DEF_GLOBAL for other flags too;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4549 perhaps only DEF_FREE_GLOBAL */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4550 if ((o = PyDict_GetItem(st->st_global, name))) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4551 val = PyInt_AS_LONG(o);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4552 val |= flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4553 } else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4554 val = flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4555 o = PyInt_FromLong(val);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4556 if (PyDict_SetItem(st->st_global, name, o) < 0) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4557 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4558 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4559 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4560 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4561 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4562 return 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4563 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4564
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4565 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4566
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4567 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4568 symtable_node(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4569 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4570 int i, start = 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4571
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4572 loop:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4573 switch (TYPE(n)) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4574 case funcdef: {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4575 char *func_name = STR(CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4576 symtable_add_def(st, func_name, DEF_LOCAL);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4577 symtable_default_args(st, CHILD(n, 2));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4578 symtable_enter_scope(st, func_name, TYPE(n));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4579 symtable_funcdef(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4580 symtable_exit_scope(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4581 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4582 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4583 case lambdef:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4584 if (NCH(n) == 4)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4585 symtable_default_args(st, CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4586 symtable_enter_scope(st, "lambda", TYPE(n));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4587 symtable_funcdef(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4588 symtable_exit_scope(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4589 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4590 case classdef: {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4591 char *tmp, *class_name = STR(CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4592 symtable_add_def(st, class_name, DEF_LOCAL);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4593 if (TYPE(CHILD(n, 2)) == LPAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4594 node *bases = CHILD(n, 3);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4595 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4596 for (i = 0; i < NCH(bases); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4597 symtable_node(st, CHILD(bases, i));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4598 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4599 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4600 symtable_enter_scope(st, class_name, TYPE(n));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4601 tmp = st->st_private;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4602 st->st_private = class_name;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4603 symtable_node(st, CHILD(n, NCH(n) - 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4604 st->st_private = tmp;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4605 symtable_exit_scope(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4606 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4607 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4608 case if_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4609 for (i = 0; i + 3 < NCH(n); i += 4) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4610 if (is_constant_false(NULL, (CHILD(n, i + 1))))
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4611 continue;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4612 symtable_node(st, CHILD(n, i + 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4613 symtable_node(st, CHILD(n, i + 3));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4614 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4615 if (i + 2 < NCH(n))
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4616 symtable_node(st, CHILD(n, i + 2));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4617 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4618 case global_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4619 symtable_global(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4620 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4621 case import_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4622 symtable_import(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4623 break;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4624 case exec_stmt: {
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4625 PyObject *zero = PyInt_FromLong(0);
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4626 if (zero == NULL)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4627 st->st_errors++;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4628 else {
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4629 if (PyDict_SetItemString(st->st_cur, NOOPT,
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4630 zero) < 0)
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4631 st->st_errors++;
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4632 Py_DECREF(zero);
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4633 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4634 symtable_node(st, CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4635 if (NCH(n) > 2)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4636 symtable_node(st, CHILD(n, 3));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4637 if (NCH(n) > 4)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4638 symtable_node(st, CHILD(n, 5));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4639 break;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4640
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4641 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4642 case except_clause:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4643 if (NCH(n) == 4)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4644 symtable_assign(st, CHILD(n, 3), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4645 if (NCH(n) > 1) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4646 n = CHILD(n, 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4647 goto loop;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4648 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4649 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4650 case del_stmt:
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4651 symtable_assign(st, CHILD(n, 1), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4652 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4653 case expr_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4654 if (NCH(n) == 1)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4655 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4656 else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4657 if (TYPE(CHILD(n, 1)) == augassign) {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4658 symtable_assign(st, CHILD(n, 0), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4659 symtable_node(st, CHILD(n, 2));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4660 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4661 } else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4662 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4663 for (i = 0; i < NCH(n) - 2; i += 2)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4664 symtable_assign(st, CHILD(n, i), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4665 n = CHILD(n, NCH(n) - 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4666 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4667 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4668 goto loop;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4669 /* watchout for fall-through logic below */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4670 case argument:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4671 if (NCH(n) == 3) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4672 n = CHILD(n, 2);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4673 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4674 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4675 case listmaker:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4676 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4677 symtable_list_comprehension(st, CHILD(n, 1));
16138
49b7c1bc6004 Visit the initial test element of the listmaker for a list
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16137
diff changeset
4678 n = CHILD(n, 0);
49b7c1bc6004 Visit the initial test element of the listmaker for a list
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16137
diff changeset
4679 goto loop;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4680 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4681 case atom:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4682 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4683 symtable_add_use(st, STR(CHILD(n, 0)));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4684 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4685 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4686 case for_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4687 if (TYPE(n) == for_stmt) {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4688 symtable_assign(st, CHILD(n, 1), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4689 start = 3;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4690 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4691 default:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4692 if (NCH(n) == 1) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4693 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4694 goto loop;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4695 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4696 for (i = start; i < NCH(n); ++i)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4697 if (TYPE(CHILD(n, i)) >= single_input)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4698 symtable_node(st, CHILD(n, i));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4699 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4700 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4701
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4702 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4703 symtable_funcdef(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4704 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4705 node *body;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4706
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4707 if (TYPE(n) == lambdef) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4708 if (NCH(n) == 4)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4709 symtable_params(st, CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4710 } else
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4711 symtable_params(st, CHILD(n, 2));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4712 body = CHILD(n, NCH(n) - 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4713 symtable_node(st, body);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4714 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4715
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4716 /* The next two functions parse the argument tuple.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4717 symtable_default_arg() checks for names in the default arguments,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4718 which are references in the defining scope. symtable_params()
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4719 parses the parameter names, which are defined in the function's
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4720 body.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4721
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4722 varargslist:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4723 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4724 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4725 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4726
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4727 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4728 symtable_default_args(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4729 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4730 node *c;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4731 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4732
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4733 if (TYPE(n) == parameters) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4734 n = CHILD(n, 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4735 if (TYPE(n) == RPAR)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4736 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4737 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4738 REQ(n, varargslist);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4739 for (i = 0; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4740 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4741 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4742 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4743 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4744 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4745 symtable_node(st, CHILD(n, i));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4746 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4747 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4748
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4749 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4750 symtable_params(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4751 {
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4752 int i, complex = -1, ext = 0;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4753 node *c = NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4754
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4755 if (TYPE(n) == parameters) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4756 n = CHILD(n, 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4757 if (TYPE(n) == RPAR)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4758 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4759 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4760 REQ(n, varargslist);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4761 for (i = 0; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4762 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4763 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4764 ext = 1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4765 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4766 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4767 if (TYPE(c) == test) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4768 continue;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4769 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4770 if (TYPE(CHILD(c, 0)) == NAME)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4771 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4772 else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4773 char nbuf[10];
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4774 sprintf(nbuf, ".%d", i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4775 symtable_add_def(st, nbuf, DEF_PARAM);
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4776 complex = i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4777 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4778 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4779 if (ext) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4780 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4781 if (TYPE(c) == STAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4782 i++;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4783 symtable_add_def(st, STR(CHILD(n, i)),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4784 DEF_PARAM | DEF_STAR);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4785 i += 2;
16137
883cb0e31bb2 prevent symtable_params() from dereferencing off the end of the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16096
diff changeset
4786 if (i >= NCH(n))
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4787 c = NULL;
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4788 else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4789 c = CHILD(n, i);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4790 }
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4791 if (c && TYPE(c) == DOUBLESTAR) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4792 i++;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4793 symtable_add_def(st, STR(CHILD(n, i)),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4794 DEF_PARAM | DEF_DOUBLESTAR);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4795 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4796 }
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4797 if (complex >= 0) {
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4798 int j;
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4799 for (j = 0; j <= complex; j++) {
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4800 c = CHILD(n, j);
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4801 if (TYPE(c) == COMMA)
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4802 c = CHILD(n, ++j);
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4803 if (TYPE(CHILD(c, 0)) == LPAR)
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4804 symtable_params_fplist(st, CHILD(c, 1));
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4805 }
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
4806 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4807 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4808
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4809 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4810 symtable_params_fplist(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4811 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4812 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4813 node *c;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4814
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4815 REQ(n, fplist);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4816 for (i = 0; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4817 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4818 REQ(c, fpdef);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4819 if (NCH(c) == 1)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4820 symtable_add_def(st, STR(CHILD(c, 0)),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4821 DEF_PARAM | DEF_INTUPLE);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4822 else
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4823 symtable_params_fplist(st, CHILD(c, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4824 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4825
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4826 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4827
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4828 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4829 symtable_global(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4830 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4831 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4832
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4833 for (i = 1; i < NCH(n); i += 2)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4834 symtable_add_def(st, STR(CHILD(n, i)), DEF_GLOBAL);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4835 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4836
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4837 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4838 symtable_list_comprehension(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4839 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4840 char tmpname[12];
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4841
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4842 sprintf(tmpname, "[%d]", ++st->st_tmpname);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4843 symtable_add_def(st, tmpname, DEF_LOCAL);
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4844 symtable_assign(st, CHILD(n, 1), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4845 symtable_node(st, CHILD(n, 3));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4846 if (NCH(n) == 5)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4847 symtable_node(st, CHILD(n, 4));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4848 --st->st_tmpname;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4849 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4850
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4851 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4852 symtable_import(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4853 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4854 int i;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4855 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4856 | 'from' dotted_name 'import'
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4857 ('*' | import_as_name (',' import_as_name)*)
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4858 import_as_name: NAME [NAME NAME]
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4859 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4860
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4861 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4862 if (TYPE(CHILD(n, 3)) == STAR) {
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4863 PyObject *zero = PyInt_FromLong(0);
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4864 if (st->st_cur_type != TYPE_MODULE) {
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4865 PyErr_SetString(PyExc_SyntaxError,
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4866 ILLEGAL_IMPORT_STAR);
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4867 st->st_errors++;
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4868 return;
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4869 }
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4870 if (zero == NULL)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4871 st->st_errors++;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4872 else {
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4873 if (PyDict_SetItemString(st->st_cur, NOOPT,
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4874 zero) < 0)
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4875 st->st_errors++;
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4876 Py_DECREF(zero);
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
4877 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4878 } else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4879 for (i = 3; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4880 node *c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4881 if (NCH(c) > 1) /* import as */
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4882 symtable_assign(st, CHILD(c, 2),
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4883 DEF_IMPORT);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4884 else
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4885 symtable_assign(st, CHILD(c, 0),
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4886 DEF_IMPORT);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4887 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4888 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4889 } else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4890 for (i = 1; i < NCH(n); i += 2) {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4891 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4892 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4893 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4894 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4895
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4896 static void
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4897 symtable_assign(struct symtable *st, node *n, int flag)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4898 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4899 node *tmp;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4900 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4901
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4902 loop:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4903 switch (TYPE(n)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4904 case lambdef:
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4905 /* invalid assignment, e.g. lambda x:x=2. The next
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4906 pass will catch this error. */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4907 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4908 case power:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4909 if (NCH(n) > 2) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4910 for (i = 2; i < NCH(n); ++i)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4911 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4912 symtable_node(st, CHILD(n, i));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4913 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4914 if (NCH(n) > 1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4915 symtable_node(st, CHILD(n, 0));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4916 symtable_node(st, CHILD(n, 1));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4917 } else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4918 n = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4919 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4920 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4921 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4922 case listmaker:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4923 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4924 symtable_list_comprehension(st, CHILD(n, 1));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4925 else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4926 for (i = 0; i < NCH(n); i += 2)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4927 symtable_assign(st, CHILD(n, i), flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4928 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4929 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4930 case exprlist:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4931 case testlist:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4932 if (NCH(n) == 1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4933 n = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4934 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4935 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4936 else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4937 int i;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4938 for (i = 0; i < NCH(n); i += 2)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4939 symtable_assign(st, CHILD(n, i), flag);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4940 return;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4941 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4942 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4943 case atom:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4944 tmp = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4945 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4946 n = CHILD(n, 1);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4947 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4948 } else if (TYPE(tmp) == NAME)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4949 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4950 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4951 case dotted_as_name:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4952 if (NCH(n) == 3)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4953 symtable_add_def(st, STR(CHILD(n, 2)),
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4954 DEF_LOCAL | flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4955 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4956 symtable_add_def(st,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4957 STR(CHILD(CHILD(n,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4958 0), 0)),
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4959 DEF_LOCAL | flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4960 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4961 case dotted_name:
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4962 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4963 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4964 case NAME:
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
4965 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4966 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4967 default:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4968 if (NCH(n) == 0)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4969 return;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4970 if (NCH(n) != 1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4971 DUMP(n);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4972 Py_FatalError("too many children in default case\n");
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4973 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4974 n = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4975 goto loop;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4976 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4977 }