Esempio n. 1
0
class SqrPattern(Pattern):
    # X * X => X ** 2
    pattern = ast.BinOp(left=Placeholder(0),
                        op=ast.Mult(),
                        right=Placeholder(0))

    @staticmethod
    def sub():
        return ast.BinOp(left=Placeholder(0), op=ast.Pow(),
                         right=ast.Constant(2, None))
Esempio n. 2
0
 def sub():
     return ast.Call(
         func=ast.Attribute(
             ast.Attribute(ast.Name('__builtin__', ast.Load(), None, None),
                           'str', ast.Load()), 'join', ast.Load()),
         args=[
             ast.Constant(Placeholder(1), None),
             ast.Tuple([Placeholder(0), Placeholder(2)], ast.Load())
         ],
         keywords=[])
Esempio n. 3
0
class PowFuncPattern(Pattern):
    # builtins.pow(X, Y) => X ** Y

    pattern = ast.Call(func=ast.Attribute(
        value=ast.Name(id=mangle('builtins'), ctx=ast.Load(),
                       annotation=None,
                       type_comment=None),
        attr='pow', ctx=ast.Load()),
        args=[Placeholder(0), Placeholder(1)],
        keywords=[])

    @staticmethod
    def sub():
        return ast.BinOp(Placeholder(0), ast.Pow(), Placeholder(1))
Esempio n. 4
0
class LenRangePattern(Pattern):
    # builtins.len(builtins.range(X)) => max(0, X)
    pattern = ast.Call(
        func=ast.Attribute(value=ast.Name('builtins', ast.Load(), None, None),
                           attr="len",
                           ctx=ast.Load()),
        args=[
            ast.Call(func=ast.Attribute(value=ast.Name('builtins', ast.Load(),
                                                       None, None),
                                        attr="range",
                                        ctx=ast.Load()),
                     args=[Placeholder(0)],
                     keywords=[])
        ],
        keywords=[])

    @staticmethod
    def sub():
        return ast.Call(func=ast.Attribute(value=ast.Name(
            'builtins', ast.Load(), None, None),
                                           attr="max",
                                           ctx=ast.Load()),
                        args=[ast.Constant(0, None),
                              Placeholder(0)],
                        keywords=[])
Esempio n. 5
0
class ReversedRangePattern(Pattern):
    # __builtin__.reversed(__builtin__.xrange(X)) =>
    # __builtin__.xrange(X-1, -1, -1)
    # FIXME : We should do it even when begin/end/step are given
    pattern = ast.Call(func=ast.Attribute(value=ast.Name(id='__builtin__',
                                                         ctx=ast.Load(),
                                                         annotation=None,
                                                         type_comment=None),
                                          attr="reversed", ctx=ast.Load()),
                       args=[ast.Call(
                           func=ast.Attribute(
                               value=ast.Name(id='__builtin__',
                                              ctx=ast.Load(), annotation=None,
                                              type_comment=None),
                               attr=range_name, ctx=ast.Load()),
                           args=[Placeholder(0)],
                           keywords=[])],
                       keywords=[])

    @staticmethod
    def sub():
        return ast.Call(
            func=ast.Attribute(value=ast.Name(id='__builtin__',
                                              ctx=ast.Load(), annotation=None,
                                              type_comment=None),
                               attr=range_name, ctx=ast.Load()),
            args=[ast.BinOp(left=Placeholder(0), op=ast.Sub(),
                            right=ast.Constant(1, None)),
                  ast.Constant(-1, None),
                  ast.Constant(-1, None)],
            keywords=[])
Esempio n. 6
0
class LenSetPattern(Pattern):
    # __builtin__.len(__builtin__.set(X)) => __builtin__.pythran.len_set(X)
    pattern = ast.Call(
        func=ast.Attribute(value=ast.Name('__builtin__', ast.Load(), None,
                                          None),
                           attr="len",
                           ctx=ast.Load()),
        args=[
            ast.Call(func=ast.Attribute(value=ast.Name('__builtin__',
                                                       ast.Load(), None, None),
                                        attr="set",
                                        ctx=ast.Load()),
                     args=[Placeholder(0)],
                     keywords=[])
        ],
        keywords=[])

    @staticmethod
    def sub():
        return ast.Call(func=ast.Attribute(value=ast.Attribute(value=ast.Name(
            '__builtin__', ast.Load(), None, None),
                                                               attr="pythran",
                                                               ctx=ast.Load()),
                                           attr="len_set",
                                           ctx=ast.Load()),
                        args=[Placeholder(0)],
                        keywords=[])
