1.1 --- a/simplify.py Thu Feb 22 22:06:10 2007 +0100
1.2 +++ b/simplify.py Fri Feb 23 00:12:44 2007 +0100
1.3 @@ -47,15 +47,15 @@
1.4 """
1.5 A simplifying visitor for AST nodes.
1.6
1.7 - Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign,
1.8 - Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard,
1.9 - Div, FloorDiv, For, From, Function, Getattr, Global, If, Import,
1.10 - Invert, Keyword, Lambda, List, ListComp, ListCompFor, ListCompIf,
1.11 - Mod, Module, Mul, Name, Not, Or, Pass, Power, Print, Printnl,
1.12 - Raise, Return, Slice, Sliceobj, Stmt, Sub, Subscript, TryExcept,
1.13 - TryFinally, Tuple, While, UnaryAdd, UnarySub.
1.14 + Covered: Add, And, Assert, AssAttr, AssList, AssName, AssTuple, Assign,
1.15 + AugAssign, Break, CallFunc, Class, Compare, Const, Continue, Dict,
1.16 + Discard, Div, FloorDiv, For, From, Function, Getattr, Global, If,
1.17 + Import, Invert, Keyword, Lambda, List, ListComp, ListCompFor,
1.18 + ListCompIf, Mod, Module, Mul, Name, Not, Or, Pass, Power, Print,
1.19 + Printnl, Raise, Return, Slice, Sliceobj, Stmt, Sub, Subscript,
1.20 + TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub.
1.21
1.22 - Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis,
1.23 + Missing: Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis,
1.24 Exec, LeftShift, RightShift, Yield.
1.25 """
1.26
1.27 @@ -205,6 +205,32 @@
1.28 result.expr = LoadRef(ref=subprogram)
1.29 return result
1.30
1.31 + def visitAssert(self, assert_):
1.32 + if assert_.fail:
1.33 + fail_args = [self.dispatch(assert_.fail)]
1.34 + else:
1.35 + fail_args = []
1.36 +
1.37 + result = Conditional(assert_, 1,
1.38 + test=self.dispatch(assert_.test),
1.39 + body=[],
1.40 + else_=[
1.41 + Raise(assert_,
1.42 + expr=InvokeFunction(assert_,
1.43 + expr=LoadName(name="AssertionError"),
1.44 + args=fail_args,
1.45 + star=None,
1.46 + dstar=None
1.47 + )
1.48 + )
1.49 + ]
1.50 + )
1.51 +
1.52 + # Make nice annotations for the viewer.
1.53 +
1.54 + assert_._raises = result.else_[0].expr
1.55 + return result
1.56 +
1.57 # Assignments.
1.58
1.59 def visitAssAttr(self, assattr, in_sequence=0):
1.60 @@ -991,7 +1017,7 @@
1.61
1.62 # Make nice annotations for the viewer.
1.63
1.64 - function._subprogram = subprogram
1.65 + lambda_._subprogram = subprogram
1.66
1.67 # Process the lambda contents.
1.68
1.69 @@ -1059,8 +1085,10 @@
1.70 body = self._visitListCompFor(node, quals[1:])
1.71 if qual.ifs:
1.72 body = self._visitListCompIf(node, qual.ifs, body)
1.73 + elif qual.ifs:
1.74 + body = self._visitListCompIf(node, qual.ifs)
1.75 else:
1.76 - body = self._visitListCompIf(node, qual.ifs)
1.77 + body = self._visitListCompBody(node)
1.78 return [self._visitFor(qual, body)]
1.79
1.80 def _visitListCompIf(self, node, ifs, expr=None):
1.81 @@ -1068,17 +1096,7 @@
1.82 if len(ifs) > 1:
1.83 body = self._visitListCompIf(node, ifs[1:], expr)
1.84 elif expr is None:
1.85 - body = [
1.86 - InvokeFunction(
1.87 - expr=LoadAttr(
1.88 - expr=LoadTemp(index="listcomp"),
1.89 - name="append"
1.90 - ),
1.91 - args=[self.dispatch(node.expr)],
1.92 - star=None,
1.93 - dstar=None
1.94 - )
1.95 - ]
1.96 + body = self._visitListCompBody(node)
1.97 else:
1.98 body = expr
1.99 return [
1.100 @@ -1095,6 +1113,19 @@
1.101 )
1.102 ]
1.103
1.104 + def _visitListCompBody(self, node):
1.105 + return [
1.106 + InvokeFunction(
1.107 + expr=LoadAttr(
1.108 + expr=LoadTemp(index="listcomp"),
1.109 + name="append"
1.110 + ),
1.111 + args=[self.dispatch(node.expr)],
1.112 + star=None,
1.113 + dstar=None
1.114 + )
1.115 + ]
1.116 +
1.117 def visitMod(self, mod):
1.118 return self._visitBinary(mod, "__mod__", "__rmod__")
1.119