simplify

Changeset

3:d6842e55677d
2006-07-09 Paul Boddie raw files shortlog changelog graph Added From and Import.
simplified.py (file) simplify.py (file) tests/from.py (file) tests/import.py (file)
     1.1 --- a/simplified.py	Sun Jul 09 13:45:27 2006 +0200
     1.2 +++ b/simplified.py	Sun Jul 09 14:15:59 2006 +0200
     1.3 @@ -97,6 +97,7 @@
     1.4  class Assign(Node): "A grouping node for assignment-related operations."
     1.5  class Keyword(Node): "A grouping node for keyword arguments."
     1.6  class Global(Node): "A global name designator."
     1.7 +class Import(Node): "A module import operation."
     1.8  class LoadTemp(Node): "Load a previously-stored temporary value."
     1.9  class LoadName(Node): "Load a named object."
    1.10  class LoadAttr(Node): "Load an object attribute."
     2.1 --- a/simplify.py	Sun Jul 09 13:45:27 2006 +0200
     2.2 +++ b/simplify.py	Sun Jul 09 14:15:59 2006 +0200
     2.3 @@ -31,15 +31,16 @@
     2.4      A simplifying visitor for AST nodes.
     2.5  
     2.6      Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break,
     2.7 -             CallFunc, Class, Const, Continue, Dict, Discard, For, Function,
     2.8 -             Getattr, Global, If, Keyword, Lambda, List, Module, Name, Pass,
     2.9 -             Return, Stmt, TryExcept, TryFinally, Tuple, While.
    2.10 +             CallFunc, Class, Const, Continue, Dict, Discard, For, From,
    2.11 +             Function, Getattr, Global, If, Import, Keyword, Lambda, List,
    2.12 +             Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, Tuple,
    2.13 +             While.
    2.14  
    2.15      Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare,
    2.16 -             Decorators, Div, Ellipsis, Exec, FloorDiv, From, Import, Invert,
    2.17 -             LeftShift, ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or,
    2.18 -             Power, Print, Printnl, Raise, RightShift, Slice, Sliceobj, Sub,
    2.19 -             Subscript, UnaryAdd, UnarySub, Yield.
    2.20 +             Decorators, Div, Ellipsis, Exec, FloorDiv, Invert, LeftShift,
    2.21 +             ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, Print,
    2.22 +             Printnl, Raise, RightShift, Slice, Sliceobj, Sub, Subscript,
    2.23 +             UnaryAdd, UnarySub, Yield.
    2.24      """
    2.25  
    2.26      def __init__(self):
    2.27 @@ -99,6 +100,25 @@
    2.28          result = Global(global_, names=global_.names)
    2.29          return result
    2.30  
    2.31 +    def visitImport(self, import_):
    2.32 +        result = Assign(import_)
    2.33 +        code = []
    2.34 +        for path, alias in import_.names:
    2.35 +            importer = Import(name=path)
    2.36 +            top = alias or path.split(".")[0]
    2.37 +            code.append(StoreName(expr=importer, name=top))
    2.38 +        result.code = code
    2.39 +        return result
    2.40 +
    2.41 +    def visitFrom(self, from_):
    2.42 +        result = Assign(from_)
    2.43 +        code = []
    2.44 +        code.append(StoreTemp(expr=Import(name=from_.modname)))
    2.45 +        for name, alias in from_.names:
    2.46 +            code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name)))
    2.47 +        result.code = code
    2.48 +        return result
    2.49 +
    2.50      def visitName(self, name):
    2.51          result = LoadName(name, name=name.name)
    2.52          return result
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/from.py	Sun Jul 09 14:15:59 2006 +0200
     3.3 @@ -0,0 +1,1 @@
     3.4 +from a.b import c, d as e, f as g
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/import.py	Sun Jul 09 14:15:59 2006 +0200
     4.3 @@ -0,0 +1,1 @@
     4.4 +import a, b.c, d as e, f.g as h