Пример #1
0
 def replace_tc_pos(node):
     with switch(node):
         if Expr(value=Call(
                 func=func,
                 args=args,
                 starargs=starargs,
                 kwargs=kwargs)):
             if starargs:
                 with hq as code:
                 # get rid of starargs
                     return (TcoIgnore,
                             ast[func],
                             ast[List(args, Load())] + list(ast[starargs]),
                             ast[kwargs or Dict([],[])])
             else:
                 with hq as code:
                     return (TcoIgnore,
                             ast[func],
                             ast[List(args, Load())],
                             ast[kwargs or Dict([], [])])
             return code
         elif If(test=test, body=body, orelse=orelse):
             body[-1] = replace_tc_pos(body[-1])
             if orelse:
                 orelse[-1] = replace_tc_pos(orelse[-1])
             return If(test, body, orelse)
         else:
             return node
Пример #2
0
 def test_switch(self):
     branch_reached = -1
     with switch(Bar(6)):
         if Bar(5):
             branch_reached = 1
         else:
             branch_reached = 2
     self.assertEquals(branch_reached, 2)
Пример #3
0
 def return_replacer(tree, **kw):
     with switch(tree):
         if Return(value=Call(
                 func=func, args=args, starargs=starargs, kwargs=kwargs)):
             with q as code:
                 return (tco.CALL, ast(func), ast(List(args, Load())),
                         ast(starargs or List([], Load())),
                         ast(kwargs or Dict([], [])))
             return code
         else:
             return tree
Пример #4
0
 def replace_tc_pos(node):
     with switch(node):
         if Expr(value=Call(
                 func=func, args=args, starargs=starargs, kwargs=kwargs)):
             with q as code:
                 return (tco.IGNORE, ast(func), ast(List(args, Load())),
                         ast(starargs or List([], Load())),
                         ast(kwargs or Dict([], [])))
             return code
         elif If(test=test, body=body, orelse=orelse):
             body[-1] = replace_tc_pos(body[-1])
             if orelse:
                 orelse[-1] = replace_tc_pos(orelse[-1])
             return If(test, body, orelse)
         else:
             return node
Пример #5
0
 def return_replacer(tree, **kw):
     with switch(tree):
         if Return(value=Call(
                 func=func, 
                 args=args, 
                 starargs=starargs, 
                 kwargs=kwargs)):
             with q as code:
                 return (tco.CALL,
                         ast(func),
                         ast(List(args, Load())),
                         ast(starargs or List([], Load())),
                         ast(kwargs or Dict([],[])))
             return code
         else:
             return tree
Пример #6
0
 def test_instance_checking(self):
     blah = Baz(5)
     branch_reached = -1
     reached_end = False
     with switch(blah):
         if Foo(lol, wat):
             branch_reached = 1
         elif Bar(4):
             branch_reached = 2
         elif Baz(x):
             branch_reached = 3
             self.assertEquals(5, x)
         self.assertEquals(8, 1 << 3)
         reached_end=True
     self.assertTrue(reached_end)
     self.assertEquals(3, branch_reached)
Пример #7
0
def Call_issubclass(t, x):
    """Translate ``issubclass(Foo, Bar)`` to ``Foo.prototype instanceof Bar``.
    """
    with switch(x):
        if ast.Call(func=ast.Name(id='issubclass'), args=[target, classes]):
            tproto = q[ast_literal[target].prototype]
            if isinstance(classes, (ast.Tuple, ast.List, ast.Set)):
                classes = classes.elts
            else:
                classes = [classes]
            prev = None
            for c in classes:
                cur = q[ast_literal[c].prototype.isPrototypeOf(
                    ast_literal[tproto])]
                if prev is not None:
                    cur = q[ast_literal[prev] or ast_literal[cur]]
                prev = cur
            return JSExpressionStatement(cur)
Пример #8
0
def Call_issubclass(t, x):
    """Translate ``issubclass(Foo, Bar)`` to ``Foo.prototype instanceof Bar``.
    """
    with switch(x):
        if ast.Call(func=ast.Name(id='issubclass'), args=[target, classes]):
            tproto = q[ast_literal[target].prototype]
            if isinstance(classes, (ast.Tuple, ast.List, ast.Set)):
                classes = classes.elts
            else:
                classes = [classes]
            prev = None
            for c in classes:
                cur = q[ast_literal[c].prototype.isPrototypeOf(
                    ast_literal[tproto])]
                if prev is not None:
                    cur = q[ast_literal[prev] or ast_literal[cur]]
                prev = cur
            return JSExpressionStatement(cur)
