1.1 --- a/simplified.py Sun Dec 03 00:50:06 2006 +0100
1.2 +++ b/simplified.py Sun Dec 03 00:51:30 2006 +0100
1.3 @@ -298,7 +298,6 @@
1.4 class Try(Node): "A try...except...else...finally grouping node."
1.5 class Raise(Node): "An exception raising node."
1.6 class Not(Node): "A negation of an expression."
1.7 -class Invoke(Node): "An invocation."
1.8
1.9 # There are two types of return node: return from function and return from
1.10 # block.
1.11 @@ -328,6 +327,12 @@
1.12
1.13 # Invocations involve some more work to process calculated attributes.
1.14
1.15 +class Invoke(Node):
1.16 +
1.17 + "An invocation."
1.18 +
1.19 + pass
1.20 +
1.21 class InvokeFunction(Invoke):
1.22
1.23 "A function or method invocation."
2.1 --- a/simplify.py Sun Dec 03 00:50:06 2006 +0100
2.2 +++ b/simplify.py Sun Dec 03 00:51:30 2006 +0100
2.3 @@ -51,12 +51,12 @@
2.4 Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard,
2.5 Div, FloorDiv, For, From, Function, Getattr, Global, If, Import,
2.6 Invert, Keyword, Lambda, List, Module, Mul, Name, Not, Or, Pass,
2.7 - Power, Raise, Return, Slice, Stmt, Sub, Subscript, TryExcept,
2.8 - TryFinally, Tuple, While, UnaryAdd, UnarySub.
2.9 + Power, Print, Printnl, Raise, Return, Slice, Stmt, Sub, Subscript,
2.10 + TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub.
2.11
2.12 Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis,
2.13 - Exec, LeftShift, ListComp, ListCompFor, ListCompIf, Print, Printnl,
2.14 - RightShift, Sliceobj, Yield.
2.15 + Exec, LeftShift, ListComp, ListCompFor, ListCompIf, RightShift,
2.16 + Sliceobj, Yield.
2.17 """
2.18
2.19 def __init__(self, builtins=0):
2.20 @@ -784,9 +784,9 @@
2.21 tree. Produce something like the following:
2.22
2.23 StoreName (name)
2.24 - (expr) -> Subprogram (params)
2.25 - (star)
2.26 - (dstar)
2.27 + (expr) -> LoadRef (ref) -> Subprogram (params)
2.28 + (star)
2.29 + (dstar)
2.30 """
2.31
2.32 subprogram = Subprogram(name=function.name, module=self.module, structures=self.current_structures[:],
2.33 @@ -1002,6 +1002,70 @@
2.34 def visitPower(self, power):
2.35 return self._visitBinary(power, "__pow__", "__rpow__")
2.36
2.37 + def visitPrint(self, print_):
2.38 +
2.39 + """
2.40 + Convert...
2.41 +
2.42 + Print (dest) ->
2.43 + (nodes)
2.44 +
2.45 + ...to:
2.46 +
2.47 + StoreTemp (index) -> "print"
2.48 + (expr) -> LoadAttr (expr) -> (dest)
2.49 + (name) -> "write"
2.50 + InvokeFunction (expr) -> LoadTemp (index) -> "print"
2.51 + (args) -> [(node)]
2.52 + ReleaseTemp (index) -> "print"
2.53 + """
2.54 +
2.55 + if print_.dest is not None:
2.56 + dest = self.dispatch(print_.dest)
2.57 + else:
2.58 + dest = self.dispatch(compiler.ast.Name("stdout"))
2.59 +
2.60 + result = Assign(print_, 1,
2.61 + code=[
2.62 + StoreTemp(
2.63 + index="print",
2.64 + expr=LoadAttr(
2.65 + expr=dest,
2.66 + name="write"
2.67 + )
2.68 + )
2.69 + ]
2.70 + )
2.71 +
2.72 + for node in print_.nodes:
2.73 + result.code.append(
2.74 + InvokeFunction(
2.75 + expr=LoadTemp(index="print"),
2.76 + args=[self.dispatch(node)],
2.77 + star=None,
2.78 + dstar=None
2.79 + )
2.80 + )
2.81 +
2.82 + result.code.append(
2.83 + ReleaseTemp(index="print")
2.84 + )
2.85 +
2.86 + return result
2.87 +
2.88 + def visitPrintnl(self, printnl):
2.89 + result = self.visitPrint(printnl)
2.90 + result.code.insert(
2.91 + len(result.code) - 1,
2.92 + InvokeFunction(
2.93 + expr=LoadTemp(index="print"),
2.94 + args=[self.dispatch(compiler.ast.Const("\n"))],
2.95 + star=None,
2.96 + dstar=None
2.97 + )
2.98 + )
2.99 + return result
2.100 +
2.101 def visitRaise(self, raise_):
2.102 result = Raise(raise_, 1)
2.103 if raise_.expr2 is None:
2.104 @@ -1255,19 +1319,27 @@
2.105 self.current_subprograms.append(subprogram)
2.106
2.107 # Include a conditional statement in the subprogram.
2.108 -
2.109 - test = Conditional(else_=[])
2.110 - test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__bool__"), args=[], star=None, dstar=None)
2.111 -
2.112 # Inside the conditional, add a recursive invocation to the subprogram
2.113 # if the test condition was satisfied.
2.114 -
2.115 - continuation = InvokeBlock()
2.116 - continuation.expr = LoadRef(ref=subprogram)
2.117 -
2.118 # Return within the main section of the loop.
2.119
2.120 - test.body = self.dispatch(while_.body) + [continuation, ReturnFromBlock()]
2.121 + test = Conditional(
2.122 + test=InvokeFunction(
2.123 + expr=LoadAttr(
2.124 + expr=self.dispatch(while_.test),
2.125 + name="__bool__"),
2.126 + args=[],
2.127 + star=None,
2.128 + dstar=None
2.129 + ),
2.130 + body=self.dispatch(while_.body) + [
2.131 + InvokeBlock(
2.132 + expr=LoadRef(ref=subprogram)
2.133 + ),
2.134 + ReturnFromBlock()
2.135 + ],
2.136 + else_=[]
2.137 + )
2.138
2.139 # Provide the else section, if present, along with an explicit return.
2.140
2.141 @@ -1283,8 +1355,9 @@
2.142
2.143 # Make an invocation of the subprogram.
2.144
2.145 - result = InvokeBlock(while_, 1)
2.146 - result.expr = LoadRef(ref=subprogram)
2.147 + result = InvokeBlock(while_, 1,
2.148 + expr=LoadRef(ref=subprogram)
2.149 + )
2.150 return result
2.151
2.152 # Convenience methods.
3.1 --- a/viewer.py Sun Dec 03 00:50:06 2006 +0100
3.2 +++ b/viewer.py Sun Dec 03 00:51:30 2006 +0100
3.3 @@ -157,6 +157,7 @@
3.4 .conditional,
3.5 .operator,
3.6 .iterator,
3.7 + .call,
3.8 .returns
3.9 {
3.10 position: relative;
3.11 @@ -168,6 +169,7 @@
3.12 .conditional:hover > .popup,
3.13 .operator:hover > .popup,
3.14 .iterator:hover > .popup,
3.15 + .call:hover > .popup,
3.16 .returns:hover > .popup
3.17 {
3.18 display: block;
3.19 @@ -192,13 +194,13 @@
3.20 Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign,
3.21 Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard,
3.22 Div, FloorDiv, For, Function, Getattr, If, Keyword, Lambda, List,
3.23 - Module, Mul, Name, Not, Or, Pass, Raise, Return, Slice, Stmt, Sub,
3.24 - Subscript, TryExcept, TryFinally, Tuple, UnaryAdd, UnarySub, While.
3.25 + Module, Mul, Name, Not, Or, Pass, Print, Printnl, Raise, Return,
3.26 + Slice, Stmt, Sub, Subscript, TryExcept, TryFinally, Tuple,
3.27 + UnaryAdd, UnarySub, While.
3.28
3.29 Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis,
3.30 Exec, From, Global, Import, Invert, LeftShift, ListComp,
3.31 - ListCompFor, ListCompIf, Mod, Power, Print, Printnl, RightShift,
3.32 - Sliceobj, Yield.
3.33 + ListCompFor, ListCompIf, Mod, Power, RightShift, Sliceobj, Yield.
3.34 """
3.35
3.36 def __init__(self, stream):
3.37 @@ -212,13 +214,14 @@
3.38 self.stream.write(html_footer)
3.39
3.40 def dispatch(self, node):
3.41 - try:
3.42 - ASTVisitor.dispatch(self, node)
3.43 - except ViewerError, exc:
3.44 - exc.add(node)
3.45 - raise
3.46 - except Exception, exc:
3.47 - raise ViewerError(exc, node)
3.48 + ASTVisitor.dispatch(self, node)
3.49 + #try:
3.50 + # ASTVisitor.dispatch(self, node)
3.51 + #except ViewerError, exc:
3.52 + # exc.add(node)
3.53 + # raise
3.54 + #except Exception, exc:
3.55 + # raise ViewerError(exc, node)
3.56
3.57 def visitModule(self, node):
3.58 self.default(node)
3.59 @@ -393,6 +396,31 @@
3.60 self._keyword("pass")
3.61 self.stream.write("</div>\n")
3.62
3.63 + def visitPrint(self, node):
3.64 + self.stream.write("<div class='print'>\n")
3.65 + self._keyword("print")
3.66 + if node.dest is not None:
3.67 + self.stream.write(">>\n")
3.68 + self.dispatch(node.dest)
3.69 + for n in node.nodes:
3.70 + self.dispatch(n)
3.71 + self.stream.write(",\n")
3.72 + self.stream.write("</div>\n")
3.73 +
3.74 + def visitPrintnl(self, node):
3.75 + self.stream.write("<div class='printnl'>\n")
3.76 + self._keyword("print")
3.77 + if node.dest is not None:
3.78 + self.stream.write(">>\n")
3.79 + self.dispatch(node.dest)
3.80 + first = 1
3.81 + for n in node.nodes:
3.82 + if not first:
3.83 + self.stream.write(",\n")
3.84 + self.dispatch(n)
3.85 + first = 0
3.86 + self.stream.write("</div>\n")
3.87 +
3.88 def visitRaise(self, node):
3.89 self.stream.write("<div class='raise'>\n")
3.90 self._keyword("raise")
3.91 @@ -477,7 +505,7 @@
3.92 self.stream.write("<span class='conditional'>\n")
3.93 self._keyword("while")
3.94 self._popup_start()
3.95 - self._invocations(node.test)
3.96 + self._invocations(node._node.expr.ref.code[0].test)
3.97 self._popup_end()
3.98 self.stream.write("</span>\n")
3.99 self.dispatch(node.test)
3.100 @@ -585,7 +613,12 @@
3.101 def visitCallFunc(self, node):
3.102 self.stream.write("<span class='callfunc'>\n")
3.103 self.dispatch(node.node)
3.104 + self.stream.write("<span class='call'>\n")
3.105 self.stream.write("(")
3.106 + self._popup_start()
3.107 + self._invocations(node._node)
3.108 + self._popup_end()
3.109 + self.stream.write("</span>\n")
3.110 first = 1
3.111 for arg in node.args:
3.112 if not first: