def transform(tree, stop, **kw): if (isinstance(tree, FunctionDef) or isinstance(tree, ClassDef)): for decorator in tree.decorator_list: if (isinstance(decorator, Name) and (decorator.id == 'jeeves')): raise Exception('Do not use nested @jeeves') if (isinstance(tree, UnaryOp) and isinstance(tree.op, Not)): return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jnot', ctx=Load()), args=[tree.operand], keywords=[], starargs=None, kwargs=None) if isinstance(tree, BoolOp): if isinstance(tree.op, And): fn = Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jand', ctx=Load()) else: fn = Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jor', ctx=Load()) result = tree.values[(-1)] for operand in tree.values[(-2)::(-1)]: result = Call(func=fn, args=[Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=operand), Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=result)], keywords=[], starargs=None, kwargs=None) return result if isinstance(tree, List): elts = [transform.recurse(elt) for elt in tree.elts] newlist = List(elts=elts, ctx=tree.ctx) stop() return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='JList', ctx=Load()), args=[newlist], keywords=[], starargs=None, kwargs=None) if isinstance(tree, IfExp): return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jif', ctx=Load()), args=[tree.test, Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=tree.body), Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=tree.orelse)], keywords=[], starargs=None, kwargs=None) if isinstance(tree, ListComp): elt = tree.elt generators = tree.generators assert (len(generators) == 1) assert (len(generators[0].ifs) == 0) target = common.storeToParam(generators[0].target) iter = generators[0].iter lmbda = Lambda(args=arguments(args=[target], vararg=None, kwarg=None, defaults=[]), body=elt) return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jmap', ctx=Load()), args=[iter, lmbda], keywords=[], starargs=None, kwargs=None) if isinstance(tree, Compare): assert (len(tree.ops) == 1) if isinstance(tree.ops[0], In): return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jhas', ctx=Load()), args=[tree.comparators[0], tree.left], keywords=[], starargs=None, kwargs=None) if isinstance(tree, Call): func = transform.recurse(tree.func) args = [transform.recurse(arg) for arg in tree.args] keywords = [transform.recurse(kw) for kw in tree.keywords] starargs = transform.recurse(tree.starargs) kwargs = transform.recurse(tree.kwargs) stop() return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jfun', ctx=Load()), args=([func] + args), keywords=keywords, starargs=starargs, kwargs=kwargs)
def transform(tree, stop, **kw): if isinstance(tree, If): thn_fn_name = gen_sym() els_fn_name = gen_sym() test = transform.recurse(tree.test) thn_body = transform.recurse(tree.body) els_body = transform.recurse(tree.orelse) stop() def get_func(funcname, funcbody): return FunctionDef(name=funcname, args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=(funcbody or [Pass()]), decorator_list=[]) return [get_func(thn_fn_name, thn_body), get_func(els_fn_name, els_body), Expr(value=Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jif', ctx=Load()), args=[test, Name(id=thn_fn_name, ctx=Load()), Name(id=els_fn_name, ctx=Load())], keywords=[], starargs=None, kwargs=None))] if isinstance(tree, For): body_fn_name = gen_sym() iter = transform.recurse(tree.iter) body = transform.recurse(tree.body) targetParams = common.storeToParam(copy.deepcopy(tree.target)) assert ((len(tree.orelse) == 0) or isinstance(tree.orelse[0], Pass)) stop() func = copy_location(FunctionDef(name=body_fn_name, args=arguments(args=[targetParams], vararg=None, kwarg=None, defaults=[]), body=body, decorator_list=[]), tree) return [func, Expr(value=Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jmap', ctx=Load()), args=[iter, Name(body_fn_name, Load())], keywords=[], starargs=None, kwargs=None))]
def transform(tree, stop, **kw): if isinstance(tree, FunctionDef) or isinstance(tree, ClassDef): for decorator in tree.decorator_list: if isinstance(decorator, Name) and decorator.id == "jeeves": raise Exception("Do not use nested @jeeves") # not expr # JeevesLib.jnot(expr) if isinstance(tree, UnaryOp) and isinstance(tree.op, Not): return q[JeevesLib.jnot(ast[tree.operand])] # a1 and a2 and ... and an # JeevesLib.jand(lambda : left, lambda : right) if isinstance(tree, BoolOp): if isinstance(tree.op, And): fn = q[JeevesLib.jand] else: fn = q[JeevesLib.jor] result = tree.values[-1] for operand in tree.values[-2::-1]: result = q[ast[fn](lambda: ast[operand], lambda: ast[result])] return result if isinstance(tree, List): elts = [transform.recurse(elt) for elt in tree.elts] newlist = List(elts=elts, ctx=tree.ctx) stop() return q[JeevesLib.JList(ast[newlist])] # thn if cond else els # JeevesLib.jif(cond, lambda : thn, lambda : els) if isinstance(tree, IfExp): return q[JeevesLib.jif(ast[tree.test], lambda: ast[tree.body], lambda: ast[tree.orelse])] # [expr for args in iterator] # JeevesLib.jmap(iterator if isinstance(tree, ListComp): elt = tree.elt generators = tree.generators assert len(generators) == 1 assert len(generators[0].ifs) == 0 target = common.storeToParam(generators[0].target) iter = generators[0].iter lmbda = Lambda(args=arguments(args=[target], vararg=None, kwarg=None, defaults=[]), body=elt) return q[JeevesLib.jmap(ast[iter], ast[lmbda])] if isinstance(tree, Compare): assert len(tree.ops) == 1 # TODO other comparisons besides 'in' if isinstance(tree.ops[0], In): return q[JeevesLib.jhas(ast[tree.comparators[0]], ast[tree.left])] # replace f(...) with jfun(f, ...) if isinstance(tree, Call): func = transform.recurse(tree.func) args = [transform.recurse(arg) for arg in tree.args] keywords = [transform.recurse(kw) for kw in tree.keywords] starargs = transform.recurse(tree.starargs) kwargs = transform.recurse(tree.kwargs) stop() return Call(func=q[JeevesLib.jfun], args=[func] + args, keywords=keywords, starargs=starargs, kwargs=kwargs)
def transform(tree, stop, **kw): if isinstance(tree, FunctionDef) or isinstance(tree, ClassDef): for decorator in tree.decorator_list: if isinstance(decorator, Name) and decorator.id == "jeeves": raise Exception("Do not use nested @jeeves") # not expr # JeevesLib.jnot(expr) if isinstance(tree, UnaryOp) and isinstance(tree.op, Not): return q[ JeevesLib.jnot(ast[tree.operand]) ] # a1 and a2 and ... and an # JeevesLib.jand(lambda : left, lambda : right) if isinstance(tree, BoolOp): if isinstance(tree.op, And): fn = q[ JeevesLib.jand ] else: fn = q[ JeevesLib.jor ] result = tree.values[-1] for operand in tree.values[-2::-1]: result = q[ ast[fn](lambda : ast[operand], lambda : ast[result]) ] return result if isinstance(tree, List): elts = [transform.recurse(elt) for elt in tree.elts] newlist = List(elts=elts, ctx=tree.ctx) stop() return q[ JeevesLib.JList(ast[newlist]) ] # thn if cond else els # JeevesLib.jif(cond, lambda : thn, lambda : els) if isinstance(tree, IfExp): return q[ JeevesLib.jif(ast[tree.test], lambda : ast[tree.body], lambda : ast[tree.orelse]) ] # [expr for args in iterator] # JeevesLib.jmap(iterator if isinstance(tree, ListComp): elt = tree.elt generators = tree.generators assert len(generators) == 1 assert len(generators[0].ifs) == 0 target = common.storeToParam(generators[0].target) iter = generators[0].iter lmbda = Lambda( args=arguments( args=[target], vararg=None, kwarg=None, defaults=[] ), body=elt ) return q[ JeevesLib.jmap(ast[iter], ast[lmbda]) ] if isinstance(tree, Compare): assert len(tree.ops) == 1 # TODO other comparisons besides 'in' if isinstance(tree.ops[0], In): return q[ JeevesLib.jhas(ast[tree.comparators[0]], ast[tree.left]) ] # replace f(...) with jfun(f, ...) if isinstance(tree, Call): func = transform.recurse(tree.func) args = [transform.recurse(arg) for arg in tree.args] keywords = [transform.recurse(kw) for kw in tree.keywords] starargs = transform.recurse(tree.starargs) kwargs = transform.recurse(tree.kwargs) stop() return Call( func=q[JeevesLib.jfun], args=[func] + args, keywords=keywords, starargs=starargs, kwargs=kwargs )
def transform(tree, stop, **kw): if (isinstance(tree, FunctionDef) or isinstance(tree, ClassDef)): for decorator in tree.decorator_list: if (isinstance(decorator, Name) and (decorator.id == 'jeeves')): raise Exception('Do not use nested @jeeves') if (isinstance(tree, UnaryOp) and isinstance(tree.op, Not)): return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jnot', ctx=Load()), args=[tree.operand], keywords=[], starargs=None, kwargs=None) if isinstance(tree, BoolOp): if isinstance(tree.op, And): fn = Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jand', ctx=Load()) else: fn = Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jor', ctx=Load()) result = tree.values[(-1)] for operand in tree.values[(-2)::(-1)]: result = Call(func=fn, args=[ Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=operand), Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=result) ], keywords=[], starargs=None, kwargs=None) return result if isinstance(tree, List): elts = [transform.recurse(elt) for elt in tree.elts] newlist = List(elts=elts, ctx=tree.ctx) stop() return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='JList', ctx=Load()), args=[newlist], keywords=[], starargs=None, kwargs=None) if isinstance(tree, IfExp): return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jif', ctx=Load()), args=[ tree.test, Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=tree.body), Lambda(args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=tree.orelse) ], keywords=[], starargs=None, kwargs=None) if isinstance(tree, ListComp): elt = tree.elt generators = tree.generators assert (len(generators) == 1) assert (len(generators[0].ifs) == 0) target = common.storeToParam(generators[0].target) iter = generators[0].iter lmbda = Lambda(args=arguments(args=[target], vararg=None, kwarg=None, defaults=[]), body=elt) return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jmap', ctx=Load()), args=[iter, lmbda], keywords=[], starargs=None, kwargs=None) if isinstance(tree, Compare): assert (len(tree.ops) == 1) if isinstance(tree.ops[0], In): return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jhas', ctx=Load()), args=[tree.comparators[0], tree.left], keywords=[], starargs=None, kwargs=None) if isinstance(tree, Call): func = transform.recurse(tree.func) args = [transform.recurse(arg) for arg in tree.args] keywords = [transform.recurse(kw) for kw in tree.keywords] starargs = transform.recurse(tree.starargs) kwargs = transform.recurse(tree.kwargs) stop() return Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jfun', ctx=Load()), args=([func] + args), keywords=keywords, starargs=starargs, kwargs=kwargs)
def transform(tree, stop, **kw): # If a1,a2,..,an are all the local variables, change # # if condition: # thn_body # else: # els_body # # to # # def thn_fn_name(): # thn_body # def els_fn_name(): # els_body # jif(condition, thn_fn_name, els_fn_name) if isinstance(tree, If): # TODO search over the bodies, and only do this for the variables that # get assigned to. thn_fn_name = gen_sym() els_fn_name = gen_sym() test = transform.recurse(tree.test) thn_body = transform.recurse(tree.body) els_body = transform.recurse(tree.orelse) stop() def get_func(funcname, funcbody): return FunctionDef( name=funcname, args=arguments( args=[], vararg=None, kwarg=None, defaults=[], ), body=funcbody or [Pass()], decorator_list=[], ) return [ get_func(thn_fn_name, thn_body), get_func(els_fn_name, els_body), Expr(value=q[ JeevesLib.jif(ast[test], ast[Name(id=thn_fn_name,ctx=Load())], ast[Name(id=els_fn_name,ctx=Load())], ) ]) ] if isinstance(tree, For): body_fn_name = gen_sym() iter = transform.recurse(tree.iter) body = transform.recurse(tree.body) targetParams = common.storeToParam(copy.deepcopy(tree.target)) assert len(tree.orelse) == 0 or isinstance(tree.orelse[0], Pass) stop() func = copy_location(FunctionDef( name=body_fn_name, args=arguments( args=[targetParams], vararg=None, kwarg=None, defaults=[], ), body=body, decorator_list=[] ), tree) return [ func, Expr(value=q[ JeevesLib.jmap(ast[iter], ast[Name(body_fn_name,Load())]) ]) ]
def transform(tree, stop, **kw): if isinstance(tree, If): thn_fn_name = gen_sym() els_fn_name = gen_sym() test = transform.recurse(tree.test) thn_body = transform.recurse(tree.body) els_body = transform.recurse(tree.orelse) stop() def get_func(funcname, funcbody): return FunctionDef(name=funcname, args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=(funcbody or [Pass()]), decorator_list=[]) return [ get_func(thn_fn_name, thn_body), get_func(els_fn_name, els_body), Expr(value=Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jif', ctx=Load()), args=[ test, Name(id=thn_fn_name, ctx=Load()), Name(id=els_fn_name, ctx=Load()) ], keywords=[], starargs=None, kwargs=None)) ] if isinstance(tree, For): body_fn_name = gen_sym() iter = transform.recurse(tree.iter) body = transform.recurse(tree.body) targetParams = common.storeToParam(copy.deepcopy(tree.target)) assert ((len(tree.orelse) == 0) or isinstance(tree.orelse[0], Pass)) stop() func = copy_location( FunctionDef(name=body_fn_name, args=arguments(args=[targetParams], vararg=None, kwarg=None, defaults=[]), body=body, decorator_list=[]), tree) return [ func, Expr(value=Call(func=Attribute(value=Name(id='JeevesLib', ctx=Load()), attr='jmap', ctx=Load()), args=[iter, Name(body_fn_name, Load())], keywords=[], starargs=None, kwargs=None)) ]
def transform(tree, stop, **kw): # If a1,a2,..,an are all the local variables, change # # if condition: # thn_body # else: # els_body # # to # # def thn_fn_name(): # thn_body # def els_fn_name(): # els_body # jif(condition, thn_fn_name, els_fn_name) if isinstance(tree, If): # TODO search over the bodies, and only do this for the variables that # get assigned to. thn_fn_name = gen_sym() els_fn_name = gen_sym() test = transform.recurse(tree.test) thn_body = transform.recurse(tree.body) els_body = transform.recurse(tree.orelse) stop() def get_func(funcname, funcbody): return FunctionDef( name=funcname, args=arguments( args=[], vararg=None, kwarg=None, defaults=[], ), body=funcbody or [Pass()], decorator_list=[], ) return [ get_func(thn_fn_name, thn_body), get_func(els_fn_name, els_body), Expr(value=q[JeevesLib.jif( ast[test], ast[Name(id=thn_fn_name, ctx=Load())], ast[Name(id=els_fn_name, ctx=Load())], )]) ] if isinstance(tree, For): body_fn_name = gen_sym() iter = transform.recurse(tree.iter) body = transform.recurse(tree.body) targetParams = common.storeToParam(copy.deepcopy(tree.target)) assert len(tree.orelse) == 0 or isinstance(tree.orelse[0], Pass) stop() func = copy_location( FunctionDef(name=body_fn_name, args=arguments( args=[targetParams], vararg=None, kwarg=None, defaults=[], ), body=body, decorator_list=[]), tree) return [ func, Expr(value=q[JeevesLib.jmap(ast[iter], ast[Name( body_fn_name, Load())])]) ]