Пример #9
0
    def return_replacer(tree, **kw):
        with switch(tree):
            if Return(value=Call(
                    func=func, args=args, starargs=starargs, kwargs=kwargs)):
                if starargs:
                    with hq as code:
                        # get rid of starargs
                        return (TcoCall, ast[func],
                                ast[List(args, Load())] + list(ast[starargs]),
                                ast[kwargs or Dict([], [])])
                else:
                    with hq as code:
                        return (TcoCall, ast[func], ast[List(args, Load())],
                                ast[kwargs or Dict([], [])])

                return code
            else:
                return tree
Пример #10
0
    def return_replacer(tree, **kw):
        with switch(tree):
            if ast.Return(value=ast.Call(
                    func=func, args=args, starargs=starargs, kwargs=kwargs)):
                if starargs:
                    with hq as code:
                        # get rid of starargs
                        return (TcoCall, ast_literal[func],
                                ast_literal[ast.List(args, ast.Load())] +
                                list(ast_literal[starargs]),
                                ast_literal[kwargs or ast.Dict([], [])])
                else:
                    with hq as code:
                        return (TcoCall, ast_literal[func],
                                ast_literal[ast.List(args, ast.Load())],
                                ast_literal[kwargs or ast.Dict([], [])])

                return code
            else:
                return tree
Пример #11
0
 def replace_tc_pos(node):
     with switch(node):
         if Expr(value=Call(
                 func=func,
                 args=args,
                 starargs=starargs,
                 kwargs=kwargs)):
             with q as code:
                 return (tco.IGNORE,
                         ast(func),
                         ast(List(args, Load())),
                         ast(starargs or List([], Load())),
                         ast(kwargs or Dict([], [])))
             return code
         elif If(test=test, body=body, orelse=orelse):
             body[-1] = replace_tc_pos(body[-1])
             if orelse:
                 orelse[-1] = replace_tc_pos(orelse[-1])
             return If(test, body, orelse)
         else:
             return node
Пример #12
0
 def replace_tc_pos(node):
     with switch(node):
         if Expr(value=Call(
                 func=func, args=args, starargs=starargs, kwargs=kwargs)):
             if starargs:
                 with hq as code:
                     # get rid of starargs
                     return (TcoIgnore, ast[func],
                             ast[List(args, Load())] + list(ast[starargs]),
                             ast[kwargs or Dict([], [])])
             else:
                 with hq as code:
                     return (TcoIgnore, ast[func], ast[List(args, Load())],
                             ast[kwargs or Dict([], [])])
             return code
         elif If(test=test, body=body, orelse=orelse):
             body[-1] = replace_tc_pos(body[-1])
             if orelse:
                 orelse[-1] = replace_tc_pos(orelse[-1])
             return If(test, body, orelse)
         else:
             return node
Пример #13
0
    def return_replacer(tree, **kw):
        with switch(tree):
            if Return(value=Call(
                    func=func, 
                    args=args, 
                    starargs=starargs, 
                    kwargs=kwargs)):
                if starargs:
                    with hq as code:
                    # get rid of starargs
                        return (TcoCall,
                                ast[func],
                                ast[List(args, Load())] + list(ast[starargs]),
                                ast[kwargs or Dict([],[])])
                else:
                    with hq as code:
                        return (TcoCall,
                                ast[func],
                                ast[List(args, Load())],
                                ast[kwargs or Dict([], [])])

                return code
            else:
                return tree
Пример #14
0
 def evenLength(xs):
     with switch(xs):
         if Nil():
             return True
         else:
             return oddLength(xs.rest)
Пример #15
0
 def oddLength(xs):
     with switch(xs):
         if Nil():
             return False
         else:
             return evenLength(xs.rest)
Пример #16
0
 def oddLength(xs):
     with switch(xs):
         if Nil():
             return False
         else:
             return evenLength(xs.rest)
Пример #17
0
 def evenLength(xs):
     with switch(xs):
         if Nil():
             return True
         else:
             return oddLength(xs.rest)