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
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)
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
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
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
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)
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)
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)
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
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
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
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
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
def evenLength(xs): with switch(xs): if Nil(): return True else: return oddLength(xs.rest)
def oddLength(xs): with switch(xs): if Nil(): return False else: return evenLength(xs.rest)
def oddLength(xs): with switch(xs): if Nil(): return False else: return evenLength(xs.rest)
def evenLength(xs): with switch(xs): if Nil(): return True else: return oddLength(xs.rest)