1.1 --- a/simplify.py Sun Nov 26 21:28:11 2006 +0100
1.2 +++ b/simplify.py Mon Nov 27 01:06:38 2006 +0100
1.3 @@ -47,14 +47,14 @@
1.4 """
1.5 A simplifying visitor for AST nodes.
1.6
1.7 - Covered: And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
1.8 - CallFunc, Class, Compare, Const, Continue, Dict, Discard, For,
1.9 - From, Function, Getattr, Global, If, Import, Invert, Keyword,
1.10 + Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign,
1.11 + Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard,
1.12 + For, From, Function, Getattr, Global, If, Import, Invert, Keyword,
1.13 Lambda, List, Module, Name, Not, Or, Pass, Raise, Return, Slice,
1.14 Stmt, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd,
1.15 UnarySub.
1.16
1.17 - Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Div,
1.18 + Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Div,
1.19 Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor,
1.20 ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, Sliceobj,
1.21 Sub, Yield.
1.22 @@ -348,8 +348,7 @@
1.23 nodes += self.dispatch(stmt)
1.24 catch_all = 1
1.25
1.26 - # Produce something like...
1.27 - # isinstance(<exc>, <spec>)
1.28 + # Produce an exception value check.
1.29
1.30 else:
1.31 test = Conditional(
1.32 @@ -689,42 +688,50 @@
1.33 """
1.34 Emulate the current mechanisms by producing nodes as follows:
1.35
1.36 - Try (body) -> x.__add__(y)
1.37 - (else)
1.38 - (handler) -> Conditional (test) -> isinstance(exc, TypeError)
1.39 - (body) -> y.__radd__(x)
1.40 - (else)
1.41 + InvokeBlock -> Subprogram -> Try (body) -> Return (expr) -> x.__add__(y)
1.42 + (else)
1.43 + (handler) -> Conditional (test) -> CheckExc (expr) -> LoadExc
1.44 + (choices) -> LoadName TypeError
1.45 + (body) -> Return (expr) -> y.__radd__(x)
1.46 + (else)
1.47 """
1.48
1.49 - result = Try(add, 1,
1.50 - body=[
1.51 - InvokeFunction(
1.52 - expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"),
1.53 - args=[self.dispatch(add.right)],
1.54 - star=None,
1.55 - dstar=None)
1.56 - ],
1.57 - else_=[],
1.58 - finally_=[],
1.59 - handler=[
1.60 - Conditional(
1.61 - test=InvokeFunction(
1.62 - expr=LoadName(name="isinstance"),
1.63 - args=[LoadExc(), LoadName(name="TypeError")],
1.64 - star=None,
1.65 - dstar=None),
1.66 - body=[
1.67 - InvokeFunction(
1.68 - expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"),
1.69 - args=[self.dispatch(add.left)],
1.70 + subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None)
1.71 + self.current_subprograms.append(subprogram)
1.72 +
1.73 + subprogram.code = [
1.74 + Try(add, 1,
1.75 + body=[
1.76 + Return(
1.77 + expr=InvokeFunction(
1.78 + expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"),
1.79 + args=[self.dispatch(add.right)],
1.80 star=None,
1.81 dstar=None)
1.82 - ],
1.83 - else_=[]
1.84 - )
1.85 - ]
1.86 - )
1.87 + )
1.88 + ],
1.89 + else_=[],
1.90 + finally_=[],
1.91 + handler=[
1.92 + Conditional(
1.93 + test=CheckExc(expr=LoadExc(), choices=[LoadName(name="TypeError")]),
1.94 + body=[
1.95 + Return(
1.96 + expr=InvokeFunction(
1.97 + expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"),
1.98 + args=[self.dispatch(add.left)],
1.99 + star=None,
1.100 + dstar=None)
1.101 + )
1.102 + ],
1.103 + else_=[]
1.104 + )
1.105 + ]
1.106 + )
1.107 + ]
1.108
1.109 + result = InvokeBlock(produces_result=1)
1.110 + result.expr = LoadRef(ref=subprogram)
1.111 return result
1.112
1.113 # Assignments.