2.1 --- a/simplify.py Sat Jul 08 21:47:12 2006 +0200
2.2 +++ b/simplify.py Sat Jul 08 22:59:43 2006 +0200
2.3 @@ -30,24 +30,23 @@
2.4 """
2.5 A simplifying visitor for AST nodes.
2.6
2.7 - Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc, Class,
2.8 - Const, Continue, Dict, Discard, For, Function, Getattr, If, Keyword, Lambda,
2.9 - List, Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple,
2.10 - While.
2.11 + Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc,
2.12 + Class, Const, Continue, Dict, Discard, For, Function, Getattr,
2.13 + Global, If, Keyword, Lambda, List, Module, Name, Pass, Return,
2.14 + Stmt, TryExcept, TryFinally, Tuple, While.
2.15
2.16 Missing: Add, And, Assert, AugAssign, Backquote, Bitand, Bitor, Bitxor,
2.17 - Compare, Decorators, Div, Ellipsis, Exec,
2.18 - FloorDiv, From, Global, Import, Invert, LeftShift,
2.19 - ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power,
2.20 - Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub,
2.21 - Subscript, UnaryAdd, UnarySub, Yield.
2.22 + Compare, Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import,
2.23 + Invert, LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul,
2.24 + Not, Or, Power, Print, Printnl, Raise, RightShift, Slice,
2.25 + Sliceobj, Sub, Subscript, UnaryAdd, UnarySub, Yield.
2.26 """
2.27
2.28 def __init__(self):
2.29 ASTVisitor.__init__(self)
2.30 - self.result = None
2.31 - self.subprograms = []
2.32 - self.current_subprograms = []
2.33 + self.result = None # The resulting tree.
2.34 + self.subprograms = [] # Subprograms outside the tree.
2.35 + self.current_subprograms = [] # Current subprograms being processed.
2.36
2.37 # Generic visitor methods.
2.38
2.39 @@ -96,6 +95,10 @@
2.40 result.expr = self.dispatch(keyword.expr)
2.41 return result
2.42
2.43 + def visitGlobal(self, global_):
2.44 + result = Global(global_, names=global_.names)
2.45 + return result
2.46 +
2.47 def visitName(self, name):
2.48 result = LoadName(name, name=name.name)
2.49 return result
2.50 @@ -114,7 +117,7 @@
2.51 return result
2.52
2.53 def visitContinue(self, continue_):
2.54 - result = Invoke(same_frame=1, star=None, dstar=None, args=[])
2.55 + result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[])
2.56 result.expr = LoadRef(ref=self.current_subprograms[-1])
2.57 return result
2.58
2.59 @@ -122,6 +125,8 @@
2.60 result = If(if_, else_=[])
2.61 tests = []
2.62 for compare, stmt in if_.tests:
2.63 + # Produce something like...
2.64 + # expr.__true__() ? body
2.65 test = Conditional(else_=[], test=Invoke(
2.66 expr=LoadAttr(expr=self.dispatch(compare), name="__true__"),
2.67 params=[], star=None, dstar=None))
2.68 @@ -133,11 +138,8 @@
2.69 return result
2.70
2.71 def _visitBuiltin(self, builtin, name):
2.72 - result = Invoke(expr=LoadName(name=name))
2.73 - args = []
2.74 - for node in builtin.nodes:
2.75 - args.append(self.dispatch(node))
2.76 - result.args = args
2.77 + result = Invoke(builtin, expr=LoadName(name=name))
2.78 + result.args = self.dispatches(builtin.nodes)
2.79 return result
2.80
2.81 def visitTuple(self, tuple):
2.82 @@ -156,6 +158,31 @@
2.83 result.args = args
2.84 return result
2.85
2.86 + # Logical operators.
2.87 +
2.88 + def visitAnd(self, and_):
2.89 + result = And(and_)
2.90 + nodes = []
2.91 + for node in and_.nodes:
2.92 + nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
2.93 + params=[], star=None, dstar=None))
2.94 + result.nodes = nodes
2.95 + return result
2.96 +
2.97 + def visitOr(self, or_):
2.98 + result = Or(or_)
2.99 + nodes = []
2.100 + for node in or_.nodes:
2.101 + nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
2.102 + params=[], star=None, dstar=None))
2.103 + result.nodes = nodes
2.104 + return result
2.105 +
2.106 + def visitNot(self, not_):
2.107 + result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"),
2.108 + params=[], star=None, dstar=None))
2.109 + return result
2.110 +
2.111 # Assignments.
2.112
2.113 def visitAssign(self, assign):