# HG changeset patch # User Paul Boddie # Date 1152392383 -7200 # Node ID adaaf65855f237f406aff95c6a997d5170467b0c # Parent 1eb049d8af395774a5b50f3df0c4b128ba7fbc05 Added Global, And, Or, Not and made minor fixes. diff -r 1eb049d8af39 -r adaaf65855f2 simplified.py --- a/simplified.py Sat Jul 08 21:47:12 2006 +0200 +++ b/simplified.py Sat Jul 08 22:59:43 2006 +0200 @@ -77,6 +77,9 @@ self._pprint(indent, "", "}") if hasattr(self, "expr"): self.expr.pprint(indent + 2, "- ") + if hasattr(self, "nodes"): + for node in self.nodes: + node.pprint(indent + 2, "- ") if hasattr(self, "lvalue"): self.lvalue.pprint(indent + 2, "= ") if hasattr(self, "args"): @@ -91,6 +94,7 @@ class Return(Node): "Return an evaluated expression." class Assign(Node): "A grouping node for assignment-related operations." class Keyword(Node): "A grouping node for keyword arguments." +class Global(Node): "A global name designator." class LoadTemp(Node): "Load a previously-stored temporary value." class LoadName(Node): "Load a named object." class LoadAttr(Node): "Load an object attribute." @@ -105,5 +109,8 @@ class Conditional(Node): "A conditional node consisting of a test and outcomes." class Try(Node): "A try...except...else...finally grouping node." class Except(Node): "An exception handler node." +class And(Node): "A conjunction of expressions." +class Or(Node): "A disjunction of expressions." +class Not(Node): "A negation of an expression." # vim: tabstop=4 expandtab shiftwidth=4 diff -r 1eb049d8af39 -r adaaf65855f2 simplify.py --- a/simplify.py Sat Jul 08 21:47:12 2006 +0200 +++ b/simplify.py Sat Jul 08 22:59:43 2006 +0200 @@ -30,24 +30,23 @@ """ A simplifying visitor for AST nodes. - Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc, Class, - Const, Continue, Dict, Discard, For, Function, Getattr, If, Keyword, Lambda, - List, Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple, - While. + Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc, + Class, Const, Continue, Dict, Discard, For, Function, Getattr, + Global, If, Keyword, Lambda, List, Module, Name, Pass, Return, + Stmt, TryExcept, TryFinally, Tuple, While. Missing: Add, And, Assert, AugAssign, Backquote, Bitand, Bitor, Bitxor, - Compare, Decorators, Div, Ellipsis, Exec, - FloorDiv, From, Global, Import, Invert, LeftShift, - ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, - Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub, - Subscript, UnaryAdd, UnarySub, Yield. + Compare, Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import, + Invert, LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul, + Not, Or, Power, Print, Printnl, Raise, RightShift, Slice, + Sliceobj, Sub, Subscript, UnaryAdd, UnarySub, Yield. """ def __init__(self): ASTVisitor.__init__(self) - self.result = None - self.subprograms = [] - self.current_subprograms = [] + self.result = None # The resulting tree. + self.subprograms = [] # Subprograms outside the tree. + self.current_subprograms = [] # Current subprograms being processed. # Generic visitor methods. @@ -96,6 +95,10 @@ result.expr = self.dispatch(keyword.expr) return result + def visitGlobal(self, global_): + result = Global(global_, names=global_.names) + return result + def visitName(self, name): result = LoadName(name, name=name.name) return result @@ -114,7 +117,7 @@ return result def visitContinue(self, continue_): - result = Invoke(same_frame=1, star=None, dstar=None, args=[]) + result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[]) result.expr = LoadRef(ref=self.current_subprograms[-1]) return result @@ -122,6 +125,8 @@ result = If(if_, else_=[]) tests = [] for compare, stmt in if_.tests: + # Produce something like... + # expr.__true__() ? body test = Conditional(else_=[], test=Invoke( expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), params=[], star=None, dstar=None)) @@ -133,11 +138,8 @@ return result def _visitBuiltin(self, builtin, name): - result = Invoke(expr=LoadName(name=name)) - args = [] - for node in builtin.nodes: - args.append(self.dispatch(node)) - result.args = args + result = Invoke(builtin, expr=LoadName(name=name)) + result.args = self.dispatches(builtin.nodes) return result def visitTuple(self, tuple): @@ -156,6 +158,31 @@ result.args = args return result + # Logical operators. + + def visitAnd(self, and_): + result = And(and_) + nodes = [] + for node in and_.nodes: + nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"), + params=[], star=None, dstar=None)) + result.nodes = nodes + return result + + def visitOr(self, or_): + result = Or(or_) + nodes = [] + for node in or_.nodes: + nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"), + params=[], star=None, dstar=None)) + result.nodes = nodes + return result + + def visitNot(self, not_): + result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), + params=[], star=None, dstar=None)) + return result + # Assignments. def visitAssign(self, assign):