Mercurial > cpython-lru_cache-weakref
annotate Lib/compiler/pycodegen.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 55130516d1d2 |
| children |
| rev | line source |
|---|---|
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1 import imp |
| 12009 | 2 import os |
|
11710
38c4d61db7ea
rapid evolution towards producing real .pyc files (even though I don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11705
diff
changeset
|
3 import marshal |
|
11724
71d038db0dd2
now produces valid pyc files for a least a trivial subset of the
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11710
diff
changeset
|
4 import struct |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
5 import sys |
| 12009 | 6 from cStringIO import StringIO |
| 7 | |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
8 from compiler import ast, parse, walk, syntax |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
9 from compiler import pyassem, misc, future, symbols |
|
77913
55130516d1d2
Issue #15212: fix typo in compiler module (rename SC_GLOBAL_EXPLICT to SC_GLOBAL_EXPLICIT).
Antoine Pitrou <solipsis@pitrou.net>
parents:
68256
diff
changeset
|
10 from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICIT, \ |
|
51425
4809afa85a9b
Issue #999042: The Python compiler now handles explict global statements
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
51420
diff
changeset
|
11 SC_FREE, SC_CELL |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
12 from compiler.consts import (CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, |
|
36821
12bcd16c31ad
Hopefully this removes all vestiges of CO_GENERATOR_ALLOWED that can
Neal Norwitz <nnorwitz@gmail.com>
parents:
36820
diff
changeset
|
13 CO_NESTED, CO_GENERATOR, CO_FUTURE_DIVISION, |
|
45897
c77e3119e267
Fixed compiler module so __future__ print_function is compilable.
Eric Smith <eric@trueblade.com>
parents:
45703
diff
changeset
|
14 CO_FUTURE_ABSIMPORT, CO_FUTURE_WITH_STATEMENT, CO_FUTURE_PRINT_FUNCTION) |
|
19619
5663b883ea5d
Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19609
diff
changeset
|
15 from compiler.pyassem import TupleArg |
| 12009 | 16 |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
17 # XXX The version-specific code can go, since this code only works with 2.x. |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
18 # Do we have Python 1.x or Python 2.x? |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
19 try: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
20 VERSION = sys.version_info[0] |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
21 except AttributeError: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
22 VERSION = 1 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
23 |
|
12563
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
24 callfunc_opcode_info = { |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
25 # (Have *args, Have **args) : opcode |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
26 (0,0) : "CALL_FUNCTION", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
27 (1,0) : "CALL_FUNCTION_VAR", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
28 (0,1) : "CALL_FUNCTION_KW", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
29 (1,1) : "CALL_FUNCTION_VAR_KW", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
30 } |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
31 |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
32 LOOP = 1 |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
33 EXCEPT = 2 |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
34 TRY_FINALLY = 3 |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
35 END_FINALLY = 4 |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
36 |
| 19946 | 37 def compileFile(filename, display=0): |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
38 f = open(filename, 'U') |
| 12009 | 39 buf = f.read() |
| 40 f.close() | |
| 41 mod = Module(buf, filename) | |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
42 try: |
|
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
43 mod.compile(display) |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
44 except SyntaxError: |
|
19943
6773dcfe34a3
Last set of change to get regression tests to pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19939
diff
changeset
|
45 raise |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
46 else: |
|
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
47 f = open(filename + "c", "wb") |
|
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
48 mod.dump(f) |
|
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
49 f.close() |
| 12009 | 50 |
| 19946 | 51 def compile(source, filename, mode, flags=None, dont_inherit=None): |
| 52 """Replacement for builtin compile() function""" | |
| 53 if flags is not None or dont_inherit is not None: | |
| 54 raise RuntimeError, "not implemented yet" | |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
55 |
| 19946 | 56 if mode == "single": |
| 57 gen = Interactive(source, filename) | |
| 58 elif mode == "exec": | |
| 59 gen = Module(source, filename) | |
| 60 elif mode == "eval": | |
| 61 gen = Expression(source, filename) | |
| 62 else: | |
| 63 raise ValueError("compile() 3rd arg must be 'exec' or " | |
| 64 "'eval' or 'single'") | |
| 65 gen.compile() | |
| 66 return gen.code | |
| 67 | |
| 68 class AbstractCompileMode: | |
| 69 | |
| 70 mode = None # defined by subclass | |
| 71 | |
| 12009 | 72 def __init__(self, source, filename): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
73 self.source = source |
| 19946 | 74 self.filename = filename |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
75 self.code = None |
| 12009 | 76 |
| 19946 | 77 def _get_tree(self): |
| 78 tree = parse(self.source, self.mode) | |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
79 misc.set_filename(self.filename, tree) |
|
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
80 syntax.check(tree) |
| 19946 | 81 return tree |
| 82 | |
| 83 def compile(self): | |
| 84 pass # implemented by subclass | |
| 85 | |
| 86 def getCode(self): | |
| 87 return self.code | |
| 88 | |
| 89 class Expression(AbstractCompileMode): | |
| 90 | |
| 91 mode = "eval" | |
| 92 | |
| 93 def compile(self): | |
| 94 tree = self._get_tree() | |
| 95 gen = ExpressionCodeGenerator(tree) | |
| 96 self.code = gen.getCode() | |
| 97 | |
| 98 class Interactive(AbstractCompileMode): | |
| 99 | |
| 100 mode = "single" | |
| 101 | |
| 102 def compile(self): | |
| 103 tree = self._get_tree() | |
| 104 gen = InteractiveCodeGenerator(tree) | |
| 105 self.code = gen.getCode() | |
| 106 | |
| 107 class Module(AbstractCompileMode): | |
| 108 | |
| 109 mode = "exec" | |
| 110 | |
| 111 def compile(self, display=0): | |
| 112 tree = self._get_tree() | |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
113 gen = ModuleCodeGenerator(tree) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
114 if display: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
115 import pprint |
|
17346
f8fdbeb73667
Add support for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15379
diff
changeset
|
116 print pprint.pprint(tree) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
117 self.code = gen.getCode() |
| 12009 | 118 |
| 119 def dump(self, f): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
120 f.write(self.getPycHeader()) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
121 marshal.dump(self.code, f) |
| 12009 | 122 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
123 MAGIC = imp.get_magic() |
| 12009 | 124 |
| 125 def getPycHeader(self): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
126 # compile.c uses marshal to write a long directly, with |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
127 # calling the interface that would also generate a 1-byte code |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
128 # to indicate the type of the value. simplest way to get the |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
129 # same effect is to call marshal and then skip the code. |
|
23710
44b9977d4848
Remove uses of string module and stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents:
21958
diff
changeset
|
130 mtime = os.path.getmtime(self.filename) |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
131 mtime = struct.pack('<i', mtime) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
132 return self.MAGIC + mtime |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
133 |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
134 class LocalNameFinder: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
135 """Find local names in scope""" |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
136 def __init__(self, names=()): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
137 self.names = misc.Set() |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
138 self.globals = misc.Set() |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
139 for name in names: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
140 self.names.add(name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
141 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
142 # XXX list comprehensions and for loops |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
143 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
144 def getLocals(self): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
145 for elt in self.globals.elements(): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
146 if self.names.has_elt(elt): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
147 self.names.remove(elt) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
148 return self.names |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
149 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
150 def visitDict(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
151 pass |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
152 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
153 def visitGlobal(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
154 for name in node.names: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
155 self.globals.add(name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
156 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
157 def visitFunction(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
158 self.names.add(node.name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
159 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
160 def visitLambda(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
161 pass |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
162 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
163 def visitImport(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
164 for name, alias in node.names: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
165 self.names.add(alias or name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
166 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
167 def visitFrom(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
168 for name, alias in node.names: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
169 self.names.add(alias or name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
170 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
171 def visitClass(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
172 self.names.add(node.name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
173 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
174 def visitAssName(self, node): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
175 self.names.add(node.name) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
176 |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
177 def is_constant_false(node): |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
178 if isinstance(node, ast.Const): |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
179 if not node.value: |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
180 return 1 |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
181 return 0 |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
182 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
183 class CodeGenerator: |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
184 """Defines basic code generator for Python bytecode |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
185 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
186 This class is an abstract base class. Concrete subclasses must |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
187 define an __init__() that defines self.graph and then calls the |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
188 __init__() defined in this class. |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
189 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
190 The concrete class must also define the class attributes |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
191 NameFinder, FunctionGen, and ClassGen. These attributes can be |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
192 defined in the initClass() method, which is a hook for |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
193 initializing these methods after all the classes have been |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
194 defined. |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
195 """ |
|
11743
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
196 |
| 12009 | 197 optimized = 0 # is namespace access optimized? |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
198 __initialized = None |
| 19522 | 199 class_name = None # provide default for instance variable |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
200 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
201 def __init__(self): |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
202 if self.__initialized is None: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
203 self.initClass() |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
204 self.__class__.__initialized = 1 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
205 self.checkClass() |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
206 self.locals = misc.Stack() |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
207 self.setups = misc.Stack() |
|
14362
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
208 self.last_lineno = None |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
209 self._setupGraphDelegation() |
|
19936
6944f5b9de03
support true division
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19923
diff
changeset
|
210 self._div_op = "BINARY_DIVIDE" |
|
11793
d2ab9553f871
support for arglists with implicit tuple unpacks
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11789
diff
changeset
|
211 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
212 # XXX set flags based on future features |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
213 futures = self.get_module().futures |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
214 for feature in futures: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
215 if feature == "division": |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
216 self.graph.setFlag(CO_FUTURE_DIVISION) |
|
19936
6944f5b9de03
support true division
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19923
diff
changeset
|
217 self._div_op = "BINARY_TRUE_DIVIDE" |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
218 elif feature == "absolute_import": |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
219 self.graph.setFlag(CO_FUTURE_ABSIMPORT) |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
220 elif feature == "with_statement": |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
221 self.graph.setFlag(CO_FUTURE_WITH_STATEMENT) |
|
45897
c77e3119e267
Fixed compiler module so __future__ print_function is compilable.
Eric Smith <eric@trueblade.com>
parents:
45703
diff
changeset
|
222 elif feature == "print_function": |
|
c77e3119e267
Fixed compiler module so __future__ print_function is compilable.
Eric Smith <eric@trueblade.com>
parents:
45703
diff
changeset
|
223 self.graph.setFlag(CO_FUTURE_PRINT_FUNCTION) |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
224 |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
225 def initClass(self): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
226 """This method is called once for each class""" |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
227 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
228 def checkClass(self): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
229 """Verify that class is constructed correctly""" |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
230 try: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
231 assert hasattr(self, 'graph') |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
232 assert getattr(self, 'NameFinder') |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
233 assert getattr(self, 'FunctionGen') |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
234 assert getattr(self, 'ClassGen') |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
235 except AssertionError, msg: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
236 intro = "Bad class construction for %s" % self.__class__.__name__ |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
237 raise AssertionError, intro |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
238 |
| 12009 | 239 def _setupGraphDelegation(self): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
240 self.emit = self.graph.emit |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
241 self.newBlock = self.graph.newBlock |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
242 self.startBlock = self.graph.startBlock |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
243 self.nextBlock = self.graph.nextBlock |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
244 self.setDocstring = self.graph.setDocstring |
|
11793
d2ab9553f871
support for arglists with implicit tuple unpacks
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11789
diff
changeset
|
245 |
| 12009 | 246 def getCode(self): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
247 """Return a code object""" |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
248 return self.graph.getCode() |
|
11754
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
249 |
| 19522 | 250 def mangle(self, name): |
| 251 if self.class_name is not None: | |
| 252 return misc.mangle(name, self.class_name) | |
| 253 else: | |
| 254 return name | |
| 255 | |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
256 def parseSymbols(self, tree): |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
257 s = symbols.SymbolVisitor() |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
258 walk(tree, s) |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
259 return s.scopes |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
260 |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
261 def get_module(self): |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
262 raise RuntimeError, "should be implemented by subclasses" |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
263 |
| 12009 | 264 # Next five methods handle name access |
|
11724
71d038db0dd2
now produces valid pyc files for a least a trivial subset of the
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11710
diff
changeset
|
265 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
266 def isLocalName(self, name): |
| 11806 | 267 return self.locals.top().has_elt(name) |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
268 |
|
11743
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
269 def storeName(self, name): |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
270 self._nameOp('STORE', name) |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
271 |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
272 def loadName(self, name): |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
273 self._nameOp('LOAD', name) |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
274 |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
275 def delName(self, name): |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
276 self._nameOp('DELETE', name) |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
277 |
| 12009 | 278 def _nameOp(self, prefix, name): |
| 19522 | 279 name = self.mangle(name) |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
280 scope = self.scope.check_name(name) |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
281 if scope == SC_LOCAL: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
282 if not self.optimized: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
283 self.emit(prefix + '_NAME', name) |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
284 else: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
285 self.emit(prefix + '_FAST', name) |
|
77913
55130516d1d2
Issue #15212: fix typo in compiler module (rename SC_GLOBAL_EXPLICT to SC_GLOBAL_EXPLICIT).
Antoine Pitrou <solipsis@pitrou.net>
parents:
68256
diff
changeset
|
286 elif scope == SC_GLOBAL_EXPLICIT: |
|
51425
4809afa85a9b
Issue #999042: The Python compiler now handles explict global statements
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
51420
diff
changeset
|
287 self.emit(prefix + '_GLOBAL', name) |
|
4809afa85a9b
Issue #999042: The Python compiler now handles explict global statements
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
51420
diff
changeset
|
288 elif scope == SC_GLOBAL_IMPLICIT: |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
289 if not self.optimized: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
290 self.emit(prefix + '_NAME', name) |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
291 else: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
292 self.emit(prefix + '_GLOBAL', name) |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
293 elif scope == SC_FREE or scope == SC_CELL: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
294 self.emit(prefix + '_DEREF', name) |
| 12009 | 295 else: |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
296 raise RuntimeError, "unsupported scope for var %s: %d" % \ |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
297 (name, scope) |
| 12009 | 298 |
|
17459
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
299 def _implicitNameOp(self, prefix, name): |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
300 """Emit name ops for names generated implicitly by for loops |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
301 |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
302 The interpreter generates names that start with a period or |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
303 dollar sign. The symbol table ignores these names because |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
304 they aren't present in the program text. |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
305 """ |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
306 if self.optimized: |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
307 self.emit(prefix + '_FAST', name) |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
308 else: |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
309 self.emit(prefix + '_NAME', name) |
|
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
310 |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
311 # The set_lineno() function and the explicit emit() calls for |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
312 # SET_LINENO below are only used to generate the line number table. |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
313 # As of Python 2.3, the interpreter does not have a SET_LINENO |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
314 # instruction. pyassem treats SET_LINENO opcodes as a special case. |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
315 |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
316 def set_lineno(self, node, force=False): |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
317 """Emit SET_LINENO if necessary. |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
318 |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
319 The instruction is considered necessary if the node has a |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
320 lineno attribute and it is different than the last lineno |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
321 emitted. |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
322 |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
323 Returns true if SET_LINENO was emitted. |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
324 |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
325 There are no rules for when an AST node should have a lineno |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
326 attribute. The transformer and AST code need to be reviewed |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
327 and a consistent policy implemented and documented. Until |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
328 then, this method works around missing line numbers. |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
329 """ |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
330 lineno = getattr(node, 'lineno', None) |
| 17453 | 331 if lineno is not None and (lineno != self.last_lineno |
| 332 or force): | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
333 self.emit('SET_LINENO', lineno) |
|
14362
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
334 self.last_lineno = lineno |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
335 return True |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
336 return False |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
337 |
| 12009 | 338 # The first few visitor methods handle nodes that generator new |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
339 # code objects. They use class attributes to determine what |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
340 # specialized code generators to use. |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
341 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
342 NameFinder = LocalNameFinder |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
343 FunctionGen = None |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
344 ClassGen = None |
| 12009 | 345 |
| 346 def visitModule(self, node): | |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
347 self.scopes = self.parseSymbols(node) |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
348 self.scope = self.scopes[node] |
|
17459
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
349 self.emit('SET_LINENO', 0) |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
350 if node.doc: |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
351 self.emit('LOAD_CONST', node.doc) |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
352 self.storeName('__doc__') |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
353 lnf = walk(node.node, self.NameFinder(), verbose=0) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
354 self.locals.push(lnf.getLocals()) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
355 self.visit(node.node) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
356 self.emit('LOAD_CONST', None) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
357 self.emit('RETURN_VALUE') |
| 12009 | 358 |
|
21522
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
359 def visitExpression(self, node): |
|
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
360 self.set_lineno(node) |
|
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
361 self.scopes = self.parseSymbols(node) |
|
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
362 self.scope = self.scopes[node] |
|
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
363 self.visit(node.node) |
|
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
364 self.emit('RETURN_VALUE') |
|
cb55f15b2730
Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents:
21059
diff
changeset
|
365 |
| 12009 | 366 def visitFunction(self, node): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
367 self._visitFuncOrLambda(node, isLambda=0) |
| 17410 | 368 if node.doc: |
| 369 self.setDocstring(node.doc) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
370 self.storeName(node.name) |
| 12009 | 371 |
| 372 def visitLambda(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
373 self._visitFuncOrLambda(node, isLambda=1) |
| 12009 | 374 |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
375 def _visitFuncOrLambda(self, node, isLambda=0): |
|
32888
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
376 if not isLambda and node.decorators: |
|
33166
2e8d2bdd33de
This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents:
33020
diff
changeset
|
377 for decorator in node.decorators.nodes: |
|
32888
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
378 self.visit(decorator) |
|
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
379 ndecorators = len(node.decorators.nodes) |
|
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
380 else: |
|
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
381 ndecorators = 0 |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33166
diff
changeset
|
382 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
383 gen = self.FunctionGen(node, self.scopes, isLambda, |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
384 self.class_name, self.get_module()) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
385 walk(node.code, gen) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
386 gen.finish() |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
387 self.set_lineno(node) |
| 12009 | 388 for default in node.defaults: |
| 389 self.visit(default) | |
|
39370
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
390 self._makeClosure(gen, len(node.defaults)) |
|
32888
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
391 for i in range(ndecorators): |
|
c7e03cf8333e
PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
32528
diff
changeset
|
392 self.emit('CALL_FUNCTION', 1) |
| 12009 | 393 |
| 394 def visitClass(self, node): | |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
395 gen = self.ClassGen(node, self.scopes, |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
396 self.get_module()) |
| 12009 | 397 walk(node.code, gen) |
| 398 gen.finish() | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
399 self.set_lineno(node) |
| 12009 | 400 self.emit('LOAD_CONST', node.name) |
| 401 for base in node.bases: | |
| 402 self.visit(base) | |
| 403 self.emit('BUILD_TUPLE', len(node.bases)) | |
|
39370
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
404 self._makeClosure(gen, 0) |
| 12009 | 405 self.emit('CALL_FUNCTION', 0) |
| 406 self.emit('BUILD_CLASS') | |
| 407 self.storeName(node.name) | |
| 408 | |
| 409 # The rest are standard visitor methods | |
| 410 | |
| 411 # The next few implement control-flow statements | |
| 412 | |
| 413 def visitIf(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
414 end = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
415 numtests = len(node.tests) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
416 for i in range(numtests): |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
417 test, suite = node.tests[i] |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
418 if is_constant_false(test): |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
419 # XXX will need to check generator stuff here |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
420 continue |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
421 self.set_lineno(test) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
422 self.visit(test) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
423 nextTest = self.newBlock() |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
424 self.emit('POP_JUMP_IF_FALSE', nextTest) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
425 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
426 self.visit(suite) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
427 self.emit('JUMP_FORWARD', end) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
428 self.startBlock(nextTest) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
429 if node.else_: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
430 self.visit(node.else_) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
431 self.nextBlock(end) |
| 12009 | 432 |
| 433 def visitWhile(self, node): | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
434 self.set_lineno(node) |
| 12009 | 435 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
436 loop = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
437 else_ = self.newBlock() |
| 12009 | 438 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
439 after = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
440 self.emit('SETUP_LOOP', after) |
| 12009 | 441 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
442 self.nextBlock(loop) |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
443 self.setups.push((LOOP, loop)) |
| 12009 | 444 |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
445 self.set_lineno(node, force=True) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
446 self.visit(node.test) |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
447 self.emit('POP_JUMP_IF_FALSE', else_ or after) |
| 12009 | 448 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
449 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
450 self.visit(node.body) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
451 self.emit('JUMP_ABSOLUTE', loop) |
| 12009 | 452 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
453 self.startBlock(else_) # or just the POPs if not else clause |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
454 self.emit('POP_BLOCK') |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
455 self.setups.pop() |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
456 if node.else_: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
457 self.visit(node.else_) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
458 self.nextBlock(after) |
| 12009 | 459 |
| 460 def visitFor(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
461 start = self.newBlock() |
| 12009 | 462 anchor = self.newBlock() |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
463 after = self.newBlock() |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
464 self.setups.push((LOOP, start)) |
| 12009 | 465 |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
466 self.set_lineno(node) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
467 self.emit('SETUP_LOOP', after) |
| 12009 | 468 self.visit(node.list) |
|
19531
deacd60272be
Generate FOR_ITER-based loops instead of old FOR_LOOP-based loops
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19522
diff
changeset
|
469 self.emit('GET_ITER') |
|
deacd60272be
Generate FOR_ITER-based loops instead of old FOR_LOOP-based loops
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19522
diff
changeset
|
470 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
471 self.nextBlock(start) |
|
17460
99c471ced170
Pop loop off the loop stack before handling the loop's else clause.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17459
diff
changeset
|
472 self.set_lineno(node, force=1) |
|
19531
deacd60272be
Generate FOR_ITER-based loops instead of old FOR_LOOP-based loops
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19522
diff
changeset
|
473 self.emit('FOR_ITER', anchor) |
| 12009 | 474 self.visit(node.assign) |
| 475 self.visit(node.body) | |
| 476 self.emit('JUMP_ABSOLUTE', start) | |
|
19534
de10151a7f63
Make sure the JUMP_ABSOLUTE and POP_BLOCK at the end of a for loop are
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19531
diff
changeset
|
477 self.nextBlock(anchor) |
| 12009 | 478 self.emit('POP_BLOCK') |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
479 self.setups.pop() |
| 12009 | 480 if node.else_: |
| 481 self.visit(node.else_) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
482 self.nextBlock(after) |
| 12009 | 483 |
| 484 def visitBreak(self, node): | |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
485 if not self.setups: |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
486 raise SyntaxError, "'break' outside loop (%s, %d)" % \ |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
487 (node.filename, node.lineno) |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
488 self.set_lineno(node) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
489 self.emit('BREAK_LOOP') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
490 |
| 12009 | 491 def visitContinue(self, node): |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
492 if not self.setups: |
| 12009 | 493 raise SyntaxError, "'continue' outside loop (%s, %d)" % \ |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
494 (node.filename, node.lineno) |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
495 kind, block = self.setups.top() |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
496 if kind == LOOP: |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
497 self.set_lineno(node) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
498 self.emit('JUMP_ABSOLUTE', block) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
499 self.nextBlock() |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
500 elif kind == EXCEPT or kind == TRY_FINALLY: |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
501 self.set_lineno(node) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
502 # find the block that starts the loop |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
503 top = len(self.setups) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
504 while top > 0: |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
505 top = top - 1 |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
506 kind, loop_block = self.setups[top] |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
507 if kind == LOOP: |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
508 break |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
509 if kind != LOOP: |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
510 raise SyntaxError, "'continue' outside loop (%s, %d)" % \ |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
511 (node.filename, node.lineno) |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
512 self.emit('CONTINUE_LOOP', loop_block) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
513 self.nextBlock() |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
514 elif kind == END_FINALLY: |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
515 msg = "'continue' not allowed inside 'finally' clause (%s, %d)" |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
516 raise SyntaxError, msg % (node.filename, node.lineno) |
| 12009 | 517 |
| 518 def visitTest(self, node, jump): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
519 end = self.newBlock() |
| 12009 | 520 for child in node.nodes[:-1]: |
| 521 self.visit(child) | |
| 522 self.emit(jump, end) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
523 self.nextBlock() |
| 12009 | 524 self.visit(node.nodes[-1]) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
525 self.nextBlock(end) |
| 12009 | 526 |
| 527 def visitAnd(self, node): | |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
528 self.visitTest(node, 'JUMP_IF_FALSE_OR_POP') |
| 12009 | 529 |
| 530 def visitOr(self, node): | |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
531 self.visitTest(node, 'JUMP_IF_TRUE_OR_POP') |
| 12009 | 532 |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
533 def visitIfExp(self, node): |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
534 endblock = self.newBlock() |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
535 elseblock = self.newBlock() |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
536 self.visit(node.test) |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
537 self.emit('POP_JUMP_IF_FALSE', elseblock) |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
538 self.visit(node.then) |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
539 self.emit('JUMP_FORWARD', endblock) |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
540 self.nextBlock(elseblock) |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
541 self.visit(node.else_) |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
542 self.nextBlock(endblock) |
|
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
543 |
| 12009 | 544 def visitCompare(self, node): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
545 self.visit(node.expr) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
546 cleanup = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
547 for op, code in node.ops[:-1]: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
548 self.visit(code) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
549 self.emit('DUP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
550 self.emit('ROT_THREE') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
551 self.emit('COMPARE_OP', op) |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
552 self.emit('JUMP_IF_FALSE_OR_POP', cleanup) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
553 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
554 # now do the last comparison |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
555 if node.ops: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
556 op, code = node.ops[-1] |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
557 self.visit(code) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
558 self.emit('COMPARE_OP', op) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
559 if len(node.ops) > 1: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
560 end = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
561 self.emit('JUMP_FORWARD', end) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
562 self.startBlock(cleanup) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
563 self.emit('ROT_TWO') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
564 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
565 self.nextBlock(end) |
| 12009 | 566 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
567 # list comprehensions |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
568 def visitListComp(self, node): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
569 self.set_lineno(node) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
570 # setup list |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
571 self.emit('BUILD_LIST', 0) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
572 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
573 stack = [] |
| 17453 | 574 for i, for_ in zip(range(len(node.quals)), node.quals): |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
575 start, anchor = self.visit(for_) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
576 cont = None |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
577 for if_ in for_.ifs: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
578 if cont is None: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
579 cont = self.newBlock() |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
580 self.visit(if_, cont) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
581 stack.insert(0, (start, cont, anchor)) |
| 17453 | 582 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
583 self.visit(node.expr) |
|
50204
94e79d78dff0
Issue #2183: Simplify and optimize bytecode for list comprehensions.
Antoine Pitrou <solipsis@pitrou.net>
parents:
45897
diff
changeset
|
584 self.emit('LIST_APPEND', len(node.quals) + 1) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
585 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
586 for start, cont, anchor in stack: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
587 if cont: |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
588 self.nextBlock(cont) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
589 self.emit('JUMP_ABSOLUTE', start) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
590 self.startBlock(anchor) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
591 |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
592 def visitSetComp(self, node): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
593 self.set_lineno(node) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
594 # setup list |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
595 self.emit('BUILD_SET', 0) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
596 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
597 stack = [] |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
598 for i, for_ in zip(range(len(node.quals)), node.quals): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
599 start, anchor = self.visit(for_) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
600 cont = None |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
601 for if_ in for_.ifs: |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
602 if cont is None: |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
603 cont = self.newBlock() |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
604 self.visit(if_, cont) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
605 stack.insert(0, (start, cont, anchor)) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
606 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
607 self.visit(node.expr) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
608 self.emit('SET_ADD', len(node.quals) + 1) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
609 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
610 for start, cont, anchor in stack: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
611 if cont: |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
612 self.nextBlock(cont) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
613 self.emit('JUMP_ABSOLUTE', start) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
614 self.startBlock(anchor) |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
615 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
616 def visitDictComp(self, node): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
617 self.set_lineno(node) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
618 # setup list |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
619 self.emit('BUILD_MAP', 0) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
620 |
|
58250
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
621 stack = [] |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
622 for i, for_ in zip(range(len(node.quals)), node.quals): |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
623 start, anchor = self.visit(for_) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
624 cont = None |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
625 for if_ in for_.ifs: |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
626 if cont is None: |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
627 cont = self.newBlock() |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
628 self.visit(if_, cont) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
629 stack.insert(0, (start, cont, anchor)) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
630 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
631 self.visit(node.value) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
632 self.visit(node.key) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
633 self.emit('MAP_ADD', len(node.quals) + 1) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
634 |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
635 for start, cont, anchor in stack: |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
636 if cont: |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
637 self.nextBlock(cont) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
638 self.emit('JUMP_ABSOLUTE', start) |
|
edfed0e32ced
Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58229
diff
changeset
|
639 self.startBlock(anchor) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
640 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
641 def visitListCompFor(self, node): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
642 start = self.newBlock() |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
643 anchor = self.newBlock() |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
644 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
645 self.visit(node.list) |
|
19619
5663b883ea5d
Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19609
diff
changeset
|
646 self.emit('GET_ITER') |
|
17459
e846e960e7f6
Use new _implicitNameOp() to generate name op code for list comprehensions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17454
diff
changeset
|
647 self.nextBlock(start) |
|
26983
2c67169f6e13
Replace all but one explicit emit('SET_LINENO') with call to set_lineno().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26979
diff
changeset
|
648 self.set_lineno(node, force=True) |
|
19619
5663b883ea5d
Fix _convert_NAME() so that it doesn't store locals for class bodies.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19609
diff
changeset
|
649 self.emit('FOR_ITER', anchor) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
650 self.nextBlock() |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
651 self.visit(node.assign) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
652 return start, anchor |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
653 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
654 def visitListCompIf(self, node, branch): |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
655 self.set_lineno(node, force=True) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
656 self.visit(node.test) |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
657 self.emit('POP_JUMP_IF_FALSE', branch) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
658 self.newBlock() |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
659 |
|
39370
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
660 def _makeClosure(self, gen, args): |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
661 frees = gen.scope.get_free_vars() |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
662 if frees: |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
663 for name in frees: |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
664 self.emit('LOAD_CLOSURE', name) |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
665 self.emit('BUILD_TUPLE', len(frees)) |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
666 self.emit('LOAD_CONST', gen) |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
667 self.emit('MAKE_CLOSURE', args) |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
668 else: |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
669 self.emit('LOAD_CONST', gen) |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
670 self.emit('MAKE_FUNCTION', args) |
|
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
671 |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
672 def visitGenExpr(self, node): |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
673 gen = GenExprCodeGenerator(node, self.scopes, self.class_name, |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
674 self.get_module()) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
675 walk(node.code, gen) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
676 gen.finish() |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
677 self.set_lineno(node) |
|
39370
eb23d096d694
Fix the 'compiler' package to generate correct code for MAKE_CLOSURE.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
38084
diff
changeset
|
678 self._makeClosure(gen, 0) |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
679 # precomputation of outmost iterable |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
680 self.visit(node.code.quals[0].iter) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
681 self.emit('GET_ITER') |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
682 self.emit('CALL_FUNCTION', 1) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
683 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
684 def visitGenExprInner(self, node): |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
685 self.set_lineno(node) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
686 # setup list |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
687 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
688 stack = [] |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
689 for i, for_ in zip(range(len(node.quals)), node.quals): |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
690 start, anchor, end = self.visit(for_) |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
691 cont = None |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
692 for if_ in for_.ifs: |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
693 if cont is None: |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
694 cont = self.newBlock() |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
695 self.visit(if_, cont) |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
696 stack.insert(0, (start, cont, anchor, end)) |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
697 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
698 self.visit(node.expr) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
699 self.emit('YIELD_VALUE') |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
700 self.emit('POP_TOP') |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
701 |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
702 for start, cont, anchor, end in stack: |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
703 if cont: |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
704 self.nextBlock(cont) |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
705 self.emit('JUMP_ABSOLUTE', start) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
706 self.startBlock(anchor) |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
707 self.emit('POP_BLOCK') |
|
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
708 self.setups.pop() |
|
51404
42faa8054c3d
Overhaul Lib/compiler block ordering. The previous code was filled with
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
50204
diff
changeset
|
709 self.nextBlock(end) |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
710 |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
711 self.emit('LOAD_CONST', None) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
712 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
713 def visitGenExprFor(self, node): |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
714 start = self.newBlock() |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
715 anchor = self.newBlock() |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
716 end = self.newBlock() |
|
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
717 |
|
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
718 self.setups.push((LOOP, start)) |
|
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
719 self.emit('SETUP_LOOP', end) |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32121
diff
changeset
|
720 |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
721 if node.is_outmost: |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
722 self.loadName('.0') |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
723 else: |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
724 self.visit(node.iter) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
725 self.emit('GET_ITER') |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
726 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
727 self.nextBlock(start) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
728 self.set_lineno(node, force=True) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
729 self.emit('FOR_ITER', anchor) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
730 self.nextBlock() |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
731 self.visit(node.assign) |
|
39532
a6afe0eda711
Fix a bug in the ``compiler`` package that caused invalid code to be
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
39370
diff
changeset
|
732 return start, anchor, end |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
733 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
734 def visitGenExprIf(self, node, branch): |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
735 self.set_lineno(node, force=True) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
736 self.visit(node.test) |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
737 self.emit('POP_JUMP_IF_FALSE', branch) |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
738 self.newBlock() |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
739 |
| 12009 | 740 # exception related |
| 741 | |
| 742 def visitAssert(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
743 # XXX would be interesting to implement this via a |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
744 # transformation of the AST before this stage |
| 33020 | 745 if __debug__: |
| 746 end = self.newBlock() | |
| 747 self.set_lineno(node) | |
| 748 # XXX AssertionError appears to be special case -- it is always | |
| 749 # loaded as a global even if there is a local name. I guess this | |
| 750 # is a sort of renaming op. | |
| 751 self.nextBlock() | |
| 752 self.visit(node.test) | |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
753 self.emit('POP_JUMP_IF_TRUE', end) |
| 33020 | 754 self.nextBlock() |
| 755 self.emit('LOAD_GLOBAL', 'AssertionError') | |
| 756 if node.fail: | |
| 757 self.visit(node.fail) | |
| 758 self.emit('RAISE_VARARGS', 2) | |
| 759 else: | |
| 760 self.emit('RAISE_VARARGS', 1) | |
| 761 self.nextBlock(end) | |
| 12009 | 762 |
| 763 def visitRaise(self, node): | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
764 self.set_lineno(node) |
| 12009 | 765 n = 0 |
| 766 if node.expr1: | |
| 767 self.visit(node.expr1) | |
| 768 n = n + 1 | |
| 769 if node.expr2: | |
| 770 self.visit(node.expr2) | |
| 771 n = n + 1 | |
| 772 if node.expr3: | |
| 773 self.visit(node.expr3) | |
| 774 n = n + 1 | |
| 775 self.emit('RAISE_VARARGS', n) | |
| 776 | |
| 777 def visitTryExcept(self, node): | |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
778 body = self.newBlock() |
| 12009 | 779 handlers = self.newBlock() |
| 780 end = self.newBlock() | |
| 781 if node.else_: | |
| 782 lElse = self.newBlock() | |
| 783 else: | |
| 784 lElse = end | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
785 self.set_lineno(node) |
| 12009 | 786 self.emit('SETUP_EXCEPT', handlers) |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
787 self.nextBlock(body) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
788 self.setups.push((EXCEPT, body)) |
| 12009 | 789 self.visit(node.body) |
| 790 self.emit('POP_BLOCK') | |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
791 self.setups.pop() |
| 12009 | 792 self.emit('JUMP_FORWARD', lElse) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
793 self.startBlock(handlers) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
794 |
| 12009 | 795 last = len(node.handlers) - 1 |
| 796 for i in range(len(node.handlers)): | |
| 797 expr, target, body = node.handlers[i] | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
798 self.set_lineno(expr) |
| 12009 | 799 if expr: |
| 800 self.emit('DUP_TOP') | |
| 801 self.visit(expr) | |
| 802 self.emit('COMPARE_OP', 'exception match') | |
| 803 next = self.newBlock() | |
|
51920
ef8fe9088696
Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51425
diff
changeset
|
804 self.emit('POP_JUMP_IF_FALSE', next) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
805 self.nextBlock() |
| 12009 | 806 self.emit('POP_TOP') |
| 807 if target: | |
| 808 self.visit(target) | |
| 809 else: | |
| 810 self.emit('POP_TOP') | |
| 811 self.emit('POP_TOP') | |
| 812 self.visit(body) | |
| 813 self.emit('JUMP_FORWARD', end) | |
| 814 if expr: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
815 self.nextBlock(next) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
816 else: |
|
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
817 self.nextBlock() |
| 12009 | 818 self.emit('END_FINALLY') |
| 819 if node.else_: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
820 self.nextBlock(lElse) |
| 12009 | 821 self.visit(node.else_) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
822 self.nextBlock(end) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
823 |
| 12009 | 824 def visitTryFinally(self, node): |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
825 body = self.newBlock() |
| 12009 | 826 final = self.newBlock() |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
827 self.set_lineno(node) |
| 12009 | 828 self.emit('SETUP_FINALLY', final) |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
829 self.nextBlock(body) |
|
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
830 self.setups.push((TRY_FINALLY, body)) |
| 12009 | 831 self.visit(node.body) |
| 832 self.emit('POP_BLOCK') | |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
833 self.setups.pop() |
| 12009 | 834 self.emit('LOAD_CONST', None) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
835 self.nextBlock(final) |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
836 self.setups.push((END_FINALLY, final)) |
| 12009 | 837 self.visit(node.final) |
| 838 self.emit('END_FINALLY') | |
|
19573
725762ca2774
Track the block stack more reasonably in order to handle continue in
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19567
diff
changeset
|
839 self.setups.pop() |
| 12009 | 840 |
|
36706
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
841 __with_count = 0 |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
842 |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
843 def visitWith(self, node): |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
844 body = self.newBlock() |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
845 final = self.newBlock() |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
846 self.__with_count += 1 |
|
51420
0151c514df56
Make names generated for 'with' variables match the built-in compiler.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
51404
diff
changeset
|
847 valuevar = "_[%d]" % self.__with_count |
|
36706
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
848 self.set_lineno(node) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
849 self.visit(node.expr) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
850 self.emit('DUP_TOP') |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
851 self.emit('LOAD_ATTR', '__exit__') |
|
45703
343597c7d682
Speed up with statements by storing the __exit__ method on the stack instead of in a temp variable (bumps the magic number for pyc files)
Nick Coghlan <ncoghlan@gmail.com>
parents:
40719
diff
changeset
|
852 self.emit('ROT_TWO') |
|
36706
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
853 self.emit('LOAD_ATTR', '__enter__') |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
854 self.emit('CALL_FUNCTION', 0) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
855 if node.vars is None: |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
856 self.emit('POP_TOP') |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
857 else: |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
858 self._implicitNameOp('STORE', valuevar) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
859 self.emit('SETUP_FINALLY', final) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
860 self.nextBlock(body) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
861 self.setups.push((TRY_FINALLY, body)) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
862 if node.vars is not None: |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
863 self._implicitNameOp('LOAD', valuevar) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
864 self._implicitNameOp('DELETE', valuevar) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
865 self.visit(node.vars) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
866 self.visit(node.body) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
867 self.emit('POP_BLOCK') |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
868 self.setups.pop() |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
869 self.emit('LOAD_CONST', None) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
870 self.nextBlock(final) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
871 self.setups.push((END_FINALLY, final)) |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
872 self.emit('WITH_CLEANUP') |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
873 self.emit('END_FINALLY') |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
874 self.setups.pop() |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
875 self.__with_count -= 1 |
|
b6b1c44d9110
Update the compiler package to compile the with-statement.
Guido van Rossum <guido@python.org>
parents:
35946
diff
changeset
|
876 |
| 12009 | 877 # misc |
| 878 | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
879 def visitDiscard(self, node): |
|
17412
3704be3db302
[finishing fix from earlier checkins]
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17410
diff
changeset
|
880 self.set_lineno(node) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
881 self.visit(node.expr) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
882 self.emit('POP_TOP') |
| 12009 | 883 |
| 884 def visitConst(self, node): | |
| 885 self.emit('LOAD_CONST', node.value) | |
| 886 | |
| 887 def visitKeyword(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
888 self.emit('LOAD_CONST', node.name) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
889 self.visit(node.expr) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
890 |
| 12009 | 891 def visitGlobal(self, node): |
| 892 # no code to generate | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
893 pass |
| 12009 | 894 |
| 895 def visitName(self, node): | |
|
14362
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
896 self.set_lineno(node) |
| 12009 | 897 self.loadName(node.name) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
898 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
899 def visitPass(self, node): |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
900 self.set_lineno(node) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
901 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
902 def visitImport(self, node): |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
903 self.set_lineno(node) |
|
36823
7a6b174ddef2
Fix compiler breakage related to absolute imports
Neal Norwitz <nnorwitz@gmail.com>
parents:
36821
diff
changeset
|
904 level = 0 if self.graph.checkFlag(CO_FUTURE_ABSIMPORT) else -1 |
| 14359 | 905 for name, alias in node.names: |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
906 if VERSION > 1: |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
907 self.emit('LOAD_CONST', level) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
908 self.emit('LOAD_CONST', None) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
909 self.emit('IMPORT_NAME', name) |
|
23710
44b9977d4848
Remove uses of string module and stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents:
21958
diff
changeset
|
910 mod = name.split(".")[0] |
|
29602
caa8c120e2cb
Jacob Hallen cornered me here at EuroPython and got me to look at
Michael W. Hudson <mwh@python.net>
parents:
26983
diff
changeset
|
911 if alias: |
|
caa8c120e2cb
Jacob Hallen cornered me here at EuroPython and got me to look at
Michael W. Hudson <mwh@python.net>
parents:
26983
diff
changeset
|
912 self._resolveDots(name) |
|
caa8c120e2cb
Jacob Hallen cornered me here at EuroPython and got me to look at
Michael W. Hudson <mwh@python.net>
parents:
26983
diff
changeset
|
913 self.storeName(alias) |
|
caa8c120e2cb
Jacob Hallen cornered me here at EuroPython and got me to look at
Michael W. Hudson <mwh@python.net>
parents:
26983
diff
changeset
|
914 else: |
|
caa8c120e2cb
Jacob Hallen cornered me here at EuroPython and got me to look at
Michael W. Hudson <mwh@python.net>
parents:
26983
diff
changeset
|
915 self.storeName(mod) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
916 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
917 def visitFrom(self, node): |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
918 self.set_lineno(node) |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
919 level = node.level |
|
36823
7a6b174ddef2
Fix compiler breakage related to absolute imports
Neal Norwitz <nnorwitz@gmail.com>
parents:
36821
diff
changeset
|
920 if level == 0 and not self.graph.checkFlag(CO_FUTURE_ABSIMPORT): |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
921 level = -1 |
|
63528
751c6dd71b5e
Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
45897
diff
changeset
|
922 fromlist = tuple(name for (name, alias) in node.names) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
923 if VERSION > 1: |
|
36820
c1c46210b4b7
Add support for absolute/relative imports and if/else expressions:
Thomas Wouters <thomas@python.org>
parents:
36706
diff
changeset
|
924 self.emit('LOAD_CONST', level) |
|
63528
751c6dd71b5e
Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
45897
diff
changeset
|
925 self.emit('LOAD_CONST', fromlist) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
926 self.emit('IMPORT_NAME', node.modname) |
| 14359 | 927 for name, alias in node.names: |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
928 if VERSION > 1: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
929 if name == '*': |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
930 self.namespace = 0 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
931 self.emit('IMPORT_STAR') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
932 # There can only be one name w/ from ... import * |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
933 assert len(node.names) == 1 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
934 return |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
935 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
936 self.emit('IMPORT_FROM', name) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
937 self._resolveDots(name) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
938 self.storeName(alias or name) |
|
15185
100beebde9df
Fix SF bug #116263: support for from .. import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14362
diff
changeset
|
939 else: |
|
100beebde9df
Fix SF bug #116263: support for from .. import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14362
diff
changeset
|
940 self.emit('IMPORT_FROM', name) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
941 self.emit('POP_TOP') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
942 |
| 14359 | 943 def _resolveDots(self, name): |
|
23710
44b9977d4848
Remove uses of string module and stat.ST_MODE
Neal Norwitz <nnorwitz@gmail.com>
parents:
21958
diff
changeset
|
944 elts = name.split(".") |
| 14359 | 945 if len(elts) == 1: |
| 946 return | |
| 947 for elt in elts[1:]: | |
| 948 self.emit('LOAD_ATTR', elt) | |
| 949 | |
| 12009 | 950 def visitGetattr(self, node): |
| 951 self.visit(node.expr) | |
| 19522 | 952 self.emit('LOAD_ATTR', self.mangle(node.attrname)) |
| 12009 | 953 |
| 954 # next five implement assignments | |
| 955 | |
| 956 def visitAssign(self, node): | |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
957 self.set_lineno(node) |
| 12009 | 958 self.visit(node.expr) |
| 959 dups = len(node.nodes) - 1 | |
| 960 for i in range(len(node.nodes)): | |
| 961 elt = node.nodes[i] | |
| 962 if i < dups: | |
| 963 self.emit('DUP_TOP') | |
| 964 if isinstance(elt, ast.Node): | |
| 965 self.visit(elt) | |
| 966 | |
| 967 def visitAssName(self, node): | |
| 968 if node.flags == 'OP_ASSIGN': | |
| 969 self.storeName(node.name) | |
| 970 elif node.flags == 'OP_DELETE': | |
| 19561 | 971 self.set_lineno(node) |
| 12009 | 972 self.delName(node.name) |
| 973 else: | |
| 974 print "oops", node.flags | |
|
11754
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
975 |
| 12009 | 976 def visitAssAttr(self, node): |
| 977 self.visit(node.expr) | |
| 978 if node.flags == 'OP_ASSIGN': | |
| 19522 | 979 self.emit('STORE_ATTR', self.mangle(node.attrname)) |
| 12009 | 980 elif node.flags == 'OP_DELETE': |
| 19522 | 981 self.emit('DELETE_ATTR', self.mangle(node.attrname)) |
| 12009 | 982 else: |
| 983 print "warning: unexpected flags:", node.flags | |
| 984 print node | |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
985 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
986 def _visitAssSequence(self, node, op='UNPACK_SEQUENCE'): |
| 12009 | 987 if findOp(node) != 'OP_DELETE': |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
988 self.emit(op, len(node.nodes)) |
| 12009 | 989 for child in node.nodes: |
| 990 self.visit(child) | |
| 991 | |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
992 if VERSION > 1: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
993 visitAssTuple = _visitAssSequence |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
994 visitAssList = _visitAssSequence |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
995 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
996 def visitAssTuple(self, node): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
997 self._visitAssSequence(node, 'UNPACK_TUPLE') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
998 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
999 def visitAssList(self, node): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1000 self._visitAssSequence(node, 'UNPACK_LIST') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1001 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1002 # augmented assignment |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1003 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1004 def visitAugAssign(self, node): |
|
19519
99e953c6709c
emit SET_LINENO for augmented assignments
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19518
diff
changeset
|
1005 self.set_lineno(node) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1006 aug_node = wrap_aug(node.node) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1007 self.visit(aug_node, "load") |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1008 self.visit(node.expr) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1009 self.emit(self._augmented_opcode[node.op]) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1010 self.visit(aug_node, "store") |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1011 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1012 _augmented_opcode = { |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1013 '+=' : 'INPLACE_ADD', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1014 '-=' : 'INPLACE_SUBTRACT', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1015 '*=' : 'INPLACE_MULTIPLY', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1016 '/=' : 'INPLACE_DIVIDE', |
| 19561 | 1017 '//=': 'INPLACE_FLOOR_DIVIDE', |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1018 '%=' : 'INPLACE_MODULO', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1019 '**=': 'INPLACE_POWER', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1020 '>>=': 'INPLACE_RSHIFT', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1021 '<<=': 'INPLACE_LSHIFT', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1022 '&=' : 'INPLACE_AND', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1023 '^=' : 'INPLACE_XOR', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1024 '|=' : 'INPLACE_OR', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1025 } |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1026 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1027 def visitAugName(self, node, mode): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1028 if mode == "load": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1029 self.loadName(node.name) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1030 elif mode == "store": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1031 self.storeName(node.name) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1032 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1033 def visitAugGetattr(self, node, mode): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1034 if mode == "load": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1035 self.visit(node.expr) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1036 self.emit('DUP_TOP') |
| 19522 | 1037 self.emit('LOAD_ATTR', self.mangle(node.attrname)) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1038 elif mode == "store": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1039 self.emit('ROT_TWO') |
| 19522 | 1040 self.emit('STORE_ATTR', self.mangle(node.attrname)) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1041 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1042 def visitAugSlice(self, node, mode): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1043 if mode == "load": |
| 17413 | 1044 self.visitSlice(node, 1) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1045 elif mode == "store": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1046 slice = 0 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1047 if node.lower: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1048 slice = slice | 1 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1049 if node.upper: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1050 slice = slice | 2 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1051 if slice == 0: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1052 self.emit('ROT_TWO') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1053 elif slice == 3: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1054 self.emit('ROT_FOUR') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1055 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1056 self.emit('ROT_THREE') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1057 self.emit('STORE_SLICE+%d' % slice) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1058 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1059 def visitAugSubscript(self, node, mode): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1060 if mode == "load": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1061 self.visitSubscript(node, 1) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1062 elif mode == "store": |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1063 self.emit('ROT_THREE') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1064 self.emit('STORE_SUBSCR') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1065 |
| 12009 | 1066 def visitExec(self, node): |
| 1067 self.visit(node.expr) | |
| 1068 if node.locals is None: | |
| 1069 self.emit('LOAD_CONST', None) | |
| 1070 else: | |
| 1071 self.visit(node.locals) | |
| 1072 if node.globals is None: | |
| 1073 self.emit('DUP_TOP') | |
| 1074 else: | |
| 1075 self.visit(node.globals) | |
| 1076 self.emit('EXEC_STMT') | |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
1077 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1078 def visitCallFunc(self, node): |
|
11754
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
1079 pos = 0 |
|
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
1080 kw = 0 |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
1081 self.set_lineno(node) |
| 11806 | 1082 self.visit(node.node) |
| 1083 for arg in node.args: | |
| 1084 self.visit(arg) | |
|
11754
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
1085 if isinstance(arg, ast.Keyword): |
|
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
1086 kw = kw + 1 |
|
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
1087 else: |
|
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
1088 pos = pos + 1 |
|
12563
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1089 if node.star_args is not None: |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1090 self.visit(node.star_args) |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1091 if node.dstar_args is not None: |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1092 self.visit(node.dstar_args) |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1093 have_star = node.star_args is not None |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1094 have_dstar = node.dstar_args is not None |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1095 opcode = callfunc_opcode_info[have_star, have_dstar] |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
1096 self.emit(opcode, kw << 8 | pos) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1097 |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1098 def visitPrint(self, node, newline=0): |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
1099 self.set_lineno(node) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1100 if node.dest: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1101 self.visit(node.dest) |
| 12009 | 1102 for child in node.nodes: |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1103 if node.dest: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1104 self.emit('DUP_TOP') |
| 12009 | 1105 self.visit(child) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1106 if node.dest: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1107 self.emit('ROT_TWO') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1108 self.emit('PRINT_ITEM_TO') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1109 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1110 self.emit('PRINT_ITEM') |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1111 if node.dest and not newline: |
|
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1112 self.emit('POP_TOP') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1113 |
| 12009 | 1114 def visitPrintnl(self, node): |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1115 self.visitPrint(node, newline=1) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1116 if node.dest: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1117 self.emit('PRINT_NEWLINE_TO') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1118 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1119 self.emit('PRINT_NEWLINE') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1120 |
| 12009 | 1121 def visitReturn(self, node): |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
1122 self.set_lineno(node) |
| 12009 | 1123 self.visit(node.value) |
| 1124 self.emit('RETURN_VALUE') | |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1125 |
|
19923
cb35d0f704de
Add code generator for yield stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19920
diff
changeset
|
1126 def visitYield(self, node): |
|
cb35d0f704de
Add code generator for yield stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19920
diff
changeset
|
1127 self.set_lineno(node) |
|
cb35d0f704de
Add code generator for yield stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19920
diff
changeset
|
1128 self.visit(node.value) |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1129 self.emit('YIELD_VALUE') |
|
19923
cb35d0f704de
Add code generator for yield stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19920
diff
changeset
|
1130 |
| 12009 | 1131 # slice and subscript stuff |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1132 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1133 def visitSlice(self, node, aug_flag=None): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1134 # aug_flag is used by visitAugSlice |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1135 self.visit(node.expr) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1136 slice = 0 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1137 if node.lower: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1138 self.visit(node.lower) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1139 slice = slice | 1 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1140 if node.upper: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1141 self.visit(node.upper) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1142 slice = slice | 2 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1143 if aug_flag: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1144 if slice == 0: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1145 self.emit('DUP_TOP') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1146 elif slice == 3: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1147 self.emit('DUP_TOPX', 3) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1148 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1149 self.emit('DUP_TOPX', 2) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1150 if node.flags == 'OP_APPLY': |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1151 self.emit('SLICE+%d' % slice) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1152 elif node.flags == 'OP_ASSIGN': |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1153 self.emit('STORE_SLICE+%d' % slice) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1154 elif node.flags == 'OP_DELETE': |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1155 self.emit('DELETE_SLICE+%d' % slice) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1156 else: |
|
11789
860524182b9c
finish first impl of code generator
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11766
diff
changeset
|
1157 print "weird slice", node.flags |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1158 raise |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1159 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1160 def visitSubscript(self, node, aug_flag=None): |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1161 self.visit(node.expr) |
| 12009 | 1162 for sub in node.subs: |
| 1163 self.visit(sub) | |
|
36980
c8ffc913d014
Teach the compiler module about augmented assignment to tuple subscripts
Nick Coghlan <ncoghlan@gmail.com>
parents:
36926
diff
changeset
|
1164 if len(node.subs) > 1: |
|
c8ffc913d014
Teach the compiler module about augmented assignment to tuple subscripts
Nick Coghlan <ncoghlan@gmail.com>
parents:
36926
diff
changeset
|
1165 self.emit('BUILD_TUPLE', len(node.subs)) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1166 if aug_flag: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1167 self.emit('DUP_TOPX', 2) |
| 12009 | 1168 if node.flags == 'OP_APPLY': |
| 1169 self.emit('BINARY_SUBSCR') | |
| 1170 elif node.flags == 'OP_ASSIGN': | |
| 1171 self.emit('STORE_SUBSCR') | |
|
11808
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1172 elif node.flags == 'OP_DELETE': |
| 12009 | 1173 self.emit('DELETE_SUBSCR') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1174 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1175 # binary ops |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1176 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1177 def binaryOp(self, node, op): |
| 11806 | 1178 self.visit(node.left) |
| 1179 self.visit(node.right) | |
| 1180 self.emit(op) | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1181 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1182 def visitAdd(self, node): |
| 11806 | 1183 return self.binaryOp(node, 'BINARY_ADD') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1184 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1185 def visitSub(self, node): |
| 11806 | 1186 return self.binaryOp(node, 'BINARY_SUBTRACT') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1187 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1188 def visitMul(self, node): |
| 11806 | 1189 return self.binaryOp(node, 'BINARY_MULTIPLY') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1190 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1191 def visitDiv(self, node): |
|
19936
6944f5b9de03
support true division
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19923
diff
changeset
|
1192 return self.binaryOp(node, self._div_op) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1193 |
| 19561 | 1194 def visitFloorDiv(self, node): |
| 1195 return self.binaryOp(node, 'BINARY_FLOOR_DIVIDE') | |
| 1196 | |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1197 def visitMod(self, node): |
| 11806 | 1198 return self.binaryOp(node, 'BINARY_MODULO') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1199 |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1200 def visitPower(self, node): |
| 11806 | 1201 return self.binaryOp(node, 'BINARY_POWER') |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1202 |
|
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1203 def visitLeftShift(self, node): |
| 11806 | 1204 return self.binaryOp(node, 'BINARY_LSHIFT') |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1205 |
|
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1206 def visitRightShift(self, node): |
| 11806 | 1207 return self.binaryOp(node, 'BINARY_RSHIFT') |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1208 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1209 # unary ops |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1210 |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1211 def unaryOp(self, node, op): |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1212 self.visit(node.expr) |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1213 self.emit(op) |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1214 |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1215 def visitInvert(self, node): |
|
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1216 return self.unaryOp(node, 'UNARY_INVERT') |
|
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
1217 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1218 def visitUnarySub(self, node): |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1219 return self.unaryOp(node, 'UNARY_NEGATIVE') |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1220 |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1221 def visitUnaryAdd(self, node): |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1222 return self.unaryOp(node, 'UNARY_POSITIVE') |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1223 |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1224 def visitUnaryInvert(self, node): |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1225 return self.unaryOp(node, 'UNARY_INVERT') |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1226 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1227 def visitNot(self, node): |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1228 return self.unaryOp(node, 'UNARY_NOT') |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1229 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1230 def visitBackquote(self, node): |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1231 return self.unaryOp(node, 'UNARY_CONVERT') |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1232 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1233 # bit ops |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
1234 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1235 def bitOp(self, nodes, op): |
| 11806 | 1236 self.visit(nodes[0]) |
| 1237 for node in nodes[1:]: | |
| 1238 self.visit(node) | |
| 1239 self.emit(op) | |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1240 |
|
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1241 def visitBitand(self, node): |
| 11806 | 1242 return self.bitOp(node.nodes, 'BINARY_AND') |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1243 |
|
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1244 def visitBitor(self, node): |
| 11806 | 1245 return self.bitOp(node.nodes, 'BINARY_OR') |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1246 |
|
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1247 def visitBitxor(self, node): |
| 11806 | 1248 return self.bitOp(node.nodes, 'BINARY_XOR') |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1249 |
| 12009 | 1250 # object constructors |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1251 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1252 def visitEllipsis(self, node): |
| 11806 | 1253 self.emit('LOAD_CONST', Ellipsis) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1254 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1255 def visitTuple(self, node): |
|
19567
b98e4346257b
Generate SET_LINENO for list and tuple literals when the open paren
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19561
diff
changeset
|
1256 self.set_lineno(node) |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1257 for elt in node.nodes: |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
1258 self.visit(elt) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1259 self.emit('BUILD_TUPLE', len(node.nodes)) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1260 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1261 def visitList(self, node): |
|
19567
b98e4346257b
Generate SET_LINENO for list and tuple literals when the open paren
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19561
diff
changeset
|
1262 self.set_lineno(node) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1263 for elt in node.nodes: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1264 self.visit(elt) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1265 self.emit('BUILD_LIST', len(node.nodes)) |
| 12009 | 1266 |
|
58229
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58159
diff
changeset
|
1267 def visitSet(self, node): |
|
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58159
diff
changeset
|
1268 self.set_lineno(node) |
|
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58159
diff
changeset
|
1269 for elt in node.nodes: |
|
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58159
diff
changeset
|
1270 self.visit(elt) |
|
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58159
diff
changeset
|
1271 self.emit('BUILD_SET', len(node.nodes)) |
|
c2fdf25329ff
Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
58159
diff
changeset
|
1272 |
| 12009 | 1273 def visitSliceobj(self, node): |
| 1274 for child in node.nodes: | |
| 1275 self.visit(child) | |
| 1276 self.emit('BUILD_SLICE', len(node.nodes)) | |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
1277 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1278 def visitDict(self, node): |
|
26983
2c67169f6e13
Replace all but one explicit emit('SET_LINENO') with call to set_lineno().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26979
diff
changeset
|
1279 self.set_lineno(node) |
| 11806 | 1280 self.emit('BUILD_MAP', 0) |
| 1281 for k, v in node.items: | |
| 1282 self.emit('DUP_TOP') | |
| 26670 | 1283 self.visit(k) |
| 11806 | 1284 self.visit(v) |
| 26670 | 1285 self.emit('ROT_THREE') |
| 11806 | 1286 self.emit('STORE_SUBSCR') |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1287 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1288 class NestedScopeMixin: |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1289 """Defines initClass() for nested scoping (Python 2.2-compatible)""" |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1290 def initClass(self): |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1291 self.__class__.NameFinder = LocalNameFinder |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1292 self.__class__.FunctionGen = FunctionCodeGenerator |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1293 self.__class__.ClassGen = ClassCodeGenerator |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1294 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1295 class ModuleCodeGenerator(NestedScopeMixin, CodeGenerator): |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1296 __super_init = CodeGenerator.__init__ |
| 12009 | 1297 |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1298 scopes = None |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1299 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1300 def __init__(self, tree): |
|
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1301 self.graph = pyassem.PyFlowGraph("<module>", tree.filename) |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1302 self.futures = future.find_futures(tree) |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1303 self.__super_init() |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1304 walk(tree, self) |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1305 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1306 def get_module(self): |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1307 return self |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1308 |
| 19946 | 1309 class ExpressionCodeGenerator(NestedScopeMixin, CodeGenerator): |
| 1310 __super_init = CodeGenerator.__init__ | |
| 1311 | |
| 1312 scopes = None | |
| 1313 futures = () | |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1314 |
| 19946 | 1315 def __init__(self, tree): |
| 1316 self.graph = pyassem.PyFlowGraph("<expression>", tree.filename) | |
| 1317 self.__super_init() | |
| 1318 walk(tree, self) | |
| 1319 | |
| 1320 def get_module(self): | |
| 1321 return self | |
| 1322 | |
| 1323 class InteractiveCodeGenerator(NestedScopeMixin, CodeGenerator): | |
| 1324 | |
| 1325 __super_init = CodeGenerator.__init__ | |
| 1326 | |
| 1327 scopes = None | |
| 1328 futures = () | |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1329 |
| 19946 | 1330 def __init__(self, tree): |
| 1331 self.graph = pyassem.PyFlowGraph("<interactive>", tree.filename) | |
| 1332 self.__super_init() | |
| 1333 self.set_lineno(tree) | |
| 1334 walk(tree, self) | |
| 1335 self.emit('RETURN_VALUE') | |
| 1336 | |
| 1337 def get_module(self): | |
| 1338 return self | |
|
21958
5ed782bdbb2a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
21522
diff
changeset
|
1339 |
| 19946 | 1340 def visitDiscard(self, node): |
| 1341 # XXX Discard means it's an expression. Perhaps this is a bad | |
| 1342 # name. | |
| 1343 self.visit(node.expr) | |
| 1344 self.emit('PRINT_EXPR') | |
| 1345 | |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1346 class AbstractFunctionCode: |
| 12009 | 1347 optimized = 1 |
| 1348 lambdaCount = 0 | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1349 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1350 def __init__(self, func, scopes, isLambda, class_name, mod): |
| 19522 | 1351 self.class_name = class_name |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1352 self.module = mod |
| 12009 | 1353 if isLambda: |
| 1354 klass = FunctionCodeGenerator | |
| 1355 name = "<lambda.%d>" % klass.lambdaCount | |
| 1356 klass.lambdaCount = klass.lambdaCount + 1 | |
| 1357 else: | |
| 1358 name = func.name | |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1359 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1360 args, hasTupleArg = generateArgList(func.argnames) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1361 self.graph = pyassem.PyFlowGraph(name, func.filename, args, |
|
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1362 optimized=1) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1363 self.isLambda = isLambda |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1364 self.super_init() |
| 12009 | 1365 |
| 17410 | 1366 if not isLambda and func.doc: |
| 1367 self.setDocstring(func.doc) | |
| 1368 | |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1369 lnf = walk(func.code, self.NameFinder(args), verbose=0) |
| 12009 | 1370 self.locals.push(lnf.getLocals()) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1371 if func.varargs: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1372 self.graph.setFlag(CO_VARARGS) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1373 if func.kwargs: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1374 self.graph.setFlag(CO_VARKEYWORDS) |
|
13864
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
1375 self.set_lineno(func) |
| 12009 | 1376 if hasTupleArg: |
| 1377 self.generateArgUnpack(func.argnames) | |
| 1378 | |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1379 def get_module(self): |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1380 return self.module |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1381 |
| 12009 | 1382 def finish(self): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1383 self.graph.startExitBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1384 if not self.isLambda: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1385 self.emit('LOAD_CONST', None) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1386 self.emit('RETURN_VALUE') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1387 |
| 12009 | 1388 def generateArgUnpack(self, args): |
|
17449
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1389 for i in range(len(args)): |
|
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1390 arg = args[i] |
|
35946
b2eb1589c22b
No need for types, use isinstance
Neal Norwitz <nnorwitz@gmail.com>
parents:
33204
diff
changeset
|
1391 if isinstance(arg, tuple): |
|
17449
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1392 self.emit('LOAD_FAST', '.%d' % (i * 2)) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1393 self.unpackSequence(arg) |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1394 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1395 def unpackSequence(self, tup): |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1396 if VERSION > 1: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1397 self.emit('UNPACK_SEQUENCE', len(tup)) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1398 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1399 self.emit('UNPACK_TUPLE', len(tup)) |
| 12009 | 1400 for elt in tup: |
|
35946
b2eb1589c22b
No need for types, use isinstance
Neal Norwitz <nnorwitz@gmail.com>
parents:
33204
diff
changeset
|
1401 if isinstance(elt, tuple): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1402 self.unpackSequence(elt) |
| 12009 | 1403 else: |
|
17438
41a423294cb5
Fix unpackSequence() to use _nameOp() rather than LOAD_FAST
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17437
diff
changeset
|
1404 self._nameOp('STORE', elt) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1405 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1406 unpackTuple = unpackSequence |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1407 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1408 class FunctionCodeGenerator(NestedScopeMixin, AbstractFunctionCode, |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19946
diff
changeset
|
1409 CodeGenerator): |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1410 super_init = CodeGenerator.__init__ # call be other init |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1411 scopes = None |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1412 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1413 __super_init = AbstractFunctionCode.__init__ |
|
11710
38c4d61db7ea
rapid evolution towards producing real .pyc files (even though I don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11705
diff
changeset
|
1414 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1415 def __init__(self, func, scopes, isLambda, class_name, mod): |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1416 self.scopes = scopes |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1417 self.scope = scopes[func] |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1418 self.__super_init(func, scopes, isLambda, class_name, mod) |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1419 self.graph.setFreeVars(self.scope.get_free_vars()) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1420 self.graph.setCellVars(self.scope.get_cell_vars()) |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1421 if self.scope.generator is not None: |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1422 self.graph.setFlag(CO_GENERATOR) |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1423 |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1424 class GenExprCodeGenerator(NestedScopeMixin, AbstractFunctionCode, |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1425 CodeGenerator): |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1426 super_init = CodeGenerator.__init__ # call be other init |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1427 scopes = None |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1428 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1429 __super_init = AbstractFunctionCode.__init__ |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1430 |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1431 def __init__(self, gexp, scopes, class_name, mod): |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1432 self.scopes = scopes |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1433 self.scope = scopes[gexp] |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1434 self.__super_init(gexp, scopes, 1, class_name, mod) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1435 self.graph.setFreeVars(self.scope.get_free_vars()) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1436 self.graph.setCellVars(self.scope.get_cell_vars()) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1437 self.graph.setFlag(CO_GENERATOR) |
|
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
31729
diff
changeset
|
1438 |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1439 class AbstractClassCode: |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1440 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1441 def __init__(self, klass, scopes, module): |
| 19522 | 1442 self.class_name = klass.name |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1443 self.module = module |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1444 self.graph = pyassem.PyFlowGraph(klass.name, klass.filename, |
| 19561 | 1445 optimized=0, klass=1) |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1446 self.super_init() |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1447 lnf = walk(klass.code, self.NameFinder(), verbose=0) |
| 12009 | 1448 self.locals.push(lnf.getLocals()) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1449 self.graph.setFlag(CO_NEWLOCALS) |
| 17410 | 1450 if klass.doc: |
| 1451 self.setDocstring(klass.doc) | |
| 12009 | 1452 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1453 def get_module(self): |
|
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1454 return self.module |
|
17437
02d69a887038
Inside a class scope always use LOAD_NAME, STORE_NAME, DEL_NAME
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17436
diff
changeset
|
1455 |
| 12009 | 1456 def finish(self): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1457 self.graph.startExitBlock() |
| 12009 | 1458 self.emit('LOAD_LOCALS') |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1459 self.emit('RETURN_VALUE') |
| 12009 | 1460 |
|
19920
63930159ca3c
Various sundry changes for 2.2 compatibility
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19619
diff
changeset
|
1461 class ClassCodeGenerator(NestedScopeMixin, AbstractClassCode, CodeGenerator): |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1462 super_init = CodeGenerator.__init__ |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1463 scopes = None |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1464 |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1465 __super_init = AbstractClassCode.__init__ |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1466 |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1467 def __init__(self, klass, scopes, module): |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1468 self.scopes = scopes |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1469 self.scope = scopes[klass] |
|
19939
aaca4d3cf62b
Handle more syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
19936
diff
changeset
|
1470 self.__super_init(klass, scopes, module) |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1471 self.graph.setFreeVars(self.scope.get_free_vars()) |
|
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1472 self.graph.setCellVars(self.scope.get_cell_vars()) |
|
26979
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1473 self.set_lineno(klass) |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1474 self.emit("LOAD_GLOBAL", "__name__") |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1475 self.storeName("__module__") |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1476 if klass.doc: |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1477 self.emit("LOAD_CONST", klass.doc) |
|
784e7fdf8c52
SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26670
diff
changeset
|
1478 self.storeName('__doc__') |
|
17436
98f69245bb69
Preliminary support for nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17413
diff
changeset
|
1479 |
| 12009 | 1480 def generateArgList(arglist): |
| 1481 """Generate an arg list marking TupleArgs""" | |
| 1482 args = [] | |
| 1483 extra = [] | |
| 1484 count = 0 | |
|
17449
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1485 for i in range(len(arglist)): |
|
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1486 elt = arglist[i] |
|
35946
b2eb1589c22b
No need for types, use isinstance
Neal Norwitz <nnorwitz@gmail.com>
parents:
33204
diff
changeset
|
1487 if isinstance(elt, str): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1488 args.append(elt) |
|
35946
b2eb1589c22b
No need for types, use isinstance
Neal Norwitz <nnorwitz@gmail.com>
parents:
33204
diff
changeset
|
1489 elif isinstance(elt, tuple): |
|
17449
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1490 args.append(TupleArg(i * 2, elt)) |
|
51c8ce1a2e25
Revise handling of tuple arguments so that the variables names match
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17438
diff
changeset
|
1491 extra.extend(misc.flatten(elt)) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1492 count = count + 1 |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1493 else: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1494 raise ValueError, "unexpect argument type:", elt |
| 12009 | 1495 return args + extra, count |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
1496 |
| 12009 | 1497 def findOp(node): |
| 1498 """Find the op (DELETE, LOAD, STORE) in an AssTuple tree""" | |
| 1499 v = OpFinder() | |
|
19518
fbb35268fed4
Many changes -- bug fixes and sundry improvements
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17460
diff
changeset
|
1500 walk(node, v, verbose=0) |
| 12009 | 1501 return v.op |
| 1502 | |
|
11808
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1503 class OpFinder: |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1504 def __init__(self): |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1505 self.op = None |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1506 def visitAssName(self, node): |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1507 if self.op is None: |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1508 self.op = node.flags |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1509 elif self.op != node.flags: |
|
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1510 raise ValueError, "mixed ops in stmt" |
|
17454
d91577783335
Add support for visitAssAttr to findOp().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17453
diff
changeset
|
1511 visitAssAttr = visitAssName |
|
20925
ee1437f5de03
Fix SF bug #479186: compiler generates bad code for "del"
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20547
diff
changeset
|
1512 visitSubscript = visitAssName |
|
11808
cf8fa92ebf3d
the previous quick hack to fix def foo((x,y)) failed on some cases
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11806
diff
changeset
|
1513 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1514 class Delegator: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1515 """Base class to support delegation for augmented assignment nodes |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1516 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1517 To generator code for augmented assignments, we use the following |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1518 wrapper classes. In visitAugAssign, the left-hand expression node |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1519 is visited twice. The first time the visit uses the normal method |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1520 for that node . The second time the visit uses a different method |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1521 that generates the appropriate code to perform the assignment. |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1522 These delegator classes wrap the original AST nodes in order to |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1523 support the variant visit methods. |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1524 """ |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1525 def __init__(self, obj): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1526 self.obj = obj |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1527 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1528 def __getattr__(self, attr): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1529 return getattr(self.obj, attr) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1530 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1531 class AugGetattr(Delegator): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1532 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1533 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1534 class AugName(Delegator): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1535 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1536 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1537 class AugSlice(Delegator): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1538 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1539 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1540 class AugSubscript(Delegator): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1541 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1542 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1543 wrapper = { |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1544 ast.Getattr: AugGetattr, |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1545 ast.Name: AugName, |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1546 ast.Slice: AugSlice, |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1547 ast.Subscript: AugSubscript, |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1548 } |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1549 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1550 def wrap_aug(node): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1551 return wrapper[node.__class__](node) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1552 |
| 12009 | 1553 if __name__ == "__main__": |
| 1554 for file in sys.argv[1:]: | |
|
21059
e76306eea370
Fix [ #484645 ] little bug in pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20925
diff
changeset
|
1555 compileFile(file) |