Esempio n. 7
0
class TupleListPattern(Pattern):
    # __builtin__.tuple(__builtin__.list(X)) => __builtin__.tuple(X)

    pattern = ast.Call(
        func=ast.Attribute(value=ast.Name('__builtin__', ast.Load(), None,
                                          None),
                           attr="tuple",
                           ctx=ast.Load()),
        args=[
            ast.Call(func=ast.Attribute(value=ast.Name('__builtin__',
                                                       ast.Load(), None, None),
                                        attr="list",
                                        ctx=ast.Load()),
                     args=[Placeholder(0)],
                     keywords=[])
        ],
        keywords=[])

    @staticmethod
    def sub():
        return ast.Call(func=ast.Attribute(value=ast.Name(id='__builtin__',
                                                          ctx=ast.Load(),
                                                          annotation=None,
                                                          type_comment=None),
                                           attr="tuple",
                                           ctx=ast.Load()),
                        args=[Placeholder(0)],
                        keywords=[])
Esempio n. 8
0
class AbsSqrPattern(Pattern):
    # __builtin__.abs(X) ** 2 => __builtin__.pythran.abssqr(X)

    pattern = ast.Call(func=ast.Attribute(value=ast.Name(id=mangle('numpy'),
                                                         ctx=ast.Load(),
                                                         annotation=None,
                                                         type_comment=None),
                                          attr="square",
                                          ctx=ast.Load()),
                       args=[
                           ast.Call(func=ast.Attribute(value=ast.Name(
                               id='__builtin__',
                               ctx=ast.Load(),
                               annotation=None,
                               type_comment=None),
                                                       attr="abs",
                                                       ctx=ast.Load()),
                                    args=[Placeholder(0)],
                                    keywords=[])
                       ],
                       keywords=[])

    @staticmethod
    def sub():
        return ast.Call(func=ast.Attribute(value=ast.Attribute(value=ast.Name(
            id='__builtin__',
            ctx=ast.Load(),
            annotation=None,
            type_comment=None),
                                                               attr="pythran",
                                                               ctx=ast.Load()),
                                           attr="abssqr",
                                           ctx=ast.Load()),
                        args=[Placeholder(0)],
                        keywords=[])
Esempio n. 9
0
 def sub():
     return ast.Call(
         func=ast.Attribute(value=ast.Name(id='__builtin__',
                                           ctx=ast.Load(),
                                           annotation=None,
                                           type_comment=None),
                            attr="tuple", ctx=ast.Load()),
         args=[Placeholder(0)], keywords=[])
Esempio n. 10
0
 def sub():
     return ast.Call(
         func=ast.Attribute(value=ast.Name(id=mangle('numpy'),
                                           ctx=ast.Load(),
                                           annotation=None,
                                           type_comment=None),
                            attr="cbrt", ctx=ast.Load()),
         args=[Placeholder(0)], keywords=[])
Esempio n. 11
0
 def sub():
     return ast.Call(func=ast.Attribute(value=ast.Name(
         'builtins', ast.Load(), None, None),
                                        attr="max",
                                        ctx=ast.Load()),
                     args=[ast.Constant(0, None),
                           Placeholder(0)],
                     keywords=[])
Esempio n. 12
0
 def sub():
     return ast.Call(
         func=ast.Attribute(
             value=ast.Attribute(value=ast.Name('__builtin__', ast.Load(),
                                                None, None),
                                 attr="pythran", ctx=ast.Load()),
             attr="len_set", ctx=ast.Load()),
         args=[Placeholder(0)], keywords=[])
Esempio n. 13
0
 def sub():
     return ast.Call(func=ast.Attribute(value=ast.Attribute(value=ast.Name(
         id='builtins', ctx=ast.Load(), annotation=None, type_comment=None),
                                                            attr="pythran",
                                                            ctx=ast.Load()),
                                        attr="abssqr",
                                        ctx=ast.Load()),
                     args=[Placeholder(0)],
                     keywords=[])
Esempio n. 14
0
class StrJoinPattern(Pattern):
    # a + "..." + b => "...".join((a, b))
    pattern = ast.BinOp(left=ast.BinOp(left=Placeholder(0),
                                       op=ast.Add(),
                                       right=ast.Constant(
                                           Placeholder(1, str), None)),
                        op=ast.Add(),
                        right=Placeholder(2))

    @staticmethod
    def sub():
        return ast.Call(
            func=ast.Attribute(
                ast.Attribute(ast.Name('__builtin__', ast.Load(), None, None),
                              'str', ast.Load()), 'join', ast.Load()),
            args=[
                ast.Constant(Placeholder(1), None),
                ast.Tuple([Placeholder(0), Placeholder(2)], ast.Load())
            ],
            keywords=[])
Esempio n. 15
0
 def sub():
     return ast.Call(
         func=ast.Attribute(value=ast.Name(id='__builtin__',
                                           ctx=ast.Load(), annotation=None,
                                           type_comment=None),
                            attr=range_name, ctx=ast.Load()),
         args=[ast.BinOp(left=Placeholder(0), op=ast.Sub(),
                         right=ast.Constant(1, None)),
               ast.Constant(-1, None),
               ast.Constant(-1, None)],
         keywords=[])
Esempio n. 16
0
class TuplePattern(Pattern):
    # __builtin__.tuple([X, ..., Z]) => (X, ..., Z)
    pattern = ast.Call(func=ast.Attribute(value=ast.Name(id='__builtin__',
                                                         ctx=ast.Load(),
                                                         annotation=None,
                                                         type_comment=None),
                                          attr="tuple", ctx=ast.Load()),
                       args=[ast.List(Placeholder(0), ast.Load())],
                       keywords=[])

    @staticmethod
    def sub():
        return ast.Tuple(Placeholder(0), ast.Load())
Esempio n. 17
0
class CbrtPattern(Pattern):
    # X ** .33333 => numpy.cbrt(X)
    pattern = ast.BinOp(Placeholder(0), ast.Pow(), ast.Constant(1./3., None))

    @staticmethod
    def sub():
        return ast.Call(
            func=ast.Attribute(value=ast.Name(id=mangle('numpy'),
                                              ctx=ast.Load(),
                                              annotation=None,
                                              type_comment=None),
                               attr="cbrt", ctx=ast.Load()),
            args=[Placeholder(0)], keywords=[])

    extra_imports = [ast.Import([ast.alias('numpy', mangle('numpy'))])]
Esempio n. 18
0
class AbsSqrPatternNumpy(AbsSqrPattern):
    # numpy.abs(X) ** 2 => __builtin__.pythran.abssqr(X)

    pattern = ast.Call(func=ast.Attribute(value=ast.Name(id=mangle('numpy'),
                                                         ctx=ast.Load(),
                                                         annotation=None,
                                                         type_comment=None),
                                          attr="square", ctx=ast.Load()),
                       args=[ast.Call(func=ast.Attribute(
                           value=ast.Name(id=mangle('numpy'),
                                          ctx=ast.Load(),
                                          annotation=None,
                                          type_comment=None),
                           attr="abs",
                           ctx=ast.Load()),
                           args=[Placeholder(0)],
                           keywords=[])],
                       keywords=[])
Esempio n. 19
0
# replacement have to be a lambda function to have a new ast to replace when
# replacement is inserted in main ast
know_pattern = [
    # __builtin__.len(__builtin__.set(X)) => __builtin__.pythran.len_set(X)
    (ast.Call(func=ast.Attribute(value=ast.Name(id='__builtin__',
                                                ctx=ast.Load(),
                                                annotation=None),
                                 attr="len",
                                 ctx=ast.Load()),
              args=[
                  ast.Call(func=ast.Attribute(value=ast.Name(id='__builtin__',
                                                             ctx=ast.Load(),
                                                             annotation=None),
                                              attr="set",
                                              ctx=ast.Load()),
                           args=[Placeholder(0)],
                           keywords=[])
              ],
              keywords=[]),
     lambda: ast.Call(func=ast.Attribute(value=ast.Attribute(value=ast.Name(
         id='__builtin__', ctx=ast.Load(), annotation=None),
                                                             attr="pythran",
                                                             ctx=ast.Load()),
                                         attr="len_set",
                                         ctx=ast.Load()),
                      args=[Placeholder(0)],
                      keywords=[])),

    # __builtin__.abs(X ** 2) => __builtin__.pythran.abssqr(X)
    (ast.Call(func=ast.Attribute(value=ast.Name(id=mangle('numpy'),
                                                ctx=ast.Load(),
Esempio n. 20
0
 def sub():
     return ast.BinOp(Placeholder(0), ast.Pow(), Placeholder(1))
Esempio n. 21
0
 def sub():
     return ast.BinOp(left=Placeholder(0),
                      op=ast.Pow(),
                      right=ast.Constant(2, None))
Esempio n. 22
0
 def sub():
     return ast.Tuple(Placeholder(0), ast.Load())