1.1 --- a/simplify.py Sat Jul 08 21:47:12 2006 +0200
1.2 +++ b/simplify.py Sat Jul 08 22:59:43 2006 +0200
1.3 @@ -30,24 +30,23 @@
1.4 """
1.5 A simplifying visitor for AST nodes.
1.6
1.7 - Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc, Class,
1.8 - Const, Continue, Dict, Discard, For, Function, Getattr, If, Keyword, Lambda,
1.9 - List, Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple,
1.10 - While.
1.11 + Covered: AssAttr, AssList, AssName, AssTuple, Assign, Break, CallFunc,
1.12 + Class, Const, Continue, Dict, Discard, For, Function, Getattr,
1.13 + Global, If, Keyword, Lambda, List, Module, Name, Pass, Return,
1.14 + Stmt, TryExcept, TryFinally, Tuple, While.
1.15
1.16 Missing: Add, And, Assert, AugAssign, Backquote, Bitand, Bitor, Bitxor,
1.17 - Compare, Decorators, Div, Ellipsis, Exec,
1.18 - FloorDiv, From, Global, Import, Invert, LeftShift,
1.19 - ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power,
1.20 - Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub,
1.21 - Subscript, UnaryAdd, UnarySub, Yield.
1.22 + Compare, Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import,
1.23 + Invert, LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul,
1.24 + Not, Or, Power, Print, Printnl, Raise, RightShift, Slice,
1.25 + Sliceobj, Sub, Subscript, UnaryAdd, UnarySub, Yield.
1.26 """
1.27
1.28 def __init__(self):
1.29 ASTVisitor.__init__(self)
1.30 - self.result = None
1.31 - self.subprograms = []
1.32 - self.current_subprograms = []
1.33 + self.result = None # The resulting tree.
1.34 + self.subprograms = [] # Subprograms outside the tree.
1.35 + self.current_subprograms = [] # Current subprograms being processed.
1.36
1.37 # Generic visitor methods.
1.38
1.39 @@ -96,6 +95,10 @@
1.40 result.expr = self.dispatch(keyword.expr)
1.41 return result
1.42
1.43 + def visitGlobal(self, global_):
1.44 + result = Global(global_, names=global_.names)
1.45 + return result
1.46 +
1.47 def visitName(self, name):
1.48 result = LoadName(name, name=name.name)
1.49 return result
1.50 @@ -114,7 +117,7 @@
1.51 return result
1.52
1.53 def visitContinue(self, continue_):
1.54 - result = Invoke(same_frame=1, star=None, dstar=None, args=[])
1.55 + result = Invoke(continue_, same_frame=1, star=None, dstar=None, args=[])
1.56 result.expr = LoadRef(ref=self.current_subprograms[-1])
1.57 return result
1.58
1.59 @@ -122,6 +125,8 @@
1.60 result = If(if_, else_=[])
1.61 tests = []
1.62 for compare, stmt in if_.tests:
1.63 + # Produce something like...
1.64 + # expr.__true__() ? body
1.65 test = Conditional(else_=[], test=Invoke(
1.66 expr=LoadAttr(expr=self.dispatch(compare), name="__true__"),
1.67 params=[], star=None, dstar=None))
1.68 @@ -133,11 +138,8 @@
1.69 return result
1.70
1.71 def _visitBuiltin(self, builtin, name):
1.72 - result = Invoke(expr=LoadName(name=name))
1.73 - args = []
1.74 - for node in builtin.nodes:
1.75 - args.append(self.dispatch(node))
1.76 - result.args = args
1.77 + result = Invoke(builtin, expr=LoadName(name=name))
1.78 + result.args = self.dispatches(builtin.nodes)
1.79 return result
1.80
1.81 def visitTuple(self, tuple):
1.82 @@ -156,6 +158,31 @@
1.83 result.args = args
1.84 return result
1.85
1.86 + # Logical operators.
1.87 +
1.88 + def visitAnd(self, and_):
1.89 + result = And(and_)
1.90 + nodes = []
1.91 + for node in and_.nodes:
1.92 + nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
1.93 + params=[], star=None, dstar=None))
1.94 + result.nodes = nodes
1.95 + return result
1.96 +
1.97 + def visitOr(self, or_):
1.98 + result = Or(or_)
1.99 + nodes = []
1.100 + for node in or_.nodes:
1.101 + nodes.append(Invoke(expr=LoadAttr(expr=self.dispatch(node), name="__true__"),
1.102 + params=[], star=None, dstar=None))
1.103 + result.nodes = nodes
1.104 + return result
1.105 +
1.106 + def visitNot(self, not_):
1.107 + result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"),
1.108 + params=[], star=None, dstar=None))
1.109 + return result
1.110 +
1.111 # Assignments.
1.112
1.113 def visitAssign(self, assign):