1.1 --- a/simplify.py Sun Dec 03 00:50:06 2006 +0100
1.2 +++ b/simplify.py Sun Dec 03 00:51:30 2006 +0100
1.3 @@ -51,12 +51,12 @@
1.4 Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard,
1.5 Div, FloorDiv, For, From, Function, Getattr, Global, If, Import,
1.6 Invert, Keyword, Lambda, List, Module, Mul, Name, Not, Or, Pass,
1.7 - Power, Raise, Return, Slice, Stmt, Sub, Subscript, TryExcept,
1.8 - TryFinally, Tuple, While, UnaryAdd, UnarySub.
1.9 + Power, Print, Printnl, Raise, Return, Slice, Stmt, Sub, Subscript,
1.10 + TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub.
1.11
1.12 Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis,
1.13 - Exec, LeftShift, ListComp, ListCompFor, ListCompIf, Print, Printnl,
1.14 - RightShift, Sliceobj, Yield.
1.15 + Exec, LeftShift, ListComp, ListCompFor, ListCompIf, RightShift,
1.16 + Sliceobj, Yield.
1.17 """
1.18
1.19 def __init__(self, builtins=0):
1.20 @@ -784,9 +784,9 @@
1.21 tree. Produce something like the following:
1.22
1.23 StoreName (name)
1.24 - (expr) -> Subprogram (params)
1.25 - (star)
1.26 - (dstar)
1.27 + (expr) -> LoadRef (ref) -> Subprogram (params)
1.28 + (star)
1.29 + (dstar)
1.30 """
1.31
1.32 subprogram = Subprogram(name=function.name, module=self.module, structures=self.current_structures[:],
1.33 @@ -1002,6 +1002,70 @@
1.34 def visitPower(self, power):
1.35 return self._visitBinary(power, "__pow__", "__rpow__")
1.36
1.37 + def visitPrint(self, print_):
1.38 +
1.39 + """
1.40 + Convert...
1.41 +
1.42 + Print (dest) ->
1.43 + (nodes)
1.44 +
1.45 + ...to:
1.46 +
1.47 + StoreTemp (index) -> "print"
1.48 + (expr) -> LoadAttr (expr) -> (dest)
1.49 + (name) -> "write"
1.50 + InvokeFunction (expr) -> LoadTemp (index) -> "print"
1.51 + (args) -> [(node)]
1.52 + ReleaseTemp (index) -> "print"
1.53 + """
1.54 +
1.55 + if print_.dest is not None:
1.56 + dest = self.dispatch(print_.dest)
1.57 + else:
1.58 + dest = self.dispatch(compiler.ast.Name("stdout"))
1.59 +
1.60 + result = Assign(print_, 1,
1.61 + code=[
1.62 + StoreTemp(
1.63 + index="print",
1.64 + expr=LoadAttr(
1.65 + expr=dest,
1.66 + name="write"
1.67 + )
1.68 + )
1.69 + ]
1.70 + )
1.71 +
1.72 + for node in print_.nodes:
1.73 + result.code.append(
1.74 + InvokeFunction(
1.75 + expr=LoadTemp(index="print"),
1.76 + args=[self.dispatch(node)],
1.77 + star=None,
1.78 + dstar=None
1.79 + )
1.80 + )
1.81 +
1.82 + result.code.append(
1.83 + ReleaseTemp(index="print")
1.84 + )
1.85 +
1.86 + return result
1.87 +
1.88 + def visitPrintnl(self, printnl):
1.89 + result = self.visitPrint(printnl)
1.90 + result.code.insert(
1.91 + len(result.code) - 1,
1.92 + InvokeFunction(
1.93 + expr=LoadTemp(index="print"),
1.94 + args=[self.dispatch(compiler.ast.Const("\n"))],
1.95 + star=None,
1.96 + dstar=None
1.97 + )
1.98 + )
1.99 + return result
1.100 +
1.101 def visitRaise(self, raise_):
1.102 result = Raise(raise_, 1)
1.103 if raise_.expr2 is None:
1.104 @@ -1255,19 +1319,27 @@
1.105 self.current_subprograms.append(subprogram)
1.106
1.107 # Include a conditional statement in the subprogram.
1.108 -
1.109 - test = Conditional(else_=[])
1.110 - test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__bool__"), args=[], star=None, dstar=None)
1.111 -
1.112 # Inside the conditional, add a recursive invocation to the subprogram
1.113 # if the test condition was satisfied.
1.114 -
1.115 - continuation = InvokeBlock()
1.116 - continuation.expr = LoadRef(ref=subprogram)
1.117 -
1.118 # Return within the main section of the loop.
1.119
1.120 - test.body = self.dispatch(while_.body) + [continuation, ReturnFromBlock()]
1.121 + test = Conditional(
1.122 + test=InvokeFunction(
1.123 + expr=LoadAttr(
1.124 + expr=self.dispatch(while_.test),
1.125 + name="__bool__"),
1.126 + args=[],
1.127 + star=None,
1.128 + dstar=None
1.129 + ),
1.130 + body=self.dispatch(while_.body) + [
1.131 + InvokeBlock(
1.132 + expr=LoadRef(ref=subprogram)
1.133 + ),
1.134 + ReturnFromBlock()
1.135 + ],
1.136 + else_=[]
1.137 + )
1.138
1.139 # Provide the else section, if present, along with an explicit return.
1.140
1.141 @@ -1283,8 +1355,9 @@
1.142
1.143 # Make an invocation of the subprogram.
1.144
1.145 - result = InvokeBlock(while_, 1)
1.146 - result.expr = LoadRef(ref=subprogram)
1.147 + result = InvokeBlock(while_, 1,
1.148 + expr=LoadRef(ref=subprogram)
1.149 + )
1.150 return result
1.151
1.152 # Convenience methods.