# HG changeset patch # User paulb@localhost.localdomain # Date 1165103490 -3600 # Node ID 8097612ea925a9f6686187eed30ba17dae36fc21 # Parent 045bd526bc79121f55c75db97525ef1f7f3a2df8 Added Print and Printnl support. Added call target pop-ups to the viewer. Suppressed exception reports. diff -r 045bd526bc79 -r 8097612ea925 simplified.py --- a/simplified.py Sun Dec 03 00:50:06 2006 +0100 +++ b/simplified.py Sun Dec 03 00:51:30 2006 +0100 @@ -298,7 +298,6 @@ class Try(Node): "A try...except...else...finally grouping node." class Raise(Node): "An exception raising node." class Not(Node): "A negation of an expression." -class Invoke(Node): "An invocation." # There are two types of return node: return from function and return from # block. @@ -328,6 +327,12 @@ # Invocations involve some more work to process calculated attributes. +class Invoke(Node): + + "An invocation." + + pass + class InvokeFunction(Invoke): "A function or method invocation." diff -r 045bd526bc79 -r 8097612ea925 simplify.py --- a/simplify.py Sun Dec 03 00:50:06 2006 +0100 +++ b/simplify.py Sun Dec 03 00:51:30 2006 +0100 @@ -51,12 +51,12 @@ Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, Div, FloorDiv, For, From, Function, Getattr, Global, If, Import, Invert, Keyword, Lambda, List, Module, Mul, Name, Not, Or, Pass, - Power, Raise, Return, Slice, Stmt, Sub, Subscript, TryExcept, - TryFinally, Tuple, While, UnaryAdd, UnarySub. + Power, Print, Printnl, Raise, Return, Slice, Stmt, Sub, Subscript, + TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub. Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, - Exec, LeftShift, ListComp, ListCompFor, ListCompIf, Print, Printnl, - RightShift, Sliceobj, Yield. + Exec, LeftShift, ListComp, ListCompFor, ListCompIf, RightShift, + Sliceobj, Yield. """ def __init__(self, builtins=0): @@ -784,9 +784,9 @@ tree. Produce something like the following: StoreName (name) - (expr) -> Subprogram (params) - (star) - (dstar) + (expr) -> LoadRef (ref) -> Subprogram (params) + (star) + (dstar) """ subprogram = Subprogram(name=function.name, module=self.module, structures=self.current_structures[:], @@ -1002,6 +1002,70 @@ def visitPower(self, power): return self._visitBinary(power, "__pow__", "__rpow__") + def visitPrint(self, print_): + + """ + Convert... + + Print (dest) -> + (nodes) + + ...to: + + StoreTemp (index) -> "print" + (expr) -> LoadAttr (expr) -> (dest) + (name) -> "write" + InvokeFunction (expr) -> LoadTemp (index) -> "print" + (args) -> [(node)] + ReleaseTemp (index) -> "print" + """ + + if print_.dest is not None: + dest = self.dispatch(print_.dest) + else: + dest = self.dispatch(compiler.ast.Name("stdout")) + + result = Assign(print_, 1, + code=[ + StoreTemp( + index="print", + expr=LoadAttr( + expr=dest, + name="write" + ) + ) + ] + ) + + for node in print_.nodes: + result.code.append( + InvokeFunction( + expr=LoadTemp(index="print"), + args=[self.dispatch(node)], + star=None, + dstar=None + ) + ) + + result.code.append( + ReleaseTemp(index="print") + ) + + return result + + def visitPrintnl(self, printnl): + result = self.visitPrint(printnl) + result.code.insert( + len(result.code) - 1, + InvokeFunction( + expr=LoadTemp(index="print"), + args=[self.dispatch(compiler.ast.Const("\n"))], + star=None, + dstar=None + ) + ) + return result + def visitRaise(self, raise_): result = Raise(raise_, 1) if raise_.expr2 is None: @@ -1255,19 +1319,27 @@ self.current_subprograms.append(subprogram) # Include a conditional statement in the subprogram. - - test = Conditional(else_=[]) - test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__bool__"), args=[], star=None, dstar=None) - # Inside the conditional, add a recursive invocation to the subprogram # if the test condition was satisfied. - - continuation = InvokeBlock() - continuation.expr = LoadRef(ref=subprogram) - # Return within the main section of the loop. - test.body = self.dispatch(while_.body) + [continuation, ReturnFromBlock()] + test = Conditional( + test=InvokeFunction( + expr=LoadAttr( + expr=self.dispatch(while_.test), + name="__bool__"), + args=[], + star=None, + dstar=None + ), + body=self.dispatch(while_.body) + [ + InvokeBlock( + expr=LoadRef(ref=subprogram) + ), + ReturnFromBlock() + ], + else_=[] + ) # Provide the else section, if present, along with an explicit return. @@ -1283,8 +1355,9 @@ # Make an invocation of the subprogram. - result = InvokeBlock(while_, 1) - result.expr = LoadRef(ref=subprogram) + result = InvokeBlock(while_, 1, + expr=LoadRef(ref=subprogram) + ) return result # Convenience methods. diff -r 045bd526bc79 -r 8097612ea925 viewer.py --- a/viewer.py Sun Dec 03 00:50:06 2006 +0100 +++ b/viewer.py Sun Dec 03 00:51:30 2006 +0100 @@ -157,6 +157,7 @@ .conditional, .operator, .iterator, + .call, .returns { position: relative; @@ -168,6 +169,7 @@ .conditional:hover > .popup, .operator:hover > .popup, .iterator:hover > .popup, + .call:hover > .popup, .returns:hover > .popup { display: block; @@ -192,13 +194,13 @@ Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, Div, FloorDiv, For, Function, Getattr, If, Keyword, Lambda, List, - Module, Mul, Name, Not, Or, Pass, Raise, Return, Slice, Stmt, Sub, - Subscript, TryExcept, TryFinally, Tuple, UnaryAdd, UnarySub, While. + Module, Mul, Name, Not, Or, Pass, Print, Printnl, Raise, Return, + Slice, Stmt, Sub, Subscript, TryExcept, TryFinally, Tuple, + UnaryAdd, UnarySub, While. Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, Exec, From, Global, Import, Invert, LeftShift, ListComp, - ListCompFor, ListCompIf, Mod, Power, Print, Printnl, RightShift, - Sliceobj, Yield. + ListCompFor, ListCompIf, Mod, Power, RightShift, Sliceobj, Yield. """ def __init__(self, stream): @@ -212,13 +214,14 @@ self.stream.write(html_footer) def dispatch(self, node): - try: - ASTVisitor.dispatch(self, node) - except ViewerError, exc: - exc.add(node) - raise - except Exception, exc: - raise ViewerError(exc, node) + ASTVisitor.dispatch(self, node) + #try: + # ASTVisitor.dispatch(self, node) + #except ViewerError, exc: + # exc.add(node) + # raise + #except Exception, exc: + # raise ViewerError(exc, node) def visitModule(self, node): self.default(node) @@ -393,6 +396,31 @@ self._keyword("pass") self.stream.write("\n") + def visitPrint(self, node): + self.stream.write("
\n") + self._keyword("print") + if node.dest is not None: + self.stream.write(">>\n") + self.dispatch(node.dest) + for n in node.nodes: + self.dispatch(n) + self.stream.write(",\n") + self.stream.write("
\n") + + def visitPrintnl(self, node): + self.stream.write("
\n") + self._keyword("print") + if node.dest is not None: + self.stream.write(">>\n") + self.dispatch(node.dest) + first = 1 + for n in node.nodes: + if not first: + self.stream.write(",\n") + self.dispatch(n) + first = 0 + self.stream.write("
\n") + def visitRaise(self, node): self.stream.write("
\n") self._keyword("raise") @@ -477,7 +505,7 @@ self.stream.write("\n") self._keyword("while") self._popup_start() - self._invocations(node.test) + self._invocations(node._node.expr.ref.code[0].test) self._popup_end() self.stream.write("\n") self.dispatch(node.test) @@ -585,7 +613,12 @@ def visitCallFunc(self, node): self.stream.write("\n") self.dispatch(node.node) + self.stream.write("\n") self.stream.write("(") + self._popup_start() + self._invocations(node._node) + self._popup_end() + self.stream.write("\n") first = 1 for arg in node.args: if not first: