1.1 --- a/simplify.py Sun Jul 09 13:45:27 2006 +0200
1.2 +++ b/simplify.py Sun Jul 09 14:15:59 2006 +0200
1.3 @@ -31,15 +31,16 @@
1.4 A simplifying visitor for AST nodes.
1.5
1.6 Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
1.7 - CallFunc, Class, Const, Continue, Dict, Discard, For, Function,
1.8 - Getattr, Global, If, Keyword, Lambda, List, Module, Name, Pass,
1.9 - Return, Stmt, TryExcept, TryFinally, Tuple, While.
1.10 + CallFunc, Class, Const, Continue, Dict, Discard, For, From,
1.11 + Function, Getattr, Global, If, Import, Keyword, Lambda, List,
1.12 + Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple,
1.13 + While.
1.14
1.15 Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare,
1.16 - Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import, Invert,
1.17 - LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or,
1.18 - Power, Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub,
1.19 - Subscript, UnaryAdd, UnarySub, Yield.
1.20 + Decorators, Div, Ellipsis, Exec, FloorDiv, Invert, LeftShift,
1.21 + ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, Print,
1.22 + Printnl, Raise, RightShift, Slice, Sliceobj, Sub, Subscript,
1.23 + UnaryAdd, UnarySub, Yield.
1.24 """
1.25
1.26 def __init__(self):
1.27 @@ -99,6 +100,25 @@
1.28 result = Global(global_, names=global_.names)
1.29 return result
1.30
1.31 + def visitImport(self, import_):
1.32 + result = Assign(import_)
1.33 + code = []
1.34 + for path, alias in import_.names:
1.35 + importer = Import(name=path)
1.36 + top = alias or path.split(".")[0]
1.37 + code.append(StoreName(expr=importer, name=top))
1.38 + result.code = code
1.39 + return result
1.40 +
1.41 + def visitFrom(self, from_):
1.42 + result = Assign(from_)
1.43 + code = []
1.44 + code.append(StoreTemp(expr=Import(name=from_.modname)))
1.45 + for name, alias in from_.names:
1.46 + code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name)))
1.47 + result.code = code
1.48 + return result
1.49 +
1.50 def visitName(self, name):
1.51 result = LoadName(name, name=name.name)
1.52 return result