Mercurial > cpython-lru_cache-weakref
annotate Lib/compiler/pycodegen.py @ 17322:bd110eadc19a legacy-trunk
One-liner critical fix from Jim Fulton: Eric's string-method crusade
got the order backwards in a line (for .find()).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 05 Apr 2001 22:26:23 +0000 |
| parents | 7a82f84290f7 |
| children | f8fdbeb73667 |
| 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 stat |
| 14359 | 5 import string |
|
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
|
6 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
|
7 import sys |
|
11793
d2ab9553f871
support for arglists with implicit tuple unpacks
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11789
diff
changeset
|
8 import types |
| 12009 | 9 from cStringIO import StringIO |
| 10 | |
| 11 from compiler import ast, parse, walk | |
| 12 from compiler import pyassem, misc | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
13 from compiler.pyassem import CO_VARARGS, CO_VARKEYWORDS, CO_NEWLOCALS, TupleArg |
| 12009 | 14 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
15 # 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
|
16 try: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
17 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
|
18 except AttributeError: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
19 VERSION = 1 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
20 |
|
12563
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
21 callfunc_opcode_info = { |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
22 # (Have *args, Have **args) : opcode |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
23 (0,0) : "CALL_FUNCTION", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
24 (1,0) : "CALL_FUNCTION_VAR", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
25 (0,1) : "CALL_FUNCTION_KW", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
26 (1,1) : "CALL_FUNCTION_VAR_KW", |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
27 } |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
28 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
29 def compile(filename, display=0): |
| 12009 | 30 f = open(filename) |
| 31 buf = f.read() | |
| 32 f.close() | |
| 33 mod = Module(buf, filename) | |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
34 mod.compile(display) |
| 12009 | 35 f = open(filename + "c", "wb") |
| 36 mod.dump(f) | |
| 37 f.close() | |
| 38 | |
| 39 class Module: | |
| 40 def __init__(self, source, filename): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
41 self.filename = filename |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
42 self.source = source |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
43 self.code = None |
| 12009 | 44 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
45 def compile(self, display=0): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
46 ast = parse(self.source) |
| 12009 | 47 root, filename = os.path.split(self.filename) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
48 gen = ModuleCodeGenerator(filename) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
49 walk(ast, gen, 1) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
50 if display: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
51 import pprint |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
52 print pprint.pprint(ast) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
53 self.code = gen.getCode() |
| 12009 | 54 |
| 55 def dump(self, f): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
56 f.write(self.getPycHeader()) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
57 marshal.dump(self.code, f) |
| 12009 | 58 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
59 MAGIC = imp.get_magic() |
| 12009 | 60 |
| 61 def getPycHeader(self): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
62 # 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
|
63 # 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
|
64 # 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
|
65 # same effect is to call marshal and then skip the code. |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
66 mtime = os.stat(self.filename)[stat.ST_MTIME] |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
67 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
|
68 return self.MAGIC + mtime |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
69 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
70 class CodeGenerator: |
|
11743
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
71 |
| 12009 | 72 optimized = 0 # is namespace access optimized? |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
73 |
| 12009 | 74 def __init__(self, filename): |
| 75 ## Subclasses must define a constructor that intializes self.graph | |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
76 ## before calling this init function, e.g. |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
77 ## self.graph = pyassem.PyFlowGraph() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
78 self.filename = filename |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
79 self.locals = misc.Stack() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
80 self.loops = misc.Stack() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
81 self.curStack = 0 |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
82 self.maxStack = 0 |
|
14362
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
83 self.last_lineno = None |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
84 self._setupGraphDelegation() |
|
11793
d2ab9553f871
support for arglists with implicit tuple unpacks
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11789
diff
changeset
|
85 |
| 12009 | 86 def _setupGraphDelegation(self): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
87 self.emit = self.graph.emit |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
88 self.newBlock = self.graph.newBlock |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
89 self.startBlock = self.graph.startBlock |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
90 self.nextBlock = self.graph.nextBlock |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
91 self.setDocstring = self.graph.setDocstring |
|
11793
d2ab9553f871
support for arglists with implicit tuple unpacks
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11789
diff
changeset
|
92 |
| 12009 | 93 def getCode(self): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
94 """Return a code object""" |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
95 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
|
96 |
| 12009 | 97 # 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
|
98 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
99 def isLocalName(self, name): |
| 11806 | 100 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
|
101 |
|
11743
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
102 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
|
103 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
|
104 |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
105 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
|
106 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
|
107 |
|
c978a23a4dab
add namespace attr to CodeGenerator, can be either MODULE_NAMESPACE or
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11734
diff
changeset
|
108 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
|
109 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
|
110 |
| 12009 | 111 def _nameOp(self, prefix, name): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
112 if not self.optimized: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
113 self.emit(prefix + '_NAME', name) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
114 return |
| 12009 | 115 if self.isLocalName(name): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
116 self.emit(prefix + '_FAST', name) |
| 12009 | 117 else: |
| 118 self.emit(prefix + '_GLOBAL', name) | |
| 119 | |
|
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
|
120 def set_lineno(self, node): |
|
14362
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
121 """Emit SET_LINENO if node has lineno attribute and it is |
|
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
122 different than the last lineno 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
|
123 |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
124 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
|
125 |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
126 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
|
127 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
|
128 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
|
129 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
|
130 """ |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
131 lineno = getattr(node, 'lineno', None) |
|
14362
a665bdc4922f
patch by Neil Schemenauer to improve (fix?) line number generation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14359
diff
changeset
|
132 if lineno is not None and lineno != self.last_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
|
133 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
|
134 self.last_lineno = 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
|
135 return 1 |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
136 return 0 |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
137 |
| 12009 | 138 # The first few visitor methods handle nodes that generator new |
| 139 # code objects | |
| 140 | |
| 141 def visitModule(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
142 lnf = walk(node.node, LocalNameFinder(), 0) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
143 self.locals.push(lnf.getLocals()) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
144 self.setDocstring(node.doc) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
145 self.visit(node.node) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
146 self.emit('LOAD_CONST', None) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
147 self.emit('RETURN_VALUE') |
| 12009 | 148 |
| 149 def visitFunction(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
150 self._visitFuncOrLambda(node, isLambda=0) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
151 self.storeName(node.name) |
| 12009 | 152 |
| 153 def visitLambda(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
154 self._visitFuncOrLambda(node, isLambda=1) |
| 12009 | 155 |
| 156 def _visitFuncOrLambda(self, node, isLambda): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
157 gen = FunctionCodeGenerator(node, self.filename, isLambda) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
158 walk(node.code, gen) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
159 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
|
160 self.set_lineno(node) |
| 12009 | 161 for default in node.defaults: |
| 162 self.visit(default) | |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
163 self.emit('LOAD_CONST', gen) |
| 12009 | 164 self.emit('MAKE_FUNCTION', len(node.defaults)) |
| 165 | |
| 166 def visitClass(self, node): | |
| 167 gen = ClassCodeGenerator(node, self.filename) | |
| 168 walk(node.code, gen) | |
| 169 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
|
170 self.set_lineno(node) |
| 12009 | 171 self.emit('LOAD_CONST', node.name) |
| 172 for base in node.bases: | |
| 173 self.visit(base) | |
| 174 self.emit('BUILD_TUPLE', len(node.bases)) | |
| 175 self.emit('LOAD_CONST', gen.getCode()) | |
| 176 self.emit('MAKE_FUNCTION', 0) | |
| 177 self.emit('CALL_FUNCTION', 0) | |
| 178 self.emit('BUILD_CLASS') | |
| 179 self.storeName(node.name) | |
| 180 | |
| 181 # The rest are standard visitor methods | |
| 182 | |
| 183 # The next few implement control-flow statements | |
| 184 | |
| 185 def visitIf(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
186 end = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
187 numtests = len(node.tests) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
188 for i in range(numtests): |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
189 test, suite = node.tests[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
|
190 self.set_lineno(test) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
191 self.visit(test) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
192 nextTest = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
193 self.emit('JUMP_IF_FALSE', nextTest) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
194 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
195 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
196 self.visit(suite) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
197 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
|
198 self.startBlock(nextTest) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
199 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
200 if node.else_: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
201 self.visit(node.else_) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
202 self.nextBlock(end) |
| 12009 | 203 |
| 204 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
|
205 self.set_lineno(node) |
| 12009 | 206 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
207 loop = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
208 else_ = self.newBlock() |
| 12009 | 209 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
210 after = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
211 self.emit('SETUP_LOOP', after) |
| 12009 | 212 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
213 self.nextBlock(loop) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
214 self.loops.push(loop) |
| 12009 | 215 |
|
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
|
216 self.set_lineno(node) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
217 self.visit(node.test) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
218 self.emit('JUMP_IF_FALSE', else_ or after) |
| 12009 | 219 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
220 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
221 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
222 self.visit(node.body) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
223 self.emit('JUMP_ABSOLUTE', loop) |
| 12009 | 224 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
225 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
|
226 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
227 self.emit('POP_BLOCK') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
228 if node.else_: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
229 self.visit(node.else_) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
230 self.loops.pop() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
231 self.nextBlock(after) |
| 12009 | 232 |
| 233 def visitFor(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
234 start = self.newBlock() |
| 12009 | 235 anchor = self.newBlock() |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
236 after = self.newBlock() |
| 12009 | 237 self.loops.push(start) |
| 238 | |
|
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
|
239 self.set_lineno(node) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
240 self.emit('SETUP_LOOP', after) |
| 12009 | 241 self.visit(node.list) |
| 242 self.visit(ast.Const(0)) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
243 self.nextBlock(start) |
|
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
|
244 self.set_lineno(node) |
| 12009 | 245 self.emit('FOR_LOOP', anchor) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
246 self.nextBlock() |
| 12009 | 247 self.visit(node.assign) |
| 248 self.visit(node.body) | |
| 249 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
|
250 self.startBlock(anchor) |
| 12009 | 251 self.emit('POP_BLOCK') |
| 252 if node.else_: | |
| 253 self.visit(node.else_) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
254 self.loops.pop() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
255 self.nextBlock(after) |
| 12009 | 256 |
| 257 def visitBreak(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
258 if not self.loops: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
259 raise SyntaxError, "'break' outside loop (%s, %d)" % \ |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
260 (self.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
|
261 self.set_lineno(node) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
262 self.emit('BREAK_LOOP') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
263 |
| 12009 | 264 def visitContinue(self, node): |
| 265 if not self.loops: | |
| 266 raise SyntaxError, "'continue' outside loop (%s, %d)" % \ | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
267 (self.filename, node.lineno) |
| 12009 | 268 l = self.loops.top() |
|
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
|
269 self.set_lineno(node) |
| 12009 | 270 self.emit('JUMP_ABSOLUTE', l) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
271 self.nextBlock() |
| 12009 | 272 |
| 273 def visitTest(self, node, jump): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
274 end = self.newBlock() |
| 12009 | 275 for child in node.nodes[:-1]: |
| 276 self.visit(child) | |
| 277 self.emit(jump, end) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
278 self.nextBlock() |
| 12009 | 279 self.emit('POP_TOP') |
| 280 self.visit(node.nodes[-1]) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
281 self.nextBlock(end) |
| 12009 | 282 |
| 283 def visitAnd(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
284 self.visitTest(node, 'JUMP_IF_FALSE') |
| 12009 | 285 |
| 286 def visitOr(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
287 self.visitTest(node, 'JUMP_IF_TRUE') |
| 12009 | 288 |
| 289 def visitCompare(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
290 self.visit(node.expr) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
291 cleanup = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
292 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
|
293 self.visit(code) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
294 self.emit('DUP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
295 self.emit('ROT_THREE') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
296 self.emit('COMPARE_OP', op) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
297 self.emit('JUMP_IF_FALSE', cleanup) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
298 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
299 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
300 # now do the last comparison |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
301 if node.ops: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
302 op, code = node.ops[-1] |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
303 self.visit(code) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
304 self.emit('COMPARE_OP', op) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
305 if len(node.ops) > 1: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
306 end = self.newBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
307 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
|
308 self.startBlock(cleanup) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
309 self.emit('ROT_TWO') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
310 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
311 self.nextBlock(end) |
| 12009 | 312 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
313 # list comprehensions |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
314 __list_count = 0 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
315 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
316 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
|
317 # XXX would it be easier to transform the AST into the form it |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
318 # would have if the list comp were expressed as a series of |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
319 # for and if stmts and an explicit append? |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
320 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
|
321 # setup list |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
322 append = "$append%d" % self.__list_count |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
323 self.__list_count = self.__list_count + 1 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
324 self.emit('BUILD_LIST', 0) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
325 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
|
326 self.emit('LOAD_ATTR', 'append') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
327 self.storeName(append) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
328 l = len(node.quals) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
329 stack = [] |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
330 for i, for_ in zip(range(l), node.quals): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
331 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
|
332 cont = None |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
333 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
|
334 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
|
335 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
|
336 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
|
337 stack.insert(0, (start, cont, anchor)) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
338 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
339 self.loadName(append) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
340 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
|
341 self.emit('CALL_FUNCTION', 1) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
342 self.emit('POP_TOP') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
343 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
344 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
|
345 if cont: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
346 skip_one = self.newBlock() |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
347 self.emit('JUMP_FORWARD', skip_one) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
348 self.startBlock(cont) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
349 self.emit('POP_TOP') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
350 self.nextBlock(skip_one) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
351 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
|
352 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
|
353 self.delName(append) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
354 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
355 self.__list_count = self.__list_count - 1 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
356 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
357 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
|
358 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
|
359 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
|
360 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
|
361 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
362 self.visit(node.list) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
363 self.visit(ast.Const(0)) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
364 self.emit('SET_LINENO', node.lineno) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
365 self.nextBlock(start) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
366 self.emit('FOR_LOOP', anchor) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
367 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
|
368 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
|
369 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
|
370 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
371 def visitListCompIf(self, node, branch): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
372 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
|
373 self.visit(node.test) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
374 self.emit('JUMP_IF_FALSE', branch) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
375 self.newBlock() |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
376 self.emit('POP_TOP') |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
377 |
| 12009 | 378 # exception related |
| 379 | |
| 380 def visitAssert(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
381 # 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
|
382 # transformation of the AST before this stage |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
383 end = 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
|
384 self.set_lineno(node) |
| 12009 | 385 # XXX __debug__ and AssertionError appear to be special cases |
| 386 # -- they are always loaded as globals even if there are local | |
| 387 # names. I guess this is a sort of renaming op. | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
388 self.emit('LOAD_GLOBAL', '__debug__') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
389 self.emit('JUMP_IF_FALSE', end) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
390 self.nextBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
391 self.emit('POP_TOP') |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
392 self.visit(node.test) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
393 self.emit('JUMP_IF_TRUE', end) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
394 self.nextBlock() |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
395 self.emit('POP_TOP') |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
396 self.emit('LOAD_GLOBAL', 'AssertionError') |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
397 if node.fail: |
|
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
398 self.visit(node.fail) |
|
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
399 self.emit('RAISE_VARARGS', 2) |
|
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
400 else: |
|
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
401 self.emit('RAISE_VARARGS', 1) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
402 self.nextBlock(end) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
403 self.emit('POP_TOP') |
| 12009 | 404 |
| 405 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
|
406 self.set_lineno(node) |
| 12009 | 407 n = 0 |
| 408 if node.expr1: | |
| 409 self.visit(node.expr1) | |
| 410 n = n + 1 | |
| 411 if node.expr2: | |
| 412 self.visit(node.expr2) | |
| 413 n = n + 1 | |
| 414 if node.expr3: | |
| 415 self.visit(node.expr3) | |
| 416 n = n + 1 | |
| 417 self.emit('RAISE_VARARGS', n) | |
| 418 | |
| 419 def visitTryExcept(self, node): | |
| 420 handlers = self.newBlock() | |
| 421 end = self.newBlock() | |
| 422 if node.else_: | |
| 423 lElse = self.newBlock() | |
| 424 else: | |
| 425 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
|
426 self.set_lineno(node) |
| 12009 | 427 self.emit('SETUP_EXCEPT', handlers) |
|
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.nextBlock() |
| 12009 | 429 self.visit(node.body) |
| 430 self.emit('POP_BLOCK') | |
| 431 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
|
432 self.startBlock(handlers) |
| 12009 | 433 |
| 434 last = len(node.handlers) - 1 | |
| 435 for i in range(len(node.handlers)): | |
| 436 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
|
437 self.set_lineno(expr) |
| 12009 | 438 if expr: |
| 439 self.emit('DUP_TOP') | |
| 440 self.visit(expr) | |
| 441 self.emit('COMPARE_OP', 'exception match') | |
| 442 next = self.newBlock() | |
| 443 self.emit('JUMP_IF_FALSE', next) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
444 self.nextBlock() |
| 12009 | 445 self.emit('POP_TOP') |
| 446 self.emit('POP_TOP') | |
| 447 if target: | |
| 448 self.visit(target) | |
| 449 else: | |
| 450 self.emit('POP_TOP') | |
| 451 self.emit('POP_TOP') | |
| 452 self.visit(body) | |
| 453 self.emit('JUMP_FORWARD', end) | |
| 454 if expr: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
455 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
|
456 else: |
|
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
457 self.nextBlock() |
| 12009 | 458 self.emit('POP_TOP') |
| 459 self.emit('END_FINALLY') | |
| 460 if node.else_: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
461 self.nextBlock(lElse) |
| 12009 | 462 self.visit(node.else_) |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
463 self.nextBlock(end) |
| 12009 | 464 |
| 465 def visitTryFinally(self, node): | |
| 466 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
|
467 self.set_lineno(node) |
| 12009 | 468 self.emit('SETUP_FINALLY', final) |
|
15379
7a82f84290f7
Change the graph structure to contain the code generator object for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15205
diff
changeset
|
469 self.nextBlock() |
| 12009 | 470 self.visit(node.body) |
| 471 self.emit('POP_BLOCK') | |
| 472 self.emit('LOAD_CONST', None) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
473 self.nextBlock(final) |
| 12009 | 474 self.visit(node.final) |
| 475 self.emit('END_FINALLY') | |
| 476 | |
| 477 # misc | |
| 478 | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
479 def visitDiscard(self, node): |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
480 self.visit(node.expr) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
481 self.emit('POP_TOP') |
| 12009 | 482 |
| 483 def visitConst(self, node): | |
| 484 self.emit('LOAD_CONST', node.value) | |
| 485 | |
| 486 def visitKeyword(self, node): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
487 self.emit('LOAD_CONST', node.name) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
488 self.visit(node.expr) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
489 |
| 12009 | 490 def visitGlobal(self, node): |
| 491 # no code to generate | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
492 pass |
| 12009 | 493 |
| 494 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
|
495 self.set_lineno(node) |
| 12009 | 496 self.loadName(node.name) |
| 497 | |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
498 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
|
499 self.set_lineno(node) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
500 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
501 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
|
502 self.set_lineno(node) |
| 14359 | 503 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
|
504 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
|
505 self.emit('LOAD_CONST', None) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
506 self.emit('IMPORT_NAME', name) |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
507 mod = string.split(name, ".")[0] |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
508 self.storeName(alias or mod) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
509 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
510 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
|
511 self.set_lineno(node) |
| 14359 | 512 fromlist = map(lambda (name, alias): name, 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
|
513 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
|
514 self.emit('LOAD_CONST', tuple(fromlist)) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
515 self.emit('IMPORT_NAME', node.modname) |
| 14359 | 516 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
|
517 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
|
518 if name == '*': |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
519 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
|
520 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
|
521 # 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
|
522 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
|
523 return |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
524 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
525 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
|
526 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
|
527 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
|
528 else: |
|
100beebde9df
Fix SF bug #116263: support for from .. import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14362
diff
changeset
|
529 self.emit('IMPORT_FROM', name) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
530 self.emit('POP_TOP') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
531 |
| 14359 | 532 def _resolveDots(self, name): |
| 533 elts = string.split(name, ".") | |
| 534 if len(elts) == 1: | |
| 535 return | |
| 536 for elt in elts[1:]: | |
| 537 self.emit('LOAD_ATTR', elt) | |
| 538 | |
| 12009 | 539 def visitGetattr(self, node): |
| 540 self.visit(node.expr) | |
| 541 self.emit('LOAD_ATTR', node.attrname) | |
| 542 | |
| 543 # next five implement assignments | |
| 544 | |
| 545 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
|
546 self.set_lineno(node) |
| 12009 | 547 self.visit(node.expr) |
| 548 dups = len(node.nodes) - 1 | |
| 549 for i in range(len(node.nodes)): | |
| 550 elt = node.nodes[i] | |
| 551 if i < dups: | |
| 552 self.emit('DUP_TOP') | |
| 553 if isinstance(elt, ast.Node): | |
| 554 self.visit(elt) | |
| 555 | |
| 556 def visitAssName(self, node): | |
| 557 if node.flags == 'OP_ASSIGN': | |
| 558 self.storeName(node.name) | |
| 559 elif node.flags == 'OP_DELETE': | |
| 560 self.delName(node.name) | |
| 561 else: | |
| 562 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
|
563 |
| 12009 | 564 def visitAssAttr(self, node): |
| 565 self.visit(node.expr) | |
| 566 if node.flags == 'OP_ASSIGN': | |
| 567 self.emit('STORE_ATTR', node.attrname) | |
| 568 elif node.flags == 'OP_DELETE': | |
| 569 self.emit('DELETE_ATTR', node.attrname) | |
| 570 else: | |
| 571 print "warning: unexpected flags:", node.flags | |
| 572 print node | |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
573 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
574 def _visitAssSequence(self, node, op='UNPACK_SEQUENCE'): |
| 12009 | 575 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
|
576 self.emit(op, len(node.nodes)) |
| 12009 | 577 for child in node.nodes: |
| 578 self.visit(child) | |
| 579 | |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
580 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
|
581 visitAssTuple = _visitAssSequence |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
582 visitAssList = _visitAssSequence |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
583 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
584 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
|
585 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
|
586 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
587 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
|
588 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
|
589 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
590 # augmented assignment |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
591 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
592 def visitAugAssign(self, node): |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
593 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
|
594 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
|
595 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
|
596 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
|
597 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
|
598 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
599 _augmented_opcode = { |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
600 '+=' : 'INPLACE_ADD', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
601 '-=' : 'INPLACE_SUBTRACT', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
602 '*=' : 'INPLACE_MULTIPLY', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
603 '/=' : 'INPLACE_DIVIDE', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
604 '%=' : 'INPLACE_MODULO', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
605 '**=': 'INPLACE_POWER', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
606 '>>=': 'INPLACE_RSHIFT', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
607 '<<=': 'INPLACE_LSHIFT', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
608 '&=' : 'INPLACE_AND', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
609 '^=' : 'INPLACE_XOR', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
610 '|=' : 'INPLACE_OR', |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
611 } |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
612 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
613 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
|
614 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
|
615 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
|
616 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
|
617 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
|
618 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
619 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
|
620 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
|
621 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
|
622 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
|
623 self.emit('LOAD_ATTR', node.attrname) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
624 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
|
625 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
|
626 self.emit('STORE_ATTR', node.attrname) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
627 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
628 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
|
629 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
|
630 self.visitSlice(node, 1) |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
631 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
|
632 slice = 0 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
633 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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 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
|
641 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
642 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
|
643 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
|
644 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
645 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
|
646 if len(node.subs) > 1: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
647 raise SyntaxError, "augmented assignment to tuple is not possible" |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
648 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
|
649 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
|
650 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
|
651 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
|
652 self.emit('STORE_SUBSCR') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
653 |
| 12009 | 654 def visitExec(self, node): |
| 655 self.visit(node.expr) | |
| 656 if node.locals is None: | |
| 657 self.emit('LOAD_CONST', None) | |
| 658 else: | |
| 659 self.visit(node.locals) | |
| 660 if node.globals is None: | |
| 661 self.emit('DUP_TOP') | |
| 662 else: | |
| 663 self.visit(node.globals) | |
| 664 self.emit('EXEC_STMT') | |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
665 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
666 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
|
667 pos = 0 |
|
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
668 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
|
669 self.set_lineno(node) |
| 11806 | 670 self.visit(node.node) |
| 671 for arg in node.args: | |
| 672 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
|
673 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
|
674 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
|
675 else: |
|
dffbd921eeed
change MODULE_NAMESPACE/FUNCTION_NAMESPACE stuff to have a single flag
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11751
diff
changeset
|
676 pos = pos + 1 |
|
12563
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
677 if node.star_args is not None: |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
678 self.visit(node.star_args) |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
679 if node.dstar_args is not None: |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
680 self.visit(node.dstar_args) |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
681 have_star = node.star_args is not None |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
682 have_dstar = node.dstar_args is not None |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
683 opcode = callfunc_opcode_info[have_star, have_dstar] |
|
b87d75798d81
patches from Mark Hammond
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12009
diff
changeset
|
684 self.emit(opcode, kw << 8 | pos) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
685 |
| 12009 | 686 def visitPrint(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
|
687 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
|
688 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
|
689 self.visit(node.dest) |
| 12009 | 690 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
|
691 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
|
692 self.emit('DUP_TOP') |
| 12009 | 693 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
|
694 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
|
695 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
|
696 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
|
697 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
698 self.emit('PRINT_ITEM') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
699 |
| 12009 | 700 def visitPrintnl(self, node): |
| 701 self.visitPrint(node) | |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
702 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
|
703 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
|
704 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
705 self.emit('PRINT_NEWLINE') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
706 |
| 12009 | 707 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
|
708 self.set_lineno(node) |
| 12009 | 709 self.visit(node.value) |
| 710 self.emit('RETURN_VALUE') | |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
711 |
| 12009 | 712 # slice and subscript stuff |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
713 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
714 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
|
715 # aug_flag is used by visitAugSlice |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
716 self.visit(node.expr) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
717 slice = 0 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
718 if node.lower: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
719 self.visit(node.lower) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
720 slice = slice | 1 |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
721 if node.upper: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
722 self.visit(node.upper) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
723 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
|
724 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
|
725 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
|
726 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
|
727 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
|
728 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
|
729 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
730 self.emit('DUP_TOPX', 2) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
731 if node.flags == 'OP_APPLY': |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
732 self.emit('SLICE+%d' % slice) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
733 elif node.flags == 'OP_ASSIGN': |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
734 self.emit('STORE_SLICE+%d' % slice) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
735 elif node.flags == 'OP_DELETE': |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
736 self.emit('DELETE_SLICE+%d' % slice) |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
737 else: |
|
11789
860524182b9c
finish first impl of code generator
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11766
diff
changeset
|
738 print "weird slice", node.flags |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
739 raise |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
740 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
741 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
|
742 self.visit(node.expr) |
| 12009 | 743 for sub in node.subs: |
| 744 self.visit(sub) | |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
745 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
|
746 self.emit('DUP_TOPX', 2) |
| 12009 | 747 if len(node.subs) > 1: |
| 748 self.emit('BUILD_TUPLE', len(node.subs)) | |
| 749 if node.flags == 'OP_APPLY': | |
| 750 self.emit('BINARY_SUBSCR') | |
| 751 elif node.flags == 'OP_ASSIGN': | |
| 752 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
|
753 elif node.flags == 'OP_DELETE': |
| 12009 | 754 self.emit('DELETE_SUBSCR') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
755 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
756 # binary ops |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
757 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
758 def binaryOp(self, node, op): |
| 11806 | 759 self.visit(node.left) |
| 760 self.visit(node.right) | |
| 761 self.emit(op) | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
762 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
763 def visitAdd(self, node): |
| 11806 | 764 return self.binaryOp(node, 'BINARY_ADD') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
765 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
766 def visitSub(self, node): |
| 11806 | 767 return self.binaryOp(node, 'BINARY_SUBTRACT') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
768 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
769 def visitMul(self, node): |
| 11806 | 770 return self.binaryOp(node, 'BINARY_MULTIPLY') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
771 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
772 def visitDiv(self, node): |
| 11806 | 773 return self.binaryOp(node, 'BINARY_DIVIDE') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
774 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
775 def visitMod(self, node): |
| 11806 | 776 return self.binaryOp(node, 'BINARY_MODULO') |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
777 |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
778 def visitPower(self, node): |
| 11806 | 779 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
|
780 |
|
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
781 def visitLeftShift(self, node): |
| 11806 | 782 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
|
783 |
|
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
784 def visitRightShift(self, node): |
| 11806 | 785 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
|
786 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
787 # unary ops |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
788 |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
789 def unaryOp(self, node, op): |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
790 self.visit(node.expr) |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
791 self.emit(op) |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
792 |
|
11761
62fa8ec8595f
looks like everything is working except for try/except (pystone
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11755
diff
changeset
|
793 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
|
794 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
|
795 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
796 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
|
797 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
|
798 |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
799 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
|
800 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
|
801 |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
802 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
|
803 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
|
804 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
805 def visitNot(self, node): |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
806 return self.unaryOp(node, 'UNARY_NOT') |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
807 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
808 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
|
809 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
|
810 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
811 # bit ops |
|
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
812 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
813 def bitOp(self, nodes, op): |
| 11806 | 814 self.visit(nodes[0]) |
| 815 for node in nodes[1:]: | |
| 816 self.visit(node) | |
| 817 self.emit(op) | |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
818 |
|
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
819 def visitBitand(self, node): |
| 11806 | 820 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
|
821 |
|
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
822 def visitBitor(self, node): |
| 11806 | 823 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
|
824 |
|
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
825 def visitBitxor(self, node): |
| 11806 | 826 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
|
827 |
| 12009 | 828 # 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
|
829 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
830 def visitEllipsis(self, node): |
| 11806 | 831 self.emit('LOAD_CONST', Ellipsis) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
832 |
|
11727
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
833 def visitTuple(self, node): |
|
1e952d933abb
add optional verbose arg to walk function. it overrides the global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11724
diff
changeset
|
834 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
|
835 self.visit(elt) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
836 self.emit('BUILD_TUPLE', len(node.nodes)) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
837 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
838 def visitList(self, node): |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
839 for elt in node.nodes: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
840 self.visit(elt) |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
841 self.emit('BUILD_LIST', len(node.nodes)) |
| 12009 | 842 |
| 843 def visitSliceobj(self, node): | |
| 844 for child in node.nodes: | |
| 845 self.visit(child) | |
| 846 self.emit('BUILD_SLICE', len(node.nodes)) | |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
847 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
848 def visitDict(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
|
849 lineno = getattr(node, 'lineno', None) |
|
c58149c0670a
replace most calls to emit 'SET_LINENO' will call to method set_lineno
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12563
diff
changeset
|
850 if 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
|
851 set.emit('SET_LINENO', lineno) |
| 11806 | 852 self.emit('BUILD_MAP', 0) |
| 853 for k, v in node.items: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
854 lineno2 = getattr(node, 'lineno', None) |
|
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
|
855 if lineno2 is not None and lineno != lineno2: |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
856 self.emit('SET_LINENO', lineno2) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
857 lineno = lineno2 |
| 11806 | 858 self.emit('DUP_TOP') |
| 859 self.visit(v) | |
| 860 self.emit('ROT_TWO') | |
| 861 self.visit(k) | |
| 862 self.emit('STORE_SUBSCR') | |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
863 |
| 12009 | 864 class ModuleCodeGenerator(CodeGenerator): |
| 865 super_init = CodeGenerator.__init__ | |
| 866 | |
| 867 def __init__(self, filename): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
868 # XXX <module> is ? in compile.c |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
869 self.graph = pyassem.PyFlowGraph("<module>", filename) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
870 self.super_init(filename) |
| 12009 | 871 |
| 872 class FunctionCodeGenerator(CodeGenerator): | |
| 873 super_init = CodeGenerator.__init__ | |
| 874 | |
| 875 optimized = 1 | |
| 876 lambdaCount = 0 | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
877 |
| 12009 | 878 def __init__(self, func, filename, isLambda=0): |
| 879 if isLambda: | |
| 880 klass = FunctionCodeGenerator | |
| 881 name = "<lambda.%d>" % klass.lambdaCount | |
| 882 klass.lambdaCount = klass.lambdaCount + 1 | |
| 883 else: | |
| 884 name = func.name | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
885 args, hasTupleArg = generateArgList(func.argnames) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
886 self.graph = pyassem.PyFlowGraph(name, filename, args, |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
887 optimized=1) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
888 self.isLambda = isLambda |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
889 self.super_init(filename) |
| 12009 | 890 |
| 891 lnf = walk(func.code, LocalNameFinder(args), 0) | |
| 892 self.locals.push(lnf.getLocals()) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
893 if func.varargs: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
894 self.graph.setFlag(CO_VARARGS) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
895 if func.kwargs: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
896 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
|
897 self.set_lineno(func) |
| 12009 | 898 if hasTupleArg: |
| 899 self.generateArgUnpack(func.argnames) | |
| 900 | |
| 901 def finish(self): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
902 self.graph.startExitBlock() |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
903 if not self.isLambda: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
904 self.emit('LOAD_CONST', None) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
905 self.emit('RETURN_VALUE') |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
906 |
| 12009 | 907 def generateArgUnpack(self, args): |
| 908 count = 0 | |
| 909 for arg in args: | |
| 910 if type(arg) == types.TupleType: | |
| 911 self.emit('LOAD_FAST', '.nested%d' % count) | |
| 912 count = count + 1 | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
913 self.unpackSequence(arg) |
| 12009 | 914 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
915 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
|
916 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
|
917 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
|
918 else: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
919 self.emit('UNPACK_TUPLE', len(tup)) |
| 12009 | 920 for elt in tup: |
| 921 if type(elt) == types.TupleType: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
922 self.unpackSequence(elt) |
| 12009 | 923 else: |
| 924 self.emit('STORE_FAST', elt) | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
925 |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
926 unpackTuple = unpackSequence |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
927 |
| 12009 | 928 class ClassCodeGenerator(CodeGenerator): |
| 929 super_init = CodeGenerator.__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
|
930 |
| 12009 | 931 def __init__(self, klass, filename): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
932 self.graph = pyassem.PyFlowGraph(klass.name, filename, |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
933 optimized=0) |
| 12009 | 934 self.super_init(filename) |
| 935 lnf = walk(klass.code, LocalNameFinder(), 0) | |
| 936 self.locals.push(lnf.getLocals()) | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
937 self.graph.setFlag(CO_NEWLOCALS) |
| 12009 | 938 |
| 939 def finish(self): | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
940 self.graph.startExitBlock() |
| 12009 | 941 self.emit('LOAD_LOCALS') |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
942 self.emit('RETURN_VALUE') |
| 12009 | 943 |
| 944 def generateArgList(arglist): | |
| 945 """Generate an arg list marking TupleArgs""" | |
| 946 args = [] | |
| 947 extra = [] | |
| 948 count = 0 | |
| 949 for elt in arglist: | |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
950 if type(elt) == types.StringType: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
951 args.append(elt) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
952 elif type(elt) == types.TupleType: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
953 args.append(TupleArg(count, elt)) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
954 count = count + 1 |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
955 extra.extend(misc.flatten(elt)) |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
956 else: |
|
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
957 raise ValueError, "unexpect argument type:", elt |
| 12009 | 958 return args + extra, count |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
959 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
960 class LocalNameFinder: |
| 12009 | 961 """Find local names in scope""" |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
962 def __init__(self, names=()): |
| 11806 | 963 self.names = misc.Set() |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
964 self.globals = misc.Set() |
| 11806 | 965 for name in names: |
| 966 self.names.add(name) | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
967 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
968 def getLocals(self): |
| 12009 | 969 for elt in self.globals.elements(): |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
970 if self.names.has_elt(elt): |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
971 self.names.remove(elt) |
| 11806 | 972 return self.names |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
973 |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
974 def visitDict(self, node): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
975 pass |
|
11755
57fb3501ab22
split compile.py into two files
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11754
diff
changeset
|
976 |
|
11734
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
977 def visitGlobal(self, node): |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
978 for name in node.names: |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
979 self.globals.add(name) |
|
6a99ff069457
add ExampleASTVisitor:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11727
diff
changeset
|
980 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
981 def visitFunction(self, node): |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
982 self.names.add(node.name) |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
983 |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
984 def visitLambda(self, node): |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
985 pass |
|
11751
3f1da05ced55
add support for Lambda nodes
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11748
diff
changeset
|
986 |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
987 def visitImport(self, node): |
| 14359 | 988 for name, alias in node.names: |
| 989 self.names.add(alias or name) | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
990 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
991 def visitFrom(self, node): |
| 14359 | 992 for name, alias in node.names: |
| 993 self.names.add(alias or name) | |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
994 |
|
11927
79b6a6a882d9
rename compile.py to pycodegen.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11808
diff
changeset
|
995 def visitClass(self, node): |
| 11806 | 996 self.names.add(node.name) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
997 |
|
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
998 def visitAssName(self, node): |
| 11806 | 999 self.names.add(node.name) |
|
11705
d10c108d3fd7
checking in initial weekend's work
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1000 |
| 12009 | 1001 def findOp(node): |
| 1002 """Find the op (DELETE, LOAD, STORE) in an AssTuple tree""" | |
| 1003 v = OpFinder() | |
| 1004 walk(node, v, 0) | |
| 1005 return v.op | |
| 1006 | |
|
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
|
1007 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
|
1008 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
|
1009 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
|
1010 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
|
1011 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
|
1012 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
|
1013 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
|
1014 raise ValueError, "mixed ops in stmt" |
|
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
|
1015 |
|
15205
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1016 class Delegator: |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1017 """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
|
1018 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1019 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
|
1020 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
|
1021 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
|
1022 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
|
1023 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
|
1024 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
|
1025 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
|
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 __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
|
1028 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
|
1029 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1030 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
|
1031 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
|
1032 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1033 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
|
1034 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1035 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1036 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
|
1037 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1038 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1039 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
|
1040 pass |
|
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 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
|
1043 pass |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1044 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1045 wrapper = { |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1046 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
|
1047 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
|
1048 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
|
1049 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
|
1050 } |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1051 |
|
04e76e08e10f
Now supports entire Python 2.0 language and still supports Python
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15185
diff
changeset
|
1052 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
|
1053 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
|
1054 |
| 12009 | 1055 if __name__ == "__main__": |
| 1056 import sys | |
|
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
|
1057 |
| 12009 | 1058 for file in sys.argv[1:]: |
|
13941
d56dd1530ce1
Bring Tools/compiler almost up to date. Specifically:
Thomas Wouters <thomas@python.org>
parents:
13864
diff
changeset
|
1059 compile(file) |